|
26 | 26 |
|
27 | 27 | namespace scl { |
28 | 28 |
|
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 { |
30 | 49 | 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}); |
33 | 57 | } |
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) { |
36 | 65 | std::random_device rd{}; |
37 | 66 | std::mt19937 rg{rd()}; |
38 | | - return ChannelDesc(PropParams{rg, bandwidth, latency}); |
| 67 | + return ChannelParams(PropParams{rg, bandwidth, latency, packet_loss}); |
39 | 68 | } |
40 | 69 |
|
| 70 | + ChannelParams() {} |
| 71 | + |
| 72 | + /** |
| 73 | + * @brief Bandwidth of the channel. |
| 74 | + */ |
41 | 75 | std::size_t bandwidth(); |
| 76 | + |
| 77 | + /** |
| 78 | + * @brief Latency of the channel. |
| 79 | + */ |
42 | 80 | std::size_t latency(); |
43 | 81 |
|
| 82 | + /** |
| 83 | + * @brief The packet loss percentage of this channel. |
| 84 | + */ |
| 85 | + float packetLoss(); |
| 86 | + |
44 | 87 | private: |
45 | 88 | struct DetParams final { |
46 | 89 | std::size_t bandwidth; |
47 | 90 | std::size_t latency; |
| 91 | + float packet_loss; |
48 | 92 | }; |
49 | 93 |
|
50 | 94 | struct PropParams final { |
51 | 95 | std::mt19937 rg; |
52 | 96 |
|
53 | 97 | std::normal_distribution<> bandwidth; |
54 | 98 | std::normal_distribution<> latency; |
| 99 | + float packet_loss; |
55 | 100 | }; |
56 | 101 |
|
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} {} |
59 | 104 |
|
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 | + } |
101 | 108 |
|
102 | | - /** |
103 | | - * @brief The channel package loss, a value in [0, 1). |
104 | | - */ |
105 | | - float loss; |
| 109 | + std::variant<DetParams, PropParams> m_params; |
106 | 110 | }; |
107 | 111 |
|
108 | 112 | /** |
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). |
110 | 118 | */ |
111 | | -class NetworkDescription final { |
| 119 | +class NetworkParams final { |
112 | 120 | public: |
113 | 121 | /** |
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. |
115 | 131 | */ |
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); |
132 | 136 |
|
133 | 137 | /** |
134 | | - * @brief Create a default LAN network. |
| 138 | + * @brief Read network parameters from a file. |
135 | 139 | */ |
136 | | - static NetworkDescription createDefaultLAN(std::size_t size); |
| 140 | + static NetworkParams fromFile(const std::string& filename); |
137 | 141 |
|
138 | 142 | /** |
139 | | - * @brief Create a default WAN network. |
| 143 | + * @brief Construct network parameters from a set of channel parameters. |
140 | 144 | */ |
141 | | - static NetworkDescription createDefaultWAN(std::size_t size); |
| 145 | + NetworkParams(const std::unordered_map<ChannelId, ChannelParams>& channels) |
| 146 | + : m_channels(channels) {} |
142 | 147 |
|
143 | 148 | /** |
144 | | - * @brief The size of the network. |
| 149 | + * @brief Get the parameters of a channel. |
145 | 150 | */ |
146 | | - std::size_t size() const { |
147 | | - return m_size; |
| 151 | + ChannelParams channel(ChannelId id) const { |
| 152 | + return m_channels.at(id); |
148 | 153 | } |
149 | 154 |
|
150 | | - /** |
151 | | - * @brief Get the parameters of a particular channel. |
152 | | - */ |
153 | | - ChannelParameters getChannel(ChannelId id); |
154 | | - |
155 | 155 | 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; |
175 | 157 | }; |
176 | 158 |
|
177 | 159 | } // namespace scl |
0 commit comments