Added
Link Here
|
1 |
diff --git src/modules/cpu.cpp src/modules/cpu.cpp |
2 |
index 527f27fb..179d9221 100644 |
3 |
--- src/modules/cpu.cpp |
4 |
+++ src/modules/cpu.cpp |
5 |
@@ -2,6 +2,11 @@ |
6 |
|
7 |
#include <fstream> |
8 |
#include <istream> |
9 |
+#ifdef __FreeBSD__ |
10 |
+ #include <sys/types.h> |
11 |
+ #include <sys/resource.h> |
12 |
+ #include <sys/sysctl.h> |
13 |
+#endif |
14 |
|
15 |
#include "drawtypes/label.hpp" |
16 |
#include "drawtypes/progressbar.hpp" |
17 |
@@ -128,6 +133,41 @@ namespace modules { |
18 |
m_cputimes.clear(); |
19 |
|
20 |
try { |
21 |
+#ifdef __FreeBSD__ |
22 |
+ // Get number of CPUs |
23 |
+ // ToDo: No need to do this on every invocation. |
24 |
+ int ncpu = -1; |
25 |
+ std::size_t sz = sizeof(ncpu); |
26 |
+ if (sysctlbyname("hw.ncpu", &ncpu, &sz, nullptr, 0) != 0) { |
27 |
+ m_log.err("Failed to query sysctl 'hw.ncpu' (errno: %s)", strerror(errno)); |
28 |
+ return false; |
29 |
+ } |
30 |
+ if (ncpu < 1) { |
31 |
+ m_log.err("Failed to determine number of CPUs."); |
32 |
+ return false; |
33 |
+ } |
34 |
+ |
35 |
+ // Query 'kern.cp_time' |
36 |
+ long cpu_stat[CPUSTATES]; |
37 |
+ sz = sizeof(cpu_stat); |
38 |
+ if (sysctlbyname("kern.cp_time", cpu_stat, &sz, nullptr, 0) != 0) { |
39 |
+ m_log.err("Failed to query sysctl 'kern.cp_time' (errno: %s)", strerror(errno)); |
40 |
+ return false; |
41 |
+ } |
42 |
+ |
43 |
+ // Parse |
44 |
+ static std::size_t field_offset = sizeof(*cpu_stat) + ncpu; |
45 |
+ for (std::size_t i = 0; i < ncpu; i++) { |
46 |
+ m_cputimes.emplace_back(new cpu_time); |
47 |
+ m_cputimes.back()->user = cpu_stat[CP_USER]; |
48 |
+ m_cputimes.back()->nice = cpu_stat[CP_NICE]; |
49 |
+ m_cputimes.back()->system = cpu_stat[CP_SYS]; |
50 |
+ m_cputimes.back()->steal = cpu_stat[CP_INTR]; // Note: This is technically the reported "interrupt" time |
51 |
+ m_cputimes.back()->idle = cpu_stat[CP_IDLE]; |
52 |
+ m_cputimes.back()->total = m_cputimes.back()->user + m_cputimes.back()->nice + m_cputimes.back()->system + |
53 |
+ m_cputimes.back()->idle + m_cputimes.back()->steal; |
54 |
+ } |
55 |
+#else |
56 |
std::ifstream in(PATH_CPU_INFO); |
57 |
string str; |
58 |
|
59 |
@@ -148,6 +188,7 @@ namespace modules { |
60 |
m_cputimes.back()->total = m_cputimes.back()->user + m_cputimes.back()->nice + m_cputimes.back()->system + |
61 |
m_cputimes.back()->idle + m_cputimes.back()->steal; |
62 |
} |
63 |
+#endif |
64 |
} catch (const std::ios_base::failure& e) { |
65 |
m_log.err("Failed to read CPU values (what: %s)", e.what()); |
66 |
} |