summaryrefslogtreecommitdiff
path: root/PerformanceTrace.hpp
blob: 81ff56a247abf1f751cb25dfaf711ae451e07d7e (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
#ifndef PERFORMANCE_TRACE_HPP_
#define PERFORMANCE_TRACE_HPP_

#include <QtGlobal>
#include <QString>

namespace PerformanceTrace
{
  using RunId = quint64;

  // Measurements use one process-wide monotonic clock and are logged at INFO
  // severity. The functions are thread-safe, but an individual Phase object
  // must remain on one thread. Runs are retained until process exit so delayed
  // asynchronous milestones can still be attributed to their originating run.
  // Enable WSJT-X Diagnostic logging to capture the emitted "PERF" records.

  // A run groups related measurements on a shared monotonic timeline. The
  // category and kind are emitted verbatim, so keep them stable and free of
  // whitespace for log analysis. Records identify milestones, timed phases,
  // and run ends explicitly through the event field.
  RunId begin_run (char const * category, char const * kind);
  RunId current_run ();
  void finish_run (RunId run, char const * phase, QString const& details = {});
  qint64 elapsed_ms ();

  // Pass an explicit run from asynchronous work. current_run() may refer to a
  // newer operation by the time a worker thread or queued callback completes.
  // A finished run still accepts such explicitly attributed measurements.
  void milestone (char const * phase, QString const& details = {});
  void milestone (RunId run, char const * phase, QString const& details = {});

  // Phase records its duration when destroyed. Call finish() when details must
  // accompany the measurement; subsequent calls and destruction are no-ops.
  class Phase final
  {
  public:
    explicit Phase (char const * phase);
    Phase (RunId run, char const * phase);
    ~Phase () noexcept;

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

    void finish (QString const& details = {});

  private:
    RunId run_;
    QString phase_;
    qint64 started_ms_;
    bool finished_ {false};
  };
}

#endif