WIP FPC-III support
[linux/fpc-iii.git] / tools / power / cpupower / bench / system.c
blob40f3679e70b54f3558a9024bcad5aec6bdb68df4
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* cpufreq-bench CPUFreq microbenchmark
4 * Copyright (C) 2008 Christian Kornacker <ckornacker@suse.de>
5 */
7 #include <stdio.h>
8 #include <time.h>
9 #include <sys/time.h>
10 #include <sys/types.h>
11 #include <unistd.h>
13 #include <sched.h>
15 #include <cpufreq.h>
16 #include <cpupower.h>
18 #include "config.h"
19 #include "system.h"
21 /**
22 * returns time since epoch in µs
24 * @retval time
25 **/
27 long long int get_time()
29 struct timeval now;
31 gettimeofday(&now, NULL);
33 return (long long int)(now.tv_sec * 1000000LL + now.tv_usec);
36 /**
37 * sets the cpufreq governor
39 * @param governor cpufreq governor name
40 * @param cpu cpu for which the governor should be set
42 * @retval 0 on success
43 * @retval -1 when failed
44 **/
46 int set_cpufreq_governor(char *governor, unsigned int cpu)
49 dprintf("set %s as cpufreq governor\n", governor);
51 if (cpupower_is_cpu_online(cpu) != 1) {
52 perror("cpufreq_cpu_exists");
53 fprintf(stderr, "error: cpu %u does not exist\n", cpu);
54 return -1;
57 if (cpufreq_modify_policy_governor(cpu, governor) != 0) {
58 perror("cpufreq_modify_policy_governor");
59 fprintf(stderr, "error: unable to set %s governor\n", governor);
60 return -1;
63 return 0;
66 /**
67 * sets cpu affinity for the process
69 * @param cpu cpu# to which the affinity should be set
71 * @retval 0 on success
72 * @retval -1 when setting the affinity failed
73 **/
75 int set_cpu_affinity(unsigned int cpu)
77 cpu_set_t cpuset;
79 CPU_ZERO(&cpuset);
80 CPU_SET(cpu, &cpuset);
82 dprintf("set affinity to cpu #%u\n", cpu);
84 if (sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpuset) < 0) {
85 perror("sched_setaffinity");
86 fprintf(stderr, "warning: unable to set cpu affinity\n");
87 return -1;
90 return 0;
93 /**
94 * sets the process priority parameter
96 * @param priority priority value
98 * @retval 0 on success
99 * @retval -1 when setting the priority failed
102 int set_process_priority(int priority)
104 struct sched_param param;
106 dprintf("set scheduler priority to %i\n", priority);
108 param.sched_priority = priority;
110 if (sched_setscheduler(0, SCHEDULER, &param) < 0) {
111 perror("sched_setscheduler");
112 fprintf(stderr, "warning: unable to set scheduler priority\n");
113 return -1;
116 return 0;
120 * notifies the user that the benchmark may run some time
122 * @param config benchmark config values
126 void prepare_user(const struct config *config)
128 unsigned long sleep_time = 0;
129 unsigned long load_time = 0;
130 unsigned int round;
132 for (round = 0; round < config->rounds; round++) {
133 sleep_time += 2 * config->cycles *
134 (config->sleep + config->sleep_step * round);
135 load_time += 2 * config->cycles *
136 (config->load + config->load_step * round) +
137 (config->load + config->load_step * round * 4);
140 if (config->verbose || config->output != stdout)
141 printf("approx. test duration: %im\n",
142 (int)((sleep_time + load_time) / 60000000));
146 * sets up the cpu affinity and scheduler priority
148 * @param config benchmark config values
152 void prepare_system(const struct config *config)
154 if (config->verbose)
155 printf("set cpu affinity to cpu #%u\n", config->cpu);
157 set_cpu_affinity(config->cpu);
159 switch (config->prio) {
160 case SCHED_HIGH:
161 if (config->verbose)
162 printf("high priority condition requested\n");
164 set_process_priority(PRIORITY_HIGH);
165 break;
166 case SCHED_LOW:
167 if (config->verbose)
168 printf("low priority condition requested\n");
170 set_process_priority(PRIORITY_LOW);
171 break;
172 default:
173 if (config->verbose)
174 printf("default priority condition requested\n");
176 set_process_priority(PRIORITY_DEFAULT);