windows: prepare for Windows build split
[fio.git] / os / os-windows.h
CommitLineData
93bcfd20
BC
1#ifndef FIO_OS_WINDOWS_H
2#define FIO_OS_WINDOWS_H
3
4#define FIO_OS os_windows
5
6#include <sys/types.h>
7#include <sys/shm.h>
8#include <sys/stat.h>
9#include <errno.h>
5aa23eb8 10#include <winsock2.h>
93bcfd20
BC
11#include <windows.h>
12#include <psapi.h>
13#include <stdlib.h>
14
15#include "../smalloc.h"
16#include "../file.h"
17#include "../log.h"
95db3079 18#include "../lib/hweight.h"
984f30c9 19#include "../oslib/strcasestr.h"
52fd65f4 20#include "../lib/types.h"
93bcfd20 21
1f81991e
JA
22#include "windows/posix.h"
23
dac7244b 24/* MinGW won't declare rand_r unless _POSIX is defined */
ab917e33
BC
25#if defined(WIN32) && !defined(rand_r)
26int rand_r(unsigned *);
27#endif
28
f16b7405
BC
29#ifndef PTHREAD_STACK_MIN
30#define PTHREAD_STACK_MIN 65535
31#endif
32
93bcfd20
BC
33#define FIO_HAVE_ODIRECT
34#define FIO_HAVE_CPU_AFFINITY
35#define FIO_HAVE_CHARDEV_SIZE
93bcfd20 36#define FIO_HAVE_GETTID
93bcfd20
BC
37#define FIO_USE_GENERIC_RAND
38
39#define FIO_PREFERRED_ENGINE "windowsaio"
40#define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME
87262d97 41#define FIO_OS_PATH_SEPARATOR '\\'
93bcfd20 42
93bcfd20
BC
43#define OS_MAP_ANON MAP_ANON
44
93bcfd20
BC
45#define fio_swap16(x) _byteswap_ushort(x)
46#define fio_swap32(x) _byteswap_ulong(x)
47#define fio_swap64(x) _byteswap_uint64(x)
48
93bcfd20
BC
49#define _SC_PAGESIZE 0x1
50#define _SC_NPROCESSORS_ONLN 0x2
51#define _SC_PHYS_PAGES 0x4
52
53#define SA_RESTART 0
54#define SIGPIPE 0
55
56/*
57 * Windows doesn't have O_DIRECT or O_SYNC, so define them
58 * here so we can reject them at runtime when using the _open
59 * interface (windowsaio uses CreateFile)
60 */
61#define O_DIRECT 0x1000000
62#define O_SYNC 0x2000000
63
64/* Windows doesn't support madvise, so any values will work */
65#define POSIX_MADV_DONTNEED 0
66#define POSIX_MADV_SEQUENTIAL 0
67#define POSIX_MADV_RANDOM 0
68
69#define F_SETFL 0x1
70#define F_GETFL 0x2
71#define O_NONBLOCK FIONBIO
72
73/* Winsock doesn't support MSG_WAIT */
74#define OS_MSG_DONTWAIT 0
75
93bcfd20 76#define SIGCONT 0
84306c1d 77#define SIGUSR1 1
2277d5d5 78#define SIGUSR2 2
93bcfd20
BC
79
80typedef int sigset_t;
81typedef int siginfo_t;
82
83struct sigaction
84{
85 void (*sa_handler)(int);
86 sigset_t sa_mask;
87 int sa_flags;
88 void* (*sa_sigaction)(int, siginfo_t *, void*);
89};
90
93bcfd20
BC
91long sysconf(int name);
92
93int kill(pid_t pid, int sig);
94pid_t setsid(void);
95int setgid(gid_t gid);
96int setuid(uid_t uid);
97int nice(int incr);
98int sigaction(int sig, const struct sigaction *act,
99 struct sigaction *oact);
100int fsync(int fildes);
101int fork(void);
102int fcntl(int fildes, int cmd, ...);
103int fdatasync(int fildes);
104int lstat(const char * path, struct stat * buf);
105uid_t geteuid(void);
824d6ff4 106char* ctime_r(const time_t *t, char *buf);
93bcfd20
BC
107int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
108ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset);
109ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
110 off_t offset);
93bcfd20
BC
111
112static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
113{
114 int rc = 0;
115 HANDLE hFile;
5aa23eb8
BC
116 GET_LENGTH_INFORMATION info;
117 DWORD outBytes;
93bcfd20
BC
118
119 if (f->hFile == NULL) {
120 hFile = CreateFile(f->file_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
121 NULL, OPEN_EXISTING, 0, NULL);
122 } else {
123 hFile = f->hFile;
124 }
125
93bcfd20
BC
126 if (DeviceIoControl(hFile, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, &info, sizeof(info), &outBytes, NULL))
127 *bytes = info.Length.QuadPart;
128 else
129 rc = EIO;
130
131 /* If we were passed a POSIX fd,
132 * close the HANDLE we created via CreateFile */
133 if (hFile != INVALID_HANDLE_VALUE && f->hFile == NULL)
134 CloseHandle(hFile);
135
136 return rc;
137}
138
139static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
140{
141 return blockdev_size(f, bytes);
142}
143
144static inline int blockdev_invalidate_cache(struct fio_file *f)
145{
22de5d77 146 return ENOTSUP;
93bcfd20
BC
147}
148
149static inline unsigned long long os_phys_mem(void)
150{
01d26955 151 long pagesize, pages;
93bcfd20 152
01d26955
BC
153 pagesize = sysconf(_SC_PAGESIZE);
154 pages = sysconf(_SC_PHYS_PAGES);
155 if (pages == -1 || pagesize == -1)
156 return 0;
157
158 return (unsigned long long) pages * (unsigned long long) pagesize;
93bcfd20
BC
159}
160
93bcfd20
BC
161static inline int gettid(void)
162{
163 return GetCurrentThreadId();
164}
165
1ed84d86 166static inline int init_random_seeds(unsigned long *rand_seeds, int size)
93bcfd20
BC
167{
168 HCRYPTPROV hCryptProv;
169
170 if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
171 {
172 errno = GetLastError();
173 log_err("CryptAcquireContext() failed: error %d\n", errno);
174 return 1;
175 }
176
177 if (!CryptGenRandom(hCryptProv, size, (BYTE*)rand_seeds)) {
178 errno = GetLastError();
179 log_err("CryptGenRandom() failed, error %d\n", errno);
180 CryptReleaseContext(hCryptProv, 0);
181 return 1;
182 }
183
184 CryptReleaseContext(hCryptProv, 0);
93bcfd20
BC
185 return 0;
186}
187
f2a2ce0e
HL
188static inline int fio_set_sched_idle(void)
189{
190 /* SetThreadPriority returns nonzero for success */
191 return (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE))? 0 : -1;
192}
193
dac7244b 194#include "os-windows-xp.h"
f2a2ce0e 195
93bcfd20 196#endif /* FIO_OS_WINDOWS_H */