2 * drivers/base/node.c - basic Node class support
5 #include <linux/sysdev.h>
6 #include <linux/module.h>
7 #include <linux/init.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>
16 static struct sysdev_class node_class = {
17 set_kset_name("node"),
21 static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
23 struct node *node_dev = to_node(dev);
24 cpumask_t mask = node_to_cpumask(node_dev->sysdev.id);
27 /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
28 BUILD_BUG_ON(MAX_NUMNODES/4 > PAGE_SIZE/2);
30 len = cpumask_scnprintf(buf, PAGE_SIZE-1, mask);
31 len += sprintf(buf + len, "\n");
35 static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
37 #define K(x) ((x) << (PAGE_SHIFT - 10))
38 static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
43 unsigned long inactive;
47 si_meminfo_node(&i, nid);
48 __get_zone_counts(&active, &inactive, &free, NODE_DATA(nid));
52 "Node %d MemTotal: %8lu kB\n"
53 "Node %d MemFree: %8lu kB\n"
54 "Node %d MemUsed: %8lu kB\n"
55 "Node %d Active: %8lu kB\n"
56 "Node %d Inactive: %8lu kB\n"
58 "Node %d HighTotal: %8lu kB\n"
59 "Node %d HighFree: %8lu kB\n"
60 "Node %d LowTotal: %8lu kB\n"
61 "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 NFS_Unstable: %8lu kB\n"
70 "Node %d Bounce: %8lu kB\n"
71 "Node %d Slab: %8lu kB\n"
72 "Node %d SReclaimable: %8lu kB\n"
73 "Node %d SUnreclaim: %8lu kB\n",
76 nid, K(i.totalram - i.freeram),
82 nid, K(i.totalram - i.totalhigh),
83 nid, K(i.freeram - i.freehigh),
85 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
86 nid, K(node_page_state(nid, NR_WRITEBACK)),
87 nid, K(node_page_state(nid, NR_FILE_PAGES)),
88 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
89 nid, K(node_page_state(nid, NR_ANON_PAGES)),
90 nid, K(node_page_state(nid, NR_PAGETABLE)),
91 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
92 nid, K(node_page_state(nid, NR_BOUNCE)),
93 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
94 node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
95 nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
96 nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
97 n += hugetlb_report_node_meminfo(nid, buf + n);
102 static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
104 static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
110 "interleave_hit %lu\n"
113 node_page_state(dev->id, NUMA_HIT),
114 node_page_state(dev->id, NUMA_MISS),
115 node_page_state(dev->id, NUMA_FOREIGN),
116 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
117 node_page_state(dev->id, NUMA_LOCAL),
118 node_page_state(dev->id, NUMA_OTHER));
120 static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
122 static ssize_t node_read_distance(struct sys_device * dev, char * buf)
128 /* buf currently PAGE_SIZE, need ~4 chars per node */
129 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
131 for_each_online_node(i)
132 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
134 len += sprintf(buf + len, "\n");
137 static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
141 * register_node - Setup a driverfs device for a node.
142 * @num - Node number to use when creating the device.
144 * Initialize and register the node device.
146 int register_node(struct node *node, int num, struct node *parent)
150 node->sysdev.id = num;
151 node->sysdev.cls = &node_class;
152 error = sysdev_register(&node->sysdev);
155 sysdev_create_file(&node->sysdev, &attr_cpumap);
156 sysdev_create_file(&node->sysdev, &attr_meminfo);
157 sysdev_create_file(&node->sysdev, &attr_numastat);
158 sysdev_create_file(&node->sysdev, &attr_distance);
164 * unregister_node - unregister a node device
165 * @node: node going away
167 * Unregisters a node device @node. All the devices on the node must be
168 * unregistered before calling this function.
170 void unregister_node(struct node *node)
172 sysdev_remove_file(&node->sysdev, &attr_cpumap);
173 sysdev_remove_file(&node->sysdev, &attr_meminfo);
174 sysdev_remove_file(&node->sysdev, &attr_numastat);
175 sysdev_remove_file(&node->sysdev, &attr_distance);
177 sysdev_unregister(&node->sysdev);
180 struct node node_devices[MAX_NUMNODES];
183 * register cpu under node
185 int register_cpu_under_node(unsigned int cpu, unsigned int nid)
187 if (node_online(nid)) {
188 struct sys_device *obj = get_cpu_sysdev(cpu);
191 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
193 kobject_name(&obj->kobj));
199 int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
201 if (node_online(nid)) {
202 struct sys_device *obj = get_cpu_sysdev(cpu);
204 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
205 kobject_name(&obj->kobj));
210 int register_one_node(int nid)
215 if (node_online(nid)) {
216 int p_node = parent_node(nid);
217 struct node *parent = NULL;
220 parent = &node_devices[p_node];
222 error = register_node(&node_devices[nid], nid, parent);
224 /* link cpu under this node */
225 for_each_present_cpu(cpu) {
226 if (cpu_to_node(cpu) == nid)
227 register_cpu_under_node(cpu, nid);
235 void unregister_one_node(int nid)
237 unregister_node(&node_devices[nid]);
240 static int __init register_node_type(void)
242 return sysdev_class_register(&node_class);
244 postcore_initcall(register_node_type);