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

Go to the SVN repository for this file.

1 /* $Id: srv_stat.cpp 75004 2016-10-13 13:40:06Z gouriano $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Pavel Ivanov
27  *
28  */
29 
30 #include "task_server_pch.hpp"
31 
32 #include "srv_stat.hpp"
33 #include "threads_man.hpp"
34 #include "memory_man.hpp"
35 
36 
38 
39 
40 extern int s_TotalSockets;
41 extern int s_SoftSocketLimit;
42 extern int s_HardSocketLimit;
43 extern SSrvThread** s_Threads;
44 
45 
46 
47 static inline void
49 {
50  state.cnt_threads = GetCntRunningThreads();
51  state.cnt_sockets = s_TotalSockets;
52 }
53 
54 
56  : m_MMStat(new SMMStat())
57 {
58  x_ClearStats();
59 }
60 
62 {}
63 
64 void
66 {
67  m_MMStat.reset(stat);
68 }
69 
70 void
72 {
74  m_MaxSlice = 0;
75  m_ExecTime = 0;
76  m_WaitTime = 0;
78  m_ThrStarted = 0;
79  m_ThrStopped = 0;
82  m_SockErrors = 0;
87  m_MMStat->ClearStats();
88 }
89 
90 void
92 {
95  m_MMStat->InitStartState();
96 }
97 
98 void
100 {
101  m_EndState = m_StartState = src_stat->m_EndState;
102  m_MMStat->TransferEndState(src_stat->m_MMStat.get());
103 }
104 
105 void
107 {
108  m_StartState = src_stat->m_StartState;
109  m_MMStat->CopyStartState(src_stat->m_MMStat.get());
110 }
111 
112 void
114 {
115  m_EndState = src_stat->m_EndState;
116  m_MMStat->CopyEndState(src_stat->m_MMStat.get());
117 }
118 
119 void
121 {
123  m_MMStat->SaveEndState();
124 }
125 
126 void
128 {
129  m_JiffyTime.AddValues(src_stat->m_JiffyTime);
130  if (src_stat->m_MaxSlice > m_MaxSlice)
131  m_MaxSlice = src_stat->m_MaxSlice;
132  m_ExecTime += src_stat->m_ExecTime;
133  m_WaitTime += src_stat->m_WaitTime;
134  m_DoneTasks.AddValues(src_stat->m_DoneTasks);
135  m_ThrStarted += src_stat->m_ThrStarted;
136  m_ThrStopped += src_stat->m_ThrStopped;
139  m_SockErrors += src_stat->m_SockErrors;
141  ITERATE(TStatusOpenTimes, it_status, src_stat->m_SocksByStatus) {
142  TSrvTimeTerm& time_term = g_SrvTimeTerm(m_SocksByStatus, it_status->first);
143  time_term.AddValues(it_status->second);
144  }
147  m_MMStat->AddStats(src_stat->m_MMStat.get());
148 }
149 
150 void
152 {
153  x_AddStats(src_stat);
154 }
155 
156 void
157 CSrvStat::CollectThreads(bool need_clear)
158 {
159  for (TSrvThreadNum i = 0; i <= s_MaxRunningThreads + 1; ++i) {
160  CSrvStat* src_stat = s_Threads[i]->stat;
161  src_stat->m_StatLock.Lock();
162  x_AddStats(src_stat);
163  if (need_clear)
164  src_stat->x_ClearStats();
165  src_stat->m_StatLock.Unlock();
166  }
167 }
168 
169 void
171 {
172  CSrvStat* thr_stat = GetCurThread()->stat;
173  thr_stat->m_StatLock.Lock();
176  thr_stat->m_MMStat->SaveEndStateStat(m_MMStat.get());
177  thr_stat->m_StatLock.Unlock();
178 }
179 
180 void
181 CSrvStat::SchedJiffyStat(Uint8 jiffy_len, Uint8 max_slice,
182  Uint8 exec_time, Uint8 done_tasks, Uint8 wait_time)
183 {
184  m_StatLock.Lock();
185  m_JiffyTime.AddValue(jiffy_len);
186  m_ExecTime += exec_time;
187  m_WaitTime += wait_time;
188  m_DoneTasks.AddValue(done_tasks);
189  if (max_slice > m_MaxSlice)
190  m_MaxSlice = max_slice;
191  m_StatLock.Unlock();
192 }
193 
194 void
196 {
198 }
199 
200 void
202 {
204 }
205 
206 void
208 {
210 }
211 
212 void
214 {
216 }
217 
218 void
219 CSrvStat::SockClose(int status, Uint8 open_time)
220 {
221  m_StatLock.Lock();
222  m_SockOpenTime.AddValue(open_time);
223  TSrvTimeTerm& time_term = g_SrvTimeTerm(m_SocksByStatus, status);
224  time_term.AddValue(open_time);
225  m_StatLock.Unlock();
226 }
227 
228 void
230 {
232 }
233 
234 void
236 {
237  if (m_SockOpenTime.GetCount() != 0) {
238  proxy << "Sockets by status:" << endl;
240  const TSrvTimeTerm& time_term = it_st->second;
241  proxy << it_st->first << ": "
242  << g_ToSmartStr(time_term.GetCount()) << " (cnt), "
243  << g_AsMSecStat(time_term.GetAverage()) << " (avg msec), "
244  << g_AsMSecStat(time_term.GetMaximum()) << " (max msec)" << endl;
245  }
246  proxy << endl;
247  }
248 }
249 
250 void
252 {
253  CSrvDiagMsg diag;
254  diag.PrintExtra(ctx);
255  diag.PrintParam("start_threads", m_StartState.cnt_threads)
256  .PrintParam("end_threads", m_EndState.cnt_threads)
257  .PrintParam("avg_threads", m_CntThreads.GetAverage())
258  .PrintParam("max_threads", m_CntThreads.GetMaximum())
259  .PrintParam("thr_started", m_ThrStarted)
260  .PrintParam("thr_stopped", m_ThrStopped);
261  diag.PrintParam("start_socks", m_StartState.cnt_sockets)
262  .PrintParam("end_socks", m_EndState.cnt_sockets)
263  .PrintParam("avg_socks", m_CntSockets.GetAverage())
264  .PrintParam("max_socks", m_CntSockets.GetMaximum())
265  .PrintParam("socks_active", m_SockActiveOpens)
266  .PrintParam("socks_passive", m_SockPassiveOpens)
267  .PrintParam("socks_close", m_SockOpenTime.GetCount())
268  .PrintParam("avg_sock_open", m_SockOpenTime.GetAverage())
269  .PrintParam("max_sock_open", m_SockOpenTime.GetMaximum())
270  .PrintParam("sock_errors", m_SockErrors);
271  diag.PrintParam("cnt_jiffies", m_JiffyTime.GetCount())
272  .PrintParam("avg_jiffy_time", g_AsMSecStat(m_JiffyTime.GetAverage()))
273  .PrintParam("max_jiffy_time", g_AsMSecStat(m_JiffyTime.GetMaximum()))
276  .PrintParam("cnt_tasks", m_DoneTasks.GetSum())
277  .PrintParam("avg_jiffy_tasks", m_DoneTasks.GetDoubleAvg())
278  .PrintParam("max_jiffy_tasks", m_DoneTasks.GetMaximum())
279  .PrintParam("avg_idle_slice", double(m_WaitTime) / m_DoneTasks.GetSum() / kUSecsPerMSec)
280  .PrintParam("avg_task_slice", double(m_ExecTime) / m_DoneTasks.GetSum() / kUSecsPerMSec)
281  .PrintParam("max_task_slice", g_AsMSecStat(m_MaxSlice));
282  diag.Flush();
283 
284  m_MMStat->PrintToLogs(ctx, proxy);
285 
286 // 13oct16: removed - nobody will see it anyway
287 // x_PrintUnstructured(proxy);
288 }
289 
291 {
292  string is("\": "), eol(",\n\"");
293  task.WriteText(eol).WriteText("cnt_threads").WriteText(is).WriteNumber(m_EndState.cnt_threads);
294  task.WriteText(eol).WriteText("cnt_sockets").WriteText(is).WriteNumber(m_EndState.cnt_sockets);
295  m_MMStat->PrintState(task);
296 }
297 
298 void
300 {
301  proxy << "Srv config - "
302  << s_MaxRunningThreads << " (max threads), "
303  << s_SoftSocketLimit << " (soft sock limit), "
304  << s_HardSocketLimit << " (hard sock limit)" << endl;
305  proxy << "Threads - "
306  << m_StartState.cnt_threads << " to "
307  << m_EndState.cnt_threads << " (avg "
308  << m_CntThreads.GetAverage() << ", max "
309  << m_CntThreads.GetMaximum() << "), avg busy "
310  << g_CalcStatPct(m_ExecTime, m_JiffyTime.GetSum()) << "%, avg idle "
311  << g_CalcStatPct(m_WaitTime, m_JiffyTime.GetSum()) << "%" << endl;
312  proxy << "Sockets - "
313  << m_StartState.cnt_sockets << " to "
314  << m_EndState.cnt_sockets << " (avg "
315  << m_CntSockets.GetAverage() << ", max "
316  << m_CntSockets.GetMaximum() << ")" << endl;
317  proxy << "Threads flow - "
318  << m_ThrStarted << " (start), "
319  << m_ThrStopped << " (stop)" << endl;
320  proxy << "Sockets flow - "
321  << g_ToSmartStr(m_SockActiveOpens) << " (active open), "
322  << g_ToSmartStr(m_SockPassiveOpens) << " (passive open), "
323  << m_SockOpenTime.GetCount() << " (closed)" << endl;
324  proxy << "Jiffies - "
325  << g_AsMSecStat(m_JiffyTime.GetAverage()) << " (avg msec), "
326  << g_AsMSecStat(m_JiffyTime.GetMaximum()) << " (max msec), "
327  << m_DoneTasks.GetDoubleAvg() << " (avg tasks), "
328  << m_DoneTasks.GetMaximum() << " (max tasks)" << endl;
329  proxy << "Tasks - "
330  << g_ToSmartStr(m_DoneTasks.GetSum()) << " (cnt), "
331  << double(m_ExecTime) / m_DoneTasks.GetSum() / kUSecsPerMSec << " (avg slice), "
332  << g_AsMSecStat(m_MaxSlice) << " (max slice), "
333  << double(m_WaitTime) / m_DoneTasks.GetSum() / kUSecsPerMSec << " (avg wait)" << endl;
334 
335  m_MMStat->PrintToSocket(proxy);
336 
337  x_PrintUnstructured(proxy);
338 }
339 
void Unlock(void)
Unlock the mutex.
Definition: srv_sync.cpp:119
void Lock(void)
Lock the mutex.
Definition: srv_sync.cpp:108
Class used in all diagnostic logging.
Definition: srv_diag.hpp:73
CSrvDiagMsg & PrintExtra(void)
Starts "extra" message.
Definition: logging.cpp:1023
CSrvDiagMsg & PrintParam(CTempString name, CTempString value)
Adds parameter to "request-start" or "extra" message.
Definition: logging.cpp:1040
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
Task controlling a socket.
CSrvSocketTask & WriteText(CTempString message)
Write text into socket.
CSrvSocketTask & WriteNumber(NumType num)
Write number into socket as string, i.e.
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
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
void AddValue(T value)
Add next value into the set.
Definition: srv_stat.hpp:539
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
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
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
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
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
void clear()
Definition: map.hpp:169
CS_CONTEXT * ctx
Definition: t0006.c:12
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
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
int i
TSrvThreadNum s_MaxRunningThreads
Definition: threads_man.cpp:71
SSrvThread ** s_Threads
Definition: threads_man.cpp:59
int s_SoftSocketLimit
static void s_SaveState(SSrvStateStat &state)
Definition: srv_stat.cpp:48
int s_HardSocketLimit
int s_TotalSockets
Uint8 g_CalcStatPct(Uint8 val, Uint8 total)
Definition: srv_stat.hpp:259
string g_ToSmartStr(T num)
Definition: srv_stat.hpp:306
double g_AsMSecStat(Uint8 time_usec)
Definition: srv_stat.hpp:312
TSrvTimeTerm & g_SrvTimeTerm(Map &map, Key key)
Definition: srv_stat.hpp:174
T AtomicAdd(T volatile &var, T add_value)
Definition: srv_sync.hpp:69
@ kUSecsPerMSec
Definition: srv_time.hpp:44
Uint4 cnt_threads
Definition: srv_stat.hpp:189
Uint4 cnt_sockets
Definition: srv_stat.hpp:190
CSrvStat * stat
Definition: threads_man.hpp:93
Uint2 TSrvThreadNum
Type for thread number in TaskServer.
Definition: task_server.hpp:42
TSrvThreadNum GetCntRunningThreads(void)
SSrvThread * GetCurThread(void)
Definition: threads_man.cpp:82
Modified on Mon Apr 22 04:04:07 2024 by modify_doxy.py rev. 669887