gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
window_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_STATISTICS_WINDOW_STATS_H
35 #define GRPC_INTERNAL_CORE_STATISTICS_WINDOW_STATS_H
36 
37 #include <grpc/support/time.h>
38 
39 /* Keep rolling sums of a user-defined statistic (containing a number of
40  measurements) over a a number of time intervals ("windows"). For example,
41  you can use a window_stats object to answer questions such as
42  "Approximately how many RPCs/s did I receive over the past minute, and
43  approximately how many bytes did I send out over that period?".
44 
45  The type of data to record, and the time intervals to keep are specified
46  when creating the object via a call to census_window_stats_create().
47 
48  A window's interval is divided into one or more "buckets"; the interval
49  must be divisible by the number of buckets. Internally, these buckets
50  control the granularity of window_stats' measurements. Increasing the
51  number of buckets lets the object respond more quickly to changes in the
52  overall rate of data added into the object, at the cost of additional
53  memory usage.
54 
55  Here's some code which keeps one minute/hour measurements for two values
56  (latency in seconds and bytes transferred), with each interval divided into
57  4 buckets.
58 
59  typedef struct my_stat {
60  double latency;
61  int bytes;
62  } my_stat;
63 
64  void add_my_stat(void* base, const void* addme) {
65  my_stat* b = (my_stat*)base;
66  const my_stat* a = (const my_stat*)addme;
67  b->latency += a->latency;
68  b->bytes += a->bytes;
69  }
70 
71  void add_proportion_my_stat(double p, void* base, const void* addme) {
72  (my_stat*)result->latency += p * (const my_stat*)base->latency;
73  (my_stat*)result->bytes += p * (const my_stat*)base->bytes;
74  }
75 
76  #define kNumIntervals 2
77  #define kMinInterval 0
78  #define kHourInterval 1
79  #define kNumBuckets 4
80 
81  const struct census_window_stats_stat_info kMyStatInfo
82  = { sizeof(my_stat), NULL, add_my_stat, add_proportion_my_stat };
83  gpr_timespec intervals[kNumIntervals] = {{60, 0}, {3600, 0}};
84  my_stat stat;
85  my_stat sums[kNumIntervals];
86  census_window_stats_sums result[kNumIntervals];
87  struct census_window_stats* stats
88  = census_window_stats_create(kNumIntervals, intervals, kNumBuckets,
89  &kMyStatInfo);
90  // Record a new event, taking 15.3ms, transferring 1784 bytes.
91  stat.latency = 0.153;
92  stat.bytes = 1784;
93  census_window_stats_add(stats, gpr_now(), &stat);
94  // Get sums and print them out
95  result[kMinInterval].statistic = &sums[kMinInterval];
96  result[kHourInterval].statistic = &sums[kHourInterval];
97  census_window_stats_get_sums(stats, gpr_now(), result);
98  printf("%d events/min, average time %gs, average bytes %g\n",
99  result[kMinInterval].count,
100  (my_stat*)result[kMinInterval].statistic->latency /
101  result[kMinInterval].count,
102  (my_stat*)result[kMinInterval].statistic->bytes /
103  result[kMinInterval].count
104  );
105  printf("%d events/hr, average time %gs, average bytes %g\n",
106  result[kHourInterval].count,
107  (my_stat*)result[kHourInterval].statistic->latency /
108  result[kHourInterval].count,
109  (my_stat*)result[kHourInterval].statistic->bytes /
110  result[kHourInterval].count
111  );
112 */
113 
114 /* Opaque structure for representing window_stats object */
115 struct census_window_stats;
116 
117 /* Information provided by API user on the information they want to record */
119  /* Number of bytes in user-defined object. */
120  size_t stat_size;
121  /* Function to initialize a user-defined statistics object. If this is set
122  * to NULL, then the object will be zero-initialized. */
123  void (*stat_initialize)(void* stat);
124  /* Function to add one user-defined statistics object ('addme') to 'base' */
125  void (*stat_add)(void* base, const void* addme);
126  /* As for previous function, but only add a proportion 'p'. This API will
127  currently only use 'p' values in the range [0,1], but other values are
128  possible in the future, and should be supported. */
129  void (*stat_add_proportion)(double p, void* base, const void* addme);
131 
132 /* Create a new window_stats object. 'nintervals' is the number of
133  'intervals', and must be >=1. 'granularity' is the number of buckets, with
134  a larger number using more memory, but providing greater accuracy of
135  results. 'granularity should be > 2. We also require that each interval be
136  at least 10 * 'granularity' nanoseconds in size. 'stat_info' contains
137  information about the statistic to be gathered. Intervals greater than ~192
138  years will be treated as essentially infinite in size. This function will
139  GPR_ASSERT() if the object cannot be created or any of the parameters have
140  invalid values. This function is thread-safe. */
141 struct census_window_stats* census_window_stats_create(
142  int nintervals, const gpr_timespec intervals[], int granularity,
143  const census_window_stats_stat_info* stat_info);
144 
145 /* Add a new measurement (in 'stat_value'), as of a given time ('when').
146  This function is thread-compatible. */
147 void census_window_stats_add(struct census_window_stats* wstats,
148  const gpr_timespec when, const void* stat_value);
149 
150 /* Structure used to record a single intervals sum for a given statistic */
151 typedef struct census_window_stats_sum {
152  /* Total count of samples. Note that because some internal interpolation
153  is performed, the count of samples returned for each interval may not be an
154  integral value. */
155  double count;
156  /* Sum for statistic */
157  void* statistic;
159 
160 /* Retrieve a set of all values stored in a window_stats object 'wstats'. The
161  number of 'sums' MUST be the same as the number 'nintervals' used in
162  census_window_stats_create(). This function is thread-compatible. */
163 void census_window_stats_get_sums(const struct census_window_stats* wstats,
164  const gpr_timespec when,
165  struct census_window_stats_sum sums[]);
166 
167 /* Destroy a window_stats object. Once this function has been called, the
168  object will no longer be usable from any of the above functions (and
169  calling them will most likely result in a NULL-pointer dereference or
170  assertion failure). This function is thread-compatible. */
171 void census_window_stats_destroy(struct census_window_stats* wstats);
172 
173 #endif /* GRPC_INTERNAL_CORE_STATISTICS_WINDOW_STATS_H */
Definition: window_stats.c:69
Definition: window_stats.h:151
Definition: time.h:48
Definition: window_stats.h:118