Salome HOME
Base implementation of Notebook
[modules/kernel.git] / src / Utils / Utils_Timer.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  SALOME Utils : general SALOME's definitions and tools
23 //  File   : Utils_Timer.cxx
24 //  Module : SALOME
25 //
26 # include "Utils_Timer.hxx"
27
28 # include <iostream>
29
30 #include "utilities.h"
31
32 #ifndef WIN32
33 static struct timezone *tz=(struct timezone*) malloc(sizeof(struct timezone));
34 #else
35 //timezone *tz=_timezone;
36 #endif
37
38 #ifndef CLK_TCK
39 # define CLK_TCK      CLOCKS_PER_SEC
40 #endif
41
42 Utils_Timer::Utils_Timer() {
43 #ifndef WIN32
44   RefToInitialTMS = new tms;
45   RefToCurrentTMS = new tms;
46
47   RefToInitialTimeB = new timeval;
48   RefToCurrentTimeB = new timeval;
49 #else
50   RefToInitialTMS = new FILETIME;
51   RefToCurrentTMS = new FILETIME;
52
53   RefToInitialTimeB = new time_t;
54   RefToCurrentTimeB = new time_t;
55 #endif
56
57   Cumul_user      = Cumul_sys = 0.;
58   Stopped         = 1;
59 }
60
61 Utils_Timer::~Utils_Timer() {
62   delete RefToInitialTMS ;
63   delete RefToCurrentTMS ;
64
65   delete RefToInitialTimeB ;
66   delete RefToCurrentTimeB ;
67 }
68
69 void Utils_Timer::Start() {
70   if (Stopped) {
71     Stopped = 0;
72 #ifndef WIN32
73     times(RefToInitialTMS);
74     gettimeofday(RefToInitialTimeB,tz);
75 #else
76     SYSTEMTIME st;
77     GetSystemTime(&st);
78     SystemTimeToFileTime(&st, RefToInitialTMS);
79           time(RefToCurrentTimeB);
80 #endif
81   }
82 }
83
84 void Utils_Timer::Stop() {
85   if (!Stopped) {
86 #ifndef WIN32
87     times(RefToCurrentTMS);
88     int diffr_user = RefToCurrentTMS->tms_utime - RefToInitialTMS->tms_utime;
89     int diffr_sys  = RefToCurrentTMS->tms_stime - RefToInitialTMS->tms_stime;
90     gettimeofday(RefToCurrentTimeB,tz);
91
92     Cumul_user += (double) diffr_user / CLK_TCK ;
93     Cumul_sys  += (double) diffr_sys  / CLK_TCK ;
94 #else
95     SYSTEMTIME st;
96     GetSystemTime(&st);
97     SystemTimeToFileTime(&st, RefToCurrentTMS);
98     Cumul_user += (int)(((ULARGE_INTEGER*)(RefToCurrentTMS))->QuadPart - ((ULARGE_INTEGER*)(RefToInitialTMS))->QuadPart) / 10000000;
99           Cumul_sys = Cumul_user;
100           time(RefToCurrentTimeB);
101 #endif
102    Stopped = 1;
103   }
104 }
105
106 void Utils_Timer::Show() {
107   bool StopSav = Stopped;
108   if (!StopSav) Stop();
109   MESSAGE("CPU user time: "   << Cumul_user  << " seconds ");
110   MESSAGE("CPU system time: " << Cumul_sys   << " seconds ");
111   if (!StopSav) Start();
112 }
113
114 void Utils_Timer::Reset() {
115   Stopped     = 1;
116   Cumul_user  = Cumul_sys = 0. ;
117 }
118
119 void Utils_Timer::ShowAbsolute(){
120 #if defined(_DEBUG_) || defined(_DEBUG)
121 #ifndef WIN32
122     unsigned long Absolute_user = (unsigned long) ((timeval*)RefToCurrentTimeB)->tv_sec ;
123 #else
124     unsigned long Absolute_user = *RefToCurrentTimeB;
125 #endif
126     MESSAGE("Absolute time: "   << Absolute_user  << " seconds ");
127 #endif
128 }