-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
149 lines (119 loc) · 3.23 KB
/
main.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
#include <inttypes.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <argp.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
typedef struct {
uint64_t ctr;
char *file;
double start_ns;
} lua_task;
void *wrapper(void *arg) {
lua_task *task = (lua_task*)arg;
struct timespec cur_time;
double start_time_ns = task->start_ns;
uint64_t ctr = 0;
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
int status = luaL_loadfile(L, task->file);
if (status) {
fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
exit(1);
}
int cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
do {
clock_gettime(CLOCK_MONOTONIC, &cur_time);
double cur_time_ns = cur_time.tv_sec * 1e9 + cur_time.tv_nsec;
if (start_time_ns + 10 * 1e9 < cur_time_ns) {
break;
}
lua_rawgeti(L, LUA_REGISTRYINDEX, cb_ref);
int result = lua_pcall(L, 0, 0, 0);
if (result) {
fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
exit(1);
}
ctr++;
} while(1);
task->ctr = ctr;
return NULL;
}
typedef struct {
int c, q, d, b;
char *file_name;
} cmd_line_options;
const char *argp_program_version =
"lua comp bench 0.0001-alpha-zeta-teta";
const char *argp_program_bug_address =
"<[email protected]>";
/* Program documentation. */
static char doc[] =
"Runs lua on multiple threads";
/* A description of the arguments we accept. */
static char args_doc[] = "file1";
static struct argp_option options[] = {
{"concurency", 'c', "concurency", 0, "Number of threads" },
{ 0 }
};
static error_t
parse_opt (int key, char *arg, struct argp_state *state) {
cmd_line_options *arguments = state->input;
switch (key) {
case 'c':
arguments->c = atoi(arg);
break;
case ARGP_KEY_ARG:
if (state->arg_num >= 1) {
argp_usage (state);
}
arguments->file_name = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 1) {
argp_usage (state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Our argp parser. */
static struct argp argp = { options, parse_opt, args_doc, doc };
int main(int argc, char *argv[]) {
pthread_t *threads;
lua_task *tasks;
int i;
uint8_t *buf;
size_t len;
struct stat s;
uint64_t total = 0;
struct timespec start_time;
cmd_line_options arguments = (cmd_line_options){.file_name = "", .q = 8, .c = 1};
argp_parse (&argp, argc, argv, 0, 0, &arguments);
int nthreads = arguments.c;
threads = malloc(nthreads * sizeof(pthread_t));
tasks = malloc(nthreads * sizeof(lua_task));
clock_gettime(CLOCK_MONOTONIC, &start_time);
double start_time_ns = start_time.tv_sec * 1e9 + start_time.tv_nsec;
for (i = 0; i < nthreads; i++) {
tasks[i] = (lua_task){.file = arguments.file_name, .start_ns = start_time_ns};
pthread_create(&threads[i], NULL, wrapper, &tasks[i]);
}
for (i = 0; i < nthreads; i++) {
pthread_join(threads[i], NULL);
total += tasks[i].ctr;
}
fprintf(stderr, "Total times executed: %"PRId64"\n", total);
fprintf(stdout, "ops/s %.2f\n", (double)total / 10);
}