Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 2 | #include <linux/cgroup.h> |
| 3 | #include <linux/slab.h> |
| 4 | #include <linux/percpu.h> |
| 5 | #include <linux/spinlock.h> |
| 6 | #include <linux/cpumask.h> |
| 7 | #include <linux/seq_file.h> |
| 8 | #include <linux/rcupdate.h> |
| 9 | #include <linux/kernel_stat.h> |
Ingo Molnar | b329fd5 | 2013-04-10 15:10:50 +0200 | [diff] [blame] | 10 | #include <linux/err.h> |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 11 | |
| 12 | #include "sched.h" |
| 13 | |
| 14 | /* |
| 15 | * CPU accounting code for task groups. |
| 16 | * |
| 17 | * Based on the work by Paul Menage (menage@google.com) and Balbir Singh |
| 18 | * (balbir@in.ibm.com). |
| 19 | */ |
| 20 | |
Li Zefan | d171279 | 2013-03-29 14:38:13 +0800 | [diff] [blame] | 21 | /* Time spent by the tasks of the cpu accounting group executing in ... */ |
| 22 | enum cpuacct_stat_index { |
| 23 | CPUACCT_STAT_USER, /* ... user mode */ |
| 24 | CPUACCT_STAT_SYSTEM, /* ... kernel mode */ |
| 25 | |
| 26 | CPUACCT_STAT_NSTATS, |
| 27 | }; |
| 28 | |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 29 | static const char * const cpuacct_stat_desc[] = { |
| 30 | [CPUACCT_STAT_USER] = "user", |
| 31 | [CPUACCT_STAT_SYSTEM] = "system", |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | struct cpuacct_usage { |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 35 | u64 usages[CPUACCT_STAT_NSTATS]; |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 36 | }; |
| 37 | |
Li Zefan | d171279 | 2013-03-29 14:38:13 +0800 | [diff] [blame] | 38 | /* track cpu usage of a group of tasks and its child groups */ |
| 39 | struct cpuacct { |
| 40 | struct cgroup_subsys_state css; |
| 41 | /* cpuusage holds pointer to a u64-type object on every cpu */ |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 42 | struct cpuacct_usage __percpu *cpuusage; |
Li Zefan | d171279 | 2013-03-29 14:38:13 +0800 | [diff] [blame] | 43 | struct kernel_cpustat __percpu *cpustat; |
| 44 | }; |
| 45 | |
Tejun Heo | a7c6d55 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 46 | static inline struct cpuacct *css_ca(struct cgroup_subsys_state *css) |
| 47 | { |
| 48 | return css ? container_of(css, struct cpuacct, css) : NULL; |
| 49 | } |
| 50 | |
Li Zefan | d171279 | 2013-03-29 14:38:13 +0800 | [diff] [blame] | 51 | /* return cpu accounting group to which this task belongs */ |
| 52 | static inline struct cpuacct *task_ca(struct task_struct *tsk) |
| 53 | { |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 54 | return css_ca(task_css(tsk, cpuacct_cgrp_id)); |
Li Zefan | d171279 | 2013-03-29 14:38:13 +0800 | [diff] [blame] | 55 | } |
| 56 | |
Li Zefan | d171279 | 2013-03-29 14:38:13 +0800 | [diff] [blame] | 57 | static inline struct cpuacct *parent_ca(struct cpuacct *ca) |
| 58 | { |
Tejun Heo | 5c9d535 | 2014-05-16 13:22:48 -0400 | [diff] [blame] | 59 | return css_ca(ca->css.parent); |
Li Zefan | d171279 | 2013-03-29 14:38:13 +0800 | [diff] [blame] | 60 | } |
| 61 | |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 62 | static DEFINE_PER_CPU(struct cpuacct_usage, root_cpuacct_cpuusage); |
Li Zefan | 14c6d3c | 2013-03-29 14:44:04 +0800 | [diff] [blame] | 63 | static struct cpuacct root_cpuacct = { |
| 64 | .cpustat = &kernel_cpustat, |
| 65 | .cpuusage = &root_cpuacct_cpuusage, |
| 66 | }; |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 67 | |
| 68 | /* create a new cpu accounting group */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 69 | static struct cgroup_subsys_state * |
| 70 | cpuacct_css_alloc(struct cgroup_subsys_state *parent_css) |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 71 | { |
| 72 | struct cpuacct *ca; |
| 73 | |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 74 | if (!parent_css) |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 75 | return &root_cpuacct.css; |
| 76 | |
| 77 | ca = kzalloc(sizeof(*ca), GFP_KERNEL); |
| 78 | if (!ca) |
| 79 | goto out; |
| 80 | |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 81 | ca->cpuusage = alloc_percpu(struct cpuacct_usage); |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 82 | if (!ca->cpuusage) |
| 83 | goto out_free_ca; |
| 84 | |
| 85 | ca->cpustat = alloc_percpu(struct kernel_cpustat); |
| 86 | if (!ca->cpustat) |
| 87 | goto out_free_cpuusage; |
| 88 | |
| 89 | return &ca->css; |
| 90 | |
| 91 | out_free_cpuusage: |
| 92 | free_percpu(ca->cpuusage); |
| 93 | out_free_ca: |
| 94 | kfree(ca); |
| 95 | out: |
| 96 | return ERR_PTR(-ENOMEM); |
| 97 | } |
| 98 | |
| 99 | /* destroy an existing cpu accounting group */ |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 100 | static void cpuacct_css_free(struct cgroup_subsys_state *css) |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 101 | { |
Tejun Heo | eb95419 | 2013-08-08 20:11:23 -0400 | [diff] [blame] | 102 | struct cpuacct *ca = css_ca(css); |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 103 | |
| 104 | free_percpu(ca->cpustat); |
| 105 | free_percpu(ca->cpuusage); |
| 106 | kfree(ca); |
| 107 | } |
| 108 | |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 109 | static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu, |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 110 | enum cpuacct_stat_index index) |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 111 | { |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 112 | struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 113 | u64 data; |
| 114 | |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 115 | /* |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 116 | * We allow index == CPUACCT_STAT_NSTATS here to read |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 117 | * the sum of suages. |
| 118 | */ |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 119 | BUG_ON(index > CPUACCT_STAT_NSTATS); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 120 | |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 121 | #ifndef CONFIG_64BIT |
| 122 | /* |
| 123 | * Take rq->lock to make 64-bit read safe on 32-bit platforms. |
| 124 | */ |
| 125 | raw_spin_lock_irq(&cpu_rq(cpu)->lock); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 126 | #endif |
| 127 | |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 128 | if (index == CPUACCT_STAT_NSTATS) { |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 129 | int i = 0; |
| 130 | |
| 131 | data = 0; |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 132 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 133 | data += cpuusage->usages[i]; |
| 134 | } else { |
| 135 | data = cpuusage->usages[index]; |
| 136 | } |
| 137 | |
| 138 | #ifndef CONFIG_64BIT |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 139 | raw_spin_unlock_irq(&cpu_rq(cpu)->lock); |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 140 | #endif |
| 141 | |
| 142 | return data; |
| 143 | } |
| 144 | |
| 145 | static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val) |
| 146 | { |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 147 | struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); |
| 148 | int i; |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 149 | |
| 150 | #ifndef CONFIG_64BIT |
| 151 | /* |
| 152 | * Take rq->lock to make 64-bit write safe on 32-bit platforms. |
| 153 | */ |
| 154 | raw_spin_lock_irq(&cpu_rq(cpu)->lock); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 155 | #endif |
| 156 | |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 157 | for (i = 0; i < CPUACCT_STAT_NSTATS; i++) |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 158 | cpuusage->usages[i] = val; |
| 159 | |
| 160 | #ifndef CONFIG_64BIT |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 161 | raw_spin_unlock_irq(&cpu_rq(cpu)->lock); |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 162 | #endif |
| 163 | } |
| 164 | |
| 165 | /* return total cpu usage (in nanoseconds) of a group */ |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 166 | static u64 __cpuusage_read(struct cgroup_subsys_state *css, |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 167 | enum cpuacct_stat_index index) |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 168 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 169 | struct cpuacct *ca = css_ca(css); |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 170 | u64 totalcpuusage = 0; |
| 171 | int i; |
| 172 | |
Zhao Lei | 5ca3726 | 2016-03-22 16:37:07 +0800 | [diff] [blame] | 173 | for_each_possible_cpu(i) |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 174 | totalcpuusage += cpuacct_cpuusage_read(ca, i, index); |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 175 | |
| 176 | return totalcpuusage; |
| 177 | } |
| 178 | |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 179 | static u64 cpuusage_user_read(struct cgroup_subsys_state *css, |
| 180 | struct cftype *cft) |
| 181 | { |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 182 | return __cpuusage_read(css, CPUACCT_STAT_USER); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static u64 cpuusage_sys_read(struct cgroup_subsys_state *css, |
| 186 | struct cftype *cft) |
| 187 | { |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 188 | return __cpuusage_read(css, CPUACCT_STAT_SYSTEM); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static u64 cpuusage_read(struct cgroup_subsys_state *css, struct cftype *cft) |
| 192 | { |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 193 | return __cpuusage_read(css, CPUACCT_STAT_NSTATS); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 194 | } |
| 195 | |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 196 | static int cpuusage_write(struct cgroup_subsys_state *css, struct cftype *cft, |
Dongsheng Yang | 1a736b7 | 2015-12-21 19:14:42 +0800 | [diff] [blame] | 197 | u64 val) |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 198 | { |
Tejun Heo | 182446d | 2013-08-08 20:11:24 -0400 | [diff] [blame] | 199 | struct cpuacct *ca = css_ca(css); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 200 | int cpu; |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 201 | |
Dongsheng Yang | 1a736b7 | 2015-12-21 19:14:42 +0800 | [diff] [blame] | 202 | /* |
| 203 | * Only allow '0' here to do a reset. |
| 204 | */ |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 205 | if (val) |
| 206 | return -EINVAL; |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 207 | |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 208 | for_each_possible_cpu(cpu) |
| 209 | cpuacct_cpuusage_write(ca, cpu, 0); |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 210 | |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 211 | return 0; |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 214 | static int __cpuacct_percpu_seq_show(struct seq_file *m, |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 215 | enum cpuacct_stat_index index) |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 216 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 217 | struct cpuacct *ca = css_ca(seq_css(m)); |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 218 | u64 percpu; |
| 219 | int i; |
| 220 | |
Zhao Lei | 5ca3726 | 2016-03-22 16:37:07 +0800 | [diff] [blame] | 221 | for_each_possible_cpu(i) { |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 222 | percpu = cpuacct_cpuusage_read(ca, i, index); |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 223 | seq_printf(m, "%llu ", (unsigned long long) percpu); |
| 224 | } |
| 225 | seq_printf(m, "\n"); |
| 226 | return 0; |
| 227 | } |
| 228 | |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 229 | static int cpuacct_percpu_user_seq_show(struct seq_file *m, void *V) |
| 230 | { |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 231 | return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_USER); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static int cpuacct_percpu_sys_seq_show(struct seq_file *m, void *V) |
| 235 | { |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 236 | return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_SYSTEM); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static int cpuacct_percpu_seq_show(struct seq_file *m, void *V) |
| 240 | { |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 241 | return __cpuacct_percpu_seq_show(m, CPUACCT_STAT_NSTATS); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Zhao Lei | 277a13e | 2016-06-20 17:37:20 +0800 | [diff] [blame] | 244 | static int cpuacct_all_seq_show(struct seq_file *m, void *V) |
| 245 | { |
| 246 | struct cpuacct *ca = css_ca(seq_css(m)); |
| 247 | int index; |
| 248 | int cpu; |
| 249 | |
| 250 | seq_puts(m, "cpu"); |
| 251 | for (index = 0; index < CPUACCT_STAT_NSTATS; index++) |
| 252 | seq_printf(m, " %s", cpuacct_stat_desc[index]); |
| 253 | seq_puts(m, "\n"); |
| 254 | |
| 255 | for_each_possible_cpu(cpu) { |
| 256 | struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu); |
| 257 | |
| 258 | seq_printf(m, "%d", cpu); |
| 259 | |
| 260 | for (index = 0; index < CPUACCT_STAT_NSTATS; index++) { |
| 261 | #ifndef CONFIG_64BIT |
| 262 | /* |
| 263 | * Take rq->lock to make 64-bit read safe on 32-bit |
| 264 | * platforms. |
| 265 | */ |
| 266 | raw_spin_lock_irq(&cpu_rq(cpu)->lock); |
| 267 | #endif |
| 268 | |
| 269 | seq_printf(m, " %llu", cpuusage->usages[index]); |
| 270 | |
| 271 | #ifndef CONFIG_64BIT |
| 272 | raw_spin_unlock_irq(&cpu_rq(cpu)->lock); |
| 273 | #endif |
| 274 | } |
| 275 | seq_puts(m, "\n"); |
| 276 | } |
| 277 | return 0; |
| 278 | } |
| 279 | |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 280 | static int cpuacct_stats_show(struct seq_file *sf, void *v) |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 281 | { |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 282 | struct cpuacct *ca = css_ca(seq_css(sf)); |
Zhao Lei | 8e546bf | 2016-06-20 17:37:19 +0800 | [diff] [blame] | 283 | s64 val[CPUACCT_STAT_NSTATS]; |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 284 | int cpu; |
Zhao Lei | 8e546bf | 2016-06-20 17:37:19 +0800 | [diff] [blame] | 285 | int stat; |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 286 | |
Zhao Lei | 8e546bf | 2016-06-20 17:37:19 +0800 | [diff] [blame] | 287 | memset(val, 0, sizeof(val)); |
Zhao Lei | 5ca3726 | 2016-03-22 16:37:07 +0800 | [diff] [blame] | 288 | for_each_possible_cpu(cpu) { |
Zhao Lei | 8e546bf | 2016-06-20 17:37:19 +0800 | [diff] [blame] | 289 | u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat; |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 290 | |
Zhao Lei | 8e546bf | 2016-06-20 17:37:19 +0800 | [diff] [blame] | 291 | val[CPUACCT_STAT_USER] += cpustat[CPUTIME_USER]; |
| 292 | val[CPUACCT_STAT_USER] += cpustat[CPUTIME_NICE]; |
| 293 | val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_SYSTEM]; |
| 294 | val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_IRQ]; |
| 295 | val[CPUACCT_STAT_SYSTEM] += cpustat[CPUTIME_SOFTIRQ]; |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 296 | } |
| 297 | |
Zhao Lei | 8e546bf | 2016-06-20 17:37:19 +0800 | [diff] [blame] | 298 | for (stat = 0; stat < CPUACCT_STAT_NSTATS; stat++) { |
| 299 | seq_printf(sf, "%s %lld\n", |
| 300 | cpuacct_stat_desc[stat], |
Frederic Weisbecker | 7fb1327 | 2017-01-31 04:09:19 +0100 | [diff] [blame] | 301 | (long long)nsec_to_clock_t(val[stat])); |
Zhao Lei | 8e546bf | 2016-06-20 17:37:19 +0800 | [diff] [blame] | 302 | } |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static struct cftype files[] = { |
| 308 | { |
| 309 | .name = "usage", |
| 310 | .read_u64 = cpuusage_read, |
| 311 | .write_u64 = cpuusage_write, |
| 312 | }, |
| 313 | { |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 314 | .name = "usage_user", |
| 315 | .read_u64 = cpuusage_user_read, |
| 316 | }, |
| 317 | { |
| 318 | .name = "usage_sys", |
| 319 | .read_u64 = cpuusage_sys_read, |
| 320 | }, |
| 321 | { |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 322 | .name = "usage_percpu", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 323 | .seq_show = cpuacct_percpu_seq_show, |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 324 | }, |
| 325 | { |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 326 | .name = "usage_percpu_user", |
| 327 | .seq_show = cpuacct_percpu_user_seq_show, |
| 328 | }, |
| 329 | { |
| 330 | .name = "usage_percpu_sys", |
| 331 | .seq_show = cpuacct_percpu_sys_seq_show, |
| 332 | }, |
| 333 | { |
Zhao Lei | 277a13e | 2016-06-20 17:37:20 +0800 | [diff] [blame] | 334 | .name = "usage_all", |
| 335 | .seq_show = cpuacct_all_seq_show, |
| 336 | }, |
| 337 | { |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 338 | .name = "stat", |
Tejun Heo | 2da8ca8 | 2013-12-05 12:28:04 -0500 | [diff] [blame] | 339 | .seq_show = cpuacct_stats_show, |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 340 | }, |
| 341 | { } /* terminate */ |
| 342 | }; |
| 343 | |
| 344 | /* |
| 345 | * charge this task's execution time to its accounting group. |
| 346 | * |
| 347 | * called with rq->lock held. |
| 348 | */ |
| 349 | void cpuacct_charge(struct task_struct *tsk, u64 cputime) |
| 350 | { |
| 351 | struct cpuacct *ca; |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 352 | int index = CPUACCT_STAT_SYSTEM; |
Anton Blanchard | bd92883 | 2016-04-06 21:59:50 +1000 | [diff] [blame] | 353 | struct pt_regs *regs = task_pt_regs(tsk); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 354 | |
Anton Blanchard | bd92883 | 2016-04-06 21:59:50 +1000 | [diff] [blame] | 355 | if (regs && user_mode(regs)) |
Zhao Lei | 9acacc2 | 2016-06-20 17:37:18 +0800 | [diff] [blame] | 356 | index = CPUACCT_STAT_USER; |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 357 | |
| 358 | rcu_read_lock(); |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 359 | |
Zhao Lei | 73e6aaf | 2016-03-17 12:19:43 +0800 | [diff] [blame] | 360 | for (ca = task_ca(tsk); ca; ca = parent_ca(ca)) |
Dongsheng Yang | d740037 | 2016-03-22 16:37:08 +0800 | [diff] [blame] | 361 | this_cpu_ptr(ca->cpuusage)->usages[index] += cputime; |
| 362 | |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 363 | rcu_read_unlock(); |
| 364 | } |
| 365 | |
Li Zefan | 1966aaf | 2013-03-29 14:37:06 +0800 | [diff] [blame] | 366 | /* |
| 367 | * Add user/system time to cpuacct. |
| 368 | * |
| 369 | * Note: it's the caller that updates the account of the root cgroup. |
| 370 | */ |
Zhao Lei | 73e6aaf | 2016-03-17 12:19:43 +0800 | [diff] [blame] | 371 | void cpuacct_account_field(struct task_struct *tsk, int index, u64 val) |
Li Zefan | 1966aaf | 2013-03-29 14:37:06 +0800 | [diff] [blame] | 372 | { |
Li Zefan | 1966aaf | 2013-03-29 14:37:06 +0800 | [diff] [blame] | 373 | struct cpuacct *ca; |
| 374 | |
Li Zefan | 1966aaf | 2013-03-29 14:37:06 +0800 | [diff] [blame] | 375 | rcu_read_lock(); |
Zhao Lei | 73e6aaf | 2016-03-17 12:19:43 +0800 | [diff] [blame] | 376 | for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca)) |
| 377 | this_cpu_ptr(ca->cpustat)->cpustat[index] += val; |
Li Zefan | 1966aaf | 2013-03-29 14:37:06 +0800 | [diff] [blame] | 378 | rcu_read_unlock(); |
| 379 | } |
| 380 | |
Tejun Heo | 073219e | 2014-02-08 10:36:58 -0500 | [diff] [blame] | 381 | struct cgroup_subsys cpuacct_cgrp_subsys = { |
Li Zefan | 621e2de | 2013-03-29 14:44:15 +0800 | [diff] [blame] | 382 | .css_alloc = cpuacct_css_alloc, |
| 383 | .css_free = cpuacct_css_free, |
Tejun Heo | 5577964 | 2014-07-15 11:05:09 -0400 | [diff] [blame] | 384 | .legacy_cftypes = files, |
Tejun Heo | b38e42e | 2016-02-23 10:00:50 -0500 | [diff] [blame] | 385 | .early_init = true, |
Li Zefan | 2e76c24 | 2013-03-29 14:36:31 +0800 | [diff] [blame] | 386 | }; |