blob: 8e3f25bb8f807e9e0fbd42bb4ff33dc99d563f3d [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 Hiroyuki76b67ed2006-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 = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +010018 .name = "node",
Linus Torvalds1da177e2005-04-16 15:20:36 -070019};
20
21
22static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
23{
24 struct node *node_dev = to_node(dev);
Mike Travisc5f59f02008-04-04 18:11:10 -070025 node_to_cpumask_ptr(mask, node_dev->sysdev.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 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
Mike Travisc5f59f02008-04-04 18:11:10 -070031 len = cpumask_scnprintf(buf, PAGE_SIZE-2, *mask);
32 buf[len++] = '\n';
33 buf[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return len;
35}
36
37static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
38
39#define K(x) ((x) << (PAGE_SHIFT - 10))
40static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
41{
42 int n;
43 int nid = dev->id;
44 struct sysinfo i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 si_meminfo_node(&i, nid);
Martin Hicksc07e02d2005-09-03 15:55:11 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 n = sprintf(buf, "\n"
49 "Node %d MemTotal: %8lu kB\n"
50 "Node %d MemFree: %8lu kB\n"
51 "Node %d MemUsed: %8lu kB\n"
52 "Node %d Active: %8lu kB\n"
53 "Node %d Inactive: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -070054#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 "Node %d HighTotal: %8lu kB\n"
56 "Node %d HighFree: %8lu kB\n"
57 "Node %d LowTotal: %8lu kB\n"
Martin Hicksc07e02d2005-09-03 15:55:11 -070058 "Node %d LowFree: %8lu kB\n"
Christoph Lameter182e8e22006-09-25 23:31:10 -070059#endif
Martin Hicksc07e02d2005-09-03 15:55:11 -070060 "Node %d Dirty: %8lu kB\n"
61 "Node %d Writeback: %8lu kB\n"
Christoph Lameter347ce432006-06-30 01:55:35 -070062 "Node %d FilePages: %8lu kB\n"
Martin Hicksc07e02d2005-09-03 15:55:11 -070063 "Node %d Mapped: %8lu kB\n"
Christoph Lameterf3dbd342006-06-30 01:55:36 -070064 "Node %d AnonPages: %8lu kB\n"
Christoph Lameterdf849a12006-06-30 01:55:38 -070065 "Node %d PageTables: %8lu kB\n"
Andrew Mortonf5ef68d2006-08-27 01:23:58 -070066 "Node %d NFS_Unstable: %8lu kB\n"
Christoph Lameterd2c5e302006-06-30 01:55:41 -070067 "Node %d Bounce: %8lu kB\n"
Christoph Lameter972d1a72006-09-25 23:31:51 -070068 "Node %d Slab: %8lu kB\n"
69 "Node %d SReclaimable: %8lu kB\n"
70 "Node %d SUnreclaim: %8lu kB\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 nid, K(i.totalram),
72 nid, K(i.freeram),
73 nid, K(i.totalram - i.freeram),
Christoph Lameter05a04162007-02-10 01:43:05 -080074 nid, node_page_state(nid, NR_ACTIVE),
75 nid, node_page_state(nid, NR_INACTIVE),
Christoph Lameter182e8e22006-09-25 23:31:10 -070076#ifdef CONFIG_HIGHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 nid, K(i.totalhigh),
78 nid, K(i.freehigh),
79 nid, K(i.totalram - i.totalhigh),
Martin Hicksc07e02d2005-09-03 15:55:11 -070080 nid, K(i.freeram - i.freehigh),
Christoph Lameter182e8e22006-09-25 23:31:10 -070081#endif
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -070082 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -070083 nid, K(node_page_state(nid, NR_WRITEBACK)),
Christoph Lameter347ce432006-06-30 01:55:35 -070084 nid, K(node_page_state(nid, NR_FILE_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -070085 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -070086 nid, K(node_page_state(nid, NR_ANON_PAGES)),
Christoph Lameterdf849a12006-06-30 01:55:38 -070087 nid, K(node_page_state(nid, NR_PAGETABLE)),
Christoph Lameterfd39fc852006-06-30 01:55:40 -070088 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -070089 nid, K(node_page_state(nid, NR_BOUNCE)),
Christoph Lameter972d1a72006-09-25 23:31:51 -070090 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
91 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
92 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
93 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 n += hugetlb_report_node_meminfo(nid, buf + n);
95 return n;
96}
97
98#undef K
99static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
100
101static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return sprintf(buf,
104 "numa_hit %lu\n"
105 "numa_miss %lu\n"
106 "numa_foreign %lu\n"
107 "interleave_hit %lu\n"
108 "local_node %lu\n"
109 "other_node %lu\n",
Christoph Lameterca889e62006-06-30 01:55:44 -0700110 node_page_state(dev->id, NUMA_HIT),
111 node_page_state(dev->id, NUMA_MISS),
112 node_page_state(dev->id, NUMA_FOREIGN),
113 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
114 node_page_state(dev->id, NUMA_LOCAL),
115 node_page_state(dev->id, NUMA_OTHER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
118
119static ssize_t node_read_distance(struct sys_device * dev, char * buf)
120{
121 int nid = dev->id;
122 int len = 0;
123 int i;
124
125 /* buf currently PAGE_SIZE, need ~4 chars per node */
126 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
127
128 for_each_online_node(i)
129 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
130
131 len += sprintf(buf + len, "\n");
132 return len;
133}
134static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
135
136
137/*
Robert P. J. Day405ae7d2007-02-17 19:13:42 +0100138 * register_node - Setup a sysfs device for a node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * @num - Node number to use when creating the device.
140 *
141 * Initialize and register the node device.
142 */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900143int register_node(struct node *node, int num, struct node *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int error;
146
147 node->sysdev.id = num;
148 node->sysdev.cls = &node_class;
149 error = sysdev_register(&node->sysdev);
150
151 if (!error){
152 sysdev_create_file(&node->sysdev, &attr_cpumap);
153 sysdev_create_file(&node->sysdev, &attr_meminfo);
154 sysdev_create_file(&node->sysdev, &attr_numastat);
155 sysdev_create_file(&node->sysdev, &attr_distance);
156 }
157 return error;
158}
159
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900160/**
161 * unregister_node - unregister a node device
162 * @node: node going away
163 *
164 * Unregisters a node device @node. All the devices on the node must be
165 * unregistered before calling this function.
166 */
167void unregister_node(struct node *node)
168{
169 sysdev_remove_file(&node->sysdev, &attr_cpumap);
170 sysdev_remove_file(&node->sysdev, &attr_meminfo);
171 sysdev_remove_file(&node->sysdev, &attr_numastat);
172 sysdev_remove_file(&node->sysdev, &attr_distance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900174 sysdev_unregister(&node->sysdev);
175}
176
Yasunori Goto0fc44152006-06-27 02:53:38 -0700177struct node node_devices[MAX_NUMNODES];
178
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700179/*
180 * register cpu under node
181 */
182int register_cpu_under_node(unsigned int cpu, unsigned int nid)
183{
184 if (node_online(nid)) {
185 struct sys_device *obj = get_cpu_sysdev(cpu);
186 if (!obj)
187 return 0;
188 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
189 &obj->kobj,
190 kobject_name(&obj->kobj));
191 }
192
193 return 0;
194}
195
196int unregister_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 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
202 kobject_name(&obj->kobj));
203 }
204 return 0;
205}
206
Yasunori Goto0fc44152006-06-27 02:53:38 -0700207int register_one_node(int nid)
208{
209 int error = 0;
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700210 int cpu;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700211
212 if (node_online(nid)) {
213 int p_node = parent_node(nid);
214 struct node *parent = NULL;
215
216 if (p_node != nid)
217 parent = &node_devices[p_node];
218
219 error = register_node(&node_devices[nid], nid, parent);
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700220
221 /* link cpu under this node */
222 for_each_present_cpu(cpu) {
223 if (cpu_to_node(cpu) == nid)
224 register_cpu_under_node(cpu, nid);
225 }
Yasunori Goto0fc44152006-06-27 02:53:38 -0700226 }
227
228 return error;
229
230}
231
232void unregister_one_node(int nid)
233{
234 unregister_node(&node_devices[nid]);
235}
236
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700237/*
238 * node states attributes
239 */
240
241static ssize_t print_nodes_state(enum node_states state, char *buf)
242{
243 int n;
244
245 n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
246 if (n > 0 && PAGE_SIZE > n + 1) {
247 *(buf + n++) = '\n';
248 *(buf + n++) = '\0';
249 }
250 return n;
251}
252
253static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
254{
255 return print_nodes_state(N_POSSIBLE, buf);
256}
257
258static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
259{
260 return print_nodes_state(N_ONLINE, buf);
261}
262
263static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
264 char *buf)
265{
266 return print_nodes_state(N_NORMAL_MEMORY, buf);
267}
268
269static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
270{
271 return print_nodes_state(N_CPU, buf);
272}
273
274static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
275static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
276static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
277 NULL);
278static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
279
280#ifdef CONFIG_HIGHMEM
281static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
282 char *buf)
283{
284 return print_nodes_state(N_HIGH_MEMORY, buf);
285}
286
287static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
288 NULL);
289#endif
290
291struct sysdev_class_attribute *node_state_attr[] = {
292 &attr_possible,
293 &attr_online,
294 &attr_has_normal_memory,
295#ifdef CONFIG_HIGHMEM
296 &attr_has_high_memory,
297#endif
298 &attr_has_cpu,
299};
300
301static int node_states_init(void)
302{
303 int i;
304 int err = 0;
305
306 for (i = 0; i < NR_NODE_STATES; i++) {
307 int ret;
308 ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
309 if (!err)
310 err = ret;
311 }
312 return err;
313}
314
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900315static int __init register_node_type(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Lee Schermerhornbde631a2007-10-16 01:26:27 -0700317 int ret;
318
319 ret = sysdev_class_register(&node_class);
320 if (!ret)
321 ret = node_states_init();
322
323 /*
324 * Note: we're not going to unregister the node class if we fail
325 * to register the node state class attribute files.
326 */
327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329postcore_initcall(register_node_type);