Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 3.5)
project(monitor)

find_package(Curses REQUIRED)
Expand Down
4 changes: 2 additions & 2 deletions include/all_processes.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef ALL_PROCESSES_H
#define ALL_PROCESSES_H

#include "process.h"

#include <string>
#include <vector>

#include "process.h"

using std::string;
using std::vector;

Expand Down
18 changes: 9 additions & 9 deletions include/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
#include <string>
#include <vector>

#include "all_processes.h"
#include "process.h"
#include "processor.h"
#include "all_processes.h"

class System {
public:
Processor& Cpu();
All_Processes& Processes();
float MemoryUtilization();
long UpTime();
int TotalProcesses();
int RunningProcesses();
std::string Kernel();
std::string OperatingSystem();
Processor& Cpu();
All_Processes& Processes();
float MemoryUtilization();
long UpTime();
int TotalProcesses();
int RunningProcesses();
std::string Kernel();
std::string OperatingSystem();

// TODO: Define any necessary private members
private:
Expand Down
4 changes: 3 additions & 1 deletion src/all_processes.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "all_processes.h"

#include <unistd.h>

#include <algorithm>
#include <vector>

#include "linux_parser.h"
#include "process.h"

Expand Down Expand Up @@ -65,4 +68,3 @@ void All_Processes::RemoveFinishedProcesses(bool& changed) {
}
}
}

1 change: 1 addition & 0 deletions src/format.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "format.h"

#include <iomanip>
#include <sstream>
#include <string>
Expand Down
9 changes: 3 additions & 6 deletions src/linux_parser.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "linux_parser.h"

#include <dirent.h>

#include <sstream>
#include <string>
#include <vector>

#include "linux_parser.h"
#include "parser_consts.h"
#include "parser_helper.h"

Expand Down Expand Up @@ -69,7 +71,6 @@ vector<int> LinuxParser::Pids() {
return pids;
}


// Read and return the system memory utilization
float LinuxParser::MemoryUtilization() {
float memTotal = ParserHelper::GetValueByKey<int>(
Expand All @@ -82,29 +83,25 @@ float LinuxParser::MemoryUtilization() {
return memory;
}


// Read and return the system uptime
long LinuxParser::UpTime() {
string line;
long upTime = ParserHelper::GetValue<long>(ParserConsts::kUptimeFilename);
return upTime;
}


// Read and return the total number of processes
int LinuxParser::TotalProcesses() {
return ParserHelper::GetValueByKey<int>(ParserConsts::filterProcesses,
ParserConsts::kStatFilename);
}


// Read and return the number of running processes
int LinuxParser::RunningProcesses() {
return ParserHelper::GetValueByKey<int>(ParserConsts::filterRunningProcesses,
ParserConsts::kStatFilename);
}


// Read and return the user associated with a process
string LinuxParser::UserByUID(int UID) {
string line, user, x;
Expand Down
5 changes: 2 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <iostream>

#include "ncurses_display.h"
#include "system.h"
#include "process.h"


#include "system.h"

int main() {
System system;
Expand Down
10 changes: 6 additions & 4 deletions src/ncurses_display.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "ncurses_display.h"

#include <curses.h>

#include <chrono>
#include <string>
#include <thread>
#include <vector>

#include "format.h"
#include "ncurses_display.h"
#include "system.h"

using std::string;
Expand Down Expand Up @@ -78,7 +80,7 @@ void NCursesDisplay::DisplayProcesses(std::vector<Process> processes,
mvwprintw(window, row, time_column,
Format::ElapsedTime(processes[i].UpTime()).c_str());
mvwprintw(window, row, command_column,
processes[i].Command().substr(0, window->_maxx - 46).c_str());
processes[i].Command().substr(0, getmaxx(window) - 46).c_str());
}
}

Expand All @@ -91,7 +93,7 @@ void NCursesDisplay::Display(System& system, int n) {
int x_max{getmaxx(stdscr)};
WINDOW* system_window = newwin(9, x_max - 1, 0, 0);
WINDOW* process_window =
newwin(3 + n, x_max - 1, system_window->_maxy + 1, 0);
newwin(3 + n, x_max - 1, getmaxy(system_window) + 1, 0);

while (1) {
init_pair(1, COLOR_BLUE, COLOR_BLACK);
Expand All @@ -106,4 +108,4 @@ void NCursesDisplay::Display(System& system, int n) {
std::this_thread::sleep_for(std::chrono::seconds(1));
}
endwin();
}
}
14 changes: 5 additions & 9 deletions src/process.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "process.h"

#include <unistd.h>

#include <cctype>
#include <iostream>
#include <iterator>
Expand All @@ -10,7 +13,6 @@
#include "linux_parser.h"
#include "parser_consts.h"
#include "parser_helper.h"
#include "process.h"

using namespace std;

Expand All @@ -24,11 +26,9 @@ Process::Process(int pid, long Hertz) : pid_(pid), Hertz_(Hertz) {
starttime_ = stof(cpuNumbers[21]);
}


// Return this process's ID
int Process::Pid() { return pid_; }


// Return this process's CPU utilization
double Process::CpuUtilization() {
long uptime = LinuxParser::UpTime();
Expand All @@ -40,16 +40,15 @@ double Process::CpuUtilization() {
return cpu_usage;
}


// Return the command that generated this process
string Process::Command() {
string cmd = ParserHelper::GetValue<string>(to_string(pid_) +
ParserConsts::kCmdlineFilename);
size_t maxSize = 50;
if(cmd.size() > maxSize) {
if (cmd.size() > maxSize) {
cmd.resize(maxSize - 3);
cmd = cmd + "...";
}
}
return cmd;
}

Expand All @@ -60,14 +59,12 @@ float Process::RawRam() {
return memInKB;
}


// Return this process's memory utilization
string Process::Ram() {
float memInKB = RawRam();
return Format::KBisMB(memInKB);
}


// Return the user (name) that generated this process
string Process::User() {
int UID = ParserHelper::GetValueByKey<int>(
Expand All @@ -77,7 +74,6 @@ string Process::User() {
return user;
}


// Return the age of this process (in seconds)
long int Process::UpTime() {
long uptime = LinuxParser::UpTime();
Expand Down
1 change: 1 addition & 0 deletions src/processor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "processor.h"

#include <sstream>
#include <string>
#include <vector>
Expand Down
11 changes: 6 additions & 5 deletions src/system.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
#include "system.h"

#include <string>
#include <vector>

#include "all_processes.h"
#include "format.h"
#include "linux_parser.h"
#include "process.h"
#include "processor.h"
#include "all_processes.h"
#include "system.h"
#include "format.h"

using std::string;
using std::vector;

// Return the system's CPU
Processor& System::Cpu() { return cpu_; }

//Return a container composed of the system's processes
// Return a container composed of the system's processes
All_Processes& System::Processes() { return processes_; }

// Return the system's kernel identifier (string)
Expand All @@ -32,5 +33,5 @@ int System::RunningProcesses() { return LinuxParser::RunningProcesses(); }
// Return the total number of processes on the system
int System::TotalProcesses() { return LinuxParser::TotalProcesses(); }

//Return the number of seconds since the system started running
// Return the number of seconds since the system started running
long int System::UpTime() { return LinuxParser::UpTime(); }