Commit | Line | Data |
---|---|---|
f2a2ce0e | 1 | #include <math.h> |
3d2d14bc | 2 | #include "fio.h" |
f2a2ce0e HL |
3 | #include "json.h" |
4 | #include "idletime.h" | |
5 | ||
6 | static volatile struct idle_prof_common ipc; | |
7 | ||
680b5abc JA |
8 | /* |
9 | * Get time to complete an unit work on a particular cpu. | |
f2a2ce0e HL |
10 | * The minimum number in CALIBRATE_RUNS runs is returned. |
11 | */ | |
12 | static double calibrate_unit(unsigned char *data) | |
13 | { | |
14 | unsigned long t, i, j, k; | |
8b6a404c | 15 | struct timespec tps; |
f2a2ce0e HL |
16 | double tunit = 0.0; |
17 | ||
680b5abc | 18 | for (i = 0; i < CALIBRATE_RUNS; i++) { |
f2a2ce0e HL |
19 | |
20 | fio_gettime(&tps, NULL); | |
21 | /* scale for less variance */ | |
680b5abc | 22 | for (j = 0; j < CALIBRATE_SCALE; j++) { |
f2a2ce0e HL |
23 | /* unit of work */ |
24 | for (k=0; k < page_size; k++) { | |
680b5abc JA |
25 | data[(k + j) % page_size] = k % 256; |
26 | /* | |
27 | * we won't see STOP here. this is to match | |
f2a2ce0e HL |
28 | * the same statement in the profiling loop. |
29 | */ | |
30 | if (ipc.status == IDLE_PROF_STATUS_PROF_STOP) | |
31 | return 0.0; | |
32 | } | |
33 | } | |
34 | ||
35 | t = utime_since_now(&tps); | |
36 | if (!t) | |
37 | continue; | |
38 | ||
39 | /* get the minimum time to complete CALIBRATE_SCALE units */ | |
680b5abc | 40 | if ((i == 0) || ((double)t < tunit)) |
f2a2ce0e HL |
41 | tunit = (double)t; |
42 | } | |
43 | ||
680b5abc | 44 | return tunit / CALIBRATE_SCALE; |
f2a2ce0e HL |
45 | } |
46 | ||
54ed125b JA |
47 | static void free_cpu_affinity(struct idle_prof_thread *ipt) |
48 | { | |
49 | #if defined(FIO_HAVE_CPU_AFFINITY) | |
50 | fio_cpuset_exit(&ipt->cpu_mask); | |
51 | #endif | |
52 | } | |
53 | ||
59358c8e JA |
54 | static int set_cpu_affinity(struct idle_prof_thread *ipt) |
55 | { | |
56 | #if defined(FIO_HAVE_CPU_AFFINITY) | |
54ed125b JA |
57 | if (fio_cpuset_init(&ipt->cpu_mask)) { |
58 | log_err("fio: cpuset init failed\n"); | |
59 | return -1; | |
60 | } | |
59358c8e | 61 | |
54ed125b | 62 | fio_cpu_set(&ipt->cpu_mask, ipt->cpu); |
59358c8e | 63 | |
54ed125b | 64 | if (fio_setaffinity(gettid(), ipt->cpu_mask)) { |
59358c8e | 65 | log_err("fio: fio_setaffinity failed\n"); |
54ed125b | 66 | fio_cpuset_exit(&ipt->cpu_mask); |
59358c8e JA |
67 | return -1; |
68 | } | |
69 | ||
70 | return 0; | |
71 | #else | |
72 | log_err("fio: fio_setaffinity not supported\n"); | |
73 | return -1; | |
74 | #endif | |
75 | } | |
76 | ||
f2a2ce0e HL |
77 | static void *idle_prof_thread_fn(void *data) |
78 | { | |
79 | int retval; | |
80 | unsigned long j, k; | |
81 | struct idle_prof_thread *ipt = data; | |
82 | ||
83 | /* wait for all threads are spawned */ | |
84 | pthread_mutex_lock(&ipt->init_lock); | |
85 | ||
86 | /* exit if any other thread failed to start */ | |
d5b351d4 JA |
87 | if (ipc.status == IDLE_PROF_STATUS_ABORT) { |
88 | pthread_mutex_unlock(&ipt->init_lock); | |
f2a2ce0e | 89 | return NULL; |
d5b351d4 | 90 | } |
f2a2ce0e | 91 | |
59358c8e | 92 | retval = set_cpu_affinity(ipt); |
f2a2ce0e HL |
93 | if (retval == -1) { |
94 | ipt->state = TD_EXITED; | |
95 | pthread_mutex_unlock(&ipt->init_lock); | |
96 | return NULL; | |
97 | } | |
98 | ||
99 | ipt->cali_time = calibrate_unit(ipt->data); | |
100 | ||
101 | /* delay to set IDLE class till now for better calibration accuracy */ | |
7e09a9f1 | 102 | #if defined(CONFIG_SCHED_IDLE) |
f2a2ce0e HL |
103 | if ((retval = fio_set_sched_idle())) |
104 | log_err("fio: fio_set_sched_idle failed\n"); | |
105 | #else | |
106 | retval = -1; | |
107 | log_err("fio: fio_set_sched_idle not supported\n"); | |
108 | #endif | |
109 | if (retval == -1) { | |
110 | ipt->state = TD_EXITED; | |
111 | pthread_mutex_unlock(&ipt->init_lock); | |
54ed125b | 112 | goto do_exit; |
f2a2ce0e HL |
113 | } |
114 | ||
115 | ipt->state = TD_INITIALIZED; | |
116 | ||
117 | /* signal the main thread that calibration is done */ | |
118 | pthread_cond_signal(&ipt->cond); | |
119 | pthread_mutex_unlock(&ipt->init_lock); | |
120 | ||
121 | /* wait for other calibration to finish */ | |
122 | pthread_mutex_lock(&ipt->start_lock); | |
123 | ||
124 | /* exit if other threads failed to initialize */ | |
735ed278 JA |
125 | if (ipc.status == IDLE_PROF_STATUS_ABORT) { |
126 | pthread_mutex_unlock(&ipt->start_lock); | |
54ed125b | 127 | goto do_exit; |
735ed278 | 128 | } |
f2a2ce0e HL |
129 | |
130 | /* exit if we are doing calibration only */ | |
735ed278 JA |
131 | if (ipc.status == IDLE_PROF_STATUS_CALI_STOP) { |
132 | pthread_mutex_unlock(&ipt->start_lock); | |
54ed125b | 133 | goto do_exit; |
735ed278 | 134 | } |
f2a2ce0e HL |
135 | |
136 | fio_gettime(&ipt->tps, NULL); | |
137 | ipt->state = TD_RUNNING; | |
138 | ||
139 | j = 0; | |
140 | while (1) { | |
680b5abc JA |
141 | for (k = 0; k < page_size; k++) { |
142 | ipt->data[(k + j) % page_size] = k % 256; | |
f2a2ce0e HL |
143 | if (ipc.status == IDLE_PROF_STATUS_PROF_STOP) { |
144 | fio_gettime(&ipt->tpe, NULL); | |
145 | goto idle_prof_done; | |
146 | } | |
147 | } | |
148 | j++; | |
149 | } | |
150 | ||
151 | idle_prof_done: | |
152 | ||
680b5abc | 153 | ipt->loops = j + (double) k / page_size; |
f2a2ce0e HL |
154 | ipt->state = TD_EXITED; |
155 | pthread_mutex_unlock(&ipt->start_lock); | |
156 | ||
54ed125b JA |
157 | do_exit: |
158 | free_cpu_affinity(ipt); | |
f2a2ce0e HL |
159 | return NULL; |
160 | } | |
161 | ||
162 | /* calculate mean and standard deviation to complete an unit of work */ | |
163 | static void calibration_stats(void) | |
164 | { | |
165 | int i; | |
680b5abc | 166 | double sum = 0.0, var = 0.0; |
f2a2ce0e HL |
167 | struct idle_prof_thread *ipt; |
168 | ||
169 | for (i = 0; i < ipc.nr_cpus; i++) { | |
170 | ipt = &ipc.ipts[i]; | |
171 | sum += ipt->cali_time; | |
172 | } | |
173 | ||
174 | ipc.cali_mean = sum/ipc.nr_cpus; | |
175 | ||
176 | for (i = 0; i < ipc.nr_cpus; i++) { | |
177 | ipt = &ipc.ipts[i]; | |
178 | var += pow(ipt->cali_time-ipc.cali_mean, 2); | |
179 | } | |
180 | ||
181 | ipc.cali_stddev = sqrt(var/(ipc.nr_cpus-1)); | |
182 | } | |
183 | ||
184 | void fio_idle_prof_init(void) | |
185 | { | |
186 | int i, ret; | |
f2a2ce0e | 187 | struct timespec ts; |
680b5abc | 188 | pthread_attr_t tattr; |
f2a2ce0e HL |
189 | struct idle_prof_thread *ipt; |
190 | ||
191 | ipc.nr_cpus = cpus_online(); | |
192 | ipc.status = IDLE_PROF_STATUS_OK; | |
193 | ||
194 | if (ipc.opt == IDLE_PROF_OPT_NONE) | |
195 | return; | |
196 | ||
197 | if ((ret = pthread_attr_init(&tattr))) { | |
198 | log_err("fio: pthread_attr_init %s\n", strerror(ret)); | |
199 | return; | |
200 | } | |
201 | if ((ret = pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM))) { | |
202 | log_err("fio: pthread_attr_setscope %s\n", strerror(ret)); | |
203 | return; | |
204 | } | |
205 | ||
206 | ipc.ipts = malloc(ipc.nr_cpus * sizeof(struct idle_prof_thread)); | |
207 | if (!ipc.ipts) { | |
208 | log_err("fio: malloc failed\n"); | |
209 | return; | |
210 | } | |
211 | ||
212 | ipc.buf = malloc(ipc.nr_cpus * page_size); | |
213 | if (!ipc.buf) { | |
214 | log_err("fio: malloc failed\n"); | |
215 | free(ipc.ipts); | |
216 | return; | |
217 | } | |
218 | ||
680b5abc JA |
219 | /* |
220 | * profiling aborts on any single thread failure since the | |
f2a2ce0e HL |
221 | * result won't be accurate if any cpu is not used. |
222 | */ | |
223 | for (i = 0; i < ipc.nr_cpus; i++) { | |
224 | ipt = &ipc.ipts[i]; | |
225 | ||
226 | ipt->cpu = i; | |
227 | ipt->state = TD_NOT_CREATED; | |
228 | ipt->data = (unsigned char *)(ipc.buf + page_size * i); | |
229 | ||
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)); | |
233 | break; | |
234 | } | |
235 | ||
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)); | |
239 | break; | |
240 | } | |
241 | ||
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)); | |
245 | break; | |
246 | } | |
247 | ||
248 | /* make sure all threads are spawned before they start */ | |
249 | pthread_mutex_lock(&ipt->init_lock); | |
250 | ||
251 | /* make sure all threads finish init before profiling starts */ | |
252 | pthread_mutex_lock(&ipt->start_lock); | |
253 | ||
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)); | |
257 | break; | |
680b5abc | 258 | } else |
f2a2ce0e | 259 | ipt->state = TD_CREATED; |
f2a2ce0e HL |
260 | |
261 | if ((ret = pthread_detach(ipt->thread))) { | |
262 | /* log error and let the thread spin */ | |
5bb39b7c | 263 | log_err("fio: pthread_detach %s\n", strerror(ret)); |
f2a2ce0e HL |
264 | } |
265 | } | |
266 | ||
680b5abc JA |
267 | /* |
268 | * let good threads continue so that they can exit | |
269 | * if errors on other threads occurred previously. | |
f2a2ce0e HL |
270 | */ |
271 | for (i = 0; i < ipc.nr_cpus; i++) { | |
272 | ipt = &ipc.ipts[i]; | |
273 | pthread_mutex_unlock(&ipt->init_lock); | |
274 | } | |
275 | ||
276 | if (ipc.status == IDLE_PROF_STATUS_ABORT) | |
277 | return; | |
278 | ||
279 | /* wait for calibration to finish */ | |
280 | for (i = 0; i < ipc.nr_cpus; i++) { | |
281 | ipt = &ipc.ipts[i]; | |
282 | pthread_mutex_lock(&ipt->init_lock); | |
680b5abc JA |
283 | while ((ipt->state != TD_EXITED) && |
284 | (ipt->state!=TD_INITIALIZED)) { | |
8b6a404c VF |
285 | fio_gettime(&ts, NULL); |
286 | ts.tv_sec += 1; | |
f2a2ce0e HL |
287 | pthread_cond_timedwait(&ipt->cond, &ipt->init_lock, &ts); |
288 | } | |
289 | pthread_mutex_unlock(&ipt->init_lock); | |
290 | ||
680b5abc JA |
291 | /* |
292 | * any thread failed to initialize would abort other threads | |
f2a2ce0e HL |
293 | * later after fio_idle_prof_start. |
294 | */ | |
295 | if (ipt->state == TD_EXITED) | |
296 | ipc.status = IDLE_PROF_STATUS_ABORT; | |
297 | } | |
298 | ||
299 | if (ipc.status != IDLE_PROF_STATUS_ABORT) | |
300 | calibration_stats(); | |
680b5abc | 301 | else |
f2a2ce0e HL |
302 | ipc.cali_mean = ipc.cali_stddev = 0.0; |
303 | ||
304 | if (ipc.opt == IDLE_PROF_OPT_CALI) | |
305 | ipc.status = IDLE_PROF_STATUS_CALI_STOP; | |
306 | } | |
307 | ||
308 | void fio_idle_prof_start(void) | |
309 | { | |
310 | int i; | |
311 | struct idle_prof_thread *ipt; | |
312 | ||
313 | if (ipc.opt == IDLE_PROF_OPT_NONE) | |
314 | return; | |
315 | ||
316 | /* unlock regardless abort is set or not */ | |
317 | for (i = 0; i < ipc.nr_cpus; i++) { | |
318 | ipt = &ipc.ipts[i]; | |
319 | pthread_mutex_unlock(&ipt->start_lock); | |
320 | } | |
321 | } | |
322 | ||
323 | void fio_idle_prof_stop(void) | |
324 | { | |
325 | int i; | |
326 | uint64_t runt; | |
f2a2ce0e HL |
327 | struct timespec ts; |
328 | struct idle_prof_thread *ipt; | |
329 | ||
330 | if (ipc.opt == IDLE_PROF_OPT_NONE) | |
331 | return; | |
332 | ||
333 | if (ipc.opt == IDLE_PROF_OPT_CALI) | |
334 | return; | |
335 | ||
336 | ipc.status = IDLE_PROF_STATUS_PROF_STOP; | |
337 | ||
338 | /* wait for all threads to exit from profiling */ | |
339 | for (i = 0; i < ipc.nr_cpus; i++) { | |
340 | ipt = &ipc.ipts[i]; | |
341 | pthread_mutex_lock(&ipt->start_lock); | |
680b5abc JA |
342 | while ((ipt->state != TD_EXITED) && |
343 | (ipt->state!=TD_NOT_CREATED)) { | |
8b6a404c VF |
344 | fio_gettime(&ts, NULL); |
345 | ts.tv_sec += 1; | |
f2a2ce0e HL |
346 | /* timed wait in case a signal is not received */ |
347 | pthread_cond_timedwait(&ipt->cond, &ipt->start_lock, &ts); | |
348 | } | |
349 | pthread_mutex_unlock(&ipt->start_lock); | |
350 | ||
351 | /* calculate idleness */ | |
352 | if (ipc.cali_mean != 0.0) { | |
353 | runt = utime_since(&ipt->tps, &ipt->tpe); | |
9da67e75 JA |
354 | if (runt) |
355 | ipt->idleness = ipt->loops * ipc.cali_mean / runt; | |
356 | else | |
357 | ipt->idleness = 0.0; | |
680b5abc | 358 | } else |
f2a2ce0e HL |
359 | ipt->idleness = 0.0; |
360 | } | |
361 | ||
680b5abc JA |
362 | /* |
363 | * memory allocations are freed via explicit fio_idle_prof_cleanup | |
f2a2ce0e HL |
364 | * after profiling stats are collected by apps. |
365 | */ | |
f2a2ce0e HL |
366 | } |
367 | ||
680b5abc JA |
368 | /* |
369 | * return system idle percentage when cpu is -1; | |
f2a2ce0e HL |
370 | * return one cpu idle percentage otherwise. |
371 | */ | |
372 | static double fio_idle_prof_cpu_stat(int cpu) | |
373 | { | |
374 | int i, nr_cpus = ipc.nr_cpus; | |
375 | struct idle_prof_thread *ipt; | |
376 | double p = 0.0; | |
377 | ||
378 | if (ipc.opt == IDLE_PROF_OPT_NONE) | |
379 | return 0.0; | |
380 | ||
381 | if ((cpu >= nr_cpus) || (cpu < -1)) { | |
382 | log_err("fio: idle profiling invalid cpu index\n"); | |
383 | return 0.0; | |
384 | } | |
385 | ||
386 | if (cpu == -1) { | |
387 | for (i = 0; i < nr_cpus; i++) { | |
388 | ipt = &ipc.ipts[i]; | |
389 | p += ipt->idleness; | |
390 | } | |
391 | p /= nr_cpus; | |
392 | } else { | |
393 | ipt = &ipc.ipts[cpu]; | |
394 | p = ipt->idleness; | |
395 | } | |
396 | ||
680b5abc | 397 | return p * 100.0; |
f2a2ce0e HL |
398 | } |
399 | ||
7ceefc09 | 400 | void fio_idle_prof_cleanup(void) |
f2a2ce0e HL |
401 | { |
402 | if (ipc.ipts) { | |
403 | free(ipc.ipts); | |
404 | ipc.ipts = NULL; | |
405 | } | |
406 | ||
407 | if (ipc.buf) { | |
408 | free(ipc.buf); | |
409 | ipc.buf = NULL; | |
410 | } | |
411 | } | |
412 | ||
413 | int fio_idle_prof_parse_opt(const char *args) | |
414 | { | |
415 | ipc.opt = IDLE_PROF_OPT_NONE; /* default */ | |
416 | ||
417 | if (!args) { | |
418 | log_err("fio: empty idle-prof option string\n"); | |
419 | return -1; | |
420 | } | |
421 | ||
7e09a9f1 | 422 | #if defined(FIO_HAVE_CPU_AFFINITY) && defined(CONFIG_SCHED_IDLE) |
f2a2ce0e HL |
423 | if (strcmp("calibrate", args) == 0) { |
424 | ipc.opt = IDLE_PROF_OPT_CALI; | |
425 | fio_idle_prof_init(); | |
426 | fio_idle_prof_start(); | |
427 | fio_idle_prof_stop(); | |
a666cab8 | 428 | show_idle_prof_stats(FIO_OUTPUT_NORMAL, NULL, NULL); |
f2a2ce0e HL |
429 | return 1; |
430 | } else if (strcmp("system", args) == 0) { | |
431 | ipc.opt = IDLE_PROF_OPT_SYSTEM; | |
432 | return 0; | |
433 | } else if (strcmp("percpu", args) == 0) { | |
434 | ipc.opt = IDLE_PROF_OPT_PERCPU; | |
435 | return 0; | |
436 | } else { | |
4e0a8fa2 | 437 | log_err("fio: incorrect idle-prof option: %s\n", args); |
f2a2ce0e HL |
438 | return -1; |
439 | } | |
440 | #else | |
441 | log_err("fio: idle-prof not supported on this platform\n"); | |
442 | return -1; | |
443 | #endif | |
444 | } | |
445 | ||
a666cab8 JA |
446 | void show_idle_prof_stats(int output, struct json_object *parent, |
447 | struct buf_output *out) | |
f2a2ce0e HL |
448 | { |
449 | int i, nr_cpus = ipc.nr_cpus; | |
450 | struct json_object *tmp; | |
451 | char s[MAX_CPU_STR_LEN]; | |
680b5abc | 452 | |
f2a2ce0e HL |
453 | if (output == FIO_OUTPUT_NORMAL) { |
454 | if (ipc.opt > IDLE_PROF_OPT_CALI) | |
a666cab8 | 455 | log_buf(out, "\nCPU idleness:\n"); |
f2a2ce0e | 456 | else if (ipc.opt == IDLE_PROF_OPT_CALI) |
a666cab8 | 457 | log_buf(out, "CPU idleness:\n"); |
f2a2ce0e HL |
458 | |
459 | if (ipc.opt >= IDLE_PROF_OPT_SYSTEM) | |
a666cab8 | 460 | log_buf(out, " system: %3.2f%%\n", fio_idle_prof_cpu_stat(-1)); |
f2a2ce0e HL |
461 | |
462 | if (ipc.opt == IDLE_PROF_OPT_PERCPU) { | |
a666cab8 | 463 | log_buf(out, " percpu: %3.2f%%", fio_idle_prof_cpu_stat(0)); |
680b5abc | 464 | for (i = 1; i < nr_cpus; i++) |
a666cab8 JA |
465 | log_buf(out, ", %3.2f%%", fio_idle_prof_cpu_stat(i)); |
466 | log_buf(out, "\n"); | |
f2a2ce0e HL |
467 | } |
468 | ||
469 | if (ipc.opt >= IDLE_PROF_OPT_CALI) { | |
a666cab8 JA |
470 | log_buf(out, " unit work: mean=%3.2fus,", ipc.cali_mean); |
471 | log_buf(out, " stddev=%3.2f\n", ipc.cali_stddev); | |
f2a2ce0e HL |
472 | } |
473 | ||
f2a2ce0e HL |
474 | return; |
475 | } | |
680b5abc | 476 | |
236d24df | 477 | if ((ipc.opt != IDLE_PROF_OPT_NONE) && (output & FIO_OUTPUT_JSON)) { |
f2a2ce0e HL |
478 | if (!parent) |
479 | return; | |
480 | ||
481 | tmp = json_create_object(); | |
482 | if (!tmp) | |
483 | return; | |
484 | ||
485 | json_object_add_value_object(parent, "cpu_idleness", tmp); | |
486 | json_object_add_value_float(tmp, "system", fio_idle_prof_cpu_stat(-1)); | |
487 | ||
488 | if (ipc.opt == IDLE_PROF_OPT_PERCPU) { | |
680b5abc | 489 | for (i = 0; i < nr_cpus; i++) { |
f2a2ce0e HL |
490 | snprintf(s, MAX_CPU_STR_LEN, "cpu-%d", i); |
491 | json_object_add_value_float(tmp, s, fio_idle_prof_cpu_stat(i)); | |
492 | } | |
493 | } | |
494 | ||
495 | json_object_add_value_float(tmp, "unit_mean", ipc.cali_mean); | |
496 | json_object_add_value_float(tmp, "unit_stddev", ipc.cali_stddev); | |
f2a2ce0e HL |
497 | } |
498 | } |