-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscall_bench.cpp
68 lines (58 loc) · 1.98 KB
/
syscall_bench.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <fmt/printf.h>
#include <iostream>
#include <memory>
#include <thread>
#include "timers.h"
#if LATENCY
constexpr uint64_t iterations = 100000;
#else
constexpr uint64_t iterations = 5000000;
#endif
int main(int args, char* argv[]) {
#if RAII
#warning "RAII is defined"
std::unique_ptr<uint32_t[]> results = std::make_unique<uint32_t[]>(iterations);
memset(results.get(), 0, iterations);
// lock the pages of the virtual address space into RAM to prevent from swapping
while(0 != mlock(results.get(), sizeof(uint32_t)*iterations)) {}
#else
uint32_t* results = new uint32_t[iterations];
memset(results, 0, iterations);
// lock the pages of the virtual address space into RAM to prevent from swapping
while(0 != mlock(results, sizeof(uint32_t)*iterations)) {}
#endif
struct timespec ts;
auto start_ns = time_ns(&ts);
auto start = rdtsc();
auto start2 = start;
auto tmp = rdtsc();
for (uint64_t i = 0; i < iterations; i++) {
// execute syscall(SYS_gettid) but this is not implemented in SCONE
std::this_thread::get_id();
#if LATENCY
tmp = rdtsc();
results[i]= tmp - start;
start = tmp;
#endif
}
auto stop = rdtsc();
const uint64_t delta = time_ns(&ts) - start_ns;
uint64_t sum = 0;
for (int i = 0; i < iterations; i++) {
sum += results[i];
}
fmt::print("{}\titerations={}\tcycles={}\tavg_lat={}cycles/syscall\n", __func__, iterations, sum, (float) sum/ ((float) iterations));
fmt::print("{}\titerations={}\tavg_cycles={}\tavg_lat={}cycles/syscall\n", __func__, iterations, (stop-start2), (float) (stop-start2)/ ((float) iterations));
fmt::print("{}\titerations={}\ttime={}ns\tavg_lat={}ns/syscall\n", __func__, iterations, delta, delta / ((float) iterations));
#ifndef RAII
delete[] results;
#endif
return 0;
}