code syntax style, vim modelines, license

* Use std::sting instead of char[] in the graph drawing functions. This is the
  only change to the code. Rest is just styling.
* Corrected whole code to follow Allman/GNU coding style, with 2 spaces for each
  indentation step.
* Added license headers to all code files
* Added vim modelines to all files. They sit on the first line and enable the
  following settings: 2 space indentation, tab expansion to spaces, line at
  80th column, automatic line breaking on "\ !@*+-;:,./?" characters, automatic
  line break if line exceeds 80 colums. This should keep the code nice and tidy.
This commit is contained in:
Pawel "l0ner" Soltys 2015-01-18 14:31:04 +01:00
parent d5506ac797
commit 51f85ba032
25 changed files with 830 additions and 415 deletions

@ -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.
#
cmake_minimum_required(VERSION 2.6) cmake_minimum_required(VERSION 2.6)
if(COMMAND cmake_policy) if(COMMAND cmake_policy)
cmake_policy(VERSION 2.6) cmake_policy(VERSION 2.6)

@ -1,5 +1,7 @@
/* /*
* Copyright 2012 Matthew McCormick * Copyright 2012 Matthew McCormick
* Copyright 2013 Justin Crawford <Justasic@gmail.com>
* Copyright 2015 Pawel 'l0ner' Soltys
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -12,8 +14,8 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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: github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c
// Based on: Apple.cpp for load_string/mem_string and apple's documentation // Based on: Apple.cpp for load_string/mem_string and apple's documentation
@ -39,16 +41,19 @@
#define CPUSTATES 5 #define CPUSTATES 5
#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var)) #define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
static inline void getsysctl(const char *name, void *ptr, size_t len) { static inline void getsysctl( const char *name, void *ptr, size_t len )
{
size_t nlen = len; size_t nlen = len;
if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) { if( sysctlbyname( name, ptr, &nlen, NULL, 0 ) == -1 )
{
std::cerr << "sysctl(" << name << "...) failed: " << strerror( errno ) std::cerr << "sysctl(" << name << "...) failed: " << strerror( errno )
<< std::endl; << std::endl;
exit( 23 ); exit( 23 );
} }
if (nlen != len) { if( nlen != len )
{
std::cerr << "sysctl(" << name << "...) expected " std::cerr << "sysctl(" << name << "...) expected "
<< static_cast<unsigned long>( len ) << " bytes, got " << static_cast<unsigned long>( len ) << " bytes, got "
<< static_cast<unsigned long>( nlen ) << " bytes\n"; << static_cast<unsigned long>( nlen ) << " bytes\n";

@ -1,5 +1,8 @@
/* /* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
*
* Copyright 2012 Matthew McCormick * Copyright 2012 Matthew McCormick
* Copyright 2013 Justin Crawford <Justasic@gmail.com>
* Copyright 2015 Pawel 'l0ner' Soltys
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -12,8 +15,8 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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: github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c
// Based on: Apple.cpp for load_string/mem_string and apple's documentation // Based on: Apple.cpp for load_string/mem_string and apple's documentation

@ -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_ #ifndef CPU_H_
#define CPU_H_ #define CPU_H_

@ -1,5 +1,8 @@
/* /* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
*
* Copyright 2012 Matthew McCormick * Copyright 2012 Matthew McCormick
* Copyright 2013 Justin Crawford <Justasic@gmail.com>
* Copyright 2015 Pawel 'l0ner' Soltys
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -12,11 +15,10 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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
// 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 <sstream> #include <sstream>
#include <string> #include <string>
@ -29,38 +31,48 @@
#include "../luts.h" #include "../luts.h"
// Load Averages // Load Averages
std::string load_string( bool use_colors = false ) { std::string load_string( bool use_colors = false )
{
std::stringstream ss; std::stringstream ss;
// Only get 3 load averages // Get only 3 load averages
int nelem = 3; const int nelem = 3;
double averages[3]; double averages[nelem];
// based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c // based on: opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c
if( getloadavg( averages, nelem ) < 0 ) 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 ) { else
{
if( use_colors )
{
// may not work // may not work
int32_t cpu_count = 0; int32_t cpu_count = 0;
GETSYSCTL( "hw.ncpu", cpu_count ); GETSYSCTL( "hw.ncpu", cpu_count );
unsigned load_percent = static_cast<unsigned int>( unsigned load_percent = static_cast<unsigned int>( averages[0] /
averages[0] / cpu_count * 0.5f * 100.0f); cpu_count * 0.5f * 100.0f );
if( load_percent > 100 ) if( load_percent > 100 )
{
load_percent = 100; load_percent = 100;
}
ss << load_lut[load_percent]; ss << load_lut[load_percent];
} }
for(int i = 0; i < nelem; ++i) { for( int i = 0; i < nelem; ++i )
{
// Round to nearest, make sure this is only a 0.00 value not a 0.0000 // 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; float avg = floorf( static_cast<float>( averages[i] ) * 100 + 0.5 ) / 100;
ss << avg << " "; ss << avg << " ";
} }
if( use_colors ) if( use_colors )
{
ss << "#[fg=default,bg=default]"; ss << "#[fg=default,bg=default]";
}
} }

@ -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_ #ifndef LOAD_H_
#define LOAD_H_ #define 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 MEMORY_H_ #ifndef MEMORY_H_
#define MEMORY_H_ #define MEMORY_H_

@ -1,5 +1,7 @@
/* /* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
*
* Copyright 2012 Matthew McCormick * Copyright 2012 Matthew McCormick
* Copyright 2015 Pawel 'l0ner' Soltys
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -12,11 +14,10 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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
// 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 <sstream> #include <sstream>
#include <string> #include <string>
@ -26,7 +27,8 @@
#include "memory.h" #include "memory.h"
#include "../luts.h" #include "../luts.h"
std::string mem_string( bool use_colors = false ) { std::string mem_string( bool use_colors = false )
{
// These values are in bytes // These values are in bytes
int32_t total_mem = 0; int32_t total_mem = 0;
int64_t used_mem = 0; int64_t used_mem = 0;
@ -51,17 +53,21 @@ std::string mem_string( bool use_colors = false ) {
// Get all memory which can be allocated // Get all memory which can be allocated
//unused_mem = (cache_mem + free_mem) * page_size; //unused_mem = (cache_mem + free_mem) * page_size;
used_mem = ( used_mem = ( static_cast<int64_t>( active_mem ) +
static_cast<int64_t>(active_mem) + static_cast<int64_t>(inactive_mem) + static_cast<int64_t>( inactive_mem ) +
static_cast<int64_t>( wired_mem ) ) * static_cast<int64_t>( page_size ); static_cast<int64_t>( wired_mem ) ) * static_cast<int64_t>( page_size );
if( use_colors ) if( use_colors )
{
oss << mem_lut[( 100 * used_mem ) / total_mem]; oss << mem_lut[( 100 * used_mem ) / total_mem];
}
oss << MEGABYTES( used_mem ) << '/' << MEGABYTES( total_mem ) << "MB"; oss << MEGABYTES( used_mem ) << '/' << MEGABYTES( total_mem ) << "MB";
if( use_colors ) if( use_colors )
{
oss << "#[fg=default,bg=default]"; oss << "#[fg=default,bg=default]";
}
return oss.str(); return oss.str();
} }

@ -1,5 +1,8 @@
/* /* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
*
* Copyright 2012 Matthew McCormick * Copyright 2012 Matthew McCormick
* Copyright 2013 Justin Crawford <Justasic@gmail.com>
* Copyright 2015 Pawel 'l0ner' Soltys
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -12,9 +15,9 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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: github.com/freebsd/freebsd/blob/master/usr.bin/top/machine.c
// Based on: Apple.cpp for load_string/mem_string and apple's documentation // 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) #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)
@ -27,7 +30,8 @@
#include "memory.h" #include "memory.h"
#include "../luts.h" #include "../luts.h"
std::string mem_string( bool use_colors = false ) { std::string mem_string( bool use_colors = false )
{
// These values are in bytes // These values are in bytes
int64_t total_mem = 0; int64_t total_mem = 0;
int64_t used_mem = 0; int64_t used_mem = 0;
@ -56,12 +60,16 @@ std::string mem_string( bool use_colors = false ) {
static_cast<int64_t>( inactive_mem ) ) * static_cast<int64_t>( page_size ); static_cast<int64_t>( inactive_mem ) ) * static_cast<int64_t>( page_size );
if( use_colors ) if( use_colors )
{
oss << mem_lut[( 100 * used_mem ) / total_mem]; oss << mem_lut[( 100 * used_mem ) / total_mem];
}
oss << MEGABYTES( used_mem ) << '/' << MEGABYTES( total_mem ) << "MB"; oss << MEGABYTES( used_mem ) << '/' << MEGABYTES( total_mem ) << "MB";
if( use_colors ) if( use_colors )
{
oss << "#[fg=default,bg=default]"; oss << "#[fg=default,bg=default]";
}
return oss.str(); return oss.str();
} }

@ -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.
*/
#include <string> #include <string>
#include <cstring> #include <cstring>

