summaryrefslogtreecommitdiff
path: root/src/MoniTool/MoniTool_Timer.cxx
blob: 9f5f0e59ae01e7527d481ef3a32fe3066b19afe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <MoniTool_Timer.ixx>

#include <MoniTool_DataMapOfTimer.hxx>
#include <MoniTool_DataMapIteratorOfDataMapOfTimer.hxx>
#include <OSD_Timer.hxx>
#include <MoniTool_TimerSentry.hxx>
#include <stdio.h>


//=======================================================================
//function : Dump
//purpose  : 
//=======================================================================

void MoniTool_Timer::Dump(Standard_OStream &ostr)
{
  Standard_Integer hours, minutes;
  Standard_Real seconds, CPUtime, user, system;
    
  myTimer.Show(seconds,minutes,hours,CPUtime);
  myTimer.OSD_Chronometer::Show(user,system);
      
  Standard_Real elapsed = seconds + minutes*60 + hours*3600;
  
  char buff[1024];
  sprintf ( buff, "Elapsed:%6.1f sec, CPU User:%9.4f sec, CPU Sys:%9.4f sec, hits: %d",
                   elapsed, user, system, myCount );
    
  ostr << buff << endl;
}

//=======================================================================
//function : Dictionary
//purpose  : Return DataMapOfTimer
//=======================================================================

MoniTool_DataMapOfTimer& MoniTool_Timer::Dictionary () 
{
  static MoniTool_DataMapOfTimer dic;
  return dic;
}

//=======================================================================
//function : Timer
//purpose  : Return handle for timer from map
//=======================================================================

Handle(MoniTool_Timer) MoniTool_Timer::Timer(const Standard_CString name) 
{
//  AmendAccess();
  MoniTool_DataMapOfTimer &dic = Dictionary();
  if ( dic.IsBound(name) )
    return dic.Find(name);
  Handle(MoniTool_Timer) MT = new MoniTool_Timer;
  MT->Timer().Reset();
  dic.Bind(name,MT);
  return MT;
}

//=======================================================================
//function : ClearTimers
//purpose  : Clears all the map of timers
//=======================================================================

void MoniTool_Timer::ClearTimers() 
{
  Dictionary().Clear();
}

//=======================================================================
//function : DumpTimers
//purpose  : Shows all timer from Dictionary
//=======================================================================

void MoniTool_Timer::DumpTimers (Standard_OStream &ostr) 
{
  MoniTool_DataMapOfTimer &dic = Dictionary();
  MoniTool_DataMapIteratorOfDataMapOfTimer iter(dic);

  Standard_Integer NbTimers = dic.Extent();

  ostr << "DUMP OF TIMERS:" << endl;
  Standard_CString *keys = new Standard_CString[NbTimers];
  Standard_Integer i=0;
  for( ; iter.More() && i < NbTimers; iter.Next()) {
    keys[i++] = iter.Key();
  }
  for(i=0; i < NbTimers; i++) {
    Standard_Integer ntmp = 0;
    Standard_CString stmp = 0;
    for(Standard_Integer j=0; j < NbTimers; j++) {
      if ( keys[j] && ( ! stmp || strcmp(stmp,keys[j]) > 0 ) ) {
        ntmp=j;
        stmp=keys[j];
      }
    }
    //Handle(MoniTool_Timer) MT = iter.Value();    
    char buff[1024];
    sprintf ( buff, "%-20s\t", stmp );
    ostr << "TIMER: " << buff;
    //iter.Value()->Dump ( ostr );
    Timer(stmp)->Dump(ostr);
    keys[ntmp]=0;
    if ( Timer(stmp)->IsRunning() ) cerr << "Warning: timer " << stmp << " is running" << endl;
  }
  delete[] keys;
}

//=======================================================================
//function : ComputeAmendments
//purpose  : 
//=======================================================================

static Standard_Real amAccess=0., amInternal=0., amExternal=0., amError=0.;

