Implement nice() for Windows
[fio.git] / os / windows / posix.c
index 6a7841d5513d743b3451ec14dd94daf11f27b679..bbd93e979522e098d9a438e0ccfdc10da0b89ec0 100755 (executable)
 #include <unistd.h>
 #include <dirent.h>
 #include <pthread.h>
+#include <time.h>
 #include <semaphore.h>
 #include <sys/shm.h>
 #include <sys/mman.h>
 #include <sys/uio.h>
 #include <sys/resource.h>
 #include <sys/poll.h>
+#include <sys/wait.h>
+#include <setjmp.h>
 
 #include "../os-windows.h"
 #include "../../lib/hweight.h"
@@ -226,6 +229,32 @@ char *dlerror(void)
        return dl_error;
 }
 
+/* Copied from http://blogs.msdn.com/b/joshpoley/archive/2007/12/19/date-time-formats-and-conversions.aspx */
+void Time_tToSystemTime(time_t dosTime, SYSTEMTIME *systemTime)
+{
+    FILETIME utcFT;
+    LONGLONG jan1970;
+
+    jan1970 = Int32x32To64(dosTime, 10000000) + 116444736000000000;
+    utcFT.dwLowDateTime = (DWORD)jan1970;
+    utcFT.dwHighDateTime = jan1970 >> 32;
+
+    FileTimeToSystemTime((FILETIME*)&utcFT, systemTime);
+}
+
+char* ctime_r(const time_t *t, char *buf)
+{
+    SYSTEMTIME systime;
+    const char * const dayOfWeek[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
+    const char * const monthOfYear[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
+
+    Time_tToSystemTime(*t, &systime);
+    /* We don't know how long `buf` is, but assume it's rounded up from the minimum of 25 to 32 */
+    StringCchPrintfA(buf, 31, "%s %s %d %02d:%02d:%02d %04d\n", dayOfWeek[systime.wDayOfWeek % 7], monthOfYear[(systime.wMonth - 1) % 12],
+                                                                                systime.wDay, systime.wHour, systime.wMinute, systime.wSecond, systime.wYear);
+    return buf;
+}
+
 int gettimeofday(struct timeval *restrict tp, void *restrict tzp)
 {
        FILETIME fileTime;
@@ -421,6 +450,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
        {
                static LARGE_INTEGER freq = {{0,0}};
                LARGE_INTEGER counts;
+               uint64_t t;
 
                QueryPerformanceCounter(&counts);
                if (freq.QuadPart == 0)
@@ -429,7 +459,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
                tp->tv_sec = counts.QuadPart / freq.QuadPart;
                /* Get the difference between the number of ns stored
                 * in 'tv_sec' and that stored in 'counts' */
-               uint64_t t = tp->tv_sec * freq.QuadPart;
+               t = tp->tv_sec * freq.QuadPart;
                t = counts.QuadPart - t;
                /* 't' now contains the number of cycles since the last second.
                 * We want the number of nanoseconds, so multiply out by 1,000,000,000
@@ -617,10 +647,19 @@ int setgid(gid_t gid)
 
 int nice(int incr)
 {
-       if (incr != 0) {
-               errno = EINVAL;
-               return -1;
-       }
+       DWORD prioclass = NORMAL_PRIORITY_CLASS;
+       
+       if (incr < -15)
+               prioclass = HIGH_PRIORITY_CLASS;
+       else if (incr < 0)
+               prioclass = ABOVE_NORMAL_PRIORITY_CLASS;
+       else if (incr > 15)
+               prioclass = IDLE_PRIORITY_CLASS;
+       else if (incr > 0)
+               prioclass = BELOW_NORMAL_PRIORITY_CLASS;
+       
+       if (!SetPriorityClass(GetCurrentProcess(), prioclass))
+               log_err("fio: SetPriorityClass failed\n");
 
        return 0;
 }
@@ -661,11 +700,6 @@ int getrusage(int who, struct rusage *r_usage)
        return 0;
 }
 
-int posix_fadvise(int fd, off_t offset, off_t len, int advice)
-{
-       return 0;
-}
-
 int posix_madvise(void *addr, size_t len, int advice)
 {
        log_err("%s is not implemented\n", __func__);
@@ -710,9 +744,22 @@ ssize_t readv(int fildes, const struct iovec *iov, int iovcnt)
 
 ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
 {
-       log_err("%s is not implemented\n", __func__);
-       errno = ENOSYS;
-       return -1;
+       int i;
+       DWORD bytes_written = 0;
+       for (i = 0; i < iovcnt; i++)
+       {
+               int len = send((SOCKET)fildes, iov[i].iov_base, iov[i].iov_len, 0);
+               if (len == SOCKET_ERROR)
+               {
+                       DWORD err = GetLastError();
+                       errno = win_to_posix_error(err);
+                       bytes_written = -1;
+                       break;
+               }
+               bytes_written += len;
+       }
+
+       return bytes_written;
 }
 
 long long strtoll(const char *restrict str, char **restrict endptr,
@@ -754,7 +801,6 @@ int poll(struct pollfd fds[], nfds_t nfds, int timeout)
 
                FD_SET(fds[i].fd, &exceptfds);
        }
-
        rc = select(nfds, &readfds, &writefds, &exceptfds, to);
 
        if (rc != SOCKET_ERROR) {
@@ -774,7 +820,6 @@ int poll(struct pollfd fds[], nfds_t nfds, int timeout)
                                fds[i].revents |= POLLHUP;
                }
        }
-
        return rc;
 }
 
@@ -854,7 +899,7 @@ struct dirent *readdir(DIR *dirp)
 
        if (dirp->find_handle == INVALID_HANDLE_VALUE) {
                char search_pattern[MAX_PATH];
-               StringCchPrintfA(search_pattern, MAX_PATH, "%s\\*", dirp->dirname);
+               StringCchPrintfA(search_pattern, MAX_PATH-1, "%s\\*", dirp->dirname);
                dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
                if (dirp->find_handle == INVALID_HANDLE_VALUE)
                        return NULL;
@@ -876,6 +921,14 @@ uid_t geteuid(void)
        return -1;
 }
 
+in_addr_t inet_network(const char *cp)
+{
+       in_addr_t hbo;
+       in_addr_t nbo = inet_addr(cp);
+       hbo = ((nbo & 0xFF) << 24) + ((nbo & 0xFF00) << 8) + ((nbo & 0xFF0000) >> 8) + ((nbo & 0xFF000000) >> 24);
+       return hbo;
+}
+
 const char* inet_ntop(int af, const void *restrict src,
                char *restrict dst, socklen_t size)
 {