Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 1 | /* |
| 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 |
| 16 | along with this program; see the file COPYING; if not, write to the |
| 17 | Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 18 | MA 02110-1301 USA |
| 19 | |
| 20 | $QuaggaId: $Format:%an, %ai, %h$ $ |
| 21 | */ |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include <sys/time.h> |
| 25 | #include <time.h> |
| 26 | |
| 27 | #include <zebra.h> |
| 28 | #include "log.h" |
| 29 | #include "thread.h" |
| 30 | |
| 31 | #include "pim_time.h" |
| 32 | |
Everton Marques | 0ef36d8 | 2010-03-11 14:47:42 -0300 | [diff] [blame^] | 33 | static int pim_gettime(int clk_id, struct timeval *tv) |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 34 | { |
Everton Marques | 0ef36d8 | 2010-03-11 14:47:42 -0300 | [diff] [blame^] | 35 | struct timespec ts; |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 36 | int result; |
| 37 | |
Everton Marques | 0ef36d8 | 2010-03-11 14:47:42 -0300 | [diff] [blame^] | 38 | #ifdef PIM_USE_QUAGGA_GETTIME |
| 39 | result = quagga_gettime(clk_id, tv); |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 40 | if (result) { |
Everton Marques | 0ef36d8 | 2010-03-11 14:47:42 -0300 | [diff] [blame^] | 41 | zlog_err("%s: quagga_gettime(clk_id=%d) failure: errno=%d: %s", |
| 42 | __PRETTY_FUNCTION__, clk_id, |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 43 | errno, safe_strerror(errno)); |
| 44 | } |
Everton Marques | 0ef36d8 | 2010-03-11 14:47:42 -0300 | [diff] [blame^] | 45 | #else |
| 46 | result = clock_gettime(clk_id, &ts); |
| 47 | if (result) { |
| 48 | zlog_err("%s: clock_gettime(clk_id=%d) failure: errno=%d: %s", |
| 49 | __PRETTY_FUNCTION__, clk_id, |
| 50 | errno, safe_strerror(errno)); |
| 51 | return result; |
| 52 | } |
| 53 | if (tv) { |
| 54 | tv->tv_sec = ts.tv_sec; |
| 55 | tv->tv_usec = 1000 * ts.tv_nsec; |
| 56 | } |
| 57 | #endif |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 58 | |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | static int gettime_monotonic(struct timeval *tv) |
| 63 | { |
| 64 | int result; |
| 65 | |
Everton Marques | 0ef36d8 | 2010-03-11 14:47:42 -0300 | [diff] [blame^] | 66 | #ifdef PIM_GETTIME_USE_GETTIMEOFDAY |
| 67 | result = gettimeofday(tv, 0); |
| 68 | if (result) { |
| 69 | zlog_err("%s: gettimeofday() failure: errno=%d: %s", |
| 70 | __PRETTY_FUNCTION__, |
| 71 | errno, safe_strerror(errno)); |
| 72 | } |
| 73 | #elif defined(PIM_USE_QUAGGA_GETTIME) |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 74 | result = pim_gettime(QUAGGA_CLK_MONOTONIC, tv); |
| 75 | if (result) { |
| 76 | zlog_err("%s: pim_gettime(QUAGGA_CLK_MONOTONIC=%d) failure: errno=%d: %s", |
Everton Marques | 0ef36d8 | 2010-03-11 14:47:42 -0300 | [diff] [blame^] | 77 | __PRETTY_FUNCTION__, QUAGGA_CLK_MONOTONIC, |
| 78 | errno, safe_strerror(errno)); |
| 79 | } |
| 80 | #else |
| 81 | result = pim_gettime(CLOCK_MONOTONIC, tv); |
| 82 | if (result) { |
| 83 | zlog_err("%s: pim_gettime(CLOCK_MONOTONIC=%d) failure: errno=%d: %s", |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 84 | __PRETTY_FUNCTION__, CLOCK_MONOTONIC, |
| 85 | errno, safe_strerror(errno)); |
| 86 | } |
Everton Marques | 0ef36d8 | 2010-03-11 14:47:42 -0300 | [diff] [blame^] | 87 | #endif |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 88 | |
| 89 | return result; |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
| 93 | pim_time_monotonic_sec(): |
| 94 | number of seconds since some unspecified starting point |
| 95 | */ |
| 96 | int64_t pim_time_monotonic_sec() |
| 97 | { |
| 98 | struct timeval now_tv; |
| 99 | |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 100 | if (gettime_monotonic(&now_tv)) { |
| 101 | zlog_err("%s: gettime_monotonic() failure: errno=%d: %s", |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 102 | __PRETTY_FUNCTION__, |
Everton Marques | e96f0af | 2009-08-11 15:48:02 -0300 | [diff] [blame] | 103 | errno, safe_strerror(errno)); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | return now_tv.tv_sec; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | pim_time_monotonic_dsec(): |
| 112 | number of deciseconds since some unspecified starting point |
| 113 | */ |
| 114 | int64_t pim_time_monotonic_dsec() |
| 115 | { |
| 116 | struct timeval now_tv; |
| 117 | int64_t now_dsec; |
| 118 | |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 119 | if (gettime_monotonic(&now_tv)) { |
| 120 | zlog_err("%s: gettime_monotonic() failure: errno=%d: %s", |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 121 | __PRETTY_FUNCTION__, |
Everton Marques | e96f0af | 2009-08-11 15:48:02 -0300 | [diff] [blame] | 122 | errno, safe_strerror(errno)); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | now_dsec = ((int64_t) now_tv.tv_sec) * 10 + ((int64_t) now_tv.tv_usec) / 100000; |
| 127 | |
| 128 | return now_dsec; |
| 129 | } |
| 130 | |
| 131 | int pim_time_mmss(char *buf, int buf_size, long sec) |
| 132 | { |
| 133 | long mm; |
| 134 | int wr; |
| 135 | |
| 136 | zassert(buf_size >= 5); |
| 137 | |
| 138 | mm = sec / 60; |
| 139 | sec %= 60; |
| 140 | |
| 141 | wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec); |
| 142 | |
| 143 | return wr != 8; |
| 144 | } |
| 145 | |
| 146 | static int pim_time_hhmmss(char *buf, int buf_size, long sec) |
| 147 | { |
| 148 | long hh; |
| 149 | long mm; |
| 150 | int wr; |
| 151 | |
| 152 | zassert(buf_size >= 8); |
| 153 | |
| 154 | hh = sec / 3600; |
| 155 | sec %= 3600; |
| 156 | mm = sec / 60; |
| 157 | sec %= 60; |
| 158 | |
| 159 | wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec); |
| 160 | |
| 161 | return wr != 8; |
| 162 | } |
| 163 | |
| 164 | void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer) |
| 165 | { |
| 166 | if (t_timer) { |
| 167 | pim_time_mmss(buf, buf_size, |
| 168 | thread_timer_remain_second(t_timer)); |
| 169 | } |
| 170 | else { |
| 171 | snprintf(buf, buf_size, "--:--"); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer) |
| 176 | { |
| 177 | if (t_timer) { |
| 178 | pim_time_hhmmss(buf, buf_size, |
| 179 | thread_timer_remain_second(t_timer)); |
| 180 | } |
| 181 | else { |
| 182 | snprintf(buf, buf_size, "--:--:--"); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec) |
| 187 | { |
| 188 | zassert(buf_size >= 8); |
| 189 | |
| 190 | pim_time_hhmmss(buf, buf_size, uptime_sec); |
| 191 | } |
| 192 | |
| 193 | long pim_time_timer_remain_msec(struct thread *t_timer) |
| 194 | { |
| 195 | /* FIXME: Actually fetch msec resolution from thread */ |
| 196 | |
| 197 | /* no timer thread running means timer has expired: return 0 */ |
| 198 | |
| 199 | return t_timer ? |
| 200 | 1000 * thread_timer_remain_second(t_timer) : |
| 201 | 0; |
| 202 | } |