Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 07ada41

Browse files
huangruixanmod
authored andcommitted
cpupower: move print_speed function into misc helper
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>
1 parent 40691db commit 07ada41

File tree

3 files changed

+54
-48
lines changed

3 files changed

+54
-48
lines changed

tools/power/cpupower/utils/cpufreq-info.c

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -84,43 +84,6 @@ static void proc_cpufreq_output(void)
8484
}
8585

8686
static int no_rounding;
87-
static void print_speed(unsigned long speed)
88-
{
89-
unsigned long tmp;
90-
91-
if (no_rounding) {
92-
if (speed > 1000000)
93-
printf("%u.%06u GHz", ((unsigned int) speed/1000000),
94-
((unsigned int) speed%1000000));
95-
else if (speed > 1000)
96-
printf("%u.%03u MHz", ((unsigned int) speed/1000),
97-
(unsigned int) (speed%1000));
98-
else
99-
printf("%lu kHz", speed);
100-
} else {
101-
if (speed > 1000000) {
102-
tmp = speed%10000;
103-
if (tmp >= 5000)
104-
speed += 10000;
105-
printf("%u.%02u GHz", ((unsigned int) speed/1000000),
106-
((unsigned int) (speed%1000000)/10000));
107-
} else if (speed > 100000) {
108-
tmp = speed%1000;
109-
if (tmp >= 500)
110-
speed += 1000;
111-
printf("%u MHz", ((unsigned int) speed/1000));
112-
} else if (speed > 1000) {
113-
tmp = speed%100;
114-
if (tmp >= 50)
115-
speed += 100;
116-
printf("%u.%01u MHz", ((unsigned int) speed/1000),
117-
((unsigned int) (speed%1000)/100));
118-
}
119-
}
120-
121-
return;
122-
}
123-
12487
static void print_duration(unsigned long duration)
12588
{
12689
unsigned long tmp;
@@ -254,11 +217,11 @@ static int get_boost_mode(unsigned int cpu)
254217
if (freqs) {
255218
printf(_(" boost frequency steps: "));
256219
while (freqs->next) {
257-
print_speed(freqs->frequency);
220+
print_speed(freqs->frequency, no_rounding);
258221
printf(", ");
259222
freqs = freqs->next;
260223
}
261-
print_speed(freqs->frequency);
224+
print_speed(freqs->frequency, no_rounding);
262225
printf("\n");
263226
cpufreq_put_available_frequencies(freqs);
264227
}
@@ -277,7 +240,7 @@ static int get_freq_kernel(unsigned int cpu, unsigned int human)
277240
return -EINVAL;
278241
}
279242
if (human) {
280-
print_speed(freq);
243+
print_speed(freq, no_rounding);
281244
} else
282245
printf("%lu", freq);
283246
printf(_(" (asserted by call to kernel)\n"));
@@ -296,7 +259,7 @@ static int get_freq_hardware(unsigned int cpu, unsigned int human)
296259
return -EINVAL;
297260
}
298261
if (human) {
299-
print_speed(freq);
262+
print_speed(freq, no_rounding);
300263
} else
301264
printf("%lu", freq);
302265
printf(_(" (asserted by call to hardware)\n"));
@@ -316,9 +279,9 @@ static int get_hardware_limits(unsigned int cpu, unsigned int human)
316279

317280
if (human) {
318281
printf(_(" hardware limits: "));
319-
print_speed(min);
282+
print_speed(min, no_rounding);
320283
printf(" - ");
321-
print_speed(max);
284+
print_speed(max, no_rounding);
322285
printf("\n");
323286
} else {
324287
printf("%lu %lu\n", min, max);
@@ -350,9 +313,9 @@ static int get_policy(unsigned int cpu)
350313
return -EINVAL;
351314
}
352315
printf(_(" current policy: frequency should be within "));
353-
print_speed(policy->min);
316+
print_speed(policy->min, no_rounding);
354317
printf(_(" and "));
355-
print_speed(policy->max);
318+
print_speed(policy->max, no_rounding);
356319

357320
printf(".\n ");
358321
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)
436399
struct cpufreq_stats *stats = cpufreq_get_stats(cpu, &total_time);
437400
while (stats) {
438401
if (human) {
439-
print_speed(stats->frequency);
402+
print_speed(stats->frequency, no_rounding);
440403
printf(":%.2f%%",
441404
(100.0 * stats->time_in_state) / total_time);
442405
} else
@@ -486,11 +449,11 @@ static void debug_output_one(unsigned int cpu)
486449
if (freqs) {
487450
printf(_(" available frequency steps: "));
488451
while (freqs->next) {
489-
print_speed(freqs->frequency);
452+
print_speed(freqs->frequency, no_rounding);
490453
printf(", ");
491454
freqs = freqs->next;
492455
}
493-
print_speed(freqs->frequency);
456+
print_speed(freqs->frequency, no_rounding);
494457
printf("\n");
495458
cpufreq_put_available_frequencies(freqs);
496459
}

tools/power/cpupower/utils/helpers/helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,6 @@ extern struct bitmask *offline_cpus;
200200
void get_cpustate(void);
201201
void print_online_cpus(void);
202202
void print_offline_cpus(void);
203+
void print_speed(unsigned long speed, int no_rounding);
203204

204205
#endif /* __CPUPOWERUTILS_HELPERS__ */

tools/power/cpupower/utils/helpers/misc.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,45 @@ void print_offline_cpus(void)
164164
printf(_("cpupower set operation was not performed on them\n"));
165165
}
166166
}
167+
168+
/*
169+
* print_speed
170+
*
171+
* Print the exact CPU frequency with appropriate unit
172+
*/
173+
void print_speed(unsigned long speed, int no_rounding)
174+
{
175+
unsigned long tmp;
176+
177+
if (no_rounding) {
178+
if (speed > 1000000)
179+
printf("%u.%06u GHz", ((unsigned int) speed/1000000),
180+
((unsigned int) speed%1000000));
181+
else if (speed > 1000)
182+
printf("%u.%03u MHz", ((unsigned int) speed/1000),
183+
(unsigned int) (speed%1000));
184+
else
185+
printf("%lu kHz", speed);
186+
} else {
187+
if (speed > 1000000) {
188+
tmp = speed%10000;
189+
if (tmp >= 5000)
190+
speed += 10000;
191+
printf("%u.%02u GHz", ((unsigned int) speed/1000000),
192+
((unsigned int) (speed%1000000)/10000));
193+
} else if (speed > 100000) {
194+
tmp = speed%1000;
195+
if (tmp >= 500)
196+
speed += 1000;
197+
printf("%u MHz", ((unsigned int) speed/1000));
198+
} else if (speed > 1000) {
199+
tmp = speed%100;
200+
if (tmp >= 50)
201+
speed += 100;
202+
printf("%u.%01u MHz", ((unsigned int) speed/1000),
203+
((unsigned int) (speed%1000)/100));
204+
}
205+
}
206+
207+
return;
208+
}

0 commit comments

Comments
 (0)