diff --git a/CMakeLists.txt b/CMakeLists.txt index eef1146..cf08326 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,11 +34,13 @@ ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin") ELSEIF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD") # FreeBSD STUFF HERE message( "FreeBSD detected") - message( FATAL_ERROR "Free BSD is not supported yet" ) + message( WARNING "FreeBSD is still experimental!" ) + SET( METER_SOURCES "bsd/memory_freebsd.cc" "bsd/cpu.cc" "bsd/load.cc" ) ELSEIF(CMAKE_SYSTEM_NAME MATCHES "OpenBSD") # OpenBSD Stuff Here message( "OpenBSD detected") - message( FATAL_ERROR "OpenBSD is not supported yet" ) + message( WARNING "OpenBSD is still experimental!" ) + SET( METER_SOURCES "bsd/memory_openbsd.cc" "bsd/cpu.cc" "bsd/load.cc" ) ELSE() message( FATAL_ERROR "Cannot be compiled on this system" ) endif() diff --git a/bsd/FreeBSD.cpp b/bsd/FreeBSD.cpp deleted file mode 100644 index 725c240..0000000 --- a/bsd/FreeBSD.cpp +++ /dev/null @@ -1,145 +0,0 @@ -/* - * 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 -#include -#include -#include -#include -#include -#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 - -#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) -static inline void getsysctl(const char *name, void *ptr, size_t len) -{ - size_t nlen = len; - - if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) - { - fprintf(stderr, "sysctl(%s...) failed: %s\n", name, strerror(errno)); - exit(23); - } - - if (nlen != len) - { - fprintf(stderr, "sysctl(%s...) expected %lu bytes, got %lu bytes\n", name, (unsigned long)len, (unsigned long)nlen); - //exit(23); - } -} - -float cpu_percentage(unsigned int cpu_usage_delay) -{ - int64_t load1[CPUSTATES]; - int64_t load2[CPUSTATES]; - - GETSYSCTL("kern.cp_time", load1); - usleep(cpu_usage_delay); - GETSYSCTL("kern.cp_time", load2); - - // 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; -} - -std::string mem_string() -{ - // 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); - GETSYSCTL("vm.stats.vm.v_active_count", active_mem); - - // Get all memory which can be allocated - //unused_mem = (cache_mem + free_mem) * page_size; - used_mem = ((int64_t)active_mem + (int64_t)inactive_mem + (int64_t)wired_mem) * (int64_t)page_size; - - oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB"; - - return oss.str(); -} - -// Load Averages -std::string load_string() -{ - std::stringstream ss; - // Only get 3 load averages - int nelem = 3; - double averages[3]; - // based on: http://www.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 - { - 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 << " "; - } - } - - return ss.str(); -} - diff --git a/bsd/OpenBSD.cpp b/bsd/OpenBSD.cpp deleted file mode 100644 index f25e7d0..0000000 --- a/bsd/OpenBSD.cpp +++ /dev/null @@ -1,145 +0,0 @@ -/* - * 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 - -#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 -#include -#include -#include -#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 - -#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) -static inline void getsysctl(const char *name, void *ptr, size_t len) -{ - size_t nlen = len; - - if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) - { - fprintf(stderr, "sysctl(%s...) failed: %s\n", name, strerror(errno)); - exit(23); - } - - if (nlen != len) - { - fprintf(stderr, "sysctl(%s...) expected %lu bytes, got %lu bytes\n", name, (unsigned long)len, (unsigned long)nlen); - //exit(23); - } -} - -float cpu_percentage(unsigned int cpu_usage_delay) -{ - int64_t load1[CPUSTATES]; - int64_t load2[CPUSTATES]; - - GETSYSCTL("kern.cp_time", load1); - usleep(cpu_usage_delay); - GETSYSCTL("kern.cp_time", load2); - - // 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; -} - -std::string mem_string() -{ - // 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 = ((int64_t)active_mem + (int64_t)wired_mem + (int64_t)inactive_mem) * (int64_t)page_size; - - oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB"; - - return oss.str(); -} - -// Load Averages -std::string load_string() -{ - std::stringstream ss; - // Only get 3 load averages - int nelem = 3; - double averages[3]; - // based on: http://www.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 - { - 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 << " "; - } - } - - return ss.str(); -} - diff --git a/bsd/common.h b/bsd/common.h new file mode 100644 index 0000000..6925357 --- /dev/null +++ b/bsd/common.h @@ -0,0 +1,59 @@ +/* + * 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 +#include +#include +#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 + +#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) +static inline void getsysctl(const char *name, void *ptr, size_t len) { + size_t nlen = len; + + if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { + std::cerr << "sysctl(" << name << "...) failed: " << strerror(errno) + << std::endl; + exit(23); + } + + if (nlen != len) { + std::cerr << "sysctl(" << name << "...) expected " + << static_cast(len) << " bytes, got " + << static_cast(nlen) << " bytes\n"; + //exit(23); + } +} + +#endif diff --git a/bsd/cpu.cc b/bsd/cpu.cc new file mode 100644 index 0000000..a11b318 --- /dev/null +++ b/bsd/cpu.cc @@ -0,0 +1,53 @@ +/* + * 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 "common.h" +#include "cpu.h" + +float cpu_percentage(unsigned int cpu_usage_delay) +{ + int64_t load1[CPUSTATES]; + int64_t load2[CPUSTATES]; + + GETSYSCTL("kern.cp_time", load1); + usleep(cpu_usage_delay); + GETSYSCTL("kern.cp_time", load2); + + // 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/bsd/cpu.h b/bsd/cpu.h new file mode 100644 index 0000000..beee11f --- /dev/null +++ b/bsd/cpu.h @@ -0,0 +1,6 @@ +#ifndef CPU_H_ +#define CPU_H_ + +float cpu_percentage(unsigned); + +#endif diff --git a/bsd/load.cc b/bsd/load.cc new file mode 100644 index 0000000..3515d5b --- /dev/null +++ b/bsd/load.cc @@ -0,0 +1,47 @@ +/* + * 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 "load.h" + +// Load Averages +std::string load_string() { + std::stringstream ss; + // Only get 3 load averages + int nelem = 3; + double averages[3]; + // 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 { + 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 << " "; + } + } + + return ss.str(); +} + diff --git a/bsd/load.h b/bsd/load.h new file mode 100644 index 0000000..3b8d814 --- /dev/null +++ b/bsd/load.h @@ -0,0 +1,8 @@ +#ifndef LOAD_H_ +#define LOAD_H_ + +#include + +std::string load_string( bool ); + +#endif diff --git a/bsd/memory.h b/bsd/memory.h new file mode 100644 index 0000000..196dfd6 --- /dev/null +++ b/bsd/memory.h @@ -0,0 +1,8 @@ +#ifndef MEMORY_H_ +#define MEMORY_H_ + +#include + +std::string mem_string( bool ); + +#endif diff --git a/bsd/memory_freebsd.cc b/bsd/memory_freebsd.cc new file mode 100644 index 0000000..e4991d6 --- /dev/null +++ b/bsd/memory_freebsd.cc @@ -0,0 +1,61 @@ +/* + * 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 + +#include "common.h" +#include "memory.h" + +std::string mem_string() { + // 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); + GETSYSCTL("vm.stats.vm.v_active_count", active_mem); + + // Get all memory which can be allocated + //unused_mem = (cache_mem + free_mem) * page_size; + used_mem = ( + static_cast(active_mem) + static_cast(inactive_mem) + + static_cast(wired_mem)) * static_cast(page_size); + + oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB"; + + return oss.str(); +} + diff --git a/bsd/memory_openbsd.cc b/bsd/memory_openbsd.cc new file mode 100644 index 0000000..574fea0 --- /dev/null +++ b/bsd/memory_openbsd.cc @@ -0,0 +1,63 @@ +/* + * 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 + +#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 "common.h" +#include "memory.h" + +std::string mem_string() { + // 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); + + oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB"; + + return oss.str(); +} + diff --git a/tmux-mem-cpu-load.cpp b/tmux-mem-cpu-load.cpp index 4e343db..979f469 100644 --- a/tmux-mem-cpu-load.cpp +++ b/tmux-mem-cpu-load.cpp @@ -34,10 +34,10 @@ #include "osx/load.h" #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) // BSD system - // TODO: Includes and *BSD support #define BSD_BASED 1 - // include _get_cpu_percentage (see osx/cpu.cc) - // include cpu_percentage (see osx/cpu.cc) + #include "bsd/cpu.h" + #include "bsd/load.h" + #include "bsd/memory.h" #else // assume linux system #include "linux/cpu.h" @@ -47,13 +47,6 @@ #include "graph.h" -// Function declarations. -// TODO: those should stay in separate headers -// LINUX: DONE/partial -// OSX: DONE/partial -// BSD: TODO - - std::string cpu_string(unsigned int cpu_usage_delay, unsigned int graph_lines, bool use_colors = false) {