From e35be97295a98ccd8413bfe7d10700068ec163ca Mon Sep 17 00:00:00 2001
From: Arnaud Blanchard <arnaud.blanchard@ensea.fr>
Date: Mon, 26 Mar 2018 15:31:10 +0200
Subject: [PATCH] Give access to blc_loop_timer which contain the time of the
 beginning of the loop

---
 include/blc_command.h | 10 ++++++++++
 src/blc_loop.cpp      | 16 ++++++++--------
 src/blc_program.cpp   | 18 +++++++++---------
 3 files changed, 27 insertions(+), 17 deletions(-)

diff --git a/include/blc_command.h b/include/blc_command.h
index 2a6b45a..d02e5b2 100644
--- a/include/blc_command.h
+++ b/include/blc_command.h
@@ -37,6 +37,12 @@ Few functions helping for pseudo realtime applications.
 typedef enum {BLC_QUIT=0, BLC_RUN, BLC_PAUSE, BLC_STEPS} type_blc_status;
 typedef void (*type_blc_command_cb)(char const *argument, void *user_data);
 
+/** Timer updated by blc_command_loop_start and  blc_command_loop_end (i.e. BLC_LOOP_FOR(<period>))
+ It may be use by the application to get the absolute time in µs using blc_loop_timer.tv_sec*1000000+blc_loop_timer.tv_usec
+ It should not be changed by the application
+ */
+extern struct timeval blc_loop_timer;
+
 extern int blc_input_terminal, blc_output_terminal;
 extern type_blc_status blc_status;
 extern uint64_t blc_loop_iteration;
@@ -47,6 +53,10 @@ extern FILE *blc_profile_file;
 
 START_EXTERN_C
 
+
+
+
+
 //void * to not need to include "semaphore.h"
 void blc_loop_sem_to_unlock_on_stop(void *sem);
 
diff --git a/src/blc_loop.cpp b/src/blc_loop.cpp
index c84ae08..b12d2d5 100644
--- a/src/blc_loop.cpp
+++ b/src/blc_loop.cpp
@@ -9,7 +9,7 @@
 #include <sys/time.h> //gettimeofday
 #include <pthread.h> //pthread_mutex
 
-static struct timeval timer;
+struct timeval blc_loop_timer;
 static uint blc_period=0;
 static uint blc_duration=0;
 static uint blc_duration_min=UINT_MAX, blc_duration_max=0;
@@ -54,7 +54,7 @@ void blc_fprint_stats(FILE *file)
         blc_duration_max=0;
     }
     else fprintf(file, "%s: No new iteration yet\n", blc_program_id);
-    gettimeofday(&timer, NULL);
+    gettimeofday(&blc_loop_timer, NULL);
 }
 
 static void display_stats(){
@@ -235,7 +235,7 @@ int blc_command_loop_start(){
     int i;
     
     
-    if (blc_profile_file) gettimeofday(&profile_timer, NULL);
+    if (blc_profile_file) gettimeofday(&profile_timer, NULL); //this is only for profiling
 
     //We wait before counting the duration time as the time for waiting does not matter
     if (blc_loop_iteration==blc_loop_iteration_limit){
@@ -256,11 +256,11 @@ int blc_command_loop_start(){
         //Waiting general user defined callbacks
         FOR(i, waiting_callbacks_nb) waiting_callbacks[i](waiting_callabacks_user_data[i]);
     }
-    else blc_command_loop_period=-3; //In case we start another loop after
+    else blc_command_loop_period=-3; //In case we start another loop after by mistake
     
     //We start counting the duration time
-    if (intermediate_iteration==0) blc_us_time_diff(&timer);
-    else  blc_period+=blc_current_duration + blc_us_time_diff(&timer);
+    if (intermediate_iteration==0) blc_us_time_diff(&blc_loop_timer);
+    else  blc_period+=blc_current_duration + blc_us_time_diff(&blc_loop_timer);
     
     return blc_status;
 }
@@ -271,8 +271,8 @@ void blc_command_loop_end(){
     int diff, i;
     uint64_t start_time;
 
-    if (blc_profile_file) start_time=timer.tv_sec*1000000+timer.tv_usec;
-    blc_current_duration=blc_us_time_diff(&timer);
+    if (blc_profile_file) start_time=blc_loop_timer.tv_sec*1000000+blc_loop_timer.tv_usec;
+    blc_current_duration=blc_us_time_diff(&blc_loop_timer);
     if (blc_profile_file) fprintf(blc_profile_file, "%16ld\t%16lld\t%8lu\t\n",profile_timer.tv_sec*1000000+profile_timer.tv_usec, start_time, blc_current_duration);
  //   fflush(stderr);
     
diff --git a/src/blc_program.cpp b/src/blc_program.cpp
index 1647b91..60df22d 100644
--- a/src/blc_program.cpp
+++ b/src/blc_program.cpp
@@ -454,7 +454,6 @@ void blc_program_init(int *argc, char ***argv, void (*exit_cb)(void))
 {
     char  const*help, *profile;
     struct sigaction action;
-    struct timeval timer;
     
     CLEAR(action);
     action.sa_handler = on_sigterm;
@@ -484,14 +483,6 @@ void blc_program_init(int *argc, char ***argv, void (*exit_cb)(void))
     blc_program_add_option(&profile, '~', "profile", "filename", "profile the execution of the loop", NULL);
     blc_program_add_option(&help, 'h', "help", 0, "display this help", NULL);
     blc_program_parse_args_and_print_title(argc, argv);
-    
-    if (profile){
-        SYSTEM_ERROR_CHECK(blc_profile_file=fopen(profile, "w"), NULL, "filename: %s", profile);
-        fprintf(blc_profile_file, "#start_time(µs) \texec_time(µs)    \tduration(µs)    \t\n");
-        gettimeofday(&timer, NULL);
-        fprintf(blc_profile_file, "%16lu\t               0\t       0\t\n", timer.tv_sec*1000000+timer.tv_usec);
-    }
-    
     if (help){
         blc_program_args_display_help();
         exit(EXIT_SUCCESS);
@@ -499,6 +490,15 @@ void blc_program_init(int *argc, char ***argv, void (*exit_cb)(void))
     
     blc_program_check_full_parsing(*argc, *argv);
     if (exit_cb) atexit(exit_cb); //This will be called first
+    
+    
+    //We consider it is the begining of the program
+    gettimeofday(&blc_loop_timer, NULL);
+    if (profile){
+        SYSTEM_ERROR_CHECK(blc_profile_file=fopen(profile, "w"), NULL, "filename: %s", profile);
+        fprintf(blc_profile_file, "#start_time(µs) \texec_time(µs)    \tduration(µs)    \t\n");
+        fprintf(blc_profile_file, "%16lu\t               0\t       0\t\n", blc_loop_timer.tv_sec*1000000+blc_loop_timer.tv_usec);
+    }
 }
 
 void blc_program_check_full_parsing(int argc, char **argv){
-- 
GitLab