[PATCH] zoned vm counters: conversion of nr_writeback to per zone counter
[linux-2.6.git] / 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
16 static struct sysdev_class node_class = {
17         set_kset_name("node"),
18 };
19
20
21 static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
22 {
23         struct node *node_dev = to_node(dev);
24         cpumask_t mask = node_to_cpumask(node_dev->sysdev.id);
25         int len;
26
27         /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
28         BUILD_BUG_ON(MAX_NUMNODES/4 > PAGE_SIZE/2);
29
30         len = cpumask_scnprintf(buf, PAGE_SIZE-1, mask);
31         len += sprintf(buf + len, "\n");
32         return len;
33 }
34
35 static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
36
37 #define K(x) ((x) << (PAGE_SHIFT - 10))
38 static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
39 {
40         int n;
41         int nid = dev->id;
42         struct sysinfo i;
43         struct page_state ps;
44         unsigned long inactive;
45         unsigned long active;
46         unsigned long free;
47
48         si_meminfo_node(&i, nid);
49         get_page_state_node(&ps, nid);
50         __get_zone_counts(&active, &inactive, &free, NODE_DATA(nid));
51
52
53         n = sprintf(buf, "\n"
54                        "Node %d MemTotal:     %8lu kB\n"
55                        "Node %d MemFree:      %8lu kB\n"
56                        "Node %d MemUsed:      %8lu kB\n"
57                        "Node %d Active:       %8lu kB\n"
58                        "Node %d Inactive:     %8lu kB\n"
59                        "Node %d HighTotal:    %8lu kB\n"
60                        "Node %d HighFree:     %8lu kB\n"
61                        "Node %d LowTotal:     %8lu kB\n"
62                        "Node %d LowFree:      %8lu kB\n"
63                        "Node %d Dirty:        %8lu kB\n"
64                        "Node %d Writeback:    %8lu kB\n"
65                        "Node %d FilePages:    %8lu kB\n"
66                        "Node %d Mapped:       %8lu kB\n"
67                        "Node %d AnonPages:    %8lu kB\n"
68                        "Node %d PageTables:   %8lu kB\n"
69                        "Node %d Slab:         %8lu kB\n",
70                        nid, K(i.totalram),
71                        nid, K(i.freeram),
72                        nid, K(i.totalram - i.freeram),
73                        nid, K(active),
74                        nid, K(inactive),
75                        nid, K(i.totalhigh),
76                        nid, K(i.freehigh),
77                        nid, K(i.totalram - i.totalhigh),
78                        nid, K(i.freeram - i.freehigh),
79                        nid, K(node_page_state(nid, NR_FILE_DIRTY)),
80                        nid, K(node_page_state(nid, NR_WRITEBACK)),
81                        nid, K(node_page_state(nid, NR_FILE_PAGES)),
82                        nid, K(node_page_state(nid, NR_FILE_MAPPED)),
83                        nid, K(node_page_state(nid, NR_ANON_PAGES)),
84                        nid, K(node_page_state(nid, NR_PAGETABLE)),
85                        nid, K(node_page_state(nid, NR_SLAB)));
86         n += hugetlb_report_node_meminfo(nid, buf + n);
87         return n;
88 }
89
90 #undef K
91 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
92
93 static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
94 {
95         unsigned long numa_hit, numa_miss, interleave_hit, numa_foreign;
96         unsigned long local_node, other_node;
97         int i, cpu;
98         pg_data_t *pg = NODE_DATA(dev->id);
99         numa_hit = 0;
100         numa_miss = 0;
101         interleave_hit = 0;
102         numa_foreign = 0;
103         local_node = 0;
104         other_node = 0;
105         for (i = 0; i < MAX_NR_ZONES; i++) {
106                 struct zone *z = &pg->node_zones[i];
107                 for_each_online_cpu(cpu) {
108                         struct per_cpu_pageset *ps = zone_pcp(z,cpu);
109                         numa_hit += ps->numa_hit;
110                         numa_miss += ps->numa_miss;
111                         numa_foreign += ps->numa_foreign;
112                         interleave_hit += ps->interleave_hit;
113                         local_node += ps->local_node;
114                         other_node += ps->other_node;
115                 }
116         }
117         return sprintf(buf,
118                        "numa_hit %lu\n"
119                        "numa_miss %lu\n"
120                        "numa_foreign %lu\n"
121                        "interleave_hit %lu\n"
122                        "local_node %lu\n"
123                        "other_node %lu\n",
124                        numa_hit,
125                        numa_miss,
126                        numa_foreign,
127                        interleave_hit,
128                        local_node,
129                        other_node);
130 }
131 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
132
133 static ssize_t node_read_distance(struct sys_device * dev, char * buf)
134 {
135         int nid = dev->id;
136         int len = 0;
137         int i;
138
139         /* buf currently PAGE_SIZE, need ~4 chars per node */
140         BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
141
142         for_each_online_node(i)
143                 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
144
145         len += sprintf(buf + len, "\n");
146         return len;
147 }
148 static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
149
150
151 /*
152  * register_node - Setup a driverfs device for a node.
153  * @num - Node number to use when creating the device.
154  *
155  * Initialize and register the node device.
156  */
157 int register_node(struct node *node, int num, struct node *parent)
158 {
159         int error;
160
161         node->sysdev.id = num;
162         node->sysdev.cls = &node_class;
163         error = sysdev_register(&node->sysdev);
164
165         if (!error){
166                 sysdev_create_file(&node->sysdev, &attr_cpumap);
167                 sysdev_create_file(&node->sysdev, &attr_meminfo);
168                 sysdev_create_file(&node->sysdev, &attr_numastat);
169                 sysdev_create_file(&node->sysdev, &attr_distance);
170         }
171         return error;
172 }
173
174 /**
175  * unregister_node - unregister a node device
176  * @node: node going away
177  *
178  * Unregisters a node device @node.  All the devices on the node must be
179  * unregistered before calling this function.
180  */
181 void unregister_node(struct node *node)
182 {
183         sysdev_remove_file(&node->sysdev, &attr_cpumap);
184         sysdev_remove_file(&node->sysdev, &attr_meminfo);
185         sysdev_remove_file(&node->sysdev, &attr_numastat);
186         sysdev_remove_file(&node->sysdev, &attr_distance);
187
188         sysdev_unregister(&node->sysdev);
189 }
190
191 struct node node_devices[MAX_NUMNODES];
192
193 /*
194  * register cpu under node
195  */
196 int register_cpu_under_node(unsigned int cpu, unsigned int nid)
197 {
198         if (node_online(nid)) {
199                 struct sys_device *obj = get_cpu_sysdev(cpu);
200                 if (!obj)
201                         return 0;
202                 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
203                                          &obj->kobj,
204                                          kobject_name(&obj->kobj));
205          }
206
207         return 0;
208 }
209
210 int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
211 {
212         if (node_online(nid)) {
213                 struct sys_device *obj = get_cpu_sysdev(cpu);
214                 if (obj)
215                         sysfs_remove_link(&node_devices[nid].sysdev.kobj,
216                                          kobject_name(&obj->kobj));
217         }
218         return 0;
219 }
220
221 int register_one_node(int nid)
222 {
223         int error = 0;
224         int cpu;
225
226         if (node_online(nid)) {
227                 int p_node = parent_node(nid);
228                 struct node *parent = NULL;
229
230                 if (p_node != nid)
231                         parent = &node_devices[p_node];
232
233                 error = register_node(&node_devices[nid], nid, parent);
234
235                 /* link cpu under this node */
236                 for_each_present_cpu(cpu) {
237                         if (cpu_to_node(cpu) == nid)
238                                 register_cpu_under_node(cpu, nid);
239                 }
240         }
241
242         return error;
243
244 }
245
246 void unregister_one_node(int nid)
247 {
248         unregister_node(&node_devices[nid]);
249 }
250
251 static int __init register_node_type(void)
252 {
253         return sysdev_class_register(&node_class);
254 }
255 postcore_initcall(register_node_type);