Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate file opens #29

Closed
wants to merge 16 commits into from
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Project

This is a fork of [CppND-System-Monitor]([email protected]:lotockib/nanodegree-system-monitor.git) to allow editing for project submission.

Notes about the Linux system and development are [here](./readme_dev_notes.md).

# CppND-System-Monitor

Starter code for System Monitor Project in the Object Oriented Programming Course of the [Udacity C++ Nanodegree Program](https://www.udacity.com/course/c-plus-plus-nanodegree--nd213).
Expand Down
10 changes: 5 additions & 5 deletions include/format.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef FORMAT_H
#define FORMAT_H
#ifndef FORMAT_H_
#define FORMAT_H_

#include <string>

namespace Format {
std::string ElapsedTime(long times); // TODO: See src/format.cpp
}; // namespace Format
std::string ElapsedTime(long times);
}; // namespace Format

#endif
#endif // FORMAT_H_
19 changes: 10 additions & 9 deletions include/linux_parser.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef SYSTEM_PARSER_H
#define SYSTEM_PARSER_H
#ifndef LINUX_PARSER_H_
#define LINUX_PARSER_H_

#include <fstream>
#include <regex>
#include <string>
#include <vector>

namespace LinuxParser {
// Paths
Expand Down Expand Up @@ -43,15 +44,15 @@ enum CPUStates {
std::vector<std::string> CpuUtilization();
long Jiffies();
long ActiveJiffies();
long ActiveJiffies(int pid);
// long ActiveJiffies(int pid);
long IdleJiffies();

// Processes
std::string Command(int pid);
std::string Ram(int pid);
std::string Uid(int pid);
std::string User(int pid);
long int UpTime(int pid);
std::string ReadCommand(int pid);
std::string ReadUser(std::string uid);
std::vector<std::string> ReadStat(int pid);
std::vector<std::string> ReadStatus(int pid);

}; // namespace LinuxParser

#endif
#endif // LINUX_PARSER_H_
4 changes: 2 additions & 2 deletions include/ncurses_display.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef NCURSES_DISPLAY_H
#ifndef NCURSES_DISPLAY_H_
#define NCURSES_DISPLAY_H

#include <curses.h>
Expand All @@ -13,4 +13,4 @@ void DisplayProcesses(std::vector<Process>& processes, WINDOW* window, int n);
std::string ProgressBar(float percent);
}; // namespace NCursesDisplay

#endif
#endif // NCURSES_DISPLAY_H_
71 changes: 57 additions & 14 deletions include/process.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,66 @@
#ifndef PROCESS_H
#define PROCESS_H
#ifndef PROCESS_H_
#define PROCESS_H_

#include <string>
#include <vector>
#include "../include/linux_parser.h"

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

/*
Basic class for Process representation
It contains relevant attributes as shown below
Contains data for each process.
*/
class Process {
public:
int Pid(); // TODO: See src/process.cpp
std::string User(); // TODO: See src/process.cpp
std::string Command(); // TODO: See src/process.cpp
float CpuUtilization(); // TODO: See src/process.cpp
std::string Ram(); // TODO: See src/process.cpp
long int UpTime(); // TODO: See src/process.cpp
bool operator<(Process const& a) const; // TODO: See src/process.cpp

// TODO: Declare any necessary private members
Process(int pid) : pid_(pid) {
// Read each file only one time
vector<string> stats = LinuxParser::ReadStat(pid_);
vector<string> statuses = LinuxParser::ReadStatus(pid_);
string commands = LinuxParser::ReadCommand(pid_);

// Set local member data using file strings
CpuUtilization(stats);
UpTime(stats);
Ram(statuses);
User(statuses);
Command(commands);
}

// Getters
int Pid();
float CpuUtilization();
long int UpTime();
string Ram();
string User();
string Command();
void Pid(int p) { pid_ = p; }
bool operator>(Process const& a) const;

private:
// Setters
void CpuUtilization(vector<string> stats);
void UpTime(vector<string> stats);
void Ram(vector<string> statuses);
void User(vector<string> statuses);
void Command(string commands);

// Data
int pid_{0};
float cpu_{0};
long int uptime_{0};
float ram_{0};
string uid_{};
string username_{};
string command_{};
};

struct PidStat {
unsigned long long int utime;
unsigned long long int stime;
unsigned long long int cutime;
unsigned long long int cstime;
unsigned long long int starttime;
};

