-
Notifications
You must be signed in to change notification settings - Fork 1
/
conf.c
174 lines (149 loc) · 3.63 KB
/
conf.c
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
#include "simrts.h"
typedef enum {
SECT_UNKNOWN,
SECT_CPUFREQ,
SECT_MEM,
SECT_TASK,
} section_t;
static char *
trim(char *str)
{
char *p;
if (str == NULL)
return NULL;
for (p = str; *p == ' ' || *p == '\t' || *p == '\r' || *p == '\n'; p++);
str = p;
if (*p != '\0') {
for (p = p + 1; *p != '\0'; p++);
for (p = p - 1; (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') && p != str; p--);
p[1] = '\0';
}
return str;
}
static section_t
check_section(const char *line)
{
if (*line != '*')
return FALSE;
if (strncmp(line + 1, "cpufreq", 7) == 0)
return SECT_CPUFREQ;
if (strncmp(line + 1, "mem", 3) == 0)
return SECT_MEM;
if (strncmp(line + 1, "task", 4) == 0)
return SECT_TASK;
return SECT_UNKNOWN;
}
static void
parse_cpufreq(FILE *fp)
{
char buf[1024];
while (fgets(buf, 1024, fp)) {
double wcet_scale, power_active, power_idle;
if (buf[0] == '#')
continue;
if (buf[0] == '\n' || buf[0] == '*') {
fseek(fp, -1 * strlen(buf), SEEK_CUR);
return;
}
if (sscanf(buf, "%lf %lf %lf", &wcet_scale, &power_active, &power_idle) != 3) {
FATAL(2, "cannot load configuration: invalid CPU frequency format: %s", trim(buf));
}
if (wcet_scale < 0 || wcet_scale > 1) {
FATAL(2, "invalid cpu frequency wcet scale: %s", trim(buf));
}
if (power_active < 0 || power_idle < 0) {
FATAL(2, "invalid cpu frequency power: %s", trim(buf));
}
if (!insert_cpufreq(wcet_scale, power_active, power_idle)) {
FATAL(2, "cannot insert CPU frequency: %s", trim(buf));
}
}
}
static void
parse_mem(FILE *fp)
{
char buf[1024];
while (fgets(buf, 1024, fp)) {
unsigned max_capacity;
double wcet_scale, power_active, power_idle;
char type[1024];
if (buf[0] == '#')
continue;
if (buf[0] == '\n' || buf[0] == '*') {
fseek(fp, -1 * strlen(buf), SEEK_CUR);
return;
}
if (sscanf(buf, "%s %u %lf %lf %lf", type, &max_capacity, &wcet_scale, &power_active, &power_idle) != 5) {
FATAL(2, "cannot load configuration: invalid memory spec: %s", trim(buf));
}
if (max_capacity == 0) {
FATAL(2, "invalid max memory capacity: %s", trim(buf));
}
if (wcet_scale < 0 || wcet_scale > 1) {
FATAL(2, "invalid memory wcet scale: %s", trim(buf));
}
if (power_active < 0 || power_idle < 0) {
FATAL(2, "invalid memory power: %s", trim(buf));
}
insert_mem(type, max_capacity, wcet_scale, power_active, power_idle);
}
}
static void
parse_task(FILE *fp)
{
char buf[1024];
while (fgets(buf, 1024, fp)) {
unsigned wcet, period, memreq;
double mem_active_ratio;
if (buf[0] == '#')
continue;
if (buf[0] == '\n' || buf[0] == '*') {
fseek(fp, -1 * strlen(buf), SEEK_CUR);
return;
}
if (sscanf(buf, "%u %u %u %lf", &wcet, &period, &memreq, &mem_active_ratio) != 4) {
FATAL(2, "cannot load configuration: invalid task format: %s", trim(buf));
}
if (wcet >= period) {
FATAL(2, "wcet is larger or equal than period: %s", trim(buf));
}
insert_task(wcet, period, memreq, mem_active_ratio);
}
}
static void
parse_conf(FILE *fp)
{
char buf[1024];
while (fgets(buf, 1024, fp)) {
if (buf[0] == '\n' || buf[0] == '#')
continue;
switch (check_section(buf)) {
case SECT_CPUFREQ:
parse_cpufreq(fp);
break;
case SECT_MEM:
parse_mem(fp);
break;
case SECT_TASK:
if (n_cpufreqs == 0) {
FATAL(2, "cpu frequency section should be defined ahead of task section");
}
parse_task(fp);
break;
default:
errmsg("unknown section: %s", trim(buf));
FATAL(2, "cannot load configuration");
}
}
}
void
load_conf(const char *fpath)
{
FILE *fp;
fp = fopen(fpath, "r");
if (fp == NULL) {
FATAL(1, "configuration not found: %s", fpath);
}
parse_conf(fp);
fclose(fp);
}