1 #ifndef FIO_OS_WINDOWS_H
2 #define FIO_OS_WINDOWS_H
4 #define FIO_OS os_windows
15 #include "../smalloc.h"
18 #include "../lib/hweight.h"
19 #include "../oslib/strcasestr.h"
21 #include "windows/posix.h"
23 /* Cygwin doesn't define rand_r if C99 or newer is being used */
24 #if defined(WIN32) && !defined(rand_r)
25 int rand_r(unsigned *);
28 #ifndef PTHREAD_STACK_MIN
29 #define PTHREAD_STACK_MIN 65535
32 #define FIO_HAVE_ODIRECT
33 #define FIO_HAVE_CPU_AFFINITY
34 #define FIO_HAVE_CHARDEV_SIZE
35 #define FIO_HAVE_GETTID
36 #define FIO_USE_GENERIC_RAND
38 #define FIO_PREFERRED_ENGINE "windowsaio"
39 #define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME
40 #define FIO_OS_PATH_SEPARATOR "\\"
42 #define FIO_MAX_CPUS MAXIMUM_PROCESSORS
44 #define OS_MAP_ANON MAP_ANON
46 #define fio_swap16(x) _byteswap_ushort(x)
47 #define fio_swap32(x) _byteswap_ulong(x)
48 #define fio_swap64(x) _byteswap_uint64(x)
50 typedef DWORD_PTR os_cpu_mask_t;
52 #define _SC_PAGESIZE 0x1
53 #define _SC_NPROCESSORS_ONLN 0x2
54 #define _SC_PHYS_PAGES 0x4
60 * Windows doesn't have O_DIRECT or O_SYNC, so define them
61 * here so we can reject them at runtime when using the _open
62 * interface (windowsaio uses CreateFile)
64 #define O_DIRECT 0x1000000
65 #define O_SYNC 0x2000000
67 /* Windows doesn't support madvise, so any values will work */
68 #define POSIX_MADV_DONTNEED 0
69 #define POSIX_MADV_SEQUENTIAL 0
70 #define POSIX_MADV_RANDOM 0
74 #define O_NONBLOCK FIONBIO
76 /* Winsock doesn't support MSG_WAIT */
77 #define OS_MSG_DONTWAIT 0
89 typedef int siginfo_t;
93 void (*sa_handler)(int);
96 void* (*sa_sigaction)(int, siginfo_t *, void*);
99 long sysconf(int name);
101 int kill(pid_t pid, int sig);
103 int setgid(gid_t gid);
104 int setuid(uid_t uid);
106 int sigaction(int sig, const struct sigaction *act,
107 struct sigaction *oact);
108 int fsync(int fildes);
110 int fcntl(int fildes, int cmd, ...);
111 int fdatasync(int fildes);
112 int lstat(const char * path, struct stat * buf);
114 char* ctime_r(const time_t *t, char *buf);
115 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
116 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset);
117 ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
119 extern void td_fill_rand_seeds(struct thread_data *);
121 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
125 GET_LENGTH_INFORMATION info;
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);
135 if (DeviceIoControl(hFile, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, &info, sizeof(info), &outBytes, NULL))
136 *bytes = info.Length.QuadPart;
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)
148 static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
150 return blockdev_size(f, bytes);
153 static inline int blockdev_invalidate_cache(struct fio_file *f)
155 /* There's no way to invalidate the cache in Windows
156 * so just pretend to succeed */
160 static inline unsigned long long os_phys_mem(void)
162 long pagesize, pages;
164 pagesize = sysconf(_SC_PAGESIZE);
165 pages = sysconf(_SC_PHYS_PAGES);
166 if (pages == -1 || pagesize == -1)
169 return (unsigned long long) pages * (unsigned long long) pagesize;
172 static inline int gettid(void)
174 return GetCurrentThreadId();
177 static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
180 BOOL bSuccess = FALSE;
182 h = OpenThread(THREAD_QUERY_INFORMATION | THREAD_SET_INFORMATION, TRUE, pid);
184 bSuccess = SetThreadAffinityMask(h, cpumask);
186 log_err("fio_setaffinity failed: failed to set thread affinity (pid %d, mask %.16llx)\n", pid, cpumask);
190 log_err("fio_setaffinity failed: failed to get handle for pid %d\n", pid);
193 return (bSuccess)? 0 : -1;
196 static inline int fio_getaffinity(int pid, os_cpu_mask_t *mask)
198 os_cpu_mask_t systemMask;
200 HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, TRUE, pid);
203 GetProcessAffinityMask(h, mask, &systemMask);
206 log_err("fio_getaffinity failed: failed to get handle for pid %d\n", pid);
213 static inline void fio_cpu_clear(os_cpu_mask_t *mask, int cpu)
215 *mask ^= 1 << (cpu-1);
218 static inline void fio_cpu_set(os_cpu_mask_t *mask, int cpu)
223 static inline int fio_cpu_isset(os_cpu_mask_t *mask, int cpu)
225 return (*mask & (1U << cpu));
228 static inline int fio_cpu_count(os_cpu_mask_t *mask)
230 return hweight64(*mask);
233 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
239 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
244 static inline int init_random_state(struct thread_data *td, unsigned long *rand_seeds, int size)
246 HCRYPTPROV hCryptProv;
248 if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
250 errno = GetLastError();
251 log_err("CryptAcquireContext() failed: error %d\n", errno);
255 if (!CryptGenRandom(hCryptProv, size, (BYTE*)rand_seeds)) {
256 errno = GetLastError();
257 log_err("CryptGenRandom() failed, error %d\n", errno);
258 CryptReleaseContext(hCryptProv, 0);
262 CryptReleaseContext(hCryptProv, 0);
263 td_fill_rand_seeds(td);
268 static inline int fio_set_sched_idle(void)
270 /* SetThreadPriority returns nonzero for success */
271 return (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE))? 0 : -1;
275 #endif /* FIO_OS_WINDOWS_H */