Merge branch 'For_Each_Td_Private_Scope' of https://github.com/horshack-dpreview/fio
[fio.git] / libfio.c
1 /*
2  * fio - the flexible io tester
3  *
4  * Copyright (C) 2005 Jens Axboe <axboe@suse.de>
5  * Copyright (C) 2006-2012 Jens Axboe <axboe@kernel.dk>
6  *
7  * The license below covers all files distributed with fio unless otherwise
8  * noted in the file itself.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License version 2 as
12  *  published by the Free Software Foundation.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  */
24
25 #include <string.h>
26 #include <signal.h>
27 #include <stdint.h>
28 #include <locale.h>
29 #include <fcntl.h>
30
31 #include "fio.h"
32 #include "smalloc.h"
33 #include "os/os.h"
34 #include "filelock.h"
35 #include "helper_thread.h"
36 #include "filehash.h"
37
38 FLIST_HEAD(disk_list);
39
40 unsigned long arch_flags = 0;
41
42 uintptr_t page_mask = 0;
43 uintptr_t page_size = 0;
44
45 /* see os/os.h */
46 static const char *fio_os_strings[os_nr] = {
47         "Invalid",
48         "Linux",
49         "AIX",
50         "FreeBSD",
51         "HP-UX",
52         "OSX",
53         "NetBSD",
54         "OpenBSD",
55         "Solaris",
56         "Windows",
57         "Android",
58         "DragonFly",
59 };
60
61 /* see arch/arch.h */
62 static const char *fio_arch_strings[arch_nr] = {
63         "Invalid",
64         "x86-64",
65         "x86",
66         "ppc",
67         "ia64",
68         "s390",
69         "alpha",
70         "sparc",
71         "sparc64",
72         "arm",
73         "sh",
74         "hppa",
75         "mips",
76         "aarch64",
77         "generic"
78 };
79
80 static void reset_io_counters(struct thread_data *td, int all)
81 {
82         int ddir;
83
84         if (all) {
85                 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
86                         td->stat_io_bytes[ddir] = 0;
87                         td->this_io_bytes[ddir] = 0;
88                         td->stat_io_blocks[ddir] = 0;
89                         td->this_io_blocks[ddir] = 0;
90                         td->last_rate_check_bytes[ddir] = 0;
91                         td->last_rate_check_blocks[ddir] = 0;
92                         td->bytes_done[ddir] = 0;
93                         td->rate_io_issue_bytes[ddir] = 0;
94                         td->rate_next_io_time[ddir] = 0;
95                         td->last_usec[ddir] = 0;
96                 }
97                 td->bytes_verified = 0;
98         }
99
100         td->zone_bytes = 0;
101
102         td->last_was_sync = false;
103         td->rwmix_issues = 0;
104
105         /*
106          * reset file done count if we are to start over
107          */
108         if (td->o.time_based || td->loops > 1 || td->o.do_verify)
109                 td->nr_done_files = 0;
110 }
111
112 void clear_io_state(struct thread_data *td, int all)
113 {
114         struct fio_file *f;
115         unsigned int i;
116
117         reset_io_counters(td, all);
118
119         close_files(td);
120         for_each_file(td, f, i) {
121                 fio_file_clear_done(f);
122                 f->file_offset = get_start_offset(td, f);
123         }
124
125         /*
126          * Re-Seed random number generator if rand_repeatable is true
127          */
128         if (td->o.rand_repeatable)
129                 td_fill_rand_seeds(td);
130 }
131
132 void reset_all_stats(struct thread_data *td)
133 {
134         int i;
135
136         reset_io_counters(td, 1);
137
138         for (i = 0; i < DDIR_RWDIR_CNT; i++) {
139                 td->io_bytes[i] = 0;
140                 td->io_blocks[i] = 0;
141                 td->io_issues[i] = 0;
142                 td->ts.total_io_u[i] = 0;
143                 td->ts.runtime[i] = 0;
144         }
145
146         set_epoch_time(td, td->o.log_unix_epoch | td->o.log_alternate_epoch, td->o.log_alternate_epoch_clock_id);
147         memcpy(&td->start, &td->epoch, sizeof(td->epoch));
148         memcpy(&td->iops_sample_time, &td->epoch, sizeof(td->epoch));
149         memcpy(&td->bw_sample_time, &td->epoch, sizeof(td->epoch));
150         memcpy(&td->ss.prev_time, &td->epoch, sizeof(td->epoch));
151
152         lat_target_reset(td);
153         clear_rusage_stat(td);
154         helper_reset();
155 }
156
157 void reset_fio_state(void)
158 {
159         int i;
160
161         groupid = 0;
162         thread_number = 0;
163         cur_segment = 0;
164         for (i = 0; i < nr_segments; i++)
165                 segments[i].nr_threads = 0;
166         stat_number = 0;
167         done_secs = 0;
168 }
169
170 const char *fio_get_os_string(int nr)
171 {
172         if (nr < os_nr)
173                 return fio_os_strings[nr];
174
175         return NULL;
176 }
177
178 const char *fio_get_arch_string(int nr)
179 {
180         if (nr < arch_nr)
181                 return fio_arch_strings[nr];
182
183         return NULL;
184 }
185
186 static const char *td_runstates[] = {
187         "NOT_CREATED",
188         "CREATED",
189         "INITIALIZED",
190         "RAMP",
191         "SETTING_UP",
192         "RUNNING",
193         "PRE_READING",
194         "VERIFYING",
195         "FSYNCING",
196         "FINISHING",
197         "EXITED",
198         "REAPED",
199 };
200
201 const char *runstate_to_name(int runstate)
202 {
203         compiletime_assert(TD_LAST == 12, "td runstate list");
204         if (runstate >= 0 && runstate < TD_LAST)
205                 return td_runstates[runstate];
206
207         return "invalid";
208 }
209
210 void td_set_runstate(struct thread_data *td, int runstate)
211 {
212         if (td->runstate == runstate)
213                 return;
214
215         dprint(FD_PROCESS, "pid=%d: runstate %s -> %s\n", (int) td->pid,
216                                                 runstate_to_name(td->runstate),
217                                                 runstate_to_name(runstate));
218         td->runstate = runstate;
219 }
220
221 int td_bump_runstate(struct thread_data *td, int new_state)
222 {
223         int old_state = td->runstate;
224
225         td_set_runstate(td, new_state);
226         return old_state;
227 }
228
229 void td_restore_runstate(struct thread_data *td, int old_state)
230 {
231         td_set_runstate(td, old_state);
232 }
233
234 void fio_mark_td_terminate(struct thread_data *td)
235 {
236         fio_gettime(&td->terminate_time, NULL);
237         write_barrier();
238         td->terminate = true;
239 }
240
241 void fio_terminate_threads(unsigned int group_id, unsigned int terminate)
242 {
243         pid_t pid = getpid();
244
245         dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
246
247         for_each_td(td) {
248                 if ((terminate == TERMINATE_GROUP && group_id == TERMINATE_ALL) ||
249                     (terminate == TERMINATE_GROUP && group_id == td->groupid) ||
250                     (terminate == TERMINATE_STONEWALL && td->runstate >= TD_RUNNING) ||
251                     (terminate == TERMINATE_ALL)) {
252                         dprint(FD_PROCESS, "setting terminate on %s/%d\n",
253                                                 td->o.name, (int) td->pid);
254
255                         if (td->terminate)
256                                 continue;
257
258                         fio_mark_td_terminate(td);
259                         td->o.start_delay = 0;
260
261                         /*
262                          * if the thread is running, just let it exit
263                          */
264                         if (!td->pid || pid == td->pid)
265                                 continue;
266                         else if (td->runstate < TD_RAMP)
267                                 kill(td->pid, SIGTERM);
268                         else {
269                                 struct ioengine_ops *ops = td->io_ops;
270
271                                 if (ops && ops->terminate)
272                                         ops->terminate(td);
273                         }
274                 }
275         } end_for_each();
276 }
277
278 int fio_running_or_pending_io_threads(void)
279 {
280         int nr_io_threads = 0;
281
282         for_each_td(td) {
283                 if (td->io_ops_init && td_ioengine_flagged(td, FIO_NOIO))
284                         continue;
285                 nr_io_threads++;
286                 if (td->runstate < TD_EXITED)
287                         return 1;
288         } end_for_each();
289
290         if (!nr_io_threads)
291                 return -1; /* we only had cpuio threads to begin with */
292         return 0;
293 }
294
295 int fio_set_fd_nonblocking(int fd, const char *who)
296 {
297         int flags;
298
299         flags = fcntl(fd, F_GETFL);
300         if (flags < 0)
301                 log_err("fio: %s failed to get file flags: %s\n", who, strerror(errno));
302         else {
303                 int new_flags = flags | O_NONBLOCK;
304
305                 new_flags = fcntl(fd, F_SETFL, new_flags);
306                 if (new_flags < 0)
307                         log_err("fio: %s failed to get file flags: %s\n", who, strerror(errno));
308         }
309
310         return flags;
311 }
312
313 enum {
314         ENDIAN_INVALID_BE = 1,
315         ENDIAN_INVALID_LE,
316         ENDIAN_INVALID_CONFIG,
317         ENDIAN_BROKEN,
318 };
319
320 static int endian_check(void)
321 {
322         union {
323                 uint8_t c[8];
324                 uint64_t v;
325         } u;
326         int le = 0, be = 0;
327
328         u.v = 0x12;
329         if (u.c[7] == 0x12)
330                 be = 1;
331         else if (u.c[0] == 0x12)
332                 le = 1;
333
334 #if defined(CONFIG_LITTLE_ENDIAN)
335         if (be)
336                 return ENDIAN_INVALID_BE;
337 #elif defined(CONFIG_BIG_ENDIAN)
338         if (le)
339                 return ENDIAN_INVALID_LE;
340 #else
341         return ENDIAN_INVALID_CONFIG;
342 #endif
343
344         if (!le && !be)
345                 return ENDIAN_BROKEN;
346
347         return 0;
348 }
349
350 int initialize_fio(char *envp[])
351 {
352         long ps;
353         int err;
354
355         /*
356          * We need these to be properly 64-bit aligned, otherwise we
357          * can run into problems on archs that fault on unaligned fp
358          * access (ARM).
359          */
360         compiletime_assert((offsetof(struct thread_data, ts) % sizeof(void *)) == 0, "ts");
361         compiletime_assert((offsetof(struct thread_stat, percentile_list) % 8) == 0, "stat percentile_list");
362         compiletime_assert((offsetof(struct thread_stat, total_run_time) % 8) == 0, "total_run_time");
363         compiletime_assert((offsetof(struct thread_stat, total_err_count) % 8) == 0, "total_err_count");
364         compiletime_assert((offsetof(struct thread_stat, latency_percentile) % 8) == 0, "stat latency_percentile");
365         compiletime_assert((offsetof(struct thread_data, ts.clat_stat) % 8) == 0, "ts.clat_stat");
366         compiletime_assert((offsetof(struct thread_options_pack, zipf_theta) % 8) == 0, "zipf_theta");
367         compiletime_assert((offsetof(struct thread_options_pack, pareto_h) % 8) == 0, "pareto_h");
368         compiletime_assert((offsetof(struct thread_options_pack, percentile_list) % 8) == 0, "percentile_list");
369         compiletime_assert((offsetof(struct thread_options_pack, latency_percentile) % 8) == 0, "latency_percentile");
370         compiletime_assert((offsetof(struct jobs_eta, m_rate) % 8) == 0, "m_rate");
371
372         compiletime_assert(__TD_F_LAST <= TD_ENG_FLAG_SHIFT, "TD_ENG_FLAG_SHIFT");
373         compiletime_assert(BSSPLIT_MAX <= ZONESPLIT_MAX, "bsssplit/zone max");
374
375         err = endian_check();
376         if (err) {
377                 log_err("fio: endianness settings appear wrong.\n");
378                 switch (err) {
379                 case ENDIAN_INVALID_BE:
380                         log_err("fio: got big-endian when configured for little\n");
381                         break;
382                 case ENDIAN_INVALID_LE:
383                         log_err("fio: got little-endian when configured for big\n");
384                         break;
385                 case ENDIAN_INVALID_CONFIG:
386                         log_err("fio: not configured to any endianness\n");
387                         break;
388                 case ENDIAN_BROKEN:
389                         log_err("fio: failed to detect endianness\n");
390                         break;
391                 default:
392                         assert(0);
393                         break;
394                 }
395                 log_err("fio: please report this to fio@vger.kernel.org\n");
396                 return 1;
397         }
398
399 #if !defined(CONFIG_GETTIMEOFDAY) && !defined(CONFIG_CLOCK_GETTIME)
400 #error "No available clock source!"
401 #endif
402
403         arch_init(envp);
404
405         sinit();
406
407         if (fio_filelock_init()) {
408                 log_err("fio: failed initializing filelock subsys\n");
409                 return 1;
410         }
411
412         file_hash_init();
413
414         /*
415          * We need locale for number printing, if it isn't set then just
416          * go with the US format.
417          */
418         if (!getenv("LC_NUMERIC"))
419                 setlocale(LC_NUMERIC, "en_US");
420
421         ps = sysconf(_SC_PAGESIZE);
422         if (ps < 0) {
423                 log_err("Failed to get page size\n");
424                 return 1;
425         }
426
427         page_size = ps;
428         page_mask = ps - 1;
429
430         fio_keywords_init();
431         return 0;
432 }
433
434 void deinitialize_fio(void)
435 {
436         fio_keywords_exit();
437 }