5 static volatile struct idle_prof_common ipc;
7 /* Get time to complete an unit work on a particular cpu.
8 * The minimum number in CALIBRATE_RUNS runs is returned.
10 static double calibrate_unit(unsigned char *data)
12 unsigned long t, i, j, k;
16 for (i=0; i<CALIBRATE_RUNS; i++) {
18 fio_gettime(&tps, NULL);
19 /* scale for less variance */
20 for (j=0; j < CALIBRATE_SCALE; j++) {
22 for (k=0; k < page_size; k++) {
23 data[(k+j)%page_size] = k%256;
24 /* we won't see STOP here. this is to match
25 * the same statement in the profiling loop.
27 if (ipc.status == IDLE_PROF_STATUS_PROF_STOP)
32 t = utime_since_now(&tps);
36 /* get the minimum time to complete CALIBRATE_SCALE units */
37 if ((i==0) || ((double)t < tunit))
41 return tunit/CALIBRATE_SCALE;
44 static void *idle_prof_thread_fn(void *data)
48 struct idle_prof_thread *ipt = data;
50 /* wait for all threads are spawned */
51 pthread_mutex_lock(&ipt->init_lock);
53 /* exit if any other thread failed to start */
54 if (ipc.status == IDLE_PROF_STATUS_ABORT)
57 #if defined(FIO_HAVE_CPU_AFFINITY)
58 os_cpu_mask_t cpu_mask;
59 memset(&cpu_mask, 0, sizeof(cpu_mask));
60 fio_cpu_set(&cpu_mask, ipt->cpu);
62 if ((retval=fio_setaffinity(gettid(), cpu_mask)) == -1)
63 log_err("fio: fio_setaffinity failed\n");
66 log_err("fio: fio_setaffinity not supported\n");
69 ipt->state = TD_EXITED;
70 pthread_mutex_unlock(&ipt->init_lock);
74 ipt->cali_time = calibrate_unit(ipt->data);
76 /* delay to set IDLE class till now for better calibration accuracy */
77 #if defined(FIO_HAVE_SCHED_IDLE)
78 if ((retval = fio_set_sched_idle()))
79 log_err("fio: fio_set_sched_idle failed\n");
82 log_err("fio: fio_set_sched_idle not supported\n");
85 ipt->state = TD_EXITED;
86 pthread_mutex_unlock(&ipt->init_lock);
90 ipt->state = TD_INITIALIZED;
92 /* signal the main thread that calibration is done */
93 pthread_cond_signal(&ipt->cond);
94 pthread_mutex_unlock(&ipt->init_lock);
96 /* wait for other calibration to finish */
97 pthread_mutex_lock(&ipt->start_lock);
99 /* exit if other threads failed to initialize */
100 if (ipc.status == IDLE_PROF_STATUS_ABORT)
103 /* exit if we are doing calibration only */
104 if (ipc.status == IDLE_PROF_STATUS_CALI_STOP)
107 fio_gettime(&ipt->tps, NULL);
108 ipt->state = TD_RUNNING;
112 for (k=0; k < page_size; k++) {
113 ipt->data[(k+j)%page_size] = k%256;
114 if (ipc.status == IDLE_PROF_STATUS_PROF_STOP) {
115 fio_gettime(&ipt->tpe, NULL);
124 ipt->loops = j + (double)k/page_size;
125 ipt->state = TD_EXITED;
126 pthread_mutex_unlock(&ipt->start_lock);
131 /* calculate mean and standard deviation to complete an unit of work */
132 static void calibration_stats(void)
135 double sum=0.0, var=0.0;
136 struct idle_prof_thread *ipt;
138 for (i = 0; i < ipc.nr_cpus; i++) {
140 sum += ipt->cali_time;
143 ipc.cali_mean = sum/ipc.nr_cpus;
145 for (i = 0; i < ipc.nr_cpus; i++) {
147 var += pow(ipt->cali_time-ipc.cali_mean, 2);
150 ipc.cali_stddev = sqrt(var/(ipc.nr_cpus-1));
153 void fio_idle_prof_init(void)
158 pthread_attr_t tattr;
159 struct idle_prof_thread *ipt;
161 ipc.nr_cpus = cpus_online();
162 ipc.status = IDLE_PROF_STATUS_OK;
164 if (ipc.opt == IDLE_PROF_OPT_NONE)
167 if ((ret = pthread_attr_init(&tattr))) {
168 log_err("fio: pthread_attr_init %s\n", strerror(ret));
171 if ((ret = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM))) {
172 log_err("fio: pthread_attr_setscope %s\n", strerror(ret));
176 ipc.ipts = malloc(ipc.nr_cpus * sizeof(struct idle_prof_thread));
178 log_err("fio: malloc failed\n");
182 ipc.buf = malloc(ipc.nr_cpus * page_size);
184 log_err("fio: malloc failed\n");
189 /* profiling aborts on any single thread failure since the
190 * result won't be accurate if any cpu is not used.
192 for (i = 0; i < ipc.nr_cpus; i++) {
196 ipt->state = TD_NOT_CREATED;
197 ipt->data = (unsigned char *)(ipc.buf + page_size * i);
199 if ((ret = pthread_mutex_init(&ipt->init_lock, NULL))) {
200 ipc.status = IDLE_PROF_STATUS_ABORT;
201 log_err("fio: pthread_mutex_init %s\n", strerror(ret));
205 if ((ret = pthread_mutex_init(&ipt->start_lock, NULL))) {
206 ipc.status = IDLE_PROF_STATUS_ABORT;
207 log_err("fio: pthread_mutex_init %s\n", strerror(ret));
211 if ((ret = pthread_cond_init(&ipt->cond, NULL))) {
212 ipc.status = IDLE_PROF_STATUS_ABORT;
213 log_err("fio: pthread_cond_init %s\n", strerror(ret));
217 /* make sure all threads are spawned before they start */
218 pthread_mutex_lock(&ipt->init_lock);
220 /* make sure all threads finish init before profiling starts */
221 pthread_mutex_lock(&ipt->start_lock);
223 if ((ret = pthread_create(&ipt->thread, &tattr, idle_prof_thread_fn, ipt))) {
224 ipc.status = IDLE_PROF_STATUS_ABORT;
225 log_err("fio: pthread_create %s\n", strerror(ret));
228 ipt->state = TD_CREATED;
231 if ((ret = pthread_detach(ipt->thread))) {
232 /* log error and let the thread spin */
233 log_err("fio: pthread_detatch %s\n", strerror(ret));
237 /* let good threads continue so that they can exit
238 * if errors on other threads occurred previously.
240 for (i = 0; i < ipc.nr_cpus; i++) {
242 pthread_mutex_unlock(&ipt->init_lock);
245 if (ipc.status == IDLE_PROF_STATUS_ABORT)
248 /* wait for calibration to finish */
249 for (i = 0; i < ipc.nr_cpus; i++) {
251 pthread_mutex_lock(&ipt->init_lock);
252 while ((ipt->state!=TD_EXITED) && (ipt->state!=TD_INITIALIZED)) {
253 fio_gettime(&tp, NULL);
254 ts.tv_sec = tp.tv_sec + 1;
255 ts.tv_nsec = tp.tv_usec * 1000;
256 pthread_cond_timedwait(&ipt->cond, &ipt->init_lock, &ts);
258 pthread_mutex_unlock(&ipt->init_lock);
260 /* any thread failed to initialize would abort other threads
261 * later after fio_idle_prof_start.
263 if (ipt->state == TD_EXITED)
264 ipc.status = IDLE_PROF_STATUS_ABORT;
267 if (ipc.status != IDLE_PROF_STATUS_ABORT)
270 ipc.cali_mean = ipc.cali_stddev = 0.0;
272 if (ipc.opt == IDLE_PROF_OPT_CALI)
273 ipc.status = IDLE_PROF_STATUS_CALI_STOP;
276 void fio_idle_prof_start(void)
279 struct idle_prof_thread *ipt;
281 if (ipc.opt == IDLE_PROF_OPT_NONE)
284 /* unlock regardless abort is set or not */
285 for (i = 0; i < ipc.nr_cpus; i++) {
287 pthread_mutex_unlock(&ipt->start_lock);
291 void fio_idle_prof_stop(void)
297 struct idle_prof_thread *ipt;
299 if (ipc.opt == IDLE_PROF_OPT_NONE)
302 if (ipc.opt == IDLE_PROF_OPT_CALI)
305 ipc.status = IDLE_PROF_STATUS_PROF_STOP;
307 /* wait for all threads to exit from profiling */
308 for (i = 0; i < ipc.nr_cpus; i++) {
310 pthread_mutex_lock(&ipt->start_lock);
311 while ((ipt->state!=TD_EXITED) && (ipt->state!=TD_NOT_CREATED)) {
312 fio_gettime(&tp, NULL);
313 ts.tv_sec = tp.tv_sec + 1;
314 ts.tv_nsec = tp.tv_usec * 1000;
315 /* timed wait in case a signal is not received */
316 pthread_cond_timedwait(&ipt->cond, &ipt->start_lock, &ts);
318 pthread_mutex_unlock(&ipt->start_lock);
320 /* calculate idleness */
321 if (ipc.cali_mean != 0.0) {
322 runt = utime_since(&ipt->tps, &ipt->tpe);
323 ipt->idleness = ipt->loops * ipc.cali_mean / runt;
328 /* memory allocations are freed via explicit fio_idle_prof_cleanup
329 * after profiling stats are collected by apps.
335 /* return system idle percentage when cpu is -1;
336 * return one cpu idle percentage otherwise.
338 static double fio_idle_prof_cpu_stat(int cpu)
340 int i, nr_cpus = ipc.nr_cpus;
341 struct idle_prof_thread *ipt;
344 if (ipc.opt == IDLE_PROF_OPT_NONE)
347 if ((cpu >= nr_cpus) || (cpu < -1)) {
348 log_err("fio: idle profiling invalid cpu index\n");
353 for (i = 0; i < nr_cpus; i++) {
359 ipt = &ipc.ipts[cpu];
366 void fio_idle_prof_cleanup(void)
379 int fio_idle_prof_parse_opt(const char *args)
381 ipc.opt = IDLE_PROF_OPT_NONE; /* default */
384 log_err("fio: empty idle-prof option string\n");
388 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(FIO_HAVE_SCHED_IDLE)
389 if (strcmp("calibrate", args) == 0) {
390 ipc.opt = IDLE_PROF_OPT_CALI;
391 fio_idle_prof_init();
392 fio_idle_prof_start();
393 fio_idle_prof_stop();
394 show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL);
396 } else if (strcmp("system", args) == 0) {
397 ipc.opt = IDLE_PROF_OPT_SYSTEM;
399 } else if (strcmp("percpu", args) == 0) {
400 ipc.opt = IDLE_PROF_OPT_PERCPU;
403 log_err("fio: incorrect idle-prof option\n", args);
407 log_err("fio: idle-prof not supported on this platform\n");
412 void show_idle_prof_stats(int output, struct json_object *parent)
414 int i, nr_cpus = ipc.nr_cpus;
415 struct json_object *tmp;
416 char s[MAX_CPU_STR_LEN];
418 if (output == FIO_OUTPUT_NORMAL) {
419 if (ipc.opt > IDLE_PROF_OPT_CALI)
420 log_info("\nCPU idleness:\n");
421 else if (ipc.opt == IDLE_PROF_OPT_CALI)
422 log_info("CPU idleness:\n");
424 if (ipc.opt >= IDLE_PROF_OPT_SYSTEM)
425 log_info(" system: %3.2f%%\n", fio_idle_prof_cpu_stat(-1));
427 if (ipc.opt == IDLE_PROF_OPT_PERCPU) {
428 log_info(" percpu: %3.2f%%", fio_idle_prof_cpu_stat(0));
429 for (i=1; i<nr_cpus; i++) {
430 log_info(", %3.2f%%", fio_idle_prof_cpu_stat(i));
435 if (ipc.opt >= IDLE_PROF_OPT_CALI) {
436 log_info(" unit work: mean=%3.2fus,", ipc.cali_mean);
437 log_info(" stddev=%3.2f\n", ipc.cali_stddev);
440 /* dynamic mem allocations can now be freed */
441 if (ipc.opt != IDLE_PROF_OPT_NONE)
442 fio_idle_prof_cleanup();
447 if ((ipc.opt != IDLE_PROF_OPT_NONE) && (output == FIO_OUTPUT_JSON)) {
451 tmp = json_create_object();
455 json_object_add_value_object(parent, "cpu_idleness", tmp);
456 json_object_add_value_float(tmp, "system", fio_idle_prof_cpu_stat(-1));
458 if (ipc.opt == IDLE_PROF_OPT_PERCPU) {
459 for (i=0; i<nr_cpus; i++) {
460 snprintf(s, MAX_CPU_STR_LEN, "cpu-%d", i);
461 json_object_add_value_float(tmp, s, fio_idle_prof_cpu_stat(i));
465 json_object_add_value_float(tmp, "unit_mean", ipc.cali_mean);
466 json_object_add_value_float(tmp, "unit_stddev", ipc.cali_stddev);
468 fio_idle_prof_cleanup();