• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

llnl / dftracer-utils / 29787659139

20 Jul 2026 11:34PM UTC coverage: 51.578% (-1.1%) from 52.66%
29787659139

Pull #99

github

web-flow
Merge ee805adeb into 06bc84ec9
Pull Request #99: Support CM time_metric (NS/MS/SEC/US) across reader and viz

34832 of 86278 branches covered (40.37%)

Branch coverage included in aggregate %.

1034 of 1319 new or added lines in 30 files covered. (78.39%)

5193 existing lines in 197 files now uncovered.

35349 of 49791 relevant lines covered (70.99%)

9765.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.62
/src/dftracer/utils/core/runtime.cpp
1
#include <dftracer/utils/core/common/error.h>
2
#include <dftracer/utils/core/common/logging.h>
3
#include <dftracer/utils/core/common/platform_compat.h>
4
#include <dftracer/utils/core/env.h>
5
#include <dftracer/utils/core/runtime.h>
6

7
#include <algorithm>
8
#include <cstdlib>
9
#include <stdexcept>
10
#include <string>
11
#include <string_view>
12
#include <thread>
13

14
namespace dftracer::utils {
15

16
namespace {
17
// Resolve the worker-thread count. DFTRACER_UTILS_THREADS overrides everything
18
// (a debugging lever: force a fixed count, e.g. 1 for a single-threaded async
19
// loop). Otherwise 0 means hardware_concurrency.
20
std::size_t resolve_threads(std::size_t requested) {
463✔
21
    if (auto env = Env::get<std::string_view>("DFTRACER_UTILS_THREADS");
926!
22
        env.has_value()) {
463✔
UNCOV
23
        const long long n =
×
24
            std::strtoll(std::string(*env).c_str(), nullptr, 10);
×
25
        if (n > 0) return static_cast<std::size_t>(n);
×
UNCOV
26
    }
×
27
    return requested == 0 ? dftracer_utils_hardware_concurrency() : requested;
463✔
28
}
463✔
29
}  // namespace
30

31
Runtime::Runtime(std::size_t threads) : threads_(resolve_threads(threads)) {
292!
32
    ExecutorConfig config;
146!
33
    config.num_threads = threads_;
146✔
34
    executor_ = std::make_unique<Executor>(config);
146!
35
    executor_->start();
146!
36

37
    watchdog_ = std::make_unique<Watchdog>();
146!
38
    watchdog_->set_executor(executor_.get());
146!
39
}
292✔
40

41
Runtime::Runtime(const ExecutorConfig& config, bool enable_watchdog)
156✔
42
    : threads_(resolve_threads(config.num_threads)) {
156!
43
    ExecutorConfig cfg = config;
78✔
44
    cfg.num_threads = threads_;
78✔
45
    executor_ = std::make_unique<Executor>(cfg);
78!
46
    executor_->start();
78!
47

48
    if (enable_watchdog) {
78!
49
        watchdog_ = std::make_unique<Watchdog>();
78!
50
        watchdog_->set_executor(executor_.get());
78!
51
    }
78✔
52
}
156✔
53

54
Runtime::Runtime(const ExecutorConfig& config,
478✔
55
                 std::unique_ptr<Watchdog> watchdog)
56
    : threads_(resolve_threads(config.num_threads)) {
478!
57
    ExecutorConfig cfg = config;
239✔
58
    cfg.num_threads = threads_;
239✔
59
    executor_ = std::make_unique<Executor>(cfg);
239!
60
    executor_->start();
239!
61

62
    watchdog_ = std::move(watchdog);
239✔
63
    if (watchdog_) {
239✔
64
        watchdog_->set_executor(executor_.get());
81!
65
    }
81✔
66
}
478✔
67

68
Runtime::~Runtime() { shutdown(); }
926!
69

70
TaskHandle Runtime::submit(coro::CoroTask<void> task, std::string name) {
533✔
71
    if (shutdown_called_.load(std::memory_order_acquire)) {
533✔
72
        throw DFTUtilsException(ErrorCode::PIPELINE, "Runtime is shut down");
2!
73
    }
74
    if (name.empty()) {
531✔
75
        name = "task-" + std::to_string(task_name_counter_++);
3!
76
    }
3✔
77

78
    auto promise = std::make_shared<std::promise<void>>();
531✔
79
    auto future = promise->get_future().share();
531!
80
    auto tid = std::make_shared<std::atomic<TaskIndex>>(-1);
531!
81

82
    auto wrapper =
531✔
83
        [](coro::CoroTask<void> t, std::shared_ptr<std::promise<void>> p,
3,711!
84
           Executor* exec,
85
           std::shared_ptr<std::atomic<TaskIndex>> task_id) -> coro::Coro {
531!
86
        try {
87
            co_await std::move(t);
2,649!
88
            t = coro::CoroTask<void>{
1,056!
89
                std::coroutine_handle<coro::CoroTask<void>::promise_type>{}};
528✔
90
            exec->mark_coro_completed(task_id->load(std::memory_order_acquire));
528!
91
        } catch (...) {
531✔
92
            t = coro::CoroTask<void>{
6!
93
                std::coroutine_handle<coro::CoroTask<void>::promise_type>{}};
3✔
94
            exec->mark_coro_completed(task_id->load(std::memory_order_acquire));
3!
95
            p->set_exception(std::current_exception());
3!
96
            co_return;
3✔
97
        }
3!
98
        p->set_value();
528!
99
    };
1,059✔
100

101
    // Set the executor on the task's promise so awaitables (e.g. channels)
102
    // that capture `get_root_promise()->get_executor()` can schedule
103
    // resumption. Without this, awaiters end up with executor=nullptr because
104
    // the wrapping `coro::Coro` doesn't extend PromiseBase and the
105
    // root-promise chain stops at the user's CoroTask.
106
    if (task.handle()) {
531!
107
        task.handle().promise().set_executor(executor_.get());
531!
108
    }
531✔
109
    auto coro = wrapper(std::move(task), promise, executor_.get(), tid);
531!
110
    TaskIndex id = executor_->enqueue_tracked(std::move(coro), name, tid);
531!
111

112
    {
113
        std::lock_guard<std::mutex> lock(futures_mutex_);
531!
114
        cleanup_completed_futures();
531!
115
        outstanding_futures_.push_back(future);
531!
116
    }
531✔
117

118
    return TaskHandle{future, id, std::move(name)};
531!
119
}
533✔
120

121
void Runtime::wait_all() {
100✔
122
    std::vector<std::shared_future<void>> futures;
100✔
123
    {
124
        std::lock_guard<std::mutex> lock(futures_mutex_);
100!
125
        futures = std::move(outstanding_futures_);
100✔
126
        outstanding_futures_.clear();
100✔
127
    }
100✔
128
    for (auto& f : futures) {
137✔
129
        f.wait();
37!
130
    }
131
}
100✔
132

133
void Runtime::cleanup_completed_futures() {
617✔
134
    outstanding_futures_.erase(
1,234✔
135
        std::remove_if(outstanding_futures_.begin(), outstanding_futures_.end(),
617✔
136
                       [](const std::shared_future<void>& f) {
451✔
137
                           return f.wait_for(std::chrono::seconds(0)) ==
451✔
138
                                  std::future_status::ready;
139
                       }),
140
        outstanding_futures_.end());
617✔
141
}
617✔
142

143
ExecutorProgress Runtime::get_progress() const {
19✔
144
    return executor_->get_progress();
19✔
145
}
146

147
bool Runtime::is_responsive() const { return executor_->is_responsive(); }
2✔
148

149
void Runtime::set_global_timeout(std::chrono::milliseconds timeout) {
×
150
    if (!watchdog_) {
×
151
        throw DFTUtilsException(
×
152
            ErrorCode::PIPELINE,
153
            "Cannot set timeout: Runtime created without watchdog");
×
154
    }
155
    watchdog_->set_global_timeout(timeout);
×
156
}
×
157

158
void Runtime::set_default_task_timeout(std::chrono::milliseconds timeout) {
×
159
    if (!watchdog_) {
×
160
        throw DFTUtilsException(
×
161
            ErrorCode::PIPELINE,
162
            "Cannot set timeout: Runtime created without watchdog");
×
163
    }
164
    watchdog_->set_default_task_timeout(timeout);
×
165
}
×
166

167
void Runtime::shutdown() {
877✔
168
    bool expected = false;
877✔
169
    if (!shutdown_called_.compare_exchange_strong(expected, true)) return;
877✔
170
    if (watchdog_) watchdog_->stop();
463✔
171
    if (executor_) executor_->shutdown();
463!
172
}
877✔
173

174
std::size_t Runtime::threads() const { return threads_; }
167✔
175

176
std::size_t Runtime::io_threads() const {
×
177
    return executor_ ? executor_->get_io_pool_size() : 0;
×
178
}
179

180
}  // namespace dftracer::utils
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc