gdb: fix nan in scoped_time_it output

Message ID 20260928181855.3217561-1-simon.marchi@polymtl.ca
State New
Headers
Series gdb: fix nan in scoped_time_it output |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply

Commit Message

Simon Marchi Sept. 28, 2026, 6:18 p.m. UTC
  From: Simon Marchi <simon.marchi@efficios.com>

With "maintenance set per-command time on", a scope that takes less
than a millisecond prints:

  Time for "...": wall 0.000, user 0.000, sys 0.000, user+sys 0.000, -nan % CPU

The durations are converted to an integer millisecond count before the
CPU usage is computed, so a sub-millisecond scope truncates the wall
time to zero, and the division produces a NaN (or an infinity, if the
CPU time is not zero).  We don't really care about sub-millisecond
durations for the purpose of this tool, so just add a check for
`wall_ms == 0`, and report the CPU usage as 0% if true.  This is purely
cosmetic.

Change-Id: Iaf682bef4b537d6dbb58e82f7727c0ea079088aa
---
 gdb/maint.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)


base-commit: 421bec796fa3a06179856da870a13cf71ddf5df2
  

Comments

Andrew Burgess Sept. 28, 2026, 9:59 p.m. UTC | #1
simon.marchi@polymtl.ca writes:

> From: Simon Marchi <simon.marchi@efficios.com>
>
> With "maintenance set per-command time on", a scope that takes less
> than a millisecond prints:
>
>   Time for "...": wall 0.000, user 0.000, sys 0.000, user+sys 0.000, -nan % CPU
>
> The durations are converted to an integer millisecond count before the
> CPU usage is computed, so a sub-millisecond scope truncates the wall
> time to zero, and the division produces a NaN (or an infinity, if the
> CPU time is not zero).  We don't really care about sub-millisecond
> durations for the purpose of this tool, so just add a check for
> `wall_ms == 0`, and report the CPU usage as 0% if true.  This is purely
> cosmetic.

LGTM.

Approved-By: Andrew Burgess <aburgess@redhat.com>

Thanks,
Andrew

>
> Change-Id: Iaf682bef4b537d6dbb58e82f7727c0ea079088aa
> ---
>  gdb/maint.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/gdb/maint.c b/gdb/maint.c
> index 8027429e6d39..22954f624efe 100644
> --- a/gdb/maint.c
> +++ b/gdb/maint.c
> @@ -1230,7 +1230,8 @@ scoped_time_it::~scoped_time_it ()
>  		     "user+sys %.03f, %.01f %% CPU\n",
>  		     m_what, wall_ms / 1000.0, user_ms / 1000.0,
>  		     sys_ms / 1000.0, user_plus_sys_ms / 1000.0,
> -		     user_plus_sys_ms * 100.0 / wall_ms);
> +		     (wall_ms == 0
> +		      ? 0.0 : user_plus_sys_ms * 100.0 / wall_ms));
>    gdb_stdlog->write_async_safe (str.data (), str.size ());
>  }
>  
>
> base-commit: 421bec796fa3a06179856da870a13cf71ddf5df2
> -- 
> 2.55.0
  
Simon Marchi Sept. 29, 2026, 1:09 a.m. UTC | #2
On 2026-09-28 17:59, Andrew Burgess wrote:
> simon.marchi@polymtl.ca writes:
> 
>> From: Simon Marchi <simon.marchi@efficios.com>
>>
>> With "maintenance set per-command time on", a scope that takes less
>> than a millisecond prints:
>>
>>   Time for "...": wall 0.000, user 0.000, sys 0.000, user+sys 0.000, -nan % CPU
>>
>> The durations are converted to an integer millisecond count before the
>> CPU usage is computed, so a sub-millisecond scope truncates the wall
>> time to zero, and the division produces a NaN (or an infinity, if the
>> CPU time is not zero).  We don't really care about sub-millisecond
>> durations for the purpose of this tool, so just add a check for
>> `wall_ms == 0`, and report the CPU usage as 0% if true.  This is purely
>> cosmetic.
> 
> LGTM.
> 
> Approved-By: Andrew Burgess <aburgess@redhat.com>

Thanks, pushed.

Simon
  

Patch

diff --git a/gdb/maint.c b/gdb/maint.c
index 8027429e6d39..22954f624efe 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -1230,7 +1230,8 @@  scoped_time_it::~scoped_time_it ()
 		     "user+sys %.03f, %.01f %% CPU\n",
 		     m_what, wall_ms / 1000.0, user_ms / 1000.0,
 		     sys_ms / 1000.0, user_plus_sys_ms / 1000.0,
-		     user_plus_sys_ms * 100.0 / wall_ms);
+		     (wall_ms == 0
+		      ? 0.0 : user_plus_sys_ms * 100.0 / wall_ms));
   gdb_stdlog->write_async_safe (str.data (), str.size ());
 }