3 #include "helper_thread.h"
4 #include "steadystate.h"
6 static struct helper_data {
10 struct sk_out *sk_out;
14 struct fio_mutex *startup_mutex;
17 void helper_thread_destroy(void)
19 pthread_cond_destroy(&helper_data->cond);
20 pthread_mutex_destroy(&helper_data->lock);
24 void helper_reset(void)
29 pthread_mutex_lock(&helper_data->lock);
31 if (!helper_data->reset) {
32 helper_data->reset = 1;
33 pthread_cond_signal(&helper_data->cond);
36 pthread_mutex_unlock(&helper_data->lock);
39 void helper_do_stat(void)
44 pthread_mutex_lock(&helper_data->lock);
45 helper_data->do_stat = 1;
46 pthread_cond_signal(&helper_data->cond);
47 pthread_mutex_unlock(&helper_data->lock);
50 bool helper_should_exit(void)
55 return helper_data->exit;
58 void helper_thread_exit(void)
62 pthread_mutex_lock(&helper_data->lock);
63 helper_data->exit = 1;
64 pthread_cond_signal(&helper_data->cond);
65 pthread_mutex_unlock(&helper_data->lock);
67 pthread_join(helper_data->thread, &ret);
70 static void *helper_thread_main(void *data)
72 struct helper_data *hd = data;
73 unsigned int msec_to_next_event, next_log, next_ss = STEADYSTATE_MSEC;
75 struct timespec ts, last_du, last_ss;
78 sk_out_assign(hd->sk_out);
80 gettimeofday(&tv, NULL);
81 ts.tv_sec = tv.tv_sec;
82 ts.tv_nsec = tv.tv_usec * 1000;
83 memcpy(&last_du, &ts, sizeof(ts));
84 memcpy(&last_ss, &ts, sizeof(ts));
86 fio_mutex_up(hd->startup_mutex);
88 msec_to_next_event = DISK_UTIL_MSEC;
89 while (!ret && !hd->exit) {
90 uint64_t since_du, since_ss = 0;
92 timespec_add_msec(&ts, msec_to_next_event);
94 pthread_mutex_lock(&hd->lock);
95 pthread_cond_timedwait(&hd->cond, &hd->lock, &ts);
97 gettimeofday(&tv, NULL);
98 ts.tv_sec = tv.tv_sec;
99 ts.tv_nsec = tv.tv_usec * 1000;
102 memcpy(&last_du, &ts, sizeof(ts));
103 memcpy(&last_ss, &ts, sizeof(ts));
107 pthread_mutex_unlock(&hd->lock);
109 since_du = mtime_since(&last_du, &ts);
110 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
111 ret = update_io_ticks();
112 timespec_add_msec(&last_du, DISK_UTIL_MSEC);
113 msec_to_next_event = DISK_UTIL_MSEC;
114 if (since_du >= DISK_UTIL_MSEC)
115 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
117 msec_to_next_event = DISK_UTIL_MSEC - since_du;
121 __show_running_run_stats();
124 next_log = calc_log_samples();
126 next_log = DISK_UTIL_MSEC;
128 if (steadystate_enabled) {
129 since_ss = mtime_since(&last_ss, &ts);
130 if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
132 timespec_add_msec(&last_ss, since_ss);
133 if (since_ss > STEADYSTATE_MSEC)
134 next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
136 next_ss = STEADYSTATE_MSEC;
138 next_ss = STEADYSTATE_MSEC - since_ss;
141 msec_to_next_event = min(min(next_log, msec_to_next_event), next_ss);
142 dprint(FD_HELPERTHREAD, "since_ss: %llu, next_ss: %u, next_log: %u, msec_to_next_event: %u\n", (unsigned long long)since_ss, next_ss, next_log, msec_to_next_event);
145 print_thread_status();
148 fio_writeout_logs(false);
154 int helper_thread_create(struct fio_mutex *startup_mutex, struct sk_out *sk_out)
156 struct helper_data *hd;
159 hd = smalloc(sizeof(*hd));
166 ret = mutex_cond_init_pshared(&hd->lock, &hd->cond);
170 hd->startup_mutex = startup_mutex;
172 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
174 log_err("Can't create helper thread: %s\n", strerror(ret));
180 dprint(FD_MUTEX, "wait on startup_mutex\n");
181 fio_mutex_down(startup_mutex);
182 dprint(FD_MUTEX, "done waiting on startup_mutex\n");