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