This repository was archived by the owner on Jul 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavg.c
More file actions
78 lines (63 loc) · 1.68 KB
/
avg.c
File metadata and controls
78 lines (63 loc) · 1.68 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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>
#define GREEN "\e[;32m"
#define RED "\e[;31m"
#define RESET "\e[;0m"
#define NTIMES 5
#define USECS(timeval) ((timeval)->tv_sec * 1000000 + (timeval)->tv_usec)
typedef struct rusage rusage_t;
static void systime(char **argv, rusage_t *usage) {
int pid, pid2, status;
pid = fork();
assert(pid != -1);
if (pid == 0) {
close(STDOUT_FILENO);
/* child */
execvp(argv[0], argv);
assert(0);
}
pid2 = wait3(&status, 0, usage);
assert(pid2 == pid);
}
static void avg(char *prog, char *file) {
rusage_t joule;
char cmd[1024];
sprintf(cmd, "luac -o %s.luac %s", file, file);
system(cmd);
sprintf(cmd, "%s.luac", file);
char **argv;
char *joule_argv[] = {prog, "-c", cmd, NULL};
char *lua_argv[] = {prog, cmd, NULL};
argv = strcmp(prog, "lua") == 0 ? lua_argv : joule_argv;
long total, user, system, mem;
total = user = system = mem = 0;
int i;
for (i = 0; i < NTIMES; i++) {
systime(argv, &joule);
total += USECS(&joule.ru_utime) + USECS(&joule.ru_stime);
user += USECS(&joule.ru_utime);
system += USECS(&joule.ru_stime);
mem += joule.ru_maxrss;
}
unlink(cmd);
printf("%27s,", file);
printf("%10ld,%10ld,%10ld,%10ld\n", total/NTIMES, user/NTIMES, system/NTIMES, mem/NTIMES);
}
int main(int argc, char **argv) {
int i;
if (argc < 2) {
printf("Usage: %s file1 [file2 [file3 ...]]\n", argv[0]);
return 1;
}
printf("%27s%10s%10s%10s%10s\n", "Test File", "total", "user",
"system", "mem");
for (i = 2; i < argc; i++) {
avg(argv[1], argv[i]);
}
return 0;
}