gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
time_averaged_stats.h
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_INTERNAL_CORE_IOMGR_TIME_AVERAGED_STATS_H
35 #define GRPC_INTERNAL_CORE_IOMGR_TIME_AVERAGED_STATS_H
36 
37 /* This tracks a time-decaying weighted average. It works by collecting
38  batches of samples and then mixing their average into a time-decaying
39  weighted mean. It is designed for batch operations where we do many adds
40  before updating the average. */
41 
42 typedef struct {
43  /* The initial average value. This is the reported average until the first
44  grpc_time_averaged_stats_update_average call. If a positive regress_weight
45  is used, we also regress towards this value on each update. */
46  double init_avg;
47  /* The sample weight of "init_avg" that is mixed in with each call to
48  grpc_time_averaged_stats_update_average. If the calls to
49  grpc_time_averaged_stats_add_sample stop, this will cause the average to
50  regress back to the mean. This should be non-negative. Set it to 0 to
51  disable the bias. A value of 1 has the effect of adding in 1 bonus sample
52  with value init_avg to each sample period. */
53  double regress_weight;
54  /* This determines the rate of decay of the time-averaging from one period
55  to the next by scaling the aggregate_total_weight of samples from prior
56  periods when combining with the latest period. It should be in the range
57  [0,1]. A higher value adapts more slowly. With a value of 0.5, if the
58  batches each have k samples, the samples_in_avg_ will grow to 2 k, so the
59  weighting of the time average will eventually be 1/3 new batch and 2/3
60  old average. */
61  double persistence_factor;
62 
63  /* The total value of samples since the last UpdateAverage(). */
64  double batch_total_value;
65  /* The number of samples since the last UpdateAverage(). */
66  double batch_num_samples;
67  /* The time-decayed sum of batch_num_samples_ over previous batches. This is
68  the "weight" of the old aggregate_weighted_avg_ when updating the
69  average. */
70  double aggregate_total_weight;
71  /* A time-decayed average of the (batch_total_value_ / batch_num_samples_),
72  computed by decaying the samples_in_avg_ weight in the weighted average. */
73  double aggregate_weighted_avg;
75 
76 /* See the comments on the members above for an explanation of init_avg,
77  regress_weight, and persistence_factor. */
78 void grpc_time_averaged_stats_init(grpc_time_averaged_stats *stats,
79  double init_avg, double regress_weight,
80  double persistence_factor);
81 /* Add a sample to the current batch. */
82 void grpc_time_averaged_stats_add_sample(grpc_time_averaged_stats *stats,
83  double value);
84 /* Complete a batch and compute the new estimate of the average sample
85  value. */
86 double grpc_time_averaged_stats_update_average(grpc_time_averaged_stats *stats);
87 
88 #endif /* GRPC_INTERNAL_CORE_IOMGR_TIME_AVERAGED_STATS_H */
Definition: time_averaged_stats.h:42