• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <ditto/logger.h>
16 #include <ditto/sampler.h>
17 
TimespecToNanos(const timespec & t)18 int64_t TimespecToNanos(const timespec& t) {
19   return t.tv_sec * 1e9 + t.tv_nsec;
20 }
21 
TimespecToDoubleNanos(const timespec & t)22 double TimespecToDoubleNanos(const timespec& t) {
23   return static_cast<double>(TimespecToNanos(t));
24 }
25 
TimespecToNanos(const std::vector<timespec> & tv)26 std::vector<int64_t> TimespecToNanos(const std::vector<timespec>& tv) {
27   std::vector<int64_t> nsv;
28   nsv.reserve(tv.size());
29   for (const auto& it : tv) nsv.push_back(TimespecToNanos(it));
30   return nsv;
31 }
32 
TimespecToDoubleNanos(const std::vector<timespec> & tv)33 std::vector<double> TimespecToDoubleNanos(const std::vector<timespec>& tv) {
34   std::vector<double> nsv;
35   nsv.reserve(tv.size());
36   for (const auto& it : tv) nsv.push_back(TimespecToDoubleNanos(it));
37   return nsv;
38 }
39 
NanosToTimespec(const int64_t t_ns)40 timespec NanosToTimespec(const int64_t t_ns) {
41   timespec result;
42   result.tv_sec = t_ns / 1e9;
43   result.tv_nsec = t_ns % static_cast<int64_t>(1e9);
44   return result;
45 }
46 
operator ==(const timespec & t1,const timespec & t2)47 bool operator==(const timespec& t1, const timespec& t2) {
48   return t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec;
49 }
50 
operator !=(const timespec & t1,const timespec & t2)51 bool operator!=(const timespec& t1, const timespec& t2) {
52   return !(t1 == t2);
53 }
54 
operator <(const timespec & t1,const timespec & t2)55 bool operator<(const timespec& t1, const timespec& t2) {
56   return ((t1.tv_sec < t2.tv_sec) || (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec));
57 }
58 
operator <=(const timespec & t1,const timespec & t2)59 bool operator<=(const timespec& t1, const timespec& t2) {
60   return t1 == t2 || t1 < t2;
61 }
62 
operator >(const timespec & t1,const timespec & t2)63 bool operator>(const timespec& t1, const timespec& t2) {
64   return !(t1 <= t2);
65 }
66 
operator >=(const timespec & t1,const timespec & t2)67 bool operator>=(const timespec& t1, const timespec& t2) {
68   return t1 == t2 || t1 > t2;
69 }
70 
71 // Return the value of t1 - t2, if t1 >= t2 or fail.
operator -(const timespec & t1,const timespec & t2)72 timespec operator-(const timespec& t1, const timespec& t2) {
73   timespec result = {0, 0};
74   if (t1 < t2) {
75     LOGF("Subtraction cannot return negative timespec values");
76   } else {
77     result.tv_sec = t1.tv_sec - t2.tv_sec;
78     if (t1.tv_nsec < t2.tv_nsec) {
79       result.tv_sec--;
80       result.tv_nsec = 1e9 - t2.tv_nsec + t1.tv_nsec;
81     } else {
82       result.tv_nsec = t1.tv_nsec - t2.tv_nsec;
83     }
84   }
85   return result;
86 }
87 
operator +(const timespec & t1,const timespec & t2)88 timespec operator+(const timespec& t1, const timespec& t2) {
89   timespec result = {0, 0};
90   result.tv_sec = t1.tv_sec + t2.tv_sec;
91   if (t1.tv_nsec + t2.tv_nsec >= 1e9) {
92     result.tv_sec++;
93     result.tv_nsec = t1.tv_nsec + t2.tv_nsec - 1e9;
94   } else {
95     result.tv_nsec = t1.tv_nsec + t2.tv_nsec;
96   }
97   return result;
98 }
99 
operator /(const timespec & t1,uint64_t t2_ns)100 timespec operator/(const timespec& t1, uint64_t t2_ns) {
101   if (t2_ns == 0) LOGF("Division by 0 for timespec");
102 
103   auto t1_ns = TimespecToNanos(t1);
104 
105   return NanosToTimespec(t1_ns / t2_ns);
106 }
107 
operator /(const timespec & t1,const timespec & t2)108 timespec operator/(const timespec& t1, const timespec& t2) {
109   return t1 / TimespecToNanos(t2);
110 }
111