node.c 8.3 KB

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