Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
stacksampler_p.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <QtCore/QJsonArray>
8#include <QtCore/QJsonObject>
9#include <QtCore/QString>
10
11#include <atomic>
12#include <chrono>
13#include <condition_variable>
14#include <map>
15#include <mutex>
16#include <thread>
17
18namespace Sonde::Detail {
19
28{
29public:
31 struct Settings
32 {
33 std::chrono::milliseconds threshold{ 250 };
34 std::chrono::milliseconds interval{ 1000 };
35 int maxSamples = 10;
36 };
37
39 static constexpr std::chrono::milliseconds kTick{ 100 };
40
42 explicit StackSampler(Settings settings);
45 StackSampler(const StackSampler &) = delete;
46 StackSampler &operator=(const StackSampler &) = delete;
47
49 QString start();
51 void stop();
53 bool isRunning() const { return m_running.load(); }
54
56 void tick();
57
60 struct Stall
61 {
62 QJsonArray samples;
63 QJsonArray modules;
65 bool isEmpty() const { return samples.isEmpty(); }
66 };
67
69 Stall takeEndedStall();
71 Stall currentStall() const;
72
75 void arm(Settings settings, std::chrono::milliseconds duration);
77 std::chrono::milliseconds effectiveThreshold() const;
78
79private:
81 void run();
83 Settings settingsAt(qint64 nowMs);
85 static qint64 nowMs();
86
88 std::atomic<bool> m_running{ false };
89 std::atomic<qint64> m_lastTickMs{ 0 };
90 qint64 m_previousTickMs = 0;
91
92 mutable std::mutex m_mutex;
93 std::condition_variable m_wake;
94 bool m_stopping = false;
95 bool m_armed = false;
97 qint64 m_armedUntilMs = 0;
99 struct Samples
100 {
101 QJsonArray samples;
102 QJsonArray modules;
103 qint64 lastSampleMs = 0;
104 int attempts = 0;
105 };
106 std::map<qint64, Samples> m_stalls;
107 std::thread m_thread;
108};
109
110} // namespace Sonde::Detail
Samples the stack of the thread it is started on, from a thread of its own, while that thread is stal...
Definition stacksampler_p.h:28
const Settings m_defaults
Without arming.
Definition stacksampler_p.h:87
static constexpr std::chrono::milliseconds kTick
The interval of the watched thread's ticks.
Definition stacksampler_p.h:39
static qint64 nowMs()
Milliseconds on a steady clock.
Definition stacksampler.cpp:52
qint64 m_armedUntilMs
When an arming without a stall runs out.
Definition stacksampler_p.h:97
qint64 m_previousTickMs
The tick before the last (watched thread only).
Definition stacksampler_p.h:90
QString start()
Starts sampling the calling thread. Returns why it can't, or an empty string.
Definition stacksampler.cpp:59
std::thread m_thread
The sampler thread.
Definition stacksampler_p.h:107
void run()
The sampler thread.
Definition stacksampler.cpp:154
std::atomic< bool > m_running
Between start() and stop().
Definition stacksampler_p.h:88
StackSampler(Settings settings)
Samples with settings by default (the threshold is the stall source's).
Definition stacksampler.cpp:45
std::mutex m_mutex
Guards everything below.
Definition stacksampler_p.h:92
bool m_armed
arm() is in effect.
Definition stacksampler_p.h:95
std::map< qint64, Samples > m_stalls
At most the current stall and one ended.
Definition stacksampler_p.h:106
Settings m_armedSettings
The settings while armed.
Definition stacksampler_p.h:96
std::chrono::milliseconds effectiveThreshold() const
The threshold in effect: lower while armed with a lower one.
Definition stacksampler.cpp:103
Settings settingsAt(qint64 nowMs)
The settings in effect at nowMs (an arming that ran out is dropped).
Definition stacksampler.cpp:96
bool m_stopping
stop() was called.
Definition stacksampler_p.h:94
Stall takeEndedStall()
The samples taken since the tick before the last one, which is when the stall began; ends an arming t...
Definition stacksampler.cpp:122
void stop()
Stops sampling; returns once the sampler thread has ended.
Definition stacksampler.cpp:77
std::condition_variable m_wake
Wakes the sampler thread: stop, or armed.
Definition stacksampler_p.h:93
void tick()
The watched thread's tick: it is responsive now.
Definition stacksampler.cpp:90
std::atomic< qint64 > m_lastTickMs
When the watched thread last ticked.
Definition stacksampler_p.h:89
void arm(Settings settings, std::chrono::milliseconds duration)
Samples the next stall, or the stalls of the next duration, with settings instead of the defaults.
Definition stacksampler.cpp:111
bool isRunning() const
Whether it is sampling.
Definition stacksampler_p.h:53
Stall currentStall() const
The samples of the stall in progress, if the watched thread is stalled now.
Definition stacksampler.cpp:141
One stall's samples, by the tick it began after.
Definition stacksampler_p.h:100
QJsonArray modules
As in Stall.
Definition stacksampler_p.h:102
int attempts
Samples tried, including any that failed.
Definition stacksampler_p.h:104
qint64 lastSampleMs
When the last was taken.
Definition stacksampler_p.h:103
QJsonArray samples
As in Stall.
Definition stacksampler_p.h:101
How samples are taken.
Definition stacksampler_p.h:32
std::chrono::milliseconds threshold
Stalled this long: take the first sample.
Definition stacksampler_p.h:33
int maxSamples
At most this many per stall.
Definition stacksampler_p.h:35
std::chrono::milliseconds interval
Then one every interval.
Definition stacksampler_p.h:34
The samples of the stall that just ended, taken by the watched thread when it reports the stall.
Definition stacksampler_p.h:61
QJsonArray samples
Each: "atMs" (into the stall) and "frames".
Definition stacksampler_p.h:62
bool isEmpty() const
Whether no sample was taken.
Definition stacksampler_p.h:65
QJsonArray modules
The modules the frames are in.
Definition stacksampler_p.h:63