blob: 88eeed72b5d6557289392202a21338496676e55b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -070014#include <linux/cpu.h>
Lee Schermerhornbde631a2007-10-16 01:26:27 -070015#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17static struct sysdev_class node_class = {
18 set_kset_name("node"),
19};
20
21
22static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
23{
24 struct node *node_dev = to_node(dev);
25 cpumask_t mask = node_to_cpumask(node_dev->sysdev.id);
26 int len;
27
28 /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
29 BUILD_BUG_ON(MAX_NUMNODES/4 > PAGE_SIZE/2);
30
31 len = cpumask_scnprintf(buf, PAGE_SIZE-1, mask);
32 len += sprintf(buf + len, "\n");
33 return len;
34}
35
36static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
37
38#define K(x) ((x) << (PAGE_SHIFT - 10))
39static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
40{
41 int n;
42 int nid = dev->id;
43 struct sysinfo i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 si_meminfo_node(&i, nid);
Martin Hicksc07e02d2005-09-03 15:55:11 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 n = sprintf(buf, "\n"
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"
Christoph Lameter182e8e22006-09-25 23:31:10 -070053#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 "Node %d HighTotal: %8lu kB\n"
55 "Node %d HighFree: %8lu kB\n"
56 "Node %d LowTotal: %8lu kB\n"
Martin Hicksc07e02d2005-09-03 15:55:11 -070057 "Node %d LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -070058#endif
Martin Hicksc07e02d2005-09-03 15:55:11 -070059 "Node %d Dirty: %8lu kB\n"
60 "Node %d Writeback: %8lu kB\n"
Christoph Lameter347ce432006-06-30 01:55:35 -070061 "Node %d FilePages: %8lu kB\n"
Martin Hicksc07e02d2005-09-03 15:55:11 -070062 "Node %d Mapped: %8lu kB\n"
Christoph Lameterf3dbd342006-06-30 01:55:36 -070063 "Node %d AnonPages: %8lu kB\n"
Christoph Lameterdf849a12006-06-30 01:55:38 -070064 "Node %d PageTables: %8lu kB\n"
Andrew Mortonf5ef68d2006-08-27 01:23:58 -070065 "Node %d NFS_Unstable: %8lu kB\n"
Christoph Lameterd2c5e302006-06-30 01:55:41 -070066 "Node %d Bounce: %8lu kB\n"
Christoph Lameter972d1a72006-09-25 23:31:51 -070067 "Node %d Slab: %8lu kB\n"
68 "Node %d SReclaimable: %8lu kB\n"
69 "Node %d SUnreclaim: %8lu kB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 nid, K(i.totalram),
71 nid, K(i.freeram),
72 nid, K(i.totalram - i.freeram),
Christoph Lameter05a04162007-02-10 01:43:05 -080073 nid, node_page_state(nid, NR_ACTIVE),
74 nid, node_page_state(nid, NR_INACTIVE),
Christoph Lameter182e8e22006-09-25 23:31:10 -070075#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 nid, K(i.totalhigh),
77 nid, K(i.freehigh),
78 nid, K(i.totalram - i.totalhigh),
Martin Hicksc07e02d2005-09-03 15:55:11 -070079 nid, K(i.freeram - i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -070080#endif
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -070081 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -070082 nid, K(node_page_state(nid, NR_WRITEBACK)),
Christoph Lameter347ce432006-06-30 01:55:35 -070083 nid, K(node_page_state(nid, NR_FILE_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -070084 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -070085 nid, K(node_page_state(nid, NR_ANON_PAGES)),
Christoph Lameterdf849a12006-06-30 01:55:38 -070086 nid, K(node_page_state(nid, NR_PAGETABLE)),
Christoph Lameterfd39fc82006-06-30 01:55:40 -070087 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -070088 nid, K(node_page_state(nid, NR_BOUNCE)),
Christoph Lameter972d1a72006-09-25 23:31:51 -070089 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)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 n += hugetlb_report_node_meminfo(nid, buf + n);
94 return n;
95}
96
97#undef K
98static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
99
100static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
101{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return sprintf(buf,
103 "numa_hit %lu\n"
104 "numa_miss %lu\n"
105 "numa_foreign %lu\n"
106 "interleave_hit %lu\n"
107 "local_node %lu\n"
108 "other_node %lu\n",
Christoph Lameterca889e62006-06-30 01:55:44 -0700109 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
117
118static ssize_t node_read_distance(struct sys_device * dev, char * buf)
119{
120 int nid = dev->id;
121 int len = 0;
122 int i;
123
124 /* buf currently PAGE_SIZE, need ~4 chars per node */
125 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
126
127 for_each_online_node(i)
128 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
129
130 len += sprintf(buf + len, "\n");
131 return len;
132}
133static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
134
135
136/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100137 * register_node - Setup a sysfs device for a node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * @num - Node number to use when creating the device.
139 *
140 * Initialize and register the node device.
141 */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900142int register_node(struct node *node, int num, struct node *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 int error;
145
146 node->sysdev.id = num;
147 node->sysdev.cls = &node_class;
148 error = sysdev_register(&node->sysdev);
149
150 if (!error){
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);
155 }
156 return error;
157}
158
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900159/**
160 * unregister_node - unregister a node device
161 * @node: node going away
162 *
163 * Unregisters a node device @node. All the devices on the node must be
164 * unregistered before calling this function.
165 */
166void unregister_node(struct node *node)
167{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900173 sysdev_unregister(&node->sysdev);
174}
175
Yasunori Goto0fc44152006-06-27 02:53:38 -0700176struct node node_devices[MAX_NUMNODES];
177
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700178/*
179 * register cpu under node
180 */
181int register_cpu_under_node(unsigned int cpu, unsigned int nid)
182{
183 if (node_online(nid)) {
184 struct sys_device *obj = get_cpu_sysdev(cpu);
185 if (!obj)
186 return 0;
187 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
188 &obj->kobj,
189 kobject_name(&obj->kobj));
190 }
191
192 return 0;
193}
194
195int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
196{
197 if (node_online(nid)) {
198 struct sys_device *obj = get_cpu_sysdev(cpu);
199 if (obj)
200 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
201 kobject_name(&obj->kobj));
202 }
203 return 0;
204}
205
Yasunori Goto0fc44152006-06-27 02:53:38 -0700206int register_one_node(int nid)
207{
208 int error = 0;
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700209 int cpu;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700210
211 if (node_online(nid)) {
212 int p_node = parent_node(nid);
213 struct node *parent = NULL;
214
215 if (p_node != nid)
216 parent = &node_devices[p_node];
217
218 error = register_node(&node_devices[nid], nid, parent);
KAMEZAWA Hiroyuki76b67ed92006-06-27 02:53:41 -0700219
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);
224 }
Yasunori Goto0fc44152006-06-27 02:53:38 -0700225 }
226
227 return error;
228
229}
230
231void unregister_one_node(int nid)
232{
233 unregister_node(&node_devices[nid]);
234}
235
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700236/*
237 * node states attributes
238 */
239
240static ssize_t print_nodes_state(enum node_states state, char *buf)
241{
242 int n;
243
244 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
245 if (n > 0 && PAGE_SIZE > n + 1) {
246 *(buf + n++) = '\n';
247 *(buf + n++) = '\0';
248 }
249 return n;
250}
251
252static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
253{
254 return print_nodes_state(N_POSSIBLE, buf);
255}
256
257static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
258{
259 return print_nodes_state(N_ONLINE, buf);
260}
261
262static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
263 char *buf)
264{
265 return print_nodes_state(N_NORMAL_MEMORY, buf);
266}
267
268static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
269{
270 return print_nodes_state(N_CPU, buf);
271}
272
273static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
274static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
275static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
276 NULL);
277static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
278
279#ifdef CONFIG_HIGHMEM
280static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
281 char *buf)
282{
283 return print_nodes_state(N_HIGH_MEMORY, buf);
284}
285
286static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
287 NULL);
288#endif
289
290struct sysdev_class_attribute *node_state_attr[] = {
291 &attr_possible,
292 &attr_online,
293 &attr_has_normal_memory,
294#ifdef CONFIG_HIGHMEM
295 &attr_has_high_memory,
296#endif
297 &attr_has_cpu,
298};
299
300static int node_states_init(void)
301{
302 int i;
303 int err = 0;
304
305 for (i = 0; i < NR_NODE_STATES; i++) {
306 int ret;
307 ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
308 if (!err)
309 err = ret;
310 }
311 return err;
312}
313
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900314static int __init register_node_type(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700316 int ret;
317
318 ret = sysdev_class_register(&node_class);
319 if (!ret)
320 ret = node_states_init();
321
322 /*
323 * Note: we're not going to unregister the node class if we fail
324 * to register the node state class attribute files.
325 */
326 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328postcore_initcall(register_node_type);