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