Commit | Line | Data |
---|---|---|
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 | ||
ab917e33 BC |
24 | /* Cygwin doesn't define rand_r if C99 or newer is being used */ |
25 | #if defined(WIN32) && !defined(rand_r) | |
26 | int 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 BC |
42 | |
43 | #define FIO_MAX_CPUS MAXIMUM_PROCESSORS | |
44 | ||
93bcfd20 BC |
45 | #define OS_MAP_ANON MAP_ANON |
46 | ||
93bcfd20 BC |
47 | #define fio_swap16(x) _byteswap_ushort(x) |
48 | #define fio_swap32(x) _byteswap_ulong(x) | |
49 | #define fio_swap64(x) _byteswap_uint64(x) | |
50 | ||
93bcfd20 BC |
51 | typedef DWORD_PTR os_cpu_mask_t; |
52 | ||
93bcfd20 BC |
53 | #define _SC_PAGESIZE 0x1 |
54 | #define _SC_NPROCESSORS_ONLN 0x2 | |
55 | #define _SC_PHYS_PAGES 0x4 | |
56 | ||
57 | #define SA_RESTART 0 | |
58 | #define SIGPIPE 0 | |
59 | ||
60 | /* | |
61 | * Windows doesn't have O_DIRECT or O_SYNC, so define them | |
62 | * here so we can reject them at runtime when using the _open | |
63 | * interface (windowsaio uses CreateFile) | |
64 | */ | |
65 | #define O_DIRECT 0x1000000 | |
66 | #define O_SYNC 0x2000000 | |
67 | ||
68 | /* Windows doesn't support madvise, so any values will work */ | |
69 | #define POSIX_MADV_DONTNEED 0 | |
70 | #define POSIX_MADV_SEQUENTIAL 0 | |
71 | #define POSIX_MADV_RANDOM 0 | |
72 | ||
73 | #define F_SETFL 0x1 | |
74 | #define F_GETFL 0x2 | |
75 | #define O_NONBLOCK FIONBIO | |
76 | ||
77 | /* Winsock doesn't support MSG_WAIT */ | |
78 | #define OS_MSG_DONTWAIT 0 | |
79 | ||
80 | #define POLLOUT 1 | |
81 | #define POLLIN 2 | |
82 | #define POLLERR 0 | |
83 | #define POLLHUP 1 | |
84 | ||
85 | #define SIGCONT 0 | |
84306c1d | 86 | #define SIGUSR1 1 |
2277d5d5 | 87 | #define SIGUSR2 2 |
93bcfd20 BC |
88 | |
89 | typedef int sigset_t; | |
90 | typedef int siginfo_t; | |
91 | ||
92 | struct sigaction | |
93 | { | |
94 | void (*sa_handler)(int); | |
95 | sigset_t sa_mask; | |
96 | int sa_flags; | |
97 | void* (*sa_sigaction)(int, siginfo_t *, void*); | |
98 | }; | |
99 | ||
93bcfd20 BC |
100 | long sysconf(int name); |
101 | ||
102 | int kill(pid_t pid, int sig); | |
103 | pid_t setsid(void); | |
104 | int setgid(gid_t gid); | |
105 | int setuid(uid_t uid); | |
106 | int nice(int incr); | |
107 | int sigaction(int sig, const struct sigaction *act, | |
108 | struct sigaction *oact); | |
109 | int fsync(int fildes); | |
110 | int fork(void); | |
111 | int fcntl(int fildes, int cmd, ...); | |
112 | int fdatasync(int fildes); | |
113 | int lstat(const char * path, struct stat * buf); | |
114 | uid_t geteuid(void); | |
824d6ff4 | 115 | char* ctime_r(const time_t *t, char *buf); |
93bcfd20 BC |
116 | int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); |
117 | ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset); | |
118 | ssize_t pwrite(int fildes, const void *buf, size_t nbyte, | |
119 | off_t offset); | |
93bcfd20 BC |
120 | |
121 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) | |
122 | { | |
123 | int rc = 0; | |
124 | HANDLE hFile; | |
5aa23eb8 BC |
125 | GET_LENGTH_INFORMATION info; |
126 | DWORD outBytes; | |
93bcfd20 BC |
127 | |
128 | if (f->hFile == NULL) { | |
129 | hFile = CreateFile(f->file_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, | |
130 | NULL, OPEN_EXISTING, 0, NULL); | |
131 | } else { | |
132 | hFile = f->hFile; | |
133 | } | |
134 | ||
93bcfd20 BC |
135 | if (DeviceIoControl(hFile, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, &info, sizeof(info), &outBytes, NULL)) |
136 | *bytes = info.Length.QuadPart; | |
137 | else | |
138 | rc = EIO; | |
139 | ||
140 | /* If we were passed a POSIX fd, | |
141 | * close the HANDLE we created via CreateFile */ | |
142 | if (hFile != INVALID_HANDLE_VALUE && f->hFile == NULL) | |
143 | CloseHandle(hFile); | |
144 | ||
145 | return rc; | |
146 | } | |
147 | ||
148 | static inline int chardev_size(struct fio_file *f, unsigned long long *bytes) | |
149 | { | |
150 | return blockdev_size(f, bytes); | |
151 | } | |
152 | ||
153 | static inline int blockdev_invalidate_cache(struct fio_file *f) | |
154 | { | |
22de5d77 | 155 | return ENOTSUP; |
93bcfd20 BC |
156 | } |
157 | ||
158 | static inline unsigned long long os_phys_mem(void) | |
159 | { | |
01d26955 | 160 | long pagesize, pages; |
93bcfd20 | 161 | |
01d26955 BC |
162 | pagesize = sysconf(_SC_PAGESIZE); |
163 | pages = sysconf(_SC_PHYS_PAGES); | |
164 | if (pages == -1 || pagesize == -1) | |
165 | return 0; | |
166 | ||
167 | return (unsigned long long) pages * (unsigned long long) pagesize; | |
93bcfd20 BC |
168 | } |
169 | ||
93bcfd20 BC |
170 | static inline int gettid(void) |
171 | { | |
172 | return GetCurrentThreadId(); | |
173 | } | |
174 | ||
175 | static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask) | |
176 | { | |
177 | HANDLE h; | |
178 | BOOL bSuccess = FALSE; | |
179 | ||
180 | h = OpenThread(THREAD_QUERY_INFORMATION | THREAD_SET_INFORMATION, TRUE, pid); | |
181 | if (h != NULL) { | |
182 | bSuccess = SetThreadAffinityMask(h, cpumask); | |
3cf1ff44 BC |
183 | if (!bSuccess) |
184 | log_err("fio_setaffinity failed: failed to set thread affinity (pid %d, mask %.16llx)\n", pid, cpumask); | |
185 | ||
93bcfd20 | 186 | CloseHandle(h); |
3cf1ff44 BC |
187 | } else { |
188 | log_err("fio_setaffinity failed: failed to get handle for pid %d\n", pid); | |
93bcfd20 BC |
189 | } |
190 | ||
191 | return (bSuccess)? 0 : -1; | |
192 | } | |
193 | ||
2c6772e2 | 194 | static inline int fio_getaffinity(int pid, os_cpu_mask_t *mask) |
93bcfd20 BC |
195 | { |
196 | os_cpu_mask_t systemMask; | |
197 | ||
198 | HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, TRUE, pid); | |
199 | ||
200 | if (h != NULL) { | |
201 | GetProcessAffinityMask(h, mask, &systemMask); | |
202 | CloseHandle(h); | |
203 | } else { | |
204 | log_err("fio_getaffinity failed: failed to get handle for pid %d\n", pid); | |
2c6772e2 | 205 | return -1; |
93bcfd20 | 206 | } |
2c6772e2 TK |
207 | |
208 | return 0; | |
93bcfd20 BC |
209 | } |
210 | ||
211 | static inline void fio_cpu_clear(os_cpu_mask_t *mask, int cpu) | |
212 | { | |
3f6685ef | 213 | *mask &= ~(1ULL << cpu); |
93bcfd20 BC |
214 | } |
215 | ||
216 | static inline void fio_cpu_set(os_cpu_mask_t *mask, int cpu) | |
217 | { | |
3f6685ef | 218 | *mask |= 1ULL << cpu; |
93bcfd20 BC |
219 | } |
220 | ||
50b5860b JA |
221 | static inline int fio_cpu_isset(os_cpu_mask_t *mask, int cpu) |
222 | { | |
52fd65f4 | 223 | return (*mask & (1ULL << cpu)) != 0; |
50b5860b JA |
224 | } |
225 | ||
a1fc70fb | 226 | static inline int fio_cpu_count(os_cpu_mask_t *mask) |
c2acfbac JA |
227 | { |
228 | return hweight64(*mask); | |
229 | } | |
230 | ||
93bcfd20 BC |
231 | static inline int fio_cpuset_init(os_cpu_mask_t *mask) |
232 | { | |
233 | *mask = 0; | |
234 | return 0; | |
235 | } | |
236 | ||
237 | static inline int fio_cpuset_exit(os_cpu_mask_t *mask) | |
238 | { | |
239 | return 0; | |
240 | } | |
241 | ||
1ed84d86 | 242 | static inline int init_random_seeds(unsigned long *rand_seeds, int size) |
93bcfd20 BC |
243 | { |
244 | HCRYPTPROV hCryptProv; | |
245 | ||
246 | if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) | |
247 | { | |
248 | errno = GetLastError(); | |
249 | log_err("CryptAcquireContext() failed: error %d\n", errno); | |
250 | return 1; | |
251 | } | |
252 | ||
253 | if (!CryptGenRandom(hCryptProv, size, (BYTE*)rand_seeds)) { | |
254 | errno = GetLastError(); | |
255 | log_err("CryptGenRandom() failed, error %d\n", errno); | |
256 | CryptReleaseContext(hCryptProv, 0); | |
257 | return 1; | |
258 | } | |
259 | ||
260 | CryptReleaseContext(hCryptProv, 0); | |
93bcfd20 BC |
261 | return 0; |
262 | } | |
263 | ||
264 | ||
f2a2ce0e HL |
265 | static inline int fio_set_sched_idle(void) |
266 | { | |
267 | /* SetThreadPriority returns nonzero for success */ | |
268 | return (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE))? 0 : -1; | |
269 | } | |
270 | ||
271 | ||
93bcfd20 | 272 | #endif /* FIO_OS_WINDOWS_H */ |