Skip to content
Commit 3b9af7e8 authored by Pedro Alves's avatar Pedro Alves
Browse files

Use C++11 std::chrono

The best intro I know of to std::chrono is the standard proposal for
the standard itself:
 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2661.htm

std::chrono is really great.  You should read that.

This patch:

- Avoids problems with gnulib's C++ namespace support and the timeval
  passed to gnulib::gettimeofday being incompatible with libiberty's
  timeval_sub/timeval_add.

  gettimeofday (&prompt_ended, NULL);
  timeval_sub (&prompt_delta, &prompt_ended, &prompt_started);
  timeval_add (&prompt_for_continue_wait_time,
               &prompt_for_continue_wait_time, &prompt_delta);

That's currently handled by simply not using gnulib's gettimeofday
(common/gdb_sys_time.h), but that won't work with gnulib's C++
namespace support, because that adds warnings for uses of
::gettimeofday, which are hard errors with -Werror.

- gettimeofday is not monotonic, so we shouldn't be using it for
  performance tracking, anyway...  Or put another way, for anything
  other than displaying a date/time to the user.

~~~
       The time returned by gettimeofday() is affected by
       discontinuous jumps in the system time (e.g., if the system
       administrator manually changes the system time).  If you need a
       monotonically increasing clock, see clock_gettime(2).
~~~

C++11's std::chrono has a monotonic clock exactly for such purposes
(std::chrono::steady_clock), and in addition is a very nice API to
use, designed to catch problems at compile time, and to offload factor
conversions (minutes <-> seconds <-> microseconds, etc.) to the
library (often at compile time).  It also supports the obvious <,>,-,+
operators, making code comparing timestamps or computing time
differences more natural.

Another nice thing with std::chrono is that it's super easy to create
extra clocks that follow the same interface, and leverage all the
std::chrono::duration/ratio niceties.  There's an example in the patch
(the new cpu_time_clock.h file), where I've added a couple clocks to
count user/system cpu time.
parent fa3a1307
Loading
Loading
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment