*BDS port:
- split *BDS files - add CMake confingure instructions on bsd systems - add includes to main file
This commit is contained in:
parent
6b8b746a0f
commit
f59b11971f
@ -34,11 +34,13 @@ ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin")
|
|||||||
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
|
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
|
||||||
# FreeBSD STUFF HERE
|
# FreeBSD STUFF HERE
|
||||||
message( "FreeBSD detected")
|
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")
|
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
|
||||||
# OpenBSD Stuff Here
|
# OpenBSD Stuff Here
|
||||||
message( "OpenBSD detected")
|
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()
|
ELSE()
|
||||||
message( FATAL_ERROR "Cannot be compiled on this system" )
|
message( FATAL_ERROR "Cannot be compiled on this system" )
|
||||||
endif()
|
endif()
|
||||||
|
145
bsd/FreeBSD.cpp
145
bsd/FreeBSD.cpp
@ -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 <Justasic@gmail.com>
|
|
||||||
// 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 <unistd.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <cerrno>
|
|
||||||
#include <string>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <cmath>
|
|
||||||
#include <sys/sysctl.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
// 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<float>(diff_user + diff_system + diff_nice)/static_cast<float>(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<float>(averages[i]) * 100 + 0.5) / 100;
|
|
||||||
ss << avg << " ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
145
bsd/OpenBSD.cpp
145
bsd/OpenBSD.cpp
@ -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 <Justasic@gmail.com>
|
|
||||||
// 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 <unistd.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <cerrno>
|
|
||||||
#include <string>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <cmath>
|
|
||||||
#include <sys/sysctl.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
// 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<float>(diff_user + diff_system + diff_nice)/static_cast<float>(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<float>(averages[i]) * 100 + 0.5) / 100;
|
|
||||||
ss << avg << " ";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ss.str();
|
|
||||||
}
|
|
||||||
|
|
59
bsd/common.h
Normal file
59
bsd/common.h
Normal file
@ -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 <Justasic@gmail.com>
|
||||||
|
// 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 <iostream>
|
||||||
|
#include <cerrno>
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
// 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<unsigned long>(len) << " bytes, got "
|
||||||
|
<< static_cast<unsigned long>(nlen) << " bytes\n";
|
||||||
|
//exit(23);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
53
bsd/cpu.cc
Normal file
53
bsd/cpu.cc
Normal file
@ -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 <Justasic@gmail.com>
|
||||||
|
// 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 <sys/types.h>
|
||||||
|
|
||||||
|
#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<float>(diff_user + diff_system + diff_nice) /
|
||||||
|
static_cast<float>(diff_user + diff_system + diff_nice + diff_idle) *
|
||||||
|
100.0;
|
||||||
|
}
|
6
bsd/cpu.h
Normal file
6
bsd/cpu.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#ifndef CPU_H_
|
||||||
|
#define CPU_H_
|
||||||
|
|
||||||
|
float cpu_percentage(unsigned);
|
||||||
|
|
||||||
|
#endif
|
47
bsd/load.cc
Normal file
47
bsd/load.cc
Normal file
@ -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 <Justasic@gmail.com>
|
||||||
|
// 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 <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <stdlib.h> // getloadavg()
|
||||||
|
#include <cmath> // 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<float>(averages[i]) * 100 + 0.5) / 100;
|
||||||
|
ss << avg << " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
8
bsd/load.h
Normal file
8
bsd/load.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef LOAD_H_
|
||||||
|
#define LOAD_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string load_string( bool );
|
||||||
|
|
||||||
|
#endif
|
8
bsd/memory.h
Normal file
8
bsd/memory.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef MEMORY_H_
|
||||||
|
#define MEMORY_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::string mem_string( bool );
|
||||||
|
|
||||||
|
#endif
|
61
bsd/memory_freebsd.cc
Normal file
61
bsd/memory_freebsd.cc
Normal file
@ -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 <Justasic@gmail.com>
|
||||||
|
// 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 <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#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<int64_t>(active_mem) + static_cast<int64_t>(inactive_mem) +
|
||||||
|
static_cast<int64_t>(wired_mem)) * static_cast<int64_t>(page_size);
|
||||||
|
|
||||||
|
oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB";
|
||||||
|
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
63
bsd/memory_openbsd.cc
Normal file
63
bsd/memory_openbsd.cc
Normal file
@ -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 <Justasic@gmail.com>
|
||||||
|
// 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 <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#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<int64_t>(active_mem) +
|
||||||
|
static_cast<int64_t>(wired_mem) +
|
||||||
|
static_cast<int64_t>(inactive_mem)) *
|
||||||
|
static_cast<int64_t>(page_size);
|
||||||
|
|
||||||
|
oss << MEGABYTES(used_mem) << '/' << MEGABYTES(total_mem) << "MB";
|
||||||
|
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
@ -34,10 +34,10 @@
|
|||||||
#include "osx/load.h"
|
#include "osx/load.h"
|
||||||
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
||||||
// BSD system
|
// BSD system
|
||||||
// TODO: Includes and *BSD support
|
|
||||||
#define BSD_BASED 1
|
#define BSD_BASED 1
|
||||||
// include _get_cpu_percentage (see osx/cpu.cc)
|
#include "bsd/cpu.h"
|
||||||
// include cpu_percentage (see osx/cpu.cc)
|
#include "bsd/load.h"
|
||||||
|
#include "bsd/memory.h"
|
||||||
#else
|
#else
|
||||||
// assume linux system
|
// assume linux system
|
||||||
#include "linux/cpu.h"
|
#include "linux/cpu.h"
|
||||||
@ -47,13 +47,6 @@
|
|||||||
|
|
||||||
#include "graph.h"
|
#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,
|
std::string cpu_string(unsigned int cpu_usage_delay, unsigned int graph_lines,
|
||||||
bool use_colors = false) {
|
bool use_colors = false) {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user