1#ifndef __DMPROFILE_H__
2#define __DMPROFILE_H__
3
4#ifndef __cplusplus
5#error "This is a C++ header file; it requires C++ to compile."
6#endif
7
8#ifdef DM_PERFORMANCE_ENABLED
9
10#include "trace_perf.h"
11
12#endif
13
14enum {
15    DM_INITIALIZE_ENTER = 18000000,
16    DM_INITIALIZE_EXIT = 18000001,
17    DM_GET_TREE_ENTER = 18000002,
18    DM_GET_TREE_EXIT = 18000003,
19    DM_GET_NODE_ENTER = 18000004,
20    DM_GET_NODE_EXIT = 18000005,
21    DM_UNINITIALIZE_ENTER = 18000006,
22    DM_UNINITIALIZE_EXIT = 18000007,
23    DM_INITIALIZE_MOUNT = 18000008,
24    DM_INITIALIZE_MDF = 18000009,
25    DM_INITIALIZE_PLUGIN = 18000010,
26    DM_INITIALIZE_ACRHIVER = 18000011,
27    DM_INITIALIZE_FILE = 18000012,
28    DM_INITIALIZE_LOCK = 18000013,
29    DM_INITIALIZE_ACL = 18000014,
30    DM_INITIALIZE_EVENT = 18000015,
31    DM_INITIALIZE_LOAD = 18000016,
32
33};
34
35
36#ifdef DM_PERFORMANCE_ENABLED
37#define DM_PERFORMANCE(event)   TRACE_PERF(event)
38#else
39#define DM_PERFORMANCE(event)
40#endif
41
42
43#ifdef DM_PROFILER_ENABLED
44#include <sys/time.h>
45#include <stdio.h>
46#include <string.h>
47
48#ifdef DEBUG
49extern int s_nBlocks , s_nSize, s_nCnt;
50#endif
51
52// static storage for performance statistic
53#define PROF_CELL_NUM   20000
54struct CDMProfCell {
55  const char* _s;
56  long long  _elapsed, _from;
57};
58
59extern CDMProfCell  g_aProfStorage[PROF_CELL_NUM];
60extern int g_nProfCurCell;
61
62struct CDMProfile {
63
64  CDMProfile( const char* s) {
65    _s = s;
66    gettimeofday( &_tv1, NULL );
67#ifdef DEBUG
68    _nasBlocks = s_nBlocks;
69    _nWasSize = s_nSize;
70    _nWasTotal = s_nCnt;
71#endif
72  }
73  ~CDMProfile() {
74    struct timeval  tv2;
75    gettimeofday( &tv2, NULL );
76    long long n1 = _tv1.tv_usec + (_tv1.tv_sec * 1000000 );
77    long long n2 = tv2.tv_usec + (tv2.tv_sec * 1000000 );
78    long long elapsed = n2 - n1;
79    //printf( "DMProfile: %s, time is %lld usec, from %lld to %lld\n", _s, elapsed, n1, n2 );
80    int nCurCell = g_nProfCurCell++ % PROF_CELL_NUM;
81    g_aProfStorage[nCurCell]._s = strdup( _s );
82    g_aProfStorage[nCurCell]._elapsed = elapsed;
83    g_aProfStorage[nCurCell]._from = n1;
84
85#ifdef DEBUG
86#ifdef DM_DM_MEMORY_USAGE_ENABLED
87    printf( "DMProfile: %s, time is %lld usec, from %lld to %lld\n", _s, elapsed, n1, n2 );
88
89    printf( "Total Blocks %d (delta is %d), size %d (delta is %d), total allocated (including deallocated) %d, (d %d)\n\n",
90      s_nBlocks,  s_nBlocks - _nasBlocks,
91      s_nSize, s_nSize - _nWasSize,
92      s_nCnt, s_nCnt - _nWasTotal );
93#endif
94#endif
95  }
96
97  struct timeval  _tv1;
98  const char* _s;
99#ifdef DEBUG
100  int _nasBlocks, _nWasSize, _nWasTotal;
101#endif
102};
103
104#define DM_PROFILE_EXT(msg,num)  CDMProfile __oProf##num(msg );
105#define DM_PROFILE(msg)  CDMProfile __oProf(msg );
106
107#else
108#define DM_PROFILE(msg)
109#define DM_PROFILE_EXT(msg,num)
110#endif
111
112
113#ifdef DM_PROFILER_STACK
114
115#include <sys/time.h>
116#include <stdio.h>
117#include <string.h>
118#include "dmvector.h"
119
120// static storage for performance statistic
121#define PROF_STACK_NUM   20000
122struct DMProfileData {
123  const char* _s;
124  long long  _elapsed, _from;
125};
126
127extern DMProfileData  __aProfStackStorage[PROF_STACK_NUM];
128extern int g_nProfCurCell;
129
130struct CDMProfileCapture {
131
132  CDMProfileCapture(DMStringVector itemNames, DMStringVector itemValues) {
133    _itemNames = itemNames;
134    _itemValues = itemValues;
135    gettimeofday( &_tv1, NULL );
136  }
137
138  ~CDMProfileCapture() {
139    struct timeval  tv2;
140    gettimeofday( &tv2, NULL );
141    long long n1 = _tv1.tv_usec + (_tv1.tv_sec * 1000000 );
142    long long n2 = tv2.tv_usec + (tv2.tv_sec * 1000000 );
143    long long elapsed = n2 - n1;
144
145    DMString tmp = "";
146    DMString tmpValue = "";
147
148    for (int i = 0; i < _itemNames.size(); i++) {
149      tmp += "    <";
150      tmp += _itemNames[i];
151      tmp += ">";
152      tmpValue = _itemValues[i];
153
154      tmpValue.replaceAll('<', '#');
155      tmpValue.replaceAll('>', '#');
156
157      tmp += tmpValue;
158
159      const char* ch = _itemValues[i].c_str();
160
161      int size = strlen(ch);
162
163      if (ch[size - 1] == '\n') {
164	tmp += "    </";
165      } else {
166        tmp += "</";
167      }
168
169      tmp += _itemNames[i];
170      tmp += ">\n";
171    }
172
173    int nCurCell = g_nProfCurCell++ % PROF_STACK_NUM;
174    __aProfStackStorage[nCurCell]._s = strdup(tmp.c_str());
175
176    __aProfStackStorage[nCurCell]._elapsed = elapsed;
177    __aProfStackStorage[nCurCell]._from = n1;
178  }
179
180  struct timeval  _tv1;
181  const char* _s;
182  DMStringVector _itemNames;
183  DMStringVector _itemValues;
184};
185
186#define DM_PROFILE_STACK(itemNames, itemValues)  CDMProfileCapture __oProfStack(itemNames, itemValues);
187
188#else
189#define DM_PROFILE_STACK(itemNames, itemValues)
190#endif
191
192
193#ifdef DM_FILE_OUTPUT
194void DMFileOutput( const char* szFormat, ... );
195#else
196#define DMFileOutput
197#endif
198
199#endif // __DMPROFILE_H__
200
201