Line data Source code
1 : /* 2 : * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef __BASE_TASK_MONITOR_H__ 7 : #define __BASE_TASK_MONITOR_H__ 8 : 9 : /**************************************************************************** 10 : * Monitor module for TBB. 11 : * It is observed that TBB randomly goes into a state where no tasks are 12 : * scheduled. The monitor here tracks the tasks enqueued and executed. 13 : * 14 : * If the monitor identifes that TBB is in a lockup state, then it will 15 : * assert the process 16 : ****************************************************************************/ 17 : #include <boost/asio/placeholders.hpp> 18 : #include <boost/system/error_code.hpp> 19 : 20 : #include "util.h" 21 : 22 : class TaskScheduler; 23 : class TimerImpl; 24 : class EventManager; 25 : 26 : class TaskMonitor { 27 : public: 28 : TaskMonitor(TaskScheduler *scheduler, uint64_t tbb_keepawake_time_msec, 29 : uint64_t inactivity_time_msec, uint64_t poll_interval_msec); 30 : ~TaskMonitor(); 31 : 32 : void Start(EventManager *evm); 33 : void Terminate(); 34 : void Run(const boost::system::error_code &ec); 35 : 36 30 : uint64_t last_activity() const { return last_activity_; } 37 30 : uint64_t last_enqueue_count() const { return last_enqueue_count_; } 38 30 : uint64_t last_done_count() const { return last_done_count_; } 39 : 40 0 : uint64_t inactivity_time_usec() const { return inactivity_time_usec_; } 41 10 : uint64_t inactivity_time_msec() const { return inactivity_time_usec_/1000; } 42 10 : uint64_t poll_interval_msec() const { return poll_interval_msec_; } 43 0 : uint64_t tbb_keepawake_time_msec() const { return tbb_keepawake_time_msec_;} 44 0 : uint64_t poll_count() const { return poll_count_; } 45 : private: 46 : friend class TaskMonitorTest; 47 : 48 : void UpdateTimers(); 49 : void Restart(); 50 : bool Monitor(uint64_t t, uint64_t enqueue_count, uint64_t done_count); 51 : 52 : TaskScheduler *scheduler_; 53 : // Monitor terminated 54 : bool cancelled_; 55 : // ASIO Timer implementation class 56 : std::unique_ptr<TimerImpl> timer_impl_; 57 : 58 : // inactivity in usec after which monitor must invoke assert() 59 : uint64_t inactivity_time_usec_; 60 : // interval at which tbb must be monitored 61 : uint64_t poll_interval_msec_; 62 : // TBB keepawake time 63 : uint64_t tbb_keepawake_time_msec_; 64 : // time of last activity seen 65 : uint64_t last_activity_; 66 : // TaskScheduler->enqueue_count() at last_change_ 67 : uint64_t last_enqueue_count_; 68 : // TaskScheduler->done_count() at last_change_ 69 : uint64_t last_done_count_; 70 : // Number of times monitor is run 71 : uint64_t poll_count_; 72 : DISALLOW_COPY_AND_ASSIGN(TaskMonitor); 73 : }; 74 : 75 : #endif // __BASE_TASK_MONITOR_H__