Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::work_stealing_stats Struct Reference

Statistics for work-stealing operations. More...

#include <work_stealing_stats.h>

Collaboration diagram for kcenon::thread::work_stealing_stats:
Collaboration graph

Public Member Functions

auto steal_success_rate () const -> double
 Calculate the steal success rate.
 
auto avg_batch_size () const -> double
 Calculate the average batch size.
 
auto cross_node_ratio () const -> double
 Calculate the cross-NUMA node steal ratio.
 
auto avg_steal_time_ns () const -> double
 Calculate average steal operation time.
 
void reset ()
 Reset all statistics to zero.
 
auto snapshot () const -> work_stealing_stats_snapshot
 Create a snapshot of current statistics.
 

Public Attributes

std::atomic< std::uint64_t > steal_attempts {0}
 Total number of steal attempts.
 
std::atomic< std::uint64_t > successful_steals {0}
 Number of successful steal operations.
 
std::atomic< std::uint64_t > failed_steals {0}
 Number of failed steal operations.
 
std::atomic< std::uint64_t > jobs_stolen {0}
 Total number of jobs successfully stolen.
 
std::atomic< std::uint64_t > same_node_steals {0}
 Steals from workers on the same NUMA node.
 
std::atomic< std::uint64_t > cross_node_steals {0}
 Steals from workers on different NUMA nodes.
 
std::atomic< std::uint64_t > batch_steals {0}
 Number of batch steal operations (stealing multiple jobs)
 
std::atomic< std::uint64_t > total_batch_size {0}
 Total size of all batch steals (for averaging)
 
std::atomic< std::uint64_t > total_steal_time_ns {0}
 Total time spent in steal operations (nanoseconds)
 
std::atomic< std::uint64_t > total_backoff_time_ns {0}
 Total time spent in backoff delays (nanoseconds)
 

Detailed Description

Statistics for work-stealing operations.

This structure tracks various metrics about work-stealing performance, including success rates, NUMA locality, batch efficiency, and timing. All counters are atomic for thread-safe updates from multiple workers.

Thread Safety

All members use atomic operations for lock-free concurrent updates. Statistics may show momentary inconsistencies during rapid updates but will eventually converge to accurate totals.

Memory Ordering

  • Counter updates: relaxed ordering (eventual consistency acceptable)
  • Snapshot reads: acquire ordering for consistent view

Usage Example

// After stealing operations
stats.steal_attempts.fetch_add(1, std::memory_order_relaxed);
stats.successful_steals.fetch_add(1, std::memory_order_relaxed);
stats.jobs_stolen.fetch_add(batch_size, std::memory_order_relaxed);
// Query statistics
double success_rate = stats.steal_success_rate();
double avg_batch = stats.avg_batch_size();
double cross_numa = stats.cross_node_ratio();
Statistics for work-stealing operations.
auto avg_batch_size() const -> double
Calculate the average batch size.
std::atomic< std::uint64_t > jobs_stolen
Total number of jobs successfully stolen.
std::atomic< std::uint64_t > successful_steals
Number of successful steal operations.
std::atomic< std::uint64_t > steal_attempts
Total number of steal attempts.
auto cross_node_ratio() const -> double
Calculate the cross-NUMA node steal ratio.
auto steal_success_rate() const -> double
Calculate the steal success rate.

Definition at line 110 of file work_stealing_stats.h.

Member Function Documentation

◆ avg_batch_size()

auto kcenon::thread::work_stealing_stats::avg_batch_size ( ) const -> double
inlinenodiscard

Calculate the average batch size.

Returns
Average number of jobs per batch steal

Returns 0.0 if no batch steals have been made.

Definition at line 185 of file work_stealing_stats.h.

186 {
187 auto batches = batch_steals.load(std::memory_order_relaxed);
188 if (batches == 0)
189 {
190 return 0.0;
191 }
192 auto total = total_batch_size.load(std::memory_order_relaxed);
193 return static_cast<double>(total) / static_cast<double>(batches);
194 }
std::atomic< std::uint64_t > total_batch_size
Total size of all batch steals (for averaging)
std::atomic< std::uint64_t > batch_steals
Number of batch steal operations (stealing multiple jobs)

References batch_steals, and total_batch_size.

◆ avg_steal_time_ns()

auto kcenon::thread::work_stealing_stats::avg_steal_time_ns ( ) const -> double
inlinenodiscard

Calculate average steal operation time.

Returns
Average time per steal attempt in nanoseconds

Returns 0.0 if no steal attempts have been made.

Definition at line 221 of file work_stealing_stats.h.

