Line data Source code
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 15 : class StatsCollector { 16 : public: 17 : enum StatsInstance { 18 : FlowStatsCollector, 19 : AgentStatsCollector, 20 : }; 21 : 22 15 : StatsCollector(int task_id, int32_t instance, boost::asio::io_service &io, 23 15 : int exp, std::string timer_name) : run_counter_(0), 24 15 : timer_(TimerManager::CreateTimer(io, timer_name.c_str(), 25 : task_id, instance)), 26 30 : timer_restart_trigger_(new TaskTrigger( 27 : boost::bind(&StatsCollector::RestartTimer, this), 28 15 : task_id, instance)), 29 15 : expiry_time_(exp) { 30 15 : }; 31 : 32 15 : virtual ~StatsCollector() { 33 15 : Shutdown(); 34 15 : timer_restart_trigger_->Reset(); 35 15 : delete timer_restart_trigger_; 36 15 : } 37 : 38 15 : void InitDone() { 39 15 : timer_->Start(expiry_time_, 40 : boost::bind(&StatsCollector::TimerExpiry, this)); 41 15 : } 42 : 43 : virtual bool Run() = 0; 44 30 : void Shutdown() { 45 30 : if (timer_) { 46 15 : timer_->Cancel(); 47 15 : TimerManager::DeleteTimer(timer_); 48 15 : timer_ = NULL; 49 : } 50 30 : } 51 : 52 : // To be used by UT only 53 56 : void TestStartStopTimer (bool stop) { 54 56 : if (timer_ != NULL) { 55 56 : if (stop) { 56 : // UTs call TestStartStopTimer() without exclusion. Hence, 57 : // timer->Cancel() can potentially fail. Retry in that case 58 20 : int i = 0; 59 20 : for (; i < 8; i++) { 60 20 : if (timer_->Cancel()) 61 20 : break; 62 0 : usleep(1000); 63 : } 64 20 : assert(i < 8); 65 : } else { 66 36 : timer_->Start(expiry_time_, 67 : boost::bind(&StatsCollector::TimerExpiry, this)); 68 : } 69 : } 70 56 : } 71 : 72 8 : int expiry_time() const { return expiry_time_; } 73 2 : void set_expiry_time(int time) { 74 2 : if (time != expiry_time_) { 75 2 : expiry_time_ = time; 76 2 : timer_restart_trigger_->Set(); 77 : } 78 2 : } 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 2 : bool RestartTimer() { 88 2 : timer_->Cancel(); 89 2 : timer_->Start(expiry_time_, 90 : boost::bind(&StatsCollector::TimerExpiry, this)); 91 2 : return true; 92 : } 93 177 : bool TimerExpiry() { 94 177 : Run(); 95 : /* Return true to request auto-restart of timer */ 96 177 : return true; 97 : } 98 : 99 : Timer *timer_; 100 : TaskTrigger *timer_restart_trigger_; 101 : int expiry_time_; 102 : DISALLOW_COPY_AND_ASSIGN(StatsCollector); 103 : }; 104 : 105 : #endif // vnsw_agent_stats_coll_h_