summaryrefslogtreecommitdiff
path: root/ace/Stats.h
blob: 83feaea39399b391d41a326c4e0d2d6093e3a868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* -*- C++ -*- */
// $Id$

// ============================================================================
//
// = LIBRARY
//    ace
//
// = FILENAME
//    Stats.h
//
// = AUTHORS
//    David L. Levine
//
// ============================================================================

#ifndef ACE_STATS_H
#define ACE_STATS_H

#include "ace/ACE.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

#include "ace/Containers.h"

class ACE_Export ACE_Stats_Value
{
  // = TITLE
  //     Helper class for ACE_Stats.
  //
  // = DESCRIPTION
  //     Container struct for 64-bit signed quantity and its
  //     precision.  It would be nicer to use a fixed-point class, but
  //     this is sufficient.  Users typically don't need to use this
  //     class directly; see ACE_Stats below.
public:
  ACE_Stats_Value (const u_int precision);
  // Constructor, which requires precision in terms of number of
  // decimal digits.  The more variation in the data, and the greater
  // the data values, the smaller the precision must be to avoid
  // overflow in the standard deviation calculation.  3 might be a
  // good value, or maybe 4.  5 will probably be too large for
  // non-trivial data sets.

  u_int precision (void) const;
  // Accessor for precision.

  void whole (const ACE_UINT32);
  // Set the whole_ field.

  ACE_UINT32 whole (void) const;
  // Accessor for the whole_ field.

  void fractional (const ACE_UINT32);
  // Set the fractional_ field.

  ACE_UINT32 fractional (void) const;
  // Accessor for the fractional_ field.

  ACE_UINT32 fractional_field (void) const;
  // Calculates the maximum value of the fractional portion, given its
  // precision.

  void scaled_value (ACE_UINT64 &) const;
  // Access the value as an _unsigned_ 64 bit quantity.  It scales the
  // value up by <precision> decimal digits, so that no precision will
  // be lost.  It assumes that <whole_> is >= 0.

  void dump (void) const;
  // Print to stdout.

private:
  ACE_UINT32 whole_;
  // The integer portion of the value.

  ACE_UINT32 fractional_;
  // The fractional portion of the value.

  u_int precision_;
  // The number of decimal digits of precision represented by
  // <fractional_>.  Not declared const, so the only way to change it
  // is via the assignment operator.

  ACE_UNIMPLEMENTED_FUNC (ACE_Stats_Value (void))
};

class ACE_Export ACE_Stats
{
  // = TITLE
  //     Provides simple statistical analysis.
  //
  // = DESCRIPTION
  //     Simple statistical analysis package.  Prominent features are:
  //     1) It does not use any floating point arithmetic.
  //     2) It handles positive and/or negative sample values.  The
  //        sample value type is ACE_INT32.
  //     3) It uses 64 bit unsigned, but not 64 bit signed, quantities
  //        internally.
  //     4) It checks for overflow of internal state.
  //     5) It has no static variables of other than built-in types.
  //
  //     Example usage:
  //       ACE_Stats stats;
  //       for (u_int i = 0; i < n; ++i)
  //         {
  //           const ACE_UINT32 sample = /* ... */;
  //           stats.sample (sample);
  //         }
  //       stats.print_summary (3);
public:
  ACE_Stats (void);
  // Default constructor.

  int sample (const ACE_INT32 value);
  // Provide a new sample.  Returns 0 on success, -1 if it fails due
  // to running out of memory, or to rolling over of the sample count.

  ACE_UINT32 samples (void) const;
  // Access the number of samples provided so far.

  ACE_INT32 min_value (void) const;
  // Value of the minimum sample provided so far.

  ACE_INT32 max_value (void) const;
  // Value of the maximum sample provided so far.

  void mean (ACE_Stats_Value &mean,
             const ACE_UINT32 scale_factor = 1);
  // Access the mean of all samples provided so far.  The fractional
  // part is to the specified number of digits.  E.g., 3 fractional
  // digits specifies that the fractional part is in thousandths.

  int std_dev (ACE_Stats_Value &std_dev,
               const ACE_UINT32 scale_factor = 1);
  // Access the standard deviation, whole and fractional parts.  See
  // description of <mean> method for argument descriptions.

  int print_summary (const u_int precision,
                     const ACE_UINT32 scale_factor = 1,
                     FILE * = stdout) const;
  // Print summary statistics.  If scale_factor is not 1, then the
  // results are divided by it, i.e., each of the samples is scaled
  // down by it.  If internal overflow is reached with the specified
  // scale factor, it successively tries to reduce it.  Returns -1 if
  // there is overflow even with a 0 scale factor.

  void reset ();
  // Initialize internal state.

  static void quotient (const ACE_UINT64 dividend,
                        const ACE_UINT32 divisor,
                        ACE_Stats_Value &quotient);
  // Utility division function, for ACE_UINT64 dividend.

  static void quotient (const ACE_Stats_Value &dividend,
                        const ACE_UINT32 divisor,
                        ACE_Stats_Value &quotient);
  // Utility division function, for ACE_Stats_Value dividend.

  static void square_root (const ACE_UINT64 n,
                           ACE_Stats_Value &square_root);
  // Sqrt function, which uses an oversimplified version of Newton's
  // method.  It's not fast, but it doesn't require floating point
  // support.

  void dump (void) const;
  // Print summary statistics to stdout.

private:
  u_int overflow_;
  // Internal indication of whether there has been overflow.  Contains
  // the errno corresponding to the cause of overflow.

  ACE_UINT32 number_of_samples_;
  // Number of samples.

  ACE_INT32 min_;
  // Minimum sample value.

  ACE_INT32 max_;
  // Maximum sample value.

  ACE_Unbounded_Queue <ACE_INT32> samples_;
  // The samples.
};

// ****************************************************************

class ACE_Export ACE_Throughput_Stats
{
  // = TITLE
  //   A simple class to make throughput and latency analysis.
  //
  // = DESCRIPTION
  //   Keep the relevant information to perform throughput and latency
  //   analysis, including:
  //   1) Minimum, Average and Maximum latency
  //   2) Jitter for the latency
  //   3) Linear regression for throughput
  //   4) Accumulate results from several samples to obtain aggregated
  //      results, across several threads or experiments.
  //
public:
  ACE_Throughput_Stats (void);
  // Default constructor.

  void sample (ACE_UINT64 throughput, ACE_UINT64 latency);
  // Store one sample

  void accumulate (const ACE_Throughput_Stats &throughput);
  // Update the values to reflect the stats in <throughput>

  void dump_results (const ASYS_TCHAR* msg, ACE_UINT32 scale_factor);
  // Print down the stats

private:
  ACE_UINT64 samples_count_;
  // The number of samples

  ACE_UINT64 latency_min_;
  ACE_UINT64 latency_max_;
  ACE_UINT64 latency_sum_;
  ACE_UINT64 latency_sum2_;
  // The stadigraphs for latency computation

  ACE_UINT64 throughput_last_;
  ACE_UINT64 throughput_sum_x_;
  ACE_UINT64 throughput_sum_x2_;
  ACE_UINT64 throughput_sum_y_;
  ACE_UINT64 throughput_sum_y2_;
  ACE_UINT64 throughput_sum_xy_;
  // The stadigraphs for throughput computation
};


#if defined (__ACE_INLINE__)
# include "ace/Stats.i"
#endif /* __ACE_INLINE__ */

#endif /* ! ACE_STATS_H */