vmscan: split LRU lists into anon & file sets
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / node.c
blobfc7e9bf0cdbcd0294119ca4d21b6b6a5ed8fe025
1 /*
2 * drivers/base/node.c - basic Node class support
3 */
5 #include <linux/sysdev.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/mm.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 .name = "node",
22 static ssize_t node_read_cpumap(struct sys_device *dev, int type, char *buf)
24 struct node *node_dev = to_node(dev);
25 node_to_cpumask_ptr(mask, node_dev->sysdev.id);
26 int len;
28 /* 2008/04/07: buf currently PAGE_SIZE, need 9 chars per 32 bits. */
29 BUILD_BUG_ON((NR_CPUS/32 * 9) > (PAGE_SIZE-1));
31 len = type?
32 cpulist_scnprintf(buf, PAGE_SIZE-2, *mask):
33 cpumask_scnprintf(buf, PAGE_SIZE-2, *mask);
34 buf[len++] = '\n';
35 buf[len] = '\0';
36 return len;
39 static inline ssize_t node_read_cpumask(struct sys_device *dev,
40 struct sysdev_attribute *attr, char *buf)
42 return node_read_cpumap(dev, 0, buf);
44 static inline ssize_t node_read_cpulist(struct sys_device *dev,
45 struct sysdev_attribute *attr, char *buf)
47 return node_read_cpumap(dev, 1, buf);
50 static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumask, NULL);
51 static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
53 #define K(x) ((x) << (PAGE_SHIFT - 10))
54 static ssize_t node_read_meminfo(struct sys_device * dev,
55 struct sysdev_attribute *attr, char * buf)
57 int n;
58 int nid = dev->id;
59 struct sysinfo i;
61 si_meminfo_node(&i, nid);
63 n = sprintf(buf, "\n"
64 "Node %d MemTotal: %8lu kB\n"
65 "Node %d MemFree: %8lu kB\n"
66 "Node %d MemUsed: %8lu kB\n"
67 "Node %d Active: %8lu kB\n"
68 "Node %d Inactive: %8lu kB\n"
69 "Node %d Active(anon): %8lu kB\n"
70 "Node %d Inactive(anon): %8lu kB\n"
71 "Node %d Active(file): %8lu kB\n"
72 "Node %d Inactive(file): %8lu kB\n"
73 #ifdef CONFIG_HIGHMEM
74 "Node %d HighTotal: %8lu kB\n"
75 "Node %d HighFree: %8lu kB\n"
76 "Node %d LowTotal: %8lu kB\n"
77 "Node %d LowFree: %8lu kB\n"
78 #endif
79 "Node %d Dirty: %8lu kB\n"
80 "Node %d Writeback: %8lu kB\n"
81 "Node %d FilePages: %8lu kB\n"
82 "Node %d Mapped: %8lu kB\n"
83 "Node %d AnonPages: %8lu kB\n"
84 "Node %d PageTables: %8lu kB\n"
85 "Node %d NFS_Unstable: %8lu kB\n"
86 "Node %d Bounce: %8lu kB\n"
87 "Node %d WritebackTmp: %8lu kB\n"
88 "Node %d Slab: %8lu kB\n"
89 "Node %d SReclaimable: %8lu kB\n"
90 "Node %d SUnreclaim: %8lu kB\n",
91 nid, K(i.totalram),
92 nid, K(i.freeram),
93 nid, K(i.totalram - i.freeram),
94 nid, K(node_page_state(nid, NR_ACTIVE_ANON) +
95 node_page_state(nid, NR_ACTIVE_FILE)),
96 nid, K(node_page_state(nid, NR_INACTIVE_ANON) +
97 node_page_state(nid, NR_INACTIVE_FILE)),
98 nid, K(node_page_state(nid, NR_ACTIVE_ANON)),
99 nid, K(node_page_state(nid, NR_INACTIVE_ANON)),
100 nid, K(node_page_state(nid, NR_ACTIVE_FILE)),
101 nid, K(node_page_state(nid, NR_INACTIVE_FILE)),
102 #ifdef CONFIG_HIGHMEM
103 nid, K(i.totalhigh),
104 nid, K(i.freehigh),
105 nid, K(i.totalram - i.totalhigh),
106 nid, K(i.freeram - i.freehigh),
107 #endif
108 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
109 nid, K(node_page_state(nid, NR_WRITEBACK)),
110 nid, K(node_page_state(nid, NR_FILE_PAGES)),
111 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
112 nid, K(node_page_state(nid, NR_ANON_PAGES)),
113 nid, K(node_page_state(nid, NR_PAGETABLE)),
114 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
115 nid, K(node_page_state(nid, NR_BOUNCE)),
116 nid, K(node_page_state(nid, NR_WRITEBACK_TEMP)),
117 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
118 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
119 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
120 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
121 n += hugetlb_report_node_meminfo(nid, buf + n);
122 return n;
125 #undef K
126 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
128 static ssize_t node_read_numastat(struct sys_device * dev,
129 struct sysdev_attribute *attr, char * buf)
131 return sprintf(buf,
132 "numa_hit %lu\n"
133 "numa_miss %lu\n"
134 "numa_foreign %lu\n"
135 "interleave_hit %lu\n"
136 "local_node %lu\n"
137 "other_node %lu\n",
138 node_page_state(dev->id, NUMA_HIT),
139 node_page_state(dev->id, NUMA_MISS),
140 node_page_state(dev->id, NUMA_FOREIGN),
141 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
142 node_page_state(dev->id, NUMA_LOCAL),
143 node_page_state(dev->id, NUMA_OTHER));
145 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
147 static ssize_t node_read_distance(struct sys_device * dev,
148 struct sysdev_attribute *attr, char * buf)
150 int nid = dev->id;
151 int len = 0;
152 int i;
154 /* buf currently PAGE_SIZE, need ~4 chars per node */
155 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
157 for_each_online_node(i)
158 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
160 len += sprintf(buf + len, "\n");
161 return len;
163 static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
167 * register_node - Setup a sysfs device for a node.
168 * @num - Node number to use when creating the device.
170 * Initialize and register the node device.
172 int register_node(struct node *node, int num, struct node *parent)
174 int error;
176 node->sysdev.id = num;
177 node->sysdev.cls = &node_class;
178 error = sysdev_register(&node->sysdev);
180 if (!error){
181 sysdev_create_file(&node->sysdev, &attr_cpumap);
182 sysdev_create_file(&node->sysdev, &attr_cpulist);
183 sysdev_create_file(&node->sysdev, &attr_meminfo);
184 sysdev_create_file(&node->sysdev, &attr_numastat);
185 sysdev_create_file(&node->sysdev, &attr_distance);
187 return error;
191 * unregister_node - unregister a node device
192 * @node: node going away
194 * Unregisters a node device @node. All the devices on the node must be
195 * unregistered before calling this function.
197 void unregister_node(struct node *node)
199 sysdev_remove_file(&node->sysdev, &attr_cpumap);
200 sysdev_remove_file(&node->sysdev, &attr_cpulist);
201 sysdev_remove_file(&node->sysdev, &attr_meminfo);
202 sysdev_remove_file(&node->sysdev, &attr_numastat);
203 sysdev_remove_file(&node->sysdev, &attr_distance);
205 sysdev_unregister(&node->sysdev);
208 struct node node_devices[MAX_NUMNODES];
211 * register cpu under node
213 int register_cpu_under_node(unsigned int cpu, unsigned int nid)
215 if (node_online(nid)) {
216 struct sys_device *obj = get_cpu_sysdev(cpu);
217 if (!obj)
218 return 0;
219 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
220 &obj->kobj,
221 kobject_name(&obj->kobj));
224 return 0;
227 int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
229 if (node_online(nid)) {
230 struct sys_device *obj = get_cpu_sysdev(cpu);
231 if (obj)
232 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
233 kobject_name(&obj->kobj));
235 return 0;
238 int register_one_node(int nid)
240 int error = 0;
241 int cpu;
243 if (node_online(nid)) {
244 int p_node = parent_node(nid);
245 struct node *parent = NULL;
247 if (p_node != nid)
248 parent = &node_devices[p_node];
250 error = register_node(&node_devices[nid], nid, parent);
252 /* link cpu under this node */
253 for_each_present_cpu(cpu) {
254 if (cpu_to_node(cpu) == nid)
255 register_cpu_under_node(cpu, nid);
259 return error;
263 void unregister_one_node(int nid)
265 unregister_node(&node_devices[nid]);
269 * node states attributes
272 static ssize_t print_nodes_state(enum node_states state, char *buf)
274 int n;
276 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
277 if (n > 0 && PAGE_SIZE > n + 1) {
278 *(buf + n++) = '\n';
279 *(buf + n++) = '\0';
281 return n;
284 static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
286 return print_nodes_state(N_POSSIBLE, buf);
289 static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
291 return print_nodes_state(N_ONLINE, buf);
294 static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
295 char *buf)
297 return print_nodes_state(N_NORMAL_MEMORY, buf);
300 static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
302 return print_nodes_state(N_CPU, buf);
305 static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
306 static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
307 static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
308 NULL);
309 static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
311 #ifdef CONFIG_HIGHMEM
312 static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
313 char *buf)
315 return print_nodes_state(N_HIGH_MEMORY, buf);
318 static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
319 NULL);
320 #endif
322 struct sysdev_class_attribute *node_state_attr[] = {
323 &attr_possible,
324 &attr_online,
325 &attr_has_normal_memory,
326 #ifdef CONFIG_HIGHMEM
327 &attr_has_high_memory,
328 #endif
329 &attr_has_cpu,
332 static int node_states_init(void)
334 int i;
335 int err = 0;
337 for (i = 0; i < NR_NODE_STATES; i++) {
338 int ret;
339 ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
340 if (!err)
341 err = ret;
343 return err;
346 static int __init register_node_type(void)
348 int ret;
350 ret = sysdev_class_register(&node_class);
351 if (!ret)
352 ret = node_states_init();
355 * Note: we're not going to unregister the node class if we fail
356 * to register the node state class attribute files.
358 return ret;
360 postcore_initcall(register_node_type);