Path: blob/main/benchmarks/apib/files/patch-src_apib__cpu.c
16125 views
--- src/apib_cpu.c.orig 2017-08-28 22:38:37 UTC1+++ src/apib_cpu.c2@@ -14,6 +14,11 @@3limitations under the License.4*/56+#ifdef __FreeBSD__7+#include <sys/types.h>8+#include <sys/sysctl.h>9+#include <sys/times.h>10+#endif11#include <stdio.h>12#include <stdlib.h>13#include <string.h>14@@ -35,6 +40,9 @@ static double TicksPerSecond;15* for hyperthreading, etc. */16int cpu_Count(apr_pool_t* pool)17{18+#ifdef _SC_NPROCESSORS_ONLN19+ return (int)sysconf(_SC_NPROCESSORS_ONLN);20+#else21apr_status_t s;22apr_file_t* f;23char buf[PROC_BUF_LEN];24@@ -66,10 +74,20 @@ int cpu_Count(apr_pool_t* pool)25count = 1;26}27return count;28+#endif29}3031static int getTicks(CPUUsage* cpu, apr_pool_t* pool)32{33+#ifdef __FreeBSD__34+ struct tms ticks;35+36+ cpu->idle = times(&ticks);37+ if (cpu->idle == -1)38+ return 0;39+ cpu->nonIdle = ticks.tms_utime + ticks.tms_stime;40+ return 1;41+#else42apr_status_t s;43apr_file_t* proc;44char buf[PROC_BUF_LEN];45@@ -118,10 +136,12 @@ static int getTicks(CPUUsage* cpu, apr_pool_t* pool)46}4748return 0;49+#endif50}5152double cpu_GetMemoryUsage(apr_pool_t* pool)53{54+#ifdef __linux__55apr_status_t s;56apr_file_t* proc;57char buf[PROC_BUF_LEN];58@@ -138,12 +158,36 @@ double cpu_GetMemoryUsage(apr_pool_t* pool)59if (s != APR_SUCCESS) {60return 0.0;61}62+#endif6364long totalMem = 0;65long freeMem = 0;66long buffers = 0;67long cache = 0;6869+#ifdef __FreeBSD__70+ /* We work with kilobytes to match Linux' /proc/meminfo. */71+ long pagesize = sysconf(_SC_PAGESIZE) / 1024;72+ totalMem = sysconf(_SC_PHYS_PAGES) * pagesize;73+74+ size_t len;75+76+ unsigned free;77+ len = sizeof(free);78+ sysctlbyname("vm.stats.vm.v_free_count", &free, &len, NULL, 0);79+ freeMem = free * pagesize;80+81+ /* `buffers' is of expected type (long), no need for another variable. */82+ len = sizeof(buffers);83+ sysctlbyname("vfs.bufspace", &buffers, &len, NULL, 0);84+ buffers /= 1024;85+86+ /* `cache' is based on number of inactive pages since r309017. */87+ unsigned inact;88+ len = sizeof(inact);89+ sysctlbyname("vm.stats.vm.v_inactive_count", &inact, &len, NULL, 0);90+ cache = inact * pagesize;91+#else92while (linep_NextLine(&line)) {93char* n = linep_NextToken(&line, " ");94char* v = linep_NextToken(&line, " ");95@@ -158,6 +202,7 @@ double cpu_GetMemoryUsage(apr_pool_t* pool)96cache = atol(v);97}98}99+#endif100101if ((totalMem <= 0) || (freeMem <= 0)) {102return 0.0;103104105