Skip to content

Commit 80a84b6

Browse files
committed
Better stats for jitter
1 parent 7d411f3 commit 80a84b6

11 files changed

Lines changed: 198 additions & 85 deletions

File tree

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
"forward_list": "cpp",
8484
"__verbose_abort": "cpp",
8585
"unordered_set": "cpp",
86-
"valarray": "cpp"
86+
"valarray": "cpp",
87+
"semaphore": "cpp",
88+
"*.tmpl": "cpp"
8789
}
8890
}

libs/AsmUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#pragma once

libs/CpuUtils.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
#include "CpuUtils.h"
22
#include <cstdio>
3+
#include <sys/mman.h>
4+
5+
static auto affinity = makeSet(getThreadAffinity());
6+
bool isIsolated(int core) {
7+
return affinity.find(core) == affinity.end();
8+
}
9+
10+
bool lockAllMemory() {
11+
if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
12+
printf("Failed to lock memory. Please increase RLIMIT_MEMLOCK\n");
13+
return false;
14+
}
15+
return true;
16+
}
317

418
std::vector<std::size_t> getThreadAffinity() {
519
std::vector<std::size_t> cores;

libs/CpuUtils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
#include <pthread.h>
44
#include <sys/sysinfo.h>
55
#include <vector>
6+
#include <set>
7+
8+
bool lockAllMemory();
9+
10+
template <typename Container>
11+
std::set<typename Container::value_type> makeSet(const Container& container) {
12+
std::set<typename Container::value_type> cset;
13+
for (const auto& value : container) {
14+
cset.insert(value);
15+
}
16+
return cset;
17+
}
18+
19+
bool isIsolated(int core);
620

721
std::vector<std::size_t> getThreadAffinity();
822

libs/DateUtils.h

Lines changed: 0 additions & 11 deletions
This file was deleted.

libs/StringUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <algorithm>
66
#include <cstring>
77

8+
static inline const char* yn(bool flag) {
9+
return flag ? "Y" : "N";
10+
}
11+
812
template <size_t N>
913
struct TinyString {
1014
char buffer[N];

libs/TimingUtils.h

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@
55
#include <limits>
66
#include <ctime>
77

8+
// Returns a timestamp in nanoseconds, monotonic
9+
static inline int64_t nowts() {
10+
timespec ts;
11+
int res = clock_gettime(CLOCK_MONOTONIC, &ts);
12+
if (res == 0) {
13+
return int64_t(ts.tv_sec) * 1000000000L + int64_t(ts.tv_nsec);
14+
}
15+
return 0;
16+
}
17+
818
//! Returns the number of nanoseconds in UTC
9-
int64_t utcnow() {
19+
static inline int64_t utcnow() {
1020
struct timespec ts;
1121
int res = ::clock_gettime(CLOCK_REALTIME, &ts);
1222
if (res == 0) {
@@ -19,10 +29,6 @@ int64_t utcnow() {
1929

2030
#include <x86intrin.h>
2131

22-
std::uint64_t tic() {
23-
return __builtin_ia32_rdtsc();
24-
}
25-
2632
template <typename IntType,
2733
typename RangeType = typename std::make_unsigned<IntType>::type>
2834
static inline RangeType forward_difference(const IntType& a, const IntType& b) {
@@ -38,6 +44,10 @@ static inline void disable_reorder() {
3844
asm volatile("" ::: "memory");
3945
}
4046

47+
static inline std::uint64_t tic() {
48+
return __builtin_ia32_rdtsc();
49+
}
50+
4151
#else
4252

4353
#include <chrono>
@@ -91,3 +101,14 @@ double timeit(HistT& hist, Fn&& fn) {
91101
hist.add(elapsed / count);
92102
return elapsed;
93103
}
104+
105+
template <typename Fn>
106+
void busyWait(uint64_t ticks, Fn fn) {
107+
uint64_t start = tic();
108+
uint64_t finish = start + ticks;
109+
uint64_t now;
110+
while ((now = tic()) < finish) {
111+
mfence();
112+
fn(now);
113+
}
114+
}

tests/cache/testTickerLookup.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "StringUtils.h"
1717
#include "Datasets.h"
1818
#include "Ticker.h"
19-
#include "DateUtils.h"
19+
#include "TimingUtils.h"
2020
#include "Regression.h"
2121

2222
#include <boost/random/mersenne_twister.hpp>
@@ -62,7 +62,7 @@ void testme(const std::string& key, Snapshot& snap,
6262
}
6363

6464
// Loop measuring lookups
65-
double start = now();
65+
double start = nowts();
6666
snap.start();
6767
uint64_t counter = 0;
6868
do {
@@ -71,7 +71,7 @@ void testme(const std::string& key, Snapshot& snap,
7171
book.count += 1;
7272
counter++;
7373
}
74-
} while (now() < start + runsecs);
74+
} while (nowts() < start + runsecs);
7575
snap.stop(key.c_str(), numtickers, counter);
7676

7777
// The sum of all counters has to match

tests/cache/testWordMap.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
#include "Datasets.h"
1212
#include "StringUtils.h"
1313
#include "Snapshot.h"
14-
#include "DateUtils.h"
1514
#include "Allocators.h"
1615
#include "Counter.h"
1716
#include "Regression.h"
17+
#include "TimingUtils.h"
1818

1919
// The actual test run, templated by map type
2020
template <template <typename Key, typename Value, typename AllocType> class MapType,
@@ -25,13 +25,13 @@ void testme(const std::string& testname, Snapshot& snap,
2525
using KeyValuePair = std::pair<std::string_view, Counter<int>>;
2626
MapType<std::string_view, Counter<int>, AllocatorType<KeyValuePair>> words;
2727

28-
double start = now();
28+
double start = nowts();
2929
snap.start();
3030
uint64_t counter = 0;
3131
do {
3232
for (std::string_view w : allwords) words[w]++;
3333
counter++;
34-
} while (now() < start + runsecs);
34+
} while (nowts() < start + runsecs);
3535
snap.stop(testname.c_str(), numwords, counter * allwords.size());
3636

3737
#ifdef DEBUG

0 commit comments

Comments
 (0)