Skip to content

Commit 7d411f3

Browse files
committed
Added jitter test
1 parent a7cc607 commit 7d411f3

11 files changed

Lines changed: 270 additions & 5 deletions

File tree

libs/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set( LIBRARY_CPP_FILES Snapshot.cpp PerfGroup.cpp )
1+
set( LIBRARY_CPP_FILES Snapshot.cpp PerfGroup.cpp CpuUtils.cpp )
22
set( LIBRARY_DEPENDENCIES pfm )
33
if ( Armadillo_FOUND )
44
list( APPEND LIBRARY_CPP_FILES Regression.cpp )
@@ -7,7 +7,7 @@ endif()
77
add_library( tinyperfstats SHARED ${LIBRARY_CPP_FILES} )
88
target_link_libraries( tinyperfstats ${LIBRARY_DEPENDENCIES} )
99

10-
set( HEADER_LIST Allocators.h BitUtils.h DateUtils.h Histogram.h KahanSum.h MicroStats.h PerfCounter.h Snapshot.h StringUtils.h Ticker.h TimingHelpers.h Regression.h )
10+
set( HEADER_LIST Allocators.h BitUtils.h CpuUtils.h DateUtils.h Histogram.h KahanSum.h MicroStats.h PerfCounter.h Snapshot.h StringUtils.h Ticker.h TimingUtils.h Regression.h )
1111
foreach( HEADER ${HEADER_LIST} )
1212
file( REAL_PATH ${HEADER} ABSHEADER BASE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} )
1313
list( APPEND ALLHEADERS ${ABSHEADER} )

libs/CpuUtils.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "CpuUtils.h"
2+
#include <cstdio>
3+
4+
std::vector<std::size_t> getThreadAffinity() {
5+
std::vector<std::size_t> cores;
6+
pthread_t self = pthread_self();
7+
cpu_set_t cpuset;
8+
CPU_ZERO(&cpuset);
9+
::pthread_getaffinity_np(self, sizeof(cpuset), &cpuset);
10+
for (int j = 0; j < CPU_SETSIZE; j++) {
11+
if (CPU_ISSET(j, &cpuset)) {
12+
cores.push_back(j);
13+
}
14+
}
15+
return cores;
16+
}
17+
18+
bool setThreadAfinity(const std::vector<std::size_t>& cores) {
19+
pthread_t self = pthread_self();
20+
cpu_set_t mask;
21+
CPU_ZERO(&mask);
22+
for (std::size_t core : cores) {
23+
CPU_SET(core, &mask);
24+
}
25+
int err = ::pthread_setaffinity_np(self, sizeof(mask), &mask);
26+
return (err == 0);
27+
}
28+
29+
bool setThreadAfinity(std::size_t core) {
30+
pthread_t self = pthread_self();
31+
cpu_set_t mask;
32+
CPU_ZERO(&mask);
33+
CPU_SET(core, &mask);
34+
int err = ::pthread_setaffinity_np(self, sizeof(mask), &mask);
35+
return (err == 0);
36+
}
37+
38+
std::size_t getCurrentCore() {
39+
return sched_getcpu();
40+
}
41+
42+
std::size_t getNumberOfCores() {
43+
return get_nprocs_conf();
44+
}
45+
46+
const char* getPolicyName(int policy) {
47+
switch (policy) {
48+
case SCHED_RR: return "RoundRobin"; break;
49+
case SCHED_BATCH: return "Batch"; break;
50+
case SCHED_OTHER: return "Standard"; break;
51+
case SCHED_IDLE: return "Idle"; break;
52+
default: return "N/A"; break;
53+
}
54+
}
55+
56+
bool setPriority(int scheduler, int prio) {
57+
pthread_t tid = pthread_self();
58+
struct sched_param param;
59+
int policy;
60+
int res = pthread_getschedparam(tid, &policy, &param);
61+
if (res != 0) {
62+
perror("pthread_getschedparam");
63+
return false;
64+
}
65+
param.sched_priority = prio;
66+
res = pthread_setschedparam(tid, scheduler, &param);
67+
if (res != 0) {
68+
perror("pthread_setschedparam");
69+
return false;
70+
}
71+
return true;
72+
}
73+
74+
void setRealtimePriority(int prio) {
75+
setPriority(SCHED_RR, prio);
76+
}
77+
78+
void setIdlePriority(int prio) {
79+
setPriority(SCHED_IDLE, prio);
80+
}
81+
82+
void setStandardPriority(int prio) {
83+
setPriority(SCHED_OTHER, prio);
84+
}

libs/CpuUtils.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <pthread.h>
4+
#include <sys/sysinfo.h>
5+
#include <vector>
6+
7+
std::vector<std::size_t> getThreadAffinity();
8+
9+
bool setThreadAfinity(const std::vector<std::size_t>& cores);
10+
11+
bool setThreadAfinity(std::size_t core);
12+
13+
std::size_t getCurrentCore();
14+
15+
std::size_t getNumberOfCores();
16+
17+
const char* getPolicyName(int policy);
18+
bool setPriority(int scheduler, int prio);
19+
void setRealtimePriority(int prio);
20+
void setIdlePriority(int prio);
21+
void setStandardPriority(int prio);

libs/MicroStats.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ class MicroStats {
124124
return Range{base + offset, base + (base >> NDB) + offset - 1};
125125
}
126126

127+
//! Returns the number of events
128+
uint64_t count() const {
129+
return totalcount;
130+
}
131+
132+
//! Returns the global average
133+
double average() const {
134+
return totalsum / totalcount;
135+
}
136+
127137
private:
128138
//! Initializes the histogram with the calculated ranges
129139
void init() {
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <type_traits>
5+
#include <limits>
6+
#include <ctime>
7+
8+
//! Returns the number of nanoseconds in UTC
9+
int64_t utcnow() {
10+
struct timespec ts;
11+
int res = ::clock_gettime(CLOCK_REALTIME, &ts);
12+
if (res == 0) {
13+
return int64_t(ts.tv_sec) * 1000000000L + int64_t(ts.tv_nsec);
14+
}
15+
return 0;
16+
}
417

518
#if defined(__GNUC__) && defined(__x86_64__)
619

720
#include <x86intrin.h>
821

922
std::uint64_t tic() {
10-
return __rdtsc();
23+
return __builtin_ia32_rdtsc();
24+
}
25+
26+
template <typename IntType,
27+
typename RangeType = typename std::make_unsigned<IntType>::type>
28+
static inline RangeType forward_difference(const IntType& a, const IntType& b) {
29+
if (a >= b) return a - b;
30+
return (std::numeric_limits<IntType>::max() - a) + b;
1131
}
1232

1333
static inline void mfence() {

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ add_subdirectory( memorymap )
33
add_subdirectory( cache )
44
add_subdirectory( duffloop )
55
add_subdirectory( microstats )
6+
add_subdirectory( jitter )
File renamed without changes.

tests/cache/testCacheSize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
#include "TimingHelpers.h"
2+
#include "TimingUtils.h"
33
#include "Histogram.h"
44
#include <x86intrin.h>
55

tests/duffloop/testDuffLoop.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <cstddef>
44
#include <cstdint>
5-
#include "TimingHelpers.h"
5+
#include "TimingUtils.h"
66
#include "Histogram.h"
77

88
// Defined in DuffLoopHelpers.cpp

tests/jitter/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_executable( jitter jitter.cpp )
2+
target_link_libraries( jitter tinyperfstats pthread )
3+
4+
list( APPEND TARGETS jitter )

0 commit comments

Comments
 (0)