Windows: fix sysconf(_SC_PHYS_PAGES).
authorBruce Cran <bruce@cran.org.uk>
Wed, 6 Feb 2013 20:27:27 +0000 (20:27 +0000)
committerJens Axboe <axboe@kernel.dk>
Thu, 7 Feb 2013 06:22:00 +0000 (07:22 +0100)
sysconf(_SC_PHYS_PAGES) was returning the number of bytes of memory
instead of the number of pages. Also improve some error messages in
sysconf(). Update os_phys_mem() to use sysconf().

Signed-off-by: Jens Axboe <axboe@kernel.dk>
os/os-windows.h
os/windows/posix.c

index 98f9030530ef23db3536d67ae38f4f6abcedeebd..09f9c5438d2ff1580d16ab2b5586da488866b648 100644 (file)
@@ -151,12 +151,14 @@ static inline int blockdev_invalidate_cache(struct fio_file *f)
 
 static inline unsigned long long os_phys_mem(void)
 {
-       SYSTEM_INFO sysInfo;
-       uintptr_t addr;
+       long pagesize, pages;
 
-       GetSystemInfo(&sysInfo);
-       addr = (uintptr_t)sysInfo.lpMaximumApplicationAddress;
-       return (unsigned long long)addr;
+       pagesize = sysconf(_SC_PAGESIZE);
+       pages = sysconf(_SC_PHYS_PAGES);
+       if (pages == -1 || pagesize == -1)
+               return 0;
+
+       return (unsigned long long) pages * (unsigned long long) pagesize;
 }
 
 static inline void os_get_tmpdir(char *path, int len)
index de679111aaa300e36918fe58aa35a263ccca0546..8a52a6849e4b4083b5d6ba0a6c787346714890a7 100755 (executable)
@@ -79,6 +79,7 @@ int GetNumLogicalProcessors(void)
 long sysconf(int name)
 {
        long val = -1;
+       long val2 = -1;
        SYSTEM_INFO sysInfo;
        MEMORYSTATUSEX status;
 
@@ -87,7 +88,7 @@ long sysconf(int name)
        case _SC_NPROCESSORS_ONLN:
                val = GetNumLogicalProcessors();
                if (val == -1)
-                       log_err("_SC_NPROCESSORS_ONLN failed\n");
+                       log_err("sysconf(_SC_NPROCESSORS_ONLN) failed\n");
 
                break;
 
@@ -98,8 +99,11 @@ long sysconf(int name)
 
        case _SC_PHYS_PAGES:
                status.dwLength = sizeof(status);
-               GlobalMemoryStatusEx(&status);
-               val = status.ullTotalPhys;
+               val2 = sysconf(_SC_PAGESIZE);
+               if (GlobalMemoryStatusEx(&status) && val2 != -1)
+                       val = status.ullTotalPhys / val2;
+               else
+                       log_err("sysconf(_SC_PHYS_PAGES) failed\n");
                break;
        default:
                log_err("sysconf(%d) is not implemented\n", name);
@@ -678,40 +682,40 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
 
 DIR *opendir(const char *dirname)
 {
-    struct dirent_ctx *dc = NULL;
-
-    /* See if we can open it. If not, we'll return an error here */
-    HANDLE file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
-    if (file != INVALID_HANDLE_VALUE) {
-        CloseHandle(file);
-        dc = (struct dirent_ctx*)malloc(sizeof(struct dirent_ctx));
-        StringCchCopyA(dc->dirname, MAX_PATH, dirname);
-        dc->find_handle = INVALID_HANDLE_VALUE;
-    } else {
-        DWORD error = GetLastError();
-        if (error == ERROR_FILE_NOT_FOUND)
-            errno = ENOENT;
-
-        else if (error == ERROR_PATH_NOT_FOUND)
-            errno = ENOTDIR;
-        else if (error == ERROR_TOO_MANY_OPEN_FILES)
-            errno = ENFILE;
-        else if (error == ERROR_ACCESS_DENIED)
-            errno = EACCES;
-        else
-            errno = error;
-    }
-
-    return dc;
+       struct dirent_ctx *dc = NULL;
+
+       /* See if we can open it. If not, we'll return an error here */
+       HANDLE file = CreateFileA(dirname, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
+       if (file != INVALID_HANDLE_VALUE) {
+               CloseHandle(file);
+               dc = (struct dirent_ctx*)malloc(sizeof(struct dirent_ctx));
+               StringCchCopyA(dc->dirname, MAX_PATH, dirname);
+               dc->find_handle = INVALID_HANDLE_VALUE;
+       } else {
+               DWORD error = GetLastError();
+               if (error == ERROR_FILE_NOT_FOUND)
+                       errno = ENOENT;
+
+               else if (error == ERROR_PATH_NOT_FOUND)
+                       errno = ENOTDIR;
+               else if (error == ERROR_TOO_MANY_OPEN_FILES)
+                       errno = ENFILE;
+               else if (error == ERROR_ACCESS_DENIED)
+                       errno = EACCES;
+               else
+                       errno = error;
+       }
+
+       return dc;
 }
 
 int closedir(DIR *dirp)
 {
-    if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE)
-        FindClose(dirp->find_handle);
+       if (dirp != NULL && dirp->find_handle != INVALID_HANDLE_VALUE)
+               FindClose(dirp->find_handle);
 
-    free(dirp);
-    return 0;
+       free(dirp);
+       return 0;
 }
 
 struct dirent *readdir(DIR *dirp)