cpuacct.c 8.1 KB

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