diff mbox series

[1/3] xenbaked: properly use time_t in dump_stats()

Message ID c87ae25d-5aa8-bb70-7d2b-ea6485a01f59@suse.com (mailing list archive)
State New, archived
Headers show
Series tools: address recently reported Coverity issues | expand

Commit Message

Jan Beulich Aug. 18, 2022, 2:05 p.m. UTC
"int" is not a suitable type to convert time()'s return value to. Avoid
casts and other extra fiddling by using difftime(), on the assumption
that the overhead of using "double" doesn't matter here.

Coverity ID: 1509374
Signed-off-by: Jan Beulich <jbeulich@suse.com>

Comments

Anthony PERARD Aug. 18, 2022, 4:34 p.m. UTC | #1
On Thu, Aug 18, 2022 at 04:05:37PM +0200, Jan Beulich wrote:
> "int" is not a suitable type to convert time()'s return value to. Avoid
> casts and other extra fiddling by using difftime(), on the assumption
> that the overhead of using "double" doesn't matter here.

dump_stats() seems to be only used once, so it's probably fine.

> Coverity ID: 1509374
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> 
> --- unstable.orig/tools/xenmon/xenbaked.c	2022-01-06 16:12:39.000000000 +0100
> +++ unstable/tools/xenmon/xenbaked.c	2022-08-18 15:36:39.918706853 +0200
> @@ -230,11 +230,7 @@ static void check_gotten_sum(void)
>  static void dump_stats(void) 
>  {
>      stat_map_t *smt = stat_map;
> -    time_t end_time, run_time;
> -
> -    time(&end_time);
> -
> -    run_time = end_time - start_time;
> +    double run_time = difftime(time(NULL), start_time);
>  
>      printf("Event counts:\n");
>      while (smt->text != NULL) {
> @@ -242,13 +238,11 @@ static void dump_stats(void)
>          smt++;
>      }
>  
> -    printf("processed %d total records in %d seconds (%ld per second)\n",
> -           rec_count, (int)run_time,
> -           run_time ? (long)(rec_count/run_time) : 0L);
> +    printf("processed %d total records in %0f seconds (%0f per second)\n",

Did you want to write "%.0f" or even "%.f" instead of "%0f"? It seems
that the '0' here mean '0'-padded, but without field width there would
be nothing to pad. I'm guessing you would want printf to write something
like "266 seconds" instead of "266.646168 seconds". Am I missing
something ?

> +           rec_count, run_time, run_time ? rec_count / run_time : 0);
>  
> -    printf("woke up %d times in %d seconds (%ld per second)\n",
> -           wakeups, (int) run_time,
> -           run_time ? (long)(wakeups/run_time) : 0L);
> +    printf("woke up %d times in %0f seconds (%0f per second)\n",
> +           wakeups, run_time, run_time ? wakeups / run_time : 0);
>  
>      check_gotten_sum();
>  }
>
Jan Beulich Aug. 19, 2022, 5:56 a.m. UTC | #2
On 18.08.2022 18:34, Anthony PERARD wrote:
> On Thu, Aug 18, 2022 at 04:05:37PM +0200, Jan Beulich wrote:
>> @@ -242,13 +238,11 @@ static void dump_stats(void)
>>          smt++;
>>      }
>>  
>> -    printf("processed %d total records in %d seconds (%ld per second)\n",
>> -           rec_count, (int)run_time,
>> -           run_time ? (long)(rec_count/run_time) : 0L);
>> +    printf("processed %d total records in %0f seconds (%0f per second)\n",
> 
> Did you want to write "%.0f" or even "%.f" instead of "%0f"? It seems
> that the '0' here mean '0'-padded, but without field width there would
> be nothing to pad. I'm guessing you would want printf to write something
> like "266 seconds" instead of "266.646168 seconds". Am I missing
> something ?

Indeed, I did miss the . in all cases. How embarrassing ...

Jan
diff mbox series

Patch

--- unstable.orig/tools/xenmon/xenbaked.c	2022-01-06 16:12:39.000000000 +0100
+++ unstable/tools/xenmon/xenbaked.c	2022-08-18 15:36:39.918706853 +0200
@@ -230,11 +230,7 @@  static void check_gotten_sum(void)
 static void dump_stats(void) 
 {
     stat_map_t *smt = stat_map;
-    time_t end_time, run_time;
-
-    time(&end_time);
-
-    run_time = end_time - start_time;
+    double run_time = difftime(time(NULL), start_time);
 
     printf("Event counts:\n");
     while (smt->text != NULL) {
@@ -242,13 +238,11 @@  static void dump_stats(void)
         smt++;
     }
 
-    printf("processed %d total records in %d seconds (%ld per second)\n",
-           rec_count, (int)run_time,
-           run_time ? (long)(rec_count/run_time) : 0L);
+    printf("processed %d total records in %0f seconds (%0f per second)\n",
+           rec_count, run_time, run_time ? rec_count / run_time : 0);
 
-    printf("woke up %d times in %d seconds (%ld per second)\n",
-           wakeups, (int) run_time,
-           run_time ? (long)(wakeups/run_time) : 0L);
+    printf("woke up %d times in %0f seconds (%0f per second)\n",
+           wakeups, run_time, run_time ? wakeups / run_time : 0);
 
     check_gotten_sum();
 }