OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESStopWatch.cc
Go to the documentation of this file.
1 // BESStopWatch.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <cerrno>
34 #include <string>
35 #include <iostream>
36 #include <cstring>
37 
38 using std::string ;
39 using std::endl ;
40 
41 #include "BESStopWatch.h"
42 #include "BESDebug.h"
43 
44 bool
46 {
47  // get timer for current usage
48  if( getrusage( RUSAGE_SELF, &_start_usage ) != 0 )
49  {
50  int myerrno = errno ;
51  char *c_err = strerror( myerrno ) ;
52  string err = "getrusage failed in start: " ;
53  if( c_err )
54  {
55  err += c_err ;
56  }
57  else
58  {
59  err += "unknown error" ;
60  }
61  BESDEBUG( "timing", err << endl ) ;
62  _started = false ;
63  }
64  else
65  {
66  _started = true ;
67  }
68 
69  // either we started the stop watch, or failed to start it. Either way,
70  // no timings are available, so set stopped to false.
71  _stopped = false ;
72 
73  return _started ;
74 }
75 
76 bool
78 {
79  // if we have started, the we can stop. Otherwise just fall through.
80  if( _started )
81  {
82  // get timer for current usage
83  if( getrusage( RUSAGE_SELF, &_stop_usage ) != 0 )
84  {
85  int myerrno = errno ;
86  char *c_err = strerror( myerrno ) ;
87  string err = "getrusage failed in stop: " ;
88  if( c_err )
89  {
90  err += c_err ;
91  }
92  else
93  {
94  err += "unknown error" ;
95  }
96  BESDEBUG( "timing", err << endl ) ;
97  _started = false ;
98  _stopped = false ;
99  }
100  else
101  {
102  // get the difference between the _start_usage and the
103  // _stop_usage and save the difference.
104  bool success = timeval_subtract() ;
105  if( !success )
106  {
107  BESDEBUG( "timing", "failed to get timing" << endl ) ;
108  _started = false ;
109  _stopped = false ;
110  }
111  else
112  {
113  _stopped = true ;
114  }
115  }
116  }
117  else
118  {
119  BESDEBUG( "timing", "timing not started" << endl ) ;
120  }
121 
122  return _stopped ;
123 }
124 
125 bool
126 BESStopWatch::timeval_subtract()
127 {
128  struct timeval &start = _start_usage.ru_utime ;
129  struct timeval &stop = _stop_usage.ru_utime ;
130 
131  /* Perform the carry for the later subtraction by updating y. */
132  if( stop.tv_usec < start.tv_usec )
133  {
134  int nsec = (start.tv_usec - stop.tv_usec) / 1000000 + 1 ;
135  start.tv_usec -= 1000000 * nsec ;
136  start.tv_sec += nsec ;
137  }
138  if( stop.tv_usec - start.tv_usec > 1000000 )
139  {
140  int nsec = (start.tv_usec - stop.tv_usec) / 1000000 ;
141  start.tv_usec += 1000000 * nsec ;
142  start.tv_sec -= nsec ;
143  }
144 
145  /* Compute the time remaining to wait.
146  tv_usec is certainly positive. */
147  _result.tv_sec = stop.tv_sec - start.tv_sec ;
148  _result.tv_usec = stop.tv_usec - start.tv_usec ;
149 
150  /* Return 1 if result is negative. */
151  return !(stop.tv_sec < start.tv_sec) ;
152 }
153 
160 void
161 BESStopWatch::dump( ostream &strm ) const
162 {
163  strm << BESIndent::LMarg << "BESStopWatch::dump - ("
164  << (void *)this << ")" << endl ;
165 }
166