18
graph.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 GRAPH_H_ #ifndef GRAPH_H_
#define GRAPH_H_ #define GRAPH_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.
*/
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <unistd.h> // usleep #include <unistd.h> // usleep
@ -5,10 +23,11 @@
#include "cpu.h" #include "cpu.h"
#include "../luts.h" #include "../luts.h"
float cpu_percentage( unsigned cpu_usage_delay ) { float cpu_percentage( unsigned cpu_usage_delay )
{
std::string line; std::string line;
size_t substrStart = 0; size_t substr_start = 0;
size_t substrLen; size_t substr_len;
// cpu stats // cpu stats
// user, nice, system, idle // user, nice, system, idle
@ -20,13 +39,13 @@ float cpu_percentage( unsigned cpu_usage_delay ) {
stat_file.close(); stat_file.close();
// skip "cpu" // skip "cpu"
substrLen = line.find_first_of(" ", 3); substr_len = line.find_first_of( " ", 3 );
// parse cpu line // parse cpu line
for(unsigned i=0; i < 4; i++) { for( unsigned i=0; i < 4; i++ )
substrStart = line.find_first_not_of(" ", substrLen); {
substrLen = line.find_first_of (" ", substrStart); substr_start = line.find_first_not_of( " ", substr_len );
stats[i] = std::stoll( substr_len = line.find_first_of( " ", substr_start );
line.substr(substrStart, substrLen)); stats[i] = std::stoll( line.substr( substr_start, substr_len ) );
} }
usleep( cpu_usage_delay ); usleep( cpu_usage_delay );
@ -36,13 +55,13 @@ float cpu_percentage( unsigned cpu_usage_delay ) {
stat_file.close(); stat_file.close();
// skip "cpu" // skip "cpu"
substrLen = line.find_first_of(" ", 3); substr_len = line.find_first_of( " ", 3 );
// parse cpu line // parse cpu line
for(unsigned i=0; i < 4; i++) { for( unsigned i=0; i < 4; i++ )
substrStart = line.find_first_not_of(" ", substrLen); {
substrLen = line.find_first_of (" ", substrStart); substr_start = line.find_first_not_of( " ", substr_len );
stats[i] = std::stoll( substr_len = line.find_first_of ( " ", substr_start );
line.substr(substrStart, substrLen)) - stats[i]; stats[i] = std::stoll( line.substr( substr_start, substr_len ) ) - stats[i];
} }
return static_cast<float>( stats[0] + stats[1] + stats[2]) / return static_cast<float>( stats[0] + stats[1] + stats[2]) /

@ -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_ #ifndef CPU_H_
#define CPU_H_ #define 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.
*/
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <unistd.h> // sysconf()? #include <unistd.h> // sysconf()?
@ -7,7 +25,8 @@
#include "load.h" #include "load.h"
#include "../luts.h" #include "../luts.h"
std::string load_string( bool use_colors = false ) { std::string load_string( bool use_colors = false )
{
std::ostringstream oss; std::ostringstream oss;
float f = static_cast<float>( 1 << SI_LOAD_SHIFT ); float f = static_cast<float>( 1 << SI_LOAD_SHIFT );
@ -15,7 +34,8 @@ std::string load_string( bool use_colors = false ) {
struct sysinfo sinfo; struct sysinfo sinfo;
sysinfo( &sinfo ); sysinfo( &sinfo );
if( use_colors ) { if( use_colors )
{
// Likely does not work on BSD, but not tested // Likely does not work on BSD, but not tested
unsigned number_of_cpus = sysconf( _SC_NPROCESSORS_ONLN ); unsigned number_of_cpus = sysconf( _SC_NPROCESSORS_ONLN );
@ -27,7 +47,9 @@ std::string load_string( bool use_colors = false ) {
recent_load / number_of_cpus * 0.5f * 100.0f ); recent_load / number_of_cpus * 0.5f * 100.0f );
if( load_percent > 100 ) if( load_percent > 100 )
{
load_percent = 100; load_percent = 100;
}
oss << load_lut[load_percent]; oss << load_lut[load_percent];
} }
@ -40,7 +62,9 @@ std::string load_string( bool use_colors = false ) {
<< sinfo.loads[2] / f; << sinfo.loads[2] / f;
if( use_colors ) if( use_colors )
{
oss << "#[fg=default,bg=default]"; oss << "#[fg=default,bg=default]";
}
return oss.str(); return oss.str();
} }

@ -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_ #ifndef LOAD_H_
#define LOAD_H_ #define LOAD_H_

@ -1,10 +1,29 @@
/* 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 <sstream> #include <sstream>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include "memory.h" #include "memory.h"
#include "../luts.h" #include "../luts.h"
std::string mem_string( bool use_colors = false ) { std::string mem_string( bool use_colors = false )
{
std::ostringstream oss; std::ostringstream oss;
struct sysinfo sinfo; struct sysinfo sinfo;
@ -15,13 +34,15 @@ std::string mem_string( bool use_colors = false ) {
// we don't need this for now // we don't need this for now
//unsigned int unused_mem = sinfo.freeram / 1024; //unsigned int unused_mem = sinfo.freeram / 1024;
if( use_colors ) { if( use_colors )
{
oss << mem_lut[(100 * used_mem) / total_mem]; oss << mem_lut[(100 * used_mem) / total_mem];
} }
oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB"; oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB";
if( use_colors ) { if( use_colors )
{
oss << "#[fg=default,bg=default]"; oss << "#[fg=default,bg=default]";
} }

@ -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_ #ifndef MEMORY_H_
#define MEMORY_H_ #define MEMORY_H_

@ -1,3 +1,21 @@
/*
* Copyright 2012 Matthew McCormick
* Copyright 2013 Justin Crawford <Justasic@gmail.com>
* 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 <mach/mach.h> #include <mach/mach.h>
#include <unistd.h> // usleep() #include <unistd.h> // usleep()
@ -6,8 +24,9 @@
// OSX or BSD based system, use BSD APIs instead // OSX or BSD based system, use BSD APIs instead
// See: http://www.opensource.apple.com/source/xnu/xnu-201/osfmk/mach/host_info.h // See: http://www.opensource.apple.com/source/xnu/xnu-201/osfmk/mach/host_info.h
// and: http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/ // and: http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/
//
host_cpu_load_info_data_t _get_cpu_percentage() { host_cpu_load_info_data_t _get_cpu_percentage()
{
kern_return_t error; kern_return_t error;
mach_msg_type_number_t count; mach_msg_type_number_t count;
host_cpu_load_info_data_t r_load; host_cpu_load_info_data_t r_load;
@ -15,16 +34,19 @@ host_cpu_load_info_data_t _get_cpu_percentage() {
count = HOST_CPU_LOAD_INFO_COUNT; count = HOST_CPU_LOAD_INFO_COUNT;
mach_port = mach_host_self(); mach_port = mach_host_self();
error = host_statistics(mach_port, HOST_CPU_LOAD_INFO, ( error = host_statistics(mach_port, HOST_CPU_LOAD_INFO,
host_info_t)&r_load, &count); ( host_info_t )&r_load, &count );
if ( error != KERN_SUCCESS ) if ( error != KERN_SUCCESS )
{
return host_cpu_load_info_data_t(); return host_cpu_load_info_data_t();
}
return r_load; return r_load;
} }
float cpu_percentage( unsigned int cpu_usage_delay ) { float cpu_percentage( unsigned int cpu_usage_delay )
{
// Get the load times from the XNU kernel // Get the load times from the XNU kernel
host_cpu_load_info_data_t load1 = _get_cpu_percentage(); host_cpu_load_info_data_t load1 = _get_cpu_percentage();
usleep( cpu_usage_delay ); usleep( cpu_usage_delay );
@ -47,7 +69,7 @@ float cpu_percentage( unsigned int cpu_usage_delay ) {
unsigned long long diff_idle = next_idle - current_idle; unsigned long long diff_idle = next_idle - current_idle;
return static_cast<float>( diff_user + diff_system + diff_nice ) / return static_cast<float>( diff_user + diff_system + diff_nice ) /
static_cast<float>( static_cast<float>( diff_user + diff_system + diff_nice + diff_idle ) *
diff_user + diff_system + diff_nice + diff_idle) * 100.0; 100.0;
} }

@ -1,3 +1,20 @@
/*
* 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_ #ifndef CPU_H_
#define CPU_H_ #define CPU_H_

@ -1,3 +1,21 @@
/*
* Copyright 2012 Matthew McCormick
* Copyright 2013 Justin Crawford <Justasic@gmail.com>
* 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 <string> #include <string>
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
@ -9,30 +27,36 @@
#include "load.h" #include "load.h"
#include "../luts.h" #include "../luts.h"
std::string load_string( bool use_colors = false ) { std::string load_string( bool use_colors = false )
{
std::ostringstream oss; std::ostringstream oss;
// Both apple and BSD style systems have these api calls // Both apple and BSD style systems have these api calls
// Only get 3 load averages // Only get 3 load averages
int nelem = 3; const int nelem = 3;
double averages[3]; double averages[nelem];
// based on: // based on:
// http://www.opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c // http://www.opensource.apple.com/source/Libc/Libc-262/gen/getloadavg.c
if( getloadavg( averages, nelem ) < 0 ) if( getloadavg( averages, nelem ) < 0 )
{
oss << "0.00 0.00 0.00"; // couldn't get averages. oss << "0.00 0.00 0.00"; // couldn't get averages.
}
else else
for(int i = 0; i < nelem; ++i) { {
for( int i = 0; i < nelem; ++i )
{
// Round to nearest, make sure this is // Round to nearest, make sure this is
// only a 0.00 value not a 0.0000 // only a 0.00 value not a 0.0000
float avg = floorf( float avg = floorf( static_cast<float>( averages[i] ) * 100 + 0.5 ) / 100;
static_cast<float>(averages[i]) * 100 + 0.5) / 100;
oss << avg << " "; oss << avg << " ";
} }
}
std::string load_line( oss.str() ); std::string load_line( oss.str() );
oss.str( "" ); oss.str( "" );
if( use_colors ) { if( use_colors )
{
// Likely does not work on BSD, but not tested // Likely does not work on BSD, but not tested
unsigned number_of_cpus = sysconf( _SC_NPROCESSORS_ONLN ); unsigned number_of_cpus = sysconf( _SC_NPROCESSORS_ONLN );
@ -46,7 +70,9 @@ std::string load_string( bool use_colors = false ) {
recent_load / number_of_cpus * 0.5f * 100.0f ); recent_load / number_of_cpus * 0.5f * 100.0f );
if( load_percent > 100 ) if( load_percent > 100 )
{
load_percent = 100; load_percent = 100;
}
oss << load_lut[load_percent]; oss << load_lut[load_percent];
} }
@ -54,7 +80,9 @@ std::string load_string( bool use_colors = false ) {
oss << load_line.substr( 0, 14 ); oss << load_line.substr( 0, 14 );
if( use_colors ) if( use_colors )
{
oss << "#[fg=default,bg=default]"; oss << "#[fg=default,bg=default]";
}
return oss.str(); return oss.str();
} }

@ -1,3 +1,20 @@
/*
* 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_ #ifndef LOAD_H_
#define LOAD_H_ #define LOAD_H_

@ -1,3 +1,21 @@
/*
* Copyright 2012 Matthew McCormick
* Copyright 2013 Justin Crawford <Justasic@gmail.com>
* 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 <string> #include <string>
#include <sstream> #include <sstream>
#include <mach/mach.h> #include <mach/mach.h>
@ -6,7 +24,8 @@
#include "memory.h" #include "memory.h"
#include "../luts.h" #include "../luts.h"
std::string mem_string( bool use_colors ) { std::string mem_string( bool use_colors )
{
std::ostringstream oss; std::ostringstream oss;
// These values are in bytes // These values are in bytes
@ -30,14 +49,13 @@ std::string mem_string( bool use_colors ) {
count = sizeof( vm_stats ) / sizeof( natural_t ); count = sizeof( vm_stats ) / sizeof( natural_t );
if( KERN_SUCCESS == host_page_size( mach_port, &page_size ) && if( KERN_SUCCESS == host_page_size( mach_port, &page_size ) &&
KERN_SUCCESS == host_statistics( mach_port, HOST_VM_INFO, KERN_SUCCESS == host_statistics( mach_port, HOST_VM_INFO,
(host_info_t)&vm_stats, &count)) { ( host_info_t )&vm_stats, &count ) )
{
unused_mem = ( int64_t )vm_stats.free_count * ( int64_t )page_size; unused_mem = ( int64_t )vm_stats.free_count * ( int64_t )page_size;
used_mem = ( used_mem = ( ( int64_t )vm_stats.active_count +
(int64_t)vm_stats.active_count + ( int64_t )vm_stats.inactive_count + ( int64_t )vm_stats.wire_count
(int64_t)vm_stats.inactive_count + ) * ( int64_t )page_size;
(int64_t)vm_stats.wire_count) *
(int64_t)page_size;
} }
// To kilobytes // To kilobytes
@ -45,12 +63,16 @@ std::string mem_string( bool use_colors ) {
total_mem /= 1024; total_mem /= 1024;
if( use_colors ) if( use_colors )
{
oss << mem_lut[( 100 * used_mem ) / total_mem]; oss << mem_lut[( 100 * used_mem ) / total_mem];
}
oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB"; oss << used_mem / 1024 << '/' << total_mem / 1024 << "MB";
if( use_colors ) if( use_colors )
{
oss << "#[fg=default,bg=default]"; oss << "#[fg=default,bg=default]";
}
return oss.str(); return oss.str();
} }

@ -1,3 +1,21 @@
/*
* Copyright 2012 Matthew McCormick
* Copyright 2013 Justin Crawford <Justasic@gmail.com>
* 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_ #ifndef MEMORY_H_
#define MEMORY_H_ #define MEMORY_H_

@ -1,4 +1,5 @@
/* /* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
*
* Copyright 2012 Matthew McCormick * Copyright 2012 Matthew McCormick
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -12,7 +13,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
* */ */
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
@ -20,13 +21,14 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <cstdlib> // EXIT_SUCCESS #include <cstdlib> // EXIT_SUCCESS
#include "argParse/argParse.h" #include "argParse/argParse.h"
#include "version.h"
#include "graph.h"
// Tmux color lookup tables for the different metrics. // Tmux color lookup tables for the different metrics.
#include "luts.h" #include "luts.h"
#include "version.h"
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
// Apple osx system // Apple osx system
#include "osx/cpu.h" #include "osx/cpu.h"
@ -45,10 +47,9 @@
#include "linux/load.h" #include "linux/load.h"
#endif #endif
#include "graph.h"
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 )
{
float percentage; float percentage;
@ -61,23 +62,29 @@ std::string cpu_string(unsigned int cpu_usage_delay, unsigned int graph_lines,
percentage = cpu_percentage( cpu_usage_delay ); percentage = cpu_percentage( cpu_usage_delay );
if( use_colors ) if( use_colors )
{
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )]; oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
}
if( graph_lines > 0) { if( graph_lines > 0)
{
oss << "["; oss << "[";
oss << getGraphByPercentage( unsigned(percentage), graph_lines ); oss << get_graph_by_percentage( unsigned( percentage ), graph_lines );
oss << "]"; oss << "]";
} }
oss.width( 5 ); oss.width( 5 );
oss << percentage; oss << percentage;
oss << "%"; oss << "%";
if( use_colors ) if( use_colors )
{
oss << "#[fg=default,bg=default]"; oss << "#[fg=default,bg=default]";
}
return oss.str(); return oss.str();
} }
int main(int argc, char** argv) { int main( int argc, char** argv )
{
using namespace ArgvParse; using namespace ArgvParse;
unsigned cpu_usage_delay = 990000; unsigned cpu_usage_delay = 990000;
@ -89,8 +96,9 @@ int main(int argc, char** argv) {
// ugly, I know // ugly, I know
std::string intro = "tmux-mem-cpu-load v"; std::string intro = "tmux-mem-cpu-load v";
intro += std::to_string(VERSION_MAJOR) + "." + std::to_string(VERSION_MINOR); intro += std::to_string( VERSION_MAJOR ) + ".";
intro += "." + std::to_string(VERSION_PATCH) + "\n"; intro += std::to_string( VERSION_MINOR ) + ".";
intro += std::to_string( VERSION_PATCH ) + "\n";
intro += "Usage: tmux-mem-cpu-load [OPTIONS]"; intro += "Usage: tmux-mem-cpu-load [OPTIONS]";
arg.setIntroduction( intro ); arg.setIntroduction( intro );
@ -107,7 +115,8 @@ int main(int argc, char** argv) {
int result = arg.parse( argc, argv ); int result = arg.parse( argc, argv );
if (result != ArgvParser::Success) { if( result != ArgvParser::Success )
{
std::cerr << arg.parseErrorDescription( result ); std::cerr << arg.parseErrorDescription( result );
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -116,18 +125,22 @@ int main(int argc, char** argv) {
if( arg.foundOption( "colors" ) ) if( arg.foundOption( "colors" ) )
use_colors = true; use_colors = true;
if (arg.foundOption("interval")) { if( arg.foundOption( "interval" ) )
{
int delay = std::stoi( arg.optionValue( "interval" ) ); int delay = std::stoi( arg.optionValue( "interval" ) );
if (delay < 1) { if( delay < 1 )
{
std::cerr << "Status interval argument must be one or greater.\n"; std::cerr << "Status interval argument must be one or greater.\n";
return EXIT_FAILURE; return EXIT_FAILURE;
} }
cpu_usage_delay = delay * 1000000 - 10000; cpu_usage_delay = delay * 1000000 - 10000;
} }
if (arg.foundOption("graph-lines")) { if( arg.foundOption( "graph-lines" ) )
{
graph_lines = std::stoi( arg.optionValue( "graph-lines" ) ); graph_lines = std::stoi( arg.optionValue( "graph-lines" ) );
if( graph_lines < 0 ) { if( graph_lines < 0 )
{
std::cerr << "Graph lines argument must be zero or greater.\n"; std::cerr << "Graph lines argument must be zero or greater.\n";
return EXIT_FAILURE; return EXIT_FAILURE;
} }

@ -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.
*/
// the configured options and settings for sysstat // the configured options and settings for sysstat
#define VERSION_MAJOR @VERSION_MAJOR@ #define VERSION_MAJOR @VERSION_MAJOR@
#define VERSION_MINOR @VERSION_MINOR@ #define VERSION_MINOR @VERSION_MINOR@