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

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

#include <job_info.h>

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

Public Member Functions

auto total_latency () const -> std::chrono::nanoseconds
 Calculates total latency (wait + execution time).
 
auto is_finished () const -> bool
 Checks if the job has finished execution.
 
auto is_active () const -> bool
 Checks if the job is still active (pending or running).
 
auto wait_time_ms () const -> double
 Converts wait_time to milliseconds.
 
auto execution_time_ms () const -> double
 Converts execution_time to milliseconds.
 
auto to_json () const -> std::string
 Converts the job info to a JSON string.
 
auto to_string () const -> std::string
 Converts the job info to a human-readable string.
 

Public Attributes

std::uint64_t job_id {0}
 Unique identifier for this job.
 
std::string job_name
 Human-readable name or description of the job.
 
std::chrono::steady_clock::time_point enqueue_time
 Time when the job was added to the queue.
 
std::chrono::steady_clock::time_point start_time
 Time when the job started executing.
 
std::optional< std::chrono::steady_clock::time_point > end_time
 Time when the job finished (completed, failed, or cancelled).
 
std::chrono::nanoseconds wait_time {0}
 Time spent waiting in the queue before execution.
 
std::chrono::nanoseconds execution_time {0}
 Time spent executing the job.
 
job_status status {job_status::pending}
 Current status of the job.
 
std::optional< std::string > error_message
 Error message if the job failed.
 
std::thread::id executed_by
 ID of the thread that executed/is executing the job.
 
std::optional< std::string > stack_trace
 Stack trace captured when the job failed.
 

Detailed Description

Information about a job in the thread pool.

Contains comprehensive information about a job including its identity, timing information, execution status, and error details.

Timing Diagram

| | |
v v v
[=======wait_time=========][====execution_time==========]
|<----- pending ---------->|<-------- running --------->|
@ running
Job is currently being executed.
@ pending
Job is waiting in the queue.
std::chrono::nanoseconds wait_time
Time spent waiting in the queue before execution.
Definition job_info.h:133
std::optional< std::chrono::steady_clock::time_point > end_time
Time when the job finished (completed, failed, or cancelled).
Definition job_info.h:125
std::chrono::nanoseconds execution_time
Time spent executing the job.
Definition job_info.h:141
std::chrono::steady_clock::time_point start_time
Time when the job started executing.
Definition job_info.h:118
std::chrono::steady_clock::time_point enqueue_time
Time when the job was added to the queue.
Definition job_info.h:110

Usage Example

auto info = diagnostics.get_active_jobs()[0];
if (info.status == job_status::running) {
auto elapsed = std::chrono::steady_clock::now() - info.start_time;
LOG_INFO("Job {} running for {}ms", info.job_name,
std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count());
}
@ info
Informational messages highlighting progress.

Definition at line 89 of file job_info.h.

Member Function Documentation

◆ execution_time_ms()

auto kcenon::thread::diagnostics::job_info::execution_time_ms ( ) const -> double
inlinenodiscard

Converts execution_time to milliseconds.

Returns
Execution time in milliseconds.

Definition at line 214 of file job_info.h.

215 {
216 return std::chrono::duration<double, std::milli>(execution_time).count();
217 }

References execution_time.

Referenced by to_json(), and to_string().

Here is the caller graph for this function:

◆ is_active()

auto kcenon::thread::diagnostics::job_info::is_active ( ) const -> bool
inlinenodiscard

Checks if the job is still active (pending or running).

Returns
true if pending or running.

Definition at line 195 of file job_info.h.

196 {
197 return status == job_status::pending ||
199 }
job_status status
Current status of the job.
Definition job_info.h:146

References kcenon::thread::diagnostics::pending, kcenon::thread::diagnostics::running, and status.

◆ is_finished()

auto kcenon::thread::diagnostics::job_info::is_finished ( ) const -> bool
inlinenodiscard

Checks if the job has finished execution.

Returns
true if completed, failed, cancelled, or timed_out.

Definition at line 183 of file job_info.h.

184 {
185 return status == job_status::completed ||
189 }
@ failed
Job failed with an error.
@ cancelled
Job was cancelled before completion.
@ timed_out
Job exceeded its timeout limit.
@ completed
Job completed successfully.