#endif
#endif // PROCESS_H_
28 changes: 23 additions & 5 deletions include/processor.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
#ifndef PROCESSOR_H
#define PROCESSOR_H
#ifndef PROCESSOR_H_
#define PROCESSOR_H_

struct Status {
unsigned long long int user = 0;
unsigned long long int nice = 0;
unsigned long long int system = 0;
unsigned long long int idle = 0;
unsigned long long int iowait = 0;
unsigned long long int irq = 0;
unsigned long long int softirq = 0;
unsigned long long int steal = 0;
unsigned long long int guest = 0;
unsigned long long int guestnice = 0;
};

class Processor {
public:
float Utilization(); // TODO: See src/processor.cpp
Processor() {
ReadStatus();
}
float Utilization();

// TODO: Declare any necessary private members
private:
Status current_;
Status previous_;
void ReadStatus();
};

#endif
#endif // PROCESSOR_H_
25 changes: 12 additions & 13 deletions include/system.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef SYSTEM_H
#define SYSTEM_H
#ifndef SYSTEM_H_
#define SYSTEM_H_

#include <string>
#include <vector>
Expand All @@ -9,19 +9,18 @@

class System {
public:
Processor& Cpu(); // TODO: See src/system.cpp
std::vector<Process>& Processes(); // TODO: See src/system.cpp
float MemoryUtilization(); // TODO: See src/system.cpp
long UpTime(); // TODO: See src/system.cpp
int TotalProcesses(); // TODO: See src/system.cpp
int RunningProcesses(); // TODO: See src/system.cpp
std::string Kernel(); // TODO: See src/system.cpp
std::string OperatingSystem(); // TODO: See src/system.cpp
Processor& Cpu();
std::vector<Process>& Processes();
float MemoryUtilization();
long UpTime();
int TotalProcesses();
int RunningProcesses();
std::string Kernel();
std::string OperatingSystem();

// TODO: Define any necessary private members
private:
Processor cpu_ = {};
Processor cpu_;
std::vector<Process> processes_ = {};
};

#endif
#endif // SYSTEM_H_
4 changes: 4 additions & 0 deletions plantuml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1. Install [plantuml](https://marketplace.visualstudio.com/items?itemName=jebbs.plantuml) extension in VSCode.
2. Preview with ```alt + d```
3. Export with right-click

Binary file added plantuml/nanodegree-monitor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions plantuml/nanodegree-monitor.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
@startuml

entity Monitor {
main()
}

package "System" #DDDDDD {

class System {
+MemoryUtilization()
+UpTime()
+TotalProcesses()
+RunningProcesses()
+Kernel()
+OperatingSystem()
-Processor
-vector<Process>
}

class Process {
+Pid()
+Pid(int p)
+User()
+Command()
+CpuUtilization()
+Ram()
+UpTime()
-pid_

}

class Processor {
+Uitlization()
-Status current_
-Status previous_
-ReadStatus()
}

System *-- Processor

System "1" *-- "0..*" Process
}

package "LinuxParser" #DDDDDD {

object LinuxParserFunctions {
MemoryUtilization()
UpTime()
Pids()
TotalProcesses()
RunningProcesses()
OperatingSystem()
Kernel()
CpuUtilization()
Jiffies()
ActiveJiffies()
IdleJiffies()
}

object PathConstants {
kProcDirectory
kCmdlineFilename
kCpuinfoFilename
kStatusFilename
kStatFilename
kUptimeFilename
kMeminfoFilename
kVersionFilename
kOSPath
kPasswordPath
}

enum CPUStates {
kUser_
kNice_
kSystem_
kIdle_
kIowait_
kIrq_
kSoftirq_
kSteal_
kGuest_
kGuestnice_
}

PathConstants <.. Process
PathConstants <.. Processor
Processor *-- CPUStates
Process *-- CPUStates
}

package NCursesDisplay #DDDDDD {

object NCursesFunctions {
Display()
DisplaySystem()
DisplayProcesses()
ProgressBar()
}

Monitor <|.. NCursesFunctions
System --> NCursesFunctions

}

package Format #DDDDDD {
object FormatFunctions {
Elapsedtime()
}

NCursesFunctions <-- FormatFunctions
}

@enduml
Loading