microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
task.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2// SPDX-License-Identifier: BSD-2-Clause
3
4#pragma once
5
6#include "../formatters/json.hpp"
7#include "../markdown.hpp"
8#include "address_space.hpp"
9#include "elf_enumerator.hpp"
10#include "metadata_map.hpp"
11#include "thread.hpp"
12
13namespace microfmt {
14
15enum class task_state : uint8_t { ready, running, suspended, waiting, terminated };
16
17} // namespace microfmt
18
19namespace microfmt {
20template <> struct formatter<task_state> {
21 constexpr void parse(format_parse_context &) noexcept {}
22
23 void format(task_state state, const sink &out) const noexcept {
24 switch (state) {
26 out.write("READY");
27 break;
29 out.write("RUNNING");
30 break;
32 out.write("SUSPENDED");
33 break;
35 out.write("WAITING");
36 break;
38 out.write("TERMINATED");
39 break;
40 }
41 }
42};
43} // namespace microfmt
44
45namespace microfmt {
46
54using thread_next_fn_t = bool (*)(void *ctx, thread_info &out) noexcept;
55
59struct MICROFMT_API_CLASS RELOCO_POINTER task_info {
60 // Classic Task Data
61 uint64_t team_id{0}; // PID (Team ID)
62 string_view name{};
63 task_state state{task_state::ready};
64 uint8_t priority{0};
65 uint32_t cpu_ticks{0};
66 uint32_t memory_usage{0};
67
68 // Memory Context Handle
70
71 // ELF Image Enumerator Handle (Modules, EXIDX, and debug frames)
72 elf_image_enumerator_ref elf_enumerator{};
73
74 // Task Extended Metadata Callback Hook
75 metadata_next_fn_t extended_metadata_fn{nullptr};
76 void *extended_metadata_ctx{nullptr};
77
78 // Abstract Thread Generator Hook
79 thread_next_fn_t thread_next_fn{nullptr};
80 void *thread_next_ctx{nullptr};
81
85 struct RELOCO_POINTER metadata_state {
86 const task_info *task{nullptr};
87 int step{0};
88 bool in_extended{false};
89 };
90
94 [[nodiscard]] constexpr metadata_map
95 make_metadata_view(metadata_state &the_state) const & noexcept RELOCO_LIFETIMEBOUND {
96 the_state.task = this;
97 the_state.step = 0;
98 the_state.in_extended = false;
99
100 return metadata_map(&the_state, [](void *ctx, property_entry &out) noexcept -> bool {
101 auto *s = static_cast<metadata_state *>(ctx);
102 if (s == nullptr || s->task == nullptr)
103 return false;
104
105 // Phase 1: Stream classic task properties
106 if (!s->in_extended) {
107 switch (s->step++) {
108 case 0:
109 out.key = "team_id";
110 out.val_ptr = &s->task->team_id;
111 out.print_fn = [](const void *p, const sink &s_out) noexcept {
112 detail::format_unsigned<detail::radix::decimal>(s_out, *static_cast<const uint64_t *>(p), false);
113 };
114 return true;
115 case 1:
116 out.key = "name";
117 out.val_ptr = &s->task->name;
118 out.print_fn = [](const void *p, const sink &s_out) noexcept {
119 json::write_escaped_string(s_out, *static_cast<const string_view *>(p));
120 };
121 return true;
122 case 2:
123 out.key = "state";
124 out.val_ptr = &s->task->state;
125 out.print_fn = [](const void *p, const sink &s_out) noexcept {
127 format_parse_context parse_context({});
128 fmt.parse(parse_context);
129 fmt.format(*static_cast<const task_state *>(p), s_out);
130 };
131 return true;
132 case 3:
133 out.key = "priority";
134 out.val_ptr = &s->task->priority;
135 out.print_fn = [](const void *p, const sink &s_out) noexcept {
136 detail::format_unsigned<detail::radix::decimal>(s_out, *static_cast<const uint8_t *>(p), false);
137 };
138 return true;
139 case 4:
140 out.key = "cpu_ticks";
141 out.val_ptr = &s->task->cpu_ticks;
142 out.print_fn = [](const void *p, const sink &s_out) noexcept {
143 detail::format_unsigned<detail::radix::decimal>(s_out, *static_cast<const uint32_t *>(p), false);
144 };
145 return true;
146 case 5:
147 out.key = "memory_usage";
148 out.val_ptr = &s->task->memory_usage;
149 out.print_fn = [](const void *p, const sink &s_out) noexcept {
150 detail::format_unsigned<detail::radix::decimal>(s_out, *static_cast<const uint32_t *>(p), false);
151 };
152 return true;
153 default:
154 s->in_extended = true;
155 break; // Transition to extended metadata
156 }
157 }
158
159 // Phase 2: Forward to task extended metadata callback
160 if (s->in_extended) {
161 if (s->task->extended_metadata_fn != nullptr && s->task->extended_metadata_ctx != nullptr) {
162 return s->task->extended_metadata_fn(s->task->extended_metadata_ctx, out);
163 }
164 }
165
166 return false;
167 });
168 }
169
171
175 [[nodiscard]] constexpr bool get_next_thread(thread_info &out_thread) const noexcept {
176 if (thread_next_fn != nullptr && thread_next_ctx != nullptr) {
177 return thread_next_fn(thread_next_ctx, out_thread);
178 }
179 return false;
180 }
181};
182
183} // namespace microfmt
Type-erased remote address-space access and remote string/object views.
Type-erased, two-word handle to a remote address space.
Definition address_space.hpp:204
Type-erased, two-word handle to an ELF image enumerator.
Definition elf_enumerator.hpp:104
Definition microfmt.hpp:881
Type-erased, non-owning metadata map view driven by a sequential next callback.
Definition metadata_map.hpp:56
#define MICROFMT_API_CLASS
Definition compat.hpp:181
ELF image introspection, type-erased image enumeration, and fixed-capacity registries.
Type-erased, zero-allocation metadata map generator view.
Definition fmt.hpp:17
Definition assert.hpp:19
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
bool(*)(void *ctx, thread_info &out) noexcept thread_next_fn_t
Function pointer signature for generating/iterating threads belonging to a task.
Definition task.hpp:54
bool(*)(void *ctx, property_entry &out) noexcept metadata_next_fn_t
Function pointer signature for fetching the next property entry.
Definition metadata_map.hpp:48
task_state
Definition task.hpp:15
constexpr void parse(format_parse_context &) noexcept
Definition task.hpp:21
void format(task_state state, const sink &out) const noexcept
Definition task.hpp:23
Definition microfmt.hpp:985
Type-erased property entry synthesized during generation.
Definition metadata_map.hpp:24
property_print_fn_t print_fn
Definition metadata_map.hpp:27
microfmt::string_view key
Definition metadata_map.hpp:25
value_ptr< const void > val_ptr
Definition metadata_map.hpp:26
Stack iteration state supporting phase transition from classic fields to extended task metadata.
Definition task.hpp:85
Comprehensive system task (Team) descriptor.
Definition task.hpp:59
constexpr bool get_next_thread(thread_info &out_thread) const noexcept
Fetches the next thread belonging to this task using the abstract thread generator.
Definition task.hpp:175
metadata_map make_metadata_view(metadata_state &) const &&=delete
constexpr metadata_map make_metadata_view(metadata_state &the_state) const &noexcept RELOCO_LIFETIMEBOUND
Exposes a unified metadata_map view for classic task data and extended metadata.
Definition task.hpp:95
Thread descriptor containing thread-specific attributes and CPU register context access.
Definition thread.hpp:58
Thread descriptor with telemetry and register context integration.