From 01d269552bca14c90bdcc2288e64ba2426c045ea Mon Sep 17 00:00:00 2001 From: Bruce Cran Date: Wed, 6 Feb 2013 20:27:27 +0000 Subject: [PATCH] Windows: fix sysconf(_SC_PHYS_PAGES). 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 --- os/os-windows.h | 12 ++++---- os/windows/posix.c | 68 ++++++++++++++++++++++++---------------------- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/os/os-windows.h b/os/os-windows.h index 98f90305..09f9c543 100644 --- a/os/os-windows.h +++ b/os/os-windows.h @@ -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) diff --git a/os/windows/posix.c b/os/windows/posix.c index de679111..8a52a684 100755 --- a/os/windows/posix.c +++ b/os/windows/posix.c @@ -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) -- 2.25.1