cputopo.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include <linux/device.h>
  2. #include <linux/proc_fs.h>
  3. #include <linux/topology.h>
  4. #include <linux/seq_file.h>
  5. #define MAX_LONG_SIZE 24
  6. struct kobject *cputopo_glb_kobj;
  7. /* tasks all */
  8. static int tasks_all_show(struct seq_file *m, void *v)
  9. {
  10. struct task_struct *g, *p;
  11. unsigned long flags;
  12. int cpu;
  13. #if 0
  14. u64 localtime = local_clock();
  15. #endif
  16. #if 0
  17. seq_printf(m, "%5llu.%06llu\n", localtime/NSEC_PER_SEC, localtime/NSEC_PER_MSEC);
  18. #endif
  19. read_lock_irqsave(&tasklist_lock, flags);
  20. for_each_online_cpu(cpu) {
  21. seq_printf(m, "cpu%d: ", cpu);
  22. do_each_thread(g, p) {
  23. if (p->flags == PF_KTHREAD || !p->mm || task_cpu(p) != cpu)
  24. continue;
  25. seq_printf(m, "%d ", task_pid_nr(p));
  26. } while_each_thread(g, p);
  27. seq_puts(m, "\n");
  28. }
  29. read_unlock_irqrestore(&tasklist_lock, flags);
  30. return 0;
  31. }
  32. static int tasks_all_open(struct inode *inode, struct file *filp)
  33. {
  34. return single_open(filp, tasks_all_show, NULL);
  35. }
  36. static const struct file_operations tasks_all_fops = {
  37. .open = tasks_all_open,
  38. .read = seq_read,
  39. .llseek = seq_lseek,
  40. .release = single_release,
  41. };
  42. /* tasks in runqueue */
  43. static int tasks_rq_show(struct seq_file *m, void *v)
  44. {
  45. struct task_struct *g, *p;
  46. unsigned long flags;
  47. int cpu;
  48. #if 0
  49. u64 localtime = local_clock();
  50. #endif
  51. #if 0
  52. seq_printf(m, "%5llu.%06llu\n", localtime/NSEC_PER_SEC, localtime/NSEC_PER_MSEC);
  53. #endif
  54. read_lock_irqsave(&tasklist_lock, flags);
  55. for_each_online_cpu(cpu) {
  56. seq_printf(m, "cpu%d: ", cpu);
  57. do_each_thread(g, p) {
  58. if (!p->on_rq || p->flags == PF_KTHREAD || !p->mm || task_cpu(p) != cpu)
  59. continue;
  60. seq_printf(m, "%d ", task_pid_nr(p));
  61. } while_each_thread(g, p);
  62. seq_puts(m, "\n");
  63. }
  64. read_unlock_irqrestore(&tasklist_lock, flags);
  65. return 0;
  66. }
  67. static int tasks_rq_open(struct inode *inode, struct file *filp)
  68. {
  69. return single_open(filp, tasks_rq_show, NULL);
  70. }
  71. static const struct file_operations tasks_rq_fops = {
  72. .open = tasks_rq_open,
  73. .read = seq_read,
  74. .llseek = seq_lseek,
  75. .release = single_release,
  76. };
  77. /*
  78. * nr_clusters attribute
  79. */
  80. static ssize_t nr_clusters_show(struct kobject *kobj,
  81. struct kobj_attribute *attr, char *buf)
  82. {
  83. return snprintf(buf, MAX_LONG_SIZE, "%u\n", arch_get_nr_clusters());
  84. }
  85. static struct kobj_attribute nr_clusters_attr = __ATTR_RO(nr_clusters);
  86. /*
  87. * is_big_little attribute
  88. */
  89. static ssize_t is_big_little_show(struct kobject *kobj,
  90. struct kobj_attribute *attr, char *buf)
  91. {
  92. return snprintf(buf, MAX_LONG_SIZE, "%u\n", !arch_is_smp());
  93. }
  94. static struct kobj_attribute is_big_little_attr = __ATTR_RO(is_big_little);
  95. /*
  96. * is_multi_cluster attribute
  97. */
  98. static ssize_t is_multi_cluster_show(struct kobject *kobj,
  99. struct kobj_attribute *attr, char *buf)
  100. {
  101. return snprintf(buf, MAX_LONG_SIZE, "%u\n", arch_is_multi_cluster());
  102. }
  103. static struct kobj_attribute is_multi_cluster_attr = __ATTR_RO(is_multi_cluster);
  104. /*
  105. * glbinfo attribute
  106. */
  107. static ssize_t glbinfo_show(struct kobject *kobj,
  108. struct kobj_attribute *attr, char *buf)
  109. {
  110. int i, len = 0;
  111. struct cpumask cpus;
  112. len += snprintf(buf + len, PAGE_SIZE - len - 1, "big/little arch: %s\n",
  113. arch_is_smp() ? "no" : "yes");
  114. len += snprintf(buf + len, PAGE_SIZE - len - 1, "nr_cups: %u\n",
  115. nr_cpu_ids);
  116. len += snprintf(buf + len, PAGE_SIZE - len - 1, "nr_clusters: %u\n",
  117. arch_get_nr_clusters());
  118. for (i = 0; i < arch_get_nr_clusters(); i++) {
  119. arch_get_cluster_cpus(&cpus, i);
  120. len += snprintf(buf + len, PAGE_SIZE - len - 1, "cluster%d: %0lx\n", i, *cpumask_bits(&cpus));
  121. }
  122. return len;
  123. }
  124. static struct kobj_attribute glbinfo_attr = __ATTR_RO(glbinfo);
  125. /*
  126. * cpus_per_cluster attribute
  127. */
  128. static ssize_t cpus_per_cluster_show(struct kobject *kobj,
  129. struct kobj_attribute *attr, char *buf)
  130. {
  131. int i, len = 0;
  132. struct cpumask cpus;
  133. for (i = 0; i < arch_get_nr_clusters(); i++) {
  134. arch_get_cluster_cpus(&cpus, i);
  135. len += snprintf(buf + len, PAGE_SIZE - len - 1, "cluster%d: %0lx\n", i, *cpumask_bits(&cpus));
  136. }
  137. return len;
  138. }
  139. static struct kobj_attribute cpus_per_cluster_attr = __ATTR_RO(cpus_per_cluster);
  140. static struct attribute *cputopo_attrs[] = {
  141. &nr_clusters_attr.attr,
  142. &is_big_little_attr.attr,
  143. &is_multi_cluster_attr.attr,
  144. &glbinfo_attr.attr,
  145. &cpus_per_cluster_attr.attr,
  146. NULL,
  147. };
  148. static struct attribute_group cputopo_attr_group = {
  149. .attrs = cputopo_attrs,
  150. };
  151. static int init_cputopo_attribs(void)
  152. {
  153. int err;
  154. struct proc_dir_entry *pe;
  155. /* Create /sys/devices/system/cpu/cputopo/... */
  156. cputopo_glb_kobj = kobject_create_and_add("cputopo", &cpu_subsys.dev_root->kobj);
  157. if (!cputopo_glb_kobj)
  158. return -ENOMEM;
  159. err = sysfs_create_group(cputopo_glb_kobj, &cputopo_attr_group);
  160. if (err)
  161. kobject_put(cputopo_glb_kobj);
  162. pe = proc_create("tasks_all", 0444, NULL, &tasks_all_fops);
  163. if (!pe)
  164. return -ENOMEM;
  165. pe = proc_create("tasks_rq", 0444, NULL, &tasks_rq_fops);
  166. if (!pe)
  167. return -ENOMEM;
  168. return err;
  169. }
  170. static int __init cputopo_info_init(void)
  171. {
  172. int ret = 0;
  173. ret = init_cputopo_attribs();
  174. return ret;
  175. }
  176. core_initcall(cputopo_info_init);