vmscan: split LRU lists into anon & file sets
[safe/jmp/linux-2.6] / drivers / base / node.c
1 /*
2  * drivers/base/node.c - basic Node class support
3  */
4
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>
16
17 static struct sysdev_class node_class = {
18         .name = "node",
19 };
20
21
22 static ssize_t node_read_cpumap(struct sys_device *dev, int type, char *buf)
23 {
24         struct node *node_dev = to_node(dev);
25         node_to_cpumask_ptr(mask, node_dev->sysdev.id);
26         int len;
27
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));
30
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;
37 }
38
39 static inline ssize_t node_read_cpumask(struct sys_device *dev,
40                                 struct sysdev_attribute *attr, char *buf)
41 {
42         return node_read_cpumap(dev, 0, buf);
43 }
44 static inline ssize_t node_read_cpulist(struct sys_device *dev,
45                                 struct sysdev_attribute *attr, char *buf)
46 {
47         return node_read_cpumap(dev, 1, buf);
48 }
49
50 static SYSDEV_ATTR(cpumap,  S_IRUGO, node_read_cpumask, NULL);
51 static SYSDEV_ATTR(cpulist, S_IRUGO, node_read_cpulist, NULL);
52
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)
56 {
57         int n;
58         int nid = dev->id;
59         struct sysinfo i;
60
61         si_meminfo_node(&i, nid);
62
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;
123 }
124
125 #undef K
126 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
127
128 static ssize_t node_read_numastat(struct sys_device * dev,
129                                 struct sysdev_attribute *attr, char * buf)
130 {
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));
144 }
145 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
146
147 static ssize_t node_read_distance(struct sys_device * dev,
148                         struct sysdev_attribute *attr, char * buf)
149 {
150         int nid = dev->id;
151         int len = 0;
152         int i;
153
154         /* buf currently PAGE_SIZE, need ~4 chars per node */
155         BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
156
157         for_each_online_node(i)
158                 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
159
160         len += sprintf(buf + len, "\n");
161         return len;
162 }
163 static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
164
165
166 /*
167  * register_node - Setup a sysfs device for a node.
168  * @num - Node number to use when creating the device.
169  *
170  * Initialize and register the node device.
171  */
172 int register_node(struct node *node, int num, struct node *parent)
173 {
174         int error;
175
176         node->sysdev.id = num;
177         node->sysdev.cls = &node_class;
178         error = sysdev_register(&node->sysdev);
179
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);
186         }
187         return error;
188 }
189
190 /**
191  * unregister_node - unregister a node device
192  * @node: node going away
193  *
194  * Unregisters a node device @node.  All the devices on the node must be
195  * unregistered before calling this function.
196  */
197 void unregister_node(struct node *node)
198 {
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);
204
205         sysdev_unregister(&node->sysdev);
206 }
207
208 struct node node_devices[MAX_NUMNODES];
209
210 /*
211  * register cpu under node
212  */
213 int register_cpu_under_node(unsigned int cpu, unsigned int nid)
214 {
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));
222          }
223
224         return 0;
225 }
226
227 int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
228 {
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));
234         }
235         return 0;
236 }
237
238 int register_one_node(int nid)
239 {
240         int error = 0;
241         int cpu;
242
243         if (node_online(nid)) {
244                 int p_node = parent_node(nid);
245                 struct node *parent = NULL;
246
247                 if (p_node != nid)
248                         parent = &node_devices[p_node];
249
250                 error = register_node(&node_devices[nid], nid, parent);
251
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);
256                 }
257         }
258
259         return error;
260
261 }
262
263 void unregister_one_node(int nid)
264 {
265         unregister_node(&node_devices[nid]);
266 }
267
268 /*
269  * node states attributes
270  */
271
272 static ssize_t print_nodes_state(enum node_states state, char *buf)
273 {
274         int n;
275
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';
280         }
281         return n;
282 }
283
284 static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
285 {
286         return print_nodes_state(N_POSSIBLE, buf);
287 }
288
289 static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
290 {
291         return print_nodes_state(N_ONLINE, buf);
292 }
293
294 static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
295                                                 char *buf)
296 {
297         return print_nodes_state(N_NORMAL_MEMORY, buf);
298 }
299
300 static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
301 {
302         return print_nodes_state(N_CPU, buf);
303 }
304
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);
310
311 #ifdef CONFIG_HIGHMEM
312 static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
313                                                  char *buf)
314 {
315         return print_nodes_state(N_HIGH_MEMORY, buf);
316 }
317
318 static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
319                                                                          NULL);
320 #endif
321
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,
330 };
331
332 static int node_states_init(void)
333 {
334         int i;
335         int err = 0;
336
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;
342         }
343         return err;
344 }
345
346 static int __init register_node_type(void)
347 {
348         int ret;
349
350         ret = sysdev_class_register(&node_class);
351         if (!ret)
352                 ret = node_states_init();
353
354         /*
355          * Note:  we're not going to unregister the node class if we fail
356          * to register the node state class attribute files.
357          */
358         return ret;
359 }
360 postcore_initcall(register_node_type);