1    	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2    	/*                                                                           */
3    	/*                  This file is part of the class library                   */
4    	/*       SoPlex --- the Sequential object-oriented simPlex.                  */
5    	/*                                                                           */
6    	/*  Copyright (c) 1996-2023 Zuse Institute Berlin (ZIB)                      */
7    	/*                                                                           */
8    	/*  Licensed under the Apache License, Version 2.0 (the "License");          */
9    	/*  you may not use this file except in compliance with the License.         */
10   	/*  You may obtain a copy of the License at                                  */
11   	/*                                                                           */
12   	/*      http://www.apache.org/licenses/LICENSE-2.0                           */
13   	/*                                                                           */
14   	/*  Unless required by applicable law or agreed to in writing, software      */
15   	/*  distributed under the License is distributed on an "AS IS" BASIS,        */
16   	/*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17   	/*  See the License for the specific language governing permissions and      */
18   	/*  limitations under the License.                                           */
19   	/*                                                                           */
20   	/*  You should have received a copy of the Apache-2.0 license                */
21   	/*  along with SoPlex; see the file LICENSE. If not email to soplex@zib.de.  */
22   	/*                                                                           */
23   	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24   	
25   	#include <assert.h>
26   	
27   	#if defined(_WIN32) || defined(_WIN64)
28   	
29   	#include <time.h>
30   	
31   	#else   // !(_WIN32 || _WIN64)
32   	
33   	#include <sys/types.h>
34   	#include <sys/times.h>
35   	//#include <sys/param.h>
36   	#include <unistd.h>
37   	
38   	#endif  // !(_WIN32 || _WIN64)
39   	
40   	#include "soplex/spxdefines.h"
41   	#include "soplex/usertimer.h"
42   	
43   	namespace soplex
44   	{
45   	/* determine TIMES_TICKS_PER_SEC for clock ticks delivered by times().
46   	 * (don't use CLOCKS_PER_SEC since this is related to clock() only).
47   	 */
48   	#if defined(CLK_TCK)
49   	#define TIMES_TICKS_PER_SEC CLK_TCK
50   	#elif defined(_SC_CLK_TCK)
51   	#define TIMES_TICKS_PER_SEC sysconf(_SC_CLK_TCK)
52   	#elif defined(HZ)
53   	#define TIMES_TICKS_PER_SEC HZ
54   	#else // !CLK_TCK && !_SC_CLK_TCK && !HZ
55   	#define TIMES_TICKS_PER_SEC 60
56   	#endif // !CLK_TCK && !_SC_CLK_TCK && !HZ
57   	
58   	const long UserTimer::ticks_per_sec = long(TIMES_TICKS_PER_SEC);
59   	
60   	// get actual user, system and real time from system
61   	void UserTimer::updateTicks() const
62   	{
63   	#if defined(_WIN32) || defined(_WIN64)
64   	
65   	   uTicks = clock();
66   	
67   	#else   /* !(_WIN32 || _WIN64) */
68   	
69   	   struct tms now;
70   	   clock_t    ret = times(&now);
71   	
72   	   if(int(ret) == -1)
73   	      now.tms_utime = now.tms_stime = ret = 0;
74   	
75   	   uTicks = now.tms_utime;
76   	
77   	#endif  /* !(_WIN32 || _WIN64) */
78   	}
79   	
80   	// start timer, resume accounting user, system and real time.
81   	void UserTimer::start()
82   	{
83   	   // ignore start request if timer is running
84   	   if(status != RUNNING)
85   	   {
86   	      updateTicks();
87   	
88   	      uAccount -= uTicks;
89   	      status    = RUNNING;
90   	   }
91   	
92   	   lasttime = 0;
93   	}
94   	
95   	// stop timer, return accounted user time.
96   	Real UserTimer::stop()
97   	{
98   	   // status remains unchanged if timer is not running
99   	   if(status == RUNNING)
100  	   {
101  	      updateTicks();
102  	
103  	      uAccount += uTicks;
104  	      status    = STOPPED;
105  	   }
106  	
107  	   return ticks2sec(uAccount);
108  	}
109  	
110  	// get accounted user time.
111  	Real UserTimer::time() const
112  	{
113  	   if(status == RUNNING)
114  	   {
115  	      updateTicks();
116  	      lasttime = ticks2sec(uTicks + uAccount);
117  	   }
118  	   else
119  	   {
120  	      lasttime = ticks2sec(uAccount);
121  	   }
122  	
123  	   return lasttime;
124  	}
125  	
126  	Real UserTimer::lastTime() const
127  	{
128  	   return lasttime;
129  	}
130  	
131  	} // namespace soplex
132