XRootD
Loading...
Searching...
No Matches
XrdSysTimer.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d S y s T i m e r . c c */
4/* */
5/* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* Produced by Andrew Hanushevsky for Stanford University under contract */
7/* DE-AC02-76-SFO0515 with the Department of Energy */
8/* */
9/* This file is part of the XRootD software suite. */
10/* */
11/* XRootD is free software: you can redistribute it and/or modify it under */
12/* the terms of the GNU Lesser General Public License as published by the */
13/* Free Software Foundation, either version 3 of the License, or (at your */
14/* option) any later version. */
15/* */
16/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19/* License for more details. */
20/* */
21/* You should have received a copy of the GNU Lesser General Public License */
22/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24/* */
25/* The copyright holder's institutional names and contributor's names may not */
26/* be used to endorse or promote products derived from this software without */
27/* specific prior written permission of the institution or contributor. */
28/******************************************************************************/
29
30#ifndef WIN32
31#include <unistd.h>
32#else
33#include "XrdSys/XrdWin32.hh"
34#endif
35#include <cerrno>
36#include <ctime>
37#include <cstdio>
38#include "XrdSys/XrdSysTimer.hh"
39#include <iostream>
40
41/******************************************************************************/
42/* D e l t a _ T i m e */
43/******************************************************************************/
44
45struct timeval *XrdSysTimer::Delta_Time(struct timeval &tbeg)
46{
47 gettimeofday(&LastReport, 0);
48 LastReport.tv_sec = LastReport.tv_sec - tbeg.tv_sec;
49 LastReport.tv_usec = LastReport.tv_usec - tbeg.tv_usec;
50 if (LastReport.tv_usec < 0) {LastReport.tv_sec--; LastReport.tv_usec += 1000000;}
51 return &LastReport;
52}
53
54/******************************************************************************/
55/* M i d n i g h t */
56/******************************************************************************/
57
58time_t XrdSysTimer::Midnight(time_t tnow)
59{
60 struct tm midtime;
61 time_t add_time;
62
63// Compute time at midnight
64//
65 if (tnow == 0 || tnow == 1) {add_time = tnow; tnow = time(0);}
66 else add_time = 0;
67 localtime_r((const time_t *) &tnow, &midtime);
68 if (add_time) {midtime.tm_hour = 23; midtime.tm_min = midtime.tm_sec = 59;}
69 else midtime.tm_hour = midtime.tm_min = midtime.tm_sec = 0;
70 return mktime(&midtime) + add_time;
71}
72
73/******************************************************************************/
74/* R e p o r t */
75/******************************************************************************/
76
77unsigned long XrdSysTimer::Report()
78{
79 unsigned long current_time;
80
81// Get current time of day
82//
83 gettimeofday(&LastReport, 0);
84 current_time = (unsigned long)LastReport.tv_sec;
85
86// Calculate the time interval thus far
87//
88 LastReport.tv_sec = LastReport.tv_sec - StopWatch.tv_sec;
89 LastReport.tv_usec = LastReport.tv_usec - StopWatch.tv_usec;
90 if (LastReport.tv_usec < 0)
91 {LastReport.tv_sec--; LastReport.tv_usec += 1000000;}
92
93// Return the current time
94//
95 return current_time;
96}
97
98/******************************************************************************/
99
100unsigned long XrdSysTimer::Report(double &Total_Time)
101{
102 unsigned long report_time = Report();
103
104// Add up the time as a double
105//
106 Total_Time += static_cast<double>(LastReport.tv_sec) +
107 static_cast<double>(LastReport.tv_usec/1000)/1000.0;
108
109// Return time
110//
111 return report_time;
112}
113
114/******************************************************************************/
115
116unsigned long XrdSysTimer::Report(unsigned long &Total_Time)
117{
118 unsigned long report_time = Report();
119
120// Add up the time as a 32-bit value to nearest milliseconds (max = 24 days)
121//
122 Total_Time += (unsigned long)LastReport.tv_sec*1000 +
123 (unsigned long)(LastReport.tv_usec/1000);
124
125// Return time
126//
127 return report_time;
128}
129
130/******************************************************************************/
131
132unsigned long XrdSysTimer::Report(unsigned long long &Total_Time)
133{
134 unsigned long report_time = Report();
135
136// Add up the time as a 64-bit value to nearest milliseconds
137//
138 Total_Time += (unsigned long long)LastReport.tv_sec*1000 +
139 (unsigned long long)(LastReport.tv_usec/1000);
140
141// Return time
142//
143 return report_time;
144}
145
146/******************************************************************************/
147
148unsigned long XrdSysTimer::Report(struct timeval &Total_Time)
149{
150 unsigned long report_time = Report();
151
152// Add the interval to the interval total time so far
153//
154 Total_Time.tv_sec += LastReport.tv_sec;
155 Total_Time.tv_usec += LastReport.tv_usec;
156 if (Total_Time.tv_usec > 1000000) {Total_Time.tv_sec++;
157 Total_Time.tv_usec -= 1000000;}
158
159// Return time
160//
161 return report_time;
162}
163
164/******************************************************************************/
165/* S n o o z e */
166/******************************************************************************/
167
169{
170#ifndef WIN32
171 struct timespec naptime, waketime;
172
173// Calculate nano sleep time
174//
175 naptime.tv_sec = sec;
176 naptime.tv_nsec = 0;
177
178// Wait for a lsoppy number of seconds
179//
180 while(nanosleep(&naptime, &waketime) && EINTR == errno)
181 {naptime.tv_sec = waketime.tv_sec;
182 naptime.tv_nsec = waketime.tv_nsec;
183 }
184#else
185 ::Sleep(sec*1000);
186#endif
187}
188/******************************************************************************/
189/* s 2 h m s */
190/******************************************************************************/
191
192char *XrdSysTimer::s2hms(int sec, char *buff, int blen)
193{
194 int hours, minutes;
195
196 minutes = sec/60;
197 sec = sec%60;
198 hours = minutes/60;
199 minutes = minutes%60;
200
201 snprintf(buff, blen-1, "%d:%02d:%02d", hours, minutes, sec);
202 buff[blen-1] = '\0';
203 return buff;
204}
205
206/******************************************************************************/
207/* T i m e Z o n e */
208/******************************************************************************/
209
211{
212 time_t currTime = time(0);
213 time_t currTimeGMT = 0;
214 tm ptm;
215
216 gmtime_r( &currTime, &ptm );
217 currTimeGMT = mktime( &ptm );
218 currTime /= 60*60;
219 currTimeGMT /= 60*60;
220 return currTime - currTimeGMT;
221}
222
223/******************************************************************************/
224/* W a i t */
225/******************************************************************************/
226
227void XrdSysTimer::Wait(int mills)
228{
229#ifndef WIN32
230 struct timespec naptime, waketime;
231
232// Calculate nano sleep time
233//
234 naptime.tv_sec = mills/1000;
235 naptime.tv_nsec = (mills%1000)*1000000;
236
237// Wait for exactly x milliseconds
238//
239 while(nanosleep(&naptime, &waketime) && EINTR == errno)
240 {naptime.tv_sec = waketime.tv_sec;
241 naptime.tv_nsec = waketime.tv_nsec;
242 }
243#else
244 ::Sleep(mills);
245#endif
246}
247
248/******************************************************************************/
249/* W a i t 4 M i d n i g h t */
250/******************************************************************************/
251
253{
254
255// Wait until midnight arrives
256//
257#ifndef __APPLE__
258 timespec Midnite = {Midnight(1), 0};
259 while(clock_nanosleep(CLOCK_REALTIME,TIMER_ABSTIME,&Midnite,0) == EINTR) {}
260#else
261 timespec tleft, Midnite = {Midnight(1) - time(0), 0};
262 int ntpWait = 60;
263do{while(nanosleep(&Midnite, &tleft) && EINTR == errno)
264 {Midnite.tv_sec = tleft.tv_sec;
265 Midnite.tv_nsec = tleft.tv_nsec;
266 }
267 if (Midnight(1) - time(0) >= 60) break;
268 Midnite.tv_sec = 1;
269 Midnite.tv_nsec = 0;
270 } while(ntpWait--); // This avoids multiple wakeups when NTP adjusts clock
271#endif
272}
static void Wait4Midnight()
static char * s2hms(int sec, char *buff, int blen)
static time_t Midnight(time_t tnow=0)
struct timeval * Delta_Time(struct timeval &tbeg)
static void Snooze(int seconds)
static int TimeZone()
unsigned long Report(double &)
static void Wait(int milliseconds)