From 554442439dd0b55b82fbeec9169b45de4d6006a8 Mon Sep 17 00:00:00 2001 From: Gabor Kovesdan Date: Thu, 20 Dec 2012 22:30:40 +0000 Subject: [PATCH] - Change the memory heuristics to an actually working one Submitted by: Oleg Moskalenko Prodded by: kib --- usr.bin/sort/sort.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c index 2718d64ff099..69a5a7496af7 100644 --- a/usr.bin/sort/sort.c +++ b/usr.bin/sort/sort.c @@ -265,33 +265,27 @@ read_fns_from_file0(const char *fn) static void set_hw_params(void) { -#if defined(SORT_THREADS) - size_t ncpusz; -#endif - unsigned int pages, psize; - size_t psz, pszsz; + long pages, psize; pages = psize = 0; + #if defined(SORT_THREADS) ncpu = 1; - ncpusz = sizeof(size_t); #endif - psz = sizeof(pages); - pszsz = sizeof(psize); - if (sysctlbyname("vm.stats.vm.v_free_count", &pages, &psz, - NULL, 0) < 0) { - perror("vm.stats.vm.v_free_count"); - return; + pages = sysconf(_SC_PHYS_PAGES); + if (pages < 1) { + perror("sysconf pages"); + psize = 1; } - if (sysctlbyname("vm.stats.vm.v_page_size", &psize, &pszsz, - NULL, 0) < 0) { - perror("vm.stats.vm.v_page_size"); - return; + psize = sysconf(_SC_PAGESIZE); + if (psize < 1) { + perror("sysconf psize"); + psize = 4096; } #if defined(SORT_THREADS) - if (sysctlbyname("hw.ncpu", &ncpu, &ncpusz, - NULL, 0) < 0) + ncpu = (unsigned int)sysconf(_SC_NPROCESSORS_ONLN); + if (ncpu < 1) ncpu = 1; else if(ncpu > 32) ncpu = 32; @@ -300,7 +294,7 @@ set_hw_params(void) #endif free_memory = (unsigned long long) pages * (unsigned long long) psize; - available_free_memory = (free_memory * 9) / 10; + available_free_memory = free_memory / 2; if (available_free_memory < 1024) available_free_memory = 1024; @@ -1232,7 +1226,9 @@ main(int argc, char **argv) } if (debug_sort) { + printf("Memory to be used for sorting: %llu\n",available_free_memory); #if defined(SORT_THREADS) + printf("Number of CPUs: %d\n",(int)ncpu); nthreads = 1; #endif printf("Using collate rules of %s locale\n",