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 | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 14 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | static struct sysdev_class node_class = { |
| 17 | set_kset_name("node"), |
| 18 | }; |
| 19 | |
| 20 | |
| 21 | static 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 | |
| 35 | static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL); |
| 36 | |
| 37 | #define K(x) ((x) << (PAGE_SHIFT - 10)) |
| 38 | static ssize_t node_read_meminfo(struct sys_device * dev, char * buf) |
| 39 | { |
| 40 | int n; |
| 41 | int nid = dev->id; |
| 42 | struct sysinfo i; |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 43 | struct page_state ps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | unsigned long inactive; |
| 45 | unsigned long active; |
| 46 | unsigned long free; |
| 47 | |
| 48 | si_meminfo_node(&i, nid); |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 49 | get_page_state_node(&ps, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | __get_zone_counts(&active, &inactive, &free, NODE_DATA(nid)); |
| 51 | |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 52 | /* Check for negative values in these approximate counters */ |
| 53 | if ((long)ps.nr_dirty < 0) |
| 54 | ps.nr_dirty = 0; |
| 55 | if ((long)ps.nr_writeback < 0) |
| 56 | ps.nr_writeback = 0; |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 57 | if ((long)ps.nr_slab < 0) |
| 58 | ps.nr_slab = 0; |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | n = sprintf(buf, "\n" |
| 61 | "Node %d MemTotal: %8lu kB\n" |
| 62 | "Node %d MemFree: %8lu kB\n" |
| 63 | "Node %d MemUsed: %8lu kB\n" |
| 64 | "Node %d Active: %8lu kB\n" |
| 65 | "Node %d Inactive: %8lu kB\n" |
| 66 | "Node %d HighTotal: %8lu kB\n" |
| 67 | "Node %d HighFree: %8lu kB\n" |
| 68 | "Node %d LowTotal: %8lu kB\n" |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 69 | "Node %d LowFree: %8lu kB\n" |
| 70 | "Node %d Dirty: %8lu kB\n" |
| 71 | "Node %d Writeback: %8lu kB\n" |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 72 | "Node %d FilePages: %8lu kB\n" |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 73 | "Node %d Mapped: %8lu kB\n" |
Christoph Lameter | f3dbd34 | 2006-06-30 01:55:36 -0700 | [diff] [blame] | 74 | "Node %d AnonPages: %8lu kB\n" |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 75 | "Node %d Slab: %8lu kB\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | nid, K(i.totalram), |
| 77 | nid, K(i.freeram), |
| 78 | nid, K(i.totalram - i.freeram), |
| 79 | nid, K(active), |
| 80 | nid, K(inactive), |
| 81 | nid, K(i.totalhigh), |
| 82 | nid, K(i.freehigh), |
| 83 | nid, K(i.totalram - i.totalhigh), |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 84 | nid, K(i.freeram - i.freehigh), |
| 85 | nid, K(ps.nr_dirty), |
| 86 | nid, K(ps.nr_writeback), |
Christoph Lameter | 347ce43 | 2006-06-30 01:55:35 -0700 | [diff] [blame] | 87 | nid, K(node_page_state(nid, NR_FILE_PAGES)), |
Christoph Lameter | 65ba55f | 2006-06-30 01:55:34 -0700 | [diff] [blame] | 88 | nid, K(node_page_state(nid, NR_FILE_MAPPED)), |
Christoph Lameter | f3dbd34 | 2006-06-30 01:55:36 -0700 | [diff] [blame] | 89 | nid, K(node_page_state(nid, NR_ANON_PAGES)), |
Martin Hicks | c07e02d | 2005-09-03 15:55:11 -0700 | [diff] [blame] | 90 | nid, K(ps.nr_slab)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | n += hugetlb_report_node_meminfo(nid, buf + n); |
| 92 | return n; |
| 93 | } |
| 94 | |
| 95 | #undef K |
| 96 | static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL); |
| 97 | |
| 98 | static ssize_t node_read_numastat(struct sys_device * dev, char * buf) |
| 99 | { |
| 100 | unsigned long numa_hit, numa_miss, interleave_hit, numa_foreign; |
| 101 | unsigned long local_node, other_node; |
| 102 | int i, cpu; |
| 103 | pg_data_t *pg = NODE_DATA(dev->id); |
| 104 | numa_hit = 0; |
| 105 | numa_miss = 0; |
| 106 | interleave_hit = 0; |
| 107 | numa_foreign = 0; |
| 108 | local_node = 0; |
| 109 | other_node = 0; |
| 110 | for (i = 0; i < MAX_NR_ZONES; i++) { |
| 111 | struct zone *z = &pg->node_zones[i]; |
Christoph Lameter | 54404e7 | 2006-04-10 22:52:47 -0700 | [diff] [blame] | 112 | for_each_online_cpu(cpu) { |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 113 | struct per_cpu_pageset *ps = zone_pcp(z,cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | numa_hit += ps->numa_hit; |
| 115 | numa_miss += ps->numa_miss; |
| 116 | numa_foreign += ps->numa_foreign; |
| 117 | interleave_hit += ps->interleave_hit; |
| 118 | local_node += ps->local_node; |
| 119 | other_node += ps->other_node; |
| 120 | } |
| 121 | } |
| 122 | return sprintf(buf, |
| 123 | "numa_hit %lu\n" |
| 124 | "numa_miss %lu\n" |
| 125 | "numa_foreign %lu\n" |
| 126 | "interleave_hit %lu\n" |
| 127 | "local_node %lu\n" |
| 128 | "other_node %lu\n", |
| 129 | numa_hit, |
| 130 | numa_miss, |
| 131 | numa_foreign, |
| 132 | interleave_hit, |
| 133 | local_node, |
| 134 | other_node); |
| 135 | } |
| 136 | static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL); |
| 137 | |
| 138 | static ssize_t node_read_distance(struct sys_device * dev, char * buf) |
| 139 | { |
| 140 | int nid = dev->id; |
| 141 | int len = 0; |
| 142 | int i; |
| 143 | |
| 144 | /* buf currently PAGE_SIZE, need ~4 chars per node */ |
| 145 | BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2); |
| 146 | |
| 147 | for_each_online_node(i) |
| 148 | len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i)); |
| 149 | |
| 150 | len += sprintf(buf + len, "\n"); |
| 151 | return len; |
| 152 | } |
| 153 | static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL); |
| 154 | |
| 155 | |
| 156 | /* |
| 157 | * register_node - Setup a driverfs device for a node. |
| 158 | * @num - Node number to use when creating the device. |
| 159 | * |
| 160 | * Initialize and register the node device. |
| 161 | */ |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 162 | int register_node(struct node *node, int num, struct node *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
| 164 | int error; |
| 165 | |
| 166 | node->sysdev.id = num; |
| 167 | node->sysdev.cls = &node_class; |
| 168 | error = sysdev_register(&node->sysdev); |
| 169 | |
| 170 | if (!error){ |
| 171 | sysdev_create_file(&node->sysdev, &attr_cpumap); |
| 172 | sysdev_create_file(&node->sysdev, &attr_meminfo); |
| 173 | sysdev_create_file(&node->sysdev, &attr_numastat); |
| 174 | sysdev_create_file(&node->sysdev, &attr_distance); |
| 175 | } |
| 176 | return error; |
| 177 | } |
| 178 | |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 179 | /** |
| 180 | * unregister_node - unregister a node device |
| 181 | * @node: node going away |
| 182 | * |
| 183 | * Unregisters a node device @node. All the devices on the node must be |
| 184 | * unregistered before calling this function. |
| 185 | */ |
| 186 | void unregister_node(struct node *node) |
| 187 | { |
| 188 | sysdev_remove_file(&node->sysdev, &attr_cpumap); |
| 189 | sysdev_remove_file(&node->sysdev, &attr_meminfo); |
| 190 | sysdev_remove_file(&node->sysdev, &attr_numastat); |
| 191 | sysdev_remove_file(&node->sysdev, &attr_distance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 193 | sysdev_unregister(&node->sysdev); |
| 194 | } |
| 195 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 196 | struct node node_devices[MAX_NUMNODES]; |
| 197 | |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 198 | /* |
| 199 | * register cpu under node |
| 200 | */ |
| 201 | int register_cpu_under_node(unsigned int cpu, unsigned int nid) |
| 202 | { |
| 203 | if (node_online(nid)) { |
| 204 | struct sys_device *obj = get_cpu_sysdev(cpu); |
| 205 | if (!obj) |
| 206 | return 0; |
| 207 | return sysfs_create_link(&node_devices[nid].sysdev.kobj, |
| 208 | &obj->kobj, |
| 209 | kobject_name(&obj->kobj)); |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | int unregister_cpu_under_node(unsigned int cpu, unsigned int nid) |
| 216 | { |
| 217 | if (node_online(nid)) { |
| 218 | struct sys_device *obj = get_cpu_sysdev(cpu); |
| 219 | if (obj) |
| 220 | sysfs_remove_link(&node_devices[nid].sysdev.kobj, |
| 221 | kobject_name(&obj->kobj)); |
| 222 | } |
| 223 | return 0; |
| 224 | } |
| 225 | |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 226 | int register_one_node(int nid) |
| 227 | { |
| 228 | int error = 0; |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 229 | int cpu; |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 230 | |
| 231 | if (node_online(nid)) { |
| 232 | int p_node = parent_node(nid); |
| 233 | struct node *parent = NULL; |
| 234 | |
| 235 | if (p_node != nid) |
| 236 | parent = &node_devices[p_node]; |
| 237 | |
| 238 | error = register_node(&node_devices[nid], nid, parent); |
KAMEZAWA Hiroyuki | 76b67ed | 2006-06-27 02:53:41 -0700 | [diff] [blame] | 239 | |
| 240 | /* link cpu under this node */ |
| 241 | for_each_present_cpu(cpu) { |
| 242 | if (cpu_to_node(cpu) == nid) |
| 243 | register_cpu_under_node(cpu, nid); |
| 244 | } |
Yasunori Goto | 0fc4415 | 2006-06-27 02:53:38 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | return error; |
| 248 | |
| 249 | } |
| 250 | |
| 251 | void unregister_one_node(int nid) |
| 252 | { |
| 253 | unregister_node(&node_devices[nid]); |
| 254 | } |
| 255 | |
Keiichiro Tokunaga | 4b45099 | 2005-05-08 21:28:53 +0900 | [diff] [blame] | 256 | static int __init register_node_type(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
| 258 | return sysdev_class_register(&node_class); |
| 259 | } |
| 260 | postcore_initcall(register_node_type); |