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 | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 33 | static int gettime_monotonic(struct timeval *tv) |
| 34 | { |
| 35 | int result; |
| 36 | |
Everton Marques | 0ef36d8 | 2010-03-11 14:47:42 -0300 | [diff] [blame] | 37 | result = gettimeofday(tv, 0); |
| 38 | if (result) { |
| 39 | zlog_err("%s: gettimeofday() failure: errno=%d: %s", |
| 40 | __PRETTY_FUNCTION__, |
| 41 | errno, safe_strerror(errno)); |
| 42 | } |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 43 | |
| 44 | return result; |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* |
| 48 | pim_time_monotonic_sec(): |
| 49 | number of seconds since some unspecified starting point |
| 50 | */ |
| 51 | int64_t pim_time_monotonic_sec() |
| 52 | { |
| 53 | struct timeval now_tv; |
| 54 | |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 55 | if (gettime_monotonic(&now_tv)) { |
| 56 | zlog_err("%s: gettime_monotonic() failure: errno=%d: %s", |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 57 | __PRETTY_FUNCTION__, |
Everton Marques | e96f0af | 2009-08-11 15:48:02 -0300 | [diff] [blame] | 58 | errno, safe_strerror(errno)); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | return now_tv.tv_sec; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | pim_time_monotonic_dsec(): |
| 67 | number of deciseconds since some unspecified starting point |
| 68 | */ |
| 69 | int64_t pim_time_monotonic_dsec() |
| 70 | { |
| 71 | struct timeval now_tv; |
| 72 | int64_t now_dsec; |
| 73 | |
Everton Marques | f9e05e5 | 2010-03-11 11:17:33 -0300 | [diff] [blame] | 74 | if (gettime_monotonic(&now_tv)) { |
| 75 | zlog_err("%s: gettime_monotonic() failure: errno=%d: %s", |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 76 | __PRETTY_FUNCTION__, |
Everton Marques | e96f0af | 2009-08-11 15:48:02 -0300 | [diff] [blame] | 77 | errno, safe_strerror(errno)); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | now_dsec = ((int64_t) now_tv.tv_sec) * 10 + ((int64_t) now_tv.tv_usec) / 100000; |
| 82 | |
| 83 | return now_dsec; |
| 84 | } |
| 85 | |
| 86 | int pim_time_mmss(char *buf, int buf_size, long sec) |
| 87 | { |
| 88 | long mm; |
| 89 | int wr; |
| 90 | |
| 91 | zassert(buf_size >= 5); |
| 92 | |
| 93 | mm = sec / 60; |
| 94 | sec %= 60; |
| 95 | |
| 96 | wr = snprintf(buf, buf_size, "%02ld:%02ld", mm, sec); |
| 97 | |
| 98 | return wr != 8; |
| 99 | } |
| 100 | |
| 101 | static int pim_time_hhmmss(char *buf, int buf_size, long sec) |
| 102 | { |
| 103 | long hh; |
| 104 | long mm; |
| 105 | int wr; |
| 106 | |
| 107 | zassert(buf_size >= 8); |
| 108 | |
| 109 | hh = sec / 3600; |
| 110 | sec %= 3600; |
| 111 | mm = sec / 60; |
| 112 | sec %= 60; |
| 113 | |
| 114 | wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec); |
| 115 | |
| 116 | return wr != 8; |
| 117 | } |
| 118 | |
| 119 | void pim_time_timer_to_mmss(char *buf, int buf_size, struct thread *t_timer) |
| 120 | { |
| 121 | if (t_timer) { |
| 122 | pim_time_mmss(buf, buf_size, |
| 123 | thread_timer_remain_second(t_timer)); |
| 124 | } |
| 125 | else { |
| 126 | snprintf(buf, buf_size, "--:--"); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void pim_time_timer_to_hhmmss(char *buf, int buf_size, struct thread *t_timer) |
| 131 | { |
| 132 | if (t_timer) { |
| 133 | pim_time_hhmmss(buf, buf_size, |
| 134 | thread_timer_remain_second(t_timer)); |
| 135 | } |
| 136 | else { |
| 137 | snprintf(buf, buf_size, "--:--:--"); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void pim_time_uptime(char *buf, int buf_size, int64_t uptime_sec) |
| 142 | { |
| 143 | zassert(buf_size >= 8); |
| 144 | |
| 145 | pim_time_hhmmss(buf, buf_size, uptime_sec); |
| 146 | } |
| 147 | |
Everton Marques | ff752d4 | 2010-03-17 10:34:24 -0300 | [diff] [blame] | 148 | void pim_time_uptime_begin(char *buf, int buf_size, int64_t now, int64_t begin) |
| 149 | { |
| 150 | if (begin > 0) |
| 151 | pim_time_uptime(buf, buf_size, now - begin); |
| 152 | else |
| 153 | snprintf(buf, buf_size, "--:--:--"); |
| 154 | } |
| 155 | |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 156 | long pim_time_timer_remain_msec(struct thread *t_timer) |
| 157 | { |
| 158 | /* FIXME: Actually fetch msec resolution from thread */ |
| 159 | |
| 160 | /* no timer thread running means timer has expired: return 0 */ |
| 161 | |
| 162 | return t_timer ? |
| 163 | 1000 * thread_timer_remain_second(t_timer) : |
| 164 | 0; |
| 165 | } |