hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/kernel/sched/cpuacct.c
....@@ -5,6 +5,7 @@
55 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
66 * (balbir@in.ibm.com).
77 */
8
+#include <asm/irq_regs.h>
89 #include "sched.h"
910
1011 /* Time spent by the tasks of the CPU accounting group executing in ... */
....@@ -20,15 +21,11 @@
2021 [CPUACCT_STAT_SYSTEM] = "system",
2122 };
2223
23
-struct cpuacct_usage {
24
- u64 usages[CPUACCT_STAT_NSTATS];
25
-};
26
-
2724 /* track CPU usage of a group of tasks and its child groups */
2825 struct cpuacct {
2926 struct cgroup_subsys_state css;
3027 /* cpuusage holds pointer to a u64-type object on every CPU */
31
- struct cpuacct_usage __percpu *cpuusage;
28
+ u64 __percpu *cpuusage;
3229 struct kernel_cpustat __percpu *cpustat;
3330 };
3431
....@@ -48,7 +45,7 @@
4845 return css_ca(ca->css.parent);
4946 }
5047
51
-static DEFINE_PER_CPU(struct cpuacct_usage, root_cpuacct_cpuusage);
48
+static DEFINE_PER_CPU(u64, root_cpuacct_cpuusage);
5249 static struct cpuacct root_cpuacct = {
5350 .cpustat = &kernel_cpustat,
5451 .cpuusage = &root_cpuacct_cpuusage,
....@@ -67,7 +64,7 @@
6764 if (!ca)
6865 goto out;
6966
70
- ca->cpuusage = alloc_percpu(struct cpuacct_usage);
67
+ ca->cpuusage = alloc_percpu(u64);
7168 if (!ca->cpuusage)
7269 goto out_free_ca;
7370
....@@ -98,7 +95,8 @@
9895 static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu,
9996 enum cpuacct_stat_index index)
10097 {
101
- struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
98
+ u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
99
+ u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
102100 u64 data;
103101
104102 /*
....@@ -114,14 +112,17 @@
114112 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
115113 #endif
116114
117
- if (index == CPUACCT_STAT_NSTATS) {
118
- int i = 0;
119
-
120
- data = 0;
121
- for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
122
- data += cpuusage->usages[i];
123
- } else {
124
- data = cpuusage->usages[index];
115
+ switch (index) {
116
+ case CPUACCT_STAT_USER:
117
+ data = cpustat[CPUTIME_USER] + cpustat[CPUTIME_NICE];
118
+ break;
119
+ case CPUACCT_STAT_SYSTEM:
120
+ data = cpustat[CPUTIME_SYSTEM] + cpustat[CPUTIME_IRQ] +
121
+ cpustat[CPUTIME_SOFTIRQ];
122
+ break;
123
+ case CPUACCT_STAT_NSTATS:
124
+ data = *cpuusage;
125
+ break;
125126 }
126127
127128 #ifndef CONFIG_64BIT
....@@ -131,10 +132,14 @@
131132 return data;
132133 }
133134
134
-static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
135
+static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu)
135136 {
136
- struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
137
- int i;
137
+ u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
138
+ u64 *cpustat = per_cpu_ptr(ca->cpustat, cpu)->cpustat;
139
+
140
+ /* Don't allow to reset global kernel_cpustat */
141
+ if (ca == &root_cpuacct)
142
+ return;
138143
139144 #ifndef CONFIG_64BIT
140145 /*
....@@ -142,9 +147,10 @@
142147 */
143148 raw_spin_lock_irq(&cpu_rq(cpu)->lock);
144149 #endif
145
-
146
- for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
147
- cpuusage->usages[i] = val;
150
+ *cpuusage = 0;
151
+ cpustat[CPUTIME_USER] = cpustat[CPUTIME_NICE] = 0;
152
+ cpustat[CPUTIME_SYSTEM] = cpustat[CPUTIME_IRQ] = 0;
153
+ cpustat[CPUTIME_SOFTIRQ] = 0;
148154
149155 #ifndef CONFIG_64BIT
150156 raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
....@@ -195,7 +201,7 @@
195201 return -EINVAL;
196202
197203 for_each_possible_cpu(cpu)
198
- cpuacct_cpuusage_write(ca, cpu, 0);
204
+ cpuacct_cpuusage_write(ca, cpu);
199205
200206 return 0;
201207 }
....@@ -242,25 +248,10 @@
242248 seq_puts(m, "\n");
243249
244250 for_each_possible_cpu(cpu) {
245
- struct cpuacct_usage *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
246
-
247251 seq_printf(m, "%d", cpu);
248
-
249
- for (index = 0; index < CPUACCT_STAT_NSTATS; index++) {
250
-#ifndef CONFIG_64BIT
251
- /*
252
- * Take rq->lock to make 64-bit read safe on 32-bit
253
- * platforms.
254
- */
255
- raw_spin_lock_irq(&cpu_rq(cpu)->lock);
256
-#endif
257
-
258
- seq_printf(m, " %llu", cpuusage->usages[index]);
259
-
260
-#ifndef CONFIG_64BIT
261
- raw_spin_unlock_irq(&cpu_rq(cpu)->lock);
262
-#endif
263
- }
252
+ for (index = 0; index < CPUACCT_STAT_NSTATS; index++)
253
+ seq_printf(m, " %llu",
254
+ cpuacct_cpuusage_read(ca, cpu, index));
264255 seq_puts(m, "\n");
265256 }
266257 return 0;
....@@ -337,19 +328,13 @@
337328 */
338329 void cpuacct_charge(struct task_struct *tsk, u64 cputime)
339330 {
331
+ unsigned int cpu = task_cpu(tsk);
340332 struct cpuacct *ca;
341
- int index = CPUACCT_STAT_SYSTEM;
342
- struct pt_regs *regs = task_pt_regs(tsk);
343333
344
- if (regs && user_mode(regs))
345
- index = CPUACCT_STAT_USER;
346
-
347
- rcu_read_lock();
334
+ lockdep_assert_held(&cpu_rq(cpu)->lock);
348335
349336 for (ca = task_ca(tsk); ca; ca = parent_ca(ca))
350
- this_cpu_ptr(ca->cpuusage)->usages[index] += cputime;
351
-
352
- rcu_read_unlock();
337
+ *per_cpu_ptr(ca->cpuusage, cpu) += cputime;
353338 }
354339
355340 /*
....@@ -363,7 +348,7 @@
363348
364349 rcu_read_lock();
365350 for (ca = task_ca(tsk); ca != &root_cpuacct; ca = parent_ca(ca))
366
- this_cpu_ptr(ca->cpustat)->cpustat[index] += val;
351
+ __this_cpu_add(ca->cpustat->cpustat[index], val);
367352 rcu_read_unlock();
368353 }
369354