]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: timer: move the timer string api to lib
authorAnuradha Karuppiah <anuradhak@cumulusnetworks.com>
Fri, 27 Mar 2020 14:30:20 +0000 (07:30 -0700)
committerAnuradha Karuppiah <anuradhak@cumulusnetworks.com>
Wed, 5 Aug 2020 13:46:12 +0000 (06:46 -0700)
This api was earlier present in the daemon code but as multiple daemons
need it moving it to lib will avoid unnecessary copy-paste.

Signed-off-by: Anuradha Karuppiah <anuradhak@cumulusnetworks.com>
lib/thread.c
lib/thread.h

index 5c7c104842e0148bda800f02f5358808d3aa811c..1df4eee25c89a9d4da90846f534edb5758313a56 100644 (file)
@@ -634,6 +634,36 @@ struct timeval thread_timer_remain(struct thread *thread)
        return remain;
 }
 
+static int time_hhmmss(char *buf, int buf_size, long sec)
+{
+       long hh;
+       long mm;
+       int wr;
+
+       zassert(buf_size >= 8);
+
+       hh = sec / 3600;
+       sec %= 3600;
+       mm = sec / 60;
+       sec %= 60;
+
+       wr = snprintf(buf, buf_size, "%02ld:%02ld:%02ld", hh, mm, sec);
+
+       return wr != 8;
+}
+
+char *thread_timer_to_hhmmss(char *buf, int buf_size,
+               struct thread *t_timer)
+{
+       if (t_timer) {
+               time_hhmmss(buf, buf_size,
+                               thread_timer_remain_second(t_timer));
+       } else {
+               snprintf(buf, buf_size, "--:--:--");
+       }
+       return buf;
+}
+
 /* Get new thread.  */
 static struct thread *thread_get(struct thread_master *m, uint8_t type,
                                 int (*func)(struct thread *), void *arg,
index 412a4d93bfed1cd5175cbdb8a2aab0bdf7235327..c22b2105cd43969215d4a597fc29dcc3ed670e3f 100644 (file)
@@ -140,6 +140,8 @@ struct cpu_thread_history {
 /* Thread yield time.  */
 #define THREAD_YIELD_TIME_SLOT     10 * 1000L /* 10ms */
 
+#define THREAD_TIMER_STRLEN 12
+
 /* Macros. */
 #define THREAD_ARG(X) ((X)->arg)
 #define THREAD_FD(X)  ((X)->u.fd)
@@ -228,6 +230,8 @@ extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before,
 
 /* only for use in logging functions! */
 extern pthread_key_t thread_current;
+extern char *thread_timer_to_hhmmss(char *buf, int buf_size,
+               struct thread *t_timer);
 
 #ifdef __cplusplus
 }