5 static volatile struct idle_prof_common ipc;
8 * Get time to complete an unit work on a particular cpu.
9 * The minimum number in CALIBRATE_RUNS runs is returned.
11 static double calibrate_unit(unsigned char *data)
13 unsigned long t, i, j, k;
17 for (i = 0; i < CALIBRATE_RUNS; i++) {
19 fio_gettime(&tps, NULL);
20 /* scale for less variance */
21 for (j = 0; j < CALIBRATE_SCALE; j++) {
23 for (k=0; k < page_size; k++) {
24 data[(k + j) % page_size] = k % 256;
26 * we won't see STOP here. this is to match
27 * the same statement in the profiling loop.
29 if (ipc.status == IDLE_PROF_STATUS_PROF_STOP)
34 t = utime_since_now(&tps);
38 /* get the minimum time to complete CALIBRATE_SCALE units */
39 if ((i == 0) || ((double)t < tunit))
43 return tunit / CALIBRATE_SCALE;
46 static void *idle_prof_thread_fn(void *data)
50 struct idle_prof_thread *ipt = data;
52 /* wait for all threads are spawned */
53 pthread_mutex_lock(&ipt->init_lock);
55 /* exit if any other thread failed to start */
56 if (ipc.status == IDLE_PROF_STATUS_ABORT)
59 #if defined(FIO_HAVE_CPU_AFFINITY)
60 os_cpu_mask_t cpu_mask;
61 memset(&cpu_mask, 0, sizeof(cpu_mask));
62 fio_cpu_set(&cpu_mask, ipt->cpu);
64 if ((retval=fio_setaffinity(gettid(), cpu_mask)) == -1)
65 log_err("fio: fio_setaffinity failed\n");
68 log_err("fio: fio_setaffinity not supported\n");
71 ipt->state = TD_EXITED;
72 pthread_mutex_unlock(&ipt->init_lock);
76 ipt->cali_time = calibrate_unit(ipt->data);
78 /* delay to set IDLE class till now for better calibration accuracy */
79 #if defined(CONFIG_SCHED_IDLE)
80 if ((retval = fio_set_sched_idle()))
81 log_err("fio: fio_set_sched_idle failed\n");
84 log_err("fio: fio_set_sched_idle not supported\n");
87 ipt->state = TD_EXITED;
88 pthread_mutex_unlock(&ipt->init_lock);
92 ipt->state = TD_INITIALIZED;
94 /* signal the main thread that calibration is done */
95 pthread_cond_signal(&ipt->cond);
96 pthread_mutex_unlock(&ipt->init_lock);
98 /* wait for other calibration to finish */
99 pthread_mutex_lock(&ipt->start_lock);
101 /* exit if other threads failed to initialize */
102 if (ipc.status == IDLE_PROF_STATUS_ABORT)
105 /* exit if we are doing calibration only */
106 if (ipc.status == IDLE_PROF_STATUS_CALI_STOP)
109 fio_gettime(&ipt->tps, NULL);
110 ipt->state = TD_RUNNING;
114 for (k = 0; k < page_size; k++) {
115 ipt->data[(k + j) % page_size] = k % 256;
116 if (ipc.status == IDLE_PROF_STATUS_PROF_STOP) {
117 fio_gettime(&ipt->tpe, NULL);
126 ipt->loops = j + (double) k / page_size;
127 ipt->state = TD_EXITED;
128 pthread_mutex_unlock(&ipt->start_lock);
133 /* calculate mean and standard deviation to complete an unit of work */
134 static void calibration_stats(void)
137 double sum = 0.0, var = 0.0;
138 struct idle_prof_thread *ipt;
140 for (i = 0; i < ipc.nr_cpus; i++) {
142 sum += ipt->cali_time;
145 ipc.cali_mean = sum/ipc.nr_cpus;
147 for (i = 0; i < ipc.nr_cpus; i++) {
149 var += pow(ipt->cali_time-ipc.cali_mean, 2);
152 ipc.cali_stddev = sqrt(var/(ipc.nr_cpus-1));
155 void fio_idle_prof_init(void)
160 pthread_attr_t tattr;
161 struct idle_prof_thread *ipt;
163 ipc.nr_cpus = cpus_online();
164 ipc.status = IDLE_PROF_STATUS_OK;
166 if (ipc.opt == IDLE_PROF_OPT_NONE)
169 if ((ret = pthread_attr_init(&tattr))) {
170 log_err("fio: pthread_attr_init %s\n", strerror(ret));
173 if ((ret = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM))) {
174 log_err("fio: pthread_attr_setscope %s\n", strerror(ret));
178 ipc.ipts = malloc(ipc.nr_cpus * sizeof(struct idle_prof_thread));
180 log_err("fio: malloc failed\n");
184 ipc.buf = malloc(ipc.nr_cpus * page_size);
186 log_err("fio: malloc failed\n");
192 * profiling aborts on any single thread failure since the
193 * result won't be accurate if any cpu is not used.
195 for (i = 0; i < ipc.nr_cpus; i++) {
199 ipt->state = TD_NOT_CREATED;
200 ipt->data = (unsigned char *)(ipc.buf + page_size * i);
202 if ((ret = pthread_mutex_init(&ipt->init_lock, NULL))) {
203 ipc.status = IDLE_PROF_STATUS_ABORT;
204 log_err("fio: pthread_mutex_init %s\n", strerror(ret));
208 if ((ret = pthread_mutex_init(&ipt->start_lock, NULL))) {
209 ipc.status = IDLE_PROF_STATUS_ABORT;
210 log_err("fio: pthread_mutex_init %s\n", strerror(ret));
214 if ((ret = pthread_cond_init(&ipt->cond, NULL))) {
215 ipc.status = IDLE_PROF_STATUS_ABORT;
216 log_err("fio: pthread_cond_init %s\n", strerror(ret));
220 /* make sure all threads are spawned before they start */
221 pthread_mutex_lock(&ipt->init_lock);
223 /* make sure all threads finish init before profiling starts */
224 pthread_mutex_lock(&ipt->start_lock);
226 if ((ret = pthread_create(&ipt->thread, &tattr, idle_prof_thread_fn, ipt))) {
227 ipc.status = IDLE_PROF_STATUS_ABORT;
228 log_err("fio: pthread_create %s\n", strerror(ret));
231 ipt->state = TD_CREATED;
233 if ((ret = pthread_detach(ipt->thread))) {
234 /* log error and let the thread spin */
235 log_err("fio: pthread_detatch %s\n", strerror(ret));
240 * let good threads continue so that they can exit
241 * if errors on other threads occurred previously.
243 for (i = 0; i < ipc.nr_cpus; i++) {
245 pthread_mutex_unlock(&ipt->init_lock);
248 if (ipc.status == IDLE_PROF_STATUS_ABORT)
251 /* wait for calibration to finish */
252 for (i = 0; i < ipc.nr_cpus; i++) {
254 pthread_mutex_lock(&ipt->init_lock);
255 while ((ipt->state != TD_EXITED) &&
256 (ipt->state!=TD_INITIALIZED)) {
257 fio_gettime(&tp, NULL);
258 ts.tv_sec = tp.tv_sec + 1;
259 ts.tv_nsec = tp.tv_usec * 1000;
260 pthread_cond_timedwait(&ipt->cond, &ipt->init_lock, &ts);
262 pthread_mutex_unlock(&ipt->init_lock);
265 * any thread failed to initialize would abort other threads
266 * later after fio_idle_prof_start.
268 if (ipt->state == TD_EXITED)
269 ipc.status = IDLE_PROF_STATUS_ABORT;
272 if (ipc.status != IDLE_PROF_STATUS_ABORT)
275 ipc.cali_mean = ipc.cali_stddev = 0.0;
277 if (ipc.opt == IDLE_PROF_OPT_CALI)
278 ipc.status = IDLE_PROF_STATUS_CALI_STOP;
281 void fio_idle_prof_start(void)
284 struct idle_prof_thread *ipt;
286 if (ipc.opt == IDLE_PROF_OPT_NONE)
289 /* unlock regardless abort is set or not */
290 for (i = 0; i < ipc.nr_cpus; i++) {
292 pthread_mutex_unlock(&ipt->start_lock);
296 void fio_idle_prof_stop(void)
302 struct idle_prof_thread *ipt;
304 if (ipc.opt == IDLE_PROF_OPT_NONE)
307 if (ipc.opt == IDLE_PROF_OPT_CALI)
310 ipc.status = IDLE_PROF_STATUS_PROF_STOP;
312 /* wait for all threads to exit from profiling */
313 for (i = 0; i < ipc.nr_cpus; i++) {
315 pthread_mutex_lock(&ipt->start_lock);
316 while ((ipt->state != TD_EXITED) &&
317 (ipt->state!=TD_NOT_CREATED)) {
318 fio_gettime(&tp, NULL);
319 ts.tv_sec = tp.tv_sec + 1;
320 ts.tv_nsec = tp.tv_usec * 1000;
321 /* timed wait in case a signal is not received */
322 pthread_cond_timedwait(&ipt->cond, &ipt->start_lock, &ts);
324 pthread_mutex_unlock(&ipt->start_lock);
326 /* calculate idleness */
327 if (ipc.cali_mean != 0.0) {
328 runt = utime_since(&ipt->tps, &ipt->tpe);
329 ipt->idleness = ipt->loops * ipc.cali_mean / runt;
335 * memory allocations are freed via explicit fio_idle_prof_cleanup
336 * after profiling stats are collected by apps.
341 * return system idle percentage when cpu is -1;
342 * return one cpu idle percentage otherwise.
344 static double fio_idle_prof_cpu_stat(int cpu)
346 int i, nr_cpus = ipc.nr_cpus;
347 struct idle_prof_thread *ipt;
350 if (ipc.opt == IDLE_PROF_OPT_NONE)
353 if ((cpu >= nr_cpus) || (cpu < -1)) {
354 log_err("fio: idle profiling invalid cpu index\n");
359 for (i = 0; i < nr_cpus; i++) {
365 ipt = &ipc.ipts[cpu];
372 void fio_idle_prof_cleanup(void)
385 int fio_idle_prof_parse_opt(const char *args)
387 ipc.opt = IDLE_PROF_OPT_NONE; /* default */
390 log_err("fio: empty idle-prof option string\n");
394 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(CONFIG_SCHED_IDLE)
395 if (strcmp("calibrate", args) == 0) {
396 ipc.opt = IDLE_PROF_OPT_CALI;
397 fio_idle_prof_init();
398 fio_idle_prof_start();
399 fio_idle_prof_stop();
400 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL);
402 } else if (strcmp("system", args) == 0) {
403 ipc.opt = IDLE_PROF_OPT_SYSTEM;
405 } else if (strcmp("percpu", args) == 0) {
406 ipc.opt = IDLE_PROF_OPT_PERCPU;
409 log_err("fio: incorrect idle-prof option\n", args);
413 log_err("fio: idle-prof not supported on this platform\n");
418 void show_idle_prof_stats(int output, struct json_object *parent)
420 int i, nr_cpus = ipc.nr_cpus;
421 struct json_object *tmp;
422 char s[MAX_CPU_STR_LEN];
424 if (output == FIO_OUTPUT_NORMAL) {
425 if (ipc.opt > IDLE_PROF_OPT_CALI)
426 log_info("\nCPU idleness:\n");
427 else if (ipc.opt == IDLE_PROF_OPT_CALI)
428 log_info("CPU idleness:\n");
430 if (ipc.opt >= IDLE_PROF_OPT_SYSTEM)
431 log_info(" system: %3.2f%%\n", fio_idle_prof_cpu_stat(-1));
433 if (ipc.opt == IDLE_PROF_OPT_PERCPU) {
434 log_info(" percpu: %3.2f%%", fio_idle_prof_cpu_stat(0));
435 for (i = 1; i < nr_cpus; i++)
436 log_info(", %3.2f%%", fio_idle_prof_cpu_stat(i));
440 if (ipc.opt >= IDLE_PROF_OPT_CALI) {
441 log_info(" unit work: mean=%3.2fus,", ipc.cali_mean);
442 log_info(" stddev=%3.2f\n", ipc.cali_stddev);
445 /* dynamic mem allocations can now be freed */
446 if (ipc.opt != IDLE_PROF_OPT_NONE)
447 fio_idle_prof_cleanup();
452 if ((ipc.opt != IDLE_PROF_OPT_NONE) && (output == FIO_OUTPUT_JSON)) {
456 tmp = json_create_object();
460 json_object_add_value_object(parent, "cpu_idleness", tmp);
461 json_object_add_value_float(tmp, "system", fio_idle_prof_cpu_stat(-1));
463 if (ipc.opt == IDLE_PROF_OPT_PERCPU) {
464 for (i = 0; i < nr_cpus; i++) {
465 snprintf(s, MAX_CPU_STR_LEN, "cpu-%d", i);
466 json_object_add_value_float(tmp, s, fio_idle_prof_cpu_stat(i));
470 json_object_add_value_float(tmp, "unit_mean", ipc.cali_mean);
471 json_object_add_value_float(tmp, "unit_stddev", ipc.cali_stddev);
473 fio_idle_prof_cleanup();