geopm  3.1.1.dev214+gba4f9f6d
GEOPM - Global Extensible Open Power Manager
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 #include <string>
18 extern "C"
19 {
20 #endif
21 
22 struct geopm_time_s;
23 
24 static inline int geopm_time_string(int buf_size, char *buf);
25 static inline int geopm_time(struct geopm_time_s *time);
26 static inline double geopm_time_diff(const struct geopm_time_s *begin, const struct geopm_time_s *end);
27 static inline bool geopm_time_comp(const struct geopm_time_s *aa, const struct geopm_time_s *bb);
28 static inline void geopm_time_add(const struct geopm_time_s *begin, double elapsed, struct geopm_time_s *end);
29 static inline double geopm_time_since(const struct geopm_time_s *begin);
30 
31 int GEOPM_PUBLIC
32  geopm_time_zero(struct geopm_time_s *zero_time);
33 
34 #include <time.h>
35 
41  struct timespec t;
42 };
43 
44 static inline int geopm_time(struct geopm_time_s *time)
45 {
46  return clock_gettime(CLOCK_MONOTONIC_RAW, &(time->t));
47 }
48 
49 static inline int geopm_time_real(struct geopm_time_s *time)
50 {
51  return clock_gettime(CLOCK_REALTIME, &(time->t));
52 }
53 
54 static inline double geopm_time_diff(const struct geopm_time_s *begin, const struct geopm_time_s *end)
55 {
56  return (end->t.tv_sec - begin->t.tv_sec) +
57  (end->t.tv_nsec - begin->t.tv_nsec) * 1E-9;
58 }
59 
60 static inline bool geopm_time_comp(const struct geopm_time_s *aa, const struct geopm_time_s *bb)
61 {
62  bool result = aa->t.tv_sec < bb->t.tv_sec;
63  if (!result && aa->t.tv_sec == bb->t.tv_sec) {
64  result = aa->t.tv_nsec < bb->t.tv_nsec;
65  }
66  return result;
67 }
68 
69 static inline void geopm_time_add(const struct geopm_time_s *begin, double elapsed, struct geopm_time_s *end)
70 {
71  *end = *begin;
72  end->t.tv_sec += elapsed;
73  elapsed -= floor(elapsed);
74  end->t.tv_nsec += 1E9 * elapsed;
75  if (end->t.tv_nsec >= 1000000000) {
76  end->t.tv_nsec -= 1000000000;
77  ++(end->t.tv_sec);
78  }
79 }
80 
81 static inline int geopm_time_to_string(const struct geopm_time_s *time, int buf_size, char *buf)
82 {
83  int err = 0;
84  struct tm tm;
85  struct geopm_time_s ref_time_real;
86  struct geopm_time_s ref_time_mono;
87  clock_gettime(CLOCK_REALTIME, &(ref_time_real.t));
88  clock_gettime(CLOCK_MONOTONIC_RAW, &(ref_time_mono.t));
89  time_t sec_since_1970 = geopm_time_diff(&ref_time_mono, &ref_time_real) + time->t.tv_sec;
90  localtime_r(&sec_since_1970, &tm);
91  size_t num_byte = strftime(buf, buf_size, "%a %b %d %H:%M:%S %Y", &tm);
92  if (!num_byte) {
93  err = EINVAL;
94  }
95  return err;
96 }
97 
98 int GEOPM_PUBLIC
99  geopm_time_real_to_iso_string(const struct geopm_time_s *time, int buf_size, char *buf);
100 
101 static inline int geopm_time_string(int buf_size, char *buf)
102 {
103  struct geopm_time_s time;
104  int err = geopm_time_real(&time);
105  if (!err) {
106  err = geopm_time_real_to_iso_string(&time, buf_size, buf);
107  }
108  return err;
109 }
110 
111 static inline double geopm_time_since(const struct geopm_time_s *begin)
112 {
113  struct geopm_time_s curr_time;
114  geopm_time(&curr_time);
115  return geopm_time_diff(begin, &curr_time);
116 }
117 
118 #ifdef __cplusplus
119 }
120 namespace geopm
121 {
123  time_zero(void);
125  time_curr(void);
127  time_curr_real(void);
128  std::string GEOPM_PUBLIC
129  time_curr_string(void);
130  void GEOPM_PUBLIC
131  time_zero_reset(const geopm_time_s &zero);
132 }
133 #endif
134 #endif
#define GEOPM_PUBLIC
Definition: geopm_public.h:10
int GEOPM_PUBLIC geopm_time_real_to_iso_string(const struct geopm_time_s *time, int buf_size, char *buf)
Definition: TimeZero.cpp:104
int GEOPM_PUBLIC geopm_time_zero(struct geopm_time_s *zero_time)
Definition: TimeZero.cpp:98
Definition: Agg.cpp:20
struct geopm_time_s time_zero(void)
Definition: TimeZero.cpp:56
struct geopm_time_s time_curr_real(void)
Definition: TimeZero.cpp:77
void time_zero_reset(const geopm_time_s &zero)
Definition: TimeZero.cpp:65
std::string time_curr_string(void)
Definition: TimeZero.cpp:84
struct geopm_time_s time_curr(void)
Definition: TimeZero.cpp:70
structure to abstract the timespec on linux from other representations of time.
Definition: geopm_time.h:40
struct timespec t
Definition: geopm_time.h:41