2 * drivers/base/node.c - basic Node class support
5 #include <linux/sysdev.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
9 #include <linux/node.h>
10 #include <linux/hugetlb.h>
11 #include <linux/cpumask.h>
12 #include <linux/topology.h>
13 #include <linux/nodemask.h>
14 #include <linux/cpu.h>
15 #include <linux/device.h>
17 static struct sysdev_class node_class = {
22 static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
24 struct node *node_dev = to_node(dev);
25 node_to_cpumask_ptr(mask, node_dev->sysdev.id);
28 /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
29 BUILD_BUG_ON(MAX_NUMNODES/4 > PAGE_SIZE/2);
31 len = cpumask_scnprintf(buf, PAGE_SIZE-2, *mask);
37 static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
39 #define K(x) ((x) << (PAGE_SHIFT - 10))
40 static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
46 si_meminfo_node(&i, nid);
49 "Node %d MemTotal: %8lu kB\n"
50 "Node %d MemFree: %8lu kB\n"
51 "Node %d MemUsed: %8lu kB\n"
52 "Node %d Active: %8lu kB\n"
53 "Node %d Inactive: %8lu kB\n"
55 "Node %d HighTotal: %8lu kB\n"
56 "Node %d HighFree: %8lu kB\n"
57 "Node %d LowTotal: %8lu kB\n"
58 "Node %d LowFree: %8lu kB\n"
60 "Node %d Dirty: %8lu kB\n"
61 "Node %d Writeback: %8lu kB\n"
62 "Node %d FilePages: %8lu kB\n"
63 "Node %d Mapped: %8lu kB\n"
64 "Node %d AnonPages: %8lu kB\n"
65 "Node %d PageTables: %8lu kB\n"
66 "Node %d NFS_Unstable: %8lu kB\n"
67 "Node %d Bounce: %8lu kB\n"
68 "Node %d Slab: %8lu kB\n"
69 "Node %d SReclaimable: %8lu kB\n"
70 "Node %d SUnreclaim: %8lu kB\n",
73 nid, K(i.totalram - i.freeram),
74 nid, node_page_state(nid, NR_ACTIVE),
75 nid, node_page_state(nid, NR_INACTIVE),
79 nid, K(i.totalram - i.totalhigh),
80 nid, K(i.freeram - i.freehigh),
82 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
83 nid, K(node_page_state(nid, NR_WRITEBACK)),
84 nid, K(node_page_state(nid, NR_FILE_PAGES)),
85 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
86 nid, K(node_page_state(nid, NR_ANON_PAGES)),
87 nid, K(node_page_state(nid, NR_PAGETABLE)),
88 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
89 nid, K(node_page_state(nid, NR_BOUNCE)),
90 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
91 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
92 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
93 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
94 n += hugetlb_report_node_meminfo(nid, buf + n);
99 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
101 static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
107 "interleave_hit %lu\n"
110 node_page_state(dev->id, NUMA_HIT),
111 node_page_state(dev->id, NUMA_MISS),
112 node_page_state(dev->id, NUMA_FOREIGN),
113 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
114 node_page_state(dev->id, NUMA_LOCAL),
115 node_page_state(dev->id, NUMA_OTHER));
117 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
119 static ssize_t node_read_distance(struct sys_device * dev, char * buf)
125 /* buf currently PAGE_SIZE, need ~4 chars per node */
126 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
128 for_each_online_node(i)
129 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
131 len += sprintf(buf + len, "\n");
134 static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
138 * register_node - Setup a sysfs device for a node.
139 * @num - Node number to use when creating the device.
141 * Initialize and register the node device.
143 int register_node(struct node *node, int num, struct node *parent)
147 node->sysdev.id = num;
148 node->sysdev.cls = &node_class;
149 error = sysdev_register(&node->sysdev);
152 sysdev_create_file(&node->sysdev, &attr_cpumap);
153 sysdev_create_file(&node->sysdev, &attr_meminfo);
154 sysdev_create_file(&node->sysdev, &attr_numastat);
155 sysdev_create_file(&node->sysdev, &attr_distance);
161 * unregister_node - unregister a node device
162 * @node: node going away
164 * Unregisters a node device @node. All the devices on the node must be
165 * unregistered before calling this function.
167 void unregister_node(struct node *node)
169 sysdev_remove_file(&node->sysdev, &attr_cpumap);
170 sysdev_remove_file(&node->sysdev, &attr_meminfo);
171 sysdev_remove_file(&node->sysdev, &attr_numastat);
172 sysdev_remove_file(&node->sysdev, &attr_distance);
174 sysdev_unregister(&node->sysdev);
177 struct node node_devices[MAX_NUMNODES];
180 * register cpu under node
182 int register_cpu_under_node(unsigned int cpu, unsigned int nid)
184 if (node_online(nid)) {
185 struct sys_device *obj = get_cpu_sysdev(cpu);
188 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
190 kobject_name(&obj->kobj));
196 int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
198 if (node_online(nid)) {
199 struct sys_device *obj = get_cpu_sysdev(cpu);
201 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
202 kobject_name(&obj->kobj));
207 int register_one_node(int nid)
212 if (node_online(nid)) {
213 int p_node = parent_node(nid);
214 struct node *parent = NULL;
217 parent = &node_devices[p_node];
219 error = register_node(&node_devices[nid], nid, parent);
221 /* link cpu under this node */
222 for_each_present_cpu(cpu) {
223 if (cpu_to_node(cpu) == nid)
224 register_cpu_under_node(cpu, nid);
232 void unregister_one_node(int nid)
234 unregister_node(&node_devices[nid]);
238 * node states attributes
241 static ssize_t print_nodes_state(enum node_states state, char *buf)
245 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
246 if (n > 0 && PAGE_SIZE > n + 1) {
253 static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
255 return print_nodes_state(N_POSSIBLE, buf);
258 static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
260 return print_nodes_state(N_ONLINE, buf);
263 static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
266 return print_nodes_state(N_NORMAL_MEMORY, buf);
269 static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
271 return print_nodes_state(N_CPU, buf);
274 static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
275 static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
276 static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
278 static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
280 #ifdef CONFIG_HIGHMEM
281 static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
284 return print_nodes_state(N_HIGH_MEMORY, buf);
287 static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
291 struct sysdev_class_attribute *node_state_attr[] = {
294 &attr_has_normal_memory,
295 #ifdef CONFIG_HIGHMEM
296 &attr_has_high_memory,
301 static int node_states_init(void)
306 for (i = 0; i < NR_NODE_STATES; i++) {
308 ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
315 static int __init register_node_type(void)
319 ret = sysdev_class_register(&node_class);
321 ret = node_states_init();
324 * Note: we're not going to unregister the node class if we fail
325 * to register the node state class attribute files.
329 postcore_initcall(register_node_type);