back to topotato report
topotato coverage report
Current view: top level - pimd - pim_time.c (source / functions) Hit Total Coverage
Test: test_pim_crp.py::PIMCandidateBSRTest Lines: 33 72 45.8 %
Date: 2023-02-16 02:09:37 Functions: 6 11 54.5 %

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

Generated by: LCOV version v1.16-topotato