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