sched: cpufreq: Adds a field cpu_power in the task_struct
authorRuchi Kandoi <kandoiruchi@google.com>
Sat, 10 Dec 2016 19:07:22 +0000 (19:07 +0000)
committerDanny Wood <danwood76@gmail.com>
Sun, 31 Mar 2019 08:48:07 +0000 (09:48 +0100)
cpu_power has been added to keep track of amount of power each task is
consuming. cpu_power is updated whenever stime and utime are updated for
a task. power is computed by taking into account the frequency at which
the current core was running and the current for cpu actively
running at hat frequency.

Change-Id: Ic535941e7b339aab5cae9081a34049daeb44b248
Signed-off-by: Ruchi Kandoi <kandoiruchi@google.com>
Original commit: https://github.com/dianlujitao/CAF_kernel_msm-3.10/commit/85a6bd2bc4c903df43186e6f41209746aa6fdf05

drivers/cpufreq/cpufreq_stats.c
include/linux/cpufreq.h
include/linux/sched.h
kernel/fork.c
kernel/sched/cputime.c

index 3825bbaa76cb89c0ec145da549bf34dca95ceafc..529fae6a6df7fea4c97da49d67531412db8e2fca 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/sort.h>
 #include <linux/err.h>
 #include <linux/of.h>
+#include <linux/sched.h>
 #include <asm/cputime.h>
 #ifdef CONFIG_BL_SWITCHER
 #include <asm/bL_switcher.h>
@@ -137,6 +138,24 @@ static int get_index_all_cpufreq_stat(struct all_cpufreq_stats *all_stat,
        return -1;
 }
 
+void acct_update_power(struct task_struct *task, cputime_t cputime) {
+       struct cpufreq_power_stats *powerstats;
+       struct cpufreq_stats *stats;
+       unsigned int cpu_num, curr;
+
+       if (!task)
+               return;
+       cpu_num = task_cpu(task);
+       powerstats = per_cpu(cpufreq_power_stats, cpu_num);
+       stats = per_cpu(cpufreq_stats_table, cpu_num);
+       if (!powerstats || !stats)
+               return;
+
+       curr = powerstats->curr[stats->last_index];
+       task->cpu_power += curr * cputime_to_usecs(cputime);
+}
+EXPORT_SYMBOL_GPL(acct_update_power);
+
 static ssize_t show_current_in_state(struct kobject *kobj,
                struct kobj_attribute *attr, char *buf)
 {
index fa1417f4f723a1c4328e202f5f9635e1809423c7..5602b8d29d246fcadccd338ef4b7bc54a1c1d35a 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/workqueue.h>
 #include <linux/cpumask.h>
 #include <asm/div64.h>
+#include <asm/cputime.h>
 
 #define CPUFREQ_NAME_LEN 16
 /* Print length for names. Extra 1 space for accomodating '\n' in prints */
@@ -469,4 +470,11 @@ void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
 void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy);
 
 void cpufreq_frequency_table_put_attr(unsigned int cpu);
+
+/*********************************************************************
+ *                         CPUFREQ STATS                             *
+ *********************************************************************/
+
+void acct_update_power(struct task_struct *p, cputime_t cputime);
+
 #endif /* _LINUX_CPUFREQ_H */
index 688f3b32674f6ef15bc59808c203079b2bacdde7..d9db13be065dbd6f56eba7bb2bc41a23e10cf915 100644 (file)
@@ -1214,6 +1214,7 @@ struct task_struct {
 
        cputime_t utime, stime, utimescaled, stimescaled;
        cputime_t gtime;
+       unsigned long long cpu_power;
 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
        struct cputime prev_cputime;
 #endif
index 07938349b2091b69c6da20ef72a1d745ea03affb..1edb21efecc6b2b148b39cafe5611e2e0b90e437 100644 (file)
@@ -1312,6 +1312,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 
        p->utime = p->stime = p->gtime = 0;
        p->utimescaled = p->stimescaled = 0;
+       p->cpu_power = 0;
 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
        p->prev_cputime.utime = p->prev_cputime.stime = 0;
 #endif
index b5ccba22603b51a92c2aa1f0ef647a878dd4e2c3..cc211d01f77f7388e9afaf136601a0d9c7a14297 100644 (file)
@@ -1,3 +1,4 @@
+#include <linux/cpufreq.h>
 #include <linux/export.h>
 #include <linux/sched.h>
 #include <linux/tsacct_kern.h>
@@ -149,6 +150,9 @@ void account_user_time(struct task_struct *p, cputime_t cputime,
 
        /* Account for user time used */
        acct_account_cputime(p);
+
+       /* Account power usage for user time */
+       acct_update_power(p, cputime);
 }
 
 /*
@@ -199,6 +203,9 @@ void __account_system_time(struct task_struct *p, cputime_t cputime,
 
        /* Account for system time used */
        acct_account_cputime(p);
+
+       /* Account power usage for system time */
+       acct_update_power(p, cputime);
 }
 
 /*