doc: Add serial to list of ci file reserved words
[charm.git] / src / ck-ldb / LBMachineUtil.C
1 /**
2  * \addtogroup CkLdb
3 */
4 /*@{*/
5
6 #include <stdlib.h>
7 #include "LBDatabase.h"
8 #include "LBMachineUtil.h"
9
10 inline void LBMachineUtil::IdleStart(double curWallTime)
11 {
12   start_idle = curWallTime;
13 }
14
15 inline void LBMachineUtil::IdleEnd(double curWallTime)
16 {
17 // skip counting idle time in BigSim
18   if (state == on) {
19     const double stop_idle = curWallTime;
20     total_idletime += (stop_idle - start_idle);
21   }
22 }
23
24 void LBMachineUtil::staticIdleStart(LBMachineUtil* obj,double curWallTime)
25 {
26   obj->IdleStart(curWallTime);
27 }
28 void LBMachineUtil::staticIdleEnd(LBMachineUtil* obj,double curWallTime)
29 {
30   obj->IdleEnd(curWallTime);
31 }
32
33 LBMachineUtil::LBMachineUtil()
34 {
35   state = off;
36   total_walltime = 0.0;
37   total_idletime = 0.0;
38   start_totalwall = -1.;
39   start_idle = -1.;
40 #if CMK_LB_CPUTIMER
41   total_cputime = 0.0;
42   start_totalcpu = -1.;
43 #endif
44 }
45
46 void LBMachineUtil::StatsOn()
47 {
48   const double cur_wall = CkWallTimer();
49 #if CMK_LB_CPUTIMER
50   const double cur_cpu = CkCpuTimer();
51 #endif
52
53   if (state == off) {
54 #if ! CMK_BIGSIM_CHARM
55     cancel_idleStart=CcdCallOnConditionKeep(
56          CcdPROCESSOR_BEGIN_IDLE,(CcdVoidFn)staticIdleStart,(void *)this);
57     cancel_idleEnd=CcdCallOnConditionKeep(
58          CcdPROCESSOR_END_IDLE,(CcdVoidFn)staticIdleEnd,(void *)this);
59 #endif
60     state = on;
61   }
62
63   if (start_totalwall != -1.) {
64     total_walltime += (cur_wall - start_totalwall);
65 #if CMK_LB_CPUTIMER
66     total_cputime += (cur_cpu - start_totalcpu);
67 #endif
68   }
69   start_totalwall = cur_wall;
70 #if CMK_LB_CPUTIMER
71   start_totalcpu = cur_cpu;
72 #endif
73 }
74
75 void LBMachineUtil::StatsOff()
76 {
77   if (state == on) {
78 #if ! CMK_BIGSIM_CHARM
79     CcdCancelCallOnConditionKeep(CcdPROCESSOR_BEGIN_IDLE,cancel_idleStart);
80     CcdCancelCallOnConditionKeep(CcdPROCESSOR_END_IDLE,cancel_idleEnd);
81 #endif
82     state = off;
83   }
84
85   if (start_totalwall != -1.) {
86     const double cur_wall = CkWallTimer();
87     total_walltime += (cur_wall - start_totalwall);
88 #if CMK_LB_CPUTIMER
89     const double cur_cpu = CkCpuTimer();
90     total_cputime += (cur_cpu - start_totalcpu);
91 #endif
92   }
93   start_totalwall = -1.;
94 #if CMK_LB_CPUTIMER
95   start_totalcpu = -1.;
96 #endif
97 }
98
99 void LBMachineUtil::Clear()
100 {
101   total_walltime = 0.0;
102 #if CMK_LB_CPUTIMER
103   total_cputime = 0.0;
104 #endif
105
106   if (state == off) {
107     start_totalwall = -1.;
108 #if CMK_LB_CPUTIMER
109     start_totalcpu = -1.;
110 #endif
111   } else {
112     const double cur_wall = CkWallTimer();
113 #if CMK_LB_CPUTIMER
114     const double cur_cpu = CkCpuTimer();
115 #endif
116
117     start_totalwall = cur_wall;
118 #if CMK_LB_CPUTIMER
119     start_totalcpu = cur_cpu;
120 #endif
121   }
122   total_idletime = 0.0;
123   start_idle = -1.;
124 }
125
126 void LBMachineUtil::TotalTime(LBRealType* walltime, LBRealType* cputime)
127 {
128   if (state == on) {
129     const double cur_wall = CkWallTimer();
130 #if CMK_LB_CPUTIMER
131     const double cur_cpu = CkCpuTimer();
132 #endif
133     total_walltime += (cur_wall - start_totalwall);
134     start_totalwall = cur_wall;
135 #if CMK_LB_CPUTIMER
136     total_cputime += (cur_cpu - start_totalcpu);
137     start_totalcpu = cur_cpu;
138 #endif
139   }
140   *walltime = total_walltime;
141 #if CMK_LB_CPUTIMER
142   *cputime = total_cputime;
143 #else
144   *cputime = *walltime;
145 #endif
146 }
147
148 /*@}*/