node.c 9.8 KB

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