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 uint64_t unix_time, windows_time;
102 const uint64_t 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 = ((uint64_t)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 uint64_t 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 (path[i] != '\\' && path[i] != '/' && i >= 0)
330 strncpy(name, path + i + 1, MAX_PATH);
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 int64_t prev_pos = _telli64(fd);
356 if (_lseeki64(fd, offset, SEEK_SET) == -1)
359 while (bytes_remaining > 0) {
360 if (bytes_remaining < BUFFER_SIZE)
361 write_len = (unsigned int)bytes_remaining;
363 write_len = BUFFER_SIZE;
365 bytes_written = _write(fd, buf, write_len);
366 if (bytes_written == -1) {
371 bytes_remaining -= bytes_written;
375 _lseeki64(fd, prev_pos, SEEK_SET);
379 int ftruncate(int fildes, off_t length)
382 int64_t prev_pos = _telli64(fildes);
383 _lseeki64(fildes, length, SEEK_SET);
384 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
385 bSuccess = SetEndOfFile(hFile);
386 _lseeki64(fildes, prev_pos, SEEK_SET);
390 int fsync(int fildes)
392 HANDLE hFile = (HANDLE)_get_osfhandle(fildes);
393 return !FlushFileBuffers(hFile);
396 int nFileMappings = 0;
397 HANDLE fileMappings[1024];
399 int shmget(key_t key, size_t size, int shmflg)
402 uint32_t size_low = size & 0xFFFFFFFF;
403 uint32_t size_high = ((uint64_t)size) >> 32;
404 HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, (PAGE_EXECUTE_READWRITE | SEC_RESERVE), size_high, size_low, NULL);
405 if (hMapping != NULL) {
406 fileMappings[nFileMappings] = hMapping;
407 mapid = nFileMappings;
416 void *shmat(int shmid, const void *shmaddr, int shmflg)
419 MEMORY_BASIC_INFORMATION memInfo;
420 mapAddr = MapViewOfFile(fileMappings[shmid], FILE_MAP_ALL_ACCESS, 0, 0, 0);
421 VirtualQuery(mapAddr, &memInfo, sizeof(memInfo));
422 mapAddr = VirtualAlloc(mapAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE);
426 int shmdt(const void *shmaddr)
428 return !UnmapViewOfFile(shmaddr);
431 int shmctl(int shmid, int cmd, struct shmid_ds *buf)
433 if (cmd == IPC_RMID) {
434 fileMappings[shmid] = INVALID_HANDLE_VALUE;
437 log_err("%s is not implemented\n", __func__);
442 int setuid(uid_t uid)
444 log_err("%s is not implemented\n", __func__);
449 int setgid(gid_t gid)
451 log_err("%s is not implemented\n", __func__);
466 int getrusage(int who, struct rusage *r_usage)
468 const uint64_t SECONDS_BETWEEN_1601_AND_1970 = 11644473600;
469 FILETIME cTime, eTime, kTime, uTime;
472 memset(r_usage, 0, sizeof(*r_usage));
474 HANDLE hProcess = GetCurrentProcess();
475 GetProcessTimes(hProcess, &cTime, &eTime, &kTime, &uTime);
476 time = ((uint64_t)uTime.dwHighDateTime << 32) + uTime.dwLowDateTime;
477 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
479 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
480 r_usage->ru_utime.tv_sec = time;
481 /* getrusage() doesn't care about anything other than seconds, so set tv_usec to 0 */
482 r_usage->ru_utime.tv_usec = 0;
483 time = ((uint64_t)kTime.dwHighDateTime << 32) + kTime.dwLowDateTime;
484 /* Divide by 10,000,000 to get the number of seconds and move the epoch from
486 time = (time_t)(((time)/10000000) - SECONDS_BETWEEN_1601_AND_1970);
487 r_usage->ru_stime.tv_sec = time;
488 r_usage->ru_stime.tv_usec = 0;
492 int posix_madvise(void *addr, size_t len, int advice)
494 log_err("%s is not implemented\n", __func__);
498 /* Windows doesn't support advice for memory pages. Just ignore it. */
499 int msync(void *addr, size_t len, int flags)
501 log_err("%s is not implemented\n", __func__);
506 int fdatasync(int fildes)
508 return fsync(fildes);
511 ssize_t pwrite(int fildes, const void *buf, size_t nbyte,
514 int64_t pos = _telli64(fildes);
515 ssize_t len = _write(fildes, buf, nbyte);
516 _lseeki64(fildes, pos, SEEK_SET);
520 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset)
522 int64_t pos = _telli64(fildes);
523 ssize_t len = read(fildes, buf, nbyte);
524 _lseeki64(fildes, pos, SEEK_SET);
528 ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
530 log_err("%s is not implemented\n", __func__);
535 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
537 log_err("%s is not implemented\n", __func__);
542 long long strtoll(const char *restrict str, char **restrict endptr,
545 return _strtoi64(str, endptr, base);
548 char *strsep(char **stringp, const char *delim)
550 char *orig = *stringp;
551 BOOL gotMatch = FALSE;
555 if (*stringp == NULL)
558 while ((*stringp)[i] != '\0') {
560 while (delim[j] != '\0') {
561 if ((*stringp)[i] == delim[j]) {
563 (*stringp)[i] = '\0';
564 *stringp = *stringp + i + 1;
581 int poll(struct pollfd fds[], nfds_t nfds, int timeout)
584 struct timeval *to = NULL;
585 fd_set readfds, writefds, exceptfds;
592 to->tv_sec = timeout / 1000;
593 to->tv_usec = (timeout % 1000) * 1000;
599 for (i = 0; i < nfds; i++)
606 if (fds[i].events & POLLIN)
607 FD_SET(fds[i].fd, &readfds);
609 if (fds[i].events & POLLOUT)
610 FD_SET(fds[i].fd, &writefds);
612 FD_SET(fds[i].fd, &exceptfds);
615 rc = select(nfds, &readfds, &writefds, &exceptfds, to);
617 if (rc != SOCKET_ERROR) {
618 for (i = 0; i < nfds; i++)
624 if ((fds[i].events & POLLIN) && FD_ISSET(fds[i].fd, &readfds))
625 fds[i].revents |= POLLIN;
627 if ((fds[i].events & POLLOUT) && FD_ISSET(fds[i].fd, &writefds))
628 fds[i].revents |= POLLOUT;
630 if (FD_ISSET(fds[i].fd, &exceptfds))
631 fds[i].revents |= POLLHUP;
638 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
642 DWORD ms_total = (rqtp->tv_sec * 1000) + (rqtp->tv_nsec / 1000000.0);
647 ms_remaining = ms_total;
649 /* Since Sleep() can sleep for less than the requested time, add a loop to
650 ensure we only return after the requested length of time has elapsed */
652 fio_gettime(&tv, NULL);
654 ms_remaining = ms_total - mtime_since_now(&tv);
655 } while (ms_remaining > 0 && ms_remaining < ms_total);
657 /* this implementation will never sleep for less than the requested time */
666 DIR *opendir(const char *dirname)
668 log_err("%s is not implemented\n", __func__);
673 int closedir(DIR *dirp)
675 log_err("%s is not implemented\n", __func__);
680 struct dirent *readdir(DIR *dirp)
682 log_err("%s is not implemented\n", __func__);
689 log_err("%s is not implemented\n", __func__);
694 int inet_aton(char *addr)
696 log_err("%s is not implemented\n", __func__);
701 const char* inet_ntop(int af, const void *restrict src,
702 char *restrict dst, socklen_t size)
704 INT status = SOCKET_ERROR;
708 if (af != AF_INET && af != AF_INET6) {
709 errno = EAFNOSUPPORT;
713 WSAStartup(MAKEWORD(2,2), &wsd);
716 struct sockaddr_in si;
718 memset(&si, 0, sizeof(si));
720 memcpy(&si.sin_addr, src, sizeof(si.sin_addr));
721 status = WSAAddressToString((struct sockaddr*)&si, sizeof(si), NULL, dst, &len);
722 } else if (af == AF_INET6) {
723 struct sockaddr_in6 si6;
725 memset(&si6, 0, sizeof(si6));
726 si6.sin6_family = af;
727 memcpy(&si6.sin6_addr, src, sizeof(si6.sin6_addr));
728 status = WSAAddressToString((struct sockaddr*)&si6, sizeof(si6), NULL, dst, &len);
731 if (status != SOCKET_ERROR)
740 int inet_pton(int af, const char *restrict src, void *restrict dst)
742 INT status = SOCKET_ERROR;
746 if (af != AF_INET && af != AF_INET6) {
747 errno = EAFNOSUPPORT;
751 WSAStartup(MAKEWORD(2,2), &wsd);
754 struct sockaddr_in si;
755 INT len = sizeof(si);
756 memset(&si, 0, sizeof(si));
758 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si, &len);
759 if (status != SOCKET_ERROR)
760 memcpy(dst, &si.sin_addr, sizeof(si.sin_addr));
761 } else if (af == AF_INET6) {
762 struct sockaddr_in6 si6;
763 INT len = sizeof(si6);
764 memset(&si6, 0, sizeof(si6));
765 si6.sin6_family = af;
766 status = WSAStringToAddressA((char*)src, af, NULL, (struct sockaddr*)&si6, &len);
767 if (status != SOCKET_ERROR)
768 memcpy(dst, &si6.sin6_addr, sizeof(si6.sin6_addr));
771 if (status == SOCKET_ERROR) {