Salome HOME
0693be3c2a92a6789e948e95797ef773b301443d
[modules/kernel.git] / src / Utils / Utils_Timer.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME Utils : general SALOME's definitions and tools
24 //  File   : Utils_Timer.cxx
25 //  Module : SALOME
26 //
27 # include "Utils_Timer.hxx"
28
29 # include <iostream>
30
31 #include "utilities.h"
32
33 #ifndef WIN32
34 static struct timezone *tz=(struct timezone*) malloc(sizeof(struct timezone));
35 #else
36 //timezone *tz=_timezone;
37 #endif
38
39 #ifndef CLK_TCK
40 # define CLK_TCK      CLOCKS_PER_SEC
41 #endif
42
43 Utils_Timer::Utils_Timer() {
44 #ifndef WIN32
45   RefToInitialTMS = new tms;
46   RefToCurrentTMS = new tms;
47
48   RefToInitialTimeB = new timeval;
49   RefToCurrentTimeB = new timeval;
50 #else
51   RefToInitialTMS = new FILETIME;
52   RefToCurrentTMS = new FILETIME;
53
54   RefToInitialTimeB = new time_t;
55   RefToCurrentTimeB = new time_t;
56 #endif
57
58   Cumul_user      = Cumul_sys = 0.;
59   Stopped         = 1;
60 }
61
62 Utils_Timer::~Utils_Timer() {
63   delete RefToInitialTMS ;
64   delete RefToCurrentTMS ;
65
66   delete RefToInitialTimeB ;
67   delete RefToCurrentTimeB ;
68 }
69
70 void Utils_Timer::Start() {
71   if (Stopped) {
72     Stopped = 0;
73 #ifndef WIN32
74     times(RefToInitialTMS);
75     gettimeofday(RefToInitialTimeB,tz);
76 #else
77     SYSTEMTIME st;
78     GetSystemTime(&st);
79     SystemTimeToFileTime(&st, RefToInitialTMS);
80           time(RefToCurrentTimeB);
81 #endif
82   }
83 }
84
85 void Utils_Timer::Stop() {
86   if (!Stopped) {
87 #ifndef WIN32
88     times(RefToCurrentTMS);
89     int diffr_user = RefToCurrentTMS->tms_utime - RefToInitialTMS->tms_utime;
90     int diffr_sys  = RefToCurrentTMS->tms_stime - RefToInitialTMS->tms_stime;
91     gettimeofday(RefToCurrentTimeB,tz);
92
93     Cumul_user += (double) diffr_user / CLK_TCK ;
94     Cumul_sys  += (double) diffr_sys  / CLK_TCK ;
95 #else
96     SYSTEMTIME st;
97     GetSystemTime(&st);
98     SystemTimeToFileTime(&st, RefToCurrentTMS);
99     Cumul_user += (int)(((ULARGE_INTEGER*)(RefToCurrentTMS))->QuadPart - ((ULARGE_INTEGER*)(RefToInitialTMS))->QuadPart) / 10000000;
100           Cumul_sys = Cumul_user;
101           time(RefToCurrentTimeB);
102 #endif
103    Stopped = 1;
104   }
105 }
106
107 void Utils_Timer::Show() {
108   bool StopSav = Stopped;
109   if (!StopSav) Stop();
110   MESSAGE("CPU user time: "   << Cumul_user  << " seconds ");
111   MESSAGE("CPU system time: " << Cumul_sys   << " seconds ");
112   if (!StopSav) Start();
113 }
114
115 void Utils_Timer::Reset() {
116   Stopped     = 1;
117   Cumul_user  = Cumul_sys = 0. ;
118 }
119
120 void Utils_Timer::ShowAbsolute(){
121 #if defined(_DEBUG_) || defined(_DEBUG)
122 #ifndef WIN32
123     unsigned long Absolute_user = (unsigned long) ((timeval*)RefToCurrentTimeB)->tv_sec ;
124 #else
125     unsigned long Absolute_user = (unsigned long) *RefToCurrentTimeB;
126 #endif
127     MESSAGE("Absolute time: "   << Absolute_user  << " seconds ");
128 #endif
129 }