Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
job_info.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
14#include <chrono>
15#include <cstdint>
16#include <iomanip>
17#include <optional>
18#include <sstream>
19#include <string>
20#include <thread>
21
23{
32 enum class job_status
33 {
34 pending,
35 running,
36 completed,
37 failed,
38 cancelled,
40 };
41
47 [[nodiscard]] inline auto job_status_to_string(job_status status) -> std::string
48 {
49 switch (status)
50 {
51 case job_status::pending: return "pending";
52 case job_status::running: return "running";
53 case job_status::completed: return "completed";
54 case job_status::failed: return "failed";
55 case job_status::cancelled: return "cancelled";
56 case job_status::timed_out: return "timed_out";
57 default: return "unknown";
58 }
59 }
60
89 struct job_info
90 {
97 std::uint64_t job_id{0};
98
105 std::string job_name;
106
110 std::chrono::steady_clock::time_point enqueue_time;
111
118 std::chrono::steady_clock::time_point start_time;
119
125 std::optional<std::chrono::steady_clock::time_point> end_time;
126
133 std::chrono::nanoseconds wait_time{0};
134
141 std::chrono::nanoseconds execution_time{0};
142
147
153 std::optional<std::string> error_message;
154
160 std::thread::id executed_by;
161
168 std::optional<std::string> stack_trace;
169
174 [[nodiscard]] auto total_latency() const -> std::chrono::nanoseconds
175 {
176 return wait_time + execution_time;
177 }
178
183 [[nodiscard]] auto is_finished() const -> bool
184 {
185 return status == job_status::completed ||
189 }
190
195 [[nodiscard]] auto is_active() const -> bool
196 {
197 return status == job_status::pending ||
199 }
200
205 [[nodiscard]] auto wait_time_ms() const -> double
206 {
207 return std::chrono::duration<double, std::milli>(wait_time).count();
208 }
209
214 [[nodiscard]] auto execution_time_ms() const -> double
215 {
216 return std::chrono::duration<double, std::milli>(execution_time).count();
217 }
218
237 [[nodiscard]] auto to_json() const -> std::string
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 }
272
284 [[nodiscard]] auto to_string() const -> std::string
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 }
310 };
311
312} // namespace kcenon::thread::diagnostics
job_status
Status of a job in the thread pool.
Definition job_info.h:33
@ failed
Job failed with an error.
@ cancelled
Job was cancelled before completion.
@ timed_out
Job exceeded its timeout limit.
@ running
Job is currently being executed.
@ pending
Job is waiting in the queue.
@ completed
Job completed successfully.
@ failed
Job failed with an error.
@ completed
Job completed successfully.
auto job_status_to_string(job_status status) -> std::string
Converts job_status to human-readable string.
Definition job_info.h:47
std::uint64_t job_id
Unique job identifier for DAG scheduler.
Definition dag_job.h:33
STL namespace.
Information about a job in the thread pool.
Definition job_info.h:90
std::chrono::nanoseconds wait_time
Time spent waiting in the queue before execution.
Definition job_info.h:133
auto to_json() const -> std::string
Converts the job info to a JSON string.
Definition job_info.h:237
std::thread::id executed_by
ID of the thread that executed/is executing the job.
Definition job_info.h:160
std::optional< std::chrono::steady_clock::time_point > end_time
Time when the job finished (completed, failed, or cancelled).
Definition job_info.h:125
auto wait_time_ms() const -> double
Converts wait_time to milliseconds.
Definition job_info.h:205
auto is_finished() const -> bool
Checks if the job has finished execution.
Definition job_info.h:183
job_status status
Current status of the job.
Definition job_info.h:146
std::optional< std::string > stack_trace
Stack trace captured when the job failed.
Definition job_info.h:168
auto is_active() const -> bool
Checks if the job is still active (pending or running).
Definition job_info.h:195
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
auto total_latency() const -> std::chrono::nanoseconds
Calculates total latency (wait + execution time).
Definition job_info.h:174
std::string job_name
Human-readable name or description of the job.
Definition job_info.h:105
auto to_string() const -> std::string
Converts the job info to a human-readable string.
Definition job_info.h:284
auto execution_time_ms() const -> double
Converts execution_time to milliseconds.
Definition job_info.h:214
std::chrono::steady_clock::time_point enqueue_time
Time when the job was added to the queue.
Definition job_info.h:110
std::optional< std::string > error_message
Error message if the job failed.
Definition job_info.h:153