-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm.cpp
148 lines (130 loc) · 3.24 KB
/
osm.cpp
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
#include "osm.h"
#include <sys/time.h>
#include <cmath>
#define UNROLL_TIMES 10
#define DEFAULT_ITER 1000
#define SEC_FACT 1000000000L
#define USEC_FACT 1000L
#define ERROR_CODE 1
double calc_total_time(unsigned int iterations, const timeval &start, const timeval &end);
unsigned int check_iterations(unsigned int iterations);
int osm_init()
{ return 0; }
int osm_finalizer()
{ return 0; }
double osm_operation_time(unsigned int iterations)
{
iterations = check_iterations(iterations);
struct timeval start, end;
double x = 0;
if (gettimeofday(&start, nullptr))
{
return ERROR_CODE;
}
for (unsigned int i = 0; i < iterations; i += UNROLL_TIMES)
{
x = 1 + 1;
x = 1 + 1;
x = 1 + 1;
x = 1 + 1;
x = 1 + 1;
x = 1 + 1;
x = 1 + 1;
x = 1 + 1;
x = 1 + 1;
x = 1 + 1;
}
if (gettimeofday(&end, nullptr))
{
return ERROR_CODE;
}
// avoid compiler issues:
x = 0;
iterations += (unsigned int) x;
return calc_total_time(iterations, start, end);
}
void func()
{}
double osm_function_time(unsigned int iterations)
{
iterations = check_iterations(iterations);
struct timeval start, end;
if (gettimeofday(&start, nullptr))
{
return ERROR_CODE;
}
for (unsigned int i = 0; i < iterations; i += UNROLL_TIMES)
{
func();
func();
func();
func();
func();
func();
func();
func();
func();
func();
}
if (gettimeofday(&end, nullptr))
{
return ERROR_CODE;
}
return calc_total_time(iterations, start, end);
}
double osm_syscall_time(unsigned int iterations)
{
iterations = check_iterations(iterations);
struct timeval start, end;
if (gettimeofday(&start, nullptr))
{
return ERROR_CODE;
}
for (unsigned int i = 0; i < iterations; i += UNROLL_TIMES)
{
OSM_NULLSYSCALL;
OSM_NULLSYSCALL;
OSM_NULLSYSCALL;
OSM_NULLSYSCALL;
OSM_NULLSYSCALL;
OSM_NULLSYSCALL;
OSM_NULLSYSCALL;
OSM_NULLSYSCALL;
OSM_NULLSYSCALL;
OSM_NULLSYSCALL;
}
if (gettimeofday(&end, nullptr))
{
return ERROR_CODE;
}
return calc_total_time(iterations, start, end);
}
/**
* This function calculates the time difference between end and start.
* @param iterations the number of iterations made in the time par, we will divide by it
* @param start the start timestamp, a timeval
* @param end the end timestamp, a timeval
* @return double- the time difference
*/
double calc_total_time(unsigned int iterations, const timeval &start, const timeval &end)
{
return double(((end.tv_sec - start.tv_sec) * SEC_FACT) +
((end.tv_usec - start.tv_usec) * USEC_FACT)) / double(iterations);
}
/**
* this function validates the iteration given
* @param iterations the iterations to check
* @return a non-zero, round-up iteration number.
*/
unsigned int check_iterations(unsigned int iterations)
{
if (iterations == 0)
{
iterations = DEFAULT_ITER;
}
if (iterations % UNROLL_TIMES)
{
iterations += UNROLL_TIMES - (iterations % UNROLL_TIMES);
}
return iterations;
}