node.c 6.1 KB

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