-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathina_bm.cc
191 lines (142 loc) · 3.43 KB
/
ina_bm.cc
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdlib.h>
#include <stdint.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
typedef struct ina {
char current_path[50];
char voltage_path[50];
char name[10];
int last;
} ina;
int cmp_ina(const void *a, const void *b) {
ina *temp1 = (ina*)a;
ina *temp2 = (ina*)b;
int len1 = strlen(temp1->current_path);
int len2 = strlen(temp2->current_path);
if(len1==len2){
return strcmp(temp1->current_path, temp2->current_path);
} else if(len1>len2){
return 1;
} else {
return -1;
}
}
void populate_ina_array(ina *inas) {
DIR *d;
struct dirent *dir;
char buffer[100];
char fname_buff[100];
FILE *fptr;
d = opendir("/sys/class/hwmon/");
int counter = 0;
while ((dir = readdir(d)) != NULL) {
if (strncmp(".", dir->d_name, 1) == 0) {
continue;
}
//printf("tree: %s\n", dir->d_name);
strcpy(fname_buff, "/sys/class/hwmon/");
strcat(fname_buff, dir->d_name);
strcat(fname_buff, "/name");
//printf("name: %s\n", fname_buff);
fptr = fopen(fname_buff, "r");
fread(&buffer, 10, 1, fptr);
//printf("device type: %s", buffer);
if (strncmp(buffer, "ina", 3) == 0) {
fname_buff[strlen(fname_buff)-5] = 0;
strcpy(inas[counter].current_path,fname_buff);
strcat(inas[counter].current_path,"/curr1_input");
strcpy(inas[counter].voltage_path,fname_buff);
strcat(inas[counter].voltage_path,"/in1_input");
// printf("found: %s\n", inas[counter].ina_dir);
inas[counter].last = 0;
counter++;
}
}
qsort(inas, counter, sizeof(ina), cmp_ina);
if (counter > 0)
inas[counter-1].last = 1;
counter = 0;
while(1) {
sprintf(inas[counter].name, "INA%03d", counter);
if(inas[counter].last == 1)
return;
counter++;
}
closedir(d);
}
void list_inas (ina *inas) {
int counter = 0;
while(1) {
printf("Found INA%03d at dir: %s\n", counter, inas[counter].current_path);
if(inas[counter].last == 1)
break;
counter++;
}
return;
}
void run_bm (char target_file[50], int sleep_per, int iterations, ina *inas) {
FILE *sav_ptr;
FILE *ina_ptr;
sav_ptr = fopen(target_file, "w");
char buffer[20];
int counter = 0;
while(1) {
fprintf(sav_ptr, "%s mV,%s mA,", inas[counter].name, inas[counter].name);
if(inas[counter].last == 1)
break;
counter++;
}
fprintf(sav_ptr, "\n");
for (int j = 0; j < iterations; j++) {
counter = 0;
while(1) {
ina_ptr = fopen(inas[counter].voltage_path, "r");
fscanf(ina_ptr,"%[^\n]", buffer);
fprintf(sav_ptr, "%s,", buffer);
fclose(ina_ptr);
ina_ptr = fopen(inas[counter].current_path, "r");
fscanf(ina_ptr,"%[^\n]", buffer);
fprintf(sav_ptr, "%s,", buffer);
if(inas[counter].last) {
fprintf(sav_ptr, "\n");
fclose(ina_ptr);
break;
}
fclose(ina_ptr);
counter++;
}
sleep(sleep_per);
}
fclose(sav_ptr);
}
int main(int argc, char *argv[]) {
ina inas[30];
populate_ina_array(inas);
int opt;
int sleep_per = 1;
int iterations = 1;
char target_file[50] = "./out.txt";
while ((opt = getopt(argc, argv, "t:o:n:l")) != -1) {
switch (opt) {
case 't':
printf("Running with sleep @ %d\n", atoi(optarg));
sleep_per = atoi(optarg);
break;
case 'o':
printf("File output to %s\n", optarg);
strcpy(target_file, optarg);
break;
case 'l':
list_inas(inas);
break;
case 'n':
printf("Testing %d iterations\n", atoi(optarg));
iterations = atoi(optarg);
break;
}
}
run_bm(target_file, sleep_per, iterations, inas);
return 0;
}