Welcome to mirror list, hosted at ThFree Co, Russian Federation.

profiler.h « Internal « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b17deddf09f2475e9e9281938fd9702717c50828 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//-----------------------------------------------------------------------------
//           Name: profiler.h
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#pragma once

class StackAllocator;

#if defined(OGDA) || defined(OG_WORKER)

#define PROFILER_ENTER(x, y) \
    do {                     \
    } while (0)
#define PROFILER_LEAVE(x) \
    do {                  \
    } while (0)
#define PROFILER_ZONE(x, y) \
    do {                    \
    } while (0)

struct ProfilerContext {};  // TODO: handle better?

#else

// for figuring out why profiler leaks zones
// only tested on Linux...
#define PROFILER_DEBUG 0

#if PROFILER_DEBUG

#undef NTELEMETRY
#define NTELEMETRY 1

struct ProfilerContext {
    void Init(StackAllocator *stack_allocator) {}
    void Dispose(StackAllocator *stack_allocator) {}
};

struct ProfilerScopedZone {
    ProfilerContext *ctx;
    void *pointer;
    unsigned int expected_depth;

    ProfilerScopedZone(ProfilerContext *ctx_, const char *msg, ...);
    ~ProfilerScopedZone();
};

void profileEnter(ProfilerContext *ctx, const char *msg, ...);
void profileLeave(ProfilerContext *ctx);

#define MERGE_HELPER(a, b) a##b
#define MERGE(a, b) MERGE_HELPER(a, b)

#define PROFILER_ENTER(ctx, ...) profileEnter(ctx, __VA_ARGS__);
#define PROFILER_LEAVE(ctx) profileLeave(ctx);
#define PROFILER_ZONE(ctx, ...) ProfilerScopedZone MERGE(p, __LINE__)(ctx, __VA_ARGS__);
#define PROFILER_ZONE_STALL(ctx, str) ProfilerScopedZone MERGE(p, __LINE__)(ctx, str);
#define PROFILER_ZONE_IDLE(ctx, str) ProfilerScopedZone MERGE(p, __LINE__)(ctx, str);
#define PROFILER_GPU_ZONE(ctx, ...) ProfilerScopedZone MERGE(p, __LINE__)(ctx, __VA_ARGS__);
#define PROFILER_ENTER_STALL_THRESHOLD(ctx, match_id_ptr, u_sec, str) profileEnter(ctx, str);
#define PROFILER_LEAVE_THRESHOLD(ctx, match_id) profileLeave(ctx);
#define PROFILER_ENTER_DYNAMIC_STRING(ctx, str) profileEnter(ctx, str);
#define PROFILER_ZONE_DYNAMIC_STRING(ctx, str) ProfilerScopedZone MERGE(p, __LINE__)(ctx, str);
#define PROFILER_NAME_TIMELINE(ctx, str)
#define PROFILER_NAME_THREAD(ctx, str)
#define PROFILER_TICK(ctx)

#define PROFILED_TEXTURE_MUTEX_LOCK \
    texture_mutex.lock();

#define PROFILED_TEXTURE_MUTEX_UNLOCK \
    texture_mutex.unlock();

#elif GPU_MARKERS

#undef NTELEMETRY
#define NTELEMETRY 1

struct ProfilerContext {
    void Init(StackAllocator* stack_allocator) {}
    void Dispose(StackAllocator* stack_allocator) {}
};

struct ProfilerScopedGPUZone {
    ProfilerScopedGPUZone(const char* msg, ...);
    ~ProfilerScopedGPUZone();
};

#define MERGE_HELPER(a, b) a##b
#define MERGE(a, b) MERGE_HELPER(a, b)

#define PROFILER_ENTER(ctx, ...)
#define PROFILER_LEAVE(ctx)
#define PROFILER_ZONE(ctx, ...)
#define PROFILER_ZONE_STALL(ctx, str)
#define PROFILER_ZONE_IDLE(ctx, str)
#define PROFILER_GPU_ZONE(ctx, ...) ProfilerScopedGPUZone MERGE(p, __LINE__)(__VA_ARGS__);
#define PROFILER_ENTER_STALL_THRESHOLD(ctx, match_id_ptr, u_sec, str)
#define PROFILER_LEAVE_THRESHOLD(ctx, match_id)
#define PROFILER_ENTER_DYNAMIC_STRING(ctx, str)
#define PROFILER_ZONE_DYNAMIC_STRING(ctx, str)
#define PROFILER_NAME_TIMELINE(ctx, str)
#define PROFILER_NAME_THREAD(ctx, str)
#define PROFILER_TICK(ctx)

#define PROFILED_TEXTURE_MUTEX_LOCK \
    texture_mutex.lock();

#define PROFILED_TEXTURE_MUTEX_UNLOCK \
    texture_mutex.unlock();

#elif !defined(NTELEMETRY)
#include <telemetry.h>

class ProfilerContext {
   public:
    static const int kMemSize = 2 * 1024 * 1024;
    HTELEMETRY tm_context;
    void* memory;
    const char* err_str;

    void Init(StackAllocator* stack_allocator);
    void Dispose(StackAllocator* stack_allocator);
};

#define PROFILER_ENTER(ctx, ...) tmEnter((ctx)->tm_context, TMZF_NONE, __VA_ARGS__)
#define PROFILER_LEAVE(ctx) tmLeave((ctx)->tm_context)
#define PROFILER_ZONE(ctx, ...) tmZone((ctx)->tm_context, TMZF_NONE, __VA_ARGS__)
#define PROFILER_ZONE_STALL(ctx, str) tmZone((ctx)->tm_context, TMZF_STALL, (str))
#define PROFILER_ZONE_IDLE(ctx, str) tmZone((ctx)->tm_context, TMZF_IDLE, (str))
#define PROFILER_GPU_ZONE(ctx, ...) tmZone((ctx)->tm_context, TMZF_NONE, __VA_ARGS__)
#define PROFILER_ENTER_STALL_THRESHOLD(ctx, match_id_ptr, u_sec, str) tmEnterEx((ctx)->tm_context, (match_id_ptr), 0, (u_sec), __FILE__, __LINE__, TMZF_STALL, (str))
#define PROFILER_LEAVE_THRESHOLD(ctx, match_id) tmLeaveEx((ctx)->tm_context, (match_id), 0, __FILE__, __LINE__)
#define PROFILER_ENTER_DYNAMIC_STRING(ctx, str) tmEnter((ctx)->tm_context, TMZF_NONE, "%s", tmDynamicString((ctx)->tm_context, (str)))
#define PROFILER_ZONE_DYNAMIC_STRING(ctx, str) tmZone((ctx)->tm_context, TMZF_NONE, "%s", tmDynamicString((ctx)->tm_context, (str)))
#define PROFILER_NAME_TIMELINE(ctx, str) tmSetTimelineSectionName((ctx)->tm_context, (str))
#define PROFILER_NAME_THREAD(ctx, str) tmThreadName((ctx)->tm_context, 0, (str))
#define PROFILER_TICK(ctx) tmTick((ctx)->tm_context);

#define PROFILED_TEXTURE_MUTEX_LOCK                                 \
    TmU64 matchid;                                                  \
    PROFILER_ENTER_STALL_THRESHOLD(g_profiler_ctx,                  \
                                   &matchid, 500, "texture mutex"); \
    texture_mutex.lock();                                           \
    PROFILER_LEAVE_THRESHOLD(g_profiler_ctx, matchid);

#define PROFILED_TEXTURE_MUTEX_UNLOCK \
    texture_mutex.unlock();

#else
struct ProfilerContext {
    void Init(StackAllocator* stack_allocator) {}
    void Dispose(StackAllocator* stack_allocator) {}
};

#define PROFILER_ENTER(ctx, ...)
#define PROFILER_LEAVE(ctx)
#define PROFILER_ZONE(ctx, ...)
#define PROFILER_ZONE_STALL(ctx, str)
#define PROFILER_ZONE_IDLE(ctx, str)
#define PROFILER_GPU_ZONE(ctx, ...)
#define PROFILER_ENTER_STALL_THRESHOLD(ctx, match_id_ptr, u_sec, str)
#define PROFILER_LEAVE_THRESHOLD(ctx, match_id)
#define PROFILER_ENTER_DYNAMIC_STRING(ctx, str)
#define PROFILER_ZONE_DYNAMIC_STRING(ctx, str)
#define PROFILER_NAME_TIMELINE(ctx, str)
#define PROFILER_NAME_THREAD(ctx, str)
#define PROFILER_TICK(ctx)

#define PROFILED_TEXTURE_MUTEX_LOCK \
    texture_mutex.lock();

#define PROFILED_TEXTURE_MUTEX_UNLOCK \
    texture_mutex.unlock();

#endif

#endif

extern ProfilerContext* g_profiler_ctx;