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