File indexing completed on 2026-10-20 08:45:51
0001 #include <cassert>
0002 #include <cstdint>
0003 #include <cstring>
0004 #include <string>
0005 #include <vector>
0006
0007 #include "EventTiming.hh"
0008 #include "EventTimingProfileReport.hh"
0009 #include "NP.hh"
0010 #include "NPFold.h"
0011
0012 namespace
0013 {
0014 int FindRange(const NP* ranges, const char* prefix)
0015 {
0016 assert(ranges);
0017 for (std::size_t index = 0; index < ranges->names.size(); ++index)
0018 if (ranges->names[index].find(prefix) == 0)
0019 return static_cast<int>(index);
0020 return -1;
0021 }
0022 }
0023
0024 int main()
0025 {
0026 std::vector<EventTimingProfileRecord> records =
0027 EventTimingProfile::ReadFile(EVENT_TIMING_PROFILE_FIXTURE);
0028 assert(records.size() == 15u);
0029
0030 std::vector<std::string> keys;
0031 std::vector<std::int64_t> timestamps;
0032 for (const EventTimingProfileRecord& record : records)
0033 {
0034 keys.push_back(record.name);
0035 timestamps.push_back(record.sample.steady_time_ns);
0036 }
0037
0038 NP* profile = EventTimingProfileReport::MakeEventTimingProfileArray(records);
0039 NP* ranges = EventTimingProfileReport::MakeEventTimingRanges(records);
0040 assert(profile);
0041 assert(profile->shape.size() == 2u);
0042 assert(profile->shape[0] == 15);
0043 assert(profile->shape[1] == 4);
0044 assert(profile->names == keys);
0045 assert(profile->values<std::int64_t>()[1] == timestamps[0]);
0046
0047 const int init = FindRange(ranges, "EventTimingProfile__Configure");
0048 const int load = FindRange(ranges, "CSGFoundry__Load_HEAD");
0049 const int launch = FindRange(ranges, "A000_QSim__simulate_PREL");
0050 assert(init >= 0);
0051 assert(load >= 0);
0052 assert(launch >= 0);
0053 assert(ranges->values<std::int64_t>()[5 * init + 2] == 100'000);
0054 assert(ranges->values<std::int64_t>()[5 * load + 2] == 300'000);
0055 assert(ranges->values<std::int64_t>()[5 * launch + 2] == 250'000);
0056
0057 for (EventTimingProfileRecord& record : records)
0058 record.sample.wall_time_us += 987'654'321;
0059 NP* shifted = EventTimingProfileReport::MakeEventTimingRanges(records);
0060 assert(shifted->arr_bytes() == ranges->arr_bytes());
0061 assert(std::memcmp(shifted->bytes(), ranges->bytes(), ranges->arr_bytes()) == 0);
0062
0063 NPFold fold;
0064 NP::SetMeta<std::string>(
0065 fold.meta, "timing", records.front().sample.serialize());
0066 NP::SetMeta<std::string>(fold.meta, "ordinary", "value");
0067 std::vector<std::string> profile_keys;
0068 std::vector<std::string> profile_values;
0069 fold.getMetaKV(&profile_keys, &profile_values, true);
0070 assert(profile_keys == std::vector<std::string>{"timing"});
0071 assert(profile_values.size() == 1u);
0072 assert(EventTimingSample::looksLikeSerialized(profile_values.front()));
0073
0074 delete shifted;
0075 delete ranges;
0076 delete profile;
0077 return 0;
0078 }