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