References kcenon::thread::diagnostics::cancelled, kcenon::thread::diagnostics::completed, kcenon::thread::diagnostics::failed, status, and kcenon::thread::diagnostics::timed_out.

◆ to_json()

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

Converts the job info to a JSON string.

Returns
JSON representation of the job info.

Output format:

{
"job_id": 123,
"job_name": "ProcessOrder",
"status": "running",
"wait_time_ms": 1.5,
"execution_time_ms": 10.2,
"total_latency_ms": 11.7,
"thread_id": "12345",
"error_message": null
}

Definition at line 237 of file job_info.h.

238 {
239 std::ostringstream oss;
240 oss << "{\n";
241 oss << " \"job_id\": " << job_id << ",\n";
242 oss << " \"job_name\": \"" << job_name << "\",\n";
243 oss << " \"status\": \"" << job_status_to_string(status) << "\",\n";
244 oss << std::fixed << std::setprecision(3);
245 oss << " \"wait_time_ms\": " << wait_time_ms() << ",\n";
246 oss << " \"execution_time_ms\": " << execution_time_ms() << ",\n";
247 oss << " \"total_latency_ms\": "
248 << std::chrono::duration<double, std::milli>(total_latency()).count() << ",\n";
249
250 // Format thread_id as string
251 std::ostringstream tid_oss;
252 tid_oss << executed_by;
253 oss << " \"thread_id\": \"" << tid_oss.str() << "\"";
254
255 if (error_message.has_value())
256 {
257 oss << ",\n \"error_message\": \"" << error_message.value() << "\"";
258 }
259 else
260 {
261 oss << ",\n \"error_message\": null";
262 }
263
264 if (stack_trace.has_value())
265 {
266 oss << ",\n \"stack_trace\": \"" << stack_trace.value() << "\"";
267 }
268
269 oss << "\n}";
270 return oss.str();
271 }
auto job_status_to_string(job_status status) -> std::string
Converts job_status to human-readable string.
Definition job_info.h:47
std::thread::id executed_by
ID of the thread that executed/is executing the job.
Definition job_info.h:160
auto wait_time_ms() const -> double
Converts wait_time to milliseconds.
Definition job_info.h:205
std::optional< std::string > stack_trace
Stack trace captured when the job failed.
Definition job_info.h:168
auto total_latency() const -> std::chrono::nanoseconds
Calculates total latency (wait + execution time).
Definition job_info.h:174
std::uint64_t job_id
Unique identifier for this job.
Definition job_info.h:97
std::string job_name
Human-readable name or description of the job.
Definition job_info.h:105
auto execution_time_ms() const -> double
Converts execution_time to milliseconds.
Definition job_info.h:214
std::optional< std::string > error_message
Error message if the job failed.
Definition job_info.h:153

References error_message, executed_by, execution_time_ms(), job_name, kcenon::thread::diagnostics::job_status_to_string(), stack_trace, status, total_latency(), and wait_time_ms().

Here is the call graph for this function:

◆ to_string()

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

Converts the job info to a human-readable string.

Returns
String representation of the job info.

Output format:

Job#123 "ProcessOrder" [running]
Wait: 1.500ms, Exec: 10.200ms, Total: 11.700ms
Thread: 12345

Definition at line 284 of file job_info.h.

285 {
286 std::ostringstream oss;
287
288 // First line: job id, name, status
289 oss << "Job#" << job_id << " \"" << job_name << "\" ["
290 << job_status_to_string(status) << "]\n";
291
292 // Second line: timing info
293 oss << std::fixed << std::setprecision(3);
294 oss << " Wait: " << wait_time_ms() << "ms, ";
295 oss << "Exec: " << execution_time_ms() << "ms, ";
296 oss << "Total: "
297 << std::chrono::duration<double, std::milli>(total_latency()).count() << "ms\n";
298
299 // Third line: thread info
300 oss << " Thread: " << executed_by;
301
302 // Error info if present
303 if (error_message.has_value())
304 {
305 oss << "\n Error: " << error_message.value();
306 }
307
308 return oss.str();
309 }

