pipeline/PollingThread.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // <PollingThread.hpp> -*- C++ -*- | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include "simdb/Assert.hpp" | ||
| 6 | #include "simdb/pipeline/Runnable.hpp" | ||
| 7 | #include "simdb/utils/StreamFormatters.hpp" | ||
| 8 | |||
| 9 | #include <atomic> | ||
| 10 | #include <chrono> | ||
| 11 | #include <condition_variable> | ||
| 12 | #include <future> | ||
| 13 | #include <iomanip> | ||
| 14 | #include <iostream> | ||
| 15 | #include <mutex> | ||
| 16 | #include <thread> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | namespace simdb::pipeline { | ||
| 20 | |||
| 21 | /*! | ||
| 22 | * \class PollingThread | ||
| 23 | * | ||
| 24 | * \brief Thread that repeatedly polls its Runnables for work; when none do | ||
| 25 | * work, it sleeps for a fixed interval before polling again. Supports | ||
| 26 | * pause/resume and performance reporting. Base for DatabaseThread. | ||
| 27 | */ | ||
| 28 | class PollingThread | ||
| 29 | { | ||
| 30 | public: | ||
| 31 | /// \brief Create a thread with the given sleep interval when no work is done. | ||
| 32 | /// \param interval_milliseconds How long to sleep when no Runnable had work (default 100ms). | ||
| 33 | 29 | PollingThread(const size_t interval_milliseconds = 100) : | |
| 34 | 29 | interval_ms_(interval_milliseconds) | |
| 35 | { | ||
| 36 | 29 | } | |
| 37 | |||
| 38 | 49 | virtual ~PollingThread() noexcept { close(); } | |
| 39 | |||
| 40 | /// \brief Return the sleep interval in milliseconds (when no work is done). | ||
| 41 | ✗ | size_t getIntervalMilliseconds() const { return interval_ms_; } | |
| 42 | |||
| 43 | /// \brief Add a Runnable to this thread; must not be called while the thread is running. | ||
| 44 | /// \throws DBException if called while the thread is running. | ||
| 45 | 32 | void addRunnable(Runnable* runnable) | |
| 46 | { | ||
| 47 | 32 | simdb_assert(!is_running_, "Cannot add runnables while thread is running"); | |
| 48 | 32 | runnables_.emplace_back(runnable); | |
| 49 | 32 | runnable->thread_ = this; | |
| 50 | 32 | } | |
| 51 | |||
| 52 | /// \brief Return the Runnables on this thread. | ||
| 53 | 31744 | const std::vector<Runnable*>& getRunnables() const { return runnables_; } | |
| 54 | |||
| 55 | /// \brief Return the number of Runnables on this thread. | ||
| 56 | size_t getNumRunnables() const { return runnables_.size(); } | ||
| 57 | |||
| 58 | /// \brief Reorder this thread's Runnables to match the order in \p runnables (only those | ||
| 59 | /// that belong to this thread). | ||
| 60 | 29 | void ensureRelativeOrder(const std::vector<Runnable*>& runnables) | |
| 61 | { | ||
| 62 | 29 | const std::set<Runnable*> my_runnables(runnables_.begin(), runnables_.end()); | |
| 63 | 29 | std::vector<Runnable*> ordered_runnables; | |
| 64 | 143 | for (auto runnable : runnables) | |
| 65 | { | ||
| 66 | 114 | if (my_runnables.count(runnable)) | |
| 67 | { | ||
| 68 | 32 | ordered_runnables.push_back(runnable); | |
| 69 | } | ||
| 70 | } | ||
| 71 | 29 | std::swap(ordered_runnables, runnables_); | |
| 72 | 29 | } | |
| 73 | |||
| 74 | /// \brief Call processAll(true) on all enabled Runnables; return true if any did work. | ||
| 75 | 49 | virtual bool flushRunnables() | |
| 76 | { | ||
| 77 | 49 | bool did_work = false; | |
| 78 | 101 | for (auto runnable : runnables_) | |
| 79 | { | ||
| 80 | 52 | if (!runnable->enabled()) | |
| 81 | { | ||
| 82 | ✗ | continue; | |
| 83 | } | ||
| 84 | |||
| 85 | 52 | if (runnable->processAll(true) == PipelineAction::PROCEED) | |
| 86 | { | ||
| 87 | 11 | did_work = true; | |
| 88 | } | ||
| 89 | } | ||
| 90 | 49 | return did_work; | |
| 91 | } | ||
| 92 | |||
| 93 | /// \brief Start the polling thread (must have at least one Runnable). | ||
| 94 | 29 | virtual void open() | |
| 95 | { | ||
| 96 | 29 | if (runnables_.empty()) | |
| 97 | { | ||
| 98 | ✗ | return; | |
| 99 | } | ||
| 100 | |||
| 101 | 29 | if (!thread_) | |
| 102 | { | ||
| 103 | 29 | stop_requested_ = false; | |
| 104 | 29 | paused_ = false; | |
| 105 | 29 | is_running_ = true; | |
| 106 | 29 | start_ = std::chrono::high_resolution_clock::now(); | |
| 107 | 29 | thread_ = std::make_unique<std::thread>(&PollingThread::loop_, this); | |
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | /// \brief Stop the thread and join. | ||
| 112 | /// \note Meant to be called from the main thread. | ||
| 113 | 58 | virtual void close() noexcept | |
| 114 | { | ||
| 115 | 58 | if (!thread_) | |
| 116 | { | ||
| 117 | 29 | return; | |
| 118 | } | ||
| 119 | |||
| 120 | { | ||
| 121 | 29 | std::lock_guard<std::mutex> lock(pause_mutex_); | |
| 122 | 29 | stop_requested_ = true; | |
| 123 | 29 | paused_ = false; // Unpause in case it was paused | |
| 124 | 29 | } | |
| 125 | 29 | pause_cv_.notify_all(); | |
| 126 | |||
| 127 | 29 | if (thread_->joinable()) | |
| 128 | { | ||
| 129 | 29 | thread_->join(); | |
| 130 | } | ||
| 131 | 29 | is_running_ = false; | |
| 132 | 29 | thread_.reset(); | |
| 133 | } | ||
| 134 | |||
| 135 | /// \brief Pause the polling loop; blocks until the thread has acknowledged it is paused. | ||
| 136 | 39999 | void pause() | |
| 137 | { | ||
| 138 | 39999 | if (!is_running_ || paused_) | |
| 139 | { | ||
| 140 | ✗ | return; | |
| 141 | } | ||
| 142 | |||
| 143 | { | ||
| 144 | 39999 | std::lock_guard<std::mutex> lock(pause_mutex_); | |
| 145 | 39999 | paused_ = true; | |
| 146 | |||
| 147 | // Reset and prepare for acknowledgment | ||
| 148 | 39999 | paused_promise_ = std::promise<void>(); | |
| 149 | 39999 | has_pending_pause_ack_ = true; | |
| 150 | 39999 | } | |
| 151 | |||
| 152 | 39999 | pause_cv_.notify_all(); // Wake the thread so it can acknowledge | |
| 153 | |||
| 154 | // Wait for thread to acknowledge it is paused | ||
| 155 | 39999 | paused_promise_.get_future().wait(); | |
| 156 | } | ||
| 157 | |||
| 158 | /// \brief Return true if the thread is currently paused. | ||
| 159 | 39999 | bool paused() { return paused_; } | |
| 160 | |||
| 161 | /// \brief Resume the polling loop after a pause. | ||
| 162 | 39999 | void resume() | |
| 163 | { | ||
| 164 | 39999 | if (!is_running_ || !paused_) | |
| 165 | { | ||
| 166 | ✗ | return; | |
| 167 | } | ||
| 168 | |||
| 169 | { | ||
| 170 | 39999 | std::lock_guard<std::mutex> lock(pause_mutex_); | |
| 171 | 39999 | paused_ = false; | |
| 172 | 39999 | } | |
| 173 | |||
| 174 | 39999 | pause_cv_.notify_all(); | |
| 175 | } | ||
| 176 | |||
| 177 | /// \brief Print a performance report (sleep vs work %) for this thread. | ||
| 178 | 29 | virtual bool printPerfReport() const noexcept | |
| 179 | { | ||
| 180 | 29 | if (runnables_.empty()) | |
| 181 | { | ||
| 182 | ✗ | return false; | |
| 183 | } | ||
| 184 | |||
| 185 | 29 | if (is_running_) | |
| 186 | { | ||
| 187 | ✗ | return false; | |
| 188 | } | ||
| 189 | |||
| 190 | 29 | const auto pct_time_sleeping = getSleepPct(); | |
| 191 | 29 | const auto pct_time_working = 100 - pct_time_sleeping; | |
| 192 | |||
| 193 | 29 | std::cout << "Thread containing:\n"; | |
| 194 | 61 | for (const auto runnable : runnables_) | |
| 195 | { | ||
| 196 | 32 | runnable->print(std::cout, 4); | |
| 197 | } | ||
| 198 | |||
| 199 | 29 | [[maybe_unused]] ios_format_saver fmt_saver(std::cout); | |
| 200 | 29 | std::cout << "\n"; | |
| 201 | 29 | std::cout << " Performance report:\n"; | |
| 202 | 29 | std::cout << " Pct time sleeping: " << std::fixed << std::setprecision(1) << pct_time_sleeping << "%\n"; | |
| 203 | 29 | std::cout << " Pct time working: " << std::fixed << std::setprecision(1) << pct_time_working << "%\n"; | |
| 204 | |||
| 205 | 29 | return true; | |
| 206 | 29 | } | |
| 207 | |||
| 208 | 25717 | double getSleepPct() const | |
| 209 | { | ||
| 210 | 25717 | auto now = std::chrono::high_resolution_clock::now(); | |
| 211 | 25717 | const std::chrono::duration<double> dur = now - start_; | |
| 212 | 25717 | const auto total_elap_seconds = dur.count(); | |
| 213 | 25717 | const auto pct_time_sleeping = (total_sleep_seconds_ / total_elap_seconds) * 100; | |
| 214 | 25717 | return pct_time_sleeping; | |
| 215 | } | ||
| 216 | |||
| 217 | private: | ||
| 218 | 29 | void loop_() | |
| 219 | { | ||
| 220 | 40778 | while (!stop_requested_) | |
| 221 | { | ||
| 222 | // Pause handling | ||
| 223 | { | ||
| 224 | 40720 | std::unique_lock<std::mutex> lock(pause_mutex_); | |
| 225 | 80719 | while (paused_ && !stop_requested_) | |
| 226 | { | ||
| 227 | 39999 | if (has_pending_pause_ack_) | |
| 228 | { | ||
| 229 | 39999 | paused_promise_.set_value(); | |
| 230 | 39999 | has_pending_pause_ack_ = false; | |
| 231 | } | ||
| 232 | 39999 | pause_cv_.wait(lock); | |
| 233 | } | ||
| 234 | |||
| 235 | 40720 | if (stop_requested_) | |
| 236 | { | ||
| 237 | ✗ | break; | |
| 238 | } | ||
| 239 | 40720 | } | |
| 240 | |||
| 241 | 40720 | if (!run_(false)) | |
| 242 | { | ||
| 243 | // Sleep for a fixed amount of time before polling all runnables | ||
| 244 | // again but wake early if paused or stop is requested | ||
| 245 | 39552 | auto sleep_start = std::chrono::high_resolution_clock::now(); | |
| 246 | 39552 | std::unique_lock<std::mutex> lock(pause_mutex_); | |
| 247 | 39552 | pause_cv_.wait_for(lock, std::chrono::milliseconds(interval_ms_), | |
| 248 | 78219 | [this] { return paused_ || stop_requested_; }); | |
| 249 | |||
| 250 | 39552 | auto sleep_end = std::chrono::high_resolution_clock::now(); | |
| 251 | 39552 | auto duration_us = std::chrono::duration_cast<std::chrono::microseconds>(sleep_end - sleep_start); | |
| 252 | 39552 | total_sleep_seconds_ += duration_us.count() / 1'000'000.0; | |
| 253 | 39552 | } | |
| 254 | } | ||
| 255 | |||
| 256 | // Flush | ||
| 257 | 34 | while (run_(true)) | |
| 258 | { | ||
| 259 | } | ||
| 260 | 29 | } | |
| 261 | |||
| 262 | 29639 | virtual bool run_(bool force) | |
| 263 | { | ||
| 264 | 29639 | bool did_work = false; | |
| 265 | while (true) | ||
| 266 | { | ||
| 267 | 54373 | bool processed = false; | |
| 268 | 108746 | for (auto runner : runnables_) | |
| 269 | { | ||
| 270 | 54373 | if (!runner->enabled()) | |
| 271 | { | ||
| 272 | 13023 | continue; | |
| 273 | } | ||
| 274 | |||
| 275 | 41350 | if (runner->processOne(force) == PipelineAction::PROCEED) | |
| 276 | { | ||
| 277 | 24734 | processed = true; | |
| 278 | } | ||
| 279 | } | ||
| 280 | 54373 | if (!processed) | |
| 281 | { | ||
| 282 | 29639 | break; | |
| 283 | } | ||
| 284 | 24734 | did_work = true; | |
| 285 | 24734 | } | |
| 286 | 29639 | return did_work; | |
| 287 | } | ||
| 288 | |||
| 289 | const size_t interval_ms_; | ||
| 290 | std::vector<Runnable*> runnables_; | ||
| 291 | std::unique_ptr<std::thread> thread_; | ||
| 292 | |||
| 293 | std::mutex pause_mutex_; | ||
| 294 | std::condition_variable pause_cv_; | ||
| 295 | |||
| 296 | std::atomic<bool> is_running_ = false; | ||
| 297 | std::atomic<bool> paused_ = false; | ||
| 298 | std::atomic<bool> stop_requested_ = false; | ||
| 299 | |||
| 300 | std::promise<void> paused_promise_; | ||
| 301 | bool has_pending_pause_ack_ = false; | ||
| 302 | |||
| 303 | std::chrono::high_resolution_clock::time_point start_; | ||
| 304 | double total_sleep_seconds_ = 0; | ||
| 305 | }; | ||
| 306 | |||
| 307 | /// Defined here so we can avoid circular includes | ||
| 308 | 10000 | inline ScopedRunnableDisabler::ScopedRunnableDisabler(PipelineManager* pipeline_mgr, | |
| 309 | const std::vector<Runnable*>& runnables, | ||
| 310 | 10000 | const std::vector<PollingThread*>& polling_threads) : | |
| 311 | 10000 | pipeline_mgr_(pipeline_mgr) | |
| 312 | { | ||
| 313 | // Disable runnables | ||
| 314 | 49999 | for (auto r : runnables) | |
| 315 | { | ||
| 316 | 39999 | if (r->enabled()) | |
| 317 | { | ||
| 318 | 39999 | r->enable(false); | |
| 319 | 39999 | disabled_runnables_.push_back(r); | |
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | // Pause polling threads | ||
| 324 | 49999 | for (auto pt : polling_threads) | |
| 325 | { | ||
| 326 | 39999 | if (!pt->paused()) | |
| 327 | { | ||
| 328 | 39999 | pt->pause(); | |
| 329 | 39999 | paused_threads_.push_back(pt); | |
| 330 | } | ||
| 331 | } | ||
| 332 | 10000 | } | |
| 333 | |||
| 334 | /// Defined here so we can avoid circular includes | ||
| 335 | ✗ | inline ScopedRunnableDisabler::ScopedRunnableDisabler(PipelineManager* pipeline_mgr, | |
| 336 | ✗ | const std::vector<Runnable*>& runnables) : | |
| 337 | ✗ | pipeline_mgr_(pipeline_mgr) | |
| 338 | { | ||
| 339 | // Disable runnables | ||
| 340 | ✗ | for (auto r : runnables) | |
| 341 | { | ||
| 342 | ✗ | if (r->enabled()) | |
| 343 | { | ||
| 344 | ✗ | r->enable(false); | |
| 345 | ✗ | disabled_runnables_.push_back(r); | |
| 346 | } | ||
| 347 | } | ||
| 348 | ✗ | } | |
| 349 | |||
| 350 | /// Defined here so we can avoid circular includes | ||
| 351 | 10000 | inline ScopedRunnableDisabler::~ScopedRunnableDisabler() | |
| 352 | { | ||
| 353 | 49999 | for (auto pt : paused_threads_) | |
| 354 | { | ||
| 355 | 39999 | pt->resume(); | |
| 356 | } | ||
| 357 | |||
| 358 | 49999 | for (auto r : disabled_runnables_) | |
| 359 | { | ||
| 360 | 39999 | r->enable(true); | |
| 361 | } | ||
| 362 | |||
| 363 | 10000 | notifyPipelineMgrReenabled_(); | |
| 364 | 10000 | } | |
| 365 | |||
| 366 | } // namespace simdb::pipeline | ||
| 367 |