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