node.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. unsigned long inactive;
  36. unsigned long active;
  37. unsigned long free;
  38. si_meminfo_node(&i, nid);
  39. __get_zone_counts(&active, &inactive, &free, NODE_DATA(nid));
  40. n = sprintf(buf, "\n"
  41. "Node %d MemTotal: %8lu kB\n"
  42. "Node %d MemFree: %8lu kB\n"
  43. "Node %d MemUsed: %8lu kB\n"
  44. "Node %d Active: %8lu kB\n"
  45. "Node %d Inactive: %8lu kB\n"
  46. #ifdef CONFIG_HIGHMEM
  47. "Node %d HighTotal: %8lu kB\n"
  48. "Node %d HighFree: %8lu kB\n"
  49. "Node %d LowTotal: %8lu kB\n"
  50. "Node %d LowFree: %8lu kB\n"
  51. #endif
  52. "Node %d Dirty: %8lu kB\n"
  53. "Node %d Writeback: %8lu kB\n"
  54. "Node %d FilePages: %8lu kB\n"
  55. "Node %d Mapped: %8lu kB\n"
  56. "Node %d AnonPages: %8lu kB\n"
  57. "Node %d PageTables: %8lu kB\n"
  58. "Node %d NFS_Unstable: %8lu kB\n"
  59. "Node %d Bounce: %8lu kB\n"
  60. "Node %d Slab: %8lu kB\n"
  61. "Node %d SReclaimable: %8lu kB\n"
  62. "Node %d SUnreclaim: %8lu kB\n",
  63. nid, K(i.totalram),
  64. nid, K(i.freeram),
  65. nid, K(i.totalram - i.freeram),
  66. nid, K(active),
  67. nid, K(inactive),
  68. #ifdef CONFIG_HIGHMEM
  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. #endif
  74. nid, K(node_page_state(nid, NR_FILE_DIRTY)),
  75. nid, K(node_page_state(nid, NR_WRITEBACK)),
  76. nid, K(node_page_state(nid, NR_FILE_PAGES)),
  77. nid, K(node_page_state(nid, NR_FILE_MAPPED)),
  78. nid, K(node_page_state(nid, NR_ANON_PAGES)),
  79. nid, K(node_page_state(nid, NR_PAGETABLE)),
  80. nid, K(node_page_state(nid, NR_UNSTABLE_NFS)),
  81. nid, K(node_page_state(nid, NR_BOUNCE)),
  82. nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE) +
  83. node_page_state(nid, NR_SLAB_UNRECLAIMABLE)),
  84. nid, K(node_page_state(nid, NR_SLAB_RECLAIMABLE)),
  85. nid, K(node_page_state(nid, NR_SLAB_UNRECLAIMABLE)));
  86. n += hugetlb_report_node_meminfo(nid, buf + n);
  87. return n;
  88. }
  89. #undef K
  90. static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
  91. static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
  92. {
  93. return sprintf(buf,
  94. "numa_hit %lu\n"
  95. "numa_miss %lu\n"
  96. "numa_foreign %lu\n"
  97. "interleave_hit %lu\n"
  98. "local_node %lu\n"
  99. "other_node %lu\n",
  100. node_page_state(dev->id, NUMA_HIT),
  101. node_page_state(dev->id, NUMA_MISS),
  102. node_page_state(dev->id, NUMA_FOREIGN),
  103. node_page_state(dev->id, NUMA_INTERLEAVE_HIT),
  104. node_page_state(dev->id, NUMA_LOCAL),
  105. node_page_state(dev->id, NUMA_OTHER));
  106. }
  107. static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
  108. static ssize_t node_read_distance(struct sys_device * dev, char * buf)
  109. {
  110. int nid = dev->id;
  111. int len = 0;
  112. int i;
  113. /* buf currently PAGE_SIZE, need ~4 chars per node */
  114. BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
  115. for_each_online_node(i)
  116. len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
  117. len += sprintf(buf + len, "\n");
  118. return len;
  119. }
  120. static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
  121. /*
  122. * register_node - Setup a driverfs device for a node.
  123. * @num - Node number to use when creating the device.
  124. *
  125. * Initialize and register the node device.
  126. */
  127. int register_node(struct node *node, int num, struct node *parent)
  128. {
  129. int error;
  130. node->sysdev.id = num;
  131. node->sysdev.cls = &node_class;
  132. error = sysdev_register(&node->sysdev);
  133. if (!error){
  134. sysdev_create_file(&node->sysdev, &attr_cpumap);
  135. sysdev_create_file(&node->sysdev, &attr_meminfo);
  136. sysdev_create_file(&node->sysdev, &attr_numastat);
  137. sysdev_create_file(&node->sysdev, &attr_distance);
  138. }
  139. return error;
  140. }
  141. /**
  142. * unregister_node - unregister a node device
  143. * @node: node going away
  144. *
  145. * Unregisters a node device @node. All the devices on the node must be
  146. * unregistered before calling this function.
  147. */
  148. void unregister_node(struct node *node)
  149. {
  150. sysdev_remove_file(&node->sysdev, &attr_cpumap);
  151. sysdev_remove_file(&node->sysdev, &attr_meminfo);
  152. sysdev_remove_file(&node->sysdev, &attr_numastat);
  153. sysdev_remove_file(&node->sysdev, &attr_distance);
  154. sysdev_unregister(&node->sysdev);
  155. }
  156. struct node node_devices[MAX_NUMNODES];
  157. /*
  158. * register cpu under node
  159. */
  160. int register_cpu_under_node(unsigned int cpu, unsigned int nid)
  161. {
  162. if (node_online(nid)) {
  163. struct sys_device *obj = get_cpu_sysdev(cpu);
  164. if (!obj)
  165. return 0;
  166. return sysfs_create_link(&node_devices[nid].sysdev.kobj,
  167. &obj->kobj,
  168. kobject_name(&obj->kobj));
  169. }
  170. return 0;
  171. }
  172. int unregister_cpu_under_node(unsigned int cpu, unsigned int nid)
  173. {
  174. if (node_online(nid)) {
  175. struct sys_device *obj = get_cpu_sysdev(cpu);
  176. if (obj)
  177. sysfs_remove_link(&node_devices[nid].sysdev.kobj,
  178. kobject_name(&obj->kobj));
  179. }
  180. return 0;
  181. }
  182. int register_one_node(int nid)
  183. {
  184. int error = 0;
  185. int cpu;
  186. if (node_online(nid)) {
  187. int p_node = parent_node(nid);
  188. struct node *parent = NULL;
  189. if (p_node != nid)
  190. parent = &node_devices[p_node];
  191. error = register_node(&node_devices[nid], nid, parent);
  192. /* link cpu under this node */
  193. for_each_present_cpu(cpu) {
  194. if (cpu_to_node(cpu) == nid)
  195. register_cpu_under_node(cpu, nid);
  196. }
  197. }
  198. return error;
  199. }
  200. void unregister_one_node(int nid)
  201. {
  202. unregister_node(&node_devices[nid]);
  203. }
  204. static int __init register_node_type(void)
  205. {
  206. return sysdev_class_register(&node_class);
  207. }
  208. postcore_initcall(register_node_type);