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