• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/iomgr/time_averaged_stats.h"
22 
grpc_time_averaged_stats_init(grpc_time_averaged_stats * stats,double init_avg,double regress_weight,double persistence_factor)23 void grpc_time_averaged_stats_init(grpc_time_averaged_stats* stats,
24                                    double init_avg, double regress_weight,
25                                    double persistence_factor) {
26   stats->init_avg = init_avg;
27   stats->regress_weight = regress_weight;
28   stats->persistence_factor = persistence_factor;
29   stats->batch_total_value = 0;
30   stats->batch_num_samples = 0;
31   stats->aggregate_total_weight = 0;
32   stats->aggregate_weighted_avg = init_avg;
33 }
34 
grpc_time_averaged_stats_add_sample(grpc_time_averaged_stats * stats,double value)35 void grpc_time_averaged_stats_add_sample(grpc_time_averaged_stats* stats,
36                                          double value) {
37   stats->batch_total_value += value;
38   ++stats->batch_num_samples;
39 }
40 
grpc_time_averaged_stats_update_average(grpc_time_averaged_stats * stats)41 double grpc_time_averaged_stats_update_average(
42     grpc_time_averaged_stats* stats) {
43   /* Start with the current batch: */
44   double weighted_sum = stats->batch_total_value;
45   double total_weight = stats->batch_num_samples;
46   if (stats->regress_weight > 0) {
47     /* Add in the regression towards init_avg_: */
48     weighted_sum += stats->regress_weight * stats->init_avg;
49     total_weight += stats->regress_weight;
50   }
51   if (stats->persistence_factor > 0) {
52     /* Add in the persistence: */
53     const double prev_sample_weight =
54         stats->persistence_factor * stats->aggregate_total_weight;
55     weighted_sum += prev_sample_weight * stats->aggregate_weighted_avg;
56     total_weight += prev_sample_weight;
57   }
58   stats->aggregate_weighted_avg =
59       (total_weight > 0) ? (weighted_sum / total_weight) : stats->init_avg;
60   stats->aggregate_total_weight = total_weight;
61   stats->batch_num_samples = 0;
62   stats->batch_total_value = 0;
63   return stats->aggregate_weighted_avg;
64 }
65