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