-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo.c
More file actions
93 lines (80 loc) · 3.25 KB
/
info.c
File metadata and controls
93 lines (80 loc) · 3.25 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
#include <sys/sysinfo.h> // for sysinfo
#include <unistd.h> //for usleep
#include <stdio.h> // standard i/o
#include <stdlib.h> //standard library
#include <string.h> //for strcmp and other string fxn
#include "info.h" //to get fxn headers
float getTotalMemory()
{
struct sysinfo info; // define sysinfo struct
if (sysinfo(&info) != 0) //call sysinfo and check error
{
perror("total memory issue");
return -1;
}
return (info.totalram * info.mem_unit) / (1024.0 * 1024.0 * 1024.0); //convert from bytes to GB as stated on manpage
}
float getUsedMemory()
{
struct sysinfo info; // define sysinfo struct
if (sysinfo(&info) != 0) //call sysinfo and check error
{
perror("used memory issue");
return -1;
}
float usedMemory = (info.totalram * info.mem_unit) / (1024.0 * 1024.0 * 1024.0) - (info.freeram * info.mem_unit) / (1024.0 * 1024.0 * 1024.0);
return usedMemory; //above is the total - free ram converted into GB
}
CpuStats parseCpuStats()
{
CpuStats stats; // define a stats struct
FILE *file = fopen("/proc/stat", "r"); //use fopen to open into proc/stat for reading
if (file == NULL) //error check if it is null, return null struct
{
perror("File is null");
return stats;
}
fscanf(file, "cpu %ld %ld %ld %ld %ld %ld %ld", &stats.user, &stats.nice, &stats.system, &stats.idle, &stats.iowait, &stats.irq, &stats.softirq); // read the values in the file and save it in the struct
fclose(file); //close file
return stats; //return struct with values
}
int parseCpuCore() {
FILE *file = fopen("/proc/cpuinfo", "r"); // use fopen to open into proc/cpuinfo for reading
char lines[512]; //define a lines buffer with 512
int cores = 0; //define cores
while (fgets(lines, 512, file) != NULL) // loop through file
{
if (strncmp(lines, "processor", 9) == 0) //every time processor is seen, add a core
{
cores++;
}
}
fclose(file); //close file
return cores; //return cores
}
float parseCpuFreq() {
int frequency; //frequency integer
FILE *freq_file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r"); //open the file where cpu info is, reference #8
fscanf(freq_file, "%d", &frequency); // read the integer frequency value in hertz
fclose(freq_file); //close file
return frequency / 1000000.0; //typecast to float and convert hertz to Ghz
}
long addTotalTime(CpuStats stats)
{ //just add all va;ues in the struct as long and return
return stats.user + stats.system + stats.softirq + stats.nice + stats.irq + stats.iowait + stats.idle;
}
float getCpuUsage(int tdelay)
{
CpuStats stats_prev = parseCpuStats(); //get cpu stats
usleep(tdelay); //sleep
CpuStats stats_curr = parseCpuStats(); // get cpu stats after sleeping
long total_time = addTotalTime(stats_curr) - addTotalTime(stats_prev); // subtract time
long idle_time = stats_curr.idle - stats_prev.idle; //subtract idle time
if (total_time <= 0) //check if time is invalid
{
return 0.0;
}
float cpu_usage = (1.0 - ((double)idle_time / total_time)) * 100; // do calculation as said in a3 description
if (cpu_usage <= 0) return 0.0; //error check
return cpu_usage; //return usage
}