Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74457 - in trunk/boost/chrono/stopwatches: formatters reporters
From: vicente.botet_at_[hidden]
Date: 2011-09-18 17:09:46


Author: viboes
Date: 2011-09-18 17:09:45 EDT (Sun, 18 Sep 2011)
New Revision: 74457
URL: http://svn.boost.org/trac/boost/changeset/74457

Log:
Chrono: Added times formatter + default formatter for process and thread clocks
Added:
   trunk/boost/chrono/stopwatches/formatters/times_formatter.hpp (contents, props changed)
   trunk/boost/chrono/stopwatches/reporters/process_default_formatter.hpp (contents, props changed)
   trunk/boost/chrono/stopwatches/reporters/thread_default_formatter.hpp (contents, props changed)
Text files modified:
   trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp | 16 ++++++++++++++++
   1 files changed, 16 insertions(+), 0 deletions(-)

Added: trunk/boost/chrono/stopwatches/formatters/times_formatter.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/formatters/times_formatter.hpp 2011-09-18 17:09:45 EDT (Sun, 18 Sep 2011)
@@ -0,0 +1,122 @@
+// boost/chrono/stopwatches/stopwatch_formatter.hpp ------------------------------------------------------------//
+// Copyright 2011 Vicente J. Botet Escriba
+// Distributed under the Boost Software License, Version 1.0.
+// See http://www.boost.org/LICENSE_1_0.txt
+// See http://www.boost.org/libs/chrono/stopwatches for documentation.
+
+#ifndef BOOST_CHRONO_STOPWATCHES_FORMATTERS_TIMES_HPP
+#define BOOST_CHRONO_STOPWATCHES_FORMATTERS_TIMES_HPP
+
+#include <boost/chrono/stopwatches/formatters/base_formatter.hpp>
+#include <boost/system/error_code.hpp>
+#include <boost/current_function.hpp>
+//#include <boost/chrono/stopwatches/detail/adaptive_string.hpp>
+#include <boost/format.hpp>
+#include <boost/format/group.hpp>
+#include <boost/cstdint.hpp>
+#include <boost/assert.hpp>
+#include <string>
+#include <iostream>
+#include <cassert>
+#include <iomanip>
+
+#define BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT_DEFAULT "real %1%, cpu %4% (%5%%%), user %2%, system %3%\n"
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<typename Ratio = micro, typename CharT = char,
+ typename Traits = std::char_traits<CharT>,
+ class Alloc = std::allocator<CharT> >
+ class basic_times_formatter: public base_formatter<CharT, Traits>
+ {
+
+ public:
+ typedef base_formatter<CharT, Traits> base_type;
+ typedef basic_format<CharT, Traits> format_type;
+ typedef std::basic_string<CharT, Traits, Alloc> string_type;
+ typedef CharT char_type;
+ typedef std::basic_ostream<CharT, Traits> ostream_type;
+
+ basic_times_formatter() :
+ base_type(),
+ internal_fmt_(BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT_DEFAULT),
+ fmt_(internal_fmt_)
+ {
+ }
+ basic_times_formatter(ostream_type& os) :
+ base_type(os),
+ internal_fmt_(BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT_DEFAULT),
+ fmt_(internal_fmt_)
+ {
+ }
+ basic_times_formatter(const char* fmt, ostream_type& os = std::cout) :
+ base_type(os), internal_fmt_(fmt), fmt_(internal_fmt_)
+ {
+ }
+ basic_times_formatter(string_type const& fmt, ostream_type& os =
+ std::cout) :
+ base_type(os), internal_fmt_(fmt), fmt_(internal_fmt_)
+ {
+ }
+ basic_times_formatter(format_type & fmt, ostream_type& os = std::cout) :
+ base_type(os), fmt_(fmt)
+ {
+ }
+
+// static string_type format(const char* s)
+// {
+// string_type res(s);
+// res += boost::chrono::detail::adaptive_string(" : ");
+// res += BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT_DEFAULT;
+// return res;
+// }
+
+ template<class Stopwatch>
+ void operator()(Stopwatch & stopwatch_, system::error_code & ec)
+ {
+ typedef typename Stopwatch::duration::rep times_type;
+ times_type times = stopwatch_.elapsed(ec).count();
+
+ if (times.real < 0)
+ return;
+ double p;
+ if (times.real > 0)
+ p=double(times.user+times.system)*100/times.real;
+ else
+ p=0;
+
+ this->os_ << fmt_
+ % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.real)))
+ % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.user)))
+ % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.system)))
+ % io::group(std::fixed, std::setprecision(this->precision_), duration_fmt(this->style_), boost::chrono::duration<double, Ratio>(nanoseconds(times.user+times.system)))
+ % io::group(std::fixed, std::setprecision(2), p)
+ << std::endl;
+ }
+ private:
+ boost::format internal_fmt_;
+ boost::format& fmt_;
+ };
+
+ typedef basic_times_formatter<milli, char> times_formatter;
+ typedef basic_times_formatter<milli, wchar_t> wtimes_formatter;
+
+ } // namespace chrono
+} // namespace boost
+
+#if 0
+#define BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT(F) \
+ boost::chrono::detail::adaptive_string(F " : " BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT_DEFAULT)
+#ifdef __GNUC__
+#define BOOST_CHRONO_STOPWATCHES_TIMES_FUNCTION_FORMAT \
+ boost::chrono::times_formatter::format(BOOST_CURRENT_FUNCTION)
+#else
+#define BOOST_CHRONO_STOPWATCHES_TIMES_FUNCTION_FORMAT \
+ BOOST_CHRONO_STOPWATCHES_TIMES_FORMAT(BOOST_CURRENT_FUNCTION)
+#endif
+#endif
+
+#endif

