]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Implement cpu plugin for NetBSD
authorEdgar Fuß <ef@math.uni-bonn.de>
Wed, 8 Jul 2020 17:10:22 +0000 (19:10 +0200)
committerGitHub <noreply@github.com>
Wed, 8 Jul 2020 17:10:22 +0000 (19:10 +0200)
Implement the cpu plugin for NetBSD
Written by Håvard Eidnes <he@NetBSD.org>

src/cpu.c

index 8c1d4cbd4158ea9f8d313c35b4a27a8483b03f02..0e0a6e8013ae5e1d013fd885a77fa9944e1d156e 100644 (file)
--- a/src/cpu.c
+++ b/src/cpu.c
 #endif
 #endif /* HAVE_SYS_DKSTAT_H */
 
-#define CAN_USE_SYSCTL 0
 #if (defined(HAVE_SYSCTL) && defined(HAVE_SYSCTLBYNAME)) || defined(__OpenBSD__)
 /* Implies BSD variant */
 #if defined(CTL_HW) && defined(HW_NCPU) && defined(CTL_KERN) &&                \
-    defined(KERN_CPTIME) && defined(CPUSTATES)
+    (defined(KERN_CPTIME) || defined(KERN_CP_TIME)) && defined(CPUSTATES)
 #define CAN_USE_SYSCTL 1
+#else
+#define CAN_USE_SYSCTL 0
 #endif
+#else
+#define CAN_USE_SYSCTL 0
 #endif /* HAVE_SYSCTL_H && HAVE_SYSCTLBYNAME || __OpenBSD__ */
 
 #define COLLECTD_CPU_STATE_USER 0
@@ -746,6 +749,24 @@ static int cpu_read(void) {
 
   memset(cpuinfo, 0, sizeof(cpuinfo));
 
+#if defined(KERN_CP_TIME) && defined(KERNEL_NETBSD)
+  {
+    int mib[] = {CTL_KERN, KERN_CP_TIME};
+
+    cpuinfo_size = sizeof(cpuinfo[0]) * numcpu * CPUSTATES;
+    status = sysctl(mib, 2, cpuinfo, &cpuinfo_size, NULL, 0);
+    if (status == -1) {
+      char errbuf[1024];
+
+      ERROR ("cpu plugin: sysctl failed: %s.",
+             sstrerror (errno, errbuf, sizeof (errbuf)));
+      return -1;
+    }
+    if (cpuinfo_size == (sizeof(cpuinfo[0]) * CPUSTATES)) {
+      numcpu = 1;
+    }
+  }
+#else /* defined(KERN_CP_TIME) && defined(KERNEL_NETBSD) */
 #if defined(KERN_CPTIME2)
   if (numcpu > 1) {
     for (int i = 0; i < numcpu; i++) {
@@ -779,6 +800,7 @@ static int cpu_read(void) {
       cpuinfo[0][i] = cpuinfo_tmp[i];
     }
   }
+#endif /* defined(KERN_CP_TIME) && defined(KERNEL_NETBSD) */
 
   for (int i = 0; i < numcpu; i++) {
     cpu_stage(i, COLLECTD_CPU_STATE_USER, (derive_t)cpuinfo[i][CP_USER], now);