]>
git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_time.c
3 * Copyright (C) 2008 Everton da Silva Marques
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "lib_errors.h"
32 static int gettime_monotonic(struct timeval
*tv
)
36 result
= gettimeofday(tv
, 0);
38 flog_err_sys(EC_LIB_SYSTEM_CALL
,
39 "%s: gettimeofday() failure: errno=%d: %s",
40 __func__
, errno
, safe_strerror(errno
));
47 pim_time_monotonic_sec():
48 number of seconds since some unspecified starting point
50 int64_t pim_time_monotonic_sec(void)
52 struct timeval now_tv
;
54 if (gettime_monotonic(&now_tv
)) {
55 flog_err_sys(EC_LIB_SYSTEM_CALL
,
56 "%s: gettime_monotonic() failure: errno=%d: %s",
57 __func__
, errno
, safe_strerror(errno
));
65 pim_time_monotonic_dsec():
66 number of deciseconds since some unspecified starting point
68 int64_t pim_time_monotonic_dsec(void)
70 struct timeval now_tv
;
73 if (gettime_monotonic(&now_tv
)) {
74 flog_err_sys(EC_LIB_SYSTEM_CALL
,
75 "%s: gettime_monotonic() failure: errno=%d: %s",
76 __func__
, errno
, safe_strerror(errno
));
80 now_dsec
= ((int64_t)now_tv
.tv_sec
) * 10
81 + ((int64_t)now_tv
.tv_usec
) / 100000;
86 int64_t pim_time_monotonic_usec(void)
88 struct timeval now_tv
;
91 if (gettime_monotonic(&now_tv
)) {
92 flog_err_sys(EC_LIB_SYSTEM_CALL
,
93 "%s: gettime_monotonic() failure: errno=%d: %s",
94 __func__
, errno
, safe_strerror(errno
));
99 ((int64_t)now_tv
.tv_sec
) * 1000000 + ((int64_t)now_tv
.tv_usec
);
104 int pim_time_mmss(char *buf
, int buf_size
, long sec
)
109 zassert(buf_size
>= 5);
114 wr
= snprintf(buf
, buf_size
, "%02ld:%02ld", mm
, sec
);
119 static int pim_time_hhmmss(char *buf
, int buf_size
, long sec
)
125 zassert(buf_size
>= 8);
132 wr
= snprintf(buf
, buf_size
, "%02ld:%02ld:%02ld", hh
, mm
, sec
);
137 void pim_time_timer_to_mmss(char *buf
, int buf_size
, struct thread
*t_timer
)
140 pim_time_mmss(buf
, buf_size
,
141 thread_timer_remain_second(t_timer
));
143 snprintf(buf
, buf_size
, "--:--");
147 void pim_time_timer_to_hhmmss(char *buf
, int buf_size
, struct thread
*t_timer
)
150 pim_time_hhmmss(buf
, buf_size
,
151 thread_timer_remain_second(t_timer
));
153 snprintf(buf
, buf_size
, "--:--:--");
157 void pim_time_uptime(char *buf
, int buf_size
, int64_t uptime_sec
)
159 zassert(buf_size
>= 8);
161 pim_time_hhmmss(buf
, buf_size
, uptime_sec
);
164 void pim_time_uptime_begin(char *buf
, int buf_size
, int64_t now
, int64_t begin
)
167 pim_time_uptime(buf
, buf_size
, now
- begin
);
169 snprintf(buf
, buf_size
, "--:--:--");
172 long pim_time_timer_remain_msec(struct thread
*t_timer
)
174 /* FIXME: Actually fetch msec resolution from thread */
176 /* no timer thread running means timer has expired: return 0 */
178 return t_timer
? 1000 * thread_timer_remain_second(t_timer
) : 0;