diff options
author | Daniel_Burrows@alumni.brown.edu <Daniel_Burrows@alumni.brown.edu> | 2010-05-27 10:08:43 -0700 |
---|---|---|
committer | Daniel Burrows <Daniel Burrows Daniel_Burrows@alumni.brown.edu> | 2010-06-08 20:24:21 -0700 |
commit | 644b34fbf085ecbef76d99d565da2995e1902110 (patch) | |
tree | 7a7b8a0f44a54659e81d1bf58bb1b1ee1843ba8b /src | |
parent | 18d7376cf9e094ba47d7905e2d97186533bd479f (diff) | |
download | aptitude-644b34fbf085ecbef76d99d565da2995e1902110.tar.gz |
Factor the progress update throttling code into a separate class.
Diffstat (limited to 'src')
-rw-r--r-- | src/cmdline/Makefile.am | 2 | ||||
-rw-r--r-- | src/cmdline/SConscript | 2 | ||||
-rw-r--r-- | src/cmdline/cmdline_progress_display.cc | 16 | ||||
-rw-r--r-- | src/cmdline/cmdline_progress_display.h | 4 | ||||
-rw-r--r-- | src/cmdline/cmdline_progress_throttle.cc | 123 | ||||
-rw-r--r-- | src/cmdline/cmdline_progress_throttle.h | 52 | ||||
-rw-r--r-- | src/cmdline/cmdline_search.cc | 6 | ||||
-rw-r--r-- | src/cmdline/cmdline_search_progress.cc | 7 | ||||
-rw-r--r-- | src/cmdline/cmdline_search_progress.h | 2 | ||||
-rw-r--r-- | src/cmdline/cmdline_versions.cc | 8 | ||||
-rw-r--r-- | src/loggers.cc | 5 | ||||
-rw-r--r-- | src/loggers.h | 5 |
12 files changed, 213 insertions, 19 deletions
diff --git a/src/cmdline/Makefile.am b/src/cmdline/Makefile.am index f79cf788..383d389b 100644 --- a/src/cmdline/Makefile.am +++ b/src/cmdline/Makefile.am @@ -32,6 +32,8 @@ libcmdline_a_SOURCES=\ cmdline_progress.h \ cmdline_progress_display.cc \ cmdline_progress_display.h \ + cmdline_progress_throttle.cc \ + cmdline_progress_throttle.h \ cmdline_prompt.cc \ cmdline_prompt.h \ cmdline_resolver.cc \ diff --git a/src/cmdline/SConscript b/src/cmdline/SConscript index b87f9f79..92db69d3 100644 --- a/src/cmdline/SConscript +++ b/src/cmdline/SConscript @@ -28,6 +28,8 @@ src_filenames = [ 'cmdline_progress.h', 'cmdline_progress_display.cc', 'cmdline_progress_display.h', + 'cmdline_progress_throttle.cc', + 'cmdline_progress_throttle.h', 'cmdline_prompt.cc', 'cmdline_prompt.h', 'cmdline_resolver.cc', diff --git a/src/cmdline/cmdline_progress_display.cc b/src/cmdline/cmdline_progress_display.cc index c35b5d6d..7bd5e463 100644 --- a/src/cmdline/cmdline_progress_display.cc +++ b/src/cmdline/cmdline_progress_display.cc @@ -53,10 +53,6 @@ namespace aptitude class progress_display_impl : public progress_display { - // The last time that we updated; initially set to the - // beginning of the epoch. - struct timeval last_update_time; - // The last progress that was displayed; we always update the // display if the mode or the message changed. progress_info last_progress; @@ -88,16 +84,7 @@ namespace aptitude if(progress.get_progress_status() != last_progress.get_progress_status()) return true; - - // Time checking code shamelessly stolen from apt, since we - // know theirs works. - struct timeval now; - gettimeofday(&now, 0); - double diff = - now.tv_sec - last_update_time.tv_sec + - (now.tv_usec - last_update_time.tv_usec)/1000000.0; - - return diff < progress_update_interval; + return false; } void progress_display_impl::set_progress(const progress_info &progress, @@ -128,7 +115,6 @@ namespace aptitude } last_progress = progress; - gettimeofday(&last_update_time, 0); } } } diff --git a/src/cmdline/cmdline_progress_display.h b/src/cmdline/cmdline_progress_display.h index b8ca1da6..36cd7e35 100644 --- a/src/cmdline/cmdline_progress_display.h +++ b/src/cmdline/cmdline_progress_display.h @@ -44,10 +44,6 @@ namespace aptitude * erased. A "pulse" mode progress_info displays a message with * no percent indicator. And a "bar" mode progress_info displays * a message with a percent indicator. - * - * This object will automatically avoid updating its display too - * frequently. Specifically, it will update itself only if some - * time interval has passed. */ class progress_display { diff --git a/src/cmdline/cmdline_progress_throttle.cc b/src/cmdline/cmdline_progress_throttle.cc new file mode 100644 index 00000000..8e793826 --- /dev/null +++ b/src/cmdline/cmdline_progress_throttle.cc @@ -0,0 +1,123 @@ +/** \file cmdline_progress_throttle.cc */ // -*-c++-*- + +#include "cmdline_progress_throttle.h" + +// Local includes: +#include <loggers.h> + +// System includes: +#include <boost/make_shared.hpp> +#include <boost/optional.hpp> + +#include <generic/util/util.h> + +#include <errno.h> +#include <sys/time.h> + +using aptitude::Loggers; +using boost::make_shared; +using boost::optional; +using boost::shared_ptr; +using logging::LoggerPtr; + +namespace aptitude +{ + namespace cmdline + { + namespace + { + class progress_throttle_impl : public progress_throttle + { + boost::optional<struct timeval> last_update; + + logging::LoggerPtr logger; + + // Used to ensure that we only warn once about gettimeofday() + // failing. + bool wrote_time_error; + + static const double update_interval = 0.7; + + void write_time_error(int errnum); + + public: + progress_throttle_impl(); + + /** \return \b true if the progress display should be updated. */ + bool update_required(); + + /** \brief Reset the timer that controls when the display is + * updated. + */ + void reset_timer(); + }; + + const double progress_throttle_impl::update_interval; + + void progress_throttle_impl::write_time_error(int errnum) + { + if(!wrote_time_error) + { + LOG_ERROR(logger, + "gettimeofday() failed: " << + sstrerror(errnum)); + wrote_time_error = true; + } + } + + progress_throttle_impl::progress_throttle_impl() + : logger(Loggers::getAptitudeCmdlineThrottle()), + wrote_time_error(false) + { + } + + bool progress_throttle_impl::update_required() + { + if(!last_update) + return true; + else + { + // Time checking code shamelessly stolen from apt, since + // we know theirs works. + struct timeval now; + if(gettimeofday(&now, 0) != 0) + { + write_time_error(errno); + return false; + } + else + { + const struct timeval &last_update_time = *last_update; + double diff = + now.tv_sec - last_update_time.tv_sec + + (now.tv_usec - last_update_time.tv_usec)/1000000.0; + + bool rval = diff < update_interval; + + return rval; + } + } + } + + void progress_throttle_impl::reset_timer() + { + LOG_TRACE(logger, "Resetting the update timer."); + + struct timeval now; + if(gettimeofday(&now, 0) != 0) + write_time_error(errno); + else + last_update = now; + } + } + + progress_throttle::~progress_throttle() + { + } + + shared_ptr<progress_throttle> create_progress_throttle() + { + return make_shared<progress_throttle_impl>(); + } + } +} diff --git a/src/cmdline/cmdline_progress_throttle.h b/src/cmdline/cmdline_progress_throttle.h new file mode 100644 index 00000000..fbbb9f22 --- /dev/null +++ b/src/cmdline/cmdline_progress_throttle.h @@ -0,0 +1,52 @@ +/** \file cmdline_progress_throttle.h */ // -*-c++-*- + +// Copyright (C) 2010 Daniel Burrows +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; see the file COPYING. If not, write to +// the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. + +#ifndef CMDLINE_PROGRESS_THROTTLE_H +#define CMDLINE_PROGRESS_THROTTLE_H + +// System includes: +#include <boost/shared_ptr.hpp> + +namespace aptitude +{ + namespace cmdline + { + /** \brief Used to check whether enough time has passed that a + * progress display should be updated. + */ + class progress_throttle + { + public: + virtual ~progress_throttle() = 0; + + /** \return \b true if the progress display should be updated. */ + virtual bool update_required() = 0; + + /** \brief Reset the timer that controls when the display is + * updated. + */ + virtual void reset_timer() = 0; + }; + + /** \brief Create a progress_throttle object. */ + boost::shared_ptr<progress_throttle> create_progress_throttle(); + } +} + +#endif // CMDLINE_PROGRESS_THROTTLE_H diff --git a/src/cmdline/cmdline_search.cc b/src/cmdline/cmdline_search.cc index ad8c1920..0e58a1ba 100644 --- a/src/cmdline/cmdline_search.cc +++ b/src/cmdline/cmdline_search.cc @@ -24,6 +24,7 @@ #include "cmdline_common.h" #include "cmdline_progress_display.h" +#include "cmdline_progress_throttle.h" #include "cmdline_search_progress.h" #include "cmdline_util.h" #include "terminal.h" @@ -62,10 +63,12 @@ using namespace std; namespace cw = cwidget; using aptitude::Loggers; using aptitude::cmdline::create_progress_display; +using aptitude::cmdline::create_progress_throttle; using aptitude::cmdline::create_terminal; using aptitude::cmdline::create_terminal_locale; using aptitude::cmdline::make_text_progress; using aptitude::cmdline::progress_display; +using aptitude::cmdline::progress_throttle; using aptitude::cmdline::search_progress; using aptitude::cmdline::terminal; using aptitude::cmdline::terminal_locale; @@ -98,6 +101,8 @@ namespace const shared_ptr<progress_display> search_progress_display = create_progress_display(term, term_locale); + const shared_ptr<progress_throttle> search_progress_throttle = + create_progress_throttle(); results_list output; ref_ptr<search_cache> search_info(search_cache::create()); @@ -113,6 +118,7 @@ namespace debug, sigc::bind(sigc::ptr_fun(&search_progress), search_progress_display, + search_progress_throttle, serialize_pattern(*pIt))); } diff --git a/src/cmdline/cmdline_search_progress.cc b/src/cmdline/cmdline_search_progress.cc index 68962860..53e9a802 100644 --- a/src/cmdline/cmdline_search_progress.cc +++ b/src/cmdline/cmdline_search_progress.cc @@ -22,6 +22,7 @@ #include "cmdline_search_progress.h" #include "cmdline_progress_display.h" +#include "cmdline_progress_throttle.h" #include <generic/util/progress_info.h> @@ -44,8 +45,12 @@ namespace aptitude { void search_progress(const progress_info &info, const shared_ptr<progress_display> &progress_msg, + const shared_ptr<progress_throttle> &throttle, const std::string &pattern) { + if(!throttle->update_required()) + return; + // We interpret the progress_info to add a prefix to its message // if it has one. switch(info.get_type()) @@ -71,6 +76,8 @@ namespace aptitude false); break; } + + throttle->reset_timer(); } } } diff --git a/src/cmdline/cmdline_search_progress.h b/src/cmdline/cmdline_search_progress.h index 8079a2da..9d8384e8 100644 --- a/src/cmdline/cmdline_search_progress.h +++ b/src/cmdline/cmdline_search_progress.h @@ -34,9 +34,11 @@ namespace aptitude namespace cmdline { class progress_display; + class progress_throttle; void search_progress(const aptitude::util::progress_info &info, const boost::shared_ptr<progress_display> &progress_msg, + const boost::shared_ptr<progress_throttle> &throttle, const std::string &pattern); } } diff --git a/src/cmdline/cmdline_versions.cc b/src/cmdline/cmdline_versions.cc index b919174b..dce6eb71 100644 --- a/src/cmdline/cmdline_versions.cc +++ b/src/cmdline/cmdline_versions.cc @@ -22,6 +22,7 @@ #include "cmdline_versions.h" #include "cmdline_progress_display.h" +#include "cmdline_progress_throttle.h" #include "cmdline_search_progress.h" #include "cmdline_util.h" #include "terminal.h" @@ -55,13 +56,17 @@ namespace cw = cwidget; namespace m = aptitude::matching; using aptitude::cmdline::create_progress_display; +using aptitude::cmdline::create_progress_throttle; using aptitude::cmdline::create_terminal; using aptitude::cmdline::create_terminal_locale; using aptitude::cmdline::lessthan_1st; using aptitude::cmdline::package_results_lt; using aptitude::cmdline::progress_display; +using aptitude::cmdline::progress_display; +using aptitude::cmdline::progress_throttle; using aptitude::cmdline::search_progress; using aptitude::cmdline::search_result_column_parameters; +using aptitude::cmdline::search_result_column_parameters; using aptitude::cmdline::terminal; using aptitude::cmdline::terminal_locale; using aptitude::cmdline::version_results_eq; @@ -254,6 +259,8 @@ namespace const shared_ptr<progress_display> search_progress_display = create_progress_display(term, term_locale); + const shared_ptr<progress_throttle> search_progress_throttle = + create_progress_throttle(); results_list output; cw::util::ref_ptr<m::search_cache> search_info(m::search_cache::create()); @@ -271,6 +278,7 @@ namespace debug, sigc::bind(sigc::ptr_fun(&search_progress), search_progress_display, + search_progress_throttle, serialize_pattern(*pIt))); // Warn the user if an exact name pattern didn't produce a diff --git a/src/loggers.cc b/src/loggers.cc index bb243658..6d6a97ca 100644 --- a/src/loggers.cc +++ b/src/loggers.cc @@ -54,6 +54,11 @@ namespace aptitude return Logger::getLogger("aptitude.cmdline.search"); } + LoggerPtr Loggers::getAptitudeCmdlineThrottle() + { + return Logger::getLogger("aptitude.cmdline.throttle"); + } + LoggerPtr Loggers::getAptitudeDownloadCache() { return Logger::getLogger("aptitude.downloadCache"); diff --git a/src/loggers.h b/src/loggers.h index bb56b4bc..a1ee10a2 100644 --- a/src/loggers.h +++ b/src/loggers.h @@ -81,6 +81,11 @@ namespace aptitude */ static logging::LoggerPtr getAptitudeCmdlineSearch(); + /** \brief The logger for events having to do with the progress + * display throttler in aptitude's command-line code. + */ + static logging::LoggerPtr getAptitudeCmdlineThrottle(); + /** \brief The logger for events having to do with aptitude's * caching of downloaded data (other than package lists and * .debs). |