OSDN Git Service

cpupower: Move print_speed function into misc helper
authorHuang Rui <ray.huang@amd.com>
Tue, 22 Feb 2022 15:34:24 +0000 (23:34 +0800)
committerShuah Khan <skhan@linuxfoundation.org>
Wed, 23 Feb 2022 01:37:07 +0000 (18:37 -0700)
The print_speed can be as a common function, and expose it into misc
helper header. Then it can be used on other helper files as well.

Reviewed-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: Huang Rui <ray.huang@amd.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/power/cpupower/utils/cpufreq-info.c
tools/power/cpupower/utils/helpers/helpers.h
tools/power/cpupower/utils/helpers/misc.c

index f9895e3..b429454 100644 (file)
@@ -84,43 +84,6 @@ static void proc_cpufreq_output(void)
 }
 
 static int no_rounding;
-static void print_speed(unsigned long speed)
-{
-       unsigned long tmp;
-
-       if (no_rounding) {
-               if (speed > 1000000)
-                       printf("%u.%06u GHz", ((unsigned int) speed/1000000),
-                               ((unsigned int) speed%1000000));
-               else if (speed > 1000)
-                       printf("%u.%03u MHz", ((unsigned int) speed/1000),
-                               (unsigned int) (speed%1000));
-               else
-                       printf("%lu kHz", speed);
-       } else {
-               if (speed > 1000000) {
-                       tmp = speed%10000;
-                       if (tmp >= 5000)
-                               speed += 10000;
-                       printf("%u.%02u GHz", ((unsigned int) speed/1000000),
-                               ((unsigned int) (speed%1000000)/10000));
-               } else if (speed > 100000) {
-                       tmp = speed%1000;
-                       if (tmp >= 500)
-                               speed += 1000;
-                       printf("%u MHz", ((unsigned int) speed/1000));
-               } else if (speed > 1000) {
-                       tmp = speed%100;
-                       if (tmp >= 50)
-                               speed += 100;
-                       printf("%u.%01u MHz", ((unsigned int) speed/1000),
-                               ((unsigned int) (speed%1000)/100));
-               }
-       }
-
-       return;
-}
-
 static void print_duration(unsigned long duration)
 {
        unsigned long tmp;
@@ -254,11 +217,11 @@ static int get_boost_mode(unsigned int cpu)
        if (freqs) {
                printf(_("  boost frequency steps: "));
                while (freqs->next) {
-                       print_speed(freqs->frequency);
+                       print_speed(freqs->frequency, no_rounding);
                        printf(", ");
                        freqs = freqs->next;
                }
-               print_speed(freqs->frequency);
+               print_speed(freqs->frequency, no_rounding);
                printf("\n");
                cpufreq_put_available_frequencies(freqs);
        }
@@ -277,7 +240,7 @@ static int get_freq_kernel(unsigned int cpu, unsigned int human)
                return -EINVAL;
        }
        if (human) {
-               print_speed(freq);
+               print_speed(freq, no_rounding);
        } else
                printf("%lu", freq);
        printf(_(" (asserted by call to kernel)\n"));
@@ -296,7 +259,7 @@ static int get_freq_hardware(unsigned int cpu, unsigned int human)
                return -EINVAL;
        }
        if (human) {
-               print_speed(freq);
+               print_speed(freq, no_rounding);
        } else
                printf("%lu", freq);
        printf(_(" (asserted by call to hardware)\n"));
@@ -316,9 +279,9 @@ static int get_hardware_limits(unsigned int cpu, unsigned int human)
 
        if (human) {
                printf(_("  hardware limits: "));
-               print_speed(min);
+               print_speed(min, no_rounding);
                printf(" - ");
-               print_speed(max);
+               print_speed(max, no_rounding);
                printf("\n");
        } else {
                printf("%lu %lu\n", min, max);
@@ -350,9 +313,9 @@ static int get_policy(unsigned int cpu)
                return -EINVAL;
        }
        printf(_("  current policy: frequency should be within "));
-       print_speed(policy->min);
+       print_speed(policy->min, no_rounding);
        printf(_(" and "));
-       print_speed(policy->max);
+       print_speed(policy->max, no_rounding);
 
        printf(".\n                  ");
        printf(_("The governor \"%s\" may decide which speed to use\n"
@@ -436,7 +399,7 @@ static int get_freq_stats(unsigned int cpu, unsigned int human)
        struct cpufreq_stats *stats = cpufreq_get_stats(cpu, &total_time);
        while (stats) {
                if (human) {
-                       print_speed(stats->frequency);
+                       print_speed(stats->frequency, no_rounding);
                        printf(":%.2f%%",
                                (100.0 * stats->time_in_state) / total_time);
                } else
@@ -486,11 +449,11 @@ static void debug_output_one(unsigned int cpu)
        if (freqs) {
                printf(_("  available frequency steps:  "));
                while (freqs->next) {
-                       print_speed(freqs->frequency);
+                       print_speed(freqs->frequency, no_rounding);
                        printf(", ");
                        freqs = freqs->next;
                }
-               print_speed(freqs->frequency);
+               print_speed(freqs->frequency, no_rounding);
                printf("\n");
                cpufreq_put_available_frequencies(freqs);
        }
index 326491e..fa2a8c1 100644 (file)
@@ -200,5 +200,6 @@ extern struct bitmask *offline_cpus;
 void get_cpustate(void);
 void print_online_cpus(void);
 void print_offline_cpus(void);
+void print_speed(unsigned long speed, int no_rounding);
 
 #endif /* __CPUPOWERUTILS_HELPERS__ */
index e0d3145..9547b29 100644 (file)
@@ -164,3 +164,43 @@ void print_offline_cpus(void)
                printf(_("cpupower set operation was not performed on them\n"));
        }
 }
+
+/*
+ * print_speed
+ *
+ * Print the exact CPU frequency with appropriate unit
+ */
+void print_speed(unsigned long speed, int no_rounding)
+{
+       unsigned long tmp;
+
+       if (no_rounding) {
+               if (speed > 1000000)
+                       printf("%u.%06u GHz", ((unsigned int)speed / 1000000),
+                              ((unsigned int)speed % 1000000));
+               else if (speed > 1000)
+                       printf("%u.%03u MHz", ((unsigned int)speed / 1000),
+                              (unsigned int)(speed % 1000));
+               else
+                       printf("%lu kHz", speed);
+       } else {
+               if (speed > 1000000) {
+                       tmp = speed % 10000;
+                       if (tmp >= 5000)
+                               speed += 10000;
+                       printf("%u.%02u GHz", ((unsigned int)speed / 1000000),
+                              ((unsigned int)(speed % 1000000) / 10000));
+               } else if (speed > 100000) {
+                       tmp = speed % 1000;
+                       if (tmp >= 500)
+                               speed += 1000;
+                       printf("%u MHz", ((unsigned int)speed / 1000));
+               } else if (speed > 1000) {
+                       tmp = speed % 100;
+                       if (tmp >= 50)
+                               speed += 100;
+                       printf("%u.%01u MHz", ((unsigned int)speed / 1000),
+                              ((unsigned int)(speed % 1000) / 100));
+               }
+       }
+}