3 //==========================================================================
7 * @author David L. Levine
9 //==========================================================================
15 #include /**/ "ace/pre.h"
17 #include /**/ "ace/ACE_export.h"
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
23 #include "ace/Unbounded_Queue.h"
24 #include "ace/Log_Category.h"
25 #include "ace/Basic_Stats.h"
27 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
30 * @class ACE_Stats_Value
32 * @brief Helper class for ACE_Stats.
34 * Container struct for 64-bit signed quantity and its
35 * precision. It would be nicer to use a fixed-point class, but
36 * this is sufficient. Users typically don't need to use this
37 * class directly; see ACE_Stats below.
39 class ACE_Export ACE_Stats_Value
43 * Constructor, which requires precision in terms of number of
44 * decimal digits. The more variation in the data, and the greater
45 * the data values, the smaller the precision must be to avoid
46 * overflow in the standard deviation calculation. 3 might be a
47 * good value, or maybe 4. 5 will probably be too large for
48 * non-trivial data sets.
50 ACE_Stats_Value (const u_int precision
);
52 /// Accessor for precision.
53 u_int
precision (void) const;
55 /// Set the whole_ field.
56 void whole (const ACE_UINT32
);
58 /// Accessor for the whole_ field.
59 ACE_UINT32
whole (void) const;
61 /// Set the fractional_ field.
62 void fractional (const ACE_UINT32
);
64 /// Accessor for the fractional_ field.
65 ACE_UINT32
fractional (void) const;
67 /// Calculates the maximum value of the fractional portion, given its
69 ACE_UINT32
fractional_field (void) const;
72 * Access the value as an _unsigned_ 64 bit quantity. It scales the
73 * value up by {precision} decimal digits, so that no precision will
74 * be lost. It assumes that {whole_} is >= 0.
76 void scaled_value (ACE_UINT64
&) const;
79 void dump (void) const;
83 ACE_Stats_Value (void) {}
86 /// The integer portion of the value.
89 /// The fractional portion of the value.
90 ACE_UINT32 fractional_
;
93 * The number of decimal digits of precision represented by
94 * {fractional_}. Not declared const, so the only way to change it
95 * is via the assignment operator.
104 * @brief Provides simple statistical analysis.
106 * Simple statistical analysis package. Prominent features are:
107 * -# It does not use any floating point arithmetic.
108 * -# It handles positive and/or negative sample values. The
109 * sample value type is ACE_INT32.
110 * -# It uses 64 bit unsigned, but not 64 bit signed, quantities
112 * -# It checks for overflow of internal state.
113 * -# It has no static variables of other than built-in types.
119 * for (u_int i = 0; i < n; ++i)
121 * const ACE_UINT32 sample = ...;
122 * stats.sample (sample);
124 * stats.print_summary (3);
127 class ACE_Export ACE_Stats
130 /// Default constructor.
133 /// Provide a new sample. Returns 0 on success, -1 if it fails due
134 /// to running out of memory, or to rolling over of the sample count.
135 int sample (const ACE_INT32 value
);
137 /// Access the number of samples provided so far.
138 ACE_UINT32
samples (void) const;
140 /// Value of the minimum sample provided so far.
141 ACE_INT32
min_value (void) const;
143 /// Value of the maximum sample provided so far.
144 ACE_INT32
max_value (void) const;
147 * Access the mean of all samples provided so far. The fractional
148 * part is to the specified number of digits. E.g., 3 fractional
149 * digits specifies that the fractional part is in thousandths.
151 void mean (ACE_Stats_Value
&mean
,
152 const ACE_UINT32 scale_factor
= 1);
154 /// Access the standard deviation, whole and fractional parts. See
155 /// description of {mean} method for argument descriptions.
156 int std_dev (ACE_Stats_Value
&std_dev
,
157 const ACE_UINT32 scale_factor
= 1);
160 * Print summary statistics. If scale_factor is not 1, then the
161 * results are divided by it, i.e., each of the samples is scaled
162 * down by it. If internal overflow is reached with the specified
163 * scale factor, it successively tries to reduce it. Returns -1 if
164 * there is overflow even with a 0 scale factor.
166 int print_summary (const u_int precision
,
167 const ACE_UINT32 scale_factor
= 1,
169 #ifdef ACE_LACKS_STDOUT
176 /// Initialize internal state.
179 /// Utility division function, for ACE_UINT64 dividend.
180 static void quotient (const ACE_UINT64 dividend
,
181 const ACE_UINT32 divisor
,
182 ACE_Stats_Value
"ient
);
184 /// Utility division function, for ACE_Stats_Value dividend.
185 static void quotient (const ACE_Stats_Value
÷nd
,
186 const ACE_UINT32 divisor
,
187 ACE_Stats_Value
"ient
);
190 * Sqrt function, which uses an oversimplified version of Newton's
191 * method. It's not fast, but it doesn't require floating point
194 static void square_root (const ACE_UINT64 n
,
195 ACE_Stats_Value
&square_root
);
197 /// Print summary statistics to stdout.
198 void dump (void) const;
201 /// Internal indication of whether there has been overflow. Contains
202 /// the errno corresponding to the cause of overflow.
205 /// Number of samples.
206 ACE_UINT32 number_of_samples_
;
208 /// Minimum sample value.
211 /// Maximum sample value.
215 ACE_Unbounded_Queue
<ACE_INT32
> samples_
;
218 ACE_END_VERSIONED_NAMESPACE_DECL
220 #if defined (__ACE_INLINE__)
221 # include "ace/Stats.inl"
222 #endif /* __ACE_INLINE__ */
224 #include /**/ "ace/post.h"
226 #endif /* ! ACE_STATS_H */