1 /* This file contains functions which implement those POSIX and Linux functions
2 * that MinGW and Microsoft don't provide. The implementations contain just enough
3 * functionality to support fio.
7 #include <netinet/in.h>
14 #include <semaphore.h>
18 #include <sys/resource.h>
21 #include "../os-windows.h"
23 extern unsigned long mtime_since_now(struct timeval *);
24 extern void fio_gettime(struct timeval *, void *);
26 long sysconf(int name)
30 SYSTEM_LOGICAL_PROCESSOR_INFORMATION processorInfo;
32 MEMORYSTATUSEX status;
36 case _SC_NPROCESSORS_ONLN:
37 len = sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
38 GetLogicalProcessorInformation(&processorInfo, &len);
39 val = len / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
43 GetSystemInfo(&sysInfo);
44 val = sysInfo.dwPageSize;
48 status.dwLength = sizeof(status);
49 GlobalMemoryStatusEx(&status);
50 val = status.ullTotalPhys;
53 log_err("sysconf(%d) is not implemented\n", name);
60 char *dl_error = NULL;
62 int dlclose(void *handle)
64 return !FreeLibrary((HMODULE)handle);
67 void *dlopen(const char *file, int mode)
71 hMod = LoadLibrary(file);
72 if (hMod == INVALID_HANDLE_VALUE)
73 dl_error = (char*)"LoadLibrary failed";
80 void *dlsym(void *handle, const char *name)
84 fnPtr = GetProcAddress((HMODULE)handle, name);
86 dl_error = (char*)"GetProcAddress failed";
98 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
101 unsigned long long unix_time, windows_time;
102 const unsigned long long MILLISECONDS_BETWEEN_1601_AND_1970 = 11644473600000;
104 /* Ignore the timezone parameter */
108 * Windows time is stored as the number 100 ns intervals since January 1 1601.
109 * Conversion details from http://www.informit.com/articles/article.aspx?p=102236&seqNum=3
110 * Its precision is 100 ns but accuracy is only one clock tick, or normally around 15 ms.
112 GetSystemTimeAsFileTime(&fileTime);
113 windows_time = ((unsigned long long)fileTime.dwHighDateTime << 32) + fileTime.dwLowDateTime;
114 /* Divide by 10,000 to convert to ms and subtract the time between 1601 and 1970 */
115 unix_time = (((windows_time)/10000) - MILLISECONDS_BETWEEN_1601_AND_1970);
116 /* unix_time is now the number of milliseconds since 1970 (the Unix epoch) */
117 tp->tv_sec = unix_time / 1000;
118 tp->tv_usec = (unix_time % 1000) * 1000;
122 void syslog(int priority, const char *message, ... /* argument */)
124 log_err("%s is not implemented\n", __func__);
127 int sigaction(int sig, const struct sigaction *act,
128 struct sigaction *oact)
131 void (*prev_handler)(int);
133 prev_handler = signal(sig, act->sa_handler);
135 oact->sa_handler = prev_handler;
137 if (prev_handler == SIG_ERR)
143 int lstat(const char * path, struct stat * buf)
145 return stat(path, buf);
148 void *mmap(void *addr, size_t len, int prot, int flags,
149 int fildes, off_t off)
152 void* allocAddr = NULL;
154 if (prot & PROT_NONE)
155 vaProt |= PAGE_NOACCESS;
157 if ((prot & PROT_READ) && !(prot & PROT_WRITE))
158 vaProt |= PAGE_READONLY;
160 if (prot & PROT_WRITE)
161 vaProt |= PAGE_READWRITE;
163 if ((flags & MAP_ANON) | (flags & MAP_ANONYMOUS))
165 allocAddr = VirtualAlloc(addr, len, MEM_COMMIT, vaProt);
171 int munmap(void *addr, size_t len)
173 return !VirtualFree(addr, 0, MEM_RELEASE);
178 log_err("%s is not implemented\n", __func__);
185 log_err("%s is not implemented\n", __func__);
190 void openlog(const char *ident, int logopt, int facility)
192 log_err("%s is not implemented\n", __func__);
197 log_err("%s is not implemented\n", __func__);
200 int kill(pid_t pid, int sig)
207 * This is assumed to be used only by the network code,
208 * and so doesn't try and handle any of the other cases
210 int fcntl(int fildes, int cmd, ...)
213 * non-blocking mode doesn't work the same as in BSD sockets,
218 int val, opt, status;
222 else if (cmd != F_SETFL) {
229 opt = va_arg(ap, int);
230 if (opt & O_NONBLOCK)
235 status = ioctlsocket((SOCKET)fildes, opt, &val);
237 if (status == SOCKET_ERROR) {
250 * Get the value of a local clock source.
251 * This implementation supports 2 clocks: CLOCK_MONOTONIC provides high-accuracy
252 * relative time, while CLOCK_REALTIME provides a low-accuracy wall time.
254 int clock_gettime(clockid_t clock_id, struct timespec *tp)
258 if (clock_id == CLOCK_MONOTONIC)
260 static LARGE_INTEGER freq = {{0,0}};
261 LARGE_INTEGER counts;
263 QueryPerformanceCounter(&counts);
264 if (freq.QuadPart == 0)
265 QueryPerformanceFrequency(&freq);
267 tp->tv_sec = counts.QuadPart / freq.QuadPart;
268 /* Get the difference between the number of ns stored
269 * in 'tv_sec' and that stored in 'counts' */
270 unsigned long long t = tp->tv_sec * freq.QuadPart;
271 t = counts.QuadPart - t;
272 /* 't' now contains the number of cycles since the last second.
273 * We want the number of nanoseconds, so multiply out by 1,000,000,000
274 * and then divide by the frequency. */
276 tp->tv_nsec = t / freq.QuadPart;
278 else if (clock_id == CLOCK_REALTIME)
280 /* clock_gettime(CLOCK_REALTIME,...) is just an alias for gettimeofday with a
281 * higher-precision field. */
283 gettimeofday(&tv, NULL);
284 tp->tv_sec = tv.tv_sec;
285 tp->tv_nsec = tv.tv_usec * 1000;
294 int mlock(const void * addr, size_t len)
296 return !VirtualLock((LPVOID)addr, len);
299 int munlock(const void * addr, size_t len)
301 return !VirtualUnlock((LPVOID)addr, len);
304 pid_t waitpid(pid_t pid, int *stat_loc, int options)
306 log_err("%s is not implemented\n", __func__);
311 int usleep(useconds_t useconds)
313 Sleep(useconds / 1000);
317 char *basename(char *path)
319 static char name[MAX_PATH];
322 if (path == NULL || strlen(path) == 0)
325 i = strlen(path) - 1;
327 while (name[i] != '\\' && name[i] != '/' && i >= 0)
330 strcpy(name, path + i);
335 int posix_fallocate(int fd, off_t offset, off_t len)
337 const int BUFFER_SIZE = 64*1024*1024;
340 unsigned int write_len;
341 unsigned int bytes_written;
342 off_t bytes_remaining = len;
344 if (len == 0 || offset < 0)
347 buf = malloc(BUFFER_SIZE);
352 memset(buf, 0, BUFFER_SIZE);
354 if (lseek(fd, offset, SEEK_SET) == -1)
357 while (bytes_remaining > 0) {
358 if (bytes_remaining < BUFFER_SIZE)
359 write_len = (unsigned int)bytes_remaining;
361 write_len = BUFFER_SIZE;
363 bytes_written = _write(fd, buf, write_len);
364 if (bytes_written == -1) {
369 bytes_remaining -= bytes_written;
376 int ftruncate(int fildes, off_t length)
379 int old_pos = tell(fildes);
380 lseek(fildes, length, SEEK_SET);
381 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
382 bSuccess = SetEndOfFile(hFile);
383 lseek(fildes, old_pos, SEEK_SET);
387 int fsync(int fildes)
389 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
390 return !FlushFileBuffers(hFile);
393 int nFileMappings = 0;
394 HANDLE fileMappings[1024];
396 int shmget(key_t key, size_t size, int shmflg)
399 uint32_t size_low = size & 0xFFFFFFFF;
400 uint32_t size_high = ((uint64_t)size) >> 32;
401 HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, (PAGE_EXECUTE_READWRITE | SEC_RESERVE), size_high, size_low, NULL);
402 if (hMapping != NULL) {
403 fileMappings[nFileMappings] = hMapping;
404 mapid = nFileMappings;
413 void *shmat(int shmid, const void *shmaddr, int shmflg)
416 MEMORY_BASIC_INFORMATION memInfo;
417 mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0);
418 VirtualQuery(mapAddr, &memInfo, sizeof(memInfo));
419 mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE);
423 int shmdt(const void *shmaddr)
425 return !UnmapViewOfFile(shmaddr);
428 int shmctl(int shmid, int cmd, struct shmid_ds *buf)
430 if (cmd == IPC_RMID) {
431 fileMappings[shmid] = INVALID_HANDLE_VALUE;
434 log_err("%s is not implemented\n", __func__);
439 int setuid(uid_t uid)
441 log_err("%s is not implemented\n", __func__);
446 int setgid(gid_t gid)
448 log_err("%s is not implemented\n", __func__);
463 int getrusage(int who, struct rusage *r_usage)
465 const unsigned long long SECONDS_BETWEEN_1601_AND_1970 = 11644473600;
466 FILETIME cTime, eTime, kTime, uTime;
469 memset(r_usage, 0, sizeof(*r_usage));
471 HANDLE hProcess = GetCurrentProcess();
472 GetProcessTimes(hProcess, &cTime, &eTime, &kTime, &uTime);
473 time = ((unsigned long long)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime;
474 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
476 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
477 r_usage->ru_utime.tv_sec = time;
478 /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */
479 r_usage->ru_utime.tv_usec = 0;
480 time = ((unsigned long long)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime;
481 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
483 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
484 r_usage->ru_stime.tv_sec = time;
485 r_usage->ru_stime.tv_usec = 0;
489 int posix_madvise(void *addr, size_t len, int advice)
491 log_err("%s is not implemented\n", __func__);
495 /* Windows doesn't support advice for memory pages. Just ignore it. */
496 int msync(void *addr, size_t len, int flags)
498 log_err("%s is not implemented\n", __func__);
503 int fdatasync(int fildes)
505 return fsync(fildes);
508 ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
511 long pos = tell(fildes);
512 ssize_t len = write(fildes, buf, nbyte);
513 lseek(fildes, pos, SEEK_SET);
517 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
519 long pos = tell(fildes);
520 ssize_t len = read(fildes, buf, nbyte);
521 lseek(fildes, pos, SEEK_SET);
525 ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
527 log_err("%s is not implemented\n", __func__);
532 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
534 log_err("%s is not implemented\n", __func__);
539 long long strtoll(const char *restrict str, char **restrict endptr,
542 return _strtoi64(str, endptr, base);
545 char *strsep(char **stringp, const char *delim)
547 char *orig = *stringp;
548 BOOL gotMatch = FALSE;
552 if (*stringp == NULL)
555 while ((*stringp)[i] != '\0') {
557 while (delim[j] != '\0') {
558 if ((*stringp)[i] == delim[j]) {
560 (*stringp)[i] = '\0';
561 *stringp = *stringp + i + 1;
578 int poll(struct pollfd fds[], nfds_t nfds, int timeout)
581 struct timeval *to = NULL;
582 fd_set readfds, writefds, exceptfds;
589 to->tv_sec = timeout / 1000;
590 to->tv_usec = (timeout % 1000) * 1000;
596 for (i = 0; i < nfds; i++)
603 if (fds[i].events & POLLIN)
604 FD_SET(fds[i].fd, &readfds);
606 if (fds[i].events & POLLOUT)
607 FD_SET(fds[i].fd, &writefds);
609 FD_SET(fds[i].fd, &exceptfds);
612 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
614 if (rc != SOCKET_ERROR) {
615 for (i = 0; i < nfds; i++)
621 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
622 fds[i].revents |= POLLIN;
624 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
625 fds[i].revents |= POLLOUT;
627 if (FD_ISSET(fds[i].fd, &exceptfds))
628 fds[i].revents |= POLLHUP;
635 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
639 DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0);
644 ms_remaining = ms_total;
646 /* Since Sleep() can sleep for less than the requested time, add a loop to
647 ensure we only return after the requested length of time has elapsed */
649 fio_gettime(&tv, NULL);
651 ms_remaining = ms_total - mtime_since_now(&tv);
652 } while (ms_remaining > 0 && ms_remaining < ms_total);
654 /* this implementation will never sleep for less than the requested time */
663 DIR *opendir(const char *dirname)
665 log_err("%s is not implemented\n", __func__);
670 int closedir(DIR *dirp)
672 log_err("%s is not implemented\n", __func__);
677 struct dirent *readdir(DIR *dirp)
679 log_err("%s is not implemented\n", __func__);
686 log_err("%s is not implemented\n", __func__);
691 int inet_aton(char *addr)
693 log_err("%s is not implemented\n", __func__);
698 const char* inet_ntop(int af, const void *restrict src,
699 char *restrict dst, socklen_t size)
701 INT status = SOCKET_ERROR;
705 if (af != AF_INET && af != AF_INET6) {
706 errno = EAFNOSUPPORT;
710 WSAStartup(MAKEWORD(2,2), &wsd);
713 struct sockaddr_in si;
715 memset(&si, 0, sizeof(si));
717 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
718 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
719 } else if (af == AF_INET6) {
720 struct sockaddr_in6 si6;
722 memset(&si6, 0, sizeof(si6));
723 si6.sin6_family = af;
724 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
725 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
728 if (status != SOCKET_ERROR)
737 int inet_pton(int af, const char *restrict src, void *restrict dst)
739 INT status = SOCKET_ERROR;
743 if (af != AF_INET && af != AF_INET6) {
744 errno = EAFNOSUPPORT;
748 WSAStartup(MAKEWORD(2,2), &wsd);
751 struct sockaddr_in si;
752 INT len = sizeof(si);
753 memset(&si, 0, sizeof(si));
755 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
756 if (status != SOCKET_ERROR)
757 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
758 } else if (af == AF_INET6) {
759 struct sockaddr_in6 si6;
760 INT len = sizeof(si6);
761 memset(&si6, 0, sizeof(si6));
762 si6.sin6_family = af;
763 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
764 if (status != SOCKET_ERROR)
765 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
768 if (status == SOCKET_ERROR) {