From 69daf4735de2f0695938b3b37ef5534f78d69284 Mon Sep 17 00:00:00 2001 From: Arvin Schnell Date: Tue, 27 Jan 2015 12:34:16 +0100 Subject: [PATCH] - use C++11 chrono::steady_clock --- dbus/DBusMainLoop.cc | 45 ++++++++++++++++++----------------------- dbus/DBusMainLoop.h | 16 +++++++++------ package/snapper.changes | 5 +++++ server/Client.cc | 2 +- server/MetaSnapper.cc | 26 +++++++----------------- server/MetaSnapper.h | 10 ++++----- server/snapperd.cc | 20 +++++++++--------- snapper/AppUtil.cc | 17 +++++++--------- snapper/AppUtil.h | 5 +++-- 9 files changed, 68 insertions(+), 78 deletions(-) diff --git a/dbus/DBusMainLoop.cc b/dbus/DBusMainLoop.cc index 08c0eb49..67d4f0b7 100644 --- a/dbus/DBusMainLoop.cc +++ b/dbus/DBusMainLoop.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) [2012-2014] Novell, Inc. + * Copyright (c) [2012-2015] Novell, Inc. * * All Rights Reserved. * @@ -22,7 +22,6 @@ #include #include -#include #include "DBusMainLoop.h" @@ -106,17 +105,16 @@ namespace DBus } } - int timeout = periodic_timeout(); + milliseconds timeout = periodic_timeout(); - if (idle_timeout >= 0) + if (idle_timeout.count() >= 0) { - int time_left = last_action - monotonic_clock() + idle_timeout; - - if (timeout > time_left * 1000 || timeout == -1) - timeout = time_left * 1000; + steady_clock::duration time_left = idle_for() + idle_timeout; + if (timeout > time_left || timeout.count() < 0) + timeout = duration_cast(time_left); } - int r = poll(&pollfds[0], pollfds.size(), timeout); + int r = poll(&pollfds[0], pollfds.size(), timeout.count()); if (r == -1) throw FatalException(); @@ -165,11 +163,10 @@ namespace DBus dispatch_incoming(msg); } - if (idle_timeout >= 0) + if (idle_timeout.count() >= 0) { - int time_left = last_action - monotonic_clock() + idle_timeout; - - if (time_left <= 0) + steady_clock::duration time_left = idle_for() + idle_timeout; + if (time_left.count() <= 0) break; } } @@ -177,16 +174,23 @@ namespace DBus void - MainLoop::set_idle_timeout(int s) + MainLoop::set_idle_timeout(milliseconds idle_timeout) { - idle_timeout = s; + MainLoop::idle_timeout = idle_timeout; } void MainLoop::reset_idle_count() { - last_action = monotonic_clock(); + last_action = steady_clock::now(); + } + + + milliseconds + MainLoop::idle_for() const + { + return duration_cast(last_action - steady_clock::now()); } @@ -341,13 +345,4 @@ namespace DBus } } - - time_t - DBus::MainLoop::monotonic_clock() - { - struct timespec tmp; - clock_gettime(CLOCK_MONOTONIC, &tmp); - return tmp.tv_sec; - } - } diff --git a/dbus/DBusMainLoop.h b/dbus/DBusMainLoop.h index d170de58..15786578 100644 --- a/dbus/DBusMainLoop.h +++ b/dbus/DBusMainLoop.h @@ -1,5 +1,5 @@ /* - * Copyright (c) [2012-2014] Novell, Inc. + * Copyright (c) [2012-2015] Novell, Inc. * * All Rights Reserved. * @@ -25,6 +25,7 @@ #include +#include #include "DBusConnection.h" @@ -32,6 +33,9 @@ namespace DBus { + using namespace std::chrono; + + class MainLoop : public Connection { public: @@ -41,7 +45,7 @@ namespace DBus void run(); - void set_idle_timeout(int s); + void set_idle_timeout(milliseconds idle_timeout); void reset_idle_count(); void add_client_match(const string& name); @@ -50,7 +54,7 @@ namespace DBus virtual void method_call(Message& message) = 0; virtual void signal(Message& message) = 0; virtual void client_disconnected(const string& name) = 0; - virtual int periodic_timeout() = 0; + virtual milliseconds periodic_timeout() = 0; virtual void periodic() = 0; private: @@ -94,10 +98,10 @@ namespace DBus void dispatch_incoming(Message& message); - int idle_timeout; - time_t last_action; + milliseconds idle_for() const; - static time_t monotonic_clock(); + milliseconds idle_timeout; + steady_clock::time_point last_action; }; diff --git a/package/snapper.changes b/package/snapper.changes index 42472c84..dd5f5180 100644 --- a/package/snapper.changes +++ b/package/snapper.changes @@ -1,3 +1,8 @@ +------------------------------------------------------------------- +Tue Jan 27 12:31:45 CET 2015 - aschnell@suse.de + +- use C++11 chrono::steady_clock + ------------------------------------------------------------------- Mon Jan 19 12:42:13 CET 2015 - aschnell@suse.de diff --git a/server/Client.cc b/server/Client.cc index 8521a444..1c1bfc41 100644 --- a/server/Client.cc +++ b/server/Client.cc @@ -1352,7 +1352,7 @@ Client::debug(DBus::Connection& conn, DBus::Message& msg) const { s << ", loaded"; if (it->use_count() == 0) - s << ", unused for " << it->unused_for() << "s"; + s << ", unused for " << duration_cast(it->unused_for()).count() << "ms"; else s << ", use count " << it->use_count(); } diff --git a/server/MetaSnapper.cc b/server/MetaSnapper.cc index 48ed4b11..18a89f8c 100644 --- a/server/MetaSnapper.cc +++ b/server/MetaSnapper.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) [2012-2014] Novell, Inc. + * Copyright (c) [2012-2015] Novell, Inc. * * All Rights Reserved. * @@ -35,7 +35,7 @@ MetaSnappers meta_snappers; RefCounter::RefCounter() - : counter(0), last_used(monotonic_time()) + : counter(0), last_used(steady_clock::now()) { } @@ -57,7 +57,7 @@ RefCounter::dec_use_count() assert(counter > 0); if (--counter == 0) - last_used = monotonic_time(); + last_used = steady_clock::now(); return counter; } @@ -68,7 +68,7 @@ RefCounter::update_use_time() { boost::lock_guard lock(mutex); - last_used = monotonic_time(); + last_used = steady_clock::now(); } @@ -81,27 +81,15 @@ RefCounter::use_count() const } -int +milliseconds RefCounter::unused_for() const { boost::lock_guard lock(mutex); if (counter != 0) - return 0; - - struct timespec tmp; - clock_gettime(CLOCK_MONOTONIC, &tmp); - - return tmp.tv_sec - last_used; -} - + return milliseconds(0); -time_t -RefCounter::monotonic_time() -{ - struct timespec tmp; - clock_gettime(CLOCK_MONOTONIC, &tmp); - return tmp.tv_sec; + return duration_cast(steady_clock::now() - last_used); } diff --git a/server/MetaSnapper.h b/server/MetaSnapper.h index e570a5d2..eec66977 100644 --- a/server/MetaSnapper.h +++ b/server/MetaSnapper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) [2012-2013] Novell, Inc. + * Copyright (c) [2012-2015] Novell, Inc. * * All Rights Reserved. * @@ -24,12 +24,14 @@ #define SNAPPER_META_SNAPPER_H +#include #include #include using namespace std; +using namespace std::chrono; using namespace snapper; @@ -44,17 +46,15 @@ public: void update_use_time(); int use_count() const; - int unused_for() const; + milliseconds unused_for() const; private: - static time_t monotonic_time(); - mutable boost::mutex mutex; int counter; - time_t last_used; + steady_clock::time_point last_used; }; diff --git a/server/snapperd.cc b/server/snapperd.cc index 6e6152db..7e0c3403 100644 --- a/server/snapperd.cc +++ b/server/snapperd.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012 Novell, Inc. + * Copyright (c) [2012-2015] Novell, Inc. * * All Rights Reserved. * @@ -37,8 +37,8 @@ using namespace std; -const int idle_time = 60; -const int snapper_cleanup_time = 30; +const seconds idle_time(60); +const seconds snapper_cleanup_time(30); bool log_stdout = false; bool log_debug = false; @@ -55,7 +55,7 @@ public: void signal(DBus::Message& message); void client_disconnected(const string& name); void periodic(); - int periodic_timeout(); + milliseconds periodic_timeout(); }; @@ -93,7 +93,7 @@ MyMainLoop::method_call(DBus::Message& msg) y2deb("client connected invisible '" << msg.get_sender() << "'"); add_client_match(msg.get_sender()); client = clients.add(msg.get_sender()); - set_idle_timeout(-1); + set_idle_timeout(seconds(-1)); } client->add_task(*this, msg); @@ -146,22 +146,22 @@ MyMainLoop::periodic() } -int +milliseconds MyMainLoop::periodic_timeout() { boost::unique_lock lock(big_mutex); if (clients.has_zombies()) - return 1000; + return seconds(1); if (!backgrounds.empty()) - return 1000; + return seconds(1); for (MetaSnappers::const_iterator it = meta_snappers.begin(); it != meta_snappers.end(); ++it) if (it->is_loaded() && it->use_count() == 0) - return 1000; + return seconds(1); - return -1; + return seconds(-1); } diff --git a/snapper/AppUtil.cc b/snapper/AppUtil.cc index 2db7269b..ed76005e 100644 --- a/snapper/AppUtil.cc +++ b/snapper/AppUtil.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) [2004-2014] Novell, Inc. + * Copyright (c) [2004-2015] Novell, Inc. * * All Rights Reserved. * @@ -357,25 +357,22 @@ namespace snapper StopWatch::StopWatch() + : start_time(chrono::steady_clock::now()) { - gettimeofday(&start_tv, NULL); } double StopWatch::read() const { - struct timeval stop_tv; - gettimeofday(&stop_tv, NULL); - - struct timeval tv; - timersub(&stop_tv, &start_tv, &tv); - - return double(tv.tv_sec) + (double)(tv.tv_usec) / 1000000.0; + chrono::steady_clock::time_point stop_time = chrono::steady_clock::now(); + chrono::steady_clock::duration duration = stop_time - start_time; + return chrono::duration(duration).count(); } - std::ostream& operator<<(std::ostream& s, const StopWatch& sw) + std::ostream& + operator<<(std::ostream& s, const StopWatch& sw) { boost::io::ios_all_saver ias(s); return s << fixed << sw.read() << "s"; diff --git a/snapper/AppUtil.h b/snapper/AppUtil.h index 452b48e1..1b0b0d82 100644 --- a/snapper/AppUtil.h +++ b/snapper/AppUtil.h @@ -1,5 +1,5 @@ /* - * Copyright (c) [2004-2014] Novell, Inc. + * Copyright (c) [2004-2015] Novell, Inc. * * All Rights Reserved. * @@ -34,6 +34,7 @@ #include #include #include +#include namespace snapper @@ -103,7 +104,7 @@ namespace snapper protected: - struct timeval start_tv; + std::chrono::steady_clock::time_point start_time; }; -- 2.47.3