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

Information about a worker thread in the pool. More...

#include <thread_info.h>

Collaboration diagram for kcenon::thread::diagnostics::thread_info:
Collaboration graph

Public Member Functions

auto state_duration () const -> std::chrono::nanoseconds
 Calculates the duration in the current state.
 
auto total_jobs () const -> std::uint64_t
 Gets the total number of jobs processed (completed + failed).
 
auto success_rate () const -> double
 Calculates the success rate.
 
auto is_busy () const -> bool
 Checks if the worker is currently processing a job.
 
auto is_available () const -> bool
 Checks if the worker is available to process jobs.
 
auto update_utilization () -> void
 Recalculates utilization based on busy and idle times.
 
auto busy_time_ms () const -> double
 Converts busy time to milliseconds.
 
auto idle_time_ms () const -> double
 Converts idle time to milliseconds.
 
auto to_json () const -> std::string
 Converts the thread info to a JSON string.
 
auto to_string () const -> std::string
 Converts the thread info to a human-readable string.
 

Public Attributes

std::thread::id thread_id
 System thread ID.
 
std::string thread_name
 Human-readable name for this thread.
 
std::size_t worker_id {0}
 Worker ID within the pool.
 
worker_state state {worker_state::idle}
 Current operational state of the worker.
 
std::chrono::steady_clock::time_point state_since
 Time when the worker entered its current state.
 
std::optional< job_infocurrent_job
 Information about the currently executing job.
 
std::uint64_t jobs_completed {0}
 Total number of jobs successfully completed by this worker.
 
std::uint64_t jobs_failed {0}
 Total number of jobs that failed during execution.
 
std::chrono::nanoseconds total_busy_time {0}
 Total time spent executing jobs (busy time).
 
std::chrono::nanoseconds total_idle_time {0}
 Total time spent waiting for jobs (idle time).
 
double utilization {0.0}
 Worker utilization ratio.
 

Detailed Description

Information about a worker thread in the pool.

Contains comprehensive information about a worker thread including its identity, current state, statistics, and optionally the job it is currently processing.

Example Output

Worker-0 [tid:12345] ACTIVE (2.5s)
Current Job: ProcessOrder#1234 (running 150ms)
Jobs: 1523 completed, 2 failed
Utilization: 87.3%
@ running
Job is currently being executed.
@ failed
Job failed with an error.
@ completed
Job completed successfully.

Usage Example

auto threads = diagnostics.dump_thread_states();
for (const auto& t : threads) {
LOG_INFO("Worker {} ({}): {} jobs done, {:.1f}% utilization",
t.thread_name, worker_state_to_string(t.state),
t.jobs_completed, t.utilization * 100.0);
}
auto worker_state_to_string(worker_state state) -> std::string
Converts worker_state to human-readable string.
Definition thread_info.h:47

Definition at line 87 of file thread_info.h.

Member Function Documentation

◆ busy_time_ms()

auto kcenon::thread::diagnostics::thread_info::busy_time_ms ( ) const -> double
inlinenodiscard

Converts busy time to milliseconds.

Returns
Busy time in milliseconds.

Definition at line 239 of file thread_info.h.

240 {
241 return std::chrono::duration<double, std::milli>(total_busy_time).count();
242 }
std::chrono::nanoseconds total_busy_time
Total time spent executing jobs (busy time).

References total_busy_time.

Referenced by to_json().

Here is the caller graph for this function:

◆ idle_time_ms()

auto kcenon::thread::diagnostics::thread_info::idle_time_ms ( ) const -> double
inlinenodiscard

Converts idle time to milliseconds.

Returns
Idle time in milliseconds.

Definition at line 248 of file thread_info.h.

249 {
250 return std::chrono::duration<double, std::milli>(total_idle_time).count();
251 }
std::chrono::nanoseconds total_idle_time
Total time spent waiting for jobs (idle time).

References total_idle_time.

Referenced by to_json().

Here is the caller graph for this function:

◆ is_available()

auto kcenon::thread::diagnostics::thread_info::is_available ( ) const -> bool
inlinenodiscard

Checks if the worker is available to process jobs.

Returns
true if state == idle.

Definition at line 210 of file thread_info.h.

211 {
212 return state == worker_state::idle;
213 }
@ idle
Worker is waiting for jobs.
worker_state state
Current operational state of the worker.

References kcenon::thread::diagnostics::idle, and state.

◆ is_busy()

auto kcenon::thread::diagnostics::thread_info::is_busy ( ) const -> bool
inlinenodiscard

Checks if the worker is currently processing a job.

Returns
true if state == active.

Definition at line 201 of file thread_info.h.

202 {
203 return state == worker_state::active;
204 }
@ active
Worker is executing a job.

References kcenon::thread::diagnostics::active, and state.

◆ state_duration()

auto kcenon::thread::diagnostics::thread_info::state_duration ( ) const -> std::chrono::nanoseconds
inlinenodiscard

Calculates the duration in the current state.

Returns
Time since entering the current state.

Definition at line 169 of file thread_info.h.

170 {
171 return std::chrono::steady_clock::now() - state_since;
172 }
std::chrono::steady_clock::time_point state_since
Time when the worker entered its current state.

References state_since.

Referenced by to_json(), and to_string().

Here is the caller graph for this function:

◆ success_rate()

auto kcenon::thread::diagnostics::thread_info::success_rate ( ) const -> double
inlinenodiscard

Calculates the success rate.

Returns
Success ratio (0.0 to 1.0), or 1.0 if no jobs processed.

Definition at line 187 of file thread_info.h.

188 {
189 auto total = total_jobs();
190 if (total == 0)
191 {
192 return 1.0;
193 }
194 return static_cast<double>(jobs_completed) / static_cast<double>(total);
195 }
std::uint64_t jobs_completed
Total number of jobs successfully completed by this worker.
auto total_jobs() const -> std::uint64_t
Gets the total number of jobs processed (completed + failed).

References jobs_completed, and total_jobs().

Referenced by to_json(), and to_string().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_json()

auto kcenon::thread::diagnostics::thread_info::to_json ( ) const -> std::string
inlinenodiscard

Converts the thread info to a JSON string.

Returns
JSON representation of the thread info.

Output format:

{
"worker_id": 0,
"thread_name": "Worker-0",
"thread_id": "12345",
"state": "ACTIVE",
"state_duration_ms": 2500.0,
"jobs_completed": 1523,
"jobs_failed": 2,
"success_rate": 0.9987,
"utilization": 0.873,
"busy_time_ms": 87300.0,
"idle_time_ms": 12700.0,
"current_job": null
}

Definition at line 275 of file thread_info.h.

276 {
277 std::ostringstream oss;
278 oss << "{\n";
279 oss << " \"worker_id\": " << worker_id << ",\n";
280 oss << " \"thread_name\": \"" << thread_name << "\",\n";
281
282 // Format thread_id as string
283 std::ostringstream tid_oss;
284 tid_oss << thread_id;
285 oss << " \"thread_id\": \"" << tid_oss.str() << "\",\n";
286
287 oss << " \"state\": \"" << worker_state_to_string(state) << "\",\n";
288 oss << std::fixed << std::setprecision(3);
289 oss << " \"state_duration_ms\": "
290 << std::chrono::duration<double, std::milli>(state_duration()).count() << ",\n";
291 oss << " \"jobs_completed\": " << jobs_completed << ",\n";
292 oss << " \"jobs_failed\": " << jobs_failed << ",\n";
293 oss << std::setprecision(4);
294 oss << " \"success_rate\": " << success_rate() << ",\n";
295 oss << " \"utilization\": " << utilization << ",\n";
296 oss << std::setprecision(3);
297 oss << " \"busy_time_ms\": " << busy_time_ms() << ",\n";
298 oss << " \"idle_time_ms\": " << idle_time_ms();
299
300 if (current_job.has_value())
301 {
302 oss << ",\n \"current_job\": " << current_job.value().to_json();
303 }
304 else
305 {
306 oss << ",\n \"current_job\": null";
307 }
308
309 oss << "\n}";
310 return oss.str();
311 }
double utilization
Worker utilization ratio.
std::size_t worker_id
Worker ID within the pool.
std::thread::id thread_id
System thread ID.
Definition thread_info.h:94
auto success_rate() const -> double
Calculates the success rate.
auto idle_time_ms() const -> double
Converts idle time to milliseconds.
std::uint64_t jobs_failed
Total number of jobs that failed during execution.
std::string thread_name
Human-readable name for this thread.
auto busy_time_ms() const -> double
Converts busy time to milliseconds.
auto state_duration() const -> std::chrono::nanoseconds
Calculates the duration in the current state.
std::optional< job_info > current_job
Information about the currently executing job.

References busy_time_ms(), current_job, idle_time_ms(), jobs_completed, jobs_failed, state, state_duration(), success_rate(), thread_id, thread_name, utilization, worker_id, and kcenon::thread::diagnostics::worker_state_to_string().

Here is the call graph for this function:

◆ to_string()

auto kcenon::thread::diagnostics::thread_info::to_string ( ) const -> std::string
inlinenodiscard

Converts the thread info to a human-readable string.

Returns
String representation of the thread info.

Output format:

Worker-0 [tid:12345] ACTIVE (2.5s)
Current Job: ProcessOrder#1234 (running 150ms)
Jobs: 1523 completed, 2 failed (99.9% success)
Utilization: 87.3%

Definition at line 325 of file thread_info.h.