void MoniTool_Timer::ComputeAmendments ()
{
  const Standard_Integer NBTESTS = 100000;

  Standard_Integer i;

  Handle(MoniTool_Timer) MT0 = MoniTool_Timer::Timer("_mt_amend_0_");
  Handle(MoniTool_Timer) MT1 = MoniTool_Timer::Timer("_mt_amend_1_");
  Handle(MoniTool_Timer) MT2 = MoniTool_Timer::Timer("_mt_amend_2_");
  Handle(MoniTool_Timer) MT3 = MoniTool_Timer::Timer("_mt_amend_3_");
  MT0->Reset();
  MT1->Reset();
  MT2->Reset();
  MT3->Reset();
  MoniTool_Timer::Timer("_mt_amend_t1_")->Reset();
  MoniTool_Timer::Timer("_mt_amend_t2_")->Reset();
  MoniTool_Timer::Timer("_mt_amend_t3_")->Reset();
  
  // reference test
  MT0->Start();
  for ( i=1; i <= NBTESTS; i++ ) {
    for ( int k=1; k <= 100; k++ ) Sqrt ( i+k );
  }
  MT0->Stop();
  
  // test for direct access
  Handle(MoniTool_Timer) MT = MoniTool_Timer::Timer("_mt_amend_t1_");
  MT1->Start();
  for ( i=1; i <= NBTESTS; i++ ) {
    MT->Start();
    for ( int k=1; k <= 100; k++ ) Sqrt ( i+k );
    MT->Stop();
  }
  MT1->Stop();

  // test for using Sentry
  MT2->Start();
  for ( i=1; i <= NBTESTS; i++ ) {
    MoniTool_TimerSentry TS ("_mt_amend_t2_");
    for ( int k=1; k <= 100; k++ ) Sqrt ( i+k );
  }
  MT2->Stop();

  // test for access by name
  MT3->Start();
  for ( i=1; i <= NBTESTS; i++ ) {
    MoniTool_Timer::Start("_mt_amend_t3_");
    for ( int k=1; k <= 100; k++ ) Sqrt ( i+k );
    MoniTool_Timer::Stop("_mt_amend_t3_");
  }
  MT3->Stop();
  
  // analyze results
  Standard_Real cpu0, cpu1, cpu2, cpu3, cput1, cput2, cput3;
  cpu0 = MoniTool_Timer::Timer("_mt_amend_0_")->CPU();
  cpu1 = MoniTool_Timer::Timer("_mt_amend_1_")->CPU();
  cput1 = MT->CPU();
  cpu2 = MoniTool_Timer::Timer("_mt_amend_2_")->CPU();
  cput2 = MoniTool_Timer::Timer("_mt_amend_t2_")->CPU();
  cpu3 = MoniTool_Timer::Timer("_mt_amend_3_")->CPU();
  cput3 = MoniTool_Timer::Timer("_mt_amend_t3_")->CPU();
  
  amExternal += ( cpu1  - cpu0 ) / NBTESTS;
  amInternal += ( cput1 - cpu0 ) / NBTESTS;
  amAccess   += ( 0.5 * ( cpu3 - cpu1 ) ) / NBTESTS;
  amError     = Abs ( cpu1 + cpu3 - 2 * cpu2 ) / NBTESTS;
  
  cout << "CPU 0: " << cpu0 << endl;
  cout << "CPU 1: " << cpu1 << " INTERNAL: " << cput1 << endl;
  cout << "CPU 2: " << cpu2 << " INTERNAL: " << cput2 << endl;
  cout << "CPU 3: " << cpu3 << " INTERNAL: " << cput3 << endl;
  cout << "Access: " << amAccess << ", External: " << amExternal << 
          ", Internal: " << amInternal << ", Error: " << amError << endl;
  
}
  
//=======================================================================
//function : GetAmendments
//purpose  : 
//=======================================================================

void MoniTool_Timer::GetAmendments (Standard_Real &access, 
                                    Standard_Real &internal, 
                                    Standard_Real &external, 
                                    Standard_Real &error10)
{
  access   = amAccess;
  internal = amInternal;
  external = amExternal;
  error10  = amError;
}

//=======================================================================
//function : AmendAccess
//purpose  : 
//=======================================================================

static Handle(MoniTool_Timer) myActive;
     
void MoniTool_Timer::AmendAccess ()
{
  Standard_Real amend = amAccess;
  for ( Handle(MoniTool_Timer) act = myActive; ! act.IsNull(); act = act->myNext )
    act->myAmend += amend;
}

void MoniTool_Timer::AmendStart ()
{
  Standard_Real amend = amExternal;
  for ( Handle(MoniTool_Timer) act = myActive; ! act.IsNull(); act = act->myNext )
    act->myAmend += amend;
  myAmend += amInternal;

  // add to list
  if ( ! myActive.IsNull() ) {
    myActive->myPrev = this;
    myNext = myActive;
  }
  myActive = this;
}

void MoniTool_Timer::AmendStop ()
{ 
  Handle (MoniTool_Timer) thisActive(this);
  if ( myActive == thisActive )  myActive = myNext;
//    if ( myActive == this )  myActive = myNext;
 
  if ( ! myPrev.IsNull() ) myPrev->myNext = myNext;
  if ( ! myNext.IsNull() ) myNext->myPrev = myPrev;
 
  myNext = myPrev = 0;
}