Query Windows clock frequency and use reported max
[fio.git] / os / os.h
... / ...
CommitLineData
1#ifndef FIO_OS_H
2#define FIO_OS_H
3
4#include <sys/types.h>
5#include <sys/socket.h>
6#include <fcntl.h>
7#include <pthread.h>
8#include <unistd.h>
9#include <stdlib.h>
10#include <errno.h>
11
12#include "../arch/arch.h" /* IWYU pragma: export */
13#include "../lib/types.h"
14
15enum {
16 os_linux = 1,
17 os_aix,
18 os_freebsd,
19 os_hpux,
20 os_mac,
21 os_netbsd,
22 os_openbsd,
23 os_solaris,
24 os_windows,
25 os_android,
26 os_dragonfly,
27
28 os_nr,
29};
30
31typedef enum {
32 CPU_ARM64_CRC32C,
33} cpu_features;
34
35/* IWYU pragma: begin_exports */
36#if defined(__ANDROID__)
37#include "os-android.h"
38#elif defined(__linux__)
39#include "os-linux.h"
40#elif defined(__FreeBSD__)
41#include "os-freebsd.h"
42#elif defined(__OpenBSD__)
43#include "os-openbsd.h"
44#elif defined(__NetBSD__)
45#include "os-netbsd.h"
46#elif defined(__sun__)
47#include "os-solaris.h"
48#elif defined(__APPLE__)
49#include "os-mac.h"
50#elif defined(_AIX)
51#include "os-aix.h"
52#elif defined(__hpux)
53#include "os-hpux.h"
54#elif defined(WIN32)
55#include "os-windows.h"
56#elif defined (__DragonFly__)
57#include "os-dragonfly.h"
58#else
59#error "unsupported os"
60#endif
61
62#ifndef EDQUOT
63#define EDQUOT EIO
64#endif
65
66#ifdef CONFIG_POSIXAIO
67#include <aio.h>
68#ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
69typedef struct aiocb os_aiocb_t;
70#endif
71#endif
72
73#ifndef CONFIG_STRSEP
74#include "../oslib/strsep.h"
75#endif
76
77#ifndef CONFIG_STRLCAT
78#include "../oslib/strlcat.h"
79#endif
80/* IWYU pragma: end_exports */
81
82#ifdef MSG_DONTWAIT
83#define OS_MSG_DONTWAIT MSG_DONTWAIT
84#endif
85
86#ifndef POSIX_FADV_DONTNEED
87#define POSIX_FADV_DONTNEED (0)
88#define POSIX_FADV_SEQUENTIAL (0)
89#define POSIX_FADV_RANDOM (0)
90#define POSIX_FADV_NORMAL (0)
91#endif
92
93#ifndef FIO_HAVE_CPU_AFFINITY
94#define fio_cpu_clear(mask, cpu) do { } while (0)
95typedef unsigned long os_cpu_mask_t;
96
97static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
98{
99 return 0;
100}
101
102static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask)
103{
104 return -1;
105}
106
107static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
108{
109 return -1;
110}
111
112static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index)
113{
114 return 0;
115}
116#else
117extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu);
118#endif
119
120#ifndef FIO_HAVE_IOPRIO_CLASS
121#define ioprio_value_is_class_rt(prio) (false)
122#endif
123#ifndef FIO_HAVE_IOPRIO
124#define ioprio_value(prioclass, prio) (0)
125#define ioprio_set(which, who, prioclass, prio) (0)
126#endif
127
128#ifndef FIO_HAVE_ODIRECT
129#define OS_O_DIRECT 0
130#else
131#define OS_O_DIRECT O_DIRECT
132#endif
133
134#ifdef OS_O_ATOMIC
135#define FIO_O_ATOMIC OS_O_ATOMIC
136#else
137#define FIO_O_ATOMIC 0
138#endif
139
140#ifndef FIO_HAVE_HUGETLB
141#define SHM_HUGETLB 0
142#define MAP_HUGETLB 0
143#ifndef FIO_HUGE_PAGE
144#define FIO_HUGE_PAGE 0
145#endif
146#else
147#ifndef FIO_HUGE_PAGE
148#define FIO_HUGE_PAGE 4194304
149#endif
150#endif
151
152#ifndef FIO_HAVE_MMAP_HUGE
153#define MAP_HUGETLB 0
154#endif
155
156#ifndef FIO_O_NOATIME
157#define FIO_O_NOATIME 0
158#endif
159
160#ifndef OS_RAND_MAX
161#define OS_RAND_MAX RAND_MAX
162#endif
163
164#ifndef FIO_PREFERRED_ENGINE
165#define FIO_PREFERRED_ENGINE "psync"
166#endif
167
168#ifndef FIO_OS_PATH_SEPARATOR
169#define FIO_OS_PATH_SEPARATOR '/'
170#endif
171
172#ifndef FIO_PREFERRED_CLOCK_SOURCE
173#ifdef CONFIG_CLOCK_GETTIME
174#define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME
175#else
176#define FIO_PREFERRED_CLOCK_SOURCE CS_GTOD
177#endif
178#endif
179
180#ifndef CONFIG_SOCKLEN_T
181typedef unsigned int socklen_t;
182#endif
183
184#ifndef FIO_OS_HAS_CTIME_R
185#define os_ctime_r(x, y, z) (void) ctime_r((x), (y))
186#endif
187
188#ifdef FIO_USE_GENERIC_SWAP
189static inline uint16_t fio_swap16(uint16_t val)
190{
191 return (val << 8) | (val >> 8);
192}
193
194static inline uint32_t fio_swap32(uint32_t val)
195{
196 val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
197
198 return (val >> 16) | (val << 16);
199}
200
201static inline uint64_t fio_swap64(uint64_t val)
202{
203 val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
204 ((val & 0x00ff00ff00ff00ffULL) << 8);
205 val = ((val & 0xffff0000ffff0000ULL) >> 16) |
206 ((val & 0x0000ffff0000ffffULL) << 16);
207
208 return (val >> 32) | (val << 32);
209}
210#endif
211
212#ifndef FIO_HAVE_BYTEORDER_FUNCS
213#ifdef CONFIG_LITTLE_ENDIAN
214#define __be16_to_cpu(x) fio_swap16(x)
215#define __be32_to_cpu(x) fio_swap32(x)
216#define __be64_to_cpu(x) fio_swap64(x)
217#define __le16_to_cpu(x) (x)
218#define __le32_to_cpu(x) (x)
219#define __le64_to_cpu(x) (x)
220#define __cpu_to_be16(x) fio_swap16(x)
221#define __cpu_to_be32(x) fio_swap32(x)
222#define __cpu_to_be64(x) fio_swap64(x)
223#define __cpu_to_le16(x) (x)
224#define __cpu_to_le32(x) (x)
225#define __cpu_to_le64(x) (x)
226#else
227#define __be16_to_cpu(x) (x)
228#define __be32_to_cpu(x) (x)
229#define __be64_to_cpu(x) (x)
230#define __le16_to_cpu(x) fio_swap16(x)
231#define __le32_to_cpu(x) fio_swap32(x)
232#define __le64_to_cpu(x) fio_swap64(x)
233#define __cpu_to_be16(x) (x)
234#define __cpu_to_be32(x) (x)
235#define __cpu_to_be64(x) (x)
236#define __cpu_to_le16(x) fio_swap16(x)
237#define __cpu_to_le32(x) fio_swap32(x)
238#define __cpu_to_le64(x) fio_swap64(x)
239#endif
240#endif /* FIO_HAVE_BYTEORDER_FUNCS */
241
242#ifdef FIO_INTERNAL
243#define be16_to_cpu(val) ({ \
244 typecheck(uint16_t, val); \
245 __be16_to_cpu(val); \
246})
247#define be32_to_cpu(val) ({ \
248 typecheck(uint32_t, val); \
249 __be32_to_cpu(val); \
250})
251#define be64_to_cpu(val) ({ \
252 typecheck(uint64_t, val); \
253 __be64_to_cpu(val); \
254})
255#define le16_to_cpu(val) ({ \
256 typecheck(uint16_t, val); \
257 __le16_to_cpu(val); \
258})
259#define le32_to_cpu(val) ({ \
260 typecheck(uint32_t, val); \
261 __le32_to_cpu(val); \
262})
263#define le64_to_cpu(val) ({ \
264 typecheck(uint64_t, val); \
265 __le64_to_cpu(val); \
266})
267#endif
268
269#define cpu_to_be16(val) ({ \
270 typecheck(uint16_t, val); \
271 __cpu_to_be16(val); \
272})
273#define cpu_to_be32(val) ({ \
274 typecheck(uint32_t, val); \
275 __cpu_to_be32(val); \
276})
277#define cpu_to_be64(val) ({ \
278 typecheck(uint64_t, val); \
279 __cpu_to_be64(val); \
280})
281#define cpu_to_le16(val) ({ \
282 typecheck(uint16_t, val); \
283 __cpu_to_le16(val); \
284})
285#define cpu_to_le32(val) ({ \
286 typecheck(uint32_t, val); \
287 __cpu_to_le32(val); \
288})
289#define cpu_to_le64(val) ({ \
290 typecheck(uint64_t, val); \
291 __cpu_to_le64(val); \
292})
293
294#define FIO_DEF_CL_SIZE 128
295
296static inline int os_cache_line_size(void)
297{
298#ifdef FIO_HAVE_CL_SIZE
299 int ret = arch_cache_line_size();
300
301 if (ret <= 0)
302 return FIO_DEF_CL_SIZE;
303
304 return ret;
305#else
306 return FIO_DEF_CL_SIZE;
307#endif
308}
309
310#ifdef FIO_USE_GENERIC_BDEV_SIZE
311static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
312{
313 off_t end;
314
315 *bytes = 0;
316
317 end = lseek(f->fd, 0, SEEK_END);
318 if (end < 0)
319 return errno;
320
321 *bytes = end;
322 return 0;
323}
324#endif
325
326#ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE
327static inline int init_random_seeds(uint64_t *rand_seeds, int size)
328{
329 int fd;
330
331 fd = open("/dev/urandom", O_RDONLY);
332 if (fd == -1) {
333 return 1;
334 }
335
336 if (read(fd, rand_seeds, size) < size) {
337 close(fd);
338 return 1;
339 }
340
341 close(fd);
342 return 0;
343}
344#endif
345
346#ifndef FIO_HAVE_FS_STAT
347static inline unsigned long long get_fs_free_size(const char *path)
348{
349 return 0;
350}
351#endif
352
353#ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
354static inline unsigned int cpus_online(void)
355{
356 return sysconf(_SC_NPROCESSORS_ONLN);
357}
358#endif
359
360#ifndef CPU_COUNT
361#ifdef FIO_HAVE_CPU_AFFINITY
362static inline int CPU_COUNT(os_cpu_mask_t *mask)
363{
364 int max_cpus = cpus_online();
365 int nr_cpus, i;
366
367 for (i = 0, nr_cpus = 0; i < max_cpus; i++)
368 if (fio_cpu_isset(mask, i))
369 nr_cpus++;
370
371 return nr_cpus;
372}
373#endif
374#endif
375
376#ifndef FIO_HAVE_GETTID
377#ifndef CONFIG_HAVE_GETTID
378static inline int gettid(void)
379{
380 return getpid();
381}
382#endif
383#endif
384
385#ifndef FIO_HAVE_SHM_ATTACH_REMOVED
386static inline int shm_attach_to_open_removed(void)
387{
388 return 0;
389}
390#endif
391
392#ifndef FIO_HAVE_NATIVE_FALLOCATE
393static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, uint64_t len)
394{
395 errno = ENOSYS;
396 return false;
397}
398#endif
399
400#if defined(CONFIG_POSIX_FALLOCATE) || defined(FIO_HAVE_NATIVE_FALLOCATE)
401# define FIO_HAVE_DEFAULT_FALLOCATE
402#endif
403
404#ifndef FIO_HAVE_CPU_HAS
405static inline bool os_cpu_has(cpu_features feature)
406{
407 return false;
408}
409#endif
410
411#ifndef FIO_EMULATED_MKDIR_TWO
412# define fio_mkdir(path, mode) mkdir(path, mode)
413#endif
414
415#ifdef _SC_CLK_TCK
416static inline void os_clk_tck(long *clk_tck)
417{
418 *clk_tck = sysconf(_SC_CLK_TCK);
419}
420#else
421extern void os_clk_tck(long *clk_tck);
422#endif
423
424#endif /* FIO_OS_H */