-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcores.c
More file actions
99 lines (86 loc) · 2.74 KB
/
Copy pathcores.c
File metadata and controls
99 lines (86 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#include <poll.h>
#include <string.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include "cores.h"
///_|> descry: Gets CORES data from /sys/
///_|> num_cores: stores total number of cores the system has
///_|> clock_rate: stores the clock rate of the cpu
///_|> returning: void function returns nothing
void dataCores(int* num_cores, double* clock_rate) {
long maxfreqcpu;
FILE *cpufreq;
cpufreq = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq","r");
if (cpufreq == NULL) {
fprintf(stderr, "Cpu Max Frequency File Empty");
*clock_rate = 0;
*num_cores = 0;
return;
}
fscanf(cpufreq, "%ld", &maxfreqcpu);
FILE *coresfile;
coresfile = fopen("/proc/cpuinfo", "r");
if (coresfile == NULL) {
fprintf(stderr, "File Empty");
*num_cores = 0;
return;
}
char buffer[40];
*num_cores = 0;
*clock_rate = maxfreqcpu / 1000000.0;
while (fgets(buffer, sizeof(buffer), coresfile) != NULL) {
if (strncmp(buffer, "processor", 9) == 0) {
char *colon = strchr(buffer, ':');
if (colon != NULL) {
int procNum = atoi(colon + 1);
*num_cores = procNum;
}
}
}
*num_cores += 1;
fclose(coresfile);
fclose(cpufreq);
}
///_|> descry: Draws a number of BOXES to show the number of CORES in the system
///_|> num_cores: stores total number of cores the system has
///_|> clock_rate: stores the clock rate of the cpu
///_|> returning: returns the lines to skip when exiting the main function
int graphCores(int num_cores, double clock_rate) {
printf("\x1B[%dG", 0);
printf("> Number of Cores: %d @ %.2f GHz\n", num_cores, clock_rate);
int printed = 0;
int lines = 0;
int remaining = 0;
while(printed < num_cores) {
remaining = num_cores - printed;
if(remaining >=4) {
printf("+----+ +----+ +----+ +----+\n");
printf("| | | | | | | |\n");
printf("+----+ +----+ +----+ +----+\n");
printf("\n");
printed += 4;
} else if(remaining == 3){
printf("+----+ +----+ +----+\n");
printf("| | | | | |\n");
printf("+----+ +----+ +----+\n");
printed += 3;
} else if(remaining == 2){
printf("+----+ +----+\n");
printf("| | | |\n");
printf("+----+ +----+\n");
printed += 2;
} else if(remaining == 1){
printf("+----+\n");
printf("| |\n");
printf("+----+\n");
printed += 1;
}
lines++;
}
return lines;
}