From 3fd4a21d27edc329be1c39130f02ad5b1d42441c Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Mon, 19 Jan 2015 23:41:51 +0100 Subject: [PATCH 01/21] 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 02/21] 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 03/21] 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 04/21] 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 05/21] 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(); } - From 8afed019770cd22ae955bb98f40fb341e7abf2f3 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Mon, 19 Jan 2015 23:41:51 +0100 Subject: [PATCH 06/21] 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 112004f3db53fcddba0ccf8eb92c24a3a4f95f4a Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Mon, 19 Jan 2015 23:57:53 +0100 Subject: [PATCH 07/21] 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 | 12 ++++++------ common/main.cc | 13 +++++++++---- {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 +- 14 files changed, 31 insertions(+), 20 deletions(-) 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 0211974..520a431 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,13 +65,13 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") set(METER_SOURCES "osx/memory.cc" "osx/cpu.cc" "osx/load.cc") elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") message("FreeBSD detected") - message(WARNING "FreeBSD is still experimental!") - set(METER_SOURCES "bsd/memory_freebsd.cc" "bsd/cpu.cc" "bsd/load.cc") + message( WARNING "FreeBSD is still experimental!" ) + set( METER_SOURCES "freebsd/memory.cc" "freebsd/cpu.cc" "freebsd/load.cc" ) elseif(CMAKE_SYSTEM_NAME MATCHES "OpenBSD") - message(STATUS "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") + # OpenBSD Stuff Here + message( "OpenBSD detected") + 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/common/main.cc b/common/main.cc index 2937a39..a5fa0db 100644 --- a/common/main.cc +++ b/common/main.cc @@ -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" 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" From 580b076b79ce94c3ce9d04711f935c2294ed6ede Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Tue, 20 Jan 2015 02:44:21 +0100 Subject: [PATCH 08/21] 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 3e7ce969036749cd25bd084f3c66599a21e131be Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Tue, 20 Jan 2015 02:57:14 +0100 Subject: [PATCH 09/21] 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 9bd9e79d3600350aa431ea212a0ef4e34dc642ec Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Tue, 20 Jan 2015 14:50:07 +0100 Subject: [PATCH 10/21] 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(); } - From cc8eb4786815a01cc67f16985f375221dec8b73d Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Tue, 20 Jan 2015 15:04:14 +0100 Subject: [PATCH 11/21] remove useless bsd/memory_openbsd.cc --- bsd/memory_openbsd.cc | 78 ------------------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 bsd/memory_openbsd.cc diff --git a/bsd/memory_openbsd.cc b/bsd/memory_openbsd.cc deleted file mode 100644 index a391cac..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(); -} - From 69df3a37bf9e903285109538e82fe7698c35d746 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Tue, 20 Jan 2015 15:08:47 +0100 Subject: [PATCH 12/21] STATUS on OS message detection, consistent spacing --- CMakeLists.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 520a431..b676edd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,16 +64,16 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") message(STATUS "Darwin detected") set(METER_SOURCES "osx/memory.cc" "osx/cpu.cc" "osx/load.cc") elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") - message("FreeBSD detected") - message( WARNING "FreeBSD is still experimental!" ) - set( METER_SOURCES "freebsd/memory.cc" "freebsd/cpu.cc" "freebsd/load.cc" ) + message(STATUS "FreeBSD detected") + message(WARNING "FreeBSD is still experimental!") + 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( WARNING "OpenBSD is still experimental!" ) - set( METER_SOURCES "openbsd/memory.cc" "openbsd/cpu.cc" "openbsd/load.cc" ) + message(STATUS "OpenBSD detected") + 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" ) + message(FATAL_ERROR "Cannot be compiled on this system") endif() # set common source files From b5a2fcfd34e18be70f7becc7c6e9df8c09db4a10 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Tue, 20 Jan 2015 15:11:32 +0100 Subject: [PATCH 13/21] update readme with supported platforms --- CONTRIBUTING.rst | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 CONTRIBUTING.rst diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst new file mode 100644 index 0000000..cc345ce --- /dev/null +++ b/CONTRIBUTING.rst @@ -0,0 +1,54 @@ +============ +Contributing +============ + +Want to improve the quality of tmux-mem-cpu-load code? Great! Here's a quick +guide: + +1. Fork, then clone the repo: + + git clone git@github.com:your-username/tmux-mem-cpu-load + +2. Make your change. Add tests for your change. +3. See if it compiles and runs like it should. +4. Run tests to check if you didn't break anything: + + make test + +Push to your fork and `submit a pull request`_. + +At this point you're waiting on us. We'll review your changes as soon as we can. +Before merging your changes we may request you to make some changes or +corrections. + +Style guidelines +---------------- + +You'll need to follow the subsequent rules in order to get your code merged: + +* Use Allman_ style for block braces. +* No space before `(` +* Add space after each `(` and before each `)` +* Use braces single line statements +* Don't use mixed case naming style, use underscores instead. + Bad example: + + int myAwesomeVariable = 0; + doSomething( myAwesomeVariable ); + + Good example: + + int my_awesome_variable = 0; + do_something( my_awesome_variable ); + +* Don't vertically align tokens on consecutive lines. +* If you break up an argument list, align the line to opening brace +* Use 2 space indentation (no tabs) +* Use spaces around operators, except for unary operators, such as `!`. +* Add LICENSE header in new files you create. +* Put vim modeline as the first line of file header +* Use the lower-case for CMake commands +* Do not add trailing whitespace + +.. _`submit a pull request`: https://github.com/thewtex/tmux-mem-cpu-load/compare/ +.. _Allman: http://en.wikipedia.org/wiki/Indent_style#Allman_style From 668a1d97ddcf9a5094facd87790ddd28347b0190 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Tue, 20 Jan 2015 15:13:39 +0100 Subject: [PATCH 14/21] suggestion about script that can be used to strip whitespaces --- CONTRIBUTING | 54 ------------------------------------------------ CONTRIBUTING.rst | 2 ++ README.rst | 2 +- 3 files changed, 3 insertions(+), 55 deletions(-) delete mode 100644 CONTRIBUTING diff --git a/CONTRIBUTING b/CONTRIBUTING deleted file mode 100644 index cc345ce..0000000 --- a/CONTRIBUTING +++ /dev/null @@ -1,54 +0,0 @@ -============ -Contributing -============ - -Want to improve the quality of tmux-mem-cpu-load code? Great! Here's a quick -guide: - -1. Fork, then clone the repo: - - git clone git@github.com:your-username/tmux-mem-cpu-load - -2. Make your change. Add tests for your change. -3. See if it compiles and runs like it should. -4. Run tests to check if you didn't break anything: - - make test - -Push to your fork and `submit a pull request`_. - -At this point you're waiting on us. We'll review your changes as soon as we can. -Before merging your changes we may request you to make some changes or -corrections. - -Style guidelines ----------------- - -You'll need to follow the subsequent rules in order to get your code merged: - -* Use Allman_ style for block braces. -* No space before `(` -* Add space after each `(` and before each `)` -* Use braces single line statements -* Don't use mixed case naming style, use underscores instead. - Bad example: - - int myAwesomeVariable = 0; - doSomething( myAwesomeVariable ); - - Good example: - - int my_awesome_variable = 0; - do_something( my_awesome_variable ); - -* Don't vertically align tokens on consecutive lines. -* If you break up an argument list, align the line to opening brace -* Use 2 space indentation (no tabs) -* Use spaces around operators, except for unary operators, such as `!`. -* Add LICENSE header in new files you create. -* Put vim modeline as the first line of file header -* Use the lower-case for CMake commands -* Do not add trailing whitespace - -.. _`submit a pull request`: https://github.com/thewtex/tmux-mem-cpu-load/compare/ -.. _Allman: http://en.wikipedia.org/wiki/Indent_style#Allman_style diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index cc345ce..70c89f5 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -49,6 +49,8 @@ You'll need to follow the subsequent rules in order to get your code merged: * Put vim modeline as the first line of file header * Use the lower-case for CMake commands * Do not add trailing whitespace + You can use this bash script to strip unnecessary whitespaces: + http://git.io/z_GA3A .. _`submit a pull request`: https://github.com/thewtex/tmux-mem-cpu-load/compare/ .. _Allman: http://en.wikipedia.org/wiki/Indent_style#Allman_style diff --git a/README.rst b/README.rst index 90d885d..6757332 100644 --- a/README.rst +++ b/README.rst @@ -47,7 +47,7 @@ Installation Dependencies ------------ -Currently, Linux and Mac OSX are supported. +Currently, Linux, Mac OSX, FreeBSD and OpenBSD are supported. Building ~~~~~~~~ From d516985146b5bb174fd01d6a45c228364665d8ca Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Wed, 21 Jan 2015 13:43:42 +0100 Subject: [PATCH 15/21] fix missing headers on OpenBSD --- openbsd/load.cc | 2 +- openbsd/memory.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/openbsd/load.cc b/openbsd/load.cc index 287f968..7c4a138 100644 --- a/openbsd/load.cc +++ b/openbsd/load.cc @@ -24,7 +24,7 @@ #include "cpu.h" #include "load.h" -#include "../luts.h" +#include "luts.h" // Load Averages std::string load_string( bool use_colors = false ) diff --git a/openbsd/memory.cc b/openbsd/memory.cc index 44eb849..5724303 100644 --- a/openbsd/memory.cc +++ b/openbsd/memory.cc @@ -25,8 +25,8 @@ #include "error.h" #include "memory.h" -#include "../luts.h" -#include "../conversions.h" +#include "luts.h" +#include "conversions.h" static int pageshift; From c052f58eab061462a00a5b481518c380b98abe1f Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Mon, 26 Jan 2015 19:48:56 +0100 Subject: [PATCH 16/21] Workaround for compilation on OpenBSD 5.6 On OpenBSD 5.6 sys/ucred.h uses NGROUP define, but misses include to sys/param.h in which NGROUP is defined. sys/ucred.h is included in sys/mount.h which we need to get the cached ram. Because of this include chain our compilation will fail with missing NGROUP define. This can be resolved by including in our code (and in correct order) system headers. --- CMakeLists.txt | 3 +++ openbsd/memory.cc | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b676edd..fd908b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,9 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "OpenBSD") message(STATUS "OpenBSD detected") message(WARNING "OpenBSD is still experimental!") set(METER_SOURCES "openbsd/memory.cc" "openbsd/cpu.cc" "openbsd/load.cc") + if(CMAKE_SYSTEM_VERSION VERSION_LESS 5.7) + add_definitions(-DOPENBSD_WORKAROUND=1) + endif() else() message(FATAL_ERROR "Cannot be compiled on this system") endif() diff --git a/openbsd/memory.cc b/openbsd/memory.cc index 5724303..832cbbf 100644 --- a/openbsd/memory.cc +++ b/openbsd/memory.cc @@ -18,9 +18,22 @@ #include #include + +// Ugly fix to compilation on OpenBSD 5.6 +// these need to be before include sys/mount.h +// and be in this exact order. Otherwise compiling on +// OpenBSD 5.6 will fail with missing NGROUPS define +// or missing gid_t typedefs +#ifdef OPENBSD_WORKAROUND +#include // typedefs +#include // missing NGROUPS +#include +#else +#include +#endif + #include // VFS_* which we use to get cache #include -#include #include // vmtotal struct #include "error.h" From ea98710e164305af8d721e0133c07c4393a07460 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Wed, 28 Jan 2015 11:32:44 +0100 Subject: [PATCH 17/21] removed messages about experimental *BSD support --- CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fd908b0..effea3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,12 +65,10 @@ elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin") set(METER_SOURCES "osx/memory.cc" "osx/cpu.cc" "osx/load.cc") elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") message(STATUS "FreeBSD detected") - message(WARNING "FreeBSD is still experimental!") set(METER_SOURCES "freebsd/memory.cc" "freebsd/cpu.cc" "freebsd/load.cc") elseif(CMAKE_SYSTEM_NAME MATCHES "OpenBSD") # OpenBSD Stuff Here message(STATUS "OpenBSD detected") - message(WARNING "OpenBSD is still experimental!") set(METER_SOURCES "openbsd/memory.cc" "openbsd/cpu.cc" "openbsd/load.cc") if(CMAKE_SYSTEM_VERSION VERSION_LESS 5.7) add_definitions(-DOPENBSD_WORKAROUND=1) From 33eac9d16dc9c4ea97b647104f5c668bdeb0d249 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Sun, 1 Feb 2015 16:38:49 +0100 Subject: [PATCH 18/21] openbsd: changed cpu stats errors --- openbsd/cpu.cc | 5 ++-- openbsd/error.cc | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 openbsd/error.cc diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index c2019e1..3d78f14 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -16,7 +16,6 @@ * limitations under the License. */ -#include #include // usleep #include @@ -51,7 +50,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 initial cpu stats"); } usleep(cpu_usage_delay); @@ -59,7 +58,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 updated cpu stats"); } // Current load times diff --git a/openbsd/error.cc b/openbsd/error.cc new file mode 100644 index 0000000..bc3dac3 --- /dev/null +++ b/openbsd/error.cc @@ -0,0 +1,70 @@ +/* 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. + */ + +#include +#include +#include + +#include "error.h" + +void error( const char * error ) +{ + using std::cerr; + using std::endl; + + cerr << error; + + switch( errno ) + { + case EFAULT: + cerr << "Bad address"; + break; + case EINVAL: + cerr << "Invalid argument"; + break; + case ENOMEM: + cerr << "Cannot allocate memory"; + break; + case ENOENT: + cerr << "No such file or directory"; + break; + case ENXIO: + cerr << "Device not configured"; + break; + case ENOTDIR: + cerr << "Not a directory"; + break; + case EOPNOTSUPP: + cerr << "Operation not supported"; + break; + case EPERM: + cerr << "Operation not permitted"; + break; + case ESRCH: + cerr << "No such process"; + break; + default: + cerr << "Unknown error code: " << errno; + break; + } + + cerr << endl; + + exit( 23 ); +} + From a727edef8f3ec294f933940c8eda75391feadd0d Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Sun, 1 Feb 2015 17:10:52 +0100 Subject: [PATCH 19/21] Openbsd: output error message on error --- openbsd/cpu.cc | 18 ++++++------ openbsd/error.cc | 70 ----------------------------------------------- openbsd/error.h | 13 +++++++-- openbsd/memory.cc | 9 ++---- 4 files changed, 22 insertions(+), 88 deletions(-) delete mode 100644 openbsd/error.cc diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index 3d78f14..43cd704 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -30,9 +30,9 @@ uint8_t get_cpu_count() int mib[2] = { CTL_HW, HW_NCPU }; size_t len = sizeof( cpu_count ); - if(sysctl(mib, 2, &cpu_count, &len, NULL, 0) < 0) + 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; @@ -48,17 +48,17 @@ float cpu_percentage( unsigned int cpu_usage_delay ) size_t size = sizeof( load1 ); // get cpu times - if(sysctl(cpu_ctl, 2, &load1, &size, NULL, 0) < 0) + if( sysctl( cpu_ctl, 2, &load1, &size, NULL, 0 ) < 0 ) { - error("sysctl: error getting initial cpu stats"); + error( "sysctl: error getting initial cpu stats" ); } - usleep(cpu_usage_delay); + usleep( cpu_usage_delay ); // update cpu times - if(sysctl(cpu_ctl, 2, &load2, &size, NULL, 0) < 0) + if( sysctl( cpu_ctl, 2, &load2, &size, NULL, 0 ) < 0 ) { - error("sysctl: error getting updated cpu stats"); + error( "sysctl: error getting updated cpu stats" ); } // Current load times @@ -77,7 +77,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/openbsd/error.cc b/openbsd/error.cc deleted file mode 100644 index bc3dac3..0000000 --- a/openbsd/error.cc +++ /dev/null @@ -1,70 +0,0 @@ -/* 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. - */ - -#include -#include -#include - -#include "error.h" - -void error( const char * error ) -{ - using std::cerr; - using std::endl; - - cerr << error; - - switch( errno ) - { - case EFAULT: - cerr << "Bad address"; - break; - case EINVAL: - cerr << "Invalid argument"; - break; - case ENOMEM: - cerr << "Cannot allocate memory"; - break; - case ENOENT: - cerr << "No such file or directory"; - break; - case ENXIO: - cerr << "Device not configured"; - break; - case ENOTDIR: - cerr << "Not a directory"; - break; - case EOPNOTSUPP: - cerr << "Operation not supported"; - break; - case EPERM: - cerr << "Operation not permitted"; - break; - case ESRCH: - cerr << "No such process"; - break; - default: - cerr << "Unknown error code: " << errno; - break; - } - - cerr << endl; - - exit( 23 ); -} - diff --git a/openbsd/error.h b/openbsd/error.h index 540b334..b4c6a7e 100644 --- a/openbsd/error.h +++ b/openbsd/error.h @@ -20,10 +20,17 @@ #define BSD_ERROR_H_ #include +#include +#include +#include // strerror -inline void error(const char * error) { - std::cerr << error << std::endl; - exit(23); +inline void error( const char * error ) +{ + using std::cerr; + using std::endl; + + cerr << error << ": " << strerror( errno ) << endl; + exit( 23 ); } #endif diff --git a/openbsd/memory.cc b/openbsd/memory.cc index 832cbbf..dc41c58 100644 --- a/openbsd/memory.cc +++ b/openbsd/memory.cc @@ -59,10 +59,9 @@ std::string mem_string( bool use_colors = false ) 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) + if( sysctl( hw_pagesize, 2, &page_size, &size, NULL, 0 ) < 0 ) { - error("memory: error getting page size"); - exit(23); + error( "memory: error getting page size" ); } // calculate how far we must shift the variables @@ -79,8 +78,7 @@ std::string mem_string( bool use_colors = false ) size = sizeof( vm_total ); if( sysctl( vm_totalmem, 2, &vm_total, &size, NULL, 0 ) < 0 ) { - error("memory: error getting vm memory stats"); - exit(23); + error( "memory: error getting vm memory stats" ); } // In case we need it, this gets the cached memory (bcstats.numbufpages) @@ -90,7 +88,6 @@ std::string mem_string( bool use_colors = false ) 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 From d21be2b87c2269c12aa3c585d3ab0a21bb19d35e Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Sun, 1 Feb 2015 17:29:04 +0100 Subject: [PATCH 20/21] openbsd: use unsigned ints to store cpu stats --- openbsd/cpu.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index 43cd704..9af4eb5 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -42,8 +42,8 @@ float cpu_percentage( unsigned int cpu_usage_delay ) { int cpu_ctl[] = { CTL_KERN, KERN_CPTIME }; - int32_t load1[CPUSTATES]; - int32_t load2[CPUSTATES]; + u_int32_t load1[CPUSTATES]; + u_int32_t load2[CPUSTATES]; size_t size = sizeof( load1 ); From 80d70b7a4d5ee9a4ba79deae0e840c65ccaf4131 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Thu, 12 Feb 2015 19:59:22 +0100 Subject: [PATCH 21/21] fix for cpu systctl failing on 64 bit OpenBSD On 64bit system KERN_CPTIME systctl gets returned as 64bit uint. On 32bit system it's returned as 32bit uint. This is not documented anywhere (or maybe I've missed it). I've added preprocessor test for 64bit system. --- common/main.cc | 4 ++-- openbsd/cpu.cc | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/common/main.cc b/common/main.cc index a5fa0db..e1bd9ba 100644 --- a/common/main.cc +++ b/common/main.cc @@ -73,7 +73,7 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines, if( graph_lines > 0) { - oss << "["; + oss << " ["; oss << get_graph_by_percentage( unsigned( percentage ), graph_lines ); oss << "]"; } @@ -173,7 +173,7 @@ int main( int argc, char** argv ) return EXIT_FAILURE; } - std::cout << mem_string( use_colors ) << ' ' + std::cout << mem_string( use_colors ) << cpu_string( cpu_usage_delay, graph_lines, use_colors ) << ' ' << load_string( use_colors ); diff --git a/openbsd/cpu.cc b/openbsd/cpu.cc index 9af4eb5..fefc215 100644 --- a/openbsd/cpu.cc +++ b/openbsd/cpu.cc @@ -42,8 +42,18 @@ float cpu_percentage( unsigned int cpu_usage_delay ) { int cpu_ctl[] = { CTL_KERN, KERN_CPTIME }; + // on 64bit systems KERN_CPTIME gets reported as 64bit + // uint. Detect 64bit system and define array to hold the + // stats accordingly. + // NOTE: the following test may need to be extended to cover + // more 64bit platforms. +#if __x86_64__ || __ppc64__ + u_int64_t load1[CPUSTATES]; + u_int64_t load2[CPUSTATES]; +#else u_int32_t load1[CPUSTATES]; u_int32_t load2[CPUSTATES]; +#endif size_t size = sizeof( load1 );