node.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * drivers/base/node.c - basic Node class support
  3. */
  4. #include <linux/sysdev.h>
  5. #include <linux/module.h>
  6. #include <linux/init.h>
  7. #include <linux/mm.h>
  8. #include <linux/node.h>
  9. #include <linux/hugetlb.h>
  10. #include <linux/cpumask.h>
  11. #include <linux/topology.h>
  12. #include <linux/nodemask.h>
  13. #include <linux/cpu.h>
  14. #include <linux/device.h>
  15. static struct sysdev_class node_class = {
  16. set_kset_name("node"),
  17. };
  18. static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
  19. {
  20. struct node *node_dev = to_node(dev);
  21. cpumask_t mask = node_to_cpumask(node_dev->sysdev.id);
  22. int len;
  23. /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
  24. BUILD_BUG_ON(MAX_NUMNODES/4 > PAGE_SIZE/2);
  25. len = cpumask_scnprintf(buf, PAGE_SIZE-1, mask);
  26. len += sprintf(buf + len, "\n");
  27. return len;
  28. }
  29. static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
  30. #define K(x) ((x) << (PAGE_SHIFT - 10))
  31. static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
  32. {
  33. int n;
  34. int nid = dev->id;
  35. struct sysinfo i;
  36. si_meminfo_node(&i, nid);
  37. n = sprintf(buf, "\n"
  38. "Node %d MemTotal: %8lu kB\n"
  39. "Node %d MemFree: %8lu kB\n"
  40. "Node %d MemUsed: %8lu kB\n"
  41. "Node %d Active: %8lu kB\n"
  42. "Node %d Inactive: %8lu kB\n"
  43. #ifdef CONFIG_HIGHMEM
  44. "Node %d HighTotal: %8lu kB\n"
  45. "Node %d HighFree: %8lu kB\n"
  46. "Node %d LowTotal: %8lu kB\n"
  47. "Node %d LowFree: %8lu kB\n"
  48. #endif
  49. "Node %d Dirty: %8lu kB\n"
  50. "Node %d Writeback: %8lu kB\n"
  51. "Node %d FilePages: %8lu kB\n"
  52. "Node %d Mapped: %8lu kB\n"
  53. "Node %d AnonPages: %8lu kB\n"
  54. "Node %d PageTables: %8lu kB\n"
  55. "Node %d NFS_Unstable: %8lu kB\n"
  56. "Node %d Bounce: %8lu kB\n"
  57. "Node %d Slab: %8lu kB\n"
  58. "Node %d SReclaimable: %8lu kB\n"
  59. "Node %d SUnreclaim: %8lu kB\n",
  60. nid, K(i.totalram),
  61. nid, K(i.freeram),
  62. nid, K(i.totalram - i.freeram),
  63. nid, node_page_state(nid, NR_ACTIVE),
  64. nid, node_page_state(nid, NR_INACTIVE),
  65. #ifdef CONFIG_HIGHMEM
  66. nid, K(i.totalhigh),
  67. nid, K(i.freehigh),
  68. nid, K(i.totalram - i.totalhigh),
  69. nid, K(i.freeram - i.freehigh),
  70. #endif
  71. nid, K(node_page_state(nid, NR_FILE_DIRTY)),
  72. nid, K(node_page_state(nid, NR_WRITEBACK)),
  73. nid, K(node_page_state(nid, NR_FILE_PAGES)),
  74. nid, K(node_page_state(nid, NR_FILE_MAPPED)),
  75. nid, K(node_page_state(nid, NR_ANON_PAGES)),
  76. nid, K(node_page_state(nid, NR_PAGETABLE)),
  77. nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
  78. nid, K(node_page_state(nid, NR_BOUNCE)),
  79. nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
  80. node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
  81. nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
  82. nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
  83. n += hugetlb_report_node_meminfo(nid, buf + n);
  84. return n;
  85. }
  86. #undef K
  87. static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
  88. static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
  89. {
  90. return sprintf(buf,
  91. "numa_hit %lu\n"
  92. "numa_miss %lu\n"
  93. "numa_foreign %lu\n"
  94. "interleave_hit %lu\n"
  95. "local_node %lu\n"
  96. "other_node %lu\n",
  97. node_page_state(dev->id, NUMA_HIT),
  98. node_page_state(dev->id, NUMA_MISS),
  99. node_page_state(dev->id, NUMA_FOREIGN),
  100. node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
  101. node_page_state(dev->id, NUMA_LOCAL),
  102. node_page_state(dev->id, NUMA_OTHER));
  103. }
  104. static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
  105. static ssize_t node_read_distance(struct sys_device * dev, char * buf)
  106. {
  107. int nid = dev->id;
  108. int len = 0;
  109. int i;
  110. /* buf currently PAGE_SIZE, need ~4 chars per node */
  111. BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
  112. for_each_online_node(i)
  113. len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
  114. len += sprintf(buf + len, "\n");
  115. return len;
  116. }
  117. static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
  118. /*
  119. * register_node - Setup a sysfs device for a node.
  120. * @num - Node number to use when creating the device.
  121. *
  122. * Initialize and register the node device.
  123. */
  124. int register_node(struct node *node, int num, struct node *parent)
  125. {
  126. int error;
  127. node->sysdev.id = num;
  128. node->sysdev.cls = &node_class;
  129. error = sysdev_register(&node->sysdev);
  130. if (!error){
  131. sysdev_create_file(&node->sysdev, &attr_cpumap);
  132. sysdev_create_file(&node->sysdev, &attr_meminfo);
  133. sysdev_create_file(&node->sysdev, &attr_numastat);
  134. sysdev_create_file(&node->sysdev, &attr_distance);
  135. }
  136. return error;
  137. }
  138. /**
  139. * unregister_node - unregister a node device
  140. * @node: node going away
  141. *
  142. * Unregisters a node device @node. All the devices on the node must be
  143. * unregistered before calling this function.
  144. */
  145. void unregister_node(struct node *node)
  146. {
  147. sysdev_remove_file(&node->sysdev, &attr_cpumap);
  148. sysdev_remove_file(&node->sysdev, &attr_meminfo);
  149. sysdev_remove_file(&node->sysdev, &attr_numastat);
  150. sysdev_remove_file(&node->sysdev, &attr_distance);
  151. sysdev_unregister(&node->sysdev);
  152. }
  153. struct node node_devices[MAX_NUMNODES];
  154. /*
  155. * register cpu under node
  156. */
  157. int register_cpu_under_node(unsigned int cpu, unsigned int nid)
  158. {
  159. if (node_online(nid)) {
  160. struct sys_device *obj = get_cpu_sysdev(cpu);
  161. if (!obj)
  162. return 0;
  163. return sysfs_create_link(&node_devices[nid].sysdev.kobj,
  164. &obj->kobj,
  165. kobject_name(&obj->kobj));
  166. }
  167. return 0;
  168. }
  169. int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
  170. {
  171. if (node_online(nid)) {
  172. struct sys_device *obj = get_cpu_sysdev(cpu);
  173. if (obj)
  174. sysfs_remove_link(&node_devices[nid].sysdev.kobj,
  175. kobject_name(&obj->kobj));
  176. }
  177. return 0;
  178. }
  179. int register_one_node(int nid)
  180. {
  181. int error = 0;
  182. int cpu;
  183. if (node_online(nid)) {
  184. int p_node = parent_node(nid);
  185. struct node *parent = NULL;
  186. if (p_node != nid)
  187. parent = &node_devices[p_node];
  188. error = register_node(&node_devices[nid], nid, parent);
  189. /* link cpu under this node */
  190. for_each_present_cpu(cpu) {
  191. if (cpu_to_node(cpu) == nid)
  192. register_cpu_under_node(cpu, nid);
  193. }
  194. }
  195. return error;
  196. }
  197. void unregister_one_node(int nid)
  198. {
  199. unregister_node(&node_devices[nid]);
  200. }
  201. /*
  202. * node states attributes
  203. */
  204. static ssize_t print_nodes_state(enum node_states state, char *buf)
  205. {
  206. int n;
  207. n = nodelist_scnprintf(buf, PAGE_SIZE, node_states[state]);
  208. if (n > 0 && PAGE_SIZE > n + 1) {
  209. *(buf + n++) = '\n';
  210. *(buf + n++) = '\0';
  211. }
  212. return n;
  213. }
  214. static ssize_t print_nodes_possible(struct sysdev_class *class, char *buf)
  215. {
  216. return print_nodes_state(N_POSSIBLE, buf);
  217. }
  218. static ssize_t print_nodes_online(struct sysdev_class *class, char *buf)
  219. {
  220. return print_nodes_state(N_ONLINE, buf);
  221. }
  222. static ssize_t print_nodes_has_normal_memory(struct sysdev_class *class,
  223. char *buf)
  224. {
  225. return print_nodes_state(N_NORMAL_MEMORY, buf);
  226. }
  227. static ssize_t print_nodes_has_cpu(struct sysdev_class *class, char *buf)
  228. {
  229. return print_nodes_state(N_CPU, buf);
  230. }
  231. static SYSDEV_CLASS_ATTR(possible, 0444, print_nodes_possible, NULL);
  232. static SYSDEV_CLASS_ATTR(online, 0444, print_nodes_online, NULL);
  233. static SYSDEV_CLASS_ATTR(has_normal_memory, 0444, print_nodes_has_normal_memory,
  234. NULL);
  235. static SYSDEV_CLASS_ATTR(has_cpu, 0444, print_nodes_has_cpu, NULL);
  236. #ifdef CONFIG_HIGHMEM
  237. static ssize_t print_nodes_has_high_memory(struct sysdev_class *class,
  238. char *buf)
  239. {
  240. return print_nodes_state(N_HIGH_MEMORY, buf);
  241. }
  242. static SYSDEV_CLASS_ATTR(has_high_memory, 0444, print_nodes_has_high_memory,
  243. NULL);
  244. #endif
  245. struct sysdev_class_attribute *node_state_attr[] = {
  246. &attr_possible,
  247. &attr_online,
  248. &attr_has_normal_memory,
  249. #ifdef CONFIG_HIGHMEM
  250. &attr_has_high_memory,
  251. #endif
  252. &attr_has_cpu,
  253. };
  254. static int node_states_init(void)
  255. {
  256. int i;
  257. int err = 0;
  258. for (i = 0; i < NR_NODE_STATES; i++) {
  259. int ret;
  260. ret = sysdev_class_create_file(&node_class, node_state_attr[i]);
  261. if (!err)
  262. err = ret;
  263. }
  264. return err;
  265. }
  266. static int __init register_node_type(void)
  267. {
  268. int ret;
  269. ret = sysdev_class_register(&node_class);
  270. if (!ret)
  271. ret = node_states_init();
  272. /*
  273. * Note: we're not going to unregister the node class if we fail
  274. * to register the node state class attribute files.
  275. */
  276. return ret;
  277. }
  278. postcore_initcall(register_node_type);