326 {
327 std::ostringstream oss;
328
329 auto duration_sec = std::chrono::duration<double>(state_duration()).count();
330
331 // First line: name, thread id, state, duration
332 oss << thread_name << " [tid:" << thread_id << "] "
334 << " (" << std::fixed << std::setprecision(1) << duration_sec << "s)\n";
335
336 // Current job if present
337 if (current_job.has_value())
338 {
339 const auto& job = current_job.value();
340 auto exec_time_ms_val = std::chrono::duration<double, std::milli>(
341 job.execution_time).count();
342 oss << " Current Job: " << job.job_name << "#" << job.job_id
343 << " (running " << std::setprecision(0) << exec_time_ms_val << "ms)\n";
344 }
345
346 // Job statistics
347 oss << " Jobs: " << jobs_completed << " completed, "
348 << jobs_failed << " failed ("
349 << std::setprecision(1) << (success_rate() * 100.0) << "% success)\n";
350
351 // Utilization
352 oss << " Utilization: " << std::setprecision(1)
353 << (utilization * 100.0) << "%";
354
355 return oss.str();
356 }

References current_job, jobs_completed, jobs_failed, state, state_duration(), success_rate(), thread_id, thread_name, utilization, and kcenon::thread::diagnostics::worker_state_to_string().

Here is the call graph for this function:

◆ total_jobs()

auto kcenon::thread::diagnostics::thread_info::total_jobs ( ) const -> std::uint64_t
inlinenodiscard

Gets the total number of jobs processed (completed + failed).

Returns
Total job count.

Definition at line 178 of file thread_info.h.

179 {
181 }

References jobs_completed, and jobs_failed.

Referenced by success_rate().

Here is the caller graph for this function:

◆ update_utilization()

auto kcenon::thread::diagnostics::thread_info::update_utilization ( ) -> void
inline

Recalculates utilization based on busy and idle times.

Updates the utilization field based on current total_busy_time and total_idle_time values.

Definition at line 221 of file thread_info.h.

222 {
223 auto total_time = total_busy_time + total_idle_time;
224 if (total_time.count() == 0)
225 {
226 utilization = 0.0;
227 }
228 else
229 {
230 utilization = static_cast<double>(total_busy_time.count()) /
231 static_cast<double>(total_time.count());
232 }
233 }

References total_busy_time, total_idle_time, and utilization.

Member Data Documentation

◆ current_job

std::optional<job_info> kcenon::thread::diagnostics::thread_info::current_job

Information about the currently executing job.

Only has a value if state == active.

Definition at line 127 of file thread_info.h.

Referenced by to_json(), and to_string().

◆ jobs_completed

std::uint64_t kcenon::thread::diagnostics::thread_info::jobs_completed {0}

Total number of jobs successfully completed by this worker.

Definition at line 136 of file thread_info.h.

136{0};

Referenced by success_rate(), to_json(), to_string(), and total_jobs().

◆ jobs_failed

std::uint64_t kcenon::thread::diagnostics::thread_info::jobs_failed {0}

Total number of jobs that failed during execution.

Definition at line 141 of file thread_info.h.

141{0};

Referenced by to_json(), to_string(), and total_jobs().

◆ state

worker_state kcenon::thread::diagnostics::thread_info::state {worker_state::idle}

Current operational state of the worker.

Definition at line 113 of file thread_info.h.

Referenced by is_available(), is_busy(), to_json(), and to_string().

◆ state_since

std::chrono::steady_clock::time_point kcenon::thread::diagnostics::thread_info::state_since

Time when the worker entered its current state.

Used to calculate how long the worker has been in its current state.

Definition at line 120 of file thread_info.h.

Referenced by state_duration().

◆ thread_id

std::thread::id kcenon::thread::diagnostics::thread_info::thread_id

System thread ID.

The native thread identifier from the operating system.

Definition at line 94 of file thread_info.h.

Referenced by to_json(), and to_string().

◆ thread_name

std::string kcenon::thread::diagnostics::thread_info::thread_name

Human-readable name for this thread.

Typically in the format "Worker-N" where N is the worker index.

Definition at line 101 of file thread_info.h.

Referenced by to_json(), and to_string().

◆ total_busy_time

std::chrono::nanoseconds kcenon::thread::diagnostics::thread_info::total_busy_time {0}

Total time spent executing jobs (busy time).

Definition at line 146 of file thread_info.h.

146{0};

Referenced by busy_time_ms(), and update_utilization().

◆ total_idle_time

std::chrono::nanoseconds kcenon::thread::diagnostics::thread_info::total_idle_time {0}

Total time spent waiting for jobs (idle time).

Definition at line 151 of file thread_info.h.

151{0};

Referenced by idle_time_ms(), and update_utilization().

◆ utilization

double kcenon::thread::diagnostics::thread_info::utilization {0.0}

Worker utilization ratio.

Calculated as: total_busy_time / (total_busy_time + total_idle_time). Value ranges from 0.0 (never busy) to 1.0 (always busy).

Definition at line 159 of file thread_info.h.

159{0.0};

Referenced by to_json(), to_string(), and update_utilization().

◆ worker_id

std::size_t kcenon::thread::diagnostics::thread_info::worker_id {0}

Worker ID within the pool.

Unique identifier for this worker, assigned by the pool.

Definition at line 108 of file thread_info.h.

108{0};

Referenced by kcenon::thread::diagnostics::thread_pool_diagnostics::get_worker_info(), and to_json().


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