-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathProfiler.h
More file actions
105 lines (91 loc) · 2.5 KB
/
Copy pathProfiler.h
File metadata and controls
105 lines (91 loc) · 2.5 KB
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
#ifndef ELFSPY_PROFILER_H
#define ELFSPY_PROFILER_H
#include <time.h>
#include "elfspy/Capture.h"
namespace spy
{
/**
* @namespace spy
* @class Profiler
*/
template <typename H, typename ReturnType, typename... ArgTypes>
class Profiler : public Capture<unsigned long long>
{
public:
Profiler(H& hook);
Profiler(const Profiler&) = delete;
Profiler& operator=(const Profiler&) = delete;
Profiler(Profiler&& move) = default;
Profiler& operator=(Profiler& move) = default;
~Profiler();
private:
static ReturnType profile(ArgTypes...);
static ReturnType (*func_)(ArgTypes...);
static Profiler* instance_;
struct Recorder;
friend class Recorder;
inline void add(unsigned long long nanoseconds)
{
this->captures_.push_back(nanoseconds);
}
struct Recorder
{
inline Recorder()
{
clock_gettime(CLOCK_REALTIME, &start_);
}
inline ~Recorder()
{
struct timespec finish;
clock_gettime(CLOCK_REALTIME, &finish);
unsigned long long nanoseconds = 1000000000ULL
* (finish.tv_sec - start_.tv_sec)
+ finish.tv_nsec - start_.tv_nsec;
Profiler::instance_->add(nanoseconds);
}
struct timespec start_;
};
H& hook_;
};
template <typename H, typename ReturnType, typename... ArgTypes>
Profiler<H, ReturnType, ArgTypes...>*
Profiler<H, ReturnType, ArgTypes...>::instance_ = nullptr;
template <typename H, typename ReturnType, typename... ArgTypes>
ReturnType (*Profiler<H, ReturnType, ArgTypes...>::func_)(ArgTypes...)
= nullptr;
template <typename H, typename ReturnType, typename... ArgTypes>
inline Profiler<H, ReturnType, ArgTypes...>::
Profiler(H& hook)
:hook_(hook)
{
func_ = hook_.patch(&Profiler::profile);
instance_ = this;
}
template <typename H, typename ReturnType, typename... ArgTypes>
inline Profiler<H, ReturnType, ArgTypes...>::~Profiler()
{
hook_.patch(func_);
instance_ = nullptr;
}
template <typename H, typename ReturnType, typename... ArgTypes>
ReturnType Profiler<H, ReturnType, ArgTypes...>::profile(ArgTypes... args)
{
Recorder r;
return (*func_)(std::forward<ArgTypes>(args)...);
}
template <typename H>
inline auto profiler(H& hook)
-> typename H::template Export<Profiler, H, typename H::Result>::Type
{
return hook;
}
template <typename H>
inline auto new_profiler(H& hook)
-> typename H::template Export<Profiler, H, typename H::Result>::Type*
{
using Install =
typename H::template Export<Profiler, H, typename H::Result>::Type;
return new Install(hook);
}
} // namespace elfspy
#endif