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