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
)
44 si_meminfo_node(&i
, nid
);
47 "Node %d MemTotal: %8lu kB\n"
48 "Node %d MemFree: %8lu kB\n"
49 "Node %d MemUsed: %8lu kB\n"
50 "Node %d Active: %8lu kB\n"
51 "Node %d Inactive: %8lu kB\n"
53 "Node %d HighTotal: %8lu kB\n"
54 "Node %d HighFree: %8lu kB\n"
55 "Node %d LowTotal: %8lu kB\n"
56 "Node %d LowFree: %8lu kB\n"
58 "Node %d Dirty: %8lu kB\n"
59 "Node %d Writeback: %8lu kB\n"
60 "Node %d FilePages: %8lu kB\n"
61 "Node %d Mapped: %8lu kB\n"
62 "Node %d AnonPages: %8lu kB\n"
63 "Node %d PageTables: %8lu kB\n"
64 "Node %d NFS_Unstable: %8lu kB\n"
65 "Node %d Bounce: %8lu kB\n"
66 "Node %d Slab: %8lu kB\n"
67 "Node %d SReclaimable: %8lu kB\n"
68 "Node %d SUnreclaim: %8lu kB\n",
71 nid
, K(i
.totalram
- i
.freeram
),
72 nid
, node_page_state(nid
, NR_ACTIVE
),
73 nid
, node_page_state(nid
, NR_INACTIVE
),
77 nid
, K(i
.totalram
- i
.totalhigh
),
78 nid
, K(i
.freeram
- i
.freehigh
),
80 nid
, K(node_page_state(nid
, NR_FILE_DIRTY
)),
81 nid
, K(node_page_state(nid
, NR_WRITEBACK
)),
82 nid
, K(node_page_state(nid
, NR_FILE_PAGES
)),
83 nid
, K(node_page_state(nid
, NR_FILE_MAPPED
)),
84 nid
, K(node_page_state(nid
, NR_ANON_PAGES
)),
85 nid
, K(node_page_state(nid
, NR_PAGETABLE
)),
86 nid
, K(node_page_state(nid
, NR_UNSTABLE_NFS
)),
87 nid
, K(node_page_state(nid
, NR_BOUNCE
)),
88 nid
, K(node_page_state(nid
, NR_SLAB_RECLAIMABLE
) +
89 node_page_state(nid
, NR_SLAB_UNRECLAIMABLE
)),
90 nid
, K(node_page_state(nid
, NR_SLAB_RECLAIMABLE
)),
91 nid
, K(node_page_state(nid
, NR_SLAB_UNRECLAIMABLE
)));
92 n
+= hugetlb_report_node_meminfo(nid
, buf
+ n
);
97 static SYSDEV_ATTR(meminfo
, S_IRUGO
, node_read_meminfo
, NULL
);
99 static ssize_t
node_read_numastat(struct sys_device
* dev
, char * buf
)
105 "interleave_hit %lu\n"
108 node_page_state(dev
->id
, NUMA_HIT
),
109 node_page_state(dev
->id
, NUMA_MISS
),
110 node_page_state(dev
->id
, NUMA_FOREIGN
),
111 node_page_state(dev
->id
, NUMA_INTERLEAVE_HIT
),
112 node_page_state(dev
->id
, NUMA_LOCAL
),
113 node_page_state(dev
->id
, NUMA_OTHER
));
115 static SYSDEV_ATTR(numastat
, S_IRUGO
, node_read_numastat
, NULL
);
117 static ssize_t
node_read_distance(struct sys_device
* dev
, char * buf
)
123 /* buf currently PAGE_SIZE, need ~4 chars per node */
124 BUILD_BUG_ON(MAX_NUMNODES
*4 > PAGE_SIZE
/2);
126 for_each_online_node(i
)
127 len
+= sprintf(buf
+ len
, "%s%d", i
? " " : "", node_distance(nid
, i
));
129 len
+= sprintf(buf
+ len
, "\n");
132 static SYSDEV_ATTR(distance
, S_IRUGO
, node_read_distance
, NULL
);
136 * register_node - Setup a sysfs device for a node.
137 * @num - Node number to use when creating the device.
139 * Initialize and register the node device.
141 int register_node(struct node
*node
, int num
, struct node
*parent
)
145 node
->sysdev
.id
= num
;
146 node
->sysdev
.cls
= &node_class
;
147 error
= sysdev_register(&node
->sysdev
);
150 sysdev_create_file(&node
->sysdev
, &attr_cpumap
);
151 sysdev_create_file(&node
->sysdev
, &attr_meminfo
);
152 sysdev_create_file(&node
->sysdev
, &attr_numastat
);
153 sysdev_create_file(&node
->sysdev
, &attr_distance
);
159 * unregister_node - unregister a node device
160 * @node: node going away
162 * Unregisters a node device @node. All the devices on the node must be
163 * unregistered before calling this function.
165 void unregister_node(struct node
*node
)
167 sysdev_remove_file(&node
->sysdev
, &attr_cpumap
);
168 sysdev_remove_file(&node
->sysdev
, &attr_meminfo
);
169 sysdev_remove_file(&node
->sysdev
, &attr_numastat
);
170 sysdev_remove_file(&node
->sysdev
, &attr_distance
);
172 sysdev_unregister(&node
->sysdev
);
175 struct node node_devices
[MAX_NUMNODES
];
178 * register cpu under node
180 int register_cpu_under_node(unsigned int cpu
, unsigned int nid
)
182 if (node_online(nid
)) {
183 struct sys_device
*obj
= get_cpu_sysdev(cpu
);
186 return sysfs_create_link(&node_devices
[nid
].sysdev
.kobj
,
188 kobject_name(&obj
->kobj
));
194 int unregister_cpu_under_node(unsigned int cpu
, unsigned int nid
)
196 if (node_online(nid
)) {
197 struct sys_device
*obj
= get_cpu_sysdev(cpu
);
199 sysfs_remove_link(&node_devices
[nid
].sysdev
.kobj
,
200 kobject_name(&obj
->kobj
));
205 int register_one_node(int nid
)
210 if (node_online(nid
)) {
211 int p_node
= parent_node(nid
);
212 struct node
*parent
= NULL
;
215 parent
= &node_devices
[p_node
];
217 error
= register_node(&node_devices
[nid
], nid
, parent
);
219 /* link cpu under this node */
220 for_each_present_cpu(cpu
) {
221 if (cpu_to_node(cpu
) == nid
)
222 register_cpu_under_node(cpu
, nid
);
230 void unregister_one_node(int nid
)
232 unregister_node(&node_devices
[nid
]);
235 static int __init
register_node_type(void)
237 return sysdev_class_register(&node_class
);
239 postcore_initcall(register_node_type
);