|
2 | 2 |
|
3 | 3 | #include <cstdint> |
4 | 4 | #include <optional> |
| 5 | +#include <random> |
5 | 6 | #include <unordered_map> |
6 | 7 | #include <unordered_set> |
7 | 8 |
|
|
10 | 11 | #include <echion/strings.h> |
11 | 12 | #include <echion/threads.h> |
12 | 13 |
|
| 14 | +#include "constants.hpp" |
13 | 15 | #include "stack_renderer.hpp" |
14 | 16 |
|
15 | 17 | // Forward declaration |
@@ -53,6 +55,13 @@ class EchionSampler |
53 | 55 | // Only accessed from the sampling thread, so no lock/atomic is needed. |
54 | 56 | size_t asyncio_task_count_ = 0; |
55 | 57 |
|
| 58 | + // Maximum number of leaf tasks / greenlets to unwind and emit per cycle. |
| 59 | + // 0 means unlimited. |
| 60 | + unsigned int max_tasks_per_sample_ = g_default_max_tasks_per_sample; |
| 61 | + |
| 62 | + // RNG used for task / greenlet reservoir sampling. |
| 63 | + std::minstd_rand rng_{ std::random_device{}() }; |
| 64 | + |
56 | 65 | // Caches |
57 | 66 | StringTable string_table_; |
58 | 67 | LRUCache<uintptr_t, Frame> frame_cache_; |
@@ -100,6 +109,11 @@ class EchionSampler |
100 | 109 | void add_asyncio_task_count(size_t count) { asyncio_task_count_ += count; } |
101 | 110 | size_t asyncio_task_count() const { return asyncio_task_count_; } |
102 | 111 |
|
| 112 | + unsigned int max_tasks_per_sample() const { return max_tasks_per_sample_; } |
| 113 | + void set_max_tasks_per_sample(unsigned int value) { max_tasks_per_sample_ = value; } |
| 114 | + |
| 115 | + std::minstd_rand& rng() { return rng_; } |
| 116 | + |
103 | 117 | // Accessor for StringTable operations |
104 | 118 | StringTable& string_table() { return string_table_; } |
105 | 119 | const StringTable& string_table() const { return string_table_; } |
@@ -138,6 +152,7 @@ class EchionSampler |
138 | 152 | asyncio_frame_cache_key_.reset(); |
139 | 153 | uvloop_frame_cache_key_.reset(); |
140 | 154 | asyncio_task_count_ = 0; |
| 155 | + rng_ = std::minstd_rand{ std::random_device{}() }; |
141 | 156 |
|
142 | 157 | new (&seen_frames_scratch_) std::unordered_set<PyObject*>(); |
143 | 158 |
|
|
0 commit comments