-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench.c
179 lines (135 loc) · 4.27 KB
/
bench.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <pthread.h>
#define ITERATIONS 1000
#define BUFFER_SIZE 2048
#define MAX_THREADS 25
/* create a type alias so that we can
* efficiently pass pointers into functions */
typedef void (*void_ptr)();
static char * program_name;
static clock_t start, end;
/* global mutex lock */
static pthread_mutex_t lock;
/* globals for increments before average */
static float global_counter = 0;
static int thread_completes = 0;
static void
die(const char * error)
{
if (!strcmp(error, ""))
fprintf(stderr, "%s: %s\n", program_name, error);
fprintf(stdout, "%s [test]"
"\navailable tests:"
"\n - singlethread\n - multithread\n",
program_name);
exit(EXIT_FAILURE);
}
static void
sig_handler(int sig)
{
if (sig != SIGINT)
exit(EXIT_FAILURE);
float sig_result;
fprintf(stderr, "\n==\tKILLED\t==\n");
/* calculate average before death */
sig_result = global_counter / (float) thread_completes;
fprintf(stderr, "\n\n[FINAL] Average for %i threads of execution: %.6f ms\n", thread_completes, sig_result);
exit(EXIT_FAILURE);
}
/* IOPS CPU-bound stack-based buffer write
*
* Description:
* Utilizing a writable buffer for benchmarking. Flexible for
* single and multi-threaded support. Configurable constants
* are ITERATIONS and BUFFER_SIZE.
*
* Execution Process:
* 1. Create a cycle
* 2. For each element of the array, set to arbitrary value
* 3. Re-run, NULL each element
* 4. Finish benchmark, output IOPS result
*/
void *
test_buffer_write()
{
/* local register integers for iteration */
register int i, j;
register int cycles;
/* the buffer we will be populating */
int mem_array[BUFFER_SIZE] = {};
/* represents benchmarked time */
int benchmark;
////////////////////////////////////////////////////////
start = clock();
for (cycles = 0; cycles <= ITERATIONS; cycles++){
for (i = 0; i <= BUFFER_SIZE; i++) {
for (j = 0; j <= BUFFER_SIZE; j++)
mem_array[j] = 10000000;
mem_array[i] = (int*) NULL;
}
}
end = clock();
////////////////////////////////////////////////////////
/* lock critical section */
pthread_mutex_lock(&lock);
/* calculate the benchmark and increment counters */
benchmark = ( end - start ) / ( CLOCKS_PER_SEC / 1000 );
global_counter += (float) benchmark;
thread_completes++;
/* unlock critical section */
pthread_mutex_unlock(&lock);
/* output */
fprintf(stdout, "[Job %ld] Run-time Execution Benchmark: %i ms\n", pthread_self(), benchmark);
/* terminate the thread */
pthread_exit(NULL);
}
static float
run(int threadnum, void (*f)(void) )
{
float bench_result;
register int a, b;
pthread_t tid;
signal(SIGINT, sig_handler);
/* kill if thread count is 0 */
if (threadnum == 0)
die("thread count cannot be 0");
/* create our threads and join up */
for ( a = 0; a < threadnum; a++ ){
pthread_create(&tid, NULL, (void *)(*f), NULL);
sleep(1);
pthread_join(tid, NULL);
}
/* calculate a final average */
bench_result = global_counter / (float) thread_completes;
return bench_result;
}
int
main(int argc, char **argv)
{
register int i;
float final_result;
program_name = argv[0];
/* one test at a time! */
if (argc < 2 || argc > 2)
die("incorrect provision of arguments.");
/* perform simple argument parsing */
for (i = 0; i < argc; i ++) {
fprintf(stdout, "==\tEXECUTION\t==\nTEST: ");
if (!strcmp(argv[i], "singlethread")) {
fprintf(stdout, "Single-threaded stack-based buffer-write\n");
final_result = run(1, (void *) test_buffer_write);
} else if (!strcmp(argv[i], "multithread")) {
fprintf(stdout, "Multi-threaded stack-based buffer-write\n");
final_result = run(MAX_THREADS, (void *) test_buffer_write);
}
}
/* success: print final average */
fprintf(stdout, "\n==\tCOMPLETE\t==\n");
fprintf(stdout, "\n\n[FINAL] Average for %i threads of execution: %.6f ms\n", thread_completes, final_result);
return 0;
}