222 {
223 auto attempts = steal_attempts.load(std::memory_order_relaxed);
224 if (attempts == 0)
225 {
226 return 0.0;
227 }
228 auto total_time = total_steal_time_ns.load(std::memory_order_relaxed);
229 return static_cast<double>(total_time) / static_cast<double>(attempts);
230 }
std::atomic< std::uint64_t > total_steal_time_ns
Total time spent in steal operations (nanoseconds)

References steal_attempts, and total_steal_time_ns.

◆ cross_node_ratio()

auto kcenon::thread::work_stealing_stats::cross_node_ratio ( ) const -> double
inlinenodiscard

Calculate the cross-NUMA node steal ratio.

Returns
Ratio of cross-node steals to total steals (0.0 to 1.0)

Returns 0.0 if no successful steals have been made. Lower values indicate better NUMA locality.

Definition at line 203 of file work_stealing_stats.h.

204 {
205 auto same = same_node_steals.load(std::memory_order_relaxed);
206 auto cross = cross_node_steals.load(std::memory_order_relaxed);
207 auto total = same + cross;
208 if (total == 0)
209 {
210 return 0.0;
211 }
212 return static_cast<double>(cross) / static_cast<double>(total);
213 }
std::atomic< std::uint64_t > cross_node_steals
Steals from workers on different NUMA nodes.
std::atomic< std::uint64_t > same_node_steals
Steals from workers on the same NUMA node.

References cross_node_steals, and same_node_steals.

◆ reset()

void kcenon::thread::work_stealing_stats::reset ( )
inline

Reset all statistics to zero.

Thread Safety: This operation is not atomic across all counters. During reset, some counters may be zero while others retain old values. For accurate snapshots during operation, avoid calling reset() while steal operations are in progress.

Definition at line 241 of file work_stealing_stats.h.

242 {
243 steal_attempts.store(0, std::memory_order_relaxed);
244 successful_steals.store(0, std::memory_order_relaxed);
245 failed_steals.store(0, std::memory_order_relaxed);
246 jobs_stolen.store(0, std::memory_order_relaxed);
247 same_node_steals.store(0, std::memory_order_relaxed);
248 cross_node_steals.store(0, std::memory_order_relaxed);
249 batch_steals.store(0, std::memory_order_relaxed);
250 total_batch_size.store(0, std::memory_order_relaxed);
251 total_steal_time_ns.store(0, std::memory_order_relaxed);
252 total_backoff_time_ns.store(0, std::memory_order_relaxed);
253 }
std::atomic< std::uint64_t > total_backoff_time_ns
Total time spent in backoff delays (nanoseconds)
std::atomic< std::uint64_t > failed_steals
Number of failed steal operations.

References batch_steals, cross_node_steals, failed_steals, jobs_stolen, same_node_steals, steal_attempts, successful_steals, total_backoff_time_ns, total_batch_size, and total_steal_time_ns.

Referenced by kcenon::thread::numa_work_stealer::reset_stats().

Here is the caller graph for this function:

◆ snapshot()

auto kcenon::thread::work_stealing_stats::snapshot ( ) const -> work_stealing_stats_snapshot
inlinenodiscard

Create a snapshot of current statistics.

Returns
A non-atomic copy of the current statistics

This creates a non-atomic snapshot that can be safely read without worrying about concurrent updates.

Definition at line 262 of file work_stealing_stats.h.

263 {
264 work_stealing_stats_snapshot snap;
265 snap.steal_attempts = steal_attempts.load(std::memory_order_acquire);
266 snap.successful_steals = successful_steals.load(std::memory_order_acquire);
267 snap.failed_steals = failed_steals.load(std::memory_order_acquire);
268 snap.jobs_stolen = jobs_stolen.load(std::memory_order_acquire);
269 snap.same_node_steals = same_node_steals.load(std::memory_order_acquire);
270 snap.cross_node_steals = cross_node_steals.load(std::memory_order_acquire);
271 snap.batch_steals = batch_steals.load(std::memory_order_acquire);
272 snap.total_batch_size = total_batch_size.load(std::memory_order_acquire);
273 snap.total_steal_time_ns = total_steal_time_ns.load(std::memory_order_acquire);
274 snap.total_backoff_time_ns = total_backoff_time_ns.load(std::memory_order_acquire);
275 return snap;
276 }

