1 #ifdef CONFIG_VALGRIND_DEV
2 #include <valgrind/drd.h>
4 #define DRD_IGNORE_VAR(x) do { } while (0)
9 #include "helper_thread.h"
10 #include "steadystate.h"
13 static struct helper_data {
17 struct sk_out *sk_out;
21 struct fio_sem *startup_sem;
24 void helper_thread_destroy(void)
26 pthread_cond_destroy(&helper_data->cond);
27 pthread_mutex_destroy(&helper_data->lock);
31 void helper_reset(void)
36 pthread_mutex_lock(&helper_data->lock);
38 if (!helper_data->reset) {
39 helper_data->reset = 1;
40 pthread_cond_signal(&helper_data->cond);
43 pthread_mutex_unlock(&helper_data->lock);
46 void helper_do_stat(void)
51 pthread_mutex_lock(&helper_data->lock);
52 helper_data->do_stat = 1;
53 pthread_cond_signal(&helper_data->cond);
54 pthread_mutex_unlock(&helper_data->lock);
57 bool helper_should_exit(void)
62 return helper_data->exit;
65 void helper_thread_exit(void)
69 pthread_mutex_lock(&helper_data->lock);
70 helper_data->exit = 1;
71 pthread_cond_signal(&helper_data->cond);
72 pthread_mutex_unlock(&helper_data->lock);
74 pthread_join(helper_data->thread, &ret);
77 static void *helper_thread_main(void *data)
79 struct helper_data *hd = data;
80 unsigned int msec_to_next_event, next_log, next_ss = STEADYSTATE_MSEC;
81 struct timespec ts, last_du, last_ss;
84 sk_out_assign(hd->sk_out);
86 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
87 clock_gettime(CLOCK_MONOTONIC, &ts);
89 clock_gettime(CLOCK_REALTIME, &ts);
91 memcpy(&last_du, &ts, sizeof(ts));
92 memcpy(&last_ss, &ts, sizeof(ts));
94 fio_sem_up(hd->startup_sem);
96 msec_to_next_event = DISK_UTIL_MSEC;
97 while (!ret && !hd->exit) {
98 uint64_t since_du, since_ss = 0;
100 timespec_add_msec(&ts, msec_to_next_event);
102 pthread_mutex_lock(&hd->lock);
103 pthread_cond_timedwait(&hd->cond, &hd->lock, &ts);
105 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
106 clock_gettime(CLOCK_MONOTONIC, &ts);
108 clock_gettime(CLOCK_REALTIME, &ts);
112 memcpy(&last_du, &ts, sizeof(ts));
113 memcpy(&last_ss, &ts, sizeof(ts));
117 pthread_mutex_unlock(&hd->lock);
119 since_du = mtime_since(&last_du, &ts);
120 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
121 ret = update_io_ticks();
122 timespec_add_msec(&last_du, DISK_UTIL_MSEC);
123 msec_to_next_event = DISK_UTIL_MSEC;
124 if (since_du >= DISK_UTIL_MSEC)
125 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
127 msec_to_next_event = DISK_UTIL_MSEC - since_du;
131 __show_running_run_stats();
134 next_log = calc_log_samples();
136 next_log = DISK_UTIL_MSEC;
138 if (steadystate_enabled) {
139 since_ss = mtime_since(&last_ss, &ts);
140 if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
142 timespec_add_msec(&last_ss, since_ss);
143 if (since_ss > STEADYSTATE_MSEC)
144 next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
146 next_ss = STEADYSTATE_MSEC;
148 next_ss = STEADYSTATE_MSEC - since_ss;
151 msec_to_next_event = min(min(next_log, msec_to_next_event), next_ss);
152 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);
155 print_thread_status();
158 fio_writeout_logs(false);
164 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
166 struct helper_data *hd;
169 hd = scalloc(1, sizeof(*hd));
176 ret = mutex_cond_init_pshared(&hd->lock, &hd->cond);
180 hd->startup_sem = startup_sem;
182 DRD_IGNORE_VAR(helper_data);
184 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
186 log_err("Can't create helper thread: %s\n", strerror(ret));
192 dprint(FD_MUTEX, "wait on startup_sem\n");
193 fio_sem_down(startup_sem);
194 dprint(FD_MUTEX, "done waiting on startup_sem\n");