node.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. static struct sysdev_class node_class = {
  15. set_kset_name("node"),
  16. };
  17. static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
  18. {
  19. struct node *node_dev = to_node(dev);
  20. cpumask_t mask = node_to_cpumask(node_dev->sysdev.id);
  21. int len;
  22. /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
  23. BUILD_BUG_ON(MAX_NUMNODES/4 > PAGE_SIZE/2);
  24. len = cpumask_scnprintf(buf, PAGE_SIZE-1, mask);
  25. len += sprintf(buf + len, "\n");
  26. return len;
  27. }
  28. static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
  29. #define K(x) ((x) << (PAGE_SHIFT - 10))
  30. static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
  31. {
  32. int n;
  33. int nid = dev->id;
  34. struct sysinfo i;
  35. struct page_state ps;
  36. unsigned long inactive;
  37. unsigned long active;
  38. unsigned long free;
  39. si_meminfo_node(&i, nid);
  40. get_page_state_node(&ps, nid);
  41. __get_zone_counts(&active, &inactive, &free, NODE_DATA(nid));
  42. /* Check for negative values in these approximate counters */
  43. if ((long)ps.nr_dirty < 0)
  44. ps.nr_dirty = 0;
  45. if ((long)ps.nr_writeback < 0)
  46. ps.nr_writeback = 0;
  47. n = sprintf(buf, "\n"
  48. "Node %d MemTotal: %8lu kB\n"
  49. "Node %d MemFree: %8lu kB\n"
  50. "Node %d MemUsed: %8lu kB\n"
  51. "Node %d Active: %8lu kB\n"
  52. "Node %d Inactive: %8lu kB\n"
  53. "Node %d HighTotal: %8lu kB\n"
  54. "Node %d HighFree: %8lu kB\n"
  55. "Node %d LowTotal: %8lu kB\n"
  56. "Node %d LowFree: %8lu kB\n"
  57. "Node %d Dirty: %8lu kB\n"
  58. "Node %d Writeback: %8lu kB\n"
  59. "Node %d FilePages: %8lu kB\n"
  60. "Node %d Mapped: %8lu kB\n"
  61. "Node %d AnonPages: %8lu kB\n"
  62. "Node %d PageTables: %8lu kB\n"
  63. "Node %d Slab: %8lu kB\n",
  64. nid, K(i.totalram),
  65. nid, K(i.freeram),
  66. nid, K(i.totalram - i.freeram),
  67. nid, K(active),
  68. nid, K(inactive),
  69. nid, K(i.totalhigh),
  70. nid, K(i.freehigh),
  71. nid, K(i.totalram - i.totalhigh),
  72. nid, K(i.freeram - i.freehigh),
  73. nid, K(ps.nr_dirty),
  74. nid, K(ps.nr_writeback),
  75. nid, K(node_page_state(nid, NR_FILE_PAGES)),
  76. nid, K(node_page_state(nid, NR_FILE_MAPPED)),
  77. nid, K(node_page_state(nid, NR_ANON_PAGES)),
  78. nid, K(node_page_state(nid, NR_PAGETABLE)),
  79. nid, K(node_page_state(nid, NR_SLAB)));
  80. n += hugetlb_report_node_meminfo(nid, buf + n);
  81. return n;
  82. }
  83. #undef K
  84. static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
  85. static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
  86. {
  87. unsigned long numa_hit, numa_miss, interleave_hit, numa_foreign;
  88. unsigned long local_node, other_node;
  89. int i, cpu;
  90. pg_data_t *pg = NODE_DATA(dev->id);
  91. numa_hit = 0;
  92. numa_miss = 0;
  93. interleave_hit = 0;
  94. numa_foreign = 0;
  95. local_node = 0;
  96. other_node = 0;
  97. for (i = 0; i < MAX_NR_ZONES; i++) {
  98. struct zone *z = &pg->node_zones[i];
  99. for_each_online_cpu(cpu) {
  100. struct per_cpu_pageset *ps = zone_pcp(z,cpu);
  101. numa_hit += ps->numa_hit;
  102. numa_miss += ps->numa_miss;
  103. numa_foreign += ps->numa_foreign;
  104. interleave_hit += ps->interleave_hit;
  105. local_node += ps->local_node;
  106. other_node += ps->other_node;
  107. }
  108. }
  109. return sprintf(buf,
  110. "numa_hit %lu\n"
  111. "numa_miss %lu\n"
  112. "numa_foreign %lu\n"
  113. "interleave_hit %lu\n"
  114. "local_node %lu\n"
  115. "other_node %lu\n",
  116. numa_hit,
  117. numa_miss,
  118. numa_foreign,
  119. interleave_hit,
  120. local_node,
  121. other_node);
  122. }
  123. static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
  124. static ssize_t node_read_distance(struct sys_device * dev, char * buf)
  125. {
  126. int nid = dev->id;
  127. int len = 0;
  128. int i;
  129. /* buf currently PAGE_SIZE, need ~4 chars per node */
  130. BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
  131. for_each_online_node(i)
  132. len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
  133. len += sprintf(buf + len, "\n");
  134. return len;
  135. }
  136. static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
  137. /*
  138. * register_node - Setup a driverfs device for a node.
  139. * @num - Node number to use when creating the device.
  140. *
  141. * Initialize and register the node device.
  142. */
  143. int register_node(struct node *node, int num, struct node *parent)
  144. {
  145. int error;
  146. node->sysdev.id = num;
  147. node->sysdev.cls = &node_class;
  148. error = sysdev_register(&node->sysdev);
  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. /**
  158. * unregister_node - unregister a node device
  159. * @node: node going away
  160. *
  161. * Unregisters a node device @node. All the devices on the node must be
  162. * unregistered before calling this function.
  163. */
  164. void unregister_node(struct node *node)
  165. {
  166. sysdev_remove_file(&node->sysdev, &attr_cpumap);
  167. sysdev_remove_file(&node->sysdev, &attr_meminfo);
  168. sysdev_remove_file(&node->sysdev, &attr_numastat);
  169. sysdev_remove_file(&node->sysdev, &attr_distance);
  170. sysdev_unregister(&node->sysdev);
  171. }
  172. struct node node_devices[MAX_NUMNODES];
  173. /*
  174. * register cpu under node
  175. */
  176. int register_cpu_under_node(unsigned int cpu, unsigned int nid)
  177. {
  178. if (node_online(nid)) {
  179. struct sys_device *obj = get_cpu_sysdev(cpu);
  180. if (!obj)
  181. return 0;
  182. return sysfs_create_link(&node_devices[nid].sysdev.kobj,
  183. &obj->kobj,
  184. kobject_name(&obj->kobj));
  185. }
  186. return 0;
  187. }
  188. int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
  189. {
  190. if (node_online(nid)) {
  191. struct sys_device *obj = get_cpu_sysdev(cpu);
  192. if (obj)
  193. sysfs_remove_link(&node_devices[nid].sysdev.kobj,
  194. kobject_name(&obj->kobj));
  195. }
  196. return 0;
  197. }
  198. int register_one_node(int nid)
  199. {
  200. int error = 0;
  201. int cpu;
  202. if (node_online(nid)) {
  203. int p_node = parent_node(nid);
  204. struct node *parent = NULL;
  205. if (p_node != nid)
  206. parent = &node_devices[p_node];
  207. error = register_node(&node_devices[nid], nid, parent);
  208. /* link cpu under this node */
  209. for_each_present_cpu(cpu) {
  210. if (cpu_to_node(cpu) == nid)
  211. register_cpu_under_node(cpu, nid);
  212. }
  213. }
  214. return error;
  215. }
  216. void unregister_one_node(int nid)
  217. {
  218. unregister_node(&node_devices[nid]);
  219. }
  220. static int __init register_node_type(void)
  221. {
  222. return sysdev_class_register(&node_class);
  223. }
  224. postcore_initcall(register_node_type);