1 #ifndef NETCACHE__NC_STAT__HPP
2 #define NETCACHE__NC_STAT__HPP
3 /*  $Id: nc_stat.hpp 487865 2015-12-22 16:29:28Z 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 
31 
32 #include "srv_stat.hpp"
33 
34 
35 BEGIN_NCBI_SCOPE
36 
37 
38 struct SConstCharCompare
39 {
40     bool operator() (const char* left, const char* right) const;
41 };
42 
43 
44 struct SNCStateStat
45 {
SNCStateStatSNCStateStat46     SNCStateStat(void) {
47         memset(this, 0, sizeof(SNCStateStat));
48         state_time = CSrvTime::Current();
49     }
50     CSrvTime state_time;
51     Uint4 progress_cmds;
52     Uint4 db_files;
53     Uint8 db_size;
54     Uint8 db_garb;
55     Int8 cnt_blobs;
56     Int8 cnt_keys;
57     int min_dead_time;
58     int peer_active_conns;
59     int peer_bg_conns;
60     Uint8 mirror_queue_size;
61     Uint8 sync_log_size;
62     size_t wb_size;
63     size_t wb_releasable;
64     size_t wb_releasing;
65     Uint8  cnt_another_server_main;
66     Uint8  avg_tdiff_blobcopy; // average time diff between blob creation time and the time it is sent to mirror
67     Uint8  max_tdiff_blobcopy; // maximum time diff between blob creation time and the time it is sent to mirror
68     Uint8  avg_tdiff_blobnotify; // average time diff between receiving blob update notification and receiving blob data
69     Uint8  max_tdiff_blobnotify; // maximum time diff between receiving blob update notification and receiving blob data
70 };
71 
72 
73 /// Class collecting statistics about NetCache server.
74 class CNCStat : public CObject
75 {
76 public:
77     static void Initialize(void);
78 
79     static CSrvRef<CNCStat> GetStat(const string& stat_type, bool is_prev);
80     static Uint4 GetCntRunningCmds(void);
81     static void DumpAllStats(void);
82     void PrintToSocket(CSrvSocketTask* sock);
83     void PrintState(CSrvSocketTask& sock);
84 
85     static void AddSyncServer(Uint8 srv_id);
86     static bool AddUnknownServer(Uint8 srv_id);
87     static void InitialSyncDone(Uint8 srv_id, bool succeeded);
88 
89     static void CmdStarted(const char* cmd);
90     static void CmdFinished(const char* cmd, Uint8 len_usec, int status);
91     static void ConnClosing(Uint8 cnt_cmds);
92 
93     static void ClientDataWrite(size_t data_size);
94     static void ClientDataRead(size_t data_size);
95     static void ClientBlobWrite(Uint8 blob_size, Uint8 len_usec);
96     static void ClientBlobRollback(Uint8 written_size);
97     static void ClientBlobRead(Uint8 blob_size, Uint8 len_usec);
98     static void PeerDataWrite(size_t data_size);
99     static void PeerDataRead(size_t data_size);
100     static void PeerSyncFinished(Uint8 srv_id, Uint2 slot, Uint8 cnt_ops, bool success);
101     static void DiskDataWrite(size_t data_size);
102     static void DiskDataRead(size_t data_size);
103     static void DiskBlobWrite(Uint8 blob_size);
104     static void DBFileCleaned(bool success, Uint4 seen_recs,
105                               Uint4 moved_recs, Uint4 moved_size);
106     static void SaveCurStateStat(const SNCStateStat& state);
107 
108 public:
109     CNCStat(void);
110 
111     void InitStartState(void);
112     void TransferEndState(CNCStat* src_stat);
113     void AddAllStats(CNCStat* src_stat);
114     static void CollectThreads(CNCStat* dst_stat, bool need_clear);
115     void PrintToLogs(CTempString stat_name);
116 
117     typedef map<const char*, TSrvTimeTerm, SConstCharCompare>   TCmdLensMap;
118     typedef map<const char*, Uint8, SConstCharCompare>          TCmdCountsMap;
119     typedef map<int, TCmdLensMap>                               TStatusCmdLens;
120 
121 private:
122     CNCStat(const CNCStat&);
123     CNCStat& operator= (const CNCStat&);
124 
125     void x_ClearStats(void);
126     void x_AddStats(CNCStat* src_stat);
127     void x_CopyStartState(CNCStat* src_stat);
128     void x_CopyEndState(CNCStat* src_stat);
129     void x_SaveEndState(void);
130     void x_PrintUnstructured(CSrvPrintProxy& proxy);
131 
132 
133     CMiniMutex m_StatLock;
134     string m_StatName;
135     SNCStateStat m_StartState;
136     SNCStateStat m_EndState;
137     Uint8 m_StartedCmds;
138     Uint8 m_ClDataWrite;
139     Uint8 m_ClDataRead;
140     Uint8 m_PeerDataWrite;
141     Uint8 m_PeerDataRead;
142     Uint8 m_DiskDataWrite;
143     Uint8 m_DiskDataRead;
144     Uint8 m_MaxBlobSize;
145     Uint8 m_ClWrBlobs;
146     Uint8 m_ClWrBlobSize;
147     vector<TSrvTimeTerm> m_ClWrLenBySize;
148     Uint8 m_ClRbackBlobs;
149     Uint8 m_ClRbackSize;
150     Uint8 m_ClRdBlobs;
151     Uint8 m_ClRdBlobSize;
152     vector<TSrvTimeTerm> m_ClRdLenBySize;
153     Uint8 m_DiskWrBlobs;
154     Uint8 m_DiskWrBlobSize;
155     vector<Uint8> m_DiskWrBySize;
156     Uint8 m_PeerSyncs;
157     Uint8 m_PeerSynOps;
158     Uint8 m_CntCleanedFiles;
159     Uint8 m_CntFailedFiles;
160     TSrvTimeTerm m_CmdLens;
161     TCmdCountsMap m_CmdsByName;
162     TStatusCmdLens m_LensByStatus;
163     CSrvStatTerm<Uint8> m_ConnCmds;
164     CSrvStatTerm<Uint4> m_CheckedRecs;
165     CSrvStatTerm<Uint4> m_MovedRecs;
166     CSrvStatTerm<Uint4> m_MovedSize;
167     CSrvStatTerm<Uint4> m_CntFiles;
168     CSrvStatTerm<Uint8> m_DBSize;
169     CSrvStatTerm<Uint8> m_GarbageSize;
170     CSrvStatTerm<Uint8> m_CntBlobs;
171     CSrvStatTerm<Uint8> m_CntKeys;
172     CSrvStatTerm<int> m_MirrorActiveConns;
173     CSrvStatTerm<int> m_MirrorBGConns;
174     CSrvStatTerm<Uint8> m_MirrorQSize;
175     CSrvStatTerm<Uint8> m_SyncLogSize;
176     CSrvStatTerm<size_t> m_WBMemSize;
177     CSrvStatTerm<size_t> m_WBReleasable;
178     CSrvStatTerm<size_t> m_WBReleasing;
179     auto_ptr<CSrvStat> m_SrvStat;
180 };
181 
182 
183 class CStatRotator : public CSrvTask
184 {
185 public:
186     CStatRotator(void);
187     virtual ~CStatRotator(void);
188 
189     void CalcNextRun(void);
190 
191 private:
192     virtual void ExecuteSlice(TSrvThreadNum thr_num);
193 };
194 
195 
196 END_NCBI_SCOPE
197 
198 #endif /* NETCACHE__NC_STAT__HPP */
199