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 = {
18 set_kset_name("node"),
22 static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
24 struct node *node_dev = to_node(dev);
25 cpumask_t mask = node_to_cpumask(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-1, mask);
32 len += sprintf(buf + len, "\n");
36 static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
38 #define K(x) ((x) << (PAGE_SHIFT - 10))
39 static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
45 si_meminfo_node(&i, nid);
48 "Node %d MemTotal: %8lu kB\n"
49 "Node %d MemFree: %8lu kB\n"
50 "Node %d MemUsed: %8lu kB\n"
51 "Node %d Active: %8lu kB\n"
52 "Node %d Inactive: %8lu kB\n"
54 "Node %d HighTotal: %8lu kB\n"
55 "Node %d HighFree: %8lu kB\n"
56 "Node %d LowTotal: %8lu kB\n"
57 "Node %d LowFree: %8lu kB\n"
59 "Node %d Dirty: %8lu kB\n"
60 "Node %d Writeback: %8lu kB\n"
61 "Node %d FilePages: %8lu kB\n"
62 "Node %d Mapped: %8lu kB\n"
63 "Node %d AnonPages: %8lu kB\n"
64 "Node %d PageTables: %8lu kB\n"
65 "Node %d NFS_Unstable: %8lu kB\n"
66 "Node %d Bounce: %8lu kB\n"
67 "Node %d Slab: %8lu kB\n"
68 "Node %d SReclaimable: %8lu kB\n"
69 "Node %d SUnreclaim: %8lu kB\n",
72 nid, K(i.totalram - i.freeram),
73 nid, node_page_state(nid, NR_ACTIVE),
74 nid, node_page_state(nid, NR_INACTIVE),
78 nid, K(i.totalram - i.totalhigh),
79 nid, K(i.freeram - i.freehigh),
81 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
82 nid, K(node_page_state(nid, NR_WRITEBACK)),
83 nid, K(node_page_state(nid, NR_FILE_PAGES)),
84 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
85 nid, K(node_page_state(nid, NR_ANON_PAGES)),
86 nid, K(node_page_state(nid, NR_PAGETABLE)),
87 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
88 nid, K(node_page_state(nid, NR_BOUNCE)),
89 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
90 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
91 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
92 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
93 n += hugetlb_report_node_meminfo(nid, buf + n);
98 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
100 static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
106 "interleave_hit %lu\n"
109 node_page_state(dev->id, NUMA_HIT),
110 node_page_state(dev->id, NUMA_MISS),
111 node_page_state(dev->id, NUMA_FOREIGN),
112 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
113 node_page_state(dev->id, NUMA_LOCAL),
114 node_page_state(dev->id, NUMA_OTHER));
116 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
118 static ssize_t node_read_distance(struct sys_device * dev, char * buf)
124 /* buf currently PAGE_SIZE, need ~4 chars per node */
125 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
127 for_each_online_node(i)
128 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
130 len += sprintf(buf + len, "\n");
133 static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
137 * register_node - Setup a sysfs device for a node.
138 * @num - Node number to use when creating the device.
140 * Initialize and register the node device.
142 int register_node(struct node *node, int num, struct node *parent)
146 node->sysdev.id = num;
147 node->sysdev.cls = &node_class;
148 error = sysdev_register(&node->sysdev);
151 sysdev_create_file(&node->sysdev, &attr_cpumap);
152 sysdev_create_file(&node->sysdev, &attr_meminfo);
153 sysdev_create_file(&node->sysdev, &attr_numastat);
154 sysdev_create_file(&node->sysdev, &attr_distance);
160 * unregister_node - unregister a node device
161 * @node: node going away
163 * Unregisters a node device @node. All the devices on the node must be
164 * unregistered before calling this function.
166 void unregister_node(struct node *node)
168 sysdev_remove_file(&node->sysdev, &attr_cpumap);
169 sysdev_remove_file(&node->sysdev, &attr_meminfo);
170 sysdev_remove_file(&node->sysdev, &attr_numastat);
171 sysdev_remove_file(&node->sysdev, &attr_distance);
173 sysdev_unregister(&node->sysdev);
176 struct node node_devices[MAX_NUMNODES];
179 * register cpu under node
181 int register_cpu_under_node(unsigned int cpu, unsigned int nid)
183 if (node_online(nid)) {
184 struct sys_device *obj = get_cpu_sysdev(cpu);
187 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
189 kobject_name(&obj->kobj));
195 int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
197 if (node_online(nid)) {
198 struct sys_device *obj = get_cpu_sysdev(cpu);
200 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
201 kobject_name(&obj->kobj));
206 int register_one_node(int nid)
211 if (node_online(nid)) {
212 int p_node = parent_node(nid);
213 struct node *parent = NULL;
216 parent = &node_devices[p_node];
218 error = register_node(&node_devices[nid], nid, parent);
220 /* link cpu under this node */
221 for_each_present_cpu(cpu) {
222 if (cpu_to_node(cpu) == nid)
223 register_cpu_under_node(cpu, nid);
231 void unregister_one_node(int nid)
233 unregister_node(&node_devices[nid]);
237 * node states attributes
240 static ssize_t print_nodes_state(enum node_states state, char *buf)
244 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
245 if (n > 0 && PAGE_SIZE > n + 1) {
252 static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
254 return print_nodes_state(N_POSSIBLE, buf);
257 static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
259 return print_nodes_state(N_ONLINE, buf);
262 static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
265 return print_nodes_state(N_NORMAL_MEMORY, buf);
268 static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
270 return print_nodes_state(N_CPU, buf);
273 static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
274 static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
275 static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
277 static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
279 #ifdef CONFIG_HIGHMEM
280 static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
283 return print_nodes_state(N_HIGH_MEMORY, buf);
286 static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
290 struct sysdev_class_attribute *node_state_attr[] = {
293 &attr_has_normal_memory,
294 #ifdef CONFIG_HIGHMEM
295 &attr_has_high_memory,
300 static int node_states_init(void)
305 for (i = 0; i < NR_NODE_STATES; i++) {
307 ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
314 static int __init register_node_type(void)
318 ret = sysdev_class_register(&node_class);
320 ret = node_states_init();
323 * Note: we're not going to unregister the node class if we fail
324 * to register the node state class attribute files.
328 postcore_initcall(register_node_type);