| # Gaudi Framework, version v22r0 |

Home |  | Generated: 9 Feb 2011

# ApMon.cpp File Reference

This file contains the implementations of the methods from the ApMon class. More...

`#include "ApMon.h"`
`#include "utils.h"`
`#include "proc_utils.h"`
`#include "monitor_utils.h"`
`#include <math.h>`
Include dependency graph for ApMon.cpp:

Go to the source code of this file.

## Defines
#define | RECHECK_CONF 0
#define | SYS_INFO_SEND 1
#define | JOB_INFO_SEND 2
## Functions
void * | bkTask (void *param)
| Performs background actions like rechecking the configuration file and the URLs and sending monitoring information.
## Variables
char | boolStrings [][10] = {"false", "true"}

## Detailed Description

This file contains the implementations of the methods from the ApMon class.

Definition in file ApMon.cpp.

## Define Documentation

#define JOB_INFO_SEND 2

Definition at line 50 of file ApMon.cpp.

#define RECHECK_CONF 0

Definition at line 48 of file ApMon.cpp.

#define SYS_INFO_SEND 1

Definition at line 49 of file ApMon.cpp.

## Function Documentation

void* bkTask | ( | void * | param | ) |

Performs background actions like rechecking the configuration file and the URLs and sending monitoring information.

This function is executed in a background thread and has two roles: it automatically sends the system/job monitoring parameters (if the user requested) and it checks the configuration file/URLs for changes.

(this is done in a separate thread).

