Merge branch 'master' of ssh://git.kernel.dk/data/git/fio
[fio.git] / libfio.c
CommitLineData
2e1df07d
JA
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
25#include <string.h>
59dfce57
JA
26#include <sys/types.h>
27#include <signal.h>
a569b45f
JA
28#include <stdint.h>
29#include <locale.h>
30
2e1df07d 31#include "fio.h"
a569b45f
JA
32#include "smalloc.h"
33#include "os/os.h"
243bfe19 34#include "filelock.h"
2e1df07d 35
a3efc919
JA
36/*
37 * Just expose an empty list, if the OS does not support disk util stats
38 */
39#ifndef FIO_HAVE_DISK_UTIL
40FLIST_HEAD(disk_list);
41#endif
42
43unsigned long arch_flags = 0;
44
a569b45f
JA
45uintptr_t page_mask = 0;
46uintptr_t page_size = 0;
47
2e1df07d
JA
48static const char *fio_os_strings[os_nr] = {
49 "Invalid",
50 "Linux",
51 "AIX",
52 "FreeBSD",
53 "HP-UX",
54 "OSX",
55 "NetBSD",
1c7eaa7b 56 "OpenBSD",
2e1df07d 57 "Solaris",
1c7eaa7b
JA
58 "Windows",
59 "Android",
2e1df07d
JA
60};
61
62static 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 "generic"
76};
77
78static void reset_io_counters(struct thread_data *td)
79{
6eaf09d6 80 int ddir;
bcd5abfa 81
6eaf09d6
SL
82 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
83 td->stat_io_bytes[ddir] = 0;
84 td->this_io_bytes[ddir] = 0;
85 td->stat_io_blocks[ddir] = 0;
86 td->this_io_blocks[ddir] = 0;
87 td->rate_bytes[ddir] = 0;
88 td->rate_blocks[ddir] = 0;
89 }
2e1df07d 90 td->zone_bytes = 0;
2e1df07d
JA
91
92 td->last_was_sync = 0;
bcd5abfa 93 td->rwmix_issues = 0;
2e1df07d
JA
94
95 /*
96 * reset file done count if we are to start over
97 */
44cbc6da 98 if (td->o.time_based || td->o.loops || td->o.do_verify)
2e1df07d
JA
99 td->nr_done_files = 0;
100}
101
102void clear_io_state(struct thread_data *td)
103{
104 struct fio_file *f;
105 unsigned int i;
106
107 reset_io_counters(td);
108
109 close_files(td);
110 for_each_file(td, f, i)
111 fio_file_clear_done(f);
112
113 /*
114 * Set the same seed to get repeatable runs
115 */
116 td_fill_rand_seeds(td);
117}
118
119void reset_all_stats(struct thread_data *td)
120{
121 struct timeval tv;
122 int i;
123
124 reset_io_counters(td);
125
6eaf09d6 126 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
2e1df07d
JA
127 td->io_bytes[i] = 0;
128 td->io_blocks[i] = 0;
129 td->io_issues[i] = 0;
130 td->ts.total_io_u[i] = 0;
6eaf09d6 131 td->ts.runtime[i] = 0;
5b3faae6 132 td->rwmix_issues = 0;
2e1df07d
JA
133 }
134
135 fio_gettime(&tv, NULL);
2e1df07d
JA
136 memcpy(&td->epoch, &tv, sizeof(tv));
137 memcpy(&td->start, &tv, sizeof(tv));
3e260a46 138
6bb58215 139 lat_target_reset(td);
2e1df07d
JA
140}
141
142void reset_fio_state(void)
143{
144 groupid = 0;
145 thread_number = 0;
2caefeec 146 stat_number = 0;
2e1df07d
JA
147 done_secs = 0;
148}
149
150const char *fio_get_os_string(int nr)
151{
152 if (nr < os_nr)
153 return fio_os_strings[nr];
154
155 return NULL;
156}
157
158const char *fio_get_arch_string(int nr)
159{
160 if (nr < arch_nr)
161 return fio_arch_strings[nr];
162
163 return NULL;
164}
165
166void td_set_runstate(struct thread_data *td, int runstate)
167{
168 if (td->runstate == runstate)
169 return;
170
171 dprint(FD_PROCESS, "pid=%d: runstate %d -> %d\n", (int) td->pid,
172 td->runstate, runstate);
173 td->runstate = runstate;
174}
175
8edd973d
JA
176int td_bump_runstate(struct thread_data *td, int new_state)
177{
178 int old_state = td->runstate;
179
180 td_set_runstate(td, new_state);
181 return old_state;
182}
183
184void td_restore_runstate(struct thread_data *td, int old_state)
185{
186 td_set_runstate(td, old_state);
187}
188
2e1df07d
JA
189void fio_terminate_threads(int group_id)
190{
191 struct thread_data *td;
41fa20ec 192 pid_t pid = getpid();
2e1df07d
JA
193 int i;
194
195 dprint(FD_PROCESS, "terminate group_id=%d\n", group_id);
196
197 for_each_td(td, i) {
198 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
199 dprint(FD_PROCESS, "setting terminate on %s/%d\n",
200 td->o.name, (int) td->pid);
201 td->terminate = 1;
202 td->o.start_delay = 0;
203
204 /*
205 * if the thread is running, just let it exit
206 */
36d80bc7 207 if (!td->pid || pid == td->pid)
2e1df07d
JA
208 continue;
209 else if (td->runstate < TD_RAMP)
210 kill(td->pid, SIGTERM);
36d80bc7 211 else {
2e1df07d
JA
212 struct ioengine_ops *ops = td->io_ops;
213
36d80bc7
JA
214 if (ops && ops->terminate)
215 ops->terminate(td);
2e1df07d
JA
216 }
217 }
218 }
219}
220
a569b45f
JA
221static int endian_check(void)
222{
223 union {
224 uint8_t c[8];
225 uint64_t v;
226 } u;
227 int le = 0, be = 0;
228
229 u.v = 0x12;
230 if (u.c[7] == 0x12)
231 be = 1;
232 else if (u.c[0] == 0x12)
233 le = 1;
234
235#if defined(CONFIG_LITTLE_ENDIAN)
236 if (be)
237 return 1;
238#elif defined(CONFIG_BIG_ENDIAN)
239 if (le)
240 return 1;
241#else
242 return 1;
243#endif
244
245 if (!le && !be)
246 return 1;
247
248 return 0;
249}
250
251int initialize_fio(char *envp[])
252{
253 long ps;
254
255 if (endian_check()) {
256 log_err("fio: endianness settings appear wrong.\n");
257 log_err("fio: please report this to fio@vger.kernel.org\n");
258 return 1;
259 }
260
261#if !defined(CONFIG_GETTIMEOFDAY) && !defined(CONFIG_CLOCK_GETTIME)
262#error "No available clock source!"
263#endif
264
265 arch_init(envp);
266
267 sinit();
268
243bfe19
JA
269 if (fio_filelock_init()) {
270 log_err("fio: failed initializing filelock subsys\n");
271 return 1;
272 }
273
a569b45f
JA
274 /*
275 * We need locale for number printing, if it isn't set then just
276 * go with the US format.
277 */
278 if (!getenv("LC_NUMERIC"))
279 setlocale(LC_NUMERIC, "en_US");
280
281 ps = sysconf(_SC_PAGESIZE);
282 if (ps < 0) {
283 log_err("Failed to get page size\n");
284 return 1;
285 }
286
287 page_size = ps;
288 page_mask = ps - 1;
2e1df07d 289
a569b45f
JA
290 fio_keywords_init();
291 return 0;
292}