Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Hiroyuki | 76b67ed9 | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 14 | #include <linux/cpu.h> |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 15 | #include <linux/device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | static struct sysdev_class node_class = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 18 | .name = "node", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | |
| 22 | static 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 | |
| 36 | static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL); |
| 37 | |
| 38 | #define K(x) ((x) << (PAGE_SHIFT - 10)) |
| 39 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | si_meminfo_node(&i, nid); |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | 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 Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 53 | #ifdef CONFIG_HIGHMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | "Node %d HighTotal: %8lu kB\n" |
| 55 | "Node %d HighFree: %8lu kB\n" |
| 56 | "Node %d LowTotal: %8lu kB\n" |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 57 | "Node %d LowFree: %8lu kB\n" |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 58 | #endif |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 59 | "Node %d Dirty: %8lu kB\n" |
| 60 | "Node %d Writeback: %8lu kB\n" |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 61 | "Node %d FilePages: %8lu kB\n" |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 62 | "Node %d Mapped: %8lu kB\n" |
Christoph Lameter | f3dbd34 | 2006-06-30 01:55:36 -0700 | [diff] [blame] | 63 | "Node %d AnonPages: %8lu kB\n" |
Christoph Lameter | df849a1 | 2006-06-30 01:55:38 -0700 | [diff] [blame] | 64 | "Node %d PageTables: %8lu kB\n" |
Andrew Morton | f5ef68d | 2006-08-27 01:23:58 -0700 | [diff] [blame] | 65 | "Node %d NFS_Unstable: %8lu kB\n" |
Christoph Lameter | d2c5e30 | 2006-06-30 01:55:41 -0700 | [diff] [blame] | 66 | "Node %d Bounce: %8lu kB\n" |
Christoph Lameter | 972d1a7 | 2006-09-25 23:31:51 -0700 | [diff] [blame] | 67 | "Node %d Slab: %8lu kB\n" |
| 68 | "Node %d SReclaimable: %8lu kB\n" |
| 69 | "Node %d SUnreclaim: %8lu kB\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | nid, K(i.totalram), |
| 71 | nid, K(i.freeram), |
| 72 | nid, K(i.totalram - i.freeram), |
Christoph Lameter | 05a0416 | 2007-02-10 01:43:05 -0800 | [diff] [blame] | 73 | nid, node_page_state(nid, NR_ACTIVE), |
| 74 | nid, node_page_state(nid, NR_INACTIVE), |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 75 | #ifdef CONFIG_HIGHMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | nid, K(i.totalhigh), |
| 77 | nid, K(i.freehigh), |
| 78 | nid, K(i.totalram - i.totalhigh), |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 79 | nid, K(i.freeram - i.freehigh), |
Christoph Lameter | 182e8e2 | 2006-09-25 23:31:10 -0700 | [diff] [blame] | 80 | #endif |
Christoph Lameter | b1e7a8f | 2006-06-30 01:55:39 -0700 | [diff] [blame] | 81 | nid, K(node_page_state(nid, NR_FILE_DIRTY)), |
Christoph Lameter | ce866b3 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 82 | nid, K(node_page_state(nid, NR_WRITEBACK)), |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 83 | nid, K(node_page_state(nid, NR_FILE_PAGES)), |
Christoph Lameter | 65ba55f | 2006-06-30 01:55:34 -0700 | [diff] [blame] | 84 | nid, K(node_page_state(nid, NR_FILE_MAPPED)), |
Christoph Lameter | f3dbd34 | 2006-06-30 01:55:36 -0700 | [diff] [blame] | 85 | nid, K(node_page_state(nid, NR_ANON_PAGES)), |
Christoph Lameter | df849a1 | 2006-06-30 01:55:38 -0700 | [diff] [blame] | 86 | nid, K(node_page_state(nid, NR_PAGETABLE)), |
Christoph Lameter | fd39fc8 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 87 | nid, K(node_page_state(nid, NR_UNSTABLE_NFS)), |
Christoph Lameter | d2c5e30 | 2006-06-30 01:55:41 -0700 | [diff] [blame] | 88 | nid, K(node_page_state(nid, NR_BOUNCE)), |
Christoph Lameter | 972d1a7 | 2006-09-25 23:31:51 -0700 | [diff] [blame] | 89 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | n += hugetlb_report_node_meminfo(nid, buf + n); |
| 94 | return n; |
| 95 | } |
| 96 | |
| 97 | #undef K |
| 98 | static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL); |
| 99 | |
| 100 | static ssize_t node_read_numastat(struct sys_device * dev, char * buf) |
| 101 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | 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 Lameter | ca889e6 | 2006-06-30 01:55:44 -0700 | [diff] [blame] | 109 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL); |
| 117 | |
| 118 | static 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 | } |
| 133 | static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL); |
| 134 | |
| 135 | |
| 136 | /* |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 137 | * register_node - Setup a sysfs device for a node. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | * @num - Node number to use when creating the device. |
| 139 | * |
| 140 | * Initialize and register the node device. |
| 141 | */ |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 142 | int register_node(struct node *node, int num, struct node *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 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 Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 159 | /** |
| 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 | */ |
| 166 | void 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 173 | sysdev_unregister(&node->sysdev); |
| 174 | } |
| 175 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 176 | struct node node_devices[MAX_NUMNODES]; |
| 177 | |
KAMEZAWA Hiroyuki | 76b67ed9 | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 178 | /* |
| 179 | * register cpu under node |
| 180 | */ |
| 181 | int 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 | |
| 195 | int 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 Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 206 | int register_one_node(int nid) |
| 207 | { |
| 208 | int error = 0; |
KAMEZAWA Hiroyuki | 76b67ed9 | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 209 | int cpu; |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 210 | |
| 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 Hiroyuki | 76b67ed9 | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 219 | |
| 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 Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | return error; |
| 228 | |
| 229 | } |
| 230 | |
| 231 | void unregister_one_node(int nid) |
| 232 | { |
| 233 | unregister_node(&node_devices[nid]); |
| 234 | } |
| 235 | |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 236 | /* |
| 237 | * node states attributes |
| 238 | */ |
| 239 | |
| 240 | static 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 | |
| 252 | static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf) |
| 253 | { |
| 254 | return print_nodes_state(N_POSSIBLE, buf); |
| 255 | } |
| 256 | |
| 257 | static ssize_t print_nodes_online(struct sysdev_class *class, char *buf) |
| 258 | { |
| 259 | return print_nodes_state(N_ONLINE, buf); |
| 260 | } |
| 261 | |
| 262 | static 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 | |
| 268 | static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf) |
| 269 | { |
| 270 | return print_nodes_state(N_CPU, buf); |
| 271 | } |
| 272 | |
| 273 | static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL); |
| 274 | static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL); |
| 275 | static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory, |
| 276 | NULL); |
| 277 | static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL); |
| 278 | |
| 279 | #ifdef CONFIG_HIGHMEM |
| 280 | static 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 | |
| 286 | static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory, |
| 287 | NULL); |
| 288 | #endif |
| 289 | |
| 290 | struct 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 | |
| 300 | static 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 Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 314 | static int __init register_node_type(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Lee Schermerhorn | bde631a | 2007-10-16 01:26:27 -0700 | [diff] [blame] | 316 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | postcore_initcall(register_node_type); |