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 int set_cpu_affinity(struct idle_prof_thread *ipt)
48 #if defined(FIO_HAVE_CPU_AFFINITY)
49 os_cpu_mask_t cpu_mask;
51 memset(&cpu_mask, 0, sizeof(cpu_mask));
52 fio_cpu_set(&cpu_mask, ipt->cpu);
54 if (fio_setaffinity(gettid(), cpu_mask)) {
55 log_err("fio: fio_setaffinity failed\n");
61 log_err("fio: fio_setaffinity not supported\n");
66 static void *idle_prof_thread_fn(void *data)
70 struct idle_prof_thread *ipt = data;
72 /* wait for all threads are spawned */
73 pthread_mutex_lock(&ipt->init_lock);
75 /* exit if any other thread failed to start */
76 if (ipc.status == IDLE_PROF_STATUS_ABORT)
79 retval = set_cpu_affinity(ipt);
81 ipt->state = TD_EXITED;
82 pthread_mutex_unlock(&ipt->init_lock);
86 ipt->cali_time = calibrate_unit(ipt->data);
88 /* delay to set IDLE class till now for better calibration accuracy */
89 #if defined(CONFIG_SCHED_IDLE)
90 if ((retval = fio_set_sched_idle()))
91 log_err("fio: fio_set_sched_idle failed\n");
94 log_err("fio: fio_set_sched_idle not supported\n");
97 ipt->state = TD_EXITED;
98 pthread_mutex_unlock(&ipt->init_lock);
102 ipt->state = TD_INITIALIZED;
104 /* signal the main thread that calibration is done */
105 pthread_cond_signal(&ipt->cond);
106 pthread_mutex_unlock(&ipt->init_lock);
108 /* wait for other calibration to finish */
109 pthread_mutex_lock(&ipt->start_lock);
111 /* exit if other threads failed to initialize */
112 if (ipc.status == IDLE_PROF_STATUS_ABORT)
115 /* exit if we are doing calibration only */
116 if (ipc.status == IDLE_PROF_STATUS_CALI_STOP)
119 fio_gettime(&ipt->tps, NULL);
120 ipt->state = TD_RUNNING;
124 for (k = 0; k < page_size; k++) {
125 ipt->data[(k + j) % page_size] = k % 256;
126 if (ipc.status == IDLE_PROF_STATUS_PROF_STOP) {
127 fio_gettime(&ipt->tpe, NULL);
136 ipt->loops = j + (double) k / page_size;
137 ipt->state = TD_EXITED;
138 pthread_mutex_unlock(&ipt->start_lock);
143 /* calculate mean and standard deviation to complete an unit of work */
144 static void calibration_stats(void)
147 double sum = 0.0, var = 0.0;
148 struct idle_prof_thread *ipt;
150 for (i = 0; i < ipc.nr_cpus; i++) {
152 sum += ipt->cali_time;
155 ipc.cali_mean = sum/ipc.nr_cpus;
157 for (i = 0; i < ipc.nr_cpus; i++) {
159 var += pow(ipt->cali_time-ipc.cali_mean, 2);
162 ipc.cali_stddev = sqrt(var/(ipc.nr_cpus-1));
165 void fio_idle_prof_init(void)
170 pthread_attr_t tattr;
171 struct idle_prof_thread *ipt;
173 ipc.nr_cpus = cpus_online();
174 ipc.status = IDLE_PROF_STATUS_OK;
176 if (ipc.opt == IDLE_PROF_OPT_NONE)
179 if ((ret = pthread_attr_init(&tattr))) {
180 log_err("fio: pthread_attr_init %s\n", strerror(ret));
183 if ((ret = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM))) {
184 log_err("fio: pthread_attr_setscope %s\n", strerror(ret));
188 ipc.ipts = malloc(ipc.nr_cpus * sizeof(struct idle_prof_thread));
190 log_err("fio: malloc failed\n");
194 ipc.buf = malloc(ipc.nr_cpus * page_size);
196 log_err("fio: malloc failed\n");
202 * profiling aborts on any single thread failure since the
203 * result won't be accurate if any cpu is not used.
205 for (i = 0; i < ipc.nr_cpus; i++) {
209 ipt->state = TD_NOT_CREATED;
210 ipt->data = (unsigned char *)(ipc.buf + page_size * i);
212 if ((ret = pthread_mutex_init(&ipt->init_lock, NULL))) {
213 ipc.status = IDLE_PROF_STATUS_ABORT;
214 log_err("fio: pthread_mutex_init %s\n", strerror(ret));
218 if ((ret = pthread_mutex_init(&ipt->start_lock, NULL))) {
219 ipc.status = IDLE_PROF_STATUS_ABORT;
220 log_err("fio: pthread_mutex_init %s\n", strerror(ret));
224 if ((ret = pthread_cond_init(&ipt->cond, NULL))) {
225 ipc.status = IDLE_PROF_STATUS_ABORT;
226 log_err("fio: pthread_cond_init %s\n", strerror(ret));
230 /* make sure all threads are spawned before they start */
231 pthread_mutex_lock(&ipt->init_lock);
233 /* make sure all threads finish init before profiling starts */
234 pthread_mutex_lock(&ipt->start_lock);
236 if ((ret = pthread_create(&ipt->thread, &tattr, idle_prof_thread_fn, ipt))) {
237 ipc.status = IDLE_PROF_STATUS_ABORT;
238 log_err("fio: pthread_create %s\n", strerror(ret));
241 ipt->state = TD_CREATED;
243 if ((ret = pthread_detach(ipt->thread))) {
244 /* log error and let the thread spin */
245 log_err("fio: pthread_detatch %s\n", strerror(ret));
250 * let good threads continue so that they can exit
251 * if errors on other threads occurred previously.
253 for (i = 0; i < ipc.nr_cpus; i++) {
255 pthread_mutex_unlock(&ipt->init_lock);
258 if (ipc.status == IDLE_PROF_STATUS_ABORT)
261 /* wait for calibration to finish */
262 for (i = 0; i < ipc.nr_cpus; i++) {
264 pthread_mutex_lock(&ipt->init_lock);
265 while ((ipt->state != TD_EXITED) &&
266 (ipt->state!=TD_INITIALIZED)) {
267 fio_gettime(&tp, NULL);
268 ts.tv_sec = tp.tv_sec + 1;
269 ts.tv_nsec = tp.tv_usec * 1000;
270 pthread_cond_timedwait(&ipt->cond, &ipt->init_lock, &ts);
272 pthread_mutex_unlock(&ipt->init_lock);
275 * any thread failed to initialize would abort other threads
276 * later after fio_idle_prof_start.
278 if (ipt->state == TD_EXITED)
279 ipc.status = IDLE_PROF_STATUS_ABORT;
282 if (ipc.status != IDLE_PROF_STATUS_ABORT)
285 ipc.cali_mean = ipc.cali_stddev = 0.0;
287 if (ipc.opt == IDLE_PROF_OPT_CALI)
288 ipc.status = IDLE_PROF_STATUS_CALI_STOP;
291 void fio_idle_prof_start(void)
294 struct idle_prof_thread *ipt;
296 if (ipc.opt == IDLE_PROF_OPT_NONE)
299 /* unlock regardless abort is set or not */
300 for (i = 0; i < ipc.nr_cpus; i++) {
302 pthread_mutex_unlock(&ipt->start_lock);
306 void fio_idle_prof_stop(void)
312 struct idle_prof_thread *ipt;
314 if (ipc.opt == IDLE_PROF_OPT_NONE)
317 if (ipc.opt == IDLE_PROF_OPT_CALI)
320 ipc.status = IDLE_PROF_STATUS_PROF_STOP;
322 /* wait for all threads to exit from profiling */
323 for (i = 0; i < ipc.nr_cpus; i++) {
325 pthread_mutex_lock(&ipt->start_lock);
326 while ((ipt->state != TD_EXITED) &&
327 (ipt->state!=TD_NOT_CREATED)) {
328 fio_gettime(&tp, NULL);
329 ts.tv_sec = tp.tv_sec + 1;
330 ts.tv_nsec = tp.tv_usec * 1000;
331 /* timed wait in case a signal is not received */
332 pthread_cond_timedwait(&ipt->cond, &ipt->start_lock, &ts);
334 pthread_mutex_unlock(&ipt->start_lock);
336 /* calculate idleness */
337 if (ipc.cali_mean != 0.0) {
338 runt = utime_since(&ipt->tps, &ipt->tpe);
340 ipt->idleness = ipt->loops * ipc.cali_mean / runt;
348 * memory allocations are freed via explicit fio_idle_prof_cleanup
349 * after profiling stats are collected by apps.
354 * return system idle percentage when cpu is -1;
355 * return one cpu idle percentage otherwise.
357 static double fio_idle_prof_cpu_stat(int cpu)
359 int i, nr_cpus = ipc.nr_cpus;
360 struct idle_prof_thread *ipt;
363 if (ipc.opt == IDLE_PROF_OPT_NONE)
366 if ((cpu >= nr_cpus) || (cpu < -1)) {
367 log_err("fio: idle profiling invalid cpu index\n");
372 for (i = 0; i < nr_cpus; i++) {
378 ipt = &ipc.ipts[cpu];
385 static void fio_idle_prof_cleanup(void)
398 int fio_idle_prof_parse_opt(const char *args)
400 ipc.opt = IDLE_PROF_OPT_NONE; /* default */
403 log_err("fio: empty idle-prof option string\n");
407 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(CONFIG_SCHED_IDLE)
408 if (strcmp("calibrate", args) == 0) {
409 ipc.opt = IDLE_PROF_OPT_CALI;
410 fio_idle_prof_init();
411 fio_idle_prof_start();
412 fio_idle_prof_stop();
413 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL);
415 } else if (strcmp("system", args) == 0) {
416 ipc.opt = IDLE_PROF_OPT_SYSTEM;
418 } else if (strcmp("percpu", args) == 0) {
419 ipc.opt = IDLE_PROF_OPT_PERCPU;
422 log_err("fio: incorrect idle-prof option: %s\n", args);
426 log_err("fio: idle-prof not supported on this platform\n");
431 void show_idle_prof_stats(int output, struct json_object *parent)
433 int i, nr_cpus = ipc.nr_cpus;
434 struct json_object *tmp;
435 char s[MAX_CPU_STR_LEN];
437 if (output == FIO_OUTPUT_NORMAL) {
438 if (ipc.opt > IDLE_PROF_OPT_CALI)
439 log_info("\nCPU idleness:\n");
440 else if (ipc.opt == IDLE_PROF_OPT_CALI)
441 log_info("CPU idleness:\n");
443 if (ipc.opt >= IDLE_PROF_OPT_SYSTEM)
444 log_info(" system: %3.2f%%\n", fio_idle_prof_cpu_stat(-1));
446 if (ipc.opt == IDLE_PROF_OPT_PERCPU) {
447 log_info(" percpu: %3.2f%%", fio_idle_prof_cpu_stat(0));
448 for (i = 1; i < nr_cpus; i++)
449 log_info(", %3.2f%%", fio_idle_prof_cpu_stat(i));
453 if (ipc.opt >= IDLE_PROF_OPT_CALI) {
454 log_info(" unit work: mean=%3.2fus,", ipc.cali_mean);
455 log_info(" stddev=%3.2f\n", ipc.cali_stddev);
458 /* dynamic mem allocations can now be freed */
459 if (ipc.opt != IDLE_PROF_OPT_NONE)
460 fio_idle_prof_cleanup();
465 if ((ipc.opt != IDLE_PROF_OPT_NONE) && (output == FIO_OUTPUT_JSON)) {
469 tmp = json_create_object();
473 json_object_add_value_object(parent, "cpu_idleness", tmp);
474 json_object_add_value_float(tmp, "system", fio_idle_prof_cpu_stat(-1));
476 if (ipc.opt == IDLE_PROF_OPT_PERCPU) {
477 for (i = 0; i < nr_cpus; i++) {
478 snprintf(s, MAX_CPU_STR_LEN, "cpu-%d", i);
479 json_object_add_value_float(tmp, s, fio_idle_prof_cpu_stat(i));
483 json_object_add_value_float(tmp, "unit_mean", ipc.cali_mean);
484 json_object_add_value_float(tmp, "unit_stddev", ipc.cali_stddev);
486 fio_idle_prof_cleanup();