node.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. static struct sysdev_class node_class = {
  14. set_kset_name("node"),
  15. };
  16. static ssize_t node_read_cpumap(struct sys_device * dev, char * buf)
  17. {
  18. struct node *node_dev = to_node(dev);
  19. cpumask_t mask = node_to_cpumask(node_dev->sysdev.id);
  20. int len;
  21. /* 2004/06/03: buf currently PAGE_SIZE, need > 1 char per 4 bits. */
  22. BUILD_BUG_ON(MAX_NUMNODES/4 > PAGE_SIZE/2);
  23. len = cpumask_scnprintf(buf, PAGE_SIZE-1, mask);
  24. len += sprintf(buf + len, "\n");
  25. return len;
  26. }
  27. static SYSDEV_ATTR(cpumap, S_IRUGO, node_read_cpumap, NULL);
  28. #define K(x) ((x) << (PAGE_SHIFT - 10))
  29. static ssize_t node_read_meminfo(struct sys_device * dev, char * buf)
  30. {
  31. int n;
  32. int nid = dev->id;
  33. struct sysinfo i;
  34. unsigned long inactive;
  35. unsigned long active;
  36. unsigned long free;
  37. si_meminfo_node(&i, nid);
  38. __get_zone_counts(&active, &inactive, &free, NODE_DATA(nid));
  39. n = sprintf(buf, "\n"
  40. "Node %d MemTotal: %8lu kB\n"
  41. "Node %d MemFree: %8lu kB\n"
  42. "Node %d MemUsed: %8lu kB\n"
  43. "Node %d Active: %8lu kB\n"
  44. "Node %d Inactive: %8lu kB\n"
  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. nid, K(i.totalram),
  50. nid, K(i.freeram),
  51. nid, K(i.totalram - i.freeram),
  52. nid, K(active),
  53. nid, K(inactive),
  54. nid, K(i.totalhigh),
  55. nid, K(i.freehigh),
  56. nid, K(i.totalram - i.totalhigh),
  57. nid, K(i.freeram - i.freehigh));
  58. n += hugetlb_report_node_meminfo(nid, buf + n);
  59. return n;
  60. }
  61. #undef K
  62. static SYSDEV_ATTR(meminfo, S_IRUGO, node_read_meminfo, NULL);
  63. static ssize_t node_read_numastat(struct sys_device * dev, char * buf)
  64. {
  65. unsigned long numa_hit, numa_miss, interleave_hit, numa_foreign;
  66. unsigned long local_node, other_node;
  67. int i, cpu;
  68. pg_data_t *pg = NODE_DATA(dev->id);
  69. numa_hit = 0;
  70. numa_miss = 0;
  71. interleave_hit = 0;
  72. numa_foreign = 0;
  73. local_node = 0;
  74. other_node = 0;
  75. for (i = 0; i < MAX_NR_ZONES; i++) {
  76. struct zone *z = &pg->node_zones[i];
  77. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  78. struct per_cpu_pageset *ps = &z->pageset[cpu];
  79. numa_hit += ps->numa_hit;
  80. numa_miss += ps->numa_miss;
  81. numa_foreign += ps->numa_foreign;
  82. interleave_hit += ps->interleave_hit;
  83. local_node += ps->local_node;
  84. other_node += ps->other_node;
  85. }
  86. }
  87. return sprintf(buf,
  88. "numa_hit %lu\n"
  89. "numa_miss %lu\n"
  90. "numa_foreign %lu\n"
  91. "interleave_hit %lu\n"
  92. "local_node %lu\n"
  93. "other_node %lu\n",
  94. numa_hit,
  95. numa_miss,
  96. numa_foreign,
  97. interleave_hit,
  98. local_node,
  99. other_node);
  100. }
  101. static SYSDEV_ATTR(numastat, S_IRUGO, node_read_numastat, NULL);
  102. static ssize_t node_read_distance(struct sys_device * dev, char * buf)
  103. {
  104. int nid = dev->id;
  105. int len = 0;
  106. int i;
  107. /* buf currently PAGE_SIZE, need ~4 chars per node */
  108. BUILD_BUG_ON(MAX_NUMNODES*4 > PAGE_SIZE/2);
  109. for_each_online_node(i)
  110. len += sprintf(buf + len, "%s%d", i ? " " : "", node_distance(nid, i));
  111. len += sprintf(buf + len, "\n");
  112. return len;
  113. }
  114. static SYSDEV_ATTR(distance, S_IRUGO, node_read_distance, NULL);
  115. /*
  116. * register_node - Setup a driverfs device for a node.
  117. * @num - Node number to use when creating the device.
  118. *
  119. * Initialize and register the node device.
  120. */
  121. int register_node(struct node *node, int num, struct node *parent)
  122. {
  123. int error;
  124. node->sysdev.id = num;
  125. node->sysdev.cls = &node_class;
  126. error = sysdev_register(&node->sysdev);
  127. if (!error){
  128. sysdev_create_file(&node->sysdev, &attr_cpumap);
  129. sysdev_create_file(&node->sysdev, &attr_meminfo);
  130. sysdev_create_file(&node->sysdev, &attr_numastat);
  131. sysdev_create_file(&node->sysdev, &attr_distance);
  132. }
  133. return error;
  134. }
  135. /**
  136. * unregister_node - unregister a node device
  137. * @node: node going away
  138. *
  139. * Unregisters a node device @node. All the devices on the node must be
  140. * unregistered before calling this function.
  141. */
  142. void unregister_node(struct node *node)
  143. {
  144. sysdev_remove_file(&node->sysdev, &attr_cpumap);
  145. sysdev_remove_file(&node->sysdev, &attr_meminfo);
  146. sysdev_remove_file(&node->sysdev, &attr_numastat);
  147. sysdev_remove_file(&node->sysdev, &attr_distance);
  148. sysdev_unregister(&node->sysdev);
  149. }
  150. static int __init register_node_type(void)
  151. {
  152. return sysdev_class_register(&node_class);
  153. }
  154. postcore_initcall(register_node_type);