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 free_cpu_affinity(struct idle_prof_thread *ipt)
48 #if defined(FIO_HAVE_CPU_AFFINITY)
49 fio_cpuset_exit(&ipt->cpu_mask);
53 static int set_cpu_affinity(struct idle_prof_thread *ipt)
55 #if defined(FIO_HAVE_CPU_AFFINITY)
56 if (fio_cpuset_init(&ipt->cpu_mask)) {
57 log_err("fio: cpuset init failed\n");
61 fio_cpu_set(&ipt->cpu_mask, ipt->cpu);
63 if (fio_setaffinity(gettid(), ipt->cpu_mask)) {
64 log_err("fio: fio_setaffinity failed\n");
65 fio_cpuset_exit(&ipt->cpu_mask);
71 log_err("fio: fio_setaffinity not supported\n");
76 static void *idle_prof_thread_fn(void *data)
80 struct idle_prof_thread *ipt = data;
82 /* wait for all threads are spawned */
83 pthread_mutex_lock(&ipt->init_lock);
85 /* exit if any other thread failed to start */
86 if (ipc.status == IDLE_PROF_STATUS_ABORT) {
87 pthread_mutex_unlock(&ipt->init_lock);
91 retval = set_cpu_affinity(ipt);
93 ipt->state = TD_EXITED;
94 pthread_mutex_unlock(&ipt->init_lock);
98 ipt->cali_time = calibrate_unit(ipt->data);
100 /* delay to set IDLE class till now for better calibration accuracy */
101 #if defined(CONFIG_SCHED_IDLE)
102 if ((retval = fio_set_sched_idle()))
103 log_err("fio: fio_set_sched_idle failed\n");
106 log_err("fio: fio_set_sched_idle not supported\n");
109 ipt->state = TD_EXITED;
110 pthread_mutex_unlock(&ipt->init_lock);
114 ipt->state = TD_INITIALIZED;
116 /* signal the main thread that calibration is done */
117 pthread_cond_signal(&ipt->cond);
118 pthread_mutex_unlock(&ipt->init_lock);
120 /* wait for other calibration to finish */
121 pthread_mutex_lock(&ipt->start_lock);
123 /* exit if other threads failed to initialize */
124 if (ipc.status == IDLE_PROF_STATUS_ABORT) {
125 pthread_mutex_unlock(&ipt->start_lock);
129 /* exit if we are doing calibration only */
130 if (ipc.status == IDLE_PROF_STATUS_CALI_STOP) {
131 pthread_mutex_unlock(&ipt->start_lock);
135 fio_gettime(&ipt->tps, NULL);
136 ipt->state = TD_RUNNING;
140 for (k = 0; k < page_size; k++) {
141 ipt->data[(k + j) % page_size] = k % 256;
142 if (ipc.status == IDLE_PROF_STATUS_PROF_STOP) {
143 fio_gettime(&ipt->tpe, NULL);
152 ipt->loops = j + (double) k / page_size;
153 ipt->state = TD_EXITED;
154 pthread_mutex_unlock(&ipt->start_lock);
157 free_cpu_affinity(ipt);
161 /* calculate mean and standard deviation to complete an unit of work */
162 static void calibration_stats(void)
165 double sum = 0.0, var = 0.0;
166 struct idle_prof_thread *ipt;
168 for (i = 0; i < ipc.nr_cpus; i++) {
170 sum += ipt->cali_time;
173 ipc.cali_mean = sum/ipc.nr_cpus;
175 for (i = 0; i < ipc.nr_cpus; i++) {
177 var += pow(ipt->cali_time-ipc.cali_mean, 2);
180 ipc.cali_stddev = sqrt(var/(ipc.nr_cpus-1));
183 void fio_idle_prof_init(void)
188 pthread_attr_t tattr;
189 struct idle_prof_thread *ipt;
191 ipc.nr_cpus = cpus_online();
192 ipc.status = IDLE_PROF_STATUS_OK;
194 if (ipc.opt == IDLE_PROF_OPT_NONE)
197 if ((ret = pthread_attr_init(&tattr))) {
198 log_err("fio: pthread_attr_init %s\n", strerror(ret));
201 if ((ret = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM))) {
202 log_err("fio: pthread_attr_setscope %s\n", strerror(ret));
206 ipc.ipts = malloc(ipc.nr_cpus * sizeof(struct idle_prof_thread));
208 log_err("fio: malloc failed\n");
212 ipc.buf = malloc(ipc.nr_cpus * page_size);
214 log_err("fio: malloc failed\n");
220 * profiling aborts on any single thread failure since the
221 * result won't be accurate if any cpu is not used.
223 for (i = 0; i < ipc.nr_cpus; i++) {
227 ipt->state = TD_NOT_CREATED;
228 ipt->data = (unsigned char *)(ipc.buf + page_size * i);
230 if ((ret = pthread_mutex_init(&ipt->init_lock, NULL))) {
231 ipc.status = IDLE_PROF_STATUS_ABORT;
232 log_err("fio: pthread_mutex_init %s\n", strerror(ret));
236 if ((ret = pthread_mutex_init(&ipt->start_lock, NULL))) {
237 ipc.status = IDLE_PROF_STATUS_ABORT;
238 log_err("fio: pthread_mutex_init %s\n", strerror(ret));
242 if ((ret = pthread_cond_init(&ipt->cond, NULL))) {
243 ipc.status = IDLE_PROF_STATUS_ABORT;
244 log_err("fio: pthread_cond_init %s\n", strerror(ret));
248 /* make sure all threads are spawned before they start */
249 pthread_mutex_lock(&ipt->init_lock);
251 /* make sure all threads finish init before profiling starts */
252 pthread_mutex_lock(&ipt->start_lock);
254 if ((ret = pthread_create(&ipt->thread, &tattr, idle_prof_thread_fn, ipt))) {
255 ipc.status = IDLE_PROF_STATUS_ABORT;
256 log_err("fio: pthread_create %s\n", strerror(ret));
259 ipt->state = TD_CREATED;
261 if ((ret = pthread_detach(ipt->thread))) {
262 /* log error and let the thread spin */
263 log_err("fio: pthread_detatch %s\n", strerror(ret));
268 * let good threads continue so that they can exit
269 * if errors on other threads occurred previously.
271 for (i = 0; i < ipc.nr_cpus; i++) {
273 pthread_mutex_unlock(&ipt->init_lock);
276 if (ipc.status == IDLE_PROF_STATUS_ABORT)
279 /* wait for calibration to finish */
280 for (i = 0; i < ipc.nr_cpus; i++) {
282 pthread_mutex_lock(&ipt->init_lock);
283 while ((ipt->state != TD_EXITED) &&
284 (ipt->state!=TD_INITIALIZED)) {
285 fio_gettime(&tp, NULL);
286 ts.tv_sec = tp.tv_sec + 1;
287 ts.tv_nsec = tp.tv_usec * 1000;
288 pthread_cond_timedwait(&ipt->cond, &ipt->init_lock, &ts);
290 pthread_mutex_unlock(&ipt->init_lock);
293 * any thread failed to initialize would abort other threads
294 * later after fio_idle_prof_start.
296 if (ipt->state == TD_EXITED)
297 ipc.status = IDLE_PROF_STATUS_ABORT;
300 if (ipc.status != IDLE_PROF_STATUS_ABORT)
303 ipc.cali_mean = ipc.cali_stddev = 0.0;
305 if (ipc.opt == IDLE_PROF_OPT_CALI)
306 ipc.status = IDLE_PROF_STATUS_CALI_STOP;
309 void fio_idle_prof_start(void)
312 struct idle_prof_thread *ipt;
314 if (ipc.opt == IDLE_PROF_OPT_NONE)
317 /* unlock regardless abort is set or not */
318 for (i = 0; i < ipc.nr_cpus; i++) {
320 pthread_mutex_unlock(&ipt->start_lock);
324 void fio_idle_prof_stop(void)
330 struct idle_prof_thread *ipt;
332 if (ipc.opt == IDLE_PROF_OPT_NONE)
335 if (ipc.opt == IDLE_PROF_OPT_CALI)
338 ipc.status = IDLE_PROF_STATUS_PROF_STOP;
340 /* wait for all threads to exit from profiling */
341 for (i = 0; i < ipc.nr_cpus; i++) {
343 pthread_mutex_lock(&ipt->start_lock);
344 while ((ipt->state != TD_EXITED) &&
345 (ipt->state!=TD_NOT_CREATED)) {
346 fio_gettime(&tp, NULL);
347 ts.tv_sec = tp.tv_sec + 1;
348 ts.tv_nsec = tp.tv_usec * 1000;
349 /* timed wait in case a signal is not received */
350 pthread_cond_timedwait(&ipt->cond, &ipt->start_lock, &ts);
352 pthread_mutex_unlock(&ipt->start_lock);
354 /* calculate idleness */
355 if (ipc.cali_mean != 0.0) {
356 runt = utime_since(&ipt->tps, &ipt->tpe);
358 ipt->idleness = ipt->loops * ipc.cali_mean / runt;
366 * memory allocations are freed via explicit fio_idle_prof_cleanup
367 * after profiling stats are collected by apps.
372 * return system idle percentage when cpu is -1;
373 * return one cpu idle percentage otherwise.
375 static double fio_idle_prof_cpu_stat(int cpu)
377 int i, nr_cpus = ipc.nr_cpus;
378 struct idle_prof_thread *ipt;
381 if (ipc.opt == IDLE_PROF_OPT_NONE)
384 if ((cpu >= nr_cpus) || (cpu < -1)) {
385 log_err("fio: idle profiling invalid cpu index\n");
390 for (i = 0; i < nr_cpus; i++) {
396 ipt = &ipc.ipts[cpu];
403 static void fio_idle_prof_cleanup(void)
416 int fio_idle_prof_parse_opt(const char *args)
418 ipc.opt = IDLE_PROF_OPT_NONE; /* default */
421 log_err("fio: empty idle-prof option string\n");
425 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(CONFIG_SCHED_IDLE)
426 if (strcmp("calibrate", args) == 0) {
427 ipc.opt = IDLE_PROF_OPT_CALI;
428 fio_idle_prof_init();
429 fio_idle_prof_start();
430 fio_idle_prof_stop();
431 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL, NULL);
433 } else if (strcmp("system", args) == 0) {
434 ipc.opt = IDLE_PROF_OPT_SYSTEM;
436 } else if (strcmp("percpu", args) == 0) {
437 ipc.opt = IDLE_PROF_OPT_PERCPU;
440 log_err("fio: incorrect idle-prof option: %s\n", args);
444 log_err("fio: idle-prof not supported on this platform\n");
449 void show_idle_prof_stats(int output, struct json_object *parent,
450 struct buf_output *out)
452 int i, nr_cpus = ipc.nr_cpus;
453 struct json_object *tmp;
454 char s[MAX_CPU_STR_LEN];
456 if (output == FIO_OUTPUT_NORMAL) {
457 if (ipc.opt > IDLE_PROF_OPT_CALI)
458 log_buf(out, "\nCPU idleness:\n");
459 else if (ipc.opt == IDLE_PROF_OPT_CALI)
460 log_buf(out, "CPU idleness:\n");
462 if (ipc.opt >= IDLE_PROF_OPT_SYSTEM)
463 log_buf(out, " system: %3.2f%%\n", fio_idle_prof_cpu_stat(-1));
465 if (ipc.opt == IDLE_PROF_OPT_PERCPU) {
466 log_buf(out, " percpu: %3.2f%%", fio_idle_prof_cpu_stat(0));
467 for (i = 1; i < nr_cpus; i++)
468 log_buf(out, ", %3.2f%%", fio_idle_prof_cpu_stat(i));
472 if (ipc.opt >= IDLE_PROF_OPT_CALI) {
473 log_buf(out, " unit work: mean=%3.2fus,", ipc.cali_mean);
474 log_buf(out, " stddev=%3.2f\n", ipc.cali_stddev);
477 /* dynamic mem allocations can now be freed */
478 if (ipc.opt != IDLE_PROF_OPT_NONE)
479 fio_idle_prof_cleanup();
484 if ((ipc.opt != IDLE_PROF_OPT_NONE) && (output & FIO_OUTPUT_JSON)) {
488 tmp = json_create_object();
492 json_object_add_value_object(parent, "cpu_idleness", tmp);
493 json_object_add_value_float(tmp, "system", fio_idle_prof_cpu_stat(-1));
495 if (ipc.opt == IDLE_PROF_OPT_PERCPU) {
496 for (i = 0; i < nr_cpus; i++) {
497 snprintf(s, MAX_CPU_STR_LEN, "cpu-%d", i);
498 json_object_add_value_float(tmp, s, fio_idle_prof_cpu_stat(i));
502 json_object_add_value_float(tmp, "unit_mean", ipc.cali_mean);
503 json_object_add_value_float(tmp, "unit_stddev", ipc.cali_stddev);
505 fio_idle_prof_cleanup();