Definition at line 918 of file ApMon.cpp.

    00918                           {
    00919 #else
    00920 DWORD WINAPI bkTask(void *param) {
    00921 #endif
    00922   struct stat st;
    00923 #ifndef WIN32
    00924   struct timespec delay;
    00925 #else
    00926   DWORD delay;
    00927 #endif
    00928   bool resourceChanged, haveChange;
    00929   int nextOp = -1, i, ret;
    00930   int generalInfoCount;
    00931   time_t crtTime, timeRemained;
    00932   time_t nextRecheck = 0, nextJobInfoSend = 0, nextSysInfoSend = 0;
    00933   ApMon *apm = (ApMon *)param;
    00934   char logmsg[200];
    00935
    00936   logger(INFO, "[Starting background thread...]");
    00937   apm -> bkThreadStarted = true;
    00938
    00939   crtTime = time(NULL);
    00940
    00941   pthread_mutex_lock(&(apm -> mutexBack));
    00942   if (apm -> confCheck) {
    00943     nextRecheck = crtTime + apm -> crtRecheckInterval;
    00944     //sprintf(logmsg, "###1 crt %ld interv %ld recheck %ld ", crtTime,
    00945     //   apm -> crtRecheckInterval, nextRecheck);
    00946     //logger(FINE, logmsg);
    00947     //fflush(stdout);
    00948   }
    00949   if (apm -> jobMonitoring)
    00950     nextJobInfoSend = crtTime + apm -> jobMonitorInterval;
    00951   if (apm -> sysMonitoring)
    00952     nextSysInfoSend = crtTime + apm -> sysMonitorInterval;
    00953   pthread_mutex_unlock(&(apm -> mutexBack));
    00954
    00955   timeRemained = -1;
    00956   generalInfoCount = 0;
    00957
    00958   while (1) {
    00959     pthread_mutex_lock(&apm -> mutexBack);
    00960     if (apm -> stopBkThread) {
    00961 //      printf("### stopBkThread \n");
    00962       pthread_mutex_unlock(&apm -> mutexBack);
    00963       break;
    00964     }
    00965     pthread_mutex_unlock(&apm -> mutexBack);
    00966
    00967     //sprintf(logmsg, "### 2 recheck %ld sys %ld ", nextRecheck,
    00968     //    nextSysInfoSend);
    00969     //logger(FINE, logmsg);
    00970
    00971     /* determine the next operation that must be performed */
    00972     if (nextRecheck > 0 && (nextJobInfoSend <= 0 ||
    00973                             nextRecheck <= nextJobInfoSend)) {
    00974       if (nextSysInfoSend <= 0 || nextRecheck <= nextSysInfoSend) {
    00975         nextOp = RECHECK_CONF;
    00976         timeRemained = nextRecheck - crtTime;
    00977       } else {
    00978         nextOp = SYS_INFO_SEND;
    00979         timeRemained = nextSysInfoSend - crtTime;
    00980       }
    00981     } else {
    00982       if (nextJobInfoSend > 0 && (nextSysInfoSend <= 0 ||
    00983                                   nextJobInfoSend <= nextSysInfoSend)) {
    00984         nextOp = JOB_INFO_SEND;
    00985         timeRemained = nextJobInfoSend - crtTime;
    00986       } else if (nextSysInfoSend > 0) {
    00987         nextOp = SYS_INFO_SEND;
    00988         timeRemained = nextSysInfoSend - crtTime;
    00989       }
    00990     }
    00991
    00992     if (timeRemained == -1)
    00993       timeRemained = RECHECK_INTERVAL;
    00994
    00995 #ifndef WIN32
    00996     /* the moment when the next operation should be performed */
    00997     delay.tv_sec = crtTime + timeRemained;
    00998     delay.tv_nsec = 0;
    00999 #else
    01000     delay = (/*crtTime +*/ timeRemained) * 1000;  // this is in millis
    01001 #endif
    01002
    01003     pthread_mutex_lock(&(apm -> mutexBack));
    01004
    01005     pthread_mutex_lock(&(apm -> mutexCond));
    01006     /* check for changes in the settings */
    01007     haveChange = false;
    01008     if (apm -> jobMonChanged || apm -> sysMonChanged || apm -> recheckChanged)
    01009       haveChange = true;
    01010     if (apm -> jobMonChanged) {
    01011       if (apm -> jobMonitoring)
    01012         nextJobInfoSend = crtTime + apm -> jobMonitorInterval;
    01013       else
    01014         nextJobInfoSend = -1;
    01015       apm -> jobMonChanged = false;
    01016     }
    01017     if (apm -> sysMonChanged) {
    01018       if (apm -> sysMonitoring)
    01019         nextSysInfoSend = crtTime + apm -> sysMonitorInterval;
    01020       else
    01021         nextSysInfoSend = -1;
    01022       apm -> sysMonChanged = false;
    01023     }
    01024     if (apm -> recheckChanged) {
    01025       if (apm -> confCheck) {
    01026         nextRecheck = crtTime + apm -> crtRecheckInterval;
    01027       }
    01028       else
    01029         nextRecheck = -1;
    01030       apm -> recheckChanged = false;
    01031     }
    01032     pthread_mutex_unlock(&(apm -> mutexBack));
    01033
    01034     if (haveChange) {
    01035       pthread_mutex_unlock(&(apm -> mutexCond));
    01036       continue;
    01037     }
    01038
    01039     /* wait until the next operation should be performed or until
    01040        a change in the settings occurs */
    01041 #ifndef WIN32
    01042     ret = pthread_cond_timedwait(&(apm -> confChangedCond),
    01043                                 &(apm -> mutexCond), &delay);
    01044     pthread_mutex_unlock(&(apm -> mutexCond));
    01045 #else
    01046     pthread_mutex_unlock(&(apm -> mutexCond));
    01047     ret = WaitForSingleObject(apm->confChangedCond, delay);
    01048 #endif
    01049     if (ret == ETIMEDOUT) {
    01050 //      printf("### ret TIMEDOUT\n");
    01051       /* now perform the operation */
    01052       if (nextOp == JOB_INFO_SEND) {
    01053         apm -> sendJobInfo();
    01054         crtTime = time(NULL);
    01055         nextJobInfoSend = crtTime + apm -> getJobMonitorInterval();
    01056       }
    01057
    01058       if (nextOp == SYS_INFO_SEND) {
    01059         apm -> sendSysInfo();
    01060         if (apm -> getGenMonitoring()) {
    01061           if (generalInfoCount <= 1)
    01062             apm -> sendGeneralInfo();
    01063           generalInfoCount = (generalInfoCount + 1) % apm -> genMonitorIntervals;
    01064         }
    01065         crtTime = time(NULL);
    01066         nextSysInfoSend = crtTime + apm -> getSysMonitorInterval();
    01067       }
    01068
    01069       if (nextOp == RECHECK_CONF) {
    01070         //logger(FINE, "### recheck conf");
    01071         resourceChanged = false;
    01072         try {
    01073           if (apm -> initType == FILE_INIT) {
    01074             sprintf(logmsg, "Checking for modifications for file %s ",
    01075                     apm -> initSources[0]);
    01076             logger(INFO, logmsg);
    01077             stat(apm -> initSources[0], &st);
    01078             if (st.st_mtime > apm -> lastModifFile) {
    01079               sprintf(logmsg, "File %s modified ", apm -> initSources[0]);
    01080               logger(INFO, logmsg);
    01081               resourceChanged = true;
    01082             }
    01083           }
    01084
    01085           // check the configuration URLs
    01086           for (i = 0; i < apm -> confURLs.nConfURLs; i++) {
    01087             sprintf(logmsg, "[Checking for modifications for URL %s ] ",
    01088                    apm -> confURLs.vURLs[i]);
    01089             logger(INFO, logmsg);
    01090             if (urlModified(apm -> confURLs.vURLs[i], apm -> confURLs.lastModifURLs[i])) {
    01091               sprintf(logmsg, "URL %s modified ", apm -> confURLs.vURLs[i]);
    01092               logger(INFO, logmsg);
    01093               resourceChanged = true;
    01094               break;
    01095             }
    01096           }
    01097
    01098           if (resourceChanged) {
    01099             logger(INFO, "Reloading configuration...");
    01100             if (apm -> initType == FILE_INIT)
    01101               apm -> initialize(apm -> initSources[0], false);
    01102             else
    01103               apm -> initialize(apm -> nInitSources, apm -> initSources, false);
    01104           }
    01105           apm -> setCrtRecheckInterval(apm -> getRecheckInterval());
    01106         } catch (runtime_error &err) {
    01107           logger(WARNING, err.what());
    01108           logger(WARNING, "Increasing the time interval for reloading the configuration...");
    01109           apm -> setCrtRecheckInterval(apm -> getRecheckInterval() * 5);
    01110         }
    01111         crtTime = time(NULL);
    01112         nextRecheck = crtTime + apm -> getCrtRecheckInterval();
    01113         //sleep(apm -> getCrtRecheckInterval());
    01114       }
    01115     }
    01116
    01117   } // while
    01118
    01119 #ifndef WIN32
    01120   return NULL; // it doesn't matter what we return here
    01121 #else
    01122   return 0;
    01123 #endif
    01124 }

## Variable Documentation

char boolStrings[][10] = {"false", "true"}

Definition at line 52 of file ApMon.cpp.

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines