OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stats_collector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef vnsw_agent_stats_coll_h_
6 #define vnsw_agent_stats_coll_h_
7 
8 #include <boost/asio.hpp>
9 #include <map>
10 #include "base/timer.h"
11 #include "base/queue_task.h"
12 
13 //The base class for statistics collector classes.
14 //Defines Timer functionality to trigger the stats collection events
16 public:
20  };
21 
22  StatsCollector(int task_id, int32_t instance, boost::asio::io_service &io,
23  int exp, std::string timer_name) : run_counter_(0),
24  timer_(TimerManager::CreateTimer(io, timer_name.c_str(),
25  task_id, instance)),
27  boost::bind(&StatsCollector::RestartTimer, this),
28  task_id, instance)),
29  expiry_time_(exp) {
30  };
31 
32  virtual ~StatsCollector() {
33  Shutdown();
36  }
37 
38  void InitDone() {
40  boost::bind(&StatsCollector::TimerExpiry, this));
41  }
42 
43  virtual bool Run() = 0;
44  void Shutdown() {
45  if (timer_) {
46  timer_->Cancel();
48  timer_ = NULL;
49  }
50  }
51 
52  // To be used by UT only
53  void TestStartStopTimer (bool stop) {
54  if (timer_ != NULL) {
55  if (stop) {
56  // UTs call TestStartStopTimer() without exclusion. Hence,
57  // timer->Cancel() can potentially fail. Retry in that case
58  int i = 0;
59  for (; i < 8; i++) {
60  if (timer_->Cancel())
61  break;
62  usleep(1000);
63  }
64  assert(i < 8);
65  } else {
67  boost::bind(&StatsCollector::TimerExpiry, this));
68  }
69  }
70  }
71 
72  int expiry_time() const { return expiry_time_; }
73  void set_expiry_time(int time) {
74  if (time != expiry_time_) {
75  expiry_time_ = time;
77  }
78  }
79 
80  void RescheduleTimer(int time) {
81  timer_->Reschedule(time);
82  }
83 
84  int run_counter_; //used only in UT code
85 
86 private:
87  bool RestartTimer() {
88  timer_->Cancel();
90  boost::bind(&StatsCollector::TimerExpiry, this));
91  return true;
92  }
93  bool TimerExpiry() {
94  Run();
95  /* Return true to request auto-restart of timer */
96  return true;
97  }
98 
103 };
104 
105 #endif // vnsw_agent_stats_coll_h_
TaskTrigger * timer_restart_trigger_
virtual ~StatsCollector()
void set_expiry_time(int time)
int expiry_time() const
void TestStartStopTimer(bool stop)
DISALLOW_COPY_AND_ASSIGN(StatsCollector)
bool Cancel()
Definition: timer.cc:150
void Reset()
Definition: task_trigger.cc:54
virtual bool Run()=0
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
Definition: timer.cc:108
bool Reschedule(int time)
Definition: timer.cc:137
Definition: timer.h:54
StatsCollector(int task_id, int32_t instance, boost::asio::io_service &io, int exp, std::string timer_name)
void RescheduleTimer(int time)
static bool DeleteTimer(Timer *Timer)
Definition: timer.cc:222