geopm  3.1.0
GEOPM - Global Extensible Open Power Manager
All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros Pages
geopm_time.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 - 2024 Intel Corporation
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #ifndef GEOPM_TIME_H_INCLUDE
6 #define GEOPM_TIME_H_INCLUDE
7 
8 #include <errno.h>
9 #include <math.h>
10 #include <string.h>
11 
12 #include "geopm_public.h"
13 
14 #ifndef __cplusplus
15 #include <stdbool.h>
16 #else
17 extern "C"
18 {
19 #endif
20 
21 struct geopm_time_s;
22 
23 static inline int geopm_time_string(int buf_size, char *buf);
24 static inline int geopm_time(struct geopm_time_s *time);
25 static inline double geopm_time_diff(const struct geopm_time_s *begin, const struct geopm_time_s *end);
26 static inline bool geopm_time_comp(const struct geopm_time_s *aa, const struct geopm_time_s *bb);
27 static inline void geopm_time_add(const struct geopm_time_s *begin, double elapsed, struct geopm_time_s *end);
28 static inline double geopm_time_since(const struct geopm_time_s *begin);
29 
30 int GEOPM_PUBLIC
31  geopm_time_zero(struct geopm_time_s *zero_time);
32 
33 #include <time.h>
34 
40  struct timespec t;
41 };
42 
43 static inline int geopm_time(struct geopm_time_s *time)
44 {
45  return clock_gettime(CLOCK_MONOTONIC_RAW, &(time->t));
46 }
47 
48 static inline int geopm_time_real(struct geopm_time_s *time)
49 {
50  return clock_gettime(CLOCK_REALTIME, &(time->t));
51 }
52 
53 static inline double geopm_time_diff(const struct geopm_time_s *begin, const struct geopm_time_s *end)
54 {
55  return (end->t.tv_sec - begin->t.tv_sec) +
56  (end->t.tv_nsec - begin->t.tv_nsec) * 1E-9;
57 }
58 
59 static inline bool geopm_time_comp(const struct geopm_time_s *aa, const struct geopm_time_s *bb)
60 {
61  bool result = aa->t.tv_sec < bb->t.tv_sec;
62  if (!result && aa->t.tv_sec == bb->t.tv_sec) {
63  result = aa->t.tv_nsec < bb->t.tv_nsec;
64  }
65  return result;
66 }
67 
68 static inline void geopm_time_add(const struct geopm_time_s *begin, double elapsed, struct geopm_time_s *end)
69 {
70  *end = *begin;
71  end->t.tv_sec += elapsed;
72  elapsed -= floor(elapsed);
73  end->t.tv_nsec += 1E9 * elapsed;
74  if (end->t.tv_nsec >= 1000000000) {
75  end->t.tv_nsec -= 1000000000;
76  ++(end->t.tv_sec);
77  }
78 }
79 
80 static inline int geopm_time_to_string(const struct geopm_time_s *time, int buf_size, char *buf)
81 {
82  int err = 0;
83  struct tm tm;
84  struct geopm_time_s ref_time_real;
85  struct geopm_time_s ref_time_mono;
86  clock_gettime(CLOCK_REALTIME, &(ref_time_real.t));
87  clock_gettime(CLOCK_MONOTONIC_RAW, &(ref_time_mono.t));
88  time_t sec_since_1970 = geopm_time_diff(&ref_time_mono, &ref_time_real) + time->t.tv_sec;
89  localtime_r(&sec_since_1970, &tm);
90  size_t num_byte = strftime(buf, buf_size, "%a %b %d %H:%M:%S %Y", &tm);
91  if (!num_byte) {
92  err = EINVAL;
93  }
94  return err;
95 }
96 
97 static inline int geopm_time_string(int buf_size, char *buf)
98 {
99  struct geopm_time_s time;
100  int err = geopm_time(&time);
101  if (!err) {
102  err = geopm_time_to_string(&time, buf_size, buf);
103  }
104  return err;
105 }
106 
107 static inline double geopm_time_since(const struct geopm_time_s *begin)
108 {
109  struct geopm_time_s curr_time;
110  geopm_time(&curr_time);
111  return geopm_time_diff(begin, &curr_time);
112 }
113 
114 #ifdef __cplusplus
115 }
116 namespace geopm
117 {
119  time_zero(void);
121  time_curr(void);
122  void GEOPM_PUBLIC
123  time_zero_reset(const geopm_time_s &zero);
124 }
125 #endif
126 #endif
#define GEOPM_PUBLIC
Definition: geopm_public.h:10
int GEOPM_PUBLIC geopm_time_zero(struct geopm_time_s *zero_time)
Definition: TimeZero.cpp:77
Definition: Agg.cpp:20
struct geopm_time_s time_zero(void)
Definition: TimeZero.cpp:53
void time_zero_reset(const geopm_time_s &zero)
Definition: TimeZero.cpp:62
struct geopm_time_s time_curr(void)
Definition: TimeZero.cpp:67
structure to abstract the timespec on linux from other representations of time.
Definition: geopm_time.h:39
struct timespec t
Definition: geopm_time.h:40