plcopen 0.22.0
载入中...
搜索中...
未找到
wcet.h
浏览该文件的文档.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <limits>
7
8#include "st/bytecode.h"
9
10namespace plcopen::core::st
11{
12
13// Work-unit charging is a VM watchdog contract. TimingClass is deliberately
14// separate: it identifies the target-specific calibration needed before an
15// artifact can be discussed in wall-clock units.
25
34
40
41inline constexpr std::uint32_t kWcetReviewedBytecodeFormatVersion = 6;
42inline constexpr std::size_t kWcetReviewedOpcodeCount = 87;
43inline constexpr std::size_t kWcetTimingClassCount =
44 static_cast<std::size_t>(WcetTimingClass::count);
45
47 "review the A2 opcode cost table for the new bytecode format");
48static_assert(static_cast<std::size_t>(Op::debug_probe) + 1U ==
50 "review the A2 cost of every added or removed opcode");
51
85
86constexpr const char *wcet_budget_rule_name(WcetBudgetRule rule) noexcept
87{
88 switch(rule) {
89 case WcetBudgetRule::fixed_one: return "fixed_one";
90 case WcetBudgetRule::string_capacity: return "string_capacity";
91 case WcetBudgetRule::object_size: return "object_size";
92 case WcetBudgetRule::standard_function: return "standard_function";
93 case WcetBudgetRule::output_count: return "output_count";
94 default: return "unreviewed";
95 }
96}
97
98constexpr const char *wcet_timing_class_name(WcetTimingClass timing) noexcept
99{
100 switch(timing) {
101 case WcetTimingClass::fixed: return "fixed";
102 case WcetTimingClass::linear_bytes: return "linear_bytes";
103 case WcetTimingClass::standard_function: return "standard_function";
104 case WcetTimingClass::native_fb: return "native_fb";
105 default: return "unreviewed";
106 }
107}
108
110{
111 std::uint32_t bytecode_format_version = 0;
113 bool bounded = false;
117 std::uint64_t worst_case_work_units = 0;
118 std::uint16_t max_call_depth = 0;
119 std::uint16_t max_instance_depth = 0;
120 std::uint32_t program_count = 0;
121 std::uint32_t native_fb_instances = 0;
122 std::array<std::uint32_t, kWcetTimingClassCount>
124 std::uint32_t unreviewed_opcodes = 0;
125};
126
127namespace wcet_detail
128{
129
130inline void increment(std::uint32_t &value) noexcept
131{
132 if(value != std::numeric_limits<std::uint32_t>::max()) ++value;
133}
134
135inline void add_size(std::uint32_t &value, std::size_t added) noexcept
136{
137 const std::size_t room =
138 std::numeric_limits<std::uint32_t>::max() - value;
139 value += static_cast<std::uint32_t>(added > room ? room : added);
140}
141
142inline void add_opcode(WcetReport &report, Op op) noexcept
143{
144 const WcetOpcodeCost cost = wcet_opcode_cost(op);
145 const std::size_t timing =
146 static_cast<std::size_t>(cost.timing_class);
148 timing >= report.artifact_opcode_counts.size()) {
149 increment(report.unreviewed_opcodes);
150 return;
151 }
152 increment(report.artifact_opcode_counts[timing]);
153}
154
155inline void add_region(WcetReport &report, const SfcRegionInfo &region) noexcept
156{
157 for(const std::uint32_t offset : region.instruction_offsets) {
158 if(offset >= region.code.size()) {
159 increment(report.unreviewed_opcodes);
160 } else {
161 add_opcode(report, static_cast<Op>(region.code[offset]));
162 }
163 }
164}
165
166inline void add_program_body(WcetReport &report,
167 const Program &program) noexcept
168{
169 report.opcode_table_reviewed = report.opcode_table_reviewed &&
171 increment(report.program_count);
172 add_size(report.native_fb_instances, program.fbs.size());
173 for(const std::uint32_t offset : program.instruction_offsets) {
174 if(offset >= program.code.size()) {
175 increment(report.unreviewed_opcodes);
176 } else {
177 add_opcode(report, static_cast<Op>(program.code[offset]));
178 }
179 }
180 for(const SfcNetworkInfo &network : program.sfc_networks) {
181 for(const SfcTransitionInfo &transition : network.transitions)
182 add_region(report, transition.condition);
183 for(const SfcActionInfo &action : network.actions)
184 add_region(report, action.region);
185 }
186}
187
189 const Program &program) noexcept
190{
191 if(program.programs.empty()) {
192 add_program_body(report, program);
193 return;
194 }
195 for(const Program &child : program.programs)
196 add_artifact_programs(report, child);
197}
198
199} // namespace wcet_detail
200
201// Load/diagnostic-domain report. It walks existing vectors but performs no
202// allocation and never changes the executable artifact.
204{
205 WcetReport report;
206 report.bytecode_format_version = program.format_version;
207 report.opcode_table_reviewed =
209 report.bounded = program.worst_case_bounded;
210 report.worst_case_work_units = report.bounded
211 ? program.worst_case_instructions : 0U;
212 report.max_call_depth = program.max_call_depth;
213 report.max_instance_depth = program.max_instance_depth;
215
216 const auto count = [&report](WcetTimingClass timing) noexcept {
217 return report.artifact_opcode_counts[
218 static_cast<std::size_t>(timing)];
219 };
221 report.native_fb_instances != 0 ||
227 return report;
228}
229
230} // namespace plcopen::core::st
void add_artifact_programs(WcetReport &report, const Program &program) noexcept
定义 wcet.h:188
void add_opcode(WcetReport &report, Op op) noexcept
定义 wcet.h:142
void increment(std::uint32_t &value) noexcept
定义 wcet.h:130
void add_region(WcetReport &report, const SfcRegionInfo &region) noexcept
定义 wcet.h:155
void add_size(std::uint32_t &value, std::size_t added) noexcept
定义 wcet.h:135
void add_program_body(WcetReport &report, const Program &program) noexcept
定义 wcet.h:166
constexpr std::size_t kWcetTimingClassCount
定义 wcet.h:43
constexpr const char * wcet_timing_class_name(WcetTimingClass timing) noexcept
定义 wcet.h:98
constexpr std::size_t kWcetReviewedOpcodeCount
定义 wcet.h:42
constexpr std::uint32_t kWcetReviewedBytecodeFormatVersion
定义 wcet.h:41
constexpr std::uint32_t kBytecodeFormatVersion
WcetReport make_wcet_report(const Program &program) noexcept
定义 wcet.h:203
constexpr WcetOpcodeCost wcet_opcode_cost(Op op) noexcept
定义 wcet.h:52
constexpr const char * wcet_budget_rule_name(WcetBudgetRule rule) noexcept
定义 wcet.h:86
std::vector< Program > programs
std::vector< SfcTransitionInfo > transitions
std::vector< SfcActionInfo > actions
std::uint64_t worst_case_work_units
定义 wcet.h:117
std::uint32_t bytecode_format_version
定义 wcet.h:111
std::uint32_t native_fb_instances
定义 wcet.h:121
std::array< std::uint32_t, kWcetTimingClassCount > artifact_opcode_counts
定义 wcet.h:123