summaryrefslogtreecommitdiffstats
path: root/ksysguard/ksysguardd/OpenBSD
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commit4aed2c8219774f5d797760606b8489a92ddc5163 (patch)
tree3f8c130f7d269626bf6a9447407ef6c35954426a /ksysguard/ksysguardd/OpenBSD
downloadtdebase-4aed2c8219774f5d797760606b8489a92ddc5163.tar.gz
tdebase-4aed2c8219774f5d797760606b8489a92ddc5163.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebase@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'ksysguard/ksysguardd/OpenBSD')
-rw-r--r--ksysguard/ksysguardd/OpenBSD/Makefile.am6
-rw-r--r--ksysguard/ksysguardd/OpenBSD/cpu.c209
-rw-r--r--ksysguard/ksysguardd/OpenBSD/cpu.h51
-rw-r--r--ksysguard/ksysguardd/OpenBSD/memory.c207
-rw-r--r--ksysguard/ksysguardd/OpenBSD/memory.h43
5 files changed, 516 insertions, 0 deletions
diff --git a/ksysguard/ksysguardd/OpenBSD/Makefile.am b/ksysguard/ksysguardd/OpenBSD/Makefile.am
new file mode 100644
index 000000000..78d97e293
--- /dev/null
+++ b/ksysguard/ksysguardd/OpenBSD/Makefile.am
@@ -0,0 +1,6 @@
+AM_CFLAGS = -Wall
+
+INCLUDES = -I$(srcdir)/../../CContLib -I$(srcdir)/..
+
+noinst_LIBRARIES = libksysguardd.a
+libksysguardd_a_SOURCES = cpu.c memory.c
diff --git a/ksysguard/ksysguardd/OpenBSD/cpu.c b/ksysguard/ksysguardd/OpenBSD/cpu.c
new file mode 100644
index 000000000..3d1535ffe
--- /dev/null
+++ b/ksysguard/ksysguardd/OpenBSD/cpu.c
@@ -0,0 +1,209 @@
+/*
+ KSysGuard, the KDE System Guard
+
+ Copyright (c) 1999 Chris Schlaeger <cs@kde.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/dkstat.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+
+#include "cpu.h"
+#include "Command.h"
+#include "ksysguardd.h"
+
+long percentages(int cnt, int *out, long *new, long *old, long *diffs);
+
+unsigned long cp_time_offset;
+
+long cp_time[CPUSTATES];
+long cp_old[CPUSTATES];
+long cp_diff[CPUSTATES];
+int cpu_states[CPUSTATES];
+
+void
+initCpuInfo(struct SensorModul* sm)
+{
+ /* Total CPU load */
+ registerMonitor("cpu/user", "integer", printCPUUser,
+ printCPUUserInfo, sm);
+ registerMonitor("cpu/nice", "integer", printCPUNice,
+ printCPUNiceInfo, sm);
+ registerMonitor("cpu/sys", "integer", printCPUSys,
+ printCPUSysInfo, sm);
+ registerMonitor("cpu/idle", "integer", printCPUIdle,
+ printCPUIdleInfo, sm);
+ registerMonitor("cpu/interrupt", "integer", printCPUInterrupt,
+ printCPUInterruptInfo, sm);
+
+ updateCpuInfo();
+}
+
+void
+exitCpuInfo(void)
+{
+}
+
+int
+updateCpuInfo(void)
+{
+ static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME};
+ size_t size;
+ size=sizeof(cp_time);
+ sysctl(cp_time_mib, 2, &cp_time, &size, NULL, 0);
+ percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
+ return (0);
+}
+
+void
+printCPUUser(const char* cmd)
+{
+ fprintf(CurrentClient, "%d\n", cpu_states[CP_USER]/10);
+}
+
+void
+printCPUUserInfo(const char* cmd)
+{
+ fprintf(CurrentClient, "CPU User Load\t0\t100\t%%\n");
+}
+
+void
+printCPUNice(const char* cmd)
+{
+ fprintf(CurrentClient, "%d\n", cpu_states[CP_NICE]/10);
+}
+
+void
+printCPUNiceInfo(const char* cmd)
+{
+ fprintf(CurrentClient, "CPU Nice Load\t0\t100\t%%\n");
+}
+
+void
+printCPUSys(const char* cmd)
+{
+ fprintf(CurrentClient, "%d\n", cpu_states[CP_SYS]/10);
+}
+
+void
+printCPUSysInfo(const char* cmd)
+{
+ fprintf(CurrentClient, "CPU System Load\t0\t100\t%%\n");
+}
+
+void
+printCPUIdle(const char* cmd)
+{
+ fprintf(CurrentClient, "%d\n", cpu_states[CP_IDLE]/10);
+}
+
+void
+printCPUIdleInfo(const char* cmd)
+{
+ fprintf(CurrentClient, "CPU Idle Load\t0\t100\t%%\n");
+}
+
+void
+printCPUInterrupt(const char* cmd)
+{
+ fprintf(CurrentClient, "%d\n", cpu_states[CP_INTR]/10);
+}
+
+void
+printCPUInterruptInfo(const char* cmd)
+{
+ fprintf(CurrentClient, "CPU Interrupt Load\t0\t100\t%%\n");
+}
+
+/* The part ripped from top... */
+/*
+ * Top users/processes display for Unix
+ * Version 3
+ *
+ * This program may be freely redistributed,
+ * but this entire comment MUST remain intact.
+ *
+ * Copyright (c) 1984, 1989, William LeFebvre, Rice University
+ * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
+ */
+
+/*
+ * percentages(cnt, out, new, old, diffs) - calculate percentage change
+ * between array "old" and "new", putting the percentages i "out".
+ * "cnt" is size of each array and "diffs" is used for scratch space.
+ * The array "old" is updated on each call.
+ * The routine assumes modulo arithmetic. This function is especially
+ * useful on BSD mchines for calculating cpu state percentages.
+ */
+
+long percentages(cnt, out, new, old, diffs)
+
+int cnt;
+int *out;
+register long *new;
+register long *old;
+long *diffs;
+
+{
+ register int i;
+ register long change;
+ register long total_change;
+ register long *dp;
+ long half_total;
+
+ /* initialization */
+ total_change = 0;
+ dp = diffs;
+
+ /* calculate changes for each state and the overall change */
+ for (i = 0; i < cnt; i++)
+ {
+ if ((change = *new - *old) < 0)
+ {
+ /* this only happens when the counter wraps */
+ change = (int)
+ ((unsigned long)*new-(unsigned long)*old);
+ }
+ total_change += (*dp++ = change);
+ *old++ = *new++;
+ }
+
+ /* avoid divide by zero potential */
+ if (total_change == 0)
+ {
+ total_change = 1;
+ }
+
+ /* calculate percentages based on overall change, rounding up */
+ half_total = total_change / 2l;
+
+ /* Do not divide by 0. Causes Floating point exception */
+ if(total_change) {
+ for (i = 0; i < cnt; i++)
+ {
+ *out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
+ }
+ }
+
+ /* return the total in case the caller wants to use it */
+ return(total_change);
+}
diff --git a/ksysguard/ksysguardd/OpenBSD/cpu.h b/ksysguard/ksysguardd/OpenBSD/cpu.h
new file mode 100644
index 000000000..a1188cd82
--- /dev/null
+++ b/ksysguard/ksysguardd/OpenBSD/cpu.h
@@ -0,0 +1,51 @@
+/*
+ KSysGuard, the KDE System Guard
+
+ Copyright (c) 1999 Chris Schlaeger <cs@kde.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+*/
+
+#ifndef _cpuinfo_h_
+#define _cpuinfo_h_
+
+struct SensorModul;
+
+void initCpuInfo(struct SensorModul* sm);
+void exitCpuInfo(void);
+
+int updateCpuInfo(void);
+
+void printCPUUser(const char* cmd);
+void printCPUUserInfo(const char* cmd);
+void printCPUNice(const char* cmd);
+void printCPUNiceInfo(const char* cmd);
+void printCPUSys(const char* cmd);
+void printCPUSysInfo(const char* cmd);
+void printCPUIdle(const char* cmd);
+void printCPUIdleInfo(const char* cmd);
+void printCPUInterrupt(const char* cmd);
+void printCPUInterruptInfo(const char* cmd);
+void printCPUxUser(const char* cmd);
+void printCPUxUserInfo(const char* cmd);
+void printCPUxNice(const char* cmd);
+void printCPUxNiceInfo(const char* cmd);
+void printCPUxSys(const char* cmd);
+void printCPUxSysInfo(const char* cmd);
+void printCPUxIdle(const char* cmd);
+void printCPUxIdleInfo(const char* cmd);
+
+#endif
diff --git a/ksysguard/ksysguardd/OpenBSD/memory.c b/ksysguard/ksysguardd/OpenBSD/memory.c
new file mode 100644
index 000000000..aaf893268
--- /dev/null
+++ b/ksysguard/ksysguardd/OpenBSD/memory.c
@@ -0,0 +1,207 @@
+/*
+ KSysGuard, the KDE System Guard
+
+ Copyright (c) 1999 Chris Schlaeger <cs@kde.org>
+ Copyright (c) 1999-2000 Hans Petter Bieker <bieker@kde.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+*/
+
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <sys/dkstat.h>
+#include <sys/swap.h>
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "Command.h"
+#include "memory.h"
+#include "ksysguardd.h"
+
+static size_t Total = 0;
+static size_t MFree = 0;
+static size_t Active = 0;
+static size_t InActive = 0;
+static size_t STotal = 0;
+static size_t SFree = 0;
+static size_t SUsed = 0;
+static int pageshift = 0;
+
+/* define pagetok in terms of pageshift */
+#define pagetok(size) ((size) << pageshift)
+
+void swapmode(int *used, int *total);
+
+void
+initMemory(struct SensorModul* sm)
+{
+ int pagesize;
+ static int physmem_mib[] = { CTL_HW, HW_PHYSMEM };
+ size_t size;
+ /* get the page size with "getpagesize" and calculate pageshift from
+ * it */
+ pagesize = getpagesize();
+ pageshift = 0;
+ while (pagesize > 1) {
+ pageshift++;
+ pagesize >>= 1;
+ }
+ size = sizeof(Total);
+ sysctl(physmem_mib, 2, &Total, &size, NULL, 0);
+ Total /= 1024;
+ swapmode(&SUsed, &STotal);
+
+ registerMonitor("mem/physical/free", "integer", printMFree, printMFreeInfo, sm);
+ registerMonitor("mem/physical/active", "integer", printActive, printActiveInfo, sm);
+ registerMonitor("mem/physical/inactive", "integer", printInActive, printInActiveInfo, sm);
+ registerMonitor("mem/swap/free", "integer", printSwapFree, printSwapFreeInfo, sm);
+ registerMonitor("mem/swap/used", "integer", printSwapUsed, printSwapUsedInfo, sm);
+}
+
+void
+exitMemory(void)
+{
+}
+
+int
+updateMemory(void)
+{
+ static int vmtotal_mib[] = {CTL_VM, VM_METER};
+ size_t size;
+ struct vmtotal vmtotal;
+ size = sizeof(vmtotal);
+
+ if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0)
+ return -1;
+
+ MFree = pagetok(vmtotal.t_free);
+ MFree /= 1024;
+ Active = pagetok(vmtotal.t_arm);
+ Active /= 1024;
+ InActive = pagetok(vmtotal.t_rm);
+ InActive /= 1024;
+ InActive -= Active;
+
+ swapmode(&SUsed, &STotal);
+ SFree = STotal - SUsed;
+ return 0;
+}
+
+void
+printMFree(const char* cmd)
+{
+ fprintf(CurrentClient, "%d\n", MFree);
+}
+
+void
+printMFreeInfo(const char* cmd)
+{
+ fprintf(CurrentClient, "Free Memory\t0\t%d\tKB\n", Total);
+}
+
+void
+printActive(const char* cmd)
+{
+ fprintf(CurrentClient, "%d\n", Active);
+}
+
+void
+printActiveInfo(const char* cmd)
+{
+ fprintf(CurrentClient, "Active Memory\t0\t%d\tKB\n", Total);
+}
+
+void
+printInActive(const char* cmd)
+{
+ fprintf(CurrentClient, "%d\n", InActive);
+}
+
+void
+printInActiveInfo(const char* cmd)
+{
+ fprintf(CurrentClient, "InActive Memory\t0\t%d\tKB\n", Total);
+}
+
+void
+printSwapUsed(const char* cmd)
+{
+ fprintf(CurrentClient, "%d\n", SUsed);
+}
+
+void
+printSwapUsedInfo(const char* cmd)
+{
+ fprintf(CurrentClient, "Used Swap Memory\t0\t%d\tKB\n", STotal);
+}
+
+void
+printSwapFree(const char* cmd)
+{
+ fprintf(CurrentClient, "%d\n", SFree);
+}
+
+void
+printSwapFreeInfo(const char* cmd)
+{
+ fprintf(CurrentClient, "Free Swap Memory\t0\t%d\tKB\n", STotal);
+}
+
+/*
+This function swapmode was originally written by Tobias
+Weingartner <weingart@openbsd.org>
+
+Taken from OpenBSD top command
+*/
+void
+swapmode (int *used, int *total)
+{
+ int nswap, rnswap, i;
+ struct swapent *swdev;
+
+ *total = *used = 0;
+
+ /* Number of swap devices */
+ nswap = swapctl(SWAP_NSWAP, 0, 0);
+ if (nswap == 0)
+ return;
+
+ swdev = (struct swapent *) malloc(nswap * sizeof(*swdev));
+ if (swdev == NULL)
+ return;
+
+ rnswap = swapctl(SWAP_STATS, swdev, nswap);
+ if (rnswap == -1) {
+ free(swdev);
+ return;
+ }
+
+ /* if rnswap != nswap, then what? */
+
+ /* Total things up */
+ for (i = 0; i < nswap; i++) {
+ if (swdev[i].se_flags & SWF_ENABLE) {
+ *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
+ *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
+ }
+ }
+
+ free(swdev);
+}
diff --git a/ksysguard/ksysguardd/OpenBSD/memory.h b/ksysguard/ksysguardd/OpenBSD/memory.h
new file mode 100644
index 000000000..9cb11373a
--- /dev/null
+++ b/ksysguard/ksysguardd/OpenBSD/memory.h
@@ -0,0 +1,43 @@
+/*
+ KSysGuard, the KDE System Guard
+
+ Copyright (c) 1999 Chris Schlaeger <cs@kde.org>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+*/
+
+#ifndef _memory_h_
+#define _memory_h_
+
+struct SensorModul;
+
+void initMemory(struct SensorModul* sm);
+void exitMemory(void);
+
+int updateMemory(void);
+
+void printMFree(const char* cmd);
+void printMFreeInfo(const char* cmd);
+void printActive(const char* cmd);
+void printActiveInfo(const char* cmd);
+void printInActive(const char* cmd);
+void printInActiveInfo(const char* cmd);
+void printSwapUsed(const char* cmd);
+void printSwapUsedInfo(const char* cmd);
+void printSwapFree(const char* cmd);
+void printSwapFreeInfo(const char* cmd);
+
+#endif