os.h revision 47f767c113be7448eb8aa9d4a9f9aa8f760ef642
1ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifndef FIO_OS_H
2ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#define FIO_OS_H
3ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
4a14ca44ada0708be91ed7741a32176bef4801cc4Jens Axboe#include <sys/types.h>
503e20d687566753b90383571e5e152c5142bdffdBruce Cran#include <pthread.h>
6a14ca44ada0708be91ed7741a32176bef4801cc4Jens Axboe#include <unistd.h>
7ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran#include <stdlib.h>
8a14ca44ada0708be91ed7741a32176bef4801cc4Jens Axboe
9ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#if defined(__linux__)
10ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include "os-linux.h"
11ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#elif defined(__FreeBSD__)
12ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include "os-freebsd.h"
137452440e1efb40680fdc951eebc7499a95a62f86YAMAMOTO Takashi#elif defined(__NetBSD__)
147452440e1efb40680fdc951eebc7499a95a62f86YAMAMOTO Takashi#include "os-netbsd.h"
152c0ecd28459b6a1b236c865defb5ef76ce8bfa02Jens Axboe#elif defined(__sun__)
162c0ecd28459b6a1b236c865defb5ef76ce8bfa02Jens Axboe#include "os-solaris.h"
172afd826bf6cd19900aee70ae14ede92d91b6f4c0Jens Axboe#elif defined(__APPLE__)
182afd826bf6cd19900aee70ae14ede92d91b6f4c0Jens Axboe#include "os-mac.h"
19bf2e821a55d8aa3de1cda7288a0e22883110fdc6Cigy Cyriac#elif defined(_AIX)
20bf2e821a55d8aa3de1cda7288a0e22883110fdc6Cigy Cyriac#include "os-aix.h"
21c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe#elif defined(__hpux)
22c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe#include "os-hpux.h"
2303e20d687566753b90383571e5e152c5142bdffdBruce Cran#elif defined(__CYGWIN__)
2403e20d687566753b90383571e5e152c5142bdffdBruce Cran#include "os-windows.h"
25ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#else
26ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#error "unsupported os"
27ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
28ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
29ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifdef FIO_HAVE_LIBAIO
30ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include <libaio.h>
31ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
32ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
33ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifdef FIO_HAVE_POSIXAIO
34ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include <aio.h>
35ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
36ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
37ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifdef FIO_HAVE_SGIO
38ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include <linux/fs.h>
39ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include <scsi/sg.h>
40ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
41ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
425921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe#ifndef FIO_HAVE_STRSEP
4300fb3c8dcbb940338fea9f6cab689b4924266305Jens Axboe#include "../lib/strsep.h"
445921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe#endif
455921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe
468e239cae8aae89f07a885ffcc985600ce9c65d5dJens Axboe#ifdef MSG_DONTWAIT
478e239cae8aae89f07a885ffcc985600ce9c65d5dJens Axboe#define OS_MSG_DONTWAIT	MSG_DONTWAIT
488e239cae8aae89f07a885ffcc985600ce9c65d5dJens Axboe#endif
498e239cae8aae89f07a885ffcc985600ce9c65d5dJens Axboe
50ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifndef FIO_HAVE_FADVISE
51ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran#define posix_fadvise(fd, off, len, advice)	(0)
52ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
534d8947de2b50578a4f6290be567cb5de5bf46776Shawn Lewis#ifndef POSIX_FADV_DONTNEED
54ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#define POSIX_FADV_DONTNEED	(0)
55ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#define POSIX_FADV_SEQUENTIAL	(0)
56ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#define POSIX_FADV_RANDOM	(0)
574d8947de2b50578a4f6290be567cb5de5bf46776Shawn Lewis#endif
58ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif /* FIO_HAVE_FADVISE */
59ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
60ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifndef FIO_HAVE_CPU_AFFINITY
61e8462bd8250cf3ff2d41f17e1a4d4cefc70b6b37Jens Axboe#define fio_setaffinity(pid, mask)	(0)
62be4ecfdf6c8daa75c4df8ac875c9a87da80d81a0Jens Axboe#define fio_getaffinity(pid, mask)	do { } while (0)
63be4ecfdf6c8daa75c4df8ac875c9a87da80d81a0Jens Axboe#define fio_cpu_clear(mask, cpu)	do { } while (0)
6485daf2c1102ed4fdf1477cfd372046efbe08046eJens Axboe#define fio_cpuset_exit(mask)		(-1)
657452440e1efb40680fdc951eebc7499a95a62f86YAMAMOTO Takashitypedef unsigned long os_cpu_mask_t;
66ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
67ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
68ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifndef FIO_HAVE_IOPRIO
69ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#define ioprio_set(which, who, prio)	(0)
70ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
71ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
722c0ecd28459b6a1b236c865defb5ef76ce8bfa02Jens Axboe#ifndef FIO_HAVE_ODIRECT
737d4247639268fdcc1769fc57564d79791042950dJens Axboe#define OS_O_DIRECT			0
747d4247639268fdcc1769fc57564d79791042950dJens Axboe#else
757d4247639268fdcc1769fc57564d79791042950dJens Axboe#define OS_O_DIRECT			O_DIRECT
762c0ecd28459b6a1b236c865defb5ef76ce8bfa02Jens Axboe#endif
772c0ecd28459b6a1b236c865defb5ef76ce8bfa02Jens Axboe
7874b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe#ifndef FIO_HAVE_HUGETLB
7974b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe#define SHM_HUGETLB			0
804d8947de2b50578a4f6290be567cb5de5bf46776Shawn Lewis#ifndef FIO_HUGE_PAGE
8174b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe#define FIO_HUGE_PAGE			0
824d8947de2b50578a4f6290be567cb5de5bf46776Shawn Lewis#endif
8374b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe#else
84cb25df61fbe19065b41bee131d19335d615c676bJens Axboe#ifndef FIO_HUGE_PAGE
85ee0e0a718f069fae189be800c2a6e2f5c28ff9f0Jens Axboe#define FIO_HUGE_PAGE			4194304
8674b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe#endif
87cb25df61fbe19065b41bee131d19335d615c676bJens Axboe#endif
8874b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe
895921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe#ifndef FIO_O_NOATIME
905921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe#define FIO_O_NOATIME			0
915921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe#endif
925921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe
93dc873b6f4a536c332b72cce268d5a7ccd356a891Jens Axboe#ifndef OS_RAND_MAX
94dc873b6f4a536c332b72cce268d5a7ccd356a891Jens Axboe#define OS_RAND_MAX			RAND_MAX
95dc873b6f4a536c332b72cce268d5a7ccd356a891Jens Axboe#endif
96dc873b6f4a536c332b72cce268d5a7ccd356a891Jens Axboe
97ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran#ifdef FIO_HAVE_CLOCK_MONOTONIC
98ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran#define FIO_TIMER_CLOCK CLOCK_MONOTONIC
99ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran#else
100ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran#define FIO_TIMER_CLOCK CLOCK_REALTIME
101ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran#endif
102ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran
10307e5b2646f673a56c05a53c6a84bf5d0c949d290Jens Axboe#ifndef FIO_HAVE_RAWBIND
1048cc7afa91ddc5a0980b9d2dbfdd66e1511a0d4b1Jens Axboe#define fio_lookup_raw(dev, majdev, mindev)	1
10507e5b2646f673a56c05a53c6a84bf5d0c949d290Jens Axboe#endif
10607e5b2646f673a56c05a53c6a84bf5d0c949d290Jens Axboe
10758483fa41bb08def4dbd3c4c9624116b643c7599Jens Axboe#ifndef FIO_PREFERRED_ENGINE
10858483fa41bb08def4dbd3c4c9624116b643c7599Jens Axboe#define FIO_PREFERRED_ENGINE	"sync"
10958483fa41bb08def4dbd3c4c9624116b643c7599Jens Axboe#endif
11058483fa41bb08def4dbd3c4c9624116b643c7599Jens Axboe
111fca7035863bd570270376a0c06776e5549ff813eJens Axboe#ifndef FIO_MAX_JOBS
112fca7035863bd570270376a0c06776e5549ff813eJens Axboe#define FIO_MAX_JOBS		2048
113fca7035863bd570270376a0c06776e5549ff813eJens Axboe#endif
114fca7035863bd570270376a0c06776e5549ff813eJens Axboe
1155e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe#ifndef FIO_HAVE_BLKTRACE
1165e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboestatic inline int is_blktrace(const char *fname)
1175e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe{
1185e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe	return 0;
1195e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe}
1205921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboestruct thread_data;
1215e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboestatic inline int load_blktrace(struct thread_data *td, const char *fname)
1225e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe{
1235e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe	return 1;
1245e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe}
1255e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe#endif
1265e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe
127eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe#define FIO_DEF_CL_SIZE		128
128eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe
129eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboestatic inline int os_cache_line_size(void)
130eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe{
131eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe#ifdef FIO_HAVE_CL_SIZE
132eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe	int ret = arch_cache_line_size();
133eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe
134eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe	if (ret <= 0)
135eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe		return FIO_DEF_CL_SIZE;
136eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe
137eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe	return ret;
138eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe#else
139eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe	return FIO_DEF_CL_SIZE;
140eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe#endif
141eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe}
142eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe
143792d551747e83601121afce033a66f357c9fc439Jens Axboe#ifdef FIO_USE_GENERIC_BDEV_SIZE
144ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cranstatic inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
145792d551747e83601121afce033a66f357c9fc439Jens Axboe{
1463b2e1464ae58caf42c74ab4bdeaa5eae7c38a23fJens Axboe	off_t end;
147792d551747e83601121afce033a66f357c9fc439Jens Axboe
1483b2e1464ae58caf42c74ab4bdeaa5eae7c38a23fJens Axboe	*bytes = 0;
1493b2e1464ae58caf42c74ab4bdeaa5eae7c38a23fJens Axboe
150ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran	end = lseek(f->fd, 0, SEEK_END);
151792d551747e83601121afce033a66f357c9fc439Jens Axboe	if (end < 0)
152792d551747e83601121afce033a66f357c9fc439Jens Axboe		return errno;
153792d551747e83601121afce033a66f357c9fc439Jens Axboe
154792d551747e83601121afce033a66f357c9fc439Jens Axboe	*bytes = end;
155792d551747e83601121afce033a66f357c9fc439Jens Axboe	return 0;
156792d551747e83601121afce033a66f357c9fc439Jens Axboe}
157792d551747e83601121afce033a66f357c9fc439Jens Axboe#endif
158792d551747e83601121afce033a66f357c9fc439Jens Axboe
159535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe#ifdef FIO_USE_GENERIC_RAND
160535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboetypedef unsigned int os_random_state_t;
161535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe
162535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboestatic inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
163535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe{
164535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe	srand(seed);
165535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe}
166535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe
167535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboestatic inline long os_random_long(os_random_state_t *rs)
168535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe{
169535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe	long val;
170535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe
171535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe	val = rand_r(rs);
172535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe	return val;
173535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe}
174535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe#endif
175535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe
1762e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe#ifndef FIO_HAVE_FS_STAT
1772e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboestatic inline unsigned long long get_fs_size(const char *path)
1782e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe{
1792e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe	return 0;
1802e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe}
1812e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe#endif
1822e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe
183c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe#ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
184c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboestatic inline unsigned int cpus_online(void)
185c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe{
186c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe	return sysconf(_SC_NPROCESSORS_ONLN);
187c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe}
188c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe#endif
189c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe
19047f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe#ifndef FIO_HAVE_GETTID
19147f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboestatic inline int gettid(void)
19247f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe{
19347f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe	return getpid();
19447f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe}
19547f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe#endif
19647f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe
197ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
198