node.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. if ((long)ps.nr_mapped < 0)
  48. ps.nr_mapped = 0;
  49. if ((long)ps.nr_slab < 0)
  50. ps.nr_slab = 0;
  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"
  57. "Node %d HighTotal: %8lu kB\n"
  58. "Node %d HighFree: %8lu kB\n"
  59. "Node %d LowTotal: %8lu kB\n"
  60. "Node %d LowFree: %8lu kB\n"
  61. "Node %d Dirty: %8lu kB\n"
  62. "Node %d Writeback: %8lu kB\n"
  63. "Node %d Mapped: %8lu kB\n"
  64. "Node %d Slab: %8lu kB\n",
  65. nid, K(i.totalram),
  66. nid, K(i.freeram),
  67. nid, K(i.totalram - i.freeram),
  68. nid, K(active),
  69. nid, K(inactive),
  70. nid, K(i.totalhigh),
  71. nid, K(i.freehigh),
  72. nid, K(i.totalram - i.totalhigh),
  73. nid, K(i.freeram - i.freehigh),
  74. nid, K(ps.nr_dirty),
  75. nid, K(ps.nr_writeback),
  76. nid, K(ps.nr_mapped),
  77. nid, K(ps.nr_slab));
  78. n += hugetlb_report_node_meminfo(nid, buf + n);
  79. return n;
  80. }
  81. #undef K
  82. static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
  83. static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
  84. {
  85. unsigned long numa_hit, numa_miss, interleave_hit, numa_foreign;
  86. unsigned long local_node, other_node;
  87. int i, cpu;
  88. pg_data_t *pg = NODE_DATA(dev->id);
  89. numa_hit = 0;
  90. numa_miss = 0;
  91. interleave_hit = 0;
  92. numa_foreign = 0;
  93. local_node = 0;
  94. other_node = 0;
  95. for (i = 0; i < MAX_NR_ZONES; i++) {
  96. struct zone *z = &pg->node_zones[i];
  97. for_each_online_cpu(cpu) {
  98. struct per_cpu_pageset *ps = zone_pcp(z,cpu);
  99. numa_hit += ps->numa_hit;
  100. numa_miss += ps->numa_miss;
  101. numa_foreign += ps->numa_foreign;
  102. interleave_hit += ps->interleave_hit;
  103. local_node += ps->local_node;
  104. other_node += ps->other_node;
  105. }
  106. }
  107. return sprintf(buf,
  108. "numa_hit %lu\n"
  109. "numa_miss %lu\n"
  110. "numa_foreign %lu\n"
  111. "interleave_hit %lu\n"
  112. "local_node %lu\n"
  113. "other_node %lu\n",
  114. numa_hit,
  115. numa_miss,
  116. numa_foreign,
  117. interleave_hit,
  118. local_node,
  119. other_node);
  120. }
  121. static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
  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. /* buf currently PAGE_SIZE, need ~4 chars per node */
  128. BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
  129. for_each_online_node(i)
  130. len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
  131. len += sprintf(buf + len, "\n");
  132. return len;
  133. }
  134. static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
  135. /*
  136. * register_node - Setup a driverfs device for a node.
  137. * @num - Node number to use when creating the device.
  138. *
  139. * Initialize and register the node device.
  140. */
  141. int register_node(struct node *node, int num, struct node *parent)
  142. {
  143. int error;
  144. node->sysdev.id = num;
  145. node->sysdev.cls = &node_class;
  146. error = sysdev_register(&node->sysdev);
  147. if (!error){
  148. sysdev_create_file(&node->sysdev, &attr_cpumap);
  149. sysdev_create_file(&node->sysdev, &attr_meminfo);
  150. sysdev_create_file(&node->sysdev, &attr_numastat);
  151. sysdev_create_file(&node->sysdev, &attr_distance);
  152. }
  153. return error;
  154. }
  155. /**
  156. * unregister_node - unregister a node device
  157. * @node: node going away
  158. *
  159. * Unregisters a node device @node. All the devices on the node must be
  160. * unregistered before calling this function.
  161. */
  162. void unregister_node(struct node *node)
  163. {
  164. sysdev_remove_file(&node->sysdev, &attr_cpumap);
  165. sysdev_remove_file(&node->sysdev, &attr_meminfo);
  166. sysdev_remove_file(&node->sysdev, &attr_numastat);
  167. sysdev_remove_file(&node->sysdev, &attr_distance);
  168. sysdev_unregister(&node->sysdev);
  169. }
  170. struct node node_devices[MAX_NUMNODES];
  171. /*
  172. * register cpu under node
  173. */
  174. int register_cpu_under_node(unsigned int cpu, unsigned int nid)
  175. {
  176. if (node_online(nid)) {
  177. struct sys_device *obj = get_cpu_sysdev(cpu);
  178. if (!obj)
  179. return 0;
  180. return sysfs_create_link(&node_devices[nid].sysdev.kobj,
  181. &obj->kobj,
  182. kobject_name(&obj->kobj));
  183. }
  184. return 0;
  185. }
  186. int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
  187. {
  188. if (node_online(nid)) {
  189. struct sys_device *obj = get_cpu_sysdev(cpu);
  190. if (obj)
  191. sysfs_remove_link(&node_devices[nid].sysdev.kobj,
  192. kobject_name(&obj->kobj));
  193. }
  194. return 0;
  195. }
  196. int register_one_node(int nid)
  197. {
  198. int error = 0;
  199. int cpu;
  200. if (node_online(nid)) {
  201. int p_node = parent_node(nid);
  202. struct node *parent = NULL;
  203. if (p_node != nid)
  204. parent = &node_devices[p_node];
  205. error = register_node(&node_devices[nid], nid, parent);
  206. /* link cpu under this node */
  207. for_each_present_cpu(cpu) {
  208. if (cpu_to_node(cpu) == nid)
  209. register_cpu_under_node(cpu, nid);
  210. }
  211. }
  212. return error;
  213. }
  214. void unregister_one_node(int nid)
  215. {
  216. unregister_node(&node_devices[nid]);
  217. }
  218. static int __init register_node_type(void)
  219. {
  220. return sysdev_class_register(&node_class);
  221. }
  222. postcore_initcall(register_node_type);