report: edits of methodology throughput sections
[csit.git] / docs / report / introduction / methodology_plrsearch.rst
1 .. _plrsearch_algorithm:
2
3 PLRsearch
4 ^^^^^^^^^
5
6 Abstract algorithm
7 ~~~~~~~~~~~~~~~~~~
8
9 .. TODO: Refer to packet forwarding terminology, such as "offered load" and
10    "loss ratio".
11
12 Eventually, a better description of the abstract search algorithm
13 will appear at this IETF standard: `plrsearch draft`_.
14
15 Motivation
16 ``````````
17
18 Network providers are interested in throughput a device can sustain.
19
20 `RFC 2544`_ assumes loss ratio is given by a deterministic function of
21 offered load. But NFV software devices are not deterministic (enough).
22 This leads for deterministic algorithms (such as MLRsearch with single
23 trial) to return results, which when repeated show relatively high
24 standard deviation, thus making it harder to tell what "the throughput"
25 actually is.
26
27 We need another algorithm, which takes this indeterminism into account.
28
29 Model
30 `````
31
32 Each algorithm searches for an answer to a precisely formulated
33 question. When the question involves indeterministic systems, it has to
34 specify probabilities (or prior distributions) which are tied to a
35 specific probabilistic model. Different models will have different
36 number (and meaning) of parameters. Complicated (but more realistic)
37 models have many parameters, and the math involved can be very
38 convoluted. It is better to start with simpler probabilistic model, and
39 only change it when the output of the simpler algorithm is not stable or
40 useful enough.
41
42 This document is focused on algorithms related to packet loss count
43 only. No latency (or other information) is taken into account. For
44 simplicity, only one type of measurement is considered: dynamically
45 computed offered load, constant within trial measurement of
46 predetermined trial duration.
47
48 The main idea of the search apgorithm is to iterate trial measurements,
49 using `Bayesian inference`_ to compute both the current estimate
50 of "the throughput" and the next offered load to measure at.
51 The computations are done in parallel with the trial measurements.
52
53 The following algorithm makes an assumption that packet traffic
54 generator detects duplicate packets on receive detection, and reports
55 this as an error.
56
57 Poisson distribution
58 ````````````````````
59
60 For given offered load, number of packets lost during trial measurement
61 is assumed to come from `Poisson distribution`_,
62 each trial is assumed to be independent, and the (unknown) Poisson parameter
63 (average number of packets lost per second) is assumed to be
64 constant across trials.
65
66 When comparing different offered loads, the average loss per second is
67 assumed to increase, but the (deterministic) function from offered load
68 into average loss rate is otherwise unknown. This is called "loss function".
69
70 Given a target loss ratio (configurable), there is an unknown offered load
71 when the average is exactly that. We call that the "critical load".
72 If critical load seems higher than maximum offerable load, we should use
73 the maximum offerable load to make search output more conservative.
74
75 Side note: `Binomial distribution`_ is a better fit compared to Poisson
76 distribution (acknowledging that the number of packets lost cannot be
77 higher than the number of packets offered), but the difference tends to
78 be relevant in loads far above the critical region, so using Poisson
79 distribution helps the algorithm focus on critical region better.
80
81 Of course, there are great many increasing functions (as candidates
82 for loss function). The offered load has to be chosen for each trial,
83 and the computed posterior distribution of critical load
84 changes with each trial result.
85
86 To make the space of possible functions more tractable, some other
87 simplifying assumptions are needed. As the algorithm will be examining
88 (also) loads close to the critical load, linear approximation to the
89 loss function in the critical region is important.
90 But as the search algorithm needs to evaluate the function also far
91 away from the critical region, the approximate function has to be
92 well-behaved for every positive offered load, specifically it cannot predict
93 non-positive packet loss rate.
94
95 Within this document, "fitting function" is the name for such a well-behaved
96 function, which approximates the unknown loss function in the critical region.
97
98 Results from trials far from the critical region are likely to affect
99 the critical rate estimate negatively, as the fitting function does not
100 need to be a good approximation there. Discarding some results,
101 or "suppressing" their impact with ad-hoc methods (other than
102 using Poisson distribution instead of binomial) is not used, as such
103 methods tend to make the overall search unstable. We rely on most of
104 measurements being done (eventually) within the critical region, and
105 overweighting far-off measurements (eventually) for well-behaved fitting
106 functions.
107
108 Speaking about new trials, each next trial will be done at offered load
109 equal to the current average of the critical load.
110 Alternative methods for selecting offered load might be used,
111 in an attempt to speed up convergence, but such methods tend to be
112 scpecific for a particular system under tests.
113
114 Fitting function coefficients distribution
115 ``````````````````````````````````````````
116
117 To accomodate systems with different behaviours, the fitting function is
118 expected to have few numeric parameters affecting its shape (mainly
119 affecting the linear approximation in the critical region).
120
121 The general search algorithm can use whatever increasing fitting
122 function, some specific functions can described later.
123
124 It is up to implementer to chose a fitting function and prior
125 distribution of its parameters. The rest of this document assumes each
126 parameter is independently and uniformly distributed over a common
127 interval. Implementers are to add non-linear transformations into their
128 fitting functions if their prior is different.
129
130 Exit condition for the search is either critical load stdev
131 becoming small enough, or overal search time becoming long enough.
132
133 The algorithm should report both avg and stdev for critical load. If the
134 reported averages follow a trend (without reaching equilibrium), avg and
135 stdev should refer to the equilibrium estimates based on the trend, not
136 to immediate posterior values.
137
138 Integration
139 ```````````
140
141 The posterior distributions for fitting function parameters will not be
142 integrable in general.
143
144 The search algorithm utilises the fact that trial measurement takes some
145 time, so this time can be used for numeric integration (using suitable
146 method, such as Monte Carlo) to achieve sufficient precision.
147
148 Optimizations
149 `````````````
150
151 After enough trials, the posterior distribution will be concentrated in
152 a narrow area of parameter space. The integration method should take
153 advantage of that.
154
155 Even in the concentrated area, the likelihood can be quite small, so the
156 integration algorithm should track the logarithm of the likelihood, and
157 also avoid underflow errors by other means.
158
159 FD.io CSIT Implementation Specifics
160 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
161
162 The search receives min_rate and max_rate values, to avoid measurements
163 at offered loads not supporeted by the traffic generator.
164
165 The implemented tests cases use bidirectional traffic.
166 The algorithm stores each rate as bidirectional rate (internally,
167 the algorithm is agnostic to flows and directions,
168 it only cares about overall counts of packets sent and packets lost),
169 but debug output from traffic generator lists unidirectional values.
170
171 Measurement delay
172 `````````````````
173
174 In a sample implemenation in FD.io CSIT project, there is roughly 0.5
175 second delay between trials due to restrictons imposed by packet traffic
176 generator in use (T-Rex).
177
178 As measurements results come in, posterior distribution computation takes
179 more time (per sample), although there is a considerable constant part
180 (mostly for inverting the fitting functions).
181
182 Also, the integrator needs a fair amount of samples to reach the region
183 the posterior distribution is concentrated at.
184
185 And of course, speed of the integrator depends on computing power
186 of the CPU the algorithm is able to use.
187
188 All those timing related effects are addressed by arithmetically increasing
189 trial durations with configurable coefficients
190 (currently 10.2 seconds for the first trial,
191 each subsequent trial being 0.2 second longer).
192
193 Rounding errors and underflows
194 ``````````````````````````````
195
196 In order to avoid them, the current implementation tracks natural logarithm
197 (instead of the original quantity) for any quantity which is never negative.
198 Logarithm of zero is minus infinity (not supported by Python),
199 so special value "None" is used instead.
200 Specific functions for frequent operations
201 (such as "logarithm of sum of exponentials")
202 are defined to handle None correctly.
203
204 Fitting functions
205 `````````````````
206
207 Current implementation uses two fitting functions.
208 In general, their estimates for critical rate differ,
209 which adds a simple source of systematic error,
210 on top of randomness error reported by integrator.
211 Otherwise the reported stdev of critical rate estimate
212 is unrealistically low.
213
214 Both functions are not only increasing, but convex
215 (meaning the rate of increase is also increasing).
216
217 As `primitive function`_ to any positive function is an increasing function,
218 and primitive function to any increasing function is convex function;
219 both fitting functions were constructed as double primitive function
220 to a positive function (even though the intermediate increasing function
221 is easier to describe).
222
223 As not any function is integrable, some more realistic functions
224 (especially with respect to behavior at very small offered loads)
225 are not easily available.
226
227 Both fitting functions have a "central point" and a "spread",
228 varied by simply shifting and scaling (in x-axis, the offered load direction)
229 the function to be doubly integrated.
230 Scaling in y-axis (the loss rate direction) is fixed by the requirement of
231 transfer rate staying nearly constant in very high offered loads.
232
233 In both fitting functions (as they are a double primitive function
234 to a symmetric function), the "central point" turns out
235 to be equal to the aforementioned limiting transfer rate,
236 so the fitting function parameter is named "mrr",
237 the same quantity our Maximum Receive Rate tests are designed to measure.
238
239 Both fitting functions return logarithm of loss rate,
240 to avoid rounding errors and underflows.
241 Parameters and offered load are not given as logarithms,
242 as they are not expected to be extreme,
243 and the formulas are simpler that way.
244
245 Both fitting functions have several mathematically equivalent formulas,
246 each can lead to an overflow or underflow in different places.
247 Overflows can be eliminated by using different exact formulas
248 for different argument ranges.
249 Underflows can be avoided by using approximate formulas
250 in affected argument ranges, such ranges have their own formulas to compute.
251 At the end, both fitting function implementations
252 contain multiple "if" branches, discontinuities are a possibility
253 at range boundaries.
254
255 Offered load for next trial measurement is the average of critical rate estimate.
256 During each measurement, two estimates are computed,
257 even though only one (in alternating order) is used for next offered load.
258
259 Stretch function
260 ----------------
261
262 The original function (before applying logarithm) is primitive function
263 to `logistic function`_.
264 The name "stretch" is used for related function
265 in context of neural networks with sigmoid activation function.
266
267 Erf function
268 ------------
269
270 The original function is double primitive function to `Gaussian function`_.
271 The name "erf" comes from error function, the first primitive to Gaussian.
272
273 Prior distributions
274 ```````````````````
275
276 The numeric integrator expects all the parameters to be distributed
277 (independently and) uniformly on an interval (-1, 1).
278
279 As both "mrr" and "spread" parameters are positive and not not dimensionless,
280 a transformation is needed. Dimentionality is inherited from max_rate value.
281
282 The "mrr" parameter follows a `Lomax distribution`_
283 with alpha equal to one, but shifted so that mrr is always greater than 1
284 packet per second.
285
286 The "stretch" parameter is generated simply as the "mrr" value
287 raised to a random power between zero and one;
288 thus it follows a `reciprocal distribution`_.
289
290 Integrator
291 ``````````
292
293 After few measurements, the posterior distribution of fitting function
294 arguments gets quite concentrated into a small area.
295 The integrator is using `Monte Carlo`_ with `importance sampling`_
296 where the biased distribution is `bivariate Gaussian`_ distribution,
297 with deliberately larger variance.
298 If the generated sample falls outside (-1, 1) interval,
299 another sample is generated.
300
301 The center and the variance for the biased distribution has three sources.
302 First is a prior information. After enough samples are generated,
303 the biased distribution is constructed from a mixture of two sources.
304 Top 12 most weight samples, and all samples (the mix ratio is computed
305 from the relative weights of the two populations).
306 When integration (run along a particular measurement) is finished,
307 the mixture bias distribution is used as the prior information
308 for the next integration.
309
310 This combination showed the best behavior, as the integrator usually follows
311 two phases. First phase (where the top 12 samples are dominating)
312 is mainly important for locating the new area the posterior distribution
313 is concentrated at. The second phase (dominated by whole sample population)
314 is actually relevant for the critical rate estimation.
315
316 Caveats
317 ```````
318
319 Current implementation does not constrict the critical rate
320 (as computed for every sample) to the min_rate, max_rate interval.
321
322 Earlier implementations were targeting loss rate (as opposed to loss ratio).
323 The chosen fitting functions do allow arbitrarily low loss ratios,
324 but may suffer from rounding errors in corresponding parameter regions.
325 Internal loss rate target is computed from given loss ratio
326 using the current trial offered load, which increases search instability,
327 especially if measurements with surprisingly high loss count appear.
328
329 As high loss count measurements add many bits of information,
330 they need a large amount of small loss count measurements to balance them,
331 making the algorithm converge quite slowly.
332
333 Some systems evidently do not follow the assumption of repeated measurements
334 having the same average loss rate (when offered load is the same).
335 The idea of estimating the trend is not implemented at all,
336 as the observed trends have varied characteristics.
337
338 Probably, using a more realistic fitting functions
339 will give better estimates than trend analysis.
340
341 Graphical examples
342 ``````````````````
343
344 The following pictures show the upper and lower bound (one sigma)
345 on estimated critical rate, as computed by PLRsearch, after each trial measurement
346 within the 30 minute duration of a test run.
347
348 Both graphs are focusing on later estimates. Estimates computed from
349 few initial measurements are wildly off the y-axis range shown.
350
351 L2 patch
352 --------
353
354 This test case shows quite narrow critical region. Both fitting functions
355 give similar estimates, the graph shows the randomness of measurements,
356 and a trend. Both fitting functions seem to be somewhat overestimating
357 the critical rate. The final estimated interval is too narrow,
358 a longer run would report estimates somewhat bellow the current lower bound.
359
360 .. only:: latex
361
362     .. raw:: latex
363
364         \begin{figure}[H]
365             \centering
366                 \graphicspath{{../_tmp/src/introduction/}}
367                 \includegraphics[width=0.90\textwidth]{PLR_patch}
368                 \label{fig:PLR_patch}
369         \end{figure}
370
371 .. only:: html
372
373     .. figure:: PLR_patch.svg
374         :alt: PLR_patch
375         :align: center
376
377 Vhost
378 -----
379
380 This test case shows quite broad critical region. Fitting functions give
381 fairly differing estimates. One overestimates, the other underestimates.
382 The graph mostly shows later measurements slowly bringing the estimates
383 towards each other. The final estimated interval is too broad,
384 a longer run would return a smaller interval within the current one.
385
386 .. only:: latex
387
388     .. raw:: latex
389
390         \begin{figure}[H]
391             \centering
392                 \graphicspath{{../_tmp/src/introduction/}}
393                 \includegraphics[width=0.90\textwidth]{PLR_vhost}
394                 \label{fig:PLR_vhost}
395         \end{figure}
396
397 .. only:: html
398
399     .. figure:: PLR_vhost.svg
400         :alt: PLR_vhost
401         :align: center
402
403 .. _plrsearch draft: https://tools.ietf.org/html/draft-vpolak-bmwg-plrsearch-00
404 .. _RFC 2544: https://tools.ietf.org/html/rfc2544
405 .. _Bayesian inference: https://en.wikipedia.org/wiki/Bayesian_statistics
406 .. _Poisson distribution: https://en.wikipedia.org/wiki/Poisson_distribution
407 .. _Binomial distribution: https://en.wikipedia.org/wiki/Binomial_distribution
408 .. _primitive function: https://en.wikipedia.org/wiki/Antiderivative
409 .. _logistic function: https://en.wikipedia.org/wiki/Logistic_function
410 .. _Gaussian function: https://en.wikipedia.org/wiki/Gaussian_function
411 .. _Lomax distribution: https://en.wikipedia.org/wiki/Lomax_distribution
412 .. _reciprocal distribution: https://en.wikipedia.org/wiki/Reciprocal_distribution
413 .. _Monte Carlo: https://en.wikipedia.org/wiki/Monte_Carlo_integration
414 .. _importance sampling: https://en.wikipedia.org/wiki/Importance_sampling
415 .. _bivariate Gaussian: https://en.wikipedia.org/wiki/Multivariate_normal_distribution