115 lines
3.1 KiB
C
115 lines
3.1 KiB
C
|
|
#include "printUtil.h"
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
void draw_graph(char buf[PANE_BUF], int *datapoints, int i) {
|
|
///_|> descry: Given datapoints and the number of datapoints, graph them. Push
|
|
/// datapoints left offscreen if they cannot all be displayed.
|
|
///_|> buf: buffer to write output to.
|
|
///_|> datapoints: array of datapoints
|
|
///_|> i: last datapoint index
|
|
///_|> returning: void
|
|
|
|
int start = 0;
|
|
if (i > X_SIZE) {
|
|
start = i - X_SIZE;
|
|
}
|
|
// Draw the graph
|
|
for (int j = 10; j > 1; j--) {
|
|
if (j == 10) {
|
|
strncat(buf, "\n100%|", 8);
|
|
} else {
|
|
strncat(buf, "\n |", 8);
|
|
}
|
|
for (int k = start; k <= i; k++) {
|
|
if (j == datapoints[k]) {
|
|
strncat(buf, "#", 2);
|
|
} else {
|
|
strncat(buf, " ", 2);
|
|
}
|
|
}
|
|
}
|
|
// Draw the last 0 line
|
|
strncat(buf, "\n 0%|", 8);
|
|
for (int k = start; k < start + X_SIZE; k++) {
|
|
if (1 >= datapoints[k] && k <= i) {
|
|
strncat(buf, "#", 2);
|
|
} else {
|
|
strncat(buf, "_", 2);
|
|
}
|
|
}
|
|
strncat(buf, "\n", 2);
|
|
}
|
|
|
|
void draw_cores(int cpu_cores, int cpu_max_freq, int pipe) {
|
|
///_|> descry: draws the cores pane for main
|
|
///_|> cpu_cores: number of cpu cores
|
|
///_|> cpu_max_freq: max frequency of the cpu
|
|
///_|> pipe: pipe to write data to
|
|
///_|> returning: void
|
|
|
|
char buf[PANE_BUF];
|
|
char tempbuf[LINE_BUF];
|
|
|
|
snprintf(tempbuf, 127, "\n- Number of Cores: %d\n Max frequency: %.2f GHz\n",
|
|
cpu_cores,
|
|
(float)cpu_max_freq / 1e6); // 1e6 kHz to GHz
|
|
|
|
strncat(buf, tempbuf, 127);
|
|
|
|
int drawn = 0;
|
|
while (drawn + 4 <= cpu_cores) {
|
|
strncat(
|
|
buf,
|
|
" +——+ +——+ +——+ +——+\n | | | | | | | |\n +——+ +——+ +——+ +——+\n",
|
|
100);
|
|
drawn += 4;
|
|
}
|
|
if (drawn + 2 <= cpu_cores) {
|
|
strncat(buf, " +——+ +——+\n | | | |\n +——+ +——+\n", 60);
|
|
drawn += 2;
|
|
}
|
|
if (drawn + 1 <= cpu_cores) {
|
|
strncat(buf, " +——+\n | |\n +——+\n", 30);
|
|
drawn += 1;
|
|
}
|
|
write(pipe, buf, PANE_BUF);
|
|
}
|
|
|
|
void draw_cpu_title(char buf[PANE_BUF], float cpu_util_percent) {
|
|
///_|> descry: draws title for cpu graph
|
|
///_|> buf: buffer to write output to.
|
|
///_|> cpu_util_percent: the percent of cpu being utilized
|
|
///_|> returning: void
|
|
snprintf(buf, LINE_BUF, "\n- CPU Used: %.2f%%", cpu_util_percent);
|
|
}
|
|
|
|
void draw_mem_title(char buf[PANE_BUF], float mem_used, float mem_total) {
|
|
///_|> descry: draws title for cpu graph
|
|
///_|> buf: buffer to write output to.
|
|
///_|> cpu_util_percent: the percent of cpu being utilized
|
|
///_|> returning: void
|
|
snprintf(buf, 32, "\n- Memory Used: %.2f/%.2f GB", mem_used, mem_total);
|
|
}
|
|
|
|
void draw_reset(int samples, int tdelay) {
|
|
///_|> descry: draws title for function and resets screen
|
|
///_|> samples: number of samples to show
|
|
///_|> tdelay: period to show
|
|
///_|> returning: void
|
|
printf("\033[2J\033[H- %d samples | %dμs (%.2fs) intervals\n", samples,
|
|
tdelay, tdelay / 1e6);
|
|
}
|
|
|
|
double min(double a, double b) {
|
|
///_|> descry: takes min of a and b
|
|
///_|> a: some number
|
|
///_|> b: some number
|
|
///_|> returning: min of a and b
|
|
if (a < b) {
|
|
return a;
|
|
} else {
|
|
return b;
|
|
}
|
|
} |