Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: Pedro Alves <palves@redhat.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [PATCH] gdb: Use C++11 std::chrono
Date: Thu, 17 Nov 2016 22:15:00 -0000	[thread overview]
Message-ID: <284edd6f2f85cd2c7cb9306a07a15b2a@polymtl.ca> (raw)
In-Reply-To: <1479402927-4639-1-git-send-email-palves@redhat.com>

On 2016-11-17 12:15, Pedro Alves wrote:
> This patch fixes a few problems with GDB's time handling.
> [...]

Indeed, it makes the code very nice and readable.  My only opinion about 
std::chrono so far was that it was so much more painful to do 
std::this_thread::sleep_for(std::chrono::seconds(2)) than sleep(2). :)

> +/* Count the total amount of time spent executing in user mode.  */
> 
> +user_cpu_time_clock::time_point
> +user_cpu_time_clock::now () noexcept
> +{
> +  return time_point (microseconds (get_run_time ()));

Looking at get_run_time() in libiberty, it seems like it returns user 
time + system time.  Doesn't it contradict the naming of this clock and 
the comment of this method?

> -      /* If gettimeofday doesn't exist, and as a portability solution 
> it has
> -	 been replaced with, e.g., time, then it doesn't make sense to print
> -	 the microseconds field.  Is there a way to check for that?  */
> -      fprintf (stderr, "%ld:%06ld ", (long) tm.tv_sec, (long) 
> tm.tv_usec);
> +      fprintf (stderr, "%ld:%06ld ", s.count (), us.count ());

Unrelated to your change, but I find it weird to format the time as 
"seconds:useconds".  I wouldn't object if you sneakily changed it to 
"seconds.useconds". :)

> @@ -2390,8 +2383,8 @@ mi_load_progress (const char *section_name,
>  		  unsigned long total_sent,
>  		  unsigned long grand_total)
>  {
> -  struct timeval time_now, delta, update_threshold;
> -  static struct timeval last_update;
> +  using namespace std::chrono;
> +  static steady_clock::time_point last_update;
>    static char *previous_sect_name = NULL;
>    int new_section;
>    struct ui_out *saved_uiout;
> @@ -2416,18 +2409,9 @@ mi_load_progress (const char *section_name,
> 
>    uiout = current_uiout;
> 
> -  update_threshold.tv_sec = 0;
> -  update_threshold.tv_usec = 500000;
> -  gettimeofday (&time_now, NULL);
> -
> -  delta.tv_usec = time_now.tv_usec - last_update.tv_usec;
> -  delta.tv_sec = time_now.tv_sec - last_update.tv_sec;
> -
> -  if (delta.tv_usec < 0)
> -    {
> -      delta.tv_sec -= 1;
> -      delta.tv_usec += 1000000L;
> -    }
> +  microseconds update_threshold (500000);
> +  steady_clock::time_point time_now = steady_clock::now ();
> +  steady_clock::duration delta = time_now - last_update;

Can this be simplified to avoid having the delta variable?  Since we can 
easily compare two time_points, we can probably make it look like:

   if (steady_clock::now () >= next_update)
     {
       next_update = steady_clock::now() + update_threshold;
       ...
     }

or

   if (steady_clock::now () >= (last_update + update_threshold))
     {
       last_update = steady_clock::now ();
       ...
     }

Also, wouldn't "update_period" or "update_rate" be more precise than 
"update_threshold"?

>  static void
>  timestamp (struct mi_timestamp *tv)
>  {
> -  gettimeofday (&tv->wallclock, NULL);
> -#ifdef HAVE_GETRUSAGE
> -  getrusage (RUSAGE_SELF, &rusage);
> -  tv->utime.tv_sec = rusage.ru_utime.tv_sec;
> -  tv->utime.tv_usec = rusage.ru_utime.tv_usec;
> -  tv->stime.tv_sec = rusage.ru_stime.tv_sec;
> -  tv->stime.tv_usec = rusage.ru_stime.tv_usec;
> -#else
> -  {
> -    long usec = get_run_time ();
> -
> -    tv->utime.tv_sec = usec/1000000L;
> -    tv->utime.tv_usec = usec - 1000000L*tv->utime.tv_sec;
> -    tv->stime.tv_sec = 0;
> -    tv->stime.tv_usec = 0;
> -  }
> -#endif
> +  using namespace std::chrono;

It's not written in the coding style guide, but I think it would be nice 
to have a newline between the "using" declaration and the following 
lines, like we do with variable declarations.

> -/* Report how fast the transfer went.  */
> +/* Report on STREAM the performance of a memory transfer operation,
> +   such as 'load'.  DATA_COUNT is the number of bytes transferred.
> +   WRITE_COUNT is the number of separate write operations, or 0, if
> +   that information is not available.  TIME is the range of time in
> +   which the operation lasted.  */
> 
> -void
> +static void
>  print_transfer_performance (struct ui_file *stream,
>  			    unsigned long data_count,
>  			    unsigned long write_count,
> -			    const struct timeval *start_time,
> -			    const struct timeval *end_time)
> +			    const time_range &time)

Is there a reason you couldn't use a std::chrono::duration instead of a 
time_range here?  The function doesn't seem to care about the particular 
time_points, only their difference.

> -      timestamp = xstrprintf ("%ld:%ld %s%s",
> -			      (long) tm.tv_sec, (long) tm.tv_usec,
> -			      linebuffer,
> -			      need_nl ? "\n": "");
> -      make_cleanup (xfree, timestamp);
> -      fputs_unfiltered (timestamp, stream);
> +      std::string timestamp = string_printf ("%ld:%ld %s%s",

Same comment about secs:usecs vs secs.usecs.


  reply	other threads:[~2016-11-17 22:15 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-17 17:15 Pedro Alves
2016-11-17 22:15 ` Simon Marchi [this message]
2016-11-23  0:59   ` [PATCH v2] " Pedro Alves
2016-11-23  2:01     ` Simon Marchi
2016-11-23  2:17       ` Pedro Alves
2016-11-23  2:27         ` Simon Marchi
2016-11-23  2:36           ` Pedro Alves
2016-11-23  2:57             ` Simon Marchi
2016-11-23 14:48               ` Simon Marchi
2016-11-23 16:02                 ` Pedro Alves
2016-11-23 16:11                   ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=284edd6f2f85cd2c7cb9306a07a15b2a@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox