NCBI C++ ToolKit
srv_stat.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef NETCACHE__SRV_STAT__HPP
2 #define NETCACHE__SRV_STAT__HPP
3 /* $Id: srv_stat.hpp 80816 2018-01-11 16:39:57Z gouriano $
4  * ===========================================================================
5  *
6  * PUBLIC DOMAIN NOTICE
7  * National Center for Biotechnology Information
8  *
9  * This software/database is a "United States Government Work" under the
10  * terms of the United States Copyright Act. It was written as part of
11  * the author's official duties as a United States Government employee and
12  * thus cannot be copyrighted. This software/database is freely available
13  * to the public for use. The National Library of Medicine and the U.S.
14  * Government have not placed any restriction on its use or reproduction.
15  *
16  * Although all reasonable efforts have been taken to ensure the accuracy
17  * and reliability of the software and data, the NLM and the U.S.
18  * Government do not and cannot warrant the performance or results that
19  * may be obtained by using this software or data. The NLM and the U.S.
20  * Government disclaim all warranties, express or implied, including
21  * warranties of performance, merchantability or fitness for any particular
22  * purpose.
23  *
24  * Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Authors: Pavel Ivanov
29  *
30  * File Description:
31  */
32 
33 
35 
36 
37 struct SMMStat;
38 
39 
40 /// Stream-like class to accumulate all in one string without actual
41 /// streams-related overhead. Can be automatically converted to string or
42 /// CTempString.
44 {
45 public:
46  /// Clear the accumulated value and start from the beginning
47  void Clear(void);
48 
49  /// Streaming operator for all supported data types
50  CSrvStrStream& operator<<(const string& str);
51  CSrvStrStream& operator<<(const char* str);
54  CSrvStrStream& operator<<(unsigned int ui);
55  CSrvStrStream& operator<<(long l);
56  CSrvStrStream& operator<<(unsigned long ul);
57 #if !NCBI_INT8_IS_LONG
60 #endif
61  CSrvStrStream& operator<<(double d);
63 
64  /// Automatic conversion to string
65  operator const string& (void) const;
66  /// Automatic conversion to CTempString
67  operator CTempString(void) const;
68 
69 private:
70  /// Accumulated data
71  string m_Data;
72 };
73 
74 
75 /// Stream-like class to help unify printing some text messages to diagnostics
76 /// and to any socket. Class accumulates line of text end then flushes it
77 /// to diagnostics by INFO or to iostream by operator<<. End of line is
78 /// recognized only by streaming std::endl into this class. Other text is not
79 /// analyzed for characters '\n' or alike. In case of printing to diagnostics
80 /// all text is printed under the same request number and it's assumed that
81 /// no other output gets to diagnostics between object creation and deletion.
82 /// Otherwise other output will interfere with output from this object. Also
83 /// it's assumed that object is used from the same thread where it's created
84 /// and deleted.
86 {
87 public:
88  /// Type of media to print to
89  enum EPrintOutput {
90  ePrintLog, ///< Print to diagnostics
91  ePrintSocket ///< Print to socket
92  };
93 
96 
97  ~CSrvPrintProxy(void);
98 
99  /// Universal streaming operator
100  template <class T>
102 
103 
104  typedef CNcbiOstream& (*TEndlType)(CNcbiOstream&);
105 
106  /// Special streaming operator for std::endl
108 
109 private:
112 
113 
114  /// Type of media to print to
121 };
122 
123 
124 /// Class representing one statistical value.
125 /// Object collects set of values and can return number of values in set,
126 /// sum of all values, maximum value and average of all values.
127 template <class T>
129 {
130 public:
131  /// Empty constructor, all initialization should be made in Initialize()
132  /// because in memory manager static objects are used before constructors
133  /// are called.
134  CSrvStatTerm (void);
135  /// Initialize all data members
136  void Initialize (void);
137 
138  /// Add next value into the set.
139  void AddValue (T value);
140 
141  /// Get number of values in the set.
142  Uint8 GetCount (void) const;
143  /// Get sum of all values in the set.
144  /// The return type is Uint8 because the class is not used with doubles.
145  /// If it ever will collect doubles the return type should be adjusted
146  /// smartly (to be double for T==double and Uint8 for all unsigned integers).
147  Uint8 GetSum (void) const;
148  /// Get maximum value in the set.
149  T GetMaximum(void) const;
150  /// Get average of all values in the set.
151  T GetAverage(void) const;
152  double GetDoubleAvg(void) const;
153 
154  /// Add all values from another set.
155  void AddValues (const CSrvStatTerm<T>& other);
156 
157 private:
158  /// Sum of all values collected.
159  /// The type is Uint8 because the class is not used with doubles. If it
160  /// ever will collect doubles the type of m_ValueSum should be adjusted
161  /// smartly (to be double for T==double and Uint8 for all unsigned integers).
163  /// Number of all values collected.
165  /// Maximum value among collected.
167 };
168 
169 
171 
172 template <class Map, class Key>
173 inline TSrvTimeTerm&
175 {
176  typename Map::iterator it_time = map.find(key);
177  if (it_time != map.end())
178  return it_time->second;
180  value.Initialize();
181  return value;
182 }
183 
184 
186 {
188  }
191 };
192 
193 
194 class CSrvStat
195 {
196 public:
197  CSrvStat(void);
198  ~CSrvStat(void);
199 
200  void InitStartState(void);
201  void TransferEndState(CSrvStat* src_stat);
202  void CopyStartState(CSrvStat* src_stat);
203  void CopyEndState(CSrvStat* src_stat);
204  void SaveEndState(void);
205  void AddAllStats(CSrvStat* src_stat);
206  void CollectThreads(bool need_clear);
207  void SaveEndStateStat(void);
208 
210  void PrintToSocket(CSrvPrintProxy& proxy);
211  void PrintState(CSrvSocketTask& sock);
212 
213 public:
214  void SchedJiffyStat(Uint8 jiffy_len, Uint8 max_slice,
215  Uint8 exec_time, Uint8 done_tasks, Uint8 wait_time);
216  void ThreadStarted(void);
217  void ThreadStopped(void);
218  void SockOpenActive(void);
219  void SockOpenPassive(void);
220  void SockClose(int status, Uint8 open_time);
221  void ErrorOnSocket(void);
222 
223  void SetMMStat(SMMStat* stat);
224 
225 private:
228 
229  void x_ClearStats(void);
230  void x_AddStats(CSrvStat* src_stat);
231  void x_PrintUnstructured(CSrvPrintProxy& proxy);
232 
233 
235 
236 
254  unique_ptr<SMMStat> m_MMStat;
255 };
256 
257 
258 inline Uint8
260 {
261  return total == 0? 0: val * 100 / total;
262 }
263 
264 #if NCBI_HAVE_CXX11
265 template<typename T>
268 {
271 }
272 
273 template<typename T>
276 {
277  if (size < 0) {
278  return string("-") + g_ToSizeStr(Uint8(-size));
279  } else {
280  return g_ToSizeStr(Uint8(size));
281  }
282 }
283 
284 #else
285 
286 inline string
287 _impl_g_ToSizeStr(Uint8 size)
288 {
291 }
292 
293 template<typename T> inline string
295 {
296  if (size < 0) {
297  return string("-") + _impl_g_ToSizeStr(Uint8(-size));
298  } else {
299  return _impl_g_ToSizeStr(Uint8(size));
300  }
301 }
302 #endif
303 
304 template<typename T>
305 inline string
307 {
309 }
310 
311 inline double
312 g_AsMSecStat(Uint8 time_usec)
313 {
314  return double(time_usec) / kUSecsPerMSec;
315 }
316 
317 
318 /// Get integer part of the logarithm with base 2.
319 /// In other words function returns index of the greatest bit set when bits
320 /// are indexed from lowest to highest starting with 0 (e.g. for binary number
321 /// 10010001 it will return 7).
322 inline unsigned int
324 {
325  unsigned int result = 0;
326  if (value > 0xFFFFFFFF) {
327  value >>= 32;
328  result += 32;
329  }
330  if (value > 0xFFFF) {
331  value >>= 16;
332  result += 16;
333  }
334  if (value > 0xFF) {
335  value >>= 8;
336  result += 8;
337  }
338  if (value > 0xF) {
339  value >>= 4;
340  result += 4;
341  }
342  if (value > 0x3) {
343  value >>= 2;
344  result += 2;
345  }
346  if (value > 0x1)
347  ++result;
348  return result;
349 }
350 
351 
352 
353 //////////////////////////////////////////////////////////////////////////
354 // Inline methods
355 //////////////////////////////////////////////////////////////////////////
356 
357 inline void
359 {
360  m_Data.clear();
361 }
362 
364 CSrvStrStream::operator<<(const string& str)
365 {
366  m_Data += str;
367  return *this;
368 }
369 
371 CSrvStrStream::operator<<(const char* str)
372 {
373  m_Data += str;
374  return *this;
375 }
376 
379 {
380  m_Data.append(str.data(), str.size());
381  return *this;
382 }
383 
386 {
388  return *this;
389 }
390 
392 CSrvStrStream::operator<<(unsigned int ui)
393 {
394  m_Data += NStr::UIntToString(ui);
395  return *this;
396 }
397 
400 {
402  return *this;
403 }
404 
406 CSrvStrStream::operator<<(unsigned long ul)
407 {
409  return *this;
410 }
411 
412 #if !NCBI_INT8_IS_LONG
413 
416 {
417  m_Data += NStr::Int8ToString(i64);
418  return *this;
419 }
420 
423 {
424  m_Data += NStr::UInt8ToString(ui64);
425  return *this;
426 }
427 
428 #endif
429 
432 {
434  return *this;
435 }
436 
439 {
440  m_Data += str.m_Data;
441  return *this;
442 }
443 
444 inline
445 CSrvStrStream::operator const string& (void) const
446 {
447  return m_Data;
448 }
449 
450 inline
451 CSrvStrStream::operator CTempString(void) const
452 {
453  return m_Data;
454 }
455 
456 
458 operator<<(CNcbiOstream& stream, const CSrvStrStream& str)
459 {
460  stream << string(str);
461  return stream;
462 }
463 
464 
465 
466 inline
468  : m_PrintMode(ePrintSocket),
469  m_LineStarted(false),
470  m_Socket(sock)
471 {}
472 
473 inline
475  : m_PrintMode(ePrintLog),
476  m_LineStarted(false),
477  m_DiagCtx(ctx)
478 {}
479 
480 inline
482 {}
483 
484 template <class T>
487 {
488  switch (m_PrintMode) {
489  case ePrintLog:
490  if (!m_LineStarted) {
492  m_LineStarted = true;
493  }
494  m_DiagMsg << x;
495  break;
496  case ePrintSocket:
497  m_StrStream << x;
498  break;
499  }
500  return *this;
501 }
502 
505 {
506  switch (m_PrintMode) {
507  case ePrintLog:
508  if (m_LineStarted) {
509  m_DiagMsg.Flush();
510  m_LineStarted = false;
511  }
512  break;
513  case ePrintSocket:
514  m_Socket->WriteText(string(m_StrStream)).WriteText("\n");
515  m_StrStream.Clear();
516  break;
517  }
518 
519  return *this;
520 }
521 
522 
523 template <class T>
524 inline
526 {}
527 
528 template <class T>
529 inline void
531 {
532  m_ValuesSum = 0;
533  m_ValuesCount = 0;
534  m_ValuesMax = 0;
535 }
536 
537 template <class T>
538 inline void
540 {
541  m_ValuesSum += value;
542  ++m_ValuesCount;
543  m_ValuesMax = max(m_ValuesMax, value);
544 }
545 
546 template <class T>
547 inline void
549 {
550  m_ValuesSum += other.m_ValuesSum;
551  m_ValuesCount += other.m_ValuesCount;
552  m_ValuesMax = max(other.m_ValuesMax, m_ValuesMax);
553 }
554 
555 template <class T>
556 inline Uint8
558 {
559  return m_ValuesCount;
560 }
561 
562 template <class T>
563 inline Uint8
565 {
566  return m_ValuesSum;
567 }
568 
569 template <class T>
570 inline T
572 {
573  return m_ValuesMax;
574 }
575 
576 template <class T>
577 inline T
579 {
580  return m_ValuesCount == 0? 0: T(m_ValuesSum / m_ValuesCount);
581 }
582 
583 template <class T>
584 inline double
586 {
587  return m_ValuesCount == 0? 0: double(m_ValuesSum) / m_ValuesCount;
588 }
589 
591 
592 #endif /* NETCACHE__SRV_STAT__HPP */
Mutex created to have minimum possible size (its size is 4 bytes) and to sleep using kernel capabilit...
Definition: srv_sync.hpp:193
Class used in all diagnostic logging.
Definition: srv_diag.hpp:73
const CSrvDiagMsg & StartInfo(void) const
Starts informational message which doesn't need to have filename, line number or function name.
Definition: logging.cpp:953
void Flush(void)
Finishes current message and prepare to start new one.
Definition: logging.cpp:1116
Stream-like class to help unify printing some text messages to diagnostics and to any socket.
Definition: srv_stat.hpp:86
CSrvPrintProxy(const CSrvPrintProxy &)
CSrvSocketTask * m_Socket
Definition: srv_stat.hpp:118
CSrvPrintProxy & operator<<(T x)
Universal streaming operator.
Definition: srv_stat.hpp:486
EPrintOutput m_PrintMode
Type of media to print to.
Definition: srv_stat.hpp:115
CSrvPrintProxy & operator=(const CSrvPrintProxy &)
CNcbiOstream &(* TEndlType)(CNcbiOstream &)
Definition: srv_stat.hpp:104
CSrvPrintProxy(CSrvSocketTask *sock)
Definition: srv_stat.hpp:467
CRequestContext * m_DiagCtx
Definition: srv_stat.hpp:120
bool m_LineStarted
Definition: srv_stat.hpp:116
~CSrvPrintProxy(void)
Definition: srv_stat.hpp:481
CSrvDiagMsg m_DiagMsg
Definition: srv_stat.hpp:119
EPrintOutput
Type of media to print to.
Definition: srv_stat.hpp:89
@ ePrintSocket
Print to socket.
Definition: srv_stat.hpp:91
@ ePrintLog
Print to diagnostics.
Definition: srv_stat.hpp:90
CSrvStrStream m_StrStream
Definition: srv_stat.hpp:117
Task controlling a socket.
CSrvSocketTask & WriteText(CTempString message)
Write text into socket.
Class representing one statistical value.
Definition: srv_stat.hpp:129
T GetAverage(void) const
Get average of all values in the set.
Definition: srv_stat.hpp:578
Uint8 GetCount(void) const
Get number of values in the set.
Definition: srv_stat.hpp:557
Uint8 m_ValuesCount
Number of all values collected.
Definition: srv_stat.hpp:164
void Initialize(void)
Initialize all data members.
Definition: srv_stat.hpp:530
Uint8 GetSum(void) const
Get sum of all values in the set.
Definition: srv_stat.hpp:564
T m_ValuesMax
Maximum value among collected.
Definition: srv_stat.hpp:166
void AddValue(T value)
Add next value into the set.
Definition: srv_stat.hpp:539
Uint8 m_ValuesSum
Sum of all values collected.
Definition: srv_stat.hpp:162
void AddValues(const CSrvStatTerm< T > &other)
Add all values from another set.
Definition: srv_stat.hpp:548
T GetMaximum(void) const
Get maximum value in the set.
Definition: srv_stat.hpp:571
CSrvStatTerm(void)
Empty constructor, all initialization should be made in Initialize() because in memory manager static...
Definition: srv_stat.hpp:525
double GetDoubleAvg(void) const
Definition: srv_stat.hpp:585
void x_ClearStats(void)
Definition: srv_stat.cpp:71
Uint8 m_ExecTime
Definition: srv_stat.hpp:242
TSrvTimeTerm m_JiffyTime
Definition: srv_stat.hpp:240
SSrvStateStat m_EndState
Definition: srv_stat.hpp:239
TStatusOpenTimes m_SocksByStatus
Definition: srv_stat.hpp:251
CSrvStat & operator=(const CSrvStat &)
SSrvStateStat m_StartState
Definition: srv_stat.hpp:238
~CSrvStat(void)
Definition: srv_stat.cpp:61
void CopyStartState(CSrvStat *src_stat)
Definition: srv_stat.cpp:106
void PrintState(CSrvSocketTask &sock)
Definition: srv_stat.cpp:290
void TransferEndState(CSrvStat *src_stat)
Definition: srv_stat.cpp:99
void SetMMStat(SMMStat *stat)
Definition: srv_stat.cpp:65
CSrvStat(const CSrvStat &)
void PrintToLogs(CRequestContext *ctx, CSrvPrintProxy &proxy)
Definition: srv_stat.cpp:251
Uint8 m_ThrStarted
Definition: srv_stat.hpp:245
CMiniMutex m_StatLock
Definition: srv_stat.hpp:237
void AddAllStats(CSrvStat *src_stat)
Definition: srv_stat.cpp:151
CSrvStatTerm< Uint4 > m_CntSockets
Definition: srv_stat.hpp:253
Uint8 m_SockActiveOpens
Definition: srv_stat.hpp:247
Uint8 m_MaxSlice
Definition: srv_stat.hpp:241
void ThreadStopped(void)
Definition: srv_stat.cpp:201
CSrvStatTerm< Uint4 > m_CntThreads
Definition: srv_stat.hpp:252
void SockOpenActive(void)
Definition: srv_stat.cpp:207
void SockClose(int status, Uint8 open_time)
Definition: srv_stat.cpp:219
void InitStartState(void)
Definition: srv_stat.cpp:91
CSrvStatTerm< Uint8 > m_DoneTasks
Definition: srv_stat.hpp:244
TSrvTimeTerm m_SockOpenTime
Definition: srv_stat.hpp:250
void SockOpenPassive(void)
Definition: srv_stat.cpp:213
void SaveEndState(void)
Definition: srv_stat.cpp:120
void x_PrintUnstructured(CSrvPrintProxy &proxy)
Definition: srv_stat.cpp:235
CSrvStat(void)
Definition: srv_stat.cpp:55
void x_AddStats(CSrvStat *src_stat)
Definition: srv_stat.cpp:127
map< int, TSrvTimeTerm > TStatusOpenTimes
Definition: srv_stat.hpp:234
void ThreadStarted(void)
Definition: srv_stat.cpp:195
void SchedJiffyStat(Uint8 jiffy_len, Uint8 max_slice, Uint8 exec_time, Uint8 done_tasks, Uint8 wait_time)
Definition: srv_stat.cpp:181
void CopyEndState(CSrvStat *src_stat)
Definition: srv_stat.cpp:113
void ErrorOnSocket(void)
Definition: srv_stat.cpp:229
void PrintToSocket(CSrvPrintProxy &proxy)
Definition: srv_stat.cpp:299
Uint8 m_WaitTime
Definition: srv_stat.hpp:243
Uint8 m_SockPassiveOpens
Definition: srv_stat.hpp:248
unique_ptr< SMMStat > m_MMStat
Definition: srv_stat.hpp:254
void CollectThreads(bool need_clear)
Definition: srv_stat.cpp:157
Uint8 m_ThrStopped
Definition: srv_stat.hpp:246
void SaveEndStateStat(void)
Definition: srv_stat.cpp:170
Uint8 m_SockErrors
Definition: srv_stat.hpp:249
Stream-like class to accumulate all in one string without actual streams-related overhead.
Definition: srv_stat.hpp:44
CSrvStrStream & operator<<(const string &str)
Streaming operator for all supported data types.
Definition: srv_stat.hpp:364
string m_Data
Accumulated data.
Definition: srv_stat.hpp:71
void Clear(void)
Clear the accumulated value and start from the beginning.
Definition: srv_stat.hpp:358
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
const_iterator end() const
Definition: map.hpp:152
const_iterator find(const key_type &key) const
Definition: map.hpp:153
Definition: map.hpp:338
#define T(s)
Definition: common.h:230
CS_CONTEXT * ctx
Definition: t0006.c:12
#define false
Definition: bool.h:36
static const char * str(char *buf, int n)
Definition: stats.c:84
CRange< Position > Map(const CRange< Position > &target, const CRange< Position > &range)
Definition: blast_aux.cpp:826
string
Definition: cgiapp.hpp:687
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
static string Int8ToString(Int8 value, TNumToStringFlags flags=0, int base=10)
Convert Int8 to string.
Definition: ncbistr.hpp:5159
static string DoubleToString(double value, int precision=-1, TNumToStringFlags flags=0)
Convert double to string.
Definition: ncbistr.hpp:5187
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
static string UInt8ToString_DataSize(Uint8 value, TNumToStringFlags flags=0, unsigned int max_digits=3)
Convert UInt8 to string using "software" qualifiers.
Definition: ncbistr.hpp:5177
static string UIntToString(unsigned int value, TNumToStringFlags flags=0, int base=10)
Convert UInt to string.
Definition: ncbistr.hpp:5109
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
static string UInt8ToString(Uint8 value, TNumToStringFlags flags=0, int base=10)
Convert UInt8 to string.
Definition: ncbistr.hpp:5168
@ fDS_PutBSuffixToo
UInt8ToString_DataSize(): Use "B" suffix for small bytes values.
Definition: ncbistr.hpp:264
@ fWithCommas
Use commas as thousands separator.
Definition: ncbistr.hpp:254
@ fDS_PutSpaceBeforeSuffix
UInt8ToString_DataSize(): Add space between value and qualifiers, like "10.0 KB".
Definition: ncbistr.hpp:262
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
const struct ncbi::grid::netcache::search::fields::KEY key
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
T max(T x_, T y_)
CSrvStatTerm< Uint8 > TSrvTimeTerm
Definition: srv_stat.hpp:170
Uint8 g_CalcStatPct(Uint8 val, Uint8 total)
Definition: srv_stat.hpp:259
std::enable_if< std::is_unsigned< T >::value, string >::type g_ToSizeStr(T size)
Definition: srv_stat.hpp:267
string g_ToSmartStr(T num)
Definition: srv_stat.hpp:306
double g_AsMSecStat(Uint8 time_usec)
Definition: srv_stat.hpp:312
CNcbiOstream & operator<<(CNcbiOstream &stream, const CSrvStrStream &str)
Definition: srv_stat.hpp:458
unsigned int g_GetLogBase2(Uint8 value)
Get integer part of the logarithm with base 2.
Definition: srv_stat.hpp:323
TSrvTimeTerm & g_SrvTimeTerm(Map &map, Key key)
Definition: srv_stat.hpp:174
@ kUSecsPerMSec
Definition: srv_time.hpp:44
Uint4 cnt_threads
Definition: srv_stat.hpp:189
Uint4 cnt_sockets
Definition: srv_stat.hpp:190
SSrvStateStat(void)
Definition: srv_stat.hpp:187
Definition: type.c:6
else result
Definition: token2.c:20
Modified on Tue Apr 23 07:37:22 2024 by modify_doxy.py rev. 669887