From 3fd4a21d27edc329be1c39130f02ad5b1d42441c Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Mon, 19 Jan 2015 23:41:51 +0100 Subject: [PATCH 1/5] Openbsd port. Should work. --- openbsd/common.h | 43 ++++++++++++++++++ openbsd/cpu.cc | 85 +++++++++++++++++++++++++++++++++++ openbsd/cpu.h | 9 ++++ openbsd/load.cc | 78 ++++++++++++++++++++++++++++++++ openbsd/load.h | 8 ++++ openbsd/memory.cc | 110 ++++++++++++++++++++++++++++++++++++++++++++++ openbsd/memory.h | 8 ++++ 7 files changed, 341 insertions(+) create mode 100644 openbsd/common.h create mode 100644 openbsd/cpu.cc create mode 100644 openbsd/cpu.h create mode 100644 openbsd/load.cc create mode 100644 openbsd/load.h create mode 100644 openbsd/memory.cc create mode 100644 openbsd/memory.h diff --git a/openbsd/common.h b/openbsd/common.h new file mode 100644 index 0000000..5904c16 --- /dev/null +++ b/openbsd/common.h @@ -0,0 +1,43 @@ +/* + * Copyright 2012 Matthew McCormick + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * */ +// This file was Authored by Justin Crawford +// Based on: github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c +// Based on: Apple.cpp for load_string/mem_string and apple's documentation + +#ifndef BSD_METER_COMMON_H_ +#define BSD_METER_COMMON_H_ + +#include + +// Memory Sizes +#define KILOBYTES(x) ((x)/1024) +#define MEGABYTES(x) (KILOBYTES((x))/1024) +#define GIGABYTES(x) (MEGABYTES((x))/1024) + +// CPU percentages stuff +#define CP_USER 0 +#define CP_NICE 1 +#define CP_SYS 2 +#define CP_INTR 3 +#define CP_IDLE 4 +#define CPUSTATES 5 + +inline void error(const char * error) { + std::cerr << error << std::endl; + exit(23); +} + +#endif diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc new file mode 100644 index 0000000..fc22f94 --- /dev/null +++ b/openbsd/cpu.cc @@ -0,0 +1,85 @@ +/* + * Copyright 2012 Matthew McCormick + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * */ +// This file was Authored by Justin Crawford +// Based on: github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c +// Based on: Apple.cpp for load_string/mem_string and apple's documentation + +#include +#include // usleep + +#include +#include + +#include "common.h" +#include "cpu.h" + +uint8_t get_cpu_count() +{ + int cpu_count = 1; // default to 1 + int mib[2] = { CTL_HW, HW_NCPU }; + size_t len = sizeof( cpu_count ); + + if(sysctl(mib, 2, &cpu_count, &len, NULL, 0) < 0) + { + error("sysctl: error getting cpu count"); + } + + return cpu_count; +} + +float cpu_percentage( unsigned int cpu_usage_delay ) +{ + int cpu_ctl[] = { CTL_KERN, KERN_CPTIME }; + + int32_t load1[CPUSTATES]; + int32_t load2[CPUSTATES]; + + size_t size = sizeof( load1 ); + + // get cpu times + if(sysctl(cpu_ctl, 2, &load1, &size, NULL, 0) < 0) + { + error("sysctl: error getting cpu stats"); + } + + usleep(cpu_usage_delay); + + // update cpu times + if(sysctl(cpu_ctl, 2, &load2, &size, NULL, 0) < 0) + { + error("sysctl: error getting cpu stats"); + } + + // Current load times + unsigned long long current_user = load1[CP_USER]; + unsigned long long current_system = load1[CP_SYS]; + unsigned long long current_nice = load1[CP_NICE]; + unsigned long long current_idle = load1[CP_IDLE]; + // Next load times + unsigned long long next_user = load2[CP_USER]; + unsigned long long next_system = load2[CP_SYS]; + unsigned long long next_nice = load2[CP_NICE]; + unsigned long long next_idle = load2[CP_IDLE]; + // Difference between the two + unsigned long long diff_user = next_user - current_user; + unsigned long long diff_system = next_system - current_system; + unsigned long long diff_nice = next_nice - current_nice; + unsigned long long diff_idle = next_idle - current_idle; + + return static_cast(diff_user + diff_system + diff_nice) / + static_cast(diff_user + diff_system + diff_nice + diff_idle) * + 100.0; +} diff --git a/openbsd/cpu.h b/openbsd/cpu.h new file mode 100644 index 0000000..ab8cee9 --- /dev/null +++ b/openbsd/cpu.h @@ -0,0 +1,9 @@ +#ifndef CPU_H_ +#define CPU_H_ + +#include + +float cpu_percentage(unsigned); +uint8_t getCpuCount(); + +#endif diff --git a/openbsd/load.cc b/openbsd/load.cc new file mode 100644 index 0000000..8434400 --- /dev/null +++ b/openbsd/load.cc @@ -0,0 +1,78 @@ +/* + * Copyright 2012 Matthew McCormick + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * */ +// This file was Authored by Justin Crawford +// Based on: https://github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c +// Based on: Apple.cpp for load_string/mem_string and apple's documentation + + +#include +#include +#include // getloadavg() +#include // floorf() +#include + +#include "cpu.h" +#include "load.h" +#include "../luts.h" + +// Load Averages +std::string load_string( bool use_colors = false ) +{ + std::stringstream ss; + // Only get 3 load averages + cont int nelem = 3; + double averages[nelem]; + // based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c + + if( getloadavg( averages, nelem ) < 0) + { + ss << "0.00 0.00 0.00"; // couldn't get averages. + } + else + { + if( use_colors ) + { + // may not work + uint8_t cpu_count = getCpuCount(); + + unsigned load_percent = static_cast( + averages[0] / cpu_count * 0.5f * 100.0f); + + if( load_percent > 100 ) + { + load_percent = 100; + } + + ss << load_lut[load_percent]; + } + + for( int i = 0; i < nelem; ++i ) + { + // Round to nearest, make sure this is only a 0.00 value not a 0.0000 + float avg = floorf( static_cast (averages[i] ) * 100 + 0.5 ) / 100; + ss << avg << " "; + } + + if( use_colors ) + { + ss << "#[fg=default,bg=default]"; + } + + } + + return ss.str(); +} + diff --git a/openbsd/load.h b/openbsd/load.h new file mode 100644 index 0000000..3b8d814 --- /dev/null +++ b/openbsd/load.h @@ -0,0 +1,8 @@ +#ifndef LOAD_H_ +#define LOAD_H_ + +#include + +std::string load_string( bool ); + +#endif diff --git a/openbsd/memory.cc b/openbsd/memory.cc new file mode 100644 index 0000000..6442a1d --- /dev/null +++ b/openbsd/memory.cc @@ -0,0 +1,110 @@ +/* + * Copyright 2012 Matthew McCormick + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * */ +// This file was Authored by Justin Crawford +// Pawel Soltys + +//#error ToDo: OpenBSD. This file is incomplete and likely will not compile if +//you remove this error (it is here to tell you it's unfinished) + +#include +#include +#include +#include +#include // vmtotal struct + +#include "common.h" +#include "memory.h" +#include "../luts.h" + +static int pageshift; + +#define pagesh(size) ((size) << pageshift) + +std::string mem_string( bool use_colors = false ) +{ + std::ostringstream oss; + + // These values are in bytes + int64_t total_mem = 0; + int64_t used_mem = 0; + int32_t free_mem = 0; + size_t size; + + // get page size + static int hw_pagesize[] = { CTL_HW, HW_PAGESIZE }; + int page_size = 0; + size = sizeof( page_size ); + if( sysctl( hw_pagesize, 2, &page_size, &size, NULL, 0) < 0) + { + error("memory: error getting page size"); + exit(23); + } + + // calculate how far we must shift the variables + pageshift = 0; + while( page_size > 1 ) + { + pageshift++; + page_size >>= 1; + } + + // get vm memory stats + static int vm_totalmem[] = { CTL_VM, VM_METER }; + struct vmtotal vm_total; + size = sizeof( vm_total ); + if( sysctl( vm_totalmem, 2, &vm_total, &size, NULL, 0 ) < 0 ) + { + error("memory: error getting vm memory stats"); + exit(23); + } + + // In case we need it, this gets the cached memory (bcstats.numbufpages) + static int vm_bcstats[] = { CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT }; + struct bcachestats bcstats; + size = sizeof( bcstats ); + if( sysctl( vm_bcstats, 3, &bcstats, &size, NULL, 0 ) < 0 ) + { + error( "memory: error getting cached memory size" ); + exit( 23 ); + } + + // calculations based on conky openbsd port + used_mem = pagesh( vm_total.t_rm ); + free_mem = pagesh( vm_total.t_free ); + + // from nagios-memory plugin + used_mem -= pagesh( bcstats.numbufpages ); + free_mem += pagesh( bcstats.numbufpages ); + + // calculate total memory + total_mem = used_mem + free_mem; + + if( use_colors ) + { + oss << mem_lut[( 100 * used_mem ) / total_mem]; + } + + oss << convert_unit( used_mem, MEGABYTES ) + << '/' << convert_unit( total_mem, MEGABYTES ) << "MB"; + + if( use_colors ) + { + oss << "#[fg=default,bg=default]"; + } + + return oss.str(); +} + diff --git a/openbsd/memory.h b/openbsd/memory.h new file mode 100644 index 0000000..196dfd6 --- /dev/null +++ b/openbsd/memory.h @@ -0,0 +1,8 @@ +#ifndef MEMORY_H_ +#define MEMORY_H_ + +#include + +std::string mem_string( bool ); + +#endif From ae5c71441b20b307d1e3bc510517901de5fd7318 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Mon, 19 Jan 2015 23:57:53 +0100 Subject: [PATCH 2/5] FreeBSD and OpenBSD ports - Removed OpenBSD stuff from freebsd port - Renamed bsd folder to freebsd since now it contains only freebsd-relevant files. - Changed CMake instructions to account for bsd port changes - modified main source file to account for openbsd port --- CMakeLists.txt | 7 +- bsd/memory_openbsd.cc | 78 ---------------------- {bsd => freebsd}/cpu.cc | 0 {bsd => freebsd}/cpu.h | 7 ++ {bsd => freebsd}/getsysctl.h | 8 --- {bsd => freebsd}/load.cc | 0 {bsd => freebsd}/load.h | 0 bsd/memory_freebsd.cc => freebsd/memory.cc | 0 {bsd => freebsd}/memory.h | 0 {bsd => freebsd}/openBSD.txt | 0 openbsd/cpu.cc | 2 +- openbsd/cpu.h | 7 ++ openbsd/{common.h => error.h} | 0 openbsd/memory.cc | 2 +- tmux-mem-cpu-load.cpp | 13 ++-- 15 files changed, 28 insertions(+), 96 deletions(-) delete mode 100644 bsd/memory_openbsd.cc rename {bsd => freebsd}/cpu.cc (100%) rename {bsd => freebsd}/cpu.h (86%) rename {bsd => freebsd}/getsysctl.h (91%) rename {bsd => freebsd}/load.cc (100%) rename {bsd => freebsd}/load.h (100%) rename bsd/memory_freebsd.cc => freebsd/memory.cc (100%) rename {bsd => freebsd}/memory.h (100%) rename {bsd => freebsd}/openBSD.txt (100%) rename openbsd/{common.h => error.h} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 86336e0..5b6b4ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,13 +68,12 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") # FreeBSD STUFF HERE message("FreeBSD detected") message( WARNING "FreeBSD is still experimental!" ) - set( METER_SOURCES "bsd/memory_freebsd.cc" "bsd/cpu.cc" "bsd/load.cc" ) + set( METER_SOURCES "freebsd/memory.cc" "freebsd/cpu.cc" "freebsd/load.cc" ) elseif(CMAKE_SYSTEM_NAME MATCHES "OpenBSD") # OpenBSD Stuff Here message( "OpenBSD detected") - message( FATAL_ERROR "OpenBSD is not supported! See bsd/openBSD.txt for more - info" ) - set( METER_SOURCES "bsd/memory_openbsd.cc" "bsd/cpu.cc" "bsd/load.cc" ) + message( WARNING "OpenBSD is still experimental!" ) + set( METER_SOURCES "openbsd/memory.cc" "openbsd/cpu.cc" "openbsd/load.cc" ) else() message( FATAL_ERROR "Cannot be compiled on this system" ) endif() diff --git a/bsd/memory_openbsd.cc b/bsd/memory_openbsd.cc deleted file mode 100644 index 69bdd03..0000000 --- a/bsd/memory_openbsd.cc +++ /dev/null @@ -1,78 +0,0 @@ -/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap - * - * Copyright 2012 Matthew McCormick - * Copyright 2013 Justin Crawford - * Copyright 2015 Pawel 'l0ner' Soltys - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Based on: github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c -// Based on: Apple.cpp for load_string/mem_string and apple's documentation - -#error ToDo: OpenBSD. This file is incomplete and likely will not compile if you remove this error (it is here to tell you it's unfinished) - -#include -#include -#include - -#include "getsysctl.h" -#include "memory.h" -#include "../luts.h" -#include "../conversions.h" - -std::string mem_string( bool use_colors = false ) -{ - // These values are in bytes - int64_t total_mem = 0; - int64_t used_mem = 0; - int64_t unused_mem = 0; - int32_t inactive_mem = 0; - int32_t active_mem = 0; - int32_t free_mem = 0; - int32_t wired_mem = 0; - int32_t page_size = 0; - int32_t cache_mem = 0; - std::ostringstream oss; - - // Get total physical memory, page size, and some other needed info - GETSYSCTL( "hw.realmem", total_mem ); - GETSYSCTL( "hw.pagesize", page_size ); - - GETSYSCTL( "vm.stats.vm.v_free_count", free_mem ); - GETSYSCTL( "vm.stats.vm.v_inactive_count", inactive_mem ); - GETSYSCTL( "vm.stats.vm.v_cache_count", cache_mem ); - GETSYSCTL( "vm.stats.vm.v_wire_count", wired_mem ); - - // Get all memory which can be allocated - //unused_mem = (inactive_mem + cache_mem + free_mem) * page_size; - used_mem = ( - static_cast( active_mem ) + static_cast( wired_mem ) + - static_cast( inactive_mem ) ) * static_cast( page_size ); - - if( use_colors ) - { - oss << mem_lut[( 100 * used_mem ) / total_mem]; - } - - oss << convert_unit( used_mem, MEGABYTES ) << '/' - << convert_unit( total_mem, MEGABYTES ) << "MB"; - - if( use_colors ) - { - oss << "#[fg=default,bg=default]"; - } - - return oss.str(); -} - diff --git a/bsd/cpu.cc b/freebsd/cpu.cc similarity index 100% rename from bsd/cpu.cc rename to freebsd/cpu.cc diff --git a/bsd/cpu.h b/freebsd/cpu.h similarity index 86% rename from bsd/cpu.h rename to freebsd/cpu.h index ebc0e8e..3840650 100644 --- a/bsd/cpu.h +++ b/freebsd/cpu.h @@ -19,6 +19,13 @@ #ifndef CPU_H_ #define CPU_H_ +#define CP_USER 0 +#define CP_NICE 1 +#define CP_SYS 2 +#define CP_INTR 3 +#define CP_IDLE 4 +#define CPUSTATES 5 + float cpu_percentage( unsigned ); #endif diff --git a/bsd/getsysctl.h b/freebsd/getsysctl.h similarity index 91% rename from bsd/getsysctl.h rename to freebsd/getsysctl.h index 58f6860..aecd004 100644 --- a/bsd/getsysctl.h +++ b/freebsd/getsysctl.h @@ -27,14 +27,6 @@ #include #include -// CPU percentages stuff -#define CP_USER 0 -#define CP_NICE 1 -#define CP_SYS 2 -#define CP_INTR 3 -#define CP_IDLE 4 -#define CPUSTATES 5 - #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) static inline void getsysctl( const char *name, void *ptr, size_t len ) { diff --git a/bsd/load.cc b/freebsd/load.cc similarity index 100% rename from bsd/load.cc rename to freebsd/load.cc diff --git a/bsd/load.h b/freebsd/load.h similarity index 100% rename from bsd/load.h rename to freebsd/load.h diff --git a/bsd/memory_freebsd.cc b/freebsd/memory.cc similarity index 100% rename from bsd/memory_freebsd.cc rename to freebsd/memory.cc diff --git a/bsd/memory.h b/freebsd/memory.h similarity index 100% rename from bsd/memory.h rename to freebsd/memory.h diff --git a/bsd/openBSD.txt b/freebsd/openBSD.txt similarity index 100% rename from bsd/openBSD.txt rename to freebsd/openBSD.txt diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index fc22f94..4fb2681 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -23,7 +23,7 @@ #include #include -#include "common.h" +#include "error.h" #include "cpu.h" uint8_t get_cpu_count() diff --git a/openbsd/cpu.h b/openbsd/cpu.h index ab8cee9..5a6a641 100644 --- a/openbsd/cpu.h +++ b/openbsd/cpu.h @@ -3,6 +3,13 @@ #include +#define CP_USER 0 +#define CP_NICE 1 +#define CP_SYS 2 +#define CP_INTR 3 +#define CP_IDLE 4 +#define CPUSTATES 5 + float cpu_percentage(unsigned); uint8_t getCpuCount(); diff --git a/openbsd/common.h b/openbsd/error.h similarity index 100% rename from openbsd/common.h rename to openbsd/error.h diff --git a/openbsd/memory.cc b/openbsd/memory.cc index 6442a1d..d42ef80 100644 --- a/openbsd/memory.cc +++ b/openbsd/memory.cc @@ -25,7 +25,7 @@ #include #include // vmtotal struct -#include "common.h" +#include "error.h" #include "memory.h" #include "../luts.h" diff --git a/tmux-mem-cpu-load.cpp b/tmux-mem-cpu-load.cpp index 11180c2..6ee6be9 100644 --- a/tmux-mem-cpu-load.cpp +++ b/tmux-mem-cpu-load.cpp @@ -34,12 +34,17 @@ #include "osx/cpu.h" #include "osx/memory.h" #include "osx/load.h" -#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) +#elif defined(__FreeBSD__) || defined(__NetBSD__) // BSD system #define BSD_BASED 1 - #include "bsd/cpu.h" - #include "bsd/load.h" - #include "bsd/memory.h" + #include "freebsd/cpu.h" + #include "freebsd/load.h" + #include "freebsd/memory.h" +#elif defined(__OpenBSD) + #define BSD_BASED 1 + #include "freebsd/cpu.h" + #include "freebsd/load.h" + #include "freebsd/memory.h" #else // assume linux system #include "linux/cpu.h" From 3a78f44e39f59b62b7b220a481fe492fa274ca75 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Tue, 20 Jan 2015 02:44:21 +0100 Subject: [PATCH 3/5] OpenBSD: missing headers, typos cpu.h: underscores in get_cpu_count() load.cc: underscores in get_cpu_count(), typos memory.cc: missing headers --- openbsd/cpu.h | 4 ++-- openbsd/load.cc | 4 ++-- openbsd/memory.cc | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/openbsd/cpu.h b/openbsd/cpu.h index 5a6a641..6cc5786 100644 --- a/openbsd/cpu.h +++ b/openbsd/cpu.h @@ -10,7 +10,7 @@ #define CP_IDLE 4 #define CPUSTATES 5 -float cpu_percentage(unsigned); -uint8_t getCpuCount(); +float cpu_percentage( unsigned ); +uint8_t get_cpu_count(); #endif diff --git a/openbsd/load.cc b/openbsd/load.cc index 8434400..3b01e5e 100644 --- a/openbsd/load.cc +++ b/openbsd/load.cc @@ -33,7 +33,7 @@ std::string load_string( bool use_colors = false ) { std::stringstream ss; // Only get 3 load averages - cont int nelem = 3; + const int nelem = 3; double averages[nelem]; // based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c @@ -46,7 +46,7 @@ std::string load_string( bool use_colors = false ) if( use_colors ) { // may not work - uint8_t cpu_count = getCpuCount(); + uint8_t cpu_count = get_cpu_count(); unsigned load_percent = static_cast( averages[0] / cpu_count * 0.5f * 100.0f); diff --git a/openbsd/memory.cc b/openbsd/memory.cc index d42ef80..9586acf 100644 --- a/openbsd/memory.cc +++ b/openbsd/memory.cc @@ -21,6 +21,7 @@ #include #include +#include // VFS_* which we use to get cache #include #include #include // vmtotal struct @@ -28,6 +29,7 @@ #include "error.h" #include "memory.h" #include "../luts.h" +#include "../conversions.h" static int pageshift; From 4d6c6760b301961cf82d7a0117c145961ce20504 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Tue, 20 Jan 2015 02:57:14 +0100 Subject: [PATCH 4/5] removed junk, reindentation to adhere to style guidelines --- openbsd/cpu.cc | 19 +++---- openbsd/cpu.h | 18 ++++++ openbsd/error.h | 26 ++------- openbsd/load.cc | 56 +++++++++---------- openbsd/load.h | 18 ++++++ openbsd/memory.cc | 139 +++++++++++++++++++++++----------------------- openbsd/memory.h | 18 ++++++ 7 files changed, 164 insertions(+), 130 deletions(-) diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index 4fb2681..a0a31d4 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -1,5 +1,7 @@ -/* +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,10 +14,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ -// This file was Authored by Justin Crawford -// Based on: github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c -// Based on: Apple.cpp for load_string/mem_string and apple's documentation + */ #include #include // usleep @@ -34,7 +33,7 @@ uint8_t get_cpu_count() if(sysctl(mib, 2, &cpu_count, &len, NULL, 0) < 0) { - error("sysctl: error getting cpu count"); + error("sysctl: error getting cpu count"); } return cpu_count; @@ -52,7 +51,7 @@ float cpu_percentage( unsigned int cpu_usage_delay ) // get cpu times if(sysctl(cpu_ctl, 2, &load1, &size, NULL, 0) < 0) { - error("sysctl: error getting cpu stats"); + error("sysctl: error getting cpu stats"); } usleep(cpu_usage_delay); @@ -60,7 +59,7 @@ float cpu_percentage( unsigned int cpu_usage_delay ) // update cpu times if(sysctl(cpu_ctl, 2, &load2, &size, NULL, 0) < 0) { - error("sysctl: error getting cpu stats"); + error("sysctl: error getting cpu stats"); } // Current load times @@ -80,6 +79,6 @@ float cpu_percentage( unsigned int cpu_usage_delay ) unsigned long long diff_idle = next_idle - current_idle; return static_cast(diff_user + diff_system + diff_nice) / - static_cast(diff_user + diff_system + diff_nice + diff_idle) * - 100.0; + static_cast(diff_user + diff_system + diff_nice + diff_idle) * + 100.0; } diff --git a/openbsd/cpu.h b/openbsd/cpu.h index 6cc5786..5bb6e87 100644 --- a/openbsd/cpu.h +++ b/openbsd/cpu.h @@ -1,3 +1,21 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef CPU_H_ #define CPU_H_ diff --git a/openbsd/error.h b/openbsd/error.h index 5904c16..540b334 100644 --- a/openbsd/error.h +++ b/openbsd/error.h @@ -1,5 +1,7 @@ -/* +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,29 +14,13 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ -// This file was Authored by Justin Crawford -// Based on: github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c -// Based on: Apple.cpp for load_string/mem_string and apple's documentation + */ -#ifndef BSD_METER_COMMON_H_ -#define BSD_METER_COMMON_H_ +#ifndef BSD_ERROR_H_ +#define BSD_ERROR_H_ #include -// Memory Sizes -#define KILOBYTES(x) ((x)/1024) -#define MEGABYTES(x) (KILOBYTES((x))/1024) -#define GIGABYTES(x) (MEGABYTES((x))/1024) - -// CPU percentages stuff -#define CP_USER 0 -#define CP_NICE 1 -#define CP_SYS 2 -#define CP_INTR 3 -#define CP_IDLE 4 -#define CPUSTATES 5 - inline void error(const char * error) { std::cerr << error << std::endl; exit(23); diff --git a/openbsd/load.cc b/openbsd/load.cc index 3b01e5e..1c3a88b 100644 --- a/openbsd/load.cc +++ b/openbsd/load.cc @@ -1,5 +1,7 @@ -/* +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,11 +14,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ -// This file was Authored by Justin Crawford -// Based on: https://github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c -// Based on: Apple.cpp for load_string/mem_string and apple's documentation - + */ #include #include @@ -39,37 +37,37 @@ std::string load_string( bool use_colors = false ) if( getloadavg( averages, nelem ) < 0) { - ss << "0.00 0.00 0.00"; // couldn't get averages. + ss << "0.00 0.00 0.00"; // couldn't get averages. } else { - if( use_colors ) - { - // may not work - uint8_t cpu_count = get_cpu_count(); + if( use_colors ) + { + // may not work + uint8_t cpu_count = get_cpu_count(); - unsigned load_percent = static_cast( - averages[0] / cpu_count * 0.5f * 100.0f); + unsigned load_percent = static_cast( + averages[0] / cpu_count * 0.5f * 100.0f); - if( load_percent > 100 ) - { - load_percent = 100; - } + if( load_percent > 100 ) + { + load_percent = 100; + } - ss << load_lut[load_percent]; - } + ss << load_lut[load_percent]; + } - for( int i = 0; i < nelem; ++i ) - { - // Round to nearest, make sure this is only a 0.00 value not a 0.0000 - float avg = floorf( static_cast (averages[i] ) * 100 + 0.5 ) / 100; - ss << avg << " "; - } + for( int i = 0; i < nelem; ++i ) + { + // Round to nearest, make sure this is only a 0.00 value not a 0.0000 + float avg = floorf( static_cast (averages[i] ) * 100 + 0.5 ) / 100; + ss << avg << " "; + } - if( use_colors ) - { - ss << "#[fg=default,bg=default]"; - } + if( use_colors ) + { + ss << "#[fg=default,bg=default]"; + } } diff --git a/openbsd/load.h b/openbsd/load.h index 3b8d814..f1bbc58 100644 --- a/openbsd/load.h +++ b/openbsd/load.h @@ -1,3 +1,21 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef LOAD_H_ #define LOAD_H_ diff --git a/openbsd/memory.cc b/openbsd/memory.cc index 9586acf..953a212 100644 --- a/openbsd/memory.cc +++ b/openbsd/memory.cc @@ -1,5 +1,7 @@ -/* +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,12 +14,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ -// This file was Authored by Justin Crawford -// Pawel Soltys - -//#error ToDo: OpenBSD. This file is incomplete and likely will not compile if -//you remove this error (it is here to tell you it's unfinished) + */ #include #include @@ -37,76 +34,76 @@ static int pageshift; std::string mem_string( bool use_colors = false ) { - std::ostringstream oss; - - // These values are in bytes - int64_t total_mem = 0; - int64_t used_mem = 0; - int32_t free_mem = 0; - size_t size; + std::ostringstream oss; - // get page size - static int hw_pagesize[] = { CTL_HW, HW_PAGESIZE }; - int page_size = 0; - size = sizeof( page_size ); - if( sysctl( hw_pagesize, 2, &page_size, &size, NULL, 0) < 0) - { - error("memory: error getting page size"); - exit(23); - } - - // calculate how far we must shift the variables - pageshift = 0; - while( page_size > 1 ) - { - pageshift++; - page_size >>= 1; - } + // These values are in bytes + int64_t total_mem = 0; + int64_t used_mem = 0; + int32_t free_mem = 0; + size_t size; - // get vm memory stats - static int vm_totalmem[] = { CTL_VM, VM_METER }; - struct vmtotal vm_total; - size = sizeof( vm_total ); - if( sysctl( vm_totalmem, 2, &vm_total, &size, NULL, 0 ) < 0 ) - { - error("memory: error getting vm memory stats"); - exit(23); - } - - // In case we need it, this gets the cached memory (bcstats.numbufpages) - static int vm_bcstats[] = { CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT }; - struct bcachestats bcstats; - size = sizeof( bcstats ); - if( sysctl( vm_bcstats, 3, &bcstats, &size, NULL, 0 ) < 0 ) - { - error( "memory: error getting cached memory size" ); - exit( 23 ); - } + // get page size + static int hw_pagesize[] = { CTL_HW, HW_PAGESIZE }; + int page_size = 0; + size = sizeof( page_size ); + if( sysctl( hw_pagesize, 2, &page_size, &size, NULL, 0) < 0) + { + error("memory: error getting page size"); + exit(23); + } - // calculations based on conky openbsd port - used_mem = pagesh( vm_total.t_rm ); - free_mem = pagesh( vm_total.t_free ); + // calculate how far we must shift the variables + pageshift = 0; + while( page_size > 1 ) + { + pageshift++; + page_size >>= 1; + } - // from nagios-memory plugin - used_mem -= pagesh( bcstats.numbufpages ); - free_mem += pagesh( bcstats.numbufpages ); - - // calculate total memory - total_mem = used_mem + free_mem; - - if( use_colors ) - { - oss << mem_lut[( 100 * used_mem ) / total_mem]; - } + // get vm memory stats + static int vm_totalmem[] = { CTL_VM, VM_METER }; + struct vmtotal vm_total; + size = sizeof( vm_total ); + if( sysctl( vm_totalmem, 2, &vm_total, &size, NULL, 0 ) < 0 ) + { + error("memory: error getting vm memory stats"); + exit(23); + } - oss << convert_unit( used_mem, MEGABYTES ) - << '/' << convert_unit( total_mem, MEGABYTES ) << "MB"; + // In case we need it, this gets the cached memory (bcstats.numbufpages) + static int vm_bcstats[] = { CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT }; + struct bcachestats bcstats; + size = sizeof( bcstats ); + if( sysctl( vm_bcstats, 3, &bcstats, &size, NULL, 0 ) < 0 ) + { + error( "memory: error getting cached memory size" ); + exit( 23 ); + } - if( use_colors ) - { - oss << "#[fg=default,bg=default]"; - } + // calculations based on conky openbsd port + used_mem = pagesh( vm_total.t_rm ); + free_mem = pagesh( vm_total.t_free ); - return oss.str(); + // from nagios-memory plugin + used_mem -= pagesh( bcstats.numbufpages ); + free_mem += pagesh( bcstats.numbufpages ); + + // calculate total memory + total_mem = used_mem + free_mem; + + if( use_colors ) + { + oss << mem_lut[( 100 * used_mem ) / total_mem]; + } + + oss << convert_unit( used_mem, MEGABYTES ) + << '/' << convert_unit( total_mem, MEGABYTES ) << "MB"; + + if( use_colors ) + { + oss << "#[fg=default,bg=default]"; + } + + return oss.str(); } diff --git a/openbsd/memory.h b/openbsd/memory.h index 196dfd6..3a123bb 100644 --- a/openbsd/memory.h +++ b/openbsd/memory.h @@ -1,3 +1,21 @@ +/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap + * + * Copyright 2012 Matthew McCormick + * Copyright 2015 Pawel 'l0ner' Soltys + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef MEMORY_H_ #define MEMORY_H_ From 243487903a535a049ca1c2d7850277a3e1a61baf Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Tue, 20 Jan 2015 14:50:07 +0100 Subject: [PATCH 5/5] Strip unnecessary whitespaces --- freebsd/cpu.cc | 4 ++-- freebsd/openBSD.txt | 19 ------------------- openbsd/cpu.cc | 2 +- openbsd/load.cc | 1 - openbsd/memory.cc | 3 +-- 5 files changed, 4 insertions(+), 25 deletions(-) delete mode 100644 freebsd/openBSD.txt diff --git a/freebsd/cpu.cc b/freebsd/cpu.cc index 70e3560..403aea9 100644 --- a/freebsd/cpu.cc +++ b/freebsd/cpu.cc @@ -51,7 +51,7 @@ float cpu_percentage( unsigned int cpu_usage_delay ) unsigned long long diff_nice = next_nice - current_nice; unsigned long long diff_idle = next_idle - current_idle; - return static_cast( diff_user + diff_system + diff_nice ) / - static_cast( diff_user + diff_system + diff_nice + diff_idle ) * + return static_cast( diff_user + diff_system + diff_nice ) / + static_cast( diff_user + diff_system + diff_nice + diff_idle ) * 100.0; } diff --git a/freebsd/openBSD.txt b/freebsd/openBSD.txt deleted file mode 100644 index 4a7c804..0000000 --- a/freebsd/openBSD.txt +++ /dev/null @@ -1,19 +0,0 @@ -About OpenBSD Port -================== - -I've decided not to do OpenBSD port. Some preparations has been made both -by myself and (mainly) by Justin Crawford, so finish it shouldn't be difficult. -Personally, after installing OpenBSD and trying to finish the port I've -discovered that g++ supplied by OpenBSD (version 5.7) doesn't support c++11. -This means we lose the ability to use to_string() and stoi() functions. I could -write replacements for them, or try to get c++11 working on OpenBSD. But I -decided not to. At least for the moment. - -I don't know OpenBSD, it's unfamiliar ground for me. And while FreeBSD was easy -to get into, I have found OpenBSD a little bit more difficult. - -So, no OpenBSD port for now. If you are OpenBSD user and know a little bit c++ -finishing the port should be easy (once you get C++11 working). - -l0ner - diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index a0a31d4..c2019e1 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -25,7 +25,7 @@ #include "error.h" #include "cpu.h" -uint8_t get_cpu_count() +uint8_t get_cpu_count() { int cpu_count = 1; // default to 1 int mib[2] = { CTL_HW, HW_NCPU }; diff --git a/openbsd/load.cc b/openbsd/load.cc index 1c3a88b..287f968 100644 --- a/openbsd/load.cc +++ b/openbsd/load.cc @@ -73,4 +73,3 @@ std::string load_string( bool use_colors = false ) return ss.str(); } - diff --git a/openbsd/memory.cc b/openbsd/memory.cc index 953a212..44eb849 100644 --- a/openbsd/memory.cc +++ b/openbsd/memory.cc @@ -96,7 +96,7 @@ std::string mem_string( bool use_colors = false ) oss << mem_lut[( 100 * used_mem ) / total_mem]; } - oss << convert_unit( used_mem, MEGABYTES ) + oss << convert_unit( used_mem, MEGABYTES ) << '/' << convert_unit( total_mem, MEGABYTES ) << "MB"; if( use_colors ) @@ -106,4 +106,3 @@ std::string mem_string( bool use_colors = false ) return oss.str(); } -