-
Notifications
You must be signed in to change notification settings - Fork 165
/
profiling.cpp
88 lines (72 loc) · 2.51 KB
/
profiling.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
#include "moonlight.hpp"
#include <stdio.h>
#include <sys/time.h>
#define PACKED_TIME_SECONDS_BITSHIFT 16
#define PACKED_TIME_MILLIS_MASK 0xFFFF
uint32_t MoonlightInstance::ProfilerGetPackedMillis() {
#if defined(ENABLE_PROFILING)
struct timeval tv;
uint32_t res;
gettimeofday(&tv, NULL);
res = tv.tv_sec << PACKED_TIME_SECONDS_BITSHIFT;
res += (tv.tv_usec / 1000) & PACKED_TIME_MILLIS_MASK;
return res;
#else
return 0;
#endif
}
uint64_t MoonlightInstance::ProfilerGetMillis() {
#if defined(ENABLE_PROFILING)
struct timeval tv;
uint64_t res;
gettimeofday(&tv, NULL);
res = tv.tv_sec * 1000;
res += tv.tv_usec / 1000;
return res;
#else
return 0;
#endif
}
// The return value of this function is referenced to an
// arbitrary epoch, and as such is only suitable for comparison
// with other unpacked time values.
uint64_t MoonlightInstance::ProfilerUnpackTime(uint32_t packedTime) {
#if defined(ENABLE_PROFILING)
uint64_t res;
res = (packedTime >> PACKED_TIME_SECONDS_BITSHIFT) * 1000;
res += (packedTime & PACKED_TIME_MILLIS_MASK);
return res;
#else
return 0;
#endif
}
static void printDeltaAboveThreshold(const char* message, uint32_t delta) {
#if defined(ENABLE_PROFILING)
if (PROFILING_MESSAGE_THRESHOLD < 0 || delta > PROFILING_MESSAGE_THRESHOLD) {
printf("%s: %d ms\n", message, delta);
}
#endif
}
void MoonlightInstance::ProfilerPrintPackedDeltaFromNow(const char* message, uint32_t packedTime) {
ProfilerPrintPackedDelta(message, packedTime, ProfilerGetPackedMillis());
}
void MoonlightInstance::ProfilerPrintPackedDelta(const char* message,
uint32_t packedTimeA,
uint32_t packedTimeB) {
printDeltaAboveThreshold(message,
(uint32_t)(ProfilerUnpackTime(packedTimeB) -
ProfilerUnpackTime(packedTimeA)));
}
void MoonlightInstance::ProfilerPrintWarning(const char* message) {
#if defined(ENABLE_PROFILING)
printf("PROFILING WARNING: %s\n", message);
#endif
}
void MoonlightInstance::ProfilerPrintDeltaFromNow(const char* message, uint64_t time) {
ProfilerPrintDelta(message, time, ProfilerGetMillis());
}
void MoonlightInstance::ProfilerPrintDelta(const char* message,
uint64_t timeA,
uint64_t timeB) {
printDeltaAboveThreshold(message, (uint32_t)(timeB - timeA));
}