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