blob: e9b0957f15d12ef522cee3145ba81bd6bf737273 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16static struct sysdev_class node_class = {
17 set_kset_name("node"),
18};
19
20
21static 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
35static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
36
37#define K(x) ((x) << (PAGE_SHIFT - 10))
38static 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 unsigned long inactive;
44 unsigned long active;
45 unsigned long free;
46
47 si_meminfo_node(&i, nid);
48 __get_zone_counts(&active, &inactive, &free, NODE_DATA(nid));
49
Martin Hicksc07e02d2005-09-03 15:55:11 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 n = sprintf(buf, "\n"
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"
57 "Node %d HighTotal: %8lu kB\n"
58 "Node %d HighFree: %8lu kB\n"
59 "Node %d LowTotal: %8lu kB\n"
Martin Hicksc07e02d2005-09-03 15:55:11 -070060 "Node %d LowFree: %8lu kB\n"
61 "Node %d Dirty: %8lu kB\n"
62 "Node %d Writeback: %8lu kB\n"
Christoph Lameter347ce432006-06-30 01:55:35 -070063 "Node %d FilePages: %8lu kB\n"
Martin Hicksc07e02d2005-09-03 15:55:11 -070064 "Node %d Mapped: %8lu kB\n"
Christoph Lameterf3dbd342006-06-30 01:55:36 -070065 "Node %d AnonPages: %8lu kB\n"
Christoph Lameterdf849a12006-06-30 01:55:38 -070066 "Node %d PageTables: %8lu kB\n"
Andrew Mortonf5ef68d2006-08-27 01:23:58 -070067 "Node %d NFS_Unstable: %8lu kB\n"
Christoph Lameterd2c5e302006-06-30 01:55:41 -070068 "Node %d Bounce: %8lu kB\n"
Martin Hicksc07e02d2005-09-03 15:55:11 -070069 "Node %d Slab: %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),
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),
Martin Hicksc07e02d2005-09-03 15:55:11 -070078 nid, K(i.freeram - i.freehigh),
Christoph Lameterb1e7a8f2006-06-30 01:55:39 -070079 nid, K(node_page_state(nid, NR_FILE_DIRTY)),
Christoph Lameterce866b32006-06-30 01:55:40 -070080 nid, K(node_page_state(nid, NR_WRITEBACK)),
Christoph Lameter347ce432006-06-30 01:55:35 -070081 nid, K(node_page_state(nid, NR_FILE_PAGES)),
Christoph Lameter65ba55f2006-06-30 01:55:34 -070082 nid, K(node_page_state(nid, NR_FILE_MAPPED)),
Christoph Lameterf3dbd342006-06-30 01:55:36 -070083 nid, K(node_page_state(nid, NR_ANON_PAGES)),
Christoph Lameterdf849a12006-06-30 01:55:38 -070084 nid, K(node_page_state(nid, NR_PAGETABLE)),
Christoph Lameterfd39fc82006-06-30 01:55:40 -070085 nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
Christoph Lameterd2c5e302006-06-30 01:55:41 -070086 nid, K(node_page_state(nid, NR_BOUNCE)),
Christoph Lameter9a865ff2006-06-30 01:55:38 -070087 nid, K(node_page_state(nid, NR_SLAB)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 n += hugetlb_report_node_meminfo(nid, buf + n);
89 return n;
90}
91
92#undef K
93static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
94
95static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
96{
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return sprintf(buf,
98 "numa_hit %lu\n"
99 "numa_miss %lu\n"
100 "numa_foreign %lu\n"
101 "interleave_hit %lu\n"
102 "local_node %lu\n"
103 "other_node %lu\n",
Christoph Lameterca889e62006-06-30 01:55:44 -0700104 node_page_state(dev->id, NUMA_HIT),
105 node_page_state(dev->id, NUMA_MISS),
106 node_page_state(dev->id, NUMA_FOREIGN),
107 node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
108 node_page_state(dev->id, NUMA_LOCAL),
109 node_page_state(dev->id, NUMA_OTHER));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
112
113static ssize_t node_read_distance(struct sys_device * dev, char * buf)
114{
115 int nid = dev->id;
116 int len = 0;
117 int i;
118
119 /* buf currently PAGE_SIZE, need ~4 chars per node */
120 BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
121
122 for_each_online_node(i)
123 len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
124
125 len += sprintf(buf + len, "\n");
126 return len;
127}
128static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
129
130
131/*
132 * register_node - Setup a driverfs device for a node.
133 * @num - Node number to use when creating the device.
134 *
135 * Initialize and register the node device.
136 */
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900137int register_node(struct node *node, int num, struct node *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int error;
140
141 node->sysdev.id = num;
142 node->sysdev.cls = &node_class;
143 error = sysdev_register(&node->sysdev);
144
145 if (!error){
146 sysdev_create_file(&node->sysdev, &attr_cpumap);
147 sysdev_create_file(&node->sysdev, &attr_meminfo);
148 sysdev_create_file(&node->sysdev, &attr_numastat);
149 sysdev_create_file(&node->sysdev, &attr_distance);
150 }
151 return error;
152}
153
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900154/**
155 * unregister_node - unregister a node device
156 * @node: node going away
157 *
158 * Unregisters a node device @node. All the devices on the node must be
159 * unregistered before calling this function.
160 */
161void unregister_node(struct node *node)
162{
163 sysdev_remove_file(&node->sysdev, &attr_cpumap);
164 sysdev_remove_file(&node->sysdev, &attr_meminfo);
165 sysdev_remove_file(&node->sysdev, &attr_numastat);
166 sysdev_remove_file(&node->sysdev, &attr_distance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900168 sysdev_unregister(&node->sysdev);
169}
170
Yasunori Goto0fc44152006-06-27 02:53:38 -0700171struct node node_devices[MAX_NUMNODES];
172
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700173/*
174 * register cpu under node
175 */
176int register_cpu_under_node(unsigned int cpu, unsigned int nid)
177{
178 if (node_online(nid)) {
179 struct sys_device *obj = get_cpu_sysdev(cpu);
180 if (!obj)
181 return 0;
182 return sysfs_create_link(&node_devices[nid].sysdev.kobj,
183 &obj->kobj,
184 kobject_name(&obj->kobj));
185 }
186
187 return 0;
188}
189
190int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
191{
192 if (node_online(nid)) {
193 struct sys_device *obj = get_cpu_sysdev(cpu);
194 if (obj)
195 sysfs_remove_link(&node_devices[nid].sysdev.kobj,
196 kobject_name(&obj->kobj));
197 }
198 return 0;
199}
200
Yasunori Goto0fc44152006-06-27 02:53:38 -0700201int register_one_node(int nid)
202{
203 int error = 0;
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700204 int cpu;
Yasunori Goto0fc44152006-06-27 02:53:38 -0700205
206 if (node_online(nid)) {
207 int p_node = parent_node(nid);
208 struct node *parent = NULL;
209
210 if (p_node != nid)
211 parent = &node_devices[p_node];
212
213 error = register_node(&node_devices[nid], nid, parent);
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -0700214
215 /* link cpu under this node */
216 for_each_present_cpu(cpu) {
217 if (cpu_to_node(cpu) == nid)
218 register_cpu_under_node(cpu, nid);
219 }
Yasunori Goto0fc44152006-06-27 02:53:38 -0700220 }
221
222 return error;
223
224}
225
226void unregister_one_node(int nid)
227{
228 unregister_node(&node_devices[nid]);
229}
230
Keiichiro Tokunaga4b450992005-05-08 21:28:53 +0900231static int __init register_node_type(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 return sysdev_class_register(&node_class);
234}
235postcore_initcall(register_node_type);