Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-gnome
Path: blob/main/benchmarks/apib/files/patch-src_apib__cpu.c
16125 views
1
--- src/apib_cpu.c.orig 2017-08-28 22:38:37 UTC
2
+++ src/apib_cpu.c
3
@@ -14,6 +14,11 @@
4
limitations under the License.
5
*/
6
7
+#ifdef __FreeBSD__
8
+#include <sys/types.h>
9
+#include <sys/sysctl.h>
10
+#include <sys/times.h>
11
+#endif
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <string.h>
15
@@ -35,6 +40,9 @@ static double TicksPerSecond;
16
* for hyperthreading, etc. */
17
int cpu_Count(apr_pool_t* pool)
18
{
19
+#ifdef _SC_NPROCESSORS_ONLN
20
+ return (int)sysconf(_SC_NPROCESSORS_ONLN);
21
+#else
22
apr_status_t s;
23
apr_file_t* f;
24
char buf[PROC_BUF_LEN];
25
@@ -66,10 +74,20 @@ int cpu_Count(apr_pool_t* pool)
26
count = 1;
27
}
28
return count;
29
+#endif
30
}
31
32
static int getTicks(CPUUsage* cpu, apr_pool_t* pool)
33
{
34
+#ifdef __FreeBSD__
35
+ struct tms ticks;
36
+
37
+ cpu->idle = times(&ticks);
38
+ if (cpu->idle == -1)
39
+ return 0;
40
+ cpu->nonIdle = ticks.tms_utime + ticks.tms_stime;
41
+ return 1;
42
+#else
43
apr_status_t s;
44
apr_file_t* proc;
45
char buf[PROC_BUF_LEN];
46
@@ -118,10 +136,12 @@ static int getTicks(CPUUsage* cpu, apr_pool_t* pool)
47
}
48
49
return 0;
50
+#endif
51
}
52
53
double cpu_GetMemoryUsage(apr_pool_t* pool)
54
{
55
+#ifdef __linux__
56
apr_status_t s;
57
apr_file_t* proc;
58
char buf[PROC_BUF_LEN];
59
@@ -138,12 +158,36 @@ double cpu_GetMemoryUsage(apr_pool_t* pool)
60
if (s != APR_SUCCESS) {
61
return 0.0;
62
}
63
+#endif
64
65
long totalMem = 0;
66
long freeMem = 0;
67
long buffers = 0;
68
long cache = 0;
69
70
+#ifdef __FreeBSD__
71
+ /* We work with kilobytes to match Linux' /proc/meminfo. */
72
+ long pagesize = sysconf(_SC_PAGESIZE) / 1024;
73
+ totalMem = sysconf(_SC_PHYS_PAGES) * pagesize;
74
+
75
+ size_t len;
76
+
77
+ unsigned free;
78
+ len = sizeof(free);
79
+ sysctlbyname("vm.stats.vm.v_free_count", &free, &len, NULL, 0);
80
+ freeMem = free * pagesize;
81
+
82
+ /* `buffers' is of expected type (long), no need for another variable. */
83
+ len = sizeof(buffers);
84
+ sysctlbyname("vfs.bufspace", &buffers, &len, NULL, 0);
85
+ buffers /= 1024;
86
+
87
+ /* `cache' is based on number of inactive pages since r309017. */
88
+ unsigned inact;
89
+ len = sizeof(inact);
90
+ sysctlbyname("vm.stats.vm.v_inactive_count", &inact, &len, NULL, 0);
91
+ cache = inact * pagesize;
92
+#else
93
while (linep_NextLine(&line)) {
94
char* n = linep_NextToken(&line, " ");
95
char* v = linep_NextToken(&line, " ");
96
@@ -158,6 +202,7 @@ double cpu_GetMemoryUsage(apr_pool_t* pool)
97
cache = atol(v);
98
}
99
}
100
+#endif
101
102
if ((totalMem <= 0) || (freeMem <= 0)) {
103
return 0.0;
104
105