1ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifndef FIO_OS_H
2ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#define FIO_OS_H
3ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
4a14ca44ada0708be91ed7741a32176bef4801cc4Jens Axboe#include <sys/types.h>
5203913ea773a19dce4a885204e9d7681b89429feBruce Cran#include <sys/socket.h>
693bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran#include <fcntl.h>
703e20d687566753b90383571e5e152c5142bdffdBruce Cran#include <pthread.h>
8a14ca44ada0708be91ed7741a32176bef4801cc4Jens Axboe#include <unistd.h>
9ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran#include <stdlib.h>
10a14ca44ada0708be91ed7741a32176bef4801cc4Jens Axboe
11836fcc0fceb233ebcc41ee63b4ea5cae20b678a4Jens Axboe#include "../arch/arch.h"
12836fcc0fceb233ebcc41ee63b4ea5cae20b678a4Jens Axboe
13cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboeenum {
14cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe	os_linux = 1,
15cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe	os_aix,
16cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe	os_freebsd,
17cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe	os_hpux,
18cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe	os_mac,
19cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe	os_netbsd,
203cd4c66f35fad0df64e4e6dfeddc5bdfd5206e0cJohn	os_openbsd,
21cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe	os_solaris,
22cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe	os_windows,
23ec5c6b125c1eab992882602158bab54957aa733dAaron Carroll	os_android,
24ec58c747242805551f25d90803fec67620424cc3Jens Axboe	os_dragonfly,
25cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe
26cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe	os_nr,
27cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe};
28cca84643cc10cd72b0b453ff92ccb8643ba51493Jens Axboe
29ec5c6b125c1eab992882602158bab54957aa733dAaron Carroll#if defined(__ANDROID__)
30ec5c6b125c1eab992882602158bab54957aa733dAaron Carroll#include "os-android.h"
31ec5c6b125c1eab992882602158bab54957aa733dAaron Carroll#elif defined(__linux__)
32ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include "os-linux.h"
33ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#elif defined(__FreeBSD__)
34ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include "os-freebsd.h"
353cd4c66f35fad0df64e4e6dfeddc5bdfd5206e0cJohn#elif defined(__OpenBSD__)
363cd4c66f35fad0df64e4e6dfeddc5bdfd5206e0cJohn#include "os-openbsd.h"
377452440e1efb40680fdc951eebc7499a95a62f86YAMAMOTO Takashi#elif defined(__NetBSD__)
387452440e1efb40680fdc951eebc7499a95a62f86YAMAMOTO Takashi#include "os-netbsd.h"
392c0ecd28459b6a1b236c865defb5ef76ce8bfa02Jens Axboe#elif defined(__sun__)
402c0ecd28459b6a1b236c865defb5ef76ce8bfa02Jens Axboe#include "os-solaris.h"
412afd826bf6cd19900aee70ae14ede92d91b6f4c0Jens Axboe#elif defined(__APPLE__)
422afd826bf6cd19900aee70ae14ede92d91b6f4c0Jens Axboe#include "os-mac.h"
43bf2e821a55d8aa3de1cda7288a0e22883110fdc6Cigy Cyriac#elif defined(_AIX)
44bf2e821a55d8aa3de1cda7288a0e22883110fdc6Cigy Cyriac#include "os-aix.h"
45c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe#elif defined(__hpux)
46c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe#include "os-hpux.h"
4793bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran#elif defined(WIN32)
4803e20d687566753b90383571e5e152c5142bdffdBruce Cran#include "os-windows.h"
49ec58c747242805551f25d90803fec67620424cc3Jens Axboe#elif defined (__DragonFly__)
50ec58c747242805551f25d90803fec67620424cc3Jens Axboe#include "os-dragonfly.h"
51ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#else
52ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#error "unsupported os"
53ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
54ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
5567bf982340d95ca98098ea050b54b4c7adb116c0Jens Axboe#ifdef CONFIG_POSIXAIO
56ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include <aio.h>
57e97c14423a5688d7adfb342c16363c263cb741f9Jens Axboe#ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
58e97c14423a5688d7adfb342c16363c263cb741f9Jens Axboetypedef struct aiocb os_aiocb_t;
59e97c14423a5688d7adfb342c16363c263cb741f9Jens Axboe#endif
60ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
61ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
62ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifdef FIO_HAVE_SGIO
63ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include <linux/fs.h>
64ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#include <scsi/sg.h>
65ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
66ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
674feafb1e7864b736e1bfe83fe185fbba933b1c60Jens Axboe#ifndef CONFIG_STRSEP
6800fb3c8dcbb940338fea9f6cab689b4924266305Jens Axboe#include "../lib/strsep.h"
695921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe#endif
705921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe
718e239cae8aae89f07a885ffcc985600ce9c65d5dJens Axboe#ifdef MSG_DONTWAIT
728e239cae8aae89f07a885ffcc985600ce9c65d5dJens Axboe#define OS_MSG_DONTWAIT	MSG_DONTWAIT
738e239cae8aae89f07a885ffcc985600ce9c65d5dJens Axboe#endif
748e239cae8aae89f07a885ffcc985600ce9c65d5dJens Axboe
754d8947de2b50578a4f6290be567cb5de5bf46776Shawn Lewis#ifndef POSIX_FADV_DONTNEED
76ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#define POSIX_FADV_DONTNEED	(0)
77ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#define POSIX_FADV_SEQUENTIAL	(0)
78ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#define POSIX_FADV_RANDOM	(0)
794d8947de2b50578a4f6290be567cb5de5bf46776Shawn Lewis#endif
80ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
81ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifndef FIO_HAVE_CPU_AFFINITY
82e8462bd8250cf3ff2d41f17e1a4d4cefc70b6b37Jens Axboe#define fio_setaffinity(pid, mask)	(0)
83be4ecfdf6c8daa75c4df8ac875c9a87da80d81a0Jens Axboe#define fio_getaffinity(pid, mask)	do { } while (0)
84be4ecfdf6c8daa75c4df8ac875c9a87da80d81a0Jens Axboe#define fio_cpu_clear(mask, cpu)	do { } while (0)
8585daf2c1102ed4fdf1477cfd372046efbe08046eJens Axboe#define fio_cpuset_exit(mask)		(-1)
86c2acfbac7e9eb8ac5407ac024c0fd000614c77e2Jens Axboe#define fio_cpus_split(mask, cpu)	(0)
877452440e1efb40680fdc951eebc7499a95a62f86YAMAMOTO Takashitypedef unsigned long os_cpu_mask_t;
88c2acfbac7e9eb8ac5407ac024c0fd000614c77e2Jens Axboe#else
89c2acfbac7e9eb8ac5407ac024c0fd000614c77e2Jens Axboeextern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu);
90ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
91ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
92ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#ifndef FIO_HAVE_IOPRIO
9328727df79348d4ebbd8fd4b0dd89bbca035f68d0Jens Axboe#define ioprio_set(which, who, prioclass, prio)	(0)
94ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
95ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe
962c0ecd28459b6a1b236c865defb5ef76ce8bfa02Jens Axboe#ifndef FIO_HAVE_ODIRECT
977d4247639268fdcc1769fc57564d79791042950dJens Axboe#define OS_O_DIRECT			0
987d4247639268fdcc1769fc57564d79791042950dJens Axboe#else
997d4247639268fdcc1769fc57564d79791042950dJens Axboe#define OS_O_DIRECT			O_DIRECT
1002c0ecd28459b6a1b236c865defb5ef76ce8bfa02Jens Axboe#endif
1012c0ecd28459b6a1b236c865defb5ef76ce8bfa02Jens Axboe
102d01612f3ae2515eb035d0c4ce954d8cb167a0a61Chris Mason#ifdef OS_O_ATOMIC
103d01612f3ae2515eb035d0c4ce954d8cb167a0a61Chris Mason#define FIO_O_ATOMIC			OS_O_ATOMIC
104d01612f3ae2515eb035d0c4ce954d8cb167a0a61Chris Mason#else
105d01612f3ae2515eb035d0c4ce954d8cb167a0a61Chris Mason#define FIO_O_ATOMIC			0
106d01612f3ae2515eb035d0c4ce954d8cb167a0a61Chris Mason#endif
107d01612f3ae2515eb035d0c4ce954d8cb167a0a61Chris Mason
10874b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe#ifndef FIO_HAVE_HUGETLB
10974b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe#define SHM_HUGETLB			0
110d6dc02fbe47fa6b4752df7a2ed52447506d68b45Jens Axboe#define MAP_HUGETLB			0
1114d8947de2b50578a4f6290be567cb5de5bf46776Shawn Lewis#ifndef FIO_HUGE_PAGE
11274b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe#define FIO_HUGE_PAGE			0
1134d8947de2b50578a4f6290be567cb5de5bf46776Shawn Lewis#endif
11474b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe#else
115cb25df61fbe19065b41bee131d19335d615c676bJens Axboe#ifndef FIO_HUGE_PAGE
116ee0e0a718f069fae189be800c2a6e2f5c28ff9f0Jens Axboe#define FIO_HUGE_PAGE			4194304
11774b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe#endif
118cb25df61fbe19065b41bee131d19335d615c676bJens Axboe#endif
11974b025b071b5bfbffa7ad7682b66b749e8d1f955Jens Axboe
12039b9356872f27c1a48a2ab09a5252dffd1de22d2Jens Axboe#ifndef FIO_HAVE_MMAP_HUGE
12139b9356872f27c1a48a2ab09a5252dffd1de22d2Jens Axboe#define MAP_HUGETLB			0
12239b9356872f27c1a48a2ab09a5252dffd1de22d2Jens Axboe#endif
12339b9356872f27c1a48a2ab09a5252dffd1de22d2Jens Axboe
1245921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe#ifndef FIO_O_NOATIME
1255921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe#define FIO_O_NOATIME			0
1265921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe#endif
1275921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboe
128dc873b6f4a536c332b72cce268d5a7ccd356a891Jens Axboe#ifndef OS_RAND_MAX
129dc873b6f4a536c332b72cce268d5a7ccd356a891Jens Axboe#define OS_RAND_MAX			RAND_MAX
130dc873b6f4a536c332b72cce268d5a7ccd356a891Jens Axboe#endif
131dc873b6f4a536c332b72cce268d5a7ccd356a891Jens Axboe
13207e5b2646f673a56c05a53c6a84bf5d0c949d290Jens Axboe#ifndef FIO_HAVE_RAWBIND
1338cc7afa91ddc5a0980b9d2dbfdd66e1511a0d4b1Jens Axboe#define fio_lookup_raw(dev, majdev, mindev)	1
13407e5b2646f673a56c05a53c6a84bf5d0c949d290Jens Axboe#endif
13507e5b2646f673a56c05a53c6a84bf5d0c949d290Jens Axboe
13658483fa41bb08def4dbd3c4c9624116b643c7599Jens Axboe#ifndef FIO_PREFERRED_ENGINE
13758483fa41bb08def4dbd3c4c9624116b643c7599Jens Axboe#define FIO_PREFERRED_ENGINE	"sync"
13858483fa41bb08def4dbd3c4c9624116b643c7599Jens Axboe#endif
13958483fa41bb08def4dbd3c4c9624116b643c7599Jens Axboe
140b9fd788f0e8adacc33316107594e9eb0463743d7Bruce Cran#ifndef FIO_OS_PATH_SEPARATOR
141b9fd788f0e8adacc33316107594e9eb0463743d7Bruce Cran#define FIO_OS_PATH_SEPARATOR	"/"
142b9fd788f0e8adacc33316107594e9eb0463743d7Bruce Cran#endif
143b9fd788f0e8adacc33316107594e9eb0463743d7Bruce Cran
14416de1bf91ca80e62ce257a24cae471fab140bbe0Bruce Cran#ifndef FIO_PREFERRED_CLOCK_SOURCE
14586e53416e5758d56e950af6cfbe407065ef36879Bruce Cran#ifdef CONFIG_CLOCK_GETTIME
14616de1bf91ca80e62ce257a24cae471fab140bbe0Bruce Cran#define FIO_PREFERRED_CLOCK_SOURCE	CS_CGETTIME
14786e53416e5758d56e950af6cfbe407065ef36879Bruce Cran#else
14886e53416e5758d56e950af6cfbe407065ef36879Bruce Cran#define FIO_PREFERRED_CLOCK_SOURCE	CS_GTOD
14986e53416e5758d56e950af6cfbe407065ef36879Bruce Cran#endif
15016de1bf91ca80e62ce257a24cae471fab140bbe0Bruce Cran#endif
15116de1bf91ca80e62ce257a24cae471fab140bbe0Bruce Cran
152fca7035863bd570270376a0c06776e5549ff813eJens Axboe#ifndef FIO_MAX_JOBS
153fca7035863bd570270376a0c06776e5549ff813eJens Axboe#define FIO_MAX_JOBS		2048
154fca7035863bd570270376a0c06776e5549ff813eJens Axboe#endif
155fca7035863bd570270376a0c06776e5549ff813eJens Axboe
15667bf982340d95ca98098ea050b54b4c7adb116c0Jens Axboe#ifndef CONFIG_SOCKLEN_T
15767bf982340d95ca98098ea050b54b4c7adb116c0Jens Axboetypedef unsigned int socklen_t;
1585ba13ea6968cf2773f10d34376afe28ef81aeee5Jens Axboe#endif
1595ba13ea6968cf2773f10d34376afe28ef81aeee5Jens Axboe
16045054cbec0e624de3b79a795d7dfe1c64cdea934Saurabh De#ifndef FIO_OS_HAS_CTIME_R
1611f81991ed356dd7257aef2c715ba9a24d9af93a5Jens Axboe#define os_ctime_r(x, y, z)     (void) ctime_r((x), (y))
16245054cbec0e624de3b79a795d7dfe1c64cdea934Saurabh De#endif
16345054cbec0e624de3b79a795d7dfe1c64cdea934Saurabh De
164901ebe18bdca90032e2fe107a1410385da80d18bJens Axboe#ifdef FIO_USE_GENERIC_SWAP
1659677bec37d1b9d2949116cdc3d691a4d3e534cbeJens Axboestatic inline uint16_t fio_swap16(uint16_t val)
166232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe{
167232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe	return (val << 8) | (val >> 8);
168232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe}
169232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe
170901ebe18bdca90032e2fe107a1410385da80d18bJens Axboestatic inline uint32_t fio_swap32(uint32_t val)
171232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe{
172232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe	val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
173232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe
174232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe	return (val >> 16) | (val << 16);
175232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe}
176232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe
177901ebe18bdca90032e2fe107a1410385da80d18bJens Axboestatic inline uint64_t fio_swap64(uint64_t val)
178232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe{
179232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe	val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
180232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe	      ((val & 0x00ff00ff00ff00ffULL) << 8);
181232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe	val = ((val & 0xffff0000ffff0000ULL) >> 16) |
182232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe	      ((val & 0x0000ffff0000ffffULL) << 16);
183232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe
184232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe	return (val >> 32) | (val << 32);
185232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe}
186232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe#endif
187232f9b73353e0322fe135d3029beecedaed7a17bJens Axboe
188ec5c6b125c1eab992882602158bab54957aa733dAaron Carroll#ifndef FIO_HAVE_BYTEORDER_FUNCS
1890dcebdf4a70ef0d8144b8fcba763ae87e7fc74b5Jens Axboe#ifdef CONFIG_LITTLE_ENDIAN
1908111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __le16_to_cpu(x)		(x)
1918111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __le32_to_cpu(x)		(x)
1928111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __le64_to_cpu(x)		(x)
1938111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __cpu_to_le16(x)		(x)
1948111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __cpu_to_le32(x)		(x)
1958111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __cpu_to_le64(x)		(x)
1968111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#else
1978111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __le16_to_cpu(x)		fio_swap16(x)
1988111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __le32_to_cpu(x)		fio_swap32(x)
1998111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __le64_to_cpu(x)		fio_swap64(x)
2008111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __cpu_to_le16(x)		fio_swap16(x)
2018111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __cpu_to_le32(x)		fio_swap32(x)
2028111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define __cpu_to_le64(x)		fio_swap64(x)
2038111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#endif
204ec5c6b125c1eab992882602158bab54957aa733dAaron Carroll#endif /* FIO_HAVE_BYTEORDER_FUNCS */
2058111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe
2060bbe46e7051d1ff9469ff9d021656793c6f3c4c2Daniel Gollub#ifdef FIO_INTERNAL
2078111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define le16_to_cpu(val) ({			\
2086cac1b6dc872933f823c696360d7b1b21a39559eJens Axboe	typecheck(uint16_t, val);		\
209c46fda904f38a66f5f68a4b766438c09ef8cddccSébastien Bouchex Bellomié	__le16_to_cpu(val);			\
2108111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe})
2118111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define le32_to_cpu(val) ({			\
2126cac1b6dc872933f823c696360d7b1b21a39559eJens Axboe	typecheck(uint32_t, val);		\
213c46fda904f38a66f5f68a4b766438c09ef8cddccSébastien Bouchex Bellomié	__le32_to_cpu(val);			\
2148111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe})
2158111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define le64_to_cpu(val) ({			\
2166cac1b6dc872933f823c696360d7b1b21a39559eJens Axboe	typecheck(uint64_t, val);		\
2176cac1b6dc872933f823c696360d7b1b21a39559eJens Axboe	__le64_to_cpu(val);			\
2188111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe})
2190bbe46e7051d1ff9469ff9d021656793c6f3c4c2Daniel Gollub#endif
2200bbe46e7051d1ff9469ff9d021656793c6f3c4c2Daniel Gollub
2218111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define cpu_to_le16(val) ({			\
2226cac1b6dc872933f823c696360d7b1b21a39559eJens Axboe	typecheck(uint16_t, val);		\
223c46fda904f38a66f5f68a4b766438c09ef8cddccSébastien Bouchex Bellomié	__cpu_to_le16(val);			\
2248111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe})
2258111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define cpu_to_le32(val) ({			\
2266cac1b6dc872933f823c696360d7b1b21a39559eJens Axboe	typecheck(uint32_t, val);		\
227c46fda904f38a66f5f68a4b766438c09ef8cddccSébastien Bouchex Bellomié	__cpu_to_le32(val);			\
2288111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe})
2298111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe#define cpu_to_le64(val) ({			\
2306cac1b6dc872933f823c696360d7b1b21a39559eJens Axboe	typecheck(uint64_t, val);		\
231c46fda904f38a66f5f68a4b766438c09ef8cddccSébastien Bouchex Bellomié	__cpu_to_le64(val);			\
2328111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe})
2338111092fd5dfc2fbdfc9b5b30094b74de982bddbJens Axboe
2345e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe#ifndef FIO_HAVE_BLKTRACE
235d95b34a61ec3fc305fe53c0132bfe82e2e5fcc04Jens Axboestatic inline int is_blktrace(const char *fname, int *need_swap)
2365e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe{
2375e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe	return 0;
2385e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe}
2395921e80c5dfc9f96d2f21da6ae58f2b5d3a0b373Jens Axboestruct thread_data;
240d95b34a61ec3fc305fe53c0132bfe82e2e5fcc04Jens Axboestatic inline int load_blktrace(struct thread_data *td, const char *fname,
241d95b34a61ec3fc305fe53c0132bfe82e2e5fcc04Jens Axboe				int need_swap)
2425e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe{
2435e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe	return 1;
2445e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe}
2455e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe#endif
2465e62c22a669c7a8c03b7871852cf85ad6d355980Jens Axboe
247eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe#define FIO_DEF_CL_SIZE		128
248eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe
249eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboestatic inline int os_cache_line_size(void)
250eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe{
251eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe#ifdef FIO_HAVE_CL_SIZE
252eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe	int ret = arch_cache_line_size();
253eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe
254eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe	if (ret <= 0)
255eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe		return FIO_DEF_CL_SIZE;
256eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe
257eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe	return ret;
258eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe#else
259eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe	return FIO_DEF_CL_SIZE;
260eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe#endif
261eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe}
262eb7ccf38bf2e9208b593d021c50e9ad2ec0781eaJens Axboe
263792d551747e83601121afce033a66f357c9fc439Jens Axboe#ifdef FIO_USE_GENERIC_BDEV_SIZE
264ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cranstatic inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
265792d551747e83601121afce033a66f357c9fc439Jens Axboe{
2663b2e1464ae58caf42c74ab4bdeaa5eae7c38a23fJens Axboe	off_t end;
267792d551747e83601121afce033a66f357c9fc439Jens Axboe
2683b2e1464ae58caf42c74ab4bdeaa5eae7c38a23fJens Axboe	*bytes = 0;
2693b2e1464ae58caf42c74ab4bdeaa5eae7c38a23fJens Axboe
270ecc314ba7c5f02b7e90ac1dfbce1a74cd4e6d6feBruce Cran	end = lseek(f->fd, 0, SEEK_END);
271792d551747e83601121afce033a66f357c9fc439Jens Axboe	if (end < 0)
272792d551747e83601121afce033a66f357c9fc439Jens Axboe		return errno;
273792d551747e83601121afce033a66f357c9fc439Jens Axboe
274792d551747e83601121afce033a66f357c9fc439Jens Axboe	*bytes = end;
275792d551747e83601121afce033a66f357c9fc439Jens Axboe	return 0;
276792d551747e83601121afce033a66f357c9fc439Jens Axboe}
277792d551747e83601121afce033a66f357c9fc439Jens Axboe#endif
278792d551747e83601121afce033a66f357c9fc439Jens Axboe
279535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe#ifdef FIO_USE_GENERIC_RAND
280535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboetypedef unsigned int os_random_state_t;
281535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe
282535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboestatic inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
283535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe{
284535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe	srand(seed);
285535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe}
286535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe
287535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboestatic inline long os_random_long(os_random_state_t *rs)
288535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe{
289535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe	long val;
290535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe
291535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe	val = rand_r(rs);
292535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe	return val;
293535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe}
294535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe#endif
295535313705d69dc16f27ca38acf2f9bf7205e410dJens Axboe
29693bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran#ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE
29793bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cranextern void td_fill_rand_seeds(struct thread_data *td);
29893bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran/*
29993bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran * Initialize the various random states we need (random io, block size ranges,
30093bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran * read/write mix, etc).
30193bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran */
30293bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cranstatic inline int init_random_state(struct thread_data *td, unsigned long *rand_seeds, int size)
30393bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran{
30493bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran	int fd;
30593bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran
30693bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran	fd = open("/dev/urandom", O_RDONLY);
30793bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran	if (fd == -1) {
30893bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran		return 1;
30993bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran	}
31093bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran
31193bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran	if (read(fd, rand_seeds, size) < size) {
31293bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran		close(fd);
31393bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran		return 1;
31493bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran	}
31593bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran
31693bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran	close(fd);
31793bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran	td_fill_rand_seeds(td);
31893bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran	return 0;
31993bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran}
32093bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran#endif
32193bcfd20e37cef8cec350fe06d3a086724c9f257Bruce Cran
3222e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe#ifndef FIO_HAVE_FS_STAT
3232e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboestatic inline unsigned long long get_fs_size(const char *path)
3242e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe{
3252e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe	return 0;
3262e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe}
3272e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe#endif
3282e3bd4c21cc239fbda992a4ede89ebb85f550920Jens Axboe
329c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe#ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
330c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboestatic inline unsigned int cpus_online(void)
331c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe{
332c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe	return sysconf(_SC_NPROCESSORS_ONLN);
333c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe}
334c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe#endif
335c00a22894b82ff53b42c1e741a7b4828199a1388Jens Axboe
3367221da3796145cf778ded56024c5e1efa00a3901Daniel Gollub#ifndef CPU_COUNT
3376cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe#ifdef FIO_HAVE_CPU_AFFINITY
3386cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboestatic inline int CPU_COUNT(os_cpu_mask_t *mask)
3396cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe{
3406cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe	int max_cpus = cpus_online();
3416cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe	int nr_cpus, i;
3426cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe
3436cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe	for (i = 0, nr_cpus = 0; i < max_cpus; i++)
3446cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe		if (fio_cpu_isset(mask, i))
3456cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe			nr_cpus++;
3466cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe
3476cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe	return nr_cpus;
3486cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe}
3496cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe#endif
3506cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe#endif
3516cf71c249abd60f2d2f6878ae436d4bab00aacbdJens Axboe
35247f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe#ifndef FIO_HAVE_GETTID
35347f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboestatic inline int gettid(void)
35447f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe{
35547f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe	return getpid();
35647f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe}
35747f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe#endif
35847f767c113be7448eb8aa9d4a9f9aa8f760ef642Jens Axboe
359ebac4655dd3624f3296ff83be48e0cdc02852f1Jens Axboe#endif
360