3 #include "helper_thread.h"
5 static struct helper_data {
13 struct fio_mutex *startup_mutex;
16 void helper_thread_destroy(void)
18 pthread_cond_destroy(&helper_data->cond);
19 pthread_mutex_destroy(&helper_data->lock);
23 void helper_reset(void)
28 pthread_mutex_lock(&helper_data->lock);
30 if (!helper_data->reset) {
31 helper_data->reset = 1;
32 pthread_cond_signal(&helper_data->cond);
35 pthread_mutex_unlock(&helper_data->lock);
38 void helper_do_stat(void)
43 pthread_mutex_lock(&helper_data->lock);
44 helper_data->do_stat = 1;
45 pthread_cond_signal(&helper_data->cond);
46 pthread_mutex_unlock(&helper_data->lock);
49 bool helper_should_exit(void)
54 return helper_data->exit;
57 void helper_thread_exit(void)
61 pthread_mutex_lock(&helper_data->lock);
62 helper_data->exit = 1;
63 pthread_cond_signal(&helper_data->cond);
64 pthread_mutex_unlock(&helper_data->lock);
66 pthread_join(helper_data->thread, &ret);
69 static void *helper_thread_main(void *data)
71 struct helper_data *hd = data;
72 unsigned int msec_to_next_event, next_log;
73 struct timeval tv, last_du;
76 sk_out_assign(hd->sk_out);
78 gettimeofday(&tv, NULL);
79 memcpy(&last_du, &tv, sizeof(tv));
81 fio_mutex_up(hd->startup_mutex);
83 msec_to_next_event = DISK_UTIL_MSEC;
84 while (!ret && !hd->exit) {
89 timeval_add_msec(&tv, msec_to_next_event);
90 ts.tv_sec = tv.tv_sec;
91 ts.tv_nsec = tv.tv_usec * 1000;
93 pthread_mutex_lock(&hd->lock);
94 pthread_cond_timedwait(&hd->cond, &hd->lock, &ts);
96 gettimeofday(&now, NULL);
99 memcpy(&tv, &now, sizeof(tv));
100 memcpy(&last_du, &now, sizeof(last_du));
104 pthread_mutex_unlock(&hd->lock);
106 since_du = mtime_since(&last_du, &now);
107 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
108 ret = update_io_ticks();
109 timeval_add_msec(&last_du, DISK_UTIL_MSEC);
110 msec_to_next_event = DISK_UTIL_MSEC;
111 if (since_du >= DISK_UTIL_MSEC)
112 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
114 msec_to_next_event = DISK_UTIL_MSEC - since_du;
118 __show_running_run_stats();
121 next_log = calc_log_samples();
123 next_log = DISK_UTIL_MSEC;
125 msec_to_next_event = min(next_log, msec_to_next_event);
128 print_thread_status();
131 fio_writeout_logs(false);
137 int helper_thread_create(struct fio_mutex *startup_mutex, struct sk_out *sk_out)
139 struct helper_data *hd;
142 hd = smalloc(sizeof(*hd));
148 ret = mutex_cond_init_pshared(&hd->lock, &hd->cond);
152 hd->startup_mutex = startup_mutex;
154 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
156 log_err("Can't create helper thread: %s\n", strerror(ret));
162 dprint(FD_MUTEX, "wait on startup_mutex\n");
163 fio_mutex_down(startup_mutex);
164 dprint(FD_MUTEX, "done waiting on startup_mutex\n");