microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
thread.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
9#include "../formatters/json.hpp"
10#include "../microfmt.hpp"
11#include "../reloco.hpp"
12#include "metadata_map.hpp"
13#include "register_context.hpp"
14
15namespace microfmt {
16
21
22} // namespace microfmt
23
24namespace microfmt {
25template <> struct formatter<thread_state> {
26 constexpr void parse(format_parse_context &) noexcept {}
27
28 void format(thread_state state, const sink &out) const noexcept {
29 switch (state) {
31 out.write("READY");
32 break;
34 out.write("RUNNING");
35 break;
37 out.write("SUSPENDED");
38 break;
40 out.write("WAITING");
41 break;
43 out.write("SLEEPING");
44 break;
46 out.write("TERMINATED");
47 break;
48 }
49 }
50};
51} // namespace microfmt
52
53namespace microfmt {
54
58struct MICROFMT_API_CLASS RELOCO_POINTER thread_info {
59 // Thread-Specific Data
60 uint64_t thread_id{0};
61 string_view name{};
62 uint8_t priority{0};
63 thread_state state{thread_state::ready};
64 uint32_t cpu_ticks{0};
65 uint32_t stack_size{0};
66 uint32_t stack_usage{0}; // High-water mark in bytes
67
68 // Zero-Allocation Architecture Register Context View
70
71 // Extended Metadata Callback Hook(Forwards additional custom properties)
72 metadata_next_fn_t extended_metadata_fn{nullptr};
73 void *extended_metadata_ctx{nullptr};
74
78 struct RELOCO_POINTER metadata_state {
79 const thread_info *thread{nullptr};
80 int step{0};
81 bool in_extended{false};
82 };
83
88 [[nodiscard]] constexpr metadata_map
89 make_metadata_view(metadata_state &the_state) const & noexcept RELOCO_LIFETIMEBOUND {
90 the_state.thread = this;
91 the_state.step = 0;
92 the_state.in_extended = false;
93
94 return metadata_map(&the_state, [](void *ctx, property_entry &out) noexcept -> bool {
95 auto *s = static_cast<metadata_state *>(ctx);
96 if (s == nullptr || s->thread == nullptr)
97 return false;
98
99 // Phase 1: Stream core thread properties
100 if (!s->in_extended) {
101 switch (s->step++) {
102 case 0:
103 out.key = "thread_id";
104 out.val_ptr = &s->thread->thread_id;
105 out.print_fn = [](const void *p, const sink &s_out) noexcept {
106 detail::format_unsigned<detail::radix::decimal>(s_out, *static_cast<const uint64_t *>(p), false);
107 };
108 return true;
109 case 1:
110 out.key = "name";
111 out.val_ptr = &s->thread->name;
112 out.print_fn = [](const void *p, const sink &s_out) noexcept {
113 json::write_escaped_string(s_out, *static_cast<const string_view *>(p));
114 };
115 return true;
116 case 2:
117 out.key = "priority";
118 out.val_ptr = &s->thread->priority;
119 out.print_fn = [](const void *p, const sink &s_out) noexcept {
120 detail::format_unsigned<detail::radix::decimal>(s_out, *static_cast<const uint8_t *>(p), false);
121 };
122 return true;
123 case 3:
124 out.key = "state";
125 out.val_ptr = &s->thread->state;
126 out.print_fn = [](const void *p, const sink &s_out) noexcept {
128 format_parse_context parse_context({});
129 fmt.parse(parse_context);
130 fmt.format(*static_cast<const thread_state *>(p), s_out);
131 };
132 return true;
133 case 4:
134 out.key = "cpu_ticks";
135 out.val_ptr = &s->thread->cpu_ticks;
136 out.print_fn = [](const void *p, const sink &s_out) noexcept {
137 detail::format_unsigned<detail::radix::decimal>(s_out, *static_cast<const uint32_t *>(p), false);
138 };
139 return true;
140 case 5:
141 out.key = "stack_size_bytes";
142 out.val_ptr = &s->thread->stack_size;
143 out.print_fn = [](const void *p, const sink &s_out) noexcept {
144 detail::format_unsigned<detail::radix::decimal>(s_out, *static_cast<const uint32_t *>(p), false);
145 };
146 return true;
147 case 6:
148 out.key = "stack_usage_bytes";
149 out.val_ptr = &s->thread->stack_usage;
150 out.print_fn = [](const void *p, const sink &s_out) noexcept {
151 detail::format_unsigned<detail::radix::decimal>(s_out, *static_cast<const uint32_t *>(p), false);
152 };
153 return true;
154 default:
155 s->in_extended = true;
156 break; // Transition to phase 2
157 }
158 }
159
160 // Phase 2: Forward to extended metadata callback if configured
161 if (s->in_extended) {
162 if (s->thread->extended_metadata_fn != nullptr && s->thread->extended_metadata_ctx != nullptr) {
163 return s->thread->extended_metadata_fn(s->thread->extended_metadata_ctx, out);
164 }
165 }
166
167 return false;
168 });
169 }
170
172};
173
174} // namespace microfmt
Definition microfmt.hpp:881
Type-erased, non-owning metadata map view driven by a sequential next callback.
Definition metadata_map.hpp:56
Zero-allocation, type-erased handle for inspecting and modifying CPU registers from a frame context.
Definition register_context.hpp:118
#define MICROFMT_API_CLASS
Definition compat.hpp:181
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, property_entry &out) noexcept metadata_next_fn_t
Function pointer signature for fetching the next property entry.
Definition metadata_map.hpp:48
thread_state
Detailed execution state of an individual thread.
Definition thread.hpp:20
constexpr void parse(format_parse_context &) noexcept
Definition thread.hpp:26
void format(thread_state state, const sink &out) const noexcept
Definition thread.hpp:28
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 core to extended properties.
Definition thread.hpp:78
Thread descriptor containing thread-specific attributes and CPU register context access.
Definition thread.hpp:58
constexpr metadata_map make_metadata_view(metadata_state &the_state) const &noexcept RELOCO_LIFETIMEBOUND
Exposes a unified metadata_map view that automatically forwards from core thread properties to any at...
Definition thread.hpp:89
metadata_map make_metadata_view(metadata_state &) const &&=delete