-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm_perf_sys.c
More file actions
132 lines (115 loc) · 2.65 KB
/
vm_perf_sys.c
File metadata and controls
132 lines (115 loc) · 2.65 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright 2015 Loading Deck Limited - https://www.loadingdeck.com/
* Use of this program or parts thereof is subject to the GPLv3 licence
*/
#include <unistd.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/sysinfo.h>
#include "vm_perf_sys.h"
static const char * info_path[NUM_INFO_PATHS] = {
"/proc/version",
"/proc/cpuinfo"
//"/proc/meminfo",
//"/proc/scsi/scsi",
};
static char* proc_get_info(const char * path){
int fd = open(path, O_RDONLY);
if(fd == -1){
perror("open");
return strdup("error");
}
size_t offset = 0;
size_t size = 0;
char * info = NULL;
while(1){
if(offset >= size){
size += 100;
info = (char*) realloc(info, sizeof(char)*size);
if(info == NULL){
perror("malloc");
return strdup("error");
}
}
int bytes = read(fd, &info[offset], size-offset);
if(bytes < 0){
perror("read");
break;
}else if(bytes == 0)
break;
offset+= bytes;
}
close(fd);
info[offset-1] = '\0';
return info;
}
static int enumerate_cpus(struct sys_result *r){
FILE * fin = fopen("/proc/cpuinfo", "r");
if(fin == NULL){
perror("fopen");
return 1;
}
size_t line_size = 100;
char * line = malloc(line_size*sizeof(char));
if(line == NULL){
perror("malloc");
return 1;
}
bzero(r->cpu_model, sizeof(r->cpu_model));
r->cpu_count = 0;
while(getline(&line, &line_size, fin) > 0){
int len = strlen(line);
line[len-1] = '\0';
if(strncmp(line, "model name", 10) == 0){
if(r->cpu_model[0] == 0){
char * model = strchr(line, ':');
if(++model){
while(*model == ' '){
model++;
}
strncpy(r->cpu_model, model, sizeof(r->cpu_model));
}
}
r->cpu_count++;
}
}
fclose(fin);
free(line);
return 0;
};
void sys_info(struct sys_result * r){
bzero(r, sizeof(struct sys_result));
int i;
for(i=0; i < NUM_INFO_PATHS; ++i)
r->info[i] = proc_get_info(info_path[i]);
if(gethostname(r->hostname, HOST_NAME_MAX) == -1){
perror("gethostname");
return;
}
struct sysinfo mi;
if(sysinfo(&mi) == -1){
perror("sysinfo");
return;
}
r->totalRAM = mi.totalram / (1024 * 1024);
r->freeRAM = mi.freeram / (1024 * 1024);
enumerate_cpus(r);
}
void sys_report(const struct sys_result * r){
printf("\"system\":{");
printf("\"uname\":\"%s\",", r->info[0]);
printf("\"hostname\":\"%s\",", r->hostname);
printf("\"cpu_model\":\"%s\",", r->cpu_model);
printf("\"cpu_count\":\"%hu\",", r->cpu_count);
printf("\"ram_total\":\"%luMB\",", r->totalRAM);
printf("\"ram_free\":\"%luMB\"", r->freeRAM);
printf("}");
}