watchdog.h

Go to the documentation of this file.
00001 
00018 #ifndef WATCHDOG_H
00019 #define WATCHDOG_H
00020 
00021 #include <stdio.h>
00022 #include <pthread.h>
00023 #include <unistd.h>
00024 #include <vector>
00025 #include <algorithm>
00026 #include <assert.h>
00027 #include "kernel.h"
00028 
00029 using namespace std;
00030 namespace SMP{
00031 
00033 class SMPKernel;
00034 
00040 class SMPWatchdog{
00042         static void *watchdog_thread(void *arg);
00043     private:
00045         int timeout;
00047         pthread_t dog_thread;
00049         int n_bones;
00052         vector<bool> bones;
00054         SMPKernel *kernel;
00055 
00057         vector<bool> not_monitored; 
00058         
00059 
00060     public:
00064         SMPWatchdog(SMPKernel *k, int timeout);
00066         ~SMPWatchdog();
00067 
00069         int start();
00071         int stop();
00072 
00074         inline void pat(int id); 
00076         inline void set_timeout(int sec);
00078         inline int get_timeout();
00079 
00080         //============================================================================================
00081         // the following function should be made public in order for watchdog thread callback function
00082         // to be able to access. 
00083 
00085         inline void pat(); 
00087         inline void stir_up();
00091         inline bool is_happy();
00095         inline int *get_bad_ids(int *num);      
00096 
00098         inline int add_not_monitored(int fid);
00099 
00100     
00101         //2007/2/6 
00106         void update_watch_size();
00107 
00108 };
00109 
00110 inline void SMPWatchdog::set_timeout(int sec)
00111 {
00112     timeout = sec;
00113 }
00114 inline int SMPWatchdog::get_timeout()
00115 {
00116     return timeout;
00117 }
00118 
00119 inline void SMPWatchdog::stir_up()
00120 {
00121     for(int i = 0; i < n_bones; i++)
00122         bones[i] = false;
00123 }
00124 
00125 inline void SMPWatchdog::pat()
00126 {
00127     for(int i = 0; i < n_bones; i++)
00128         bones[i] = true;
00129 }
00130 
00131 inline void SMPWatchdog::pat(int id)
00132 {
00133 //    printf("id=%d n_bones=%d\n", id, n_bones);
00134     assert(id >= 0 && id < n_bones);
00135     bones[id] = true;
00136 }
00137 
00138 inline bool SMPWatchdog::is_happy()
00139 {
00140     for(int i = 0; i < n_bones; i++){
00141         if(not_monitored[i]){  //skip not-monitored threads 
00142             //printf("[watchdog]:skip filter:%d\n", i);
00143             continue;
00144         }
00145         if(!bones[i])
00146             return false;
00147     }
00148     return true;
00149 }
00150 
00151 inline int *SMPWatchdog::get_bad_ids(int *num)
00152 {
00153     //find the first bad id. 
00154     int i = 0;
00155     int no = 0;
00156     for(; i < n_bones; i++){
00157         if(!bones[i] && !not_monitored[i]) //if flag not set, and it is monitored
00158             no++;
00159     }
00160     *num = no;
00161     if(no == 0) 
00162         return NULL;
00163 
00164     int *bads = new int[no];
00165     for(i = no = 0; i < n_bones; i++){
00166         if(!bones[i] && !not_monitored[i])
00167             bads[no++] = i;
00168     }
00169     
00170     return bads;
00171 }
00172 
00173 
00174 inline int SMPWatchdog::add_not_monitored(int fid)
00175 {
00176     assert(fid >= 0 && fid < n_bones);
00177     not_monitored[fid]=true;
00178     printf("[watchdog]:filter id=%d added to not_monitored list\n", fid);
00179     return 0;
00180     /*
00181     vector<int>::iterator it = find(not_monitored.begin(), not_monitored.end(), fid);
00182     if(it == not_monitored.end()){ //not in not_monitored vector
00183         printf("filter id=%d added to not_monitored list in watchdog\n", fid);
00184         not_monitored.push_back(fid);
00185         return 0;
00186     }
00187     return -1; //fid has already been in the not_monitored list!
00188     */
00189 }
00190 /*
00191 inline pthread_t SMPWatchdog::get_watchdog_thread()
00192 {
00193     return watched_thread;
00194 }
00195 */
00196 } //end of namespace
00197 
00198 #endif

Generated on Fri Aug 31 15:41:42 2007 for SCREAMLinuxMiddleware(SLIM) by  doxygen 1.5.1