~ubuntu-branches/ubuntu/precise/aptitude/precise

« back to all changes in this revision

Viewing changes to src/generic/util/throttle.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-06-22 12:32:56 UTC
  • mfrom: (1.8.6 sid)
  • Revision ID: james.westby@ubuntu.com-20110622123256-8aox9w9ch3x72dci
Tags: 0.6.4-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/05aptitude: never autoremove kernels
  - drop aptitude-doc to Suggests
  - 03_branding.dpatch: ubuntu branding
  - 04_changelog.dpatch: take changelogs from changelogs.ubuntu.com
  - 09_ubuntu_fortify_source.dpatch: Suppress a number of warnings (turned
    into errors by -Werror) triggered by Ubuntu's default of
    -D_FORTIFY_SOURCE=2.
  - 11_ubuntu_uses_sudo.dpatch: fix status line of 'Become root' menu entry
    to not refer to su.
  - 12_point_manpage_to_doc_package.dpatch: point Finnish manpage to the
    correct place for further info
  - 14_html2text_preferred.dpatch: switch back to html2text in favor of
    elinks, since html2text is in main and elinks isn't.
* dropped 01_intltool_update.dpatch
* updated 15_ftbfs_new_apt

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** \file throttle.cc */   // -*-c++-*-
 
2
 
 
3
#include "throttle.h"
 
4
 
 
5
// Local includes:
 
6
#include <loggers.h>
 
7
 
 
8
// System includes:
 
9
#include <boost/make_shared.hpp>
 
10
#include <boost/optional.hpp>
 
11
 
 
12
#include <generic/util/util.h>
 
13
 
 
14
#include <errno.h>
 
15
#include <sys/time.h>
 
16
 
 
17
using aptitude::Loggers;
 
18
using boost::make_shared;
 
19
using boost::optional;
 
20
using boost::shared_ptr;
 
21
using logging::LoggerPtr;
 
22
 
 
23
namespace aptitude
 
24
{
 
25
  namespace util
 
26
  {
 
27
    namespace
 
28
    {
 
29
      class throttle_impl : public throttle
 
30
      {
 
31
        boost::optional<struct timeval> last_update;
 
32
 
 
33
        logging::LoggerPtr logger;
 
34
 
 
35
        // Used to ensure that we only warn once about gettimeofday()
 
36
        // failing.
 
37
        bool wrote_time_error;
 
38
 
 
39
        static const double update_interval = 0.7;
 
40
 
 
41
        void write_time_error(int errnum);
 
42
 
 
43
      public:
 
44
        throttle_impl();
 
45
 
 
46
        /** \return \b true if the timer has expired. */
 
47
        bool update_required();
 
48
 
 
49
        /** \brief Reset the timer that controls when the display is
 
50
         *  updated.
 
51
         */
 
52
        void reset_timer();
 
53
      };
 
54
 
 
55
      const double throttle_impl::update_interval;
 
56
 
 
57
      void throttle_impl::write_time_error(int errnum)
 
58
      {
 
59
        if(!wrote_time_error)
 
60
          {
 
61
            LOG_ERROR(logger,
 
62
                      "gettimeofday() failed: " <<
 
63
                      sstrerror(errnum));
 
64
            wrote_time_error = true;
 
65
          }
 
66
      }
 
67
 
 
68
      throttle_impl::throttle_impl()
 
69
        : logger(Loggers::getAptitudeCmdlineThrottle()),
 
70
          wrote_time_error(false)
 
71
      {
 
72
      }
 
73
 
 
74
      bool throttle_impl::update_required()
 
75
      {
 
76
        if(!last_update)
 
77
          return true;
 
78
        else
 
79
          {
 
80
            // Time checking code shamelessly stolen from apt, since
 
81
            // we know theirs works.
 
82
            struct timeval now;
 
83
            if(gettimeofday(&now, 0) != 0)
 
84
              {
 
85
                write_time_error(errno);
 
86
                return false;
 
87
              }
 
88
            else
 
89
              {
 
90
                const struct timeval &last_update_time = *last_update;
 
91
                double diff =
 
92
                  now.tv_sec - last_update_time.tv_sec +
 
93
                  (now.tv_usec - last_update_time.tv_usec)/1000000.0;
 
94
 
 
95
                bool rval = diff >= update_interval;
 
96
 
 
97
                return rval;
 
98
              }
 
99
          }
 
100
      }
 
101
 
 
102
      void throttle_impl::reset_timer()
 
103
      {
 
104
        LOG_TRACE(logger, "Resetting the update timer.");
 
105
 
 
106
        struct timeval now;
 
107
        if(gettimeofday(&now, 0) != 0)
 
108
          write_time_error(errno);
 
109
        else
 
110
          last_update = now;
 
111
      }
 
112
    }
 
113
 
 
114
    throttle::~throttle()
 
115
    {
 
116
    }
 
117
 
 
118
    shared_ptr<throttle> create_throttle()
 
119
    {
 
120
      return make_shared<throttle_impl>();
 
121
    }
 
122
  }
 
123
}