]> wimlib.net Git - wimlib/commitdiff
Move CPU and memory information to util
authorEric Biggers <ebiggers3@gmail.com>
Sat, 2 May 2015 22:54:04 +0000 (17:54 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Sat, 2 May 2015 23:13:41 +0000 (18:13 -0500)
include/wimlib/util.h
include/wimlib/win32.h
src/compress_parallel.c
src/util.c
src/win32_replacements.c

index 67044638d825f6bf32fabb41102815be0ab7276f..7f34cbf39721f0a6e411595f568474bf5db5f6c9 100644 (file)
@@ -117,4 +117,14 @@ cmp_u64(u64 n1, u64 n2)
        return 0;
 }
 
+/************************
+ * System information
+ ************************/
+
+unsigned
+get_available_cpus(void);
+
+u64
+get_available_memory(void);
+
 #endif /* _WIMLIB_UTIL_H */
index dd3c5c9c1c24dd72cc0c2ece75649d164b3e391b..c76a7ff5ee729b746ffe8553be4357bac4dd16fc 100644 (file)
@@ -27,12 +27,6 @@ win32_global_cleanup(void);
 extern int
 fsync(int fd);
 
-extern unsigned
-win32_get_number_of_processors(void);
-
-extern u64
-win32_get_avail_memory(void);
-
 extern tchar *
 realpath(const tchar *path, tchar *resolved_path);
 
index 9b3c8521e34e87a52936b7c9af1cce1d0e5dd973..511ef1b84ee73451048c21020cf9af7845f4a8b9 100644 (file)
 #ifdef ENABLE_MULTITHREADED_COMPRESSION
 
 #include <errno.h>
-#include <limits.h>
 #include <pthread.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-#ifdef HAVE_SYS_SYSCTL_H
-#  include <sys/sysctl.h>
-#endif
 
 #include "wimlib/assert.h"
 #include "wimlib/chunk_compressor.h"
 #include "wimlib/error.h"
 #include "wimlib/list.h"
 #include "wimlib/util.h"
-#include "wimlib/win32.h" /* win32_get_number_of_processors() */
 
 struct message_queue {
        struct list_head list;
@@ -92,49 +86,7 @@ struct parallel_chunk_compressor {
        size_t next_chunk_idx;
 };
 
-static unsigned
-get_default_num_threads(void)
-{
-       long n;
-#ifdef __WIN32__
-       n = win32_get_number_of_processors();
-#else
-       n = sysconf(_SC_NPROCESSORS_ONLN);
-#endif
-       if (n < 1 || n >= UINT_MAX) {
-               WARNING("Failed to determine number of processors; assuming 1.");
-               return 1;
-       }
-       return n;
-}
 
-static u64
-get_avail_memory(void)
-{
-#ifdef __WIN32__
-       u64 phys_bytes = win32_get_avail_memory();
-       if (phys_bytes == 0)
-               goto default_size;
-       return phys_bytes;
-#elif defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES)
-       long page_size = sysconf(_SC_PAGESIZE);
-       long num_pages = sysconf(_SC_PHYS_PAGES);
-       if (page_size <= 0 || num_pages <= 0)
-               goto default_size;
-       return ((u64)page_size * (u64)num_pages);
-#else
-       int mib[2] = {CTL_HW, HW_MEMSIZE};
-       u64 memsize;
-       size_t len = sizeof(memsize);
-       if (sysctl(mib, ARRAY_LEN(mib), &memsize, &len, NULL, 0) < 0 || len != 8)
-               goto default_size;
-       return memsize;
-#endif
-
-default_size:
-       WARNING("Failed to determine available memory; assuming 1 GiB");
-       return 1ULL << 30;
-}
 
 static int
 message_queue_init(struct message_queue *q)
@@ -423,7 +375,7 @@ new_parallel_chunk_compressor(int out_ctype, u32 out_chunk_size,
        wimlib_assert(out_chunk_size > 0);
 
        if (num_threads == 0)
-               num_threads = get_default_num_threads();
+               num_threads = get_available_cpus();
 
        if (num_threads == 1) {
                DEBUG("Only 1 thread; Not bothering with "
@@ -432,7 +384,7 @@ new_parallel_chunk_compressor(int out_ctype, u32 out_chunk_size,
        }
 
        if (max_memory == 0)
-               max_memory = get_avail_memory();
+               max_memory = get_available_memory();
 
        desired_num_threads = num_threads;
 
index 242280961d634fbcee28def2272df4ef64dec741..f22d451e6e8f4b4fd08c5a023dbaecb90ea62017 100644 (file)
 #  include "config.h"
 #endif
 
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#ifdef HAVE_SYS_SYSCTL_H
+#  include <sys/sysctl.h>
+#endif
+#include <unistd.h>
 
 #include "wimlib.h"
 #include "wimlib/assert.h"
+#include "wimlib/error.h"
 #include "wimlib/timestamp.h"
 #include "wimlib/util.h"
 #include "wimlib/xml.h"
@@ -205,3 +211,41 @@ randomize_byte_array(u8 *p, size_t n)
        while (n--)
                *p++ = rand();
 }
+
+#ifndef __WIN32__
+unsigned
+get_available_cpus(void)
+{
+       long n = sysconf(_SC_NPROCESSORS_ONLN);
+       if (n < 1 || n >= UINT_MAX) {
+               WARNING("Failed to determine number of processors; assuming 1.");
+               return 1;
+       }
+       return n;
+}
+#endif /* !__WIN32__ */
+
+#ifndef __WIN32__
+u64
+get_available_memory(void)
+{
+#if defined(_SC_PAGESIZE) && defined(_SC_PHYS_PAGES)
+       long page_size = sysconf(_SC_PAGESIZE);
+       long num_pages = sysconf(_SC_PHYS_PAGES);
+       if (page_size <= 0 || num_pages <= 0)
+               goto default_size;
+       return ((u64)page_size * (u64)num_pages);
+#else
+       int mib[2] = {CTL_HW, HW_MEMSIZE};
+       u64 memsize;
+       size_t len = sizeof(memsize);
+       if (sysctl(mib, ARRAY_LEN(mib), &memsize, &len, NULL, 0) < 0 || len != 8)
+               goto default_size;
+       return memsize;
+#endif
+
+default_size:
+       WARNING("Failed to determine available memory; assuming 1 GiB");
+       return (u64)1 << 30;
+}
+#endif /* !__WIN32__ */
index 89289b0e056ab708a3696bee00094073c69876db..cd309eb4b734ee2203f9b4684d2b1825ddb598f2 100644 (file)
@@ -339,7 +339,7 @@ err:
 
 /* Use the Win32 API to get the number of processors.  */
 unsigned
-win32_get_number_of_processors(void)
+get_available_cpus(void)
 {
        SYSTEM_INFO sysinfo;
        GetSystemInfo(&sysinfo);
@@ -348,7 +348,7 @@ win32_get_number_of_processors(void)
 
 /* Use the Win32 API to get the amount of available memory.  */
 u64
-win32_get_avail_memory(void)
+get_available_memory(void)
 {
        MEMORYSTATUSEX status = {
                .dwLength = sizeof(status),