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)
29 pthread_cond_destroy(&helper_data->cond);
30 pthread_mutex_destroy(&helper_data->lock);
34 void helper_reset(void)
39 pthread_mutex_lock(&helper_data->lock);
41 if (!helper_data->reset) {
42 helper_data->reset = 1;
43 pthread_cond_signal(&helper_data->cond);
46 pthread_mutex_unlock(&helper_data->lock);
49 void helper_do_stat(void)
54 pthread_mutex_lock(&helper_data->lock);
55 helper_data->do_stat = 1;
56 pthread_cond_signal(&helper_data->cond);
57 pthread_mutex_unlock(&helper_data->lock);
60 bool helper_should_exit(void)
65 return helper_data->exit;
68 void helper_thread_exit(void)
75 pthread_mutex_lock(&helper_data->lock);
76 helper_data->exit = 1;
77 pthread_cond_signal(&helper_data->cond);
78 pthread_mutex_unlock(&helper_data->lock);
80 pthread_join(helper_data->thread, &ret);
83 static void *helper_thread_main(void *data)
85 struct helper_data *hd = data;
86 unsigned int msec_to_next_event, next_log, next_ss = STEADYSTATE_MSEC;
87 struct timespec ts, last_du, last_ss;
90 sk_out_assign(hd->sk_out);
92 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
93 clock_gettime(CLOCK_MONOTONIC, &ts);
95 clock_gettime(CLOCK_REALTIME, &ts);
97 memcpy(&last_du, &ts, sizeof(ts));
98 memcpy(&last_ss, &ts, sizeof(ts));
100 fio_sem_up(hd->startup_sem);
102 msec_to_next_event = DISK_UTIL_MSEC;
103 while (!ret && !hd->exit) {
104 uint64_t since_du, since_ss = 0;
106 timespec_add_msec(&ts, msec_to_next_event);
108 pthread_mutex_lock(&hd->lock);
109 pthread_cond_timedwait(&hd->cond, &hd->lock, &ts);
111 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
112 clock_gettime(CLOCK_MONOTONIC, &ts);
114 clock_gettime(CLOCK_REALTIME, &ts);
118 memcpy(&last_du, &ts, sizeof(ts));
119 memcpy(&last_ss, &ts, sizeof(ts));
123 pthread_mutex_unlock(&hd->lock);
125 since_du = mtime_since(&last_du, &ts);
126 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
127 ret = update_io_ticks();
128 timespec_add_msec(&last_du, DISK_UTIL_MSEC);
129 msec_to_next_event = DISK_UTIL_MSEC;
130 if (since_du >= DISK_UTIL_MSEC)
131 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
133 msec_to_next_event = DISK_UTIL_MSEC - since_du;
137 __show_running_run_stats();
140 next_log = calc_log_samples();
142 next_log = DISK_UTIL_MSEC;
144 if (steadystate_enabled) {
145 since_ss = mtime_since(&last_ss, &ts);
146 if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
148 timespec_add_msec(&last_ss, since_ss);
149 if (since_ss > STEADYSTATE_MSEC)
150 next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
152 next_ss = STEADYSTATE_MSEC;
154 next_ss = STEADYSTATE_MSEC - since_ss;
157 msec_to_next_event = min(min(next_log, msec_to_next_event), next_ss);
158 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);
161 print_thread_status();
164 fio_writeout_logs(false);
170 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
172 struct helper_data *hd;
175 hd = scalloc(1, sizeof(*hd));
182 ret = mutex_cond_init_pshared(&hd->lock, &hd->cond);
186 hd->startup_sem = startup_sem;
188 DRD_IGNORE_VAR(helper_data);
190 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
192 log_err("Can't create helper thread: %s\n", strerror(ret));
198 dprint(FD_MUTEX, "wait on startup_sem\n");
199 fio_sem_down(startup_sem);
200 dprint(FD_MUTEX, "done waiting on startup_sem\n");