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 #include <windows.h> 29 #else 30 #include <sys/times.h> 31 #include <sys/time.h> 32 #endif 33 #include <time.h> 34 35 #include "soplex/spxdefines.h" 36 #include "soplex/wallclocktimer.h" 37 38 namespace soplex 39 { 40 41 // start timer, resume accounting user, system and real time. 42 void WallclockTimer::start() 43 { 44 // ignore start request if timer is running 45 if(status != RUNNING) 46 { 47 #if !defined(_WIN32) && !defined(_WIN64) 48 struct timeval tp; /*lint !e86*/ 49 #endif 50 #if defined(_WIN32) || defined(_WIN64) 51 sec = -::time(NULL); 52 #else 53 gettimeofday(&tp, NULL); 54 55 if(tp.tv_usec > usec) /*lint !e115 !e40*/ 56 { 57 sec = -(tp.tv_sec + 1); /*lint !e115 !e40*/ 58 usec = (1000000 - tp.tv_usec); /*lint !e115 !e40*/ 59 } 60 else 61 { 62 sec = -tp.tv_sec; /*lint !e115 !e40*/ 63 usec = -tp.tv_usec; /*lint !e115 !e40*/ 64 } 65 66 #endif 67 status = RUNNING; 68 } 69 70 lasttime = 0.0; 71 } 72 73 // stop timer, return accounted wallclock time. 74 Real WallclockTimer::stop() 75 { 76 // status remains unchanged if timer is not running 77 if(status == RUNNING) 78 { 79 #if !defined(_WIN32) && !defined(_WIN64) 80 struct timeval tp; /*lint !e86*/ 81 #endif 82 83 #if defined(_WIN32) || defined(_WIN64) 84 // we need the blank specifier to distiguish this method from WallclockTimer::time 85 sec += ::time(NULL); 86 #else 87 gettimeofday(&tp, NULL); 88 89 if(tp.tv_usec + usec > 1000000) /*lint !e115 !e40*/ 90 { 91 sec += (tp.tv_sec + 1); /*lint !e115 !e40*/ 92 usec -= (1000000 - tp.tv_usec); /*lint !e115 !e40*/ 93 } 94 else 95 { 96 sec += tp.tv_sec; /*lint !e115 !e40*/ 97 usec += tp.tv_usec; /*lint !e115 !e40*/ 98 } 99 100 #endif 101 status = STOPPED; 102 lasttime = wall2sec(sec, usec); 103 } 104 105 return lasttime; 106 } 107 108 109 Real WallclockTimer::time() const 110 { 111 #if !defined(_WIN32) && !defined(_WIN64) 112 struct timeval tp; /*lint !e86*/ 113 #endif 114 115 // only update times if timer is still running 116 if(status == RUNNING) 117 { 118 #if defined(_WIN32) || defined(_WIN64) 119 // we need the blank specifier to distiguish this method from WallclockTimer::time 120 lasttime = wall2sec(sec + ::time(NULL), 0); 121 #else 122 gettimeofday(&tp, NULL); 123 124 // check whether the microseconds add up to more than a second 125 if(tp.tv_usec + usec > 1000000) /*lint !e115 !e40*/ 126 lasttime = wall2sec(sec + tp.tv_sec + 1, /*lint !e115 !e40*/ 127 (usec - 1000000) + tp.tv_usec); /*lint !e115 !e40*/ 128 else 129 lasttime = wall2sec(sec + tp.tv_sec, /*lint !e115 !e40*/ 130 usec + tp.tv_usec); /*lint !e115 !e40*/ 131 132 #endif 133 } 134 135 return lasttime; 136 } 137 138 Real WallclockTimer::lastTime() const 139 { 140 return lasttime; 141 } 142 143 } // namespace soplex 144