cpuacct.c 8.2 KB

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