node.c 8.8 KB

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