]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_time.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / pimd / pim_time.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
12e41d03 2/*
896014f4
DL
3 * PIM for Quagga
4 * Copyright (C) 2008 Everton da Silva Marques
896014f4 5 */
12e41d03 6
7a2fbbf0
DL
7#include <zebra.h>
8
12e41d03
DL
9#include <string.h>
10#include <sys/time.h>
11#include <time.h>
12
12e41d03
DL
13#include "log.h"
14#include "thread.h"
3613d898 15#include "lib_errors.h"
12e41d03
DL
16
17#include "pim_time.h"
18
19static int gettime_monotonic(struct timeval *tv)
20{
d62a17ae 21 int result;
12e41d03 22
d62a17ae 23 result = gettimeofday(tv, 0);
24 if (result) {
450971aa 25 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3 26 "%s: gettimeofday() failure: errno=%d: %s",
15569c58 27 __func__, errno, safe_strerror(errno));
d62a17ae 28 }
12e41d03 29
d62a17ae 30 return result;
12e41d03
DL
31}
32
33/*
34 pim_time_monotonic_sec():
35 number of seconds since some unspecified starting point
36*/
4d762f26 37int64_t pim_time_monotonic_sec(void)
12e41d03 38{
d62a17ae 39 struct timeval now_tv;
12e41d03 40
d62a17ae 41 if (gettime_monotonic(&now_tv)) {
450971aa 42 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3 43 "%s: gettime_monotonic() failure: errno=%d: %s",
15569c58 44 __func__, errno, safe_strerror(errno));
d62a17ae 45 return -1;
46 }
12e41d03 47
d62a17ae 48 return now_tv.tv_sec;
12e41d03
DL
49}
50
51/*
52 pim_time_monotonic_dsec():
53 number of deciseconds since some unspecified starting point
54*/
4d762f26 55int64_t pim_time_monotonic_dsec(void)
12e41d03 56{
d62a17ae 57 struct timeval now_tv;
58 int64_t now_dsec;
12e41d03 59
d62a17ae 60 if (gettime_monotonic(&now_tv)) {
450971aa 61 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3 62 "%s: gettime_monotonic() failure: errno=%d: %s",
15569c58 63 __func__, errno, safe_strerror(errno));
d62a17ae 64 return -1;
65 }
12e41d03 66
d62a17ae 67 now_dsec = ((int64_t)now_tv.tv_sec) * 10
68 + ((int64_t)now_tv.tv_usec) / 100000;
12e41d03 69
d62a17ae 70 return now_dsec;
12e41d03
DL
71}
72
d62a17ae 73int64_t pim_time_monotonic_usec(void)
8a9bd91c 74{
d62a17ae 75 struct timeval now_tv;
76 int64_t now_dsec;
8a9bd91c 77
d62a17ae 78 if (gettime_monotonic(&now_tv)) {
450971aa 79 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3 80 "%s: gettime_monotonic() failure: errno=%d: %s",
15569c58 81 __func__, errno, safe_strerror(errno));
d62a17ae 82 return -1;
83 }
8a9bd91c 84
d62a17ae 85 now_dsec =
86 ((int64_t)now_tv.tv_sec) * 1000000 + ((int64_t)now_tv.tv_usec);
8a9bd91c 87
d62a17ae 88 return now_dsec;
8a9bd91c
DS
89}
90
12e41d03
DL
91int pim_time_mmss(char *buf, int buf_size, long sec)
92{
d62a17ae 93 long mm;
94 int wr;
95
df5dfb77 96 assert(buf_size >= 5);
12e41d03 97
d62a17ae 98 mm = sec / 60;
99 sec %= 60;
12e41d03 100
d62a17ae 101 wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec);
12e41d03 102
d62a17ae 103 return wr != 8;
12e41d03
DL
104}
105
106static int pim_time_hhmmss(char *buf, int buf_size, long sec)
107{
d62a17ae 108 long hh;
109 long mm;
110 int wr;
111
df5dfb77 112 assert(buf_size >= 8);
12e41d03 113
d62a17ae 114 hh = sec / 3600;
115 sec %= 3600;
116 mm = sec / 60;
117 sec %= 60;
12e41d03 118
d62a17ae 119 wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
12e41d03 120
d62a17ae 121 return wr != 8;
12e41d03
DL
122}
123
124void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer)
125{
d62a17ae 126 if (t_timer) {
127 pim_time_mmss(buf, buf_size,
128 thread_timer_remain_second(t_timer));
129 } else {
130 snprintf(buf, buf_size, "--:--");
131 }
12e41d03
DL
132}
133
134void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer)
135{
d62a17ae 136 if (t_timer) {
137 pim_time_hhmmss(buf, buf_size,
138 thread_timer_remain_second(t_timer));
139 } else {
140 snprintf(buf, buf_size, "--:--:--");
141 }
12e41d03
DL
142}
143
144void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec)
145{
df5dfb77 146 assert(buf_size >= 8);
12e41d03 147
d62a17ae 148 pim_time_hhmmss(buf, buf_size, uptime_sec);
12e41d03
DL
149}
150
151void pim_time_uptime_begin(char *buf, int buf_size, int64_t now, int64_t begin)
152{
d62a17ae 153 if (begin > 0)
154 pim_time_uptime(buf, buf_size, now - begin);
155 else
156 snprintf(buf, buf_size, "--:--:--");
12e41d03
DL
157}
158
159long pim_time_timer_remain_msec(struct thread *t_timer)
160{
d62a17ae 161 /* no timer thread running means timer has expired: return 0 */
12e41d03 162
1ee6df33 163 return t_timer ? thread_timer_remain_msec(t_timer) : 0;
12e41d03 164}