References batch_steals, kcenon::thread::work_stealing_stats_snapshot::batch_steals, cross_node_steals, kcenon::thread::work_stealing_stats_snapshot::cross_node_steals, failed_steals, kcenon::thread::work_stealing_stats_snapshot::failed_steals, jobs_stolen, kcenon::thread::work_stealing_stats_snapshot::jobs_stolen, same_node_steals, kcenon::thread::work_stealing_stats_snapshot::same_node_steals, steal_attempts, kcenon::thread::work_stealing_stats_snapshot::steal_attempts, successful_steals, kcenon::thread::work_stealing_stats_snapshot::successful_steals, total_backoff_time_ns, kcenon::thread::work_stealing_stats_snapshot::total_backoff_time_ns, total_batch_size, kcenon::thread::work_stealing_stats_snapshot::total_batch_size, total_steal_time_ns, and kcenon::thread::work_stealing_stats_snapshot::total_steal_time_ns.

Referenced by kcenon::thread::numa_work_stealer::get_stats_snapshot().

Here is the caller graph for this function:

◆ steal_success_rate()

auto kcenon::thread::work_stealing_stats::steal_success_rate ( ) const -> double
inlinenodiscard

Calculate the steal success rate.

Returns
Success rate as a ratio (0.0 to 1.0)

Returns 0.0 if no steal attempts have been made.

Definition at line 168 of file work_stealing_stats.h.

169 {
170 auto attempts = steal_attempts.load(std::memory_order_relaxed);
171 if (attempts == 0)
172 {
173 return 0.0;
174 }
175 auto successes = successful_steals.load(std::memory_order_relaxed);
176 return static_cast<double>(successes) / static_cast<double>(attempts);
177 }

References steal_attempts, and successful_steals.

Member Data Documentation

◆ batch_steals

std::atomic<std::uint64_t> kcenon::thread::work_stealing_stats::batch_steals {0}

Number of batch steal operations (stealing multiple jobs)

Definition at line 143 of file work_stealing_stats.h.

143{0};

Referenced by avg_batch_size(), reset(), and snapshot().

◆ cross_node_steals

std::atomic<std::uint64_t> kcenon::thread::work_stealing_stats::cross_node_steals {0}

Steals from workers on different NUMA nodes.

Definition at line 136 of file work_stealing_stats.h.

136{0};

Referenced by cross_node_ratio(), reset(), and snapshot().

◆ failed_steals

std::atomic<std::uint64_t> kcenon::thread::work_stealing_stats::failed_steals {0}

Number of failed steal operations.

Definition at line 123 of file work_stealing_stats.h.

123{0};

Referenced by reset(), and snapshot().

◆ jobs_stolen

std::atomic<std::uint64_t> kcenon::thread::work_stealing_stats::jobs_stolen {0}

Total number of jobs successfully stolen.

Definition at line 126 of file work_stealing_stats.h.

126{0};

Referenced by reset(), and snapshot().

◆ same_node_steals

std::atomic<std::uint64_t> kcenon::thread::work_stealing_stats::same_node_steals {0}

Steals from workers on the same NUMA node.

Definition at line 133 of file work_stealing_stats.h.

133{0};

Referenced by cross_node_ratio(), reset(), and snapshot().

◆ steal_attempts

std::atomic<std::uint64_t> kcenon::thread::work_stealing_stats::steal_attempts {0}

Total number of steal attempts.

Definition at line 117 of file work_stealing_stats.h.

117{0};

Referenced by avg_steal_time_ns(), reset(), snapshot(), and steal_success_rate().

◆ successful_steals

std::atomic<std::uint64_t> kcenon::thread::work_stealing_stats::successful_steals {0}

Number of successful steal operations.

Definition at line 120 of file work_stealing_stats.h.

120{0};

Referenced by reset(), snapshot(), and steal_success_rate().

◆ total_backoff_time_ns

std::atomic<std::uint64_t> kcenon::thread::work_stealing_stats::total_backoff_time_ns {0}

Total time spent in backoff delays (nanoseconds)

Definition at line 156 of file work_stealing_stats.h.

156{0};

Referenced by reset(), and snapshot().

◆ total_batch_size

std::atomic<std::uint64_t> kcenon::thread::work_stealing_stats::total_batch_size {0}

Total size of all batch steals (for averaging)

Definition at line 146 of file work_stealing_stats.h.

146{0};

Referenced by avg_batch_size(), reset(), and snapshot().

◆ total_steal_time_ns

std::atomic<std::uint64_t> kcenon::thread::work_stealing_stats::total_steal_time_ns {0}

Total time spent in steal operations (nanoseconds)

Definition at line 153 of file work_stealing_stats.h.

153{0};

Referenced by avg_steal_time_ns(), reset(), and snapshot().


The documentation for this struct was generated from the following file: