aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/nanotrace/nanotracehr.h
blob: 1adb6d586b221b27771090d5fb41210f06f336d3 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "nanotraceglobals.h"

#include <utils/span.h>

#include <array>
#include <chrono>
#include <fstream>
#include <future>
#include <mutex>
#include <string>
#include <string_view>

namespace NanotraceHR {
using Clock = std::chrono::steady_clock;
using TimePoint = std::chrono::time_point<Clock>;
using Duration = std::chrono::nanoseconds;

static_assert(Clock::is_steady, "clock should be steady");
static_assert(std::is_same_v<Clock::duration, std::chrono::nanoseconds>,
              "the steady clock should have nano second resolution");

constexpr bool isTracerActive()
{
#ifdef NANOTRACE_ENABLED
    return true;
#else
    return false;
#endif
}

template<std::size_t size>
std::string_view toStringView(Utils::span<const char, size> string)
{
    return {string.data(), string.size()};
}

template<typename String>
struct TraceEvent
{
    TraceEvent() = default;
    TraceEvent(const TraceEvent &) = delete;
    TraceEvent(TraceEvent &&) = delete;
    TraceEvent &operator=(const TraceEvent &) = delete;
    TraceEvent &operator=(TraceEvent &&) = delete;
    ~TraceEvent() = default;

    String name;
    String category;
    String arguments;
    TimePoint start;
    Duration duration;
};

using StringViewTraceEvent = TraceEvent<std::string_view>;
using StringTraceEvent = TraceEvent<std::string>;

enum class IsEnabled { No, Yes };

template<typename TraceEvent>
class EventQueue;

template<typename TraceEvent>
void flushEvents(const Utils::span<TraceEvent> events,
                 std::thread::id threadId,
                 EventQueue<TraceEvent> &eventQueue);
extern template void flushEvents(const Utils::span<StringViewTraceEvent> events,
                                 std::thread::id threadId,
                                 EventQueue<StringViewTraceEvent> &eventQueue);
extern template void flushEvents(const Utils::span<StringTraceEvent> events,
                                 std::thread::id threadId,
                                 EventQueue<StringTraceEvent> &eventQueue);

template<typename TraceEvent>
void flushInThread(EventQueue<TraceEvent> &eventQueue);
extern template void flushInThread(EventQueue<StringViewTraceEvent> &eventQueue);
extern template void flushInThread(EventQueue<StringTraceEvent> &eventQueue);

void openFile(class TraceFile &file);
void finalizeFile(class TraceFile &file);

class TraceFile
{
public:
    TraceFile([[maybe_unused]] std::string_view filePath)
        : filePath{filePath}
    {
        openFile(*this);
    }

    TraceFile(const TraceFile &) = delete;
    TraceFile(TraceFile &&) = delete;
    TraceFile &operator=(const TraceFile &) = delete;
    TraceFile &operator=(TraceFile &&) = delete;

    ~TraceFile() { finalizeFile(*this); }
    std::string filePath;
    std::mutex fileMutex;
    std::future<void> processing;
    std::ofstream out;
};

template<typename TraceEvent>
class EventQueue
{
    using TraceEventsSpan = Utils::span<TraceEvent>;

public:
    EventQueue() = default;
    ~EventQueue()
    {
        if (isEnabled == IsEnabled::Yes)
            flushEvents(currentEvents, std::this_thread::get_id(), *this);
    }

    EventQueue(const EventQueue &) = delete;
    EventQueue(EventQueue &&) = delete;
    EventQueue &operator=(const EventQueue &) = delete;
    EventQueue &operator=(EventQueue &&) = delete;

    TraceFile *file = nullptr;
    TraceEventsSpan eventsOne;
    TraceEventsSpan eventsTwo;
    TraceEventsSpan currentEvents;
    std::size_t eventsIndex = 0;
    IsEnabled isEnabled = IsEnabled::No;
};

template<typename TraceEvent, std::size_t eventCount>
class EventQueueData
{
    using TraceEvents = std::array<TraceEvent, eventCount>;

public:
    EventQueueData(TraceFile &file)
        : file{file}
    {}

    TraceFile &file;
    TraceEvents eventsOne;
    TraceEvents eventsTwo;
};

template<typename TraceEvent, std::size_t eventCount>
struct EventQueueDataPointer
{
    operator EventQueue<TraceEvent>() const
    {
        if constexpr (isTracerActive()) {
            return {&data->file, data->eventsOne, data->eventsTwo, data->eventsOne, 0, IsEnabled::Yes};
        } else {
            return {};
        }
    }

    std::unique_ptr<EventQueueData<TraceEvent, eventCount>> data;
};

template<typename TraceEvent, std::size_t eventCount>
EventQueueDataPointer<TraceEvent, eventCount> makeEventQueueData(TraceFile &file)
{
    if constexpr (isTracerActive()) {
        return {std::make_unique<EventQueueData<TraceEvent, eventCount>>(file)};
    } else {
        return {};
    }
}

EventQueue<StringTraceEvent> &globalEventQueue();

template<typename TraceEvent>
TraceEvent &getTraceEvent(EventQueue<TraceEvent> &eventQueue)
{
    if (eventQueue.eventsIndex == eventQueue.currentEvents.size())
        flushInThread(eventQueue);

    return eventQueue.currentEvents[eventQueue.eventsIndex++];
}

namespace Literals {
struct TracerLiteral
{
    friend constexpr TracerLiteral operator""_t(const char *text, size_t size);

    constexpr operator std::string_view() const { return text; }

private:
    constexpr TracerLiteral(std::string_view text)
        : text{text}
    {}

    std::string_view text;
};
constexpr TracerLiteral operator""_t(const char *text, size_t size)
{
    return {std::string_view{text, size}};
}
} // namespace Literals

using namespace Literals;

template<typename TraceEvent>
class Category
{
public:
    using IsActive = std::true_type;
    TracerLiteral name;
    EventQueue<TraceEvent> &eventQueue;
};

using StringViewCategory = Category<StringViewTraceEvent>;
using StringCategory = Category<StringTraceEvent>;

class DisabledCategory
{};

template<typename TraceEvent>
Category(TracerLiteral name, EventQueue<TraceEvent> &eventQueue) -> Category<TraceEvent>;

template<typename Category>
class Tracer
{
public:
    constexpr Tracer(TracerLiteral, Category &, TracerLiteral) {}
    constexpr Tracer(TracerLiteral, Category &) {}

    ~Tracer() {}
};

template<typename String>
class BasicTracer
{};

template<>
class Tracer<StringViewCategory>
{
public:
    constexpr Tracer(TracerLiteral name, StringViewCategory &category, TracerLiteral arguments)
        : m_name{name}
        , m_arguments{arguments}
        , m_category{category}
    {
        if constexpr (isTracerActive()) {
            if (category.eventQueue.isEnabled == IsEnabled::Yes)
                m_start = Clock::now();
        }
    }

    constexpr Tracer(TracerLiteral name, StringViewCategory &category)
        : Tracer{name, category, "{}"_t}
    {
        if constexpr (isTracerActive()) {
            if (category.eventQueue.isEnabled == IsEnabled::Yes)
                m_start = Clock::now();
        }
    }

    ~Tracer()
    {
        if constexpr (isTracerActive()) {
            if (m_category.eventQueue.isEnabled == IsEnabled::Yes) {
                auto duration = Clock::now() - m_start;
                auto &traceEvent = getTraceEvent(m_category.eventQueue);
                traceEvent.name = m_name;
                traceEvent.category = m_category.name;
                traceEvent.arguments = m_arguments;
                traceEvent.start = m_start;
                traceEvent.duration = duration;
            }
        }
    }

private:
    TimePoint m_start;
    std::string_view m_name;
    std::string_view m_arguments;
    StringViewCategory &m_category;
};

template<>
class Tracer<StringCategory>
{
public:
    Tracer(std::string name, StringViewCategory &category, std::string arguments)
        : m_name{std::move(name)}
        , m_arguments{arguments}
        , m_category{category}
    {
        if constexpr (isTracerActive()) {
            if (category.eventQueue.isEnabled == IsEnabled::Yes)
                m_start = Clock::now();
        }
    }

    Tracer(std::string name, StringViewCategory &category)
        : Tracer{std::move(name), category, "{}"}
    {
        if constexpr (isTracerActive()) {
            if (category.eventQueue.isEnabled == IsEnabled::Yes)
                m_start = Clock::now();
        }
    }

    ~Tracer()
    {
        if constexpr (isTracerActive()) {
            if (m_category.eventQueue.isEnabled == IsEnabled::Yes) {
                auto duration = Clock::now() - m_start;
                auto &traceEvent = getTraceEvent(m_category.eventQueue);
                traceEvent.name = std::move(m_name);
                traceEvent.category = m_category.name;
                traceEvent.arguments = std::move(m_arguments);
                traceEvent.start = m_start;
                traceEvent.duration = duration;
            }
        }
    }

private:
    TimePoint m_start;
    std::string m_name;
    std::string m_arguments;
    StringViewCategory &m_category;
};

template<typename Category>
Tracer(TracerLiteral name, Category &category) -> Tracer<Category>;

class GlobalTracer
{
public:
    GlobalTracer(std::string name, std::string category, std::string arguments)
        : m_name{std::move(name)}
        , m_category{std::move(category)}
        , m_arguments{std::move(arguments)}
    {
        if constexpr (isTracerActive()) {
            if (globalEventQueue().isEnabled == IsEnabled::Yes)
                m_start = Clock::now();
        }
    }

    GlobalTracer(std::string name, std::string category)
        : GlobalTracer{std::move(name), std::move(category), "{}"}
    {}

    ~GlobalTracer()
    {
        if constexpr (isTracerActive()) {
            if (globalEventQueue().isEnabled == IsEnabled::Yes) {
                auto duration = Clock::now() - m_start;
                auto &traceEvent = getTraceEvent(globalEventQueue());
                traceEvent.name = std::move(m_name);
                traceEvent.category = std::move(m_category);
                traceEvent.arguments = std::move(m_arguments);
                traceEvent.start = std::move(m_start);
                traceEvent.duration = std::move(duration);
            }
        }
    }

private:
    TimePoint m_start;
    std::string m_name;
    std::string m_category;
    std::string m_arguments;
};

} // namespace NanotraceHR