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