Skip to content

Commit

Permalink
cpupower: Move print_speed function into misc helper
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Huang Rui <ray.huang@amd.com>
  • Loading branch information
huangrui authored and xanmod committed Feb 23, 2022
1 parent bb1a3d5 commit 06778cd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 48 deletions.
59 changes: 11 additions & 48 deletions tools/power/cpupower/utils/cpufreq-info.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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"));
Expand All @@ -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"));
Expand All @@ -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);
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions tools/power/cpupower/utils/helpers/helpers.h
Expand Up @@ -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__ */
40 changes: 40 additions & 0 deletions tools/power/cpupower/utils/helpers/misc.c
Expand Up @@ -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));
}
}
}

0 comments on commit 06778cd

Please sign in to comment.