|
| 1 | +/* |
| 2 | + * Copyright(c) 2024 ZettaScale Technology and others |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License |
| 7 | + * v. 1.0 which is available at |
| 8 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 | + * |
| 10 | + * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 11 | + */ |
| 12 | + |
| 13 | +#include <algorithm> |
| 14 | +#include <iostream> |
| 15 | +#include <fstream> |
| 16 | +#include <sstream> |
| 17 | +#include <iomanip> |
| 18 | +#include <chrono> |
| 19 | +#include <thread> |
| 20 | +#include <string> |
| 21 | +#include <cstdlib> |
| 22 | +#include <random> |
| 23 | + |
| 24 | +#include <unistd.h> |
| 25 | + |
| 26 | +#include "dds/dds.hpp" |
| 27 | +#include "mop_type.hpp" |
| 28 | + |
| 29 | +using namespace org::eclipse::cyclonedds; |
| 30 | +using namespace std::chrono_literals; |
| 31 | +using namespace std::chrono; |
| 32 | + |
| 33 | +using CLK = high_resolution_clock; |
| 34 | + |
| 35 | +enum class Type { T8, T128, T1k, T8k, T128k }; |
| 36 | + |
| 37 | +static Type type = Type::T128; |
| 38 | +static uint32_t pairid = 0; |
| 39 | +static uint32_t ntopics = 10; |
| 40 | +static std::optional<std::string> datafile; |
| 41 | + |
| 42 | +static dds::core::Time mkDDSTime (const time_point<CLK> x) |
| 43 | +{ |
| 44 | + int64_t t = duration_cast<nanoseconds>(x.time_since_epoch()).count(); |
| 45 | + return dds::core::Time(t / 1000000000, static_cast<uint32_t>(t % 1000000000)); |
| 46 | +} |
| 47 | + |
| 48 | +static volatile std::atomic<bool> interrupted = false; |
| 49 | +static void sigh(int sig) |
| 50 | +{ |
| 51 | + static_cast<void>(sig); |
| 52 | + interrupted = true; |
| 53 | +} |
| 54 | + |
| 55 | +template<typename T> |
| 56 | +static dds::sub::DataReader<T> make_reader(dds::topic::Topic<T> tp) |
| 57 | +{ |
| 58 | + dds::domain::DomainParticipant dp = tp.domain_participant(); |
| 59 | + std::vector<std::string> spart{"P" + std::to_string(pairid + 1 - 2 * (pairid % 2))}; |
| 60 | + dds::sub::qos::SubscriberQos sqos = dp.default_subscriber_qos() << dds::core::policy::Partition(spart); |
| 61 | + dds::sub::Subscriber sub{dp, sqos}; |
| 62 | + return dds::sub::DataReader<T>{sub, tp, tp.qos()}; |
| 63 | +} |
| 64 | + |
| 65 | +template<typename T> |
| 66 | +static dds::pub::DataWriter<T> make_writer(dds::topic::Topic<T> tp) |
| 67 | +{ |
| 68 | + dds::domain::DomainParticipant dp = tp.domain_participant(); |
| 69 | + std::vector<std::string> ppart{"P" + std::to_string(pairid)}; |
| 70 | + dds::pub::qos::PublisherQos pqos = dp.default_publisher_qos() << dds::core::policy::Partition(ppart); |
| 71 | + dds::pub::Publisher pub{dp, pqos}; |
| 72 | + return dds::pub::DataWriter<T>{pub, tp, tp.qos()}; |
| 73 | +} |
| 74 | + |
| 75 | +template<typename T> |
| 76 | +static void source(dds::pub::DataWriter<T> wr) |
| 77 | +{ |
| 78 | + T sample{}; |
| 79 | + sample.k(0); |
| 80 | + auto now = CLK::now(); |
| 81 | + while (!interrupted) |
| 82 | + { |
| 83 | + wr.write(sample, mkDDSTime(CLK::now())); |
| 84 | + ++sample.seq(); |
| 85 | + now += 10ms; |
| 86 | + std::this_thread::sleep_until(now); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// t = reception time, l = latency, i = topic index, k = source key |
| 91 | +struct TLK { int64_t t; double l; uint32_t k; }; |
| 92 | +struct TLIK { int64_t t; double l; size_t i; uint32_t k; }; |
| 93 | +struct LIK { double l; size_t i; uint32_t k; }; |
| 94 | + |
| 95 | +template<typename T> |
| 96 | +class Sink : public dds::sub::NoOpDataReaderListener<T> { |
| 97 | +public: |
| 98 | + Sink() = default; |
| 99 | + |
| 100 | + const std::vector<TLK>& lats() const { |
| 101 | + return lats_; |
| 102 | + }; |
| 103 | + |
| 104 | +private: |
| 105 | + void on_data_available(dds::sub::DataReader<T>& rd) |
| 106 | + { |
| 107 | + const auto now_clk = CLK::now(); |
| 108 | + const int64_t now = duration_cast<nanoseconds>(now_clk.time_since_epoch()).count(); |
| 109 | + auto xs = rd.take(); |
| 110 | + for (const auto& x : xs) { |
| 111 | + if (x.info().valid()) { |
| 112 | + const auto lat = now - (x.info().timestamp().sec() * 1000000000 + x.info().timestamp().nanosec()); |
| 113 | + lats_.push_back(TLK{now, lat / 1e3, x.data().k()}); |
| 114 | + } else { |
| 115 | + interrupted = true; |
| 116 | + } |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + std::vector<TLK> lats_; |
| 121 | +}; |
| 122 | + |
| 123 | +template<typename T> |
| 124 | +static void run() |
| 125 | +{ |
| 126 | + dds::domain::DomainParticipant dp{0}; |
| 127 | + auto tpqos = dp.default_topic_qos() |
| 128 | + << dds::core::policy::Reliability::Reliable(dds::core::Duration::infinite()) |
| 129 | + << dds::core::policy::History::KeepLast(1); |
| 130 | + std::vector<dds::topic::Topic<T>> tps; |
| 131 | + std::vector<dds::pub::DataWriter<T>> wrs; |
| 132 | + for (uint32_t i = 0; i < ntopics; i++) |
| 133 | + tps.push_back(dds::topic::Topic<T>{dp, "Mop" + std::to_string(i), tpqos}); |
| 134 | + for (auto& tp : tps) |
| 135 | + wrs.push_back(make_writer(tp)); |
| 136 | + std::vector<dds::sub::DataReader<T>> rds; |
| 137 | + std::vector<Sink<T>> ls; |
| 138 | + for (size_t i = 0; i < tps.size(); i++) |
| 139 | + ls.push_back(Sink<T>{}); |
| 140 | + for (size_t i = 0; i < tps.size(); i++) |
| 141 | + rds.push_back(make_reader(tps[i])); |
| 142 | + for (size_t i = 0; i < tps.size(); i++) |
| 143 | + rds[i].listener(&ls[i], dds::core::status::StatusMask::data_available()); |
| 144 | + |
| 145 | + signal(SIGINT, sigh); |
| 146 | + signal(SIGTERM, sigh); |
| 147 | + std::vector<std::thread> threads; |
| 148 | + for (auto wr : wrs) |
| 149 | + threads.push_back(std::thread(source<T>, wr)); |
| 150 | + |
| 151 | + // latencies in microseconds |
| 152 | + std::vector<LIK> lats; |
| 153 | + while (!interrupted) |
| 154 | + std::this_thread::sleep_for(103ms); |
| 155 | + for (auto& t : threads) |
| 156 | + t.join(); |
| 157 | + for (auto rd : rds) |
| 158 | + rd.close(); |
| 159 | + for (auto wr : wrs) |
| 160 | + wr.close(); |
| 161 | + // collect latencies for all topics and sort by reception time |
| 162 | + std::vector<TLIK> tlats; |
| 163 | + for (size_t i = 0; i < ls.size(); i++) |
| 164 | + for (const auto& x : ls[i].lats()) |
| 165 | + tlats.push_back(TLIK{x.t, x.l, i, x.k}); |
| 166 | + std::sort(tlats.begin(), tlats.end(), [](const TLIK& a, const TLIK& b) -> bool { return a.t < b.t; }); |
| 167 | + // then reduce to just latency, topic and key |
| 168 | + for (const auto& x : tlats) |
| 169 | + lats.push_back(LIK{x.l, x.i, x.k}); |
| 170 | + |
| 171 | + if (datafile.has_value()) |
| 172 | + { |
| 173 | + std::ofstream f; |
| 174 | + f.open(datafile.value()); |
| 175 | + for (const auto& l : lats) |
| 176 | + f << l.l << " " << l.i << " " << l.k << std::endl; |
| 177 | + f.close(); |
| 178 | + } |
| 179 | + const size_t n = lats.size(); |
| 180 | + if (n < 2) { |
| 181 | + std::cout << "insufficient data" << std::endl; |
| 182 | + } else { |
| 183 | + std::sort(lats.begin(), lats.end(), [](const LIK& a, const LIK& b) -> bool { return a.l < b.l; }); |
| 184 | + std::cout |
| 185 | + << "received " << n |
| 186 | + << " samples; min " << lats[0].l |
| 187 | + << " max-1 " << lats[n-2].l |
| 188 | + << " max " << lats[n-1].l << std::endl; |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +[[noreturn]] |
| 193 | +static void usage() |
| 194 | +{ |
| 195 | + std::cout |
| 196 | + << "usage: cwpl [OPTIONS] id" << std::endl |
| 197 | + << std::endl |
| 198 | + << "OPTIONS:" << std::endl |
| 199 | + << "-tTYPE type to use one of 8, 128 (def), 1k, 8k, 128k" << std::endl |
| 200 | + << "-nNTPS use N (def = 10) topics in parallel" << std::endl |
| 201 | + << "-oFILE write latencies to FILE" << std::endl |
| 202 | + << std::endl |
| 203 | + << "id = 0 writes in partition P0, reads from P1" << std::endl |
| 204 | + << "id = 1 writes in partition P1, reads from P0" << std::endl |
| 205 | + << "id = 2 writes in partition P2, reads from P3" << std::endl |
| 206 | + << "etc." << std::endl; |
| 207 | + std::exit(1); |
| 208 | +} |
| 209 | + |
| 210 | +static Type convert_typestr (const std::string& typestr) |
| 211 | +{ |
| 212 | + if (typestr == "8") { |
| 213 | + return Type::T8; |
| 214 | + } else if (typestr == "128") { |
| 215 | + return Type::T128; |
| 216 | + } else if (typestr == "1k") { |
| 217 | + return Type::T1k; |
| 218 | + } else if (typestr == "8k") { |
| 219 | + return Type::T8k; |
| 220 | + } else if (typestr == "128k") { |
| 221 | + return Type::T128k; |
| 222 | + } else { |
| 223 | + std::cout << "invalid type, should be 8, 128, 1k, 8k, 128k" << std::endl; |
| 224 | + std::exit(1); |
| 225 | + return Type::T128; |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +int main (int argc, char **argv) |
| 230 | +{ |
| 231 | + if (argc < 1) |
| 232 | + usage(); |
| 233 | + |
| 234 | + int opt; |
| 235 | + while ((opt = getopt (argc, argv, "n:o:t:")) != EOF) |
| 236 | + { |
| 237 | + switch (opt) |
| 238 | + { |
| 239 | + case 'n': |
| 240 | + ntopics = static_cast<uint32_t>(std::atoi(optarg)); |
| 241 | + break; |
| 242 | + case 'o': |
| 243 | + datafile = std::string(optarg); |
| 244 | + break; |
| 245 | + case 't': |
| 246 | + type = convert_typestr(std::string(optarg)); |
| 247 | + break; |
| 248 | + default: |
| 249 | + usage(); |
| 250 | + } |
| 251 | + } |
| 252 | + if (argc - optind != 1) |
| 253 | + { |
| 254 | + usage(); |
| 255 | + } |
| 256 | + pairid = static_cast<uint32_t>(std::atoi(argv[optind])); |
| 257 | + switch (type) |
| 258 | + { |
| 259 | + case Type::T8: run<Mop8>(); break; |
| 260 | + case Type::T128: run<Mop128>(); break; |
| 261 | + case Type::T1k: run<Mop1k>(); break; |
| 262 | + case Type::T8k: run<Mop8k>(); break; |
| 263 | + case Type::T128k: run<Mop128k>(); break; |
| 264 | + } |
| 265 | + return 0; |
| 266 | +} |
0 commit comments