root/trunk/whisperlib/common/base/timer.cc

Revision 7, 3.7 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #include <unistd.h>
33 #include "common/base/types.h"
34 #include "common/base/log.h"
35 #include "common/base/timer.h"
36
37 namespace timer {
38
39 int64 TicksNsec() {
40   struct timespec ts = { 0, };
41   const int result = clock_gettime(CLOCK_MONOTONIC, &ts);
42   CHECK_EQ(result, 0);
43   const int64 time_nsec = ts.tv_sec * 1000000000LL + ts.tv_nsec;
44   return time_nsec;
45 }
46
47 void SleepMsec(int32 miliseconds) {
48   while ( miliseconds > 0 ) {
49     const int64 ms_start = TicksMsec();
50     // usleep sleeps less then 1000000 usecs - need to split in
51     // sleep and usleep.
52     const int64 seconds = miliseconds / 1000;
53     const int64 useconds = (miliseconds % 1000) * 1000;
54     if ( seconds > 0 ) {
55       ::sleep(seconds);
56     } else {
57       ::usleep(useconds);
58     }
59     const int64 ms_end = TicksMsec();
60     CHECK_GE(ms_end, ms_start);
61     const int64 elapsed = ms_end - ms_start;
62     miliseconds -= elapsed;
63   }
64 }
65
66 struct timespec TimespecFromMsec(int64 miliseconds) {
67   struct timespec ts;
68   ts.tv_sec = static_cast<time_t>(miliseconds / 1000LL);
69   ts.tv_nsec = static_cast<long>((miliseconds % 1000LL) * 1000000LL);
70   return ts;
71 }
72 int64 TimespecToMsec(struct timespec ts) {
73   return ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL;
74 }
75 void SetTimespecAbsoluteTimespec(struct timespec* ts,
76                                  const struct timespec& delta_ts) {
77   const int result = clock_gettime(CLOCK_REALTIME, ts);
78   CHECK_EQ(result, 0);
79   ts->tv_sec += delta_ts.tv_sec;
80   ts->tv_nsec += delta_ts.tv_nsec;
81
82   while ( ts->tv_nsec > 999999999 ) {
83     ts->tv_nsec -= 1000000000;
84     ts->tv_sec++;
85   }
86 }
87
88 void SetTimespecAbsoluteMsec(struct timespec *ts,
89                              int64 shift_up_miliseconds) {
90   struct timespec delta(TimespecFromMsec(shift_up_miliseconds));
91   SetTimespecAbsoluteTimespec(ts, delta);
92 }
93
94 struct timespec
95 TimespecAbsoluteTimespec(const struct timespec& delta_ts) {
96   struct timespec ts;
97   SetTimespecAbsoluteTimespec(&ts, delta_ts);
98   return ts;
99 }
100
101 struct timespec TimespecAbsoluteMsec(int64 shift_up_miliseconds) {
102   struct timespec ts(TimespecFromMsec(shift_up_miliseconds));
103   return TimespecAbsoluteTimespec(ts);
104 }
105
106 }  // end namespace timer
Note: See TracBrowser for help on using the browser.