References error_message, executed_by, execution_time_ms(), job_name, kcenon::thread::diagnostics::job_status_to_string(), status, total_latency(), and wait_time_ms().

Here is the call graph for this function:

◆ total_latency()

auto kcenon::thread::diagnostics::job_info::total_latency ( ) const -> std::chrono::nanoseconds
inlinenodiscard

Calculates total latency (wait + execution time).

Returns
Total time from enqueue to completion.

Definition at line 174 of file job_info.h.

175 {
176 return wait_time + execution_time;
177 }

References execution_time, and wait_time.

Referenced by to_json(), and to_string().

Here is the caller graph for this function:

◆ wait_time_ms()

auto kcenon::thread::diagnostics::job_info::wait_time_ms ( ) const -> double
inlinenodiscard

Converts wait_time to milliseconds.

Returns
Wait time in milliseconds.

Definition at line 205 of file job_info.h.

206 {
207 return std::chrono::duration<double, std::milli>(wait_time).count();
208 }

References wait_time.

Referenced by to_json(), and to_string().

Here is the caller graph for this function:

Member Data Documentation

◆ end_time

std::optional<std::chrono::steady_clock::time_point> kcenon::thread::diagnostics::job_info::end_time

Time when the job finished (completed, failed, or cancelled).

Only has a value if the job has finished execution.

Definition at line 125 of file job_info.h.

◆ enqueue_time

std::chrono::steady_clock::time_point kcenon::thread::diagnostics::job_info::enqueue_time

Time when the job was added to the queue.

Definition at line 110 of file job_info.h.

◆ error_message

std::optional<std::string> kcenon::thread::diagnostics::job_info::error_message

Error message if the job failed.

Only has a value if status == failed or status == timed_out.

Definition at line 153 of file job_info.h.

Referenced by to_json(), and to_string().

◆ executed_by

std::thread::id kcenon::thread::diagnostics::job_info::executed_by

ID of the thread that executed/is executing the job.

Only valid if status >= running.

Definition at line 160 of file job_info.h.

Referenced by to_json(), and to_string().

◆ execution_time

std::chrono::nanoseconds kcenon::thread::diagnostics::job_info::execution_time {0}

Time spent executing the job.

Calculated as: end_time - start_time. For running jobs, this is the current execution time.

Definition at line 141 of file job_info.h.

141{0};

Referenced by execution_time_ms(), and total_latency().

◆ job_id

std::uint64_t kcenon::thread::diagnostics::job_info::job_id {0}

Unique identifier for this job.

Generated when the job is created, unique within the lifetime of the thread pool.

Definition at line 97 of file job_info.h.

97{0};

Referenced by kcenon::thread::thread_worker::get_current_job_info(), and kcenon::thread::job_queue::inspect_pending_jobs().

◆ job_name

std::string kcenon::thread::diagnostics::job_info::job_name

Human-readable name or description of the job.

May be empty if the job was not named. Used for logging and debugging purposes.

Definition at line 105 of file job_info.h.

Referenced by to_json(), and to_string().

◆ stack_trace

std::optional<std::string> kcenon::thread::diagnostics::job_info::stack_trace

Stack trace captured when the job failed.

Only has a value if status == failed and stack trace capture was enabled.

Definition at line 168 of file job_info.h.

Referenced by to_json().

◆ start_time

std::chrono::steady_clock::time_point kcenon::thread::diagnostics::job_info::start_time

Time when the job started executing.

Only valid if status >= running. Will be the same as enqueue_time for pending jobs until they start.

Definition at line 118 of file job_info.h.

◆ status

job_status kcenon::thread::diagnostics::job_info::status {job_status::pending}

Current status of the job.

Definition at line 146 of file job_info.h.

Referenced by is_active(), is_finished(), to_json(), and to_string().

◆ wait_time

std::chrono::nanoseconds kcenon::thread::diagnostics::job_info::wait_time {0}

Time spent waiting in the queue before execution.

Calculated as: start_time - enqueue_time. For pending jobs, this is the current wait time.

Definition at line 133 of file job_info.h.

133{0};

Referenced by total_latency(), and wait_time_ms().


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