cpuacct.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include <linux/cgroup.h>
  2. #include <linux/slab.h>
  3. #include <linux/percpu.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/cpumask.h>
  6. #include <linux/seq_file.h>
  7. #include <linux/rcupdate.h>
  8. #include <linux/kernel_stat.h>
  9. #include <linux/err.h>
  10. #include "sched.h"
  11. /*
  12. * CPU accounting code for task groups.
  13. *
  14. * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
  15. * (balbir@in.ibm.com).
  16. */
  17. /* Time spent by the tasks of the cpu accounting group executing in ... */
  18. enum cpuacct_stat_index {
  19. CPUACCT_STAT_USER, /* ... user mode */
  20. CPUACCT_STAT_SYSTEM, /* ... kernel mode */
  21. CPUACCT_STAT_NSTATS,
  22. };
  23. enum cpuacct_usage_index {
  24. CPUACCT_USAGE_USER, /* ... user mode */
  25. CPUACCT_USAGE_SYSTEM, /* ... kernel mode */
  26. CPUACCT_USAGE_NRUSAGE,
  27. };
  28. struct cpuacct_usage {
  29. u64 usages[CPUACCT_USAGE_NRUSAGE];
  30. };
  31. /* track cpu usage of a group of tasks and its child groups */
  32. struct cpuacct {
  33. struct cgroup_subsys_state css;
  34. /* cpuusage holds pointer to a u64-type object on every cpu */
  35. struct cpuacct_usage __percpu *cpuusage;
  36. struct kernel_cpustat __percpu *cpustat;
  37. };
  38. static inline struct cpuacct *css_ca(struct cgroup_subsys_state *css)
  39. {
  40. return css ? container_of(css, struct cpuacct, css) : NULL;
  41. }
  42. /* return cpu accounting group to which this task belongs */
  43. static inline struct cpuacct *task_ca(struct task_struct *tsk)
  44. {
  45. return css_ca(task_css(tsk, cpuacct_cgrp_id));
  46. }
  47. static inline struct cpuacct *parent_ca(struct cpuacct *ca)
  48. {
  49. return css_ca(ca->css.parent);
  50. }
  51. static DEFINE_PER_CPU(struct cpuacct_usage, root_cpuacct_cpuusage);
  52. static struct cpuacct root_cpuacct = {
  53. .cpustat = &kernel_cpustat,
  54. .cpuusage = &root_cpuacct_cpuusage,
  55. };
  56. /* create a new cpu accounting group */
  57. static struct cgroup_subsys_state *
  58. cpuacct_css_alloc(struct cgroup_subsys_state *parent_css)
  59. {
  60. struct cpuacct *ca;
  61. if (!parent_css)
  62. return &root_cpuacct.css;
  63. ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  64. if (!ca)
  65. goto out;
  66. ca->cpuusage = alloc_percpu(struct cpuacct_usage);
  67. if (!ca->cpuusage)
  68. goto out_free_ca;
  69. ca->cpustat = alloc_percpu(struct kernel_cpustat);
  70. if (!ca->cpustat)
  71. goto out_free_cpuusage;
  72. return &ca->css;
  73. out_free_cpuusage:
  74. free_percpu(ca->cpuusage);
  75. out_free_ca:
  76. kfree(ca);
  77. out:
  78. return ERR_PTR(-ENOMEM);
  79. }
  80. /* destroy an existing cpu accounting group */
  81. static void cpuacct_css_free(struct cgroup_subsys_state *css)
  82. {
  83. struct cpuacct *ca = css_ca(css);
  84. free_percpu(ca->cpustat);
  85. free_percpu(ca->cpuusage);
  86. kfree(ca);
  87. }
  88. static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu,
  89. enum cpuacct_usage_index index)
  90. {
  91. struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  92. u64 data;
  93. /*
  94. * We allow index == CPUACCT_USAGE_NRUSAGE here to read
  95. * the sum of suages.
  96. */
  97. BUG_ON(index > CPUACCT_USAGE_NRUSAGE);
  98. #ifndef CONFIG_64BIT
  99. /*
  100. * Take rq->lock to make 64-bit read safe on 32-bit platforms.
  101. */
  102. raw_spin_lock_irq(&cpu_rq(cpu)->lock);
  103. #endif
  104. if (index == CPUACCT_USAGE_NRUSAGE) {
  105. int i = 0;
  106. data = 0;
  107. for (i = 0; i < CPUACCT_USAGE_NRUSAGE; i++)
  108. data += cpuusage->usages[i];
  109. } else {
  110. data = cpuusage->usages[index];
  111. }
  112. #ifndef CONFIG_64BIT
  113. raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
  114. #endif
  115. return data;
  116. }
  117. static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
  118. {
  119. struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  120. int i;
  121. #ifndef CONFIG_64BIT
  122. /*
  123. * Take rq->lock to make 64-bit write safe on 32-bit platforms.
  124. */
  125. raw_spin_lock_irq(&cpu_rq(cpu)->lock);
  126. #endif
  127. for (i = 0; i < CPUACCT_USAGE_NRUSAGE; i++)
  128. cpuusage->usages[i] = val;
  129. #ifndef CONFIG_64BIT
  130. raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
  131. #endif
  132. }
  133. /* return total cpu usage (in nanoseconds) of a group */
  134. static u64 __cpuusage_read(struct cgroup_subsys_state *css,
  135. enum cpuacct_usage_index index)
  136. {
  137. struct cpuacct *ca = css_ca(css);
  138. u64 totalcpuusage = 0;
  139. int i;
  140. for_each_possible_cpu(i)
  141. totalcpuusage += cpuacct_cpuusage_read(ca, i, index);
  142. return totalcpuusage;
  143. }
  144. static u64 cpuusage_user_read(struct cgroup_subsys_state *css,
  145. struct cftype *cft)
  146. {
  147. return __cpuusage_read(css, CPUACCT_USAGE_USER);
  148. }
  149. static u64 cpuusage_sys_read(struct cgroup_subsys_state *css,
  150. struct cftype *cft)
  151. {
  152. return __cpuusage_read(css, CPUACCT_USAGE_SYSTEM);
  153. }
  154. static u64 cpuusage_read(struct cgroup_subsys_state *css, struct cftype *cft)
  155. {
  156. return __cpuusage_read(css, CPUACCT_USAGE_NRUSAGE);
  157. }
  158. static int cpuusage_write(struct cgroup_subsys_state *css, struct cftype *cft,
  159. u64 val)
  160. {
  161. struct cpuacct *ca = css_ca(css);
  162. int cpu;
  163. /*
  164. * Only allow '0' here to do a reset.
  165. */
  166. if (val)
  167. return -EINVAL;
  168. for_each_possible_cpu(cpu)
  169. cpuacct_cpuusage_write(ca, cpu, 0);
  170. return 0;
  171. }
  172. static int __cpuacct_percpu_seq_show(struct seq_file *m,
  173. enum cpuacct_usage_index index)
  174. {
  175. struct cpuacct *ca = css_ca(seq_css(m));
  176. u64 percpu;
  177. int i;
  178. for_each_possible_cpu(i) {
  179. percpu = cpuacct_cpuusage_read(ca, i, index);
  180. seq_printf(m, "%llu ", (unsigned long long) percpu);
  181. }
  182. seq_printf(m, "\n");
  183. return 0;
  184. }
  185. static int cpuacct_percpu_user_seq_show(struct seq_file *m, void *V)
  186. {
  187. return __cpuacct_percpu_seq_show(m, CPUACCT_USAGE_USER);
  188. }
  189. static int cpuacct_percpu_sys_seq_show(struct seq_file *m, void *V)
  190. {
  191. return __cpuacct_percpu_seq_show(m, CPUACCT_USAGE_SYSTEM);
  192. }
  193. static int cpuacct_percpu_seq_show(struct seq_file *m, void *V)
  194. {
  195. return __cpuacct_percpu_seq_show(m, CPUACCT_USAGE_NRUSAGE);
  196. }
  197. static const char * const cpuacct_stat_desc[] = {
  198. [CPUACCT_STAT_USER] = "user",
  199. [CPUACCT_STAT_SYSTEM] = "system",
  200. };
  201. static int cpuacct_stats_show(struct seq_file *sf, void *v)
  202. {
  203. struct cpuacct *ca = css_ca(seq_css(sf));
  204. int cpu;
  205. s64 val = 0;
  206. for_each_possible_cpu(cpu) {
  207. struct kernel_cpustat *kcpustat = per_cpu_ptr(ca->cpustat, cpu);
  208. val += kcpustat->cpustat[CPUTIME_USER];
  209. val += kcpustat->cpustat[CPUTIME_NICE];
  210. }
  211. val = cputime64_to_clock_t(val);
  212. seq_printf(sf, "%s %lld\n", cpuacct_stat_desc[CPUACCT_STAT_USER], val);
  213. val = 0;
  214. for_each_possible_cpu(cpu) {
  215. struct kernel_cpustat *kcpustat = per_cpu_ptr(ca->cpustat, cpu);
  216. val += kcpustat->cpustat[CPUTIME_SYSTEM];
  217. val += kcpustat->cpustat[CPUTIME_IRQ];
  218. val += kcpustat->cpustat[CPUTIME_SOFTIRQ];
  219. }
  220. val = cputime64_to_clock_t(val);
  221. seq_printf(sf, "%s %lld\n", cpuacct_stat_desc[CPUACCT_STAT_SYSTEM], val);
  222. return 0;
  223. }
  224. static struct cftype files[] = {
  225. {
  226. .name = "usage",
  227. .read_u64 = cpuusage_read,
  228. .write_u64 = cpuusage_write,
  229. },
  230. {
  231. .name = "usage_user",
  232. .read_u64 = cpuusage_user_read,
  233. },
  234. {
  235. .name = "usage_sys",
  236. .read_u64 = cpuusage_sys_read,
  237. },
  238. {
  239. .name = "usage_percpu",
  240. .seq_show = cpuacct_percpu_seq_show,
  241. },
  242. {
  243. .name = "usage_percpu_user",
  244. .seq_show = cpuacct_percpu_user_seq_show,
  245. },
  246. {
  247. .name = "usage_percpu_sys",
  248. .seq_show = cpuacct_percpu_sys_seq_show,
  249. },
  250. {
  251. .name = "stat",
  252. .seq_show = cpuacct_stats_show,
  253. },
  254. { } /* terminate */
  255. };
  256. /*
  257. * charge this task's execution time to its accounting group.
  258. *
  259. * called with rq->lock held.
  260. */
  261. void cpuacct_charge(struct task_struct *tsk, u64 cputime)
  262. {
  263. struct cpuacct *ca;
  264. int index = CPUACCT_USAGE_SYSTEM;
  265. struct pt_regs *regs = task_pt_regs(tsk);
  266. if (regs && user_mode(regs))
  267. index = CPUACCT_USAGE_USER;
  268. rcu_read_lock();
  269. for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
  270. this_cpu_ptr(ca->cpuusage)->usages[index] += cputime;
  271. rcu_read_unlock();
  272. }
  273. /*
  274. * Add user/system time to cpuacct.
  275. *
  276. * Note: it's the caller that updates the account of the root cgroup.
  277. */
  278. void cpuacct_account_field(struct task_struct *tsk, int index, u64 val)
  279. {
  280. struct cpuacct *ca;
  281. rcu_read_lock();
  282. for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca))
  283. this_cpu_ptr(ca->cpustat)->cpustat[index] += val;
  284. rcu_read_unlock();
  285. }
  286. struct cgroup_subsys cpuacct_cgrp_subsys = {
  287. .css_alloc = cpuacct_css_alloc,
  288. .css_free = cpuacct_css_free,
  289. .legacy_cftypes = files,
  290. .early_init = true,
  291. };