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
35 changes: 29 additions & 6 deletions src/console_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
* along with gmonitor. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <sys/ioctl.h>
#include "console_writer.h"
#include <ctime>
#include <chrono>

using namespace std;

Expand Down Expand Up @@ -88,7 +91,7 @@ string ConsoleWriter::inBlue(const string &text)
}

//return the correct colour for a value (0 - 100)
Color ConsoleWriter::getColorByValue(int value)
Color ConsoleWriter::getColorByValue(unsigned int value)
{
if(value < 25 - resolution){ return BLUE;}
else
Expand Down Expand Up @@ -183,16 +186,36 @@ void ConsoleWriter::updateTerminalDimensions(int colms, int rows)
void ConsoleWriter::clearConsole()
{
cout << "\x1B[2J\x1B[H";
// On the first run, the dimensions are wrong, so let's check them
refreshTerminalDimensions();
}

//print gpu identifiers
void ConsoleWriter::printGpuIdentifier(const string &gpuId,
const string &gpuName,
int totalAvailableMemory)
{
cout << "\nID: " << gpuId
<< ", Name: " << gpuName
<< " ("<< totalAvailableMemory << " MB)";
char outline[BUFFER_LENGTH + 1];
size_t len;
chrono::system_clock::time_point today = chrono::system_clock::now();
time_t tt = chrono::system_clock::to_time_t(today);

cout << endl;
len = snprintf(outline, BUFFER_LENGTH, "ID: %s", gpuId.c_str());
if ((len < terminalColms) && (len < BUFFER_LENGTH)) {
len += snprintf(&outline[len], BUFFER_LENGTH - len, ", Name: %s",
gpuName.c_str());
if ((len < terminalColms) && (len < BUFFER_LENGTH)) {
len += snprintf(&outline[len], BUFFER_LENGTH - len, " (%d MB)",
totalAvailableMemory);
if ((len < terminalColms) && (len < BUFFER_LENGTH)) {
len += snprintf(&outline[len], BUFFER_LENGTH - len, "%*s",
terminalColms - len, ctime(&tt));
}
}
}
outline[len] = 0; // make sure it's terminated
cout << outline;
}

//print the current state of the gpu
Expand Down Expand Up @@ -229,15 +252,15 @@ void ConsoleWriter::printCurrentGpuState(const GpuStates &state)

//print one state, value is represented by a list
//of '|', 10 -> |||||||||| (for resolution = 1)
string ConsoleWriter::printOneStateInOneLine(States state, int value)
string ConsoleWriter::printOneStateInOneLine(States state, unsigned int value)
{
string preLabel, postLabel;

getLabelsByState(state, &preLabel, &postLabel);

ostringstream oss;

int character = 0;
unsigned int character = 0;

while(character <= (terminalColms ) * resolution)
{
Expand Down
16 changes: 8 additions & 8 deletions src/console_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
using namespace std;

const int DEFAULT_ROW_N = 100; //default terminal window vertical size
const int DEFAULT_COL_N = 100; //default terminal window hotizontal size
const int DEFAULT_COL_N = 100; //default terminal window horizontal size
const int DEFAULT_RES = 1; //default resolution, '|' represents one % / c

/*
Expand All @@ -34,10 +34,10 @@ const int DEFAULT_RES = 1; //default resolution, '|' represents one % / c
class ConsoleWriter
{
private:
int terminalRows; //total rows of the terminal
int terminalColms; //total columns of the terminal
int resolution; //how many unites a column represents
int combinedBarSize;
unsigned int terminalRows; //total rows of the terminal
unsigned int terminalColms; //total columns of the terminal
unsigned int resolution; //how many units a column represents
unsigned int combinedBarSize;

//return a coloured text
string colourText(Color color, const string &text);
Expand All @@ -48,7 +48,7 @@ class ConsoleWriter
string inLightBlue(const string &text);

//return the correct color for a value (0 - 100)
Color getColorByValue(int value);
Color getColorByValue(unsigned int value);

//return the correct color for a state
Color getColorByState(States toPrint);
Expand All @@ -75,7 +75,7 @@ class ConsoleWriter
//print one state, value is represented by a list
//of '|', 10 -> |||||||||| (for resolution = 1)
string printOneStateInOneLine(States state,
int value);
unsigned int value);

//print one state, value is represented by a list
//of '|', resolution depends on the line size
Expand All @@ -98,7 +98,7 @@ class ConsoleWriter
: terminalRows(DEFAULT_ROW_N),
terminalColms(DEFAULT_COL_N),
resolution(DEFAULT_RES),
combinedBarSize(0) {};
combinedBarSize(0) { };

//clear the terminal
void clearConsole();
Expand Down
3 changes: 2 additions & 1 deletion src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ enum DisplayMode
// for each gpu)
const DisplayMode DEFAULT_DISPLAY_MODE = CURRENT_NEXT_TO_HISTORY;


const int DEFAULT_REFRESH_RATE = 2; //default refresh rate, 2 seconds

const int BUFFER_LENGTH = 512;

#endif
4 changes: 2 additions & 2 deletions src/stats_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void StatsReader::getGpuStates(GpuStates *gpuStates,
bool StatsReader::getDoubleFromSystemCall(string &command, std::vector<double> *values)
{
FILE *in;
char buff[512];
char buff[BUFFER_LENGTH];

command = checkIfSshCommand(command);

Expand All @@ -75,7 +75,7 @@ bool StatsReader::getDoubleFromSystemCall(string &command, std::vector<double> *
bool StatsReader::getGpuList(vector<string> *gpuList)
{
FILE *in;
char buff[512];
char buff[BUFFER_LENGTH];

string command;

Expand Down