Skip to content

Commit 44a482a

Browse files
committed
more simulator stuff
1 parent 866bedf commit 44a482a

File tree

14 files changed

+236
-238
lines changed

14 files changed

+236
-238
lines changed

include/scl/simulation/context.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919

2020
#include <cstddef>
2121
#include <type_traits>
22-
#include <iostream>
2322

2423
#include "scl/simulation/event.h"
25-
#include "scl/simulation/network_description.h"
24+
#include "scl/simulation/params.h"
2625
#include "scl/simulation/simulator.h"
2726

2827
namespace scl::details {
@@ -39,7 +38,8 @@ class SimulatorContext final {
3938
* @brief Create a new simulation context object.
4039
*/
4140
static SimulatorContext create(
42-
NetworkDescription network_desc,
41+
std::size_t number_of_parties,
42+
NetworkParams network_params,
4343
std::vector<Simulator::SimulationHook>&& hooks);
4444

4545
/**
@@ -86,29 +86,30 @@ class SimulatorContext final {
8686
/**
8787
* @brief Get the channel parameters of a channel.
8888
*/
89-
NetworkDescription::ChannelParameters getChannel(ChannelId cid) {
90-
return m_network_desc.getChannel(cid);
89+
ChannelParams getChannel(ChannelId cid) {
90+
return m_network_params.channel(cid);
9191
}
9292

9393
/**
9494
* @brief Get the number of parties in this simulation.
9595
*/
9696
std::size_t numberOfParties() const {
97-
return m_network_desc.size();
97+
return m_number_of_parties;
9898
}
9999

100100
Simulator::Result toResult() {
101101
return Simulator::Result{std::move(m_events)};
102102
}
103103

104104
private:
105-
NetworkDescription m_network_desc;
105+
std::size_t m_number_of_parties;
106+
NetworkParams m_network_params;
106107
std::vector<EventList> m_events;
107108
std::vector<Time::TimePoint> m_clocks;
108109
std::vector<Simulator::SimulationHook> m_hooks;
109110

110-
SimulatorContext(NetworkDescription network_desc)
111-
: m_network_desc(network_desc) {}
111+
SimulatorContext(NetworkParams network_params)
112+
: m_network_params(network_params) {}
112113

113114
void runHooks(std::size_t pid, Event* event);
114115
};

include/scl/simulation/params.h

Lines changed: 85 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -26,152 +26,134 @@
2626

2727
namespace scl {
2828

29-
class ChannelDesc final {
29+
/**
30+
* @brief Provider for a communication channels paramters.
31+
* @ingroup eval-sim
32+
*
33+
* A ChannelParams object describes the characteristics of a particular
34+
* communication channel in terms of its bandwidth and latency.
35+
*
36+
* A communication channel is either "deterministic" or "probabilistic", with
37+
* respect to its parameters. Concretely, either the channel's parameters are
38+
* fixed or they are sampled from some distribution.
39+
*
40+
* Note that the packet loss of a channel is a (by its nature) probabilistic
41+
* parameter regardless of the type of the other parameters.
42+
*
43+
* The units used is as follows:
44+
* - bits per second for bandwidth.
45+
* - microseconds for latency.
46+
* - percentage, as a value between 0 and 1 for packet loss.
47+
*/
48+
class ChannelParams final {
3049
public:
31-
ChannelDesc createDet(std::size_t bandwidth, std::size_t latency) {
32-
return ChannelDesc(DetParams{bandwidth, latency});
50+
/**
51+
* @brief Create a deterministic channel description.
52+
*/
53+
static ChannelParams createDet(std::size_t bandwidth,
54+
std::size_t latency,
55+
float packet_loss) {
56+
return ChannelParams(DetParams{bandwidth, latency, packet_loss});
3357
}
34-
ChannelDesc createProp(std::normal_distribution<> bandwidth,
35-
std::normal_distribution<> latency) {
58+
59+
/**
60+
* @brief Create a probabilistic channel description.
61+
*/
62+
static ChannelParams createProb(std::normal_distribution<> bandwidth,
63+
std::normal_distribution<> latency,
64+
float packet_loss) {
3665
std::random_device rd{};
3766
std::mt19937 rg{rd()};
38-
return ChannelDesc(PropParams{rg, bandwidth, latency});
67+
return ChannelParams(PropParams{rg, bandwidth, latency, packet_loss});
3968
}
4069

70+
ChannelParams() {}
71+
72+
/**
73+
* @brief Bandwidth of the channel.
74+
*/
4175
std::size_t bandwidth();
76+
77+
/**
78+
* @brief Latency of the channel.
79+
*/
4280
std::size_t latency();
4381

82+
/**
83+
* @brief The packet loss percentage of this channel.
84+
*/
85+
float packetLoss();
86+
4487
private:
4588
struct DetParams final {
4689
std::size_t bandwidth;
4790
std::size_t latency;
91+
float packet_loss;
4892
};
4993

5094
struct PropParams final {
5195
std::mt19937 rg;
5296

5397
std::normal_distribution<> bandwidth;
5498
std::normal_distribution<> latency;
99+
float packet_loss;
55100
};
56101

57-
ChannelDesc(DetParams params) : m_params(params) {}
58-
ChannelDesc(PropParams params) : m_params(params) {}
102+
ChannelParams(DetParams params) : m_params{params} {}
103+
ChannelParams(PropParams params) : m_params{params} {}
59104

60-
std::variant<DetParams, PropParams> m_params;
61-
};
62-
63-
// TODO: Create a simplier, and more extensible network description. Potentially
64-
// shorten the name as well.
65-
//
66-
// The new object (interface shown below) should provide the "raw" link
67-
// bandwidth and latency on a given channel. I guess it is possible to
68-
// dynamically adjust the bandwidth of a channel depending on usage, but I think
69-
// that task is better done elsewhere (e.g., in the Transport).
70-
//
71-
// NetworkDesc:
72-
// std::size_t latency(ChannelId cid);
73-
// std::size_t bandwidth(ChannelId cid);
74-
//
75-
// ChannelDesc:
76-
// virtual std::size_t latency();
77-
// virtual std::size_t bandwidth();
78-
//
79-
// NetworkDesc is internally composed of a list of ChannelDesc. The definition
80-
// of ChannelDesc allows the user to either provide some constant value, or to
81-
// sample the value from a distribution.
82-
83-
/**
84-
* @brief Describes the characterists of a channel.
85-
*/
86-
struct ChannelDescription final {
87-
/**
88-
* @brief The channel bandwidth, in bits/s.
89-
*/
90-
std::size_t bandwith;
91-
92-
/**
93-
* @brief The channel latency, in microseconds.
94-
*/
95-
std::size_t latency;
96-
97-
/**
98-
* @brief The latency variance, in microseconds.
99-
*/
100-
std::size_t latency_jitter;
105+
bool deterministicChannel() const {
106+
return m_params.index() == 0;
107+
}
101108

102-
/**
103-
* @brief The channel package loss, a value in [0, 1).
104-
*/
105-
float loss;
109+
std::variant<DetParams, PropParams> m_params;
106110
};
107111

108112
/**
109-
* @brief Describes a network in terms of its channels.
113+
* @brief A parameter collection for a network.
114+
* @ingroup eval-sim
115+
*
116+
* NetworkParams can be viewed as a collection of ChannelParams (as that is what
117+
* it ultimately is).
110118
*/
111-
class NetworkDescription final {
119+
class NetworkParams final {
112120
public:
113121
/**
114-
* @brief The parameters of a channel at a given point in time.
122+
* @brief Create a set of network parameters with some sane defaults.
123+
*
124+
* This creates a new set of network parameters supporting a specified number
125+
* of parties. The provided \p bandwidth and \p latency arguments determine
126+
* the bandwidth and latency for all channels that are not "loopback"
127+
* channels.
128+
*
129+
* The default values provide a sane "WAN"-like default of 1Mbps bandwidth,
130+
* 50ms latency and 0.1% packet loss.
115131
*/
116-
struct ChannelParameters {
117-
/**
118-
* @brief The current channel bandwidth.
119-
*/
120-
std::size_t bandwidth;
121-
122-
/**
123-
* @brief The current channel latency.
124-
*/
125-
std::size_t latency;
126-
127-
/**
128-
* @brief The current channel packege loss.
129-
*/
130-
float loss;
131-
};
132+
static NetworkParams create(std::size_t number_of_parties,
133+
std::size_t bandwidth = 1000000,
134+
std::size_t latency = 50000,
135+
float packet_loss = 0.01);
132136

133137
/**
134-
* @brief Create a default LAN network.
138+
* @brief Read network parameters from a file.
135139
*/
136-
static NetworkDescription createDefaultLAN(std::size_t size);
140+
static NetworkParams fromFile(const std::string& filename);
137141

138142
/**
139-
* @brief Create a default WAN network.
143+
* @brief Construct network parameters from a set of channel parameters.
140144
*/
141-
static NetworkDescription createDefaultWAN(std::size_t size);
145+
NetworkParams(const std::unordered_map<ChannelId, ChannelParams>& channels)
146+
: m_channels(channels) {}
142147

143148
/**
144-
* @brief The size of the network.
149+
* @brief Get the parameters of a channel.
145150
*/
146-
std::size_t size() const {
147-
return m_size;
151+
ChannelParams channel(ChannelId id) const {
152+
return m_channels.at(id);
148153
}
149154

150-
/**
151-
* @brief Get the parameters of a particular channel.
152-
*/
153-
ChannelParameters getChannel(ChannelId id);
154-
155155
private:
156-
struct JitterSampler {
157-
std::mt19937 rg;
158-
std::normal_distribution<> dist;
159-
160-
std::size_t operator()() {
161-
return std::lround(dist(rg));
162-
}
163-
};
164-
165-
std::unordered_map<ChannelId, ChannelDescription> m_channels;
166-
std::unordered_map<ChannelId, JitterSampler> m_samplers;
167-
168-
std::size_t m_size;
169-
170-
NetworkDescription(
171-
const std::unordered_map<ChannelId, ChannelDescription>& channels,
172-
const std::unordered_map<ChannelId, JitterSampler>& samplers,
173-
std::size_t size)
174-
: m_channels(channels), m_samplers(samplers), m_size(size) {}
156+
std::unordered_map<ChannelId, ChannelParams> m_channels;
175157
};
176158

177159
} // namespace scl

include/scl/simulation/runtime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class SimulatorRuntime final : public Runtime {
3232
static const std::size_t MANAGE_PID = static_cast<std::size_t>(-1);
3333

3434
public:
35+
using Runtime::schedule;
36+
3537
SimulatorRuntime(SimulatorContext& sim_ctx)
3638
: m_sim_ctx(sim_ctx), m_current_pid(MANAGE_PID) {}
3739

include/scl/simulation/simulator.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
#include <concepts>
2121
#include <functional>
2222
#include <memory>
23-
#include <stdexcept>
2423
#include <vector>
2524

2625
#include "scl/protocol.h"
2726
#include "scl/simulation/event.h"
28-
#include "scl/simulation/network_description.h"
27+
#include "scl/simulation/params.h"
2928

3029
namespace scl {
3130

3231
/**
3332
* @brief Concept for protocol builder objects.
33+
* @ingroup eval-sim
3434
*
3535
* A "ProtocolBuilder" is any type which, when invoked, returns an std::vector
3636
* of Protocol objects.
@@ -42,22 +42,28 @@ concept ProtocolBuilder = requires(BUILDER builder) {
4242

4343
/**
4444
* @brief The simulator.
45+
* @ingroup eval-sim
4546
*/
4647
class Simulator final {
4748
public:
49+
/**
50+
* @brief The result of a simulation.
51+
*/
4852
class Result final {
4953
public:
5054
Result(std::vector<EventList>&& events) : m_events(std::move(events)) {}
5155
std::size_t numberOfParties() const {
5256
return m_events.size();
5357
}
58+
5459
EventList& operator[](std::size_t i) {
5560
return m_events[i];
5661
}
5762

5863
private:
5964
std::vector<EventList> m_events;
6065
};
66+
6167
/**
6268
* @brief A simulation hook.
6369
*
@@ -100,12 +106,9 @@ class Simulator final {
100106
* @brief Run the simulation.
101107
*/
102108
template <ProtocolBuilder BUILDER>
103-
Result run(BUILDER builder, NetworkDescription network_desc) {
109+
Result run(BUILDER builder, NetworkParams network_params) {
104110
auto protocol = builder();
105-
if (protocol.size() != network_desc.size()) {
106-
throw std::logic_error("protocols do not match network definition");
107-
}
108-
return run(std::move(protocol), network_desc);
111+
return run(std::move(protocol), network_params);
109112
}
110113

111114
/**
@@ -130,7 +133,7 @@ class Simulator final {
130133
std::vector<SimulationHook> m_hooks;
131134

132135
Result run(std::vector<std::unique_ptr<Protocol>>&& protocols,
133-
NetworkDescription network_desc);
136+
NetworkParams network_params);
134137
};
135138

136139
} // namespace scl

src/scl/simulation/channel.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ namespace {
5252
// receiving.
5353
class RecvPendingEvent final : public ChannelEvent {
5454
public:
55-
RecvPendingEvent(Time::Duration timestamp, ChannelId id)
56-
: ChannelEvent(timestamp, id), m_offset(Time::Duration::zero()) {}
55+
using ChannelEvent::ChannelEvent;
5756

5857
void write(std::ostream&) override {}
5958

@@ -70,7 +69,7 @@ class RecvPendingEvent final : public ChannelEvent {
7069
}
7170

7271
private:
73-
Time::Duration m_offset;
72+
Time::Duration m_offset = Time::Duration::zero();
7473
};
7574

7675
// Waits (i.e., suspends) until the transport has data ready for us

0 commit comments

Comments
 (0)