-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpusm.c
331 lines (329 loc) · 10.2 KB
/
cpusm.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include "./cpusm.h"
int main(int argc, char **argv)
{
srand(time(NULL));
printf(DEFCOLOR);
char *dev;
dev=NULL;
char *outputfile;
outputfile=NULL;
int cpuaffinity = 0;
int ival = 0;
int op = 0;
int em[29] = {0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,4,0,1,0,0,0,0,0,0};
double secs = 0L;
double decsecs = 0L;
double dur[4] = {0L,0L,0L,0L};
size_t yards=247;
em[17]=em[18]=0;
em[11] = sysconf(_SC_NPROCESSORS_CONF);
int rmax = sysconf(_SC_CLK_TCK);
double inmin = (double)1/sysconf(_SC_CLK_TCK);
while (1)
{
static struct option long_options[] =
{
{"core", required_argument, 0, 'a'},
{"core-opt", required_argument, 0, 'b'},
{"colour", required_argument, 0, 'c'},
{"duration", required_argument, 0, 'd'},
{"slip", required_argument, 0, 'e'},
{"file", required_argument, 0, 'f'},
{"coefficient", required_argument, 0, 'g'},
{"help", no_argument, 0, 'h'},
{"shortmode", required_argument, 0, 'i'},
{"step", required_argument, 0, 'j'},
{"mode", required_argument, 0, 'k'},
{"log", required_argument, 0, 'l'},
{"display-size", required_argument, 0, 'm'},
{"network", required_argument, 0, 'n'},
{"priority", required_argument, 0, 'o'},
{"timing", required_argument, 0, 'p'},
{"precision", required_argument, 0, 'q'},
{"rate", required_argument, 0, 'r'},
{"scrolling", required_argument, 0, 's'},
{"interval", required_argument, 0, 't'},
{"rolling", required_argument, 0, 'u'},
{"category", required_argument, 0, 'v'},
{"scheduler", required_argument, 0, 'w'},
{"header-interval", required_argument, 0, 'x'},
{"nice", required_argument, 0, 'y'},
{"samples", required_argument, 0, 'z'},
{0, 0, 0, 0}
};
int option_index = 0;
op = getopt_long (argc, argv, "a:b:c:d:e:f:g:hi:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z:", long_options, &option_index);
if (op == -1)
break;
switch (op)
{
case 'a': //cpu core affinity
em[7] = atoi(optarg);
if ( em[7] >= em[11] || em[7] <= -2 ) {
printf("-a%i: cpu affinity violation [cpu affinity range: 0 - %i || -1 for random affinity]\n", em[7], em[11]-1);
exit(EXIT_FAILURE);
}
cpuaffinity = 1;
break;
case 'b': //cpu core optimization algorithm
em[8] = atoi(optarg);
if ( em[8] >= 4 ) {
printf("b%i: cpu optimization algorithm violation [<0> kernel, <1> min.cpu, <2> max.cpu, <3> random]\n", em[8]);
exit(EXIT_FAILURE);
}
break;
case 'c': //output colour scheme
em[3] = atoi(optarg);
if ( em[3] >= 8 ) {
print_app_usage();
exit(EXIT_FAILURE);
}
break;
case 'd': //duration
dur[0] = dur[1] = atof(optarg);
break;
case 'e': //slip interval
em[20] = atoi(optarg);
break;
case 'f': //output filename [use in conjunction with -l]
outputfile = optarg;
fo = fopen(outputfile, "w+");
if( fo == NULL ) {
fprintf ( stderr, "execution error [log file inaccessible]: -f %s\n\n", outputfile);
exit(EXIT_FAILURE);
}
break;
case 'g': //coefficient
dur[3] = atof(optarg);
break;
case 'h': //help
print_app_usage();
exit(EXIT_FAILURE);
break;
case 'i': //brief-start mode [0=disabled, 1=enabled]
em[26] = atoi(optarg);
break;
case 'j': //step scroll value
em[21] = em[22] = atoi(optarg);
break;
case 'k': //mode - multi/uni
em[14] = atoi(optarg);
if ( em[14] >= 2 ) {
print_app_usage();
exit(EXIT_FAILURE);
}
break;
case 'l': //output mode - stdout/file
em[16] = atoi(optarg);
if ( em[16] >= 3 ) {
print_app_usage();
exit(EXIT_FAILURE);
}
break;
case 'm': //full/compact display
em[27] = atoi(optarg);
break;
case 'n': //network device
//~ sprintf(dev, "%s", optarg);
dev = strdup(optarg);
em[2] = 1;
break;
case 'o': //app priority
em[24] = atoi(optarg);
//~ if ( em[24] < 0 || em[24] > 99 ) {
//~ printf("process priority -o%i: priority range violation [priority range: 0:99]\n", em[24]);
//~ exit(EXIT_FAILURE);
//~ }
if ( em[24] == 99 ) {
int u = getuid();
if ( u != 0 ) {
printf(" -o%i: real-time priority requested ['root' privileges required]\n", em[24]);
exit(EXIT_FAILURE);
}
}
break;
case 'p': //time tracking algorithm
em[9] = atoi(optarg);
if ( em[9] >= 2 ) {
print_app_usage();
exit(EXIT_FAILURE);
}
break;
case 'q': //time tracking precision
em[4] = atoi(optarg);
if ( em[4] >= 2 ) {
print_app_usage();
exit(EXIT_FAILURE);
}
break;
case 'r': //rate [samples/second]
em[15] = atoi(optarg);
if ( em[15] > rmax || em[15] < 1 ) {
fprintf ( stderr, "execution error [sample rate violation]: -r %s\n", optarg);
fprintf ( stderr, "sample rate limit: 1 <= r <= %i\n", rmax);
exit(EXIT_FAILURE);
}
break;
case 's': //stdout display mode
em[13] = atoi(optarg);
break;
case 't': //interval
decsecs = modf(atof(optarg), &secs);
if ( (secs+decsecs) < inmin ) {
fprintf ( stderr, "execution error [interval violation]: -t %s\n", optarg);
fprintf ( stderr, "interval limit: t >= %6.3lf\n", inmin );
exit(EXIT_FAILURE);
}else{
ival = 1;
dur[2] = secs+decsecs;
}
break;
case 'u': //rolling average interval
em[19] = atoi(optarg);
if ( em[19] < 1 || em[19] > 999 ) {
fprintf ( stderr, "configuration error: -u %s beyond runtime configuration limit of 1 - 999\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'w': //scheduler
em[25] = atoi(optarg);
if ( em[25] >= 7 ) {
print_app_usage();
exit(EXIT_FAILURE);
}
break;
case 'x': //periodic header display interval
em[17] = em[18] = atoi(optarg);
break;
case 'y': //app nice value
em[5] = atoi(optarg);
if ( em[5] < -20 || em[5] >= 20 ) {
printf("process nice -y%i: nice range violation [nice range: -20...19]\n", em[5]);
exit(EXIT_FAILURE);
}
if ( em[5] < 0 ) {
int u = getuid();
if ( u != 0 ) {
printf("priority -y%i < 0 ['root' privileges required]\n", em[5]);
exit(EXIT_FAILURE);
}
}
break;
case 'z': //samples
em[1] = atoi(optarg);
break;
case 'v': //stats mode
em[10] = atoi(optarg);
if ( em[10] >= 5 ) {
print_app_usage();
exit(EXIT_FAILURE);
}
break;
case '?':
if ( optopt == 'a' || optopt == 'b' || optopt == 'c' || optopt == 'd' || optopt == 'f' || optopt == 'j' || optopt == 'k' || optopt == 'l' || optopt == 'm' || optopt == 'n' || optopt == 'o' || optopt == 'q' || optopt == 'p' || optopt == 's' || optopt == 't' || optopt == 'u' || optopt == 'v' || optopt == 'w' || optopt == 'x' || optopt == 'y' || optopt == 'z' ) {
fprintf ( stderr, "execution error [illegal command-line option value]: -%c\n", optopt);
print_app_usage();
exit(EXIT_FAILURE);
} else {
fprintf ( stderr, "execution error [illegal command-line option]: -%c\n", optopt);
print_app_usage();
exit(EXIT_FAILURE);
}
break;
default:
abort ();
}
}
printf("\n");
em[6] = set_ppid(em[3]);
int termshort = 195-termsize(em[3]);
if ( termshort > 0 ) {
if ( termshort != 1 ) printf("terminal line-width is %i columns short of application line-width - disabling line wrapping\n", termshort);
if ( termshort == 1 ) printf("terminal line-width is %i column short of application line-width - disabling line wrapping\n", termshort);
system(DISLINEWRAP);
}
if ( em[3] != 0 ) printf(CLOADLOW);
printf("maximum rate: %lu [samples/second]\n", sysconf(_SC_CLK_TCK));
if ( dev == NULL && ( em[14] == 1 || em[10] == 1) ) {
dev = getNetDev();
}else{
dev = strdup("n/a");
}
if ( dev != NULL ) em[2] = 2;
int sched[4] = {em[6], em[5], em[24], em[25]};
set_priority(sched);
em[5] = getpriority(PRIO_PROCESS, em[6]);
em[24] = sched[2];
em[25] = sched[3];
printf("process %i user-defined priority: %i\n", em[6], em[5]);
if ( cpuaffinity == 1 && em[8] !=0 ) {
printf ("-a%i -b%i: core.affinity/optimization conflict [cpu.affinity takes precedence]\n", em[7], em[8]);
em[8] = 0;
}
if ( cpuaffinity == 1 ) {
em[7] = em[7]==-1?rng(0,em[11],1):em[7];
set_cpuAffinity(em[7], em[12]);
} else {
em[7] = -9;
}
for (int i=optind;i<argc;i++) {
printf ("execution error [unknown command-line option]: %s\n\n", argv[i]);
exit(0);
}
if ( em[16] >= 1 && !outputfile ) {
printf ("execution error - log file unspecified [-f 'filename']\n\n");
exit(0);
}
if ( em[2] == 0 && ( em[14] == 1 || em[10] == 1) ) {
printf ("execution error - network interface unspecified [-n 'device']\n\n");
exit(0);
}
if ( em[2] == 2 && ( em[14] == 1 || em[10] == 1 ) ) printf("network interface unspecified - using device bound to default route: -n %s\n", dev);
if (ival == 0) {
if ( em[15] == 0 ) {
printf ("sample interval/rate unspecified - assuming default: -t 0.1\n");
decsecs = 0.1;
secs = 0;
}else {
if ( em[15] == 1 ) {
decsecs = 0;
secs = 1;
}else if ( em[15] <= RATEMAX ) {
decsecs = 1/(double)em[15];
secs = 0;
}else if ( em[15] > RATEMAX ) {
printf ("execution error [sample rate violation]: %i\n\n", em[15]);
print_app_usage();
exit(EXIT_FAILURE);
}
}
dur[2] = secs+decsecs;
} else if ( ival == 1 ) {
if (em[15] != 0 && em[15] != (double)1/(secs+decsecs) ) {
printf ("option conflit: -t%.3lf -r%i\n\n", secs+decsecs, em[15]);
print_app_usage();
exit(EXIT_FAILURE);
}
}
if (em[1] != 0 && dur[1] !=0 ) {
printf ("option conflit: -d%.2lf -z%i\n\n", dur[1], em[1]);
print_app_usage();
exit(EXIT_FAILURE);
}
em[15]=em[15]!=0?em[15]:rint((double)1/dur[2]);
char *appstart = (char *) malloc (yards + 1);
sprintf(appstart, "%s", argv[0]);
for ( int i=1;i<argc;i++) {
strcat(appstart, " ");
strcat(appstart, argv[i]);
}
em[0] = dmtp(outputfile, appstart, dev, em, dur);
free(dev);
if ( em[0] == 11 ) {
old_attr();
execvp(argv[0], argv);
}
free(appstart);
if ( termshort > 0 ) system(ENALINEWRAP);
return 0;
}