Added: trunk/boost/chrono/stopwatches/reporters/process_default_formatter.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/reporters/process_default_formatter.hpp 2011-09-18 17:09:45 EDT (Sun, 18 Sep 2011)
@@ -0,0 +1,77 @@
+// boost/chrono/stopwatches/stopwatch_reporter.hpp
+// Copyright 2011 Vicente J. Botet Escriba
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or
+// copy at http://www.boost.org/LICENSE_1_0.txt)
+// See http://www.boost.org/libs/chrono/stopwatches for documentation.
+
+#ifndef BOOST_CHRONO_STOPWATCHES_REPORTERS_PROCESS_DEFAULT_FORMATTER_HPP
+#define BOOST_CHRONO_STOPWATCHES_REPORTERS_PROCESS_DEFAULT_FORMATTER_HPP
+
+#include <boost/chrono/stopwatches/reporters/stopwatch_reporter_default_formatter.hpp>
+#include <boost/chrono/stopwatches/formatters/elapsed_formatter.hpp>
+#include <boost/chrono/stopwatches/formatters/times_formatter.hpp>
+#include <boost/chrono/stopwatches/simple_stopwatch.hpp>
+#include <boost/chrono/process_cpu_clocks.hpp>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<>
+ struct stopwatch_reporter_default_formatter<simple_stopwatch<process_real_cpu_clock> >
+ {
+ typedef elapsed_formatter type;
+ };
+
+ template<>
+ struct wstopwatch_reporter_default_formatter<simple_stopwatch<process_real_cpu_clock> >
+ {
+ typedef welapsed_formatter type;
+ };
+
+ template<>
+ struct stopwatch_reporter_default_formatter<simple_stopwatch<process_user_cpu_clock> >
+ {
+ typedef elapsed_formatter type;
+ };
+
+ template<>
+ struct wstopwatch_reporter_default_formatter<simple_stopwatch<process_user_cpu_clock> >
+ {
+ typedef welapsed_formatter type;
+ };
+
+ template<>
+ struct stopwatch_reporter_default_formatter<simple_stopwatch<process_system_cpu_clock> >
+ {
+ typedef elapsed_formatter type;
+ };
+
+ template<>
+ struct wstopwatch_reporter_default_formatter<simple_stopwatch<process_system_cpu_clock> >
+ {
+ typedef welapsed_formatter type;
+ };
+
+ template<>
+ struct stopwatch_reporter_default_formatter<simple_stopwatch<process_cpu_clock> >
+ {
+ typedef times_formatter type;
+ };
+
+ template<>
+ struct wstopwatch_reporter_default_formatter<simple_stopwatch<process_cpu_clock> >
+ {
+ typedef wtimes_formatter type;
+ };
+
+ } // namespace chrono
+} // namespace boost
+
+
+
+#endif
+
+

Modified: trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp
==============================================================================
--- trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp (original)
+++ trunk/boost/chrono/stopwatches/reporters/stopwatch_reporter.hpp 2011-09-18 17:09:45 EDT (Sun, 18 Sep 2011)
@@ -42,6 +42,11 @@
       {
       }
 
+ basic_stopwatch_reporter(system::error_code & ec) :
+ Stopwatch(ec), formatter_(), reported_(false)
+ {
+ }
+
       explicit basic_stopwatch_reporter(const typename Formatter::char_type* fmt) :
         formatter_(fmt), reported_(false)
       {
@@ -109,10 +114,17 @@
       //: base_type()
       {
       }
+
+ stopwatch_reporter(system::error_code & ec) :
+ base_type(ec)
+ {
+ }
+
       explicit stopwatch_reporter(formatter_type const& fmt) :
         base_type(fmt)
       {
       }
+
       explicit stopwatch_reporter(const typename Formatter::char_type* fmt) :
         base_type(fmt)
       {
@@ -162,6 +174,10 @@
         base_type()
       {
       }
+ wstopwatch_reporter(system::error_code & ec) :
+ base_type(ec)
+ {
+ }
       explicit wstopwatch_reporter(formatter_type const& fmt) :
         base_type(fmt)
       {

Added: trunk/boost/chrono/stopwatches/reporters/thread_default_formatter.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/stopwatches/reporters/thread_default_formatter.hpp 2011-09-18 17:09:45 EDT (Sun, 18 Sep 2011)
@@ -0,0 +1,43 @@
+// boost/chrono/stopwatches/stopwatch_reporter.hpp
+// Copyright 2011 Vicente J. Botet Escriba
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE_1_0.txt or
+// copy at http://www.boost.org/LICENSE_1_0.txt)
+// See http://www.boost.org/libs/chrono/stopwatches for documentation.
+
+#ifndef BOOST_CHRONO_STOPWATCHES_REPORTERS_THREAD_DEFAULT_FORMATTER_HPP
+#define BOOST_CHRONO_STOPWATCHES_REPORTERS_THREAD_DEFAULT_FORMATTER_HPP
+
+#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
+
+#include <boost/chrono/stopwatches/reporters/stopwatch_reporter_default_formatter.hpp>
+#include <boost/chrono/stopwatches/formatters/elapsed_formatter.hpp>
+#include <boost/chrono/stopwatches/simple_stopwatch.hpp>
+#include <boost/chrono/thread_clock.hpp>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<>
+ struct stopwatch_reporter_default_formatter<simple_stopwatch<thread_clock> >
+ {
+ typedef elapsed_formatter type;
+ };
+
+ template<>
+ struct wstopwatch_reporter_default_formatter<simple_stopwatch<thread_clock> >
+ {
+ typedef welapsed_formatter type;
+ };
+
+ } // namespace chrono
+} // namespace boost
+
+
+#endif
+
+#endif
+
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk