83b7a53dd090828ba0d48924632cb6fbdbf5a1bb
[charm.git] / src / ck-ldb / LBMachineUtil.C
1 /*****************************************************************************
2  * $Source$
3  * $Author$
4  * $Date$
5  * $Revision$
6  *****************************************************************************/
7
8 /**
9  * \addtogroup CkLdb
10 */
11 /*@{*/
12
13 #include "LBMachineUtil.h"
14 #include <stdlib.h>
15
16 inline void LBMachineUtil::IdleStart(double curWallTime)
17 {
18   start_idle = curWallTime;
19 }
20
21 inline void LBMachineUtil::IdleEnd(double curWallTime)
22 {
23   if (state == on) {
24     const double stop_idle = curWallTime;
25     total_idletime += (stop_idle - start_idle);
26   }
27 }
28
29 void LBMachineUtil::staticIdleStart(LBMachineUtil* obj,double curWallTime)
30 {
31   obj->IdleStart(curWallTime);
32 }
33 void LBMachineUtil::staticIdleEnd(LBMachineUtil* obj,double curWallTime)
34 {
35   obj->IdleEnd(curWallTime);
36 }
37
38 LBMachineUtil::LBMachineUtil()
39 {
40   state = off;
41   total_walltime = total_cputime = 0.0;
42   total_idletime = 0;
43   start_totalwall = start_totalcpu = -1.;
44   total_idletime = 0;
45 };
46
47 void LBMachineUtil::StatsOn()
48 {
49   const double cur_wall = CmiWallTimer();
50   const double cur_cpu = CmiCpuTimer();
51
52   if (state == off) {
53     cancel_idleStart=CcdCallOnConditionKeep(
54          CcdPROCESSOR_BEGIN_IDLE,(CcdVoidFn)staticIdleStart,(void *)this);
55     cancel_idleEnd=CcdCallOnConditionKeep(
56          CcdPROCESSOR_END_IDLE,(CcdVoidFn)staticIdleEnd,(void *)this);
57     state = on;
58   }
59
60   if (start_totalwall != -1.) {
61     total_walltime += (cur_wall - start_totalwall);
62     total_cputime += (cur_cpu - start_totalcpu);
63   }
64   start_totalwall = cur_wall;
65   start_totalcpu = cur_cpu;
66 }
67
68 void LBMachineUtil::StatsOff()
69 {
70   if (state == on) {
71     CcdCancelCallOnConditionKeep(CcdPROCESSOR_BEGIN_IDLE,cancel_idleStart);
72     CcdCancelCallOnConditionKeep(CcdPROCESSOR_END_IDLE,cancel_idleEnd);
73     state = off;
74   }
75
76   if (start_totalwall != -1.) {
77     const double cur_wall = CmiWallTimer();
78     const double cur_cpu = CmiCpuTimer();
79     total_walltime += (cur_wall - start_totalwall);
80     total_cputime += (cur_cpu - start_totalcpu);
81   }
82   start_totalwall = start_totalcpu = -1.;
83 };
84
85 void LBMachineUtil::Clear()
86 {
87   total_walltime = total_cputime = 0;
88
89   if (state == off) {
90     start_totalwall = start_totalcpu = -1.;
91   } else {
92     const double cur_wall = CmiWallTimer();
93     const double cur_cpu = CmiCpuTimer();
94
95     start_totalwall = cur_wall;
96     start_totalcpu = cur_cpu;
97   }
98   total_idletime = 0;
99 }
100
101 void LBMachineUtil::TotalTime(double* walltime, double* cputime)
102 {
103   if (state == on) {
104     const double cur_wall = CmiWallTimer();
105     const double cur_cpu = CmiCpuTimer();
106     total_walltime += (cur_wall - start_totalwall);
107     total_cputime += (cur_cpu - start_totalcpu);
108     start_totalwall = cur_wall;
109     start_totalcpu = cur_cpu;
110   }
111   *walltime = total_walltime;
112   *cputime = total_cputime;
113 }
114
115 /*@}*/