Gaudi Framework, version v22r4

Home   Generated: Fri Sep 2 2011
Defines | Functions | Variables

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.

                          {
#else
DWORD WINAPI bkTask(void *param) {
#endif
  struct stat st;
#ifndef WIN32
  struct timespec delay;
#else
  DWORD delay;
#endif
  bool resourceChanged, haveChange;
  int nextOp = -1, i, ret;
  int generalInfoCount;
  time_t crtTime, timeRemained;
  time_t nextRecheck = 0, nextJobInfoSend = 0, nextSysInfoSend = 0;
  ApMon *apm = (ApMon *)param;
  char logmsg[200];

  logger(INFO, "[Starting background thread...]");
  apm -> bkThreadStarted = true;

  crtTime = time(NULL);

  pthread_mutex_lock(&(apm -> mutexBack));
  if (apm -> confCheck) {
    nextRecheck = crtTime + apm -> crtRecheckInterval;
    //sprintf(logmsg, "###1 crt %ld interv %ld recheck %ld ", crtTime,
    //   apm -> crtRecheckInterval, nextRecheck);
    //logger(FINE, logmsg);
    //fflush(stdout);
  }
  if (apm -> jobMonitoring)
    nextJobInfoSend = crtTime + apm -> jobMonitorInterval;
  if (apm -> sysMonitoring)
    nextSysInfoSend = crtTime + apm -> sysMonitorInterval;
  pthread_mutex_unlock(&(apm -> mutexBack));

  timeRemained = -1;
  generalInfoCount = 0;

  while (1) {
    pthread_mutex_lock(&apm -> mutexBack);
    if (apm -> stopBkThread) {
//      printf("### stopBkThread \n");
      pthread_mutex_unlock(&apm -> mutexBack);
      break;
    }
    pthread_mutex_unlock(&apm -> mutexBack);

    //sprintf(logmsg, "### 2 recheck %ld sys %ld ", nextRecheck,
    //    nextSysInfoSend);
    //logger(FINE, logmsg);

    /* determine the next operation that must be performed */
    if (nextRecheck > 0 && (nextJobInfoSend <= 0 ||
                            nextRecheck <= nextJobInfoSend)) {
      if (nextSysInfoSend <= 0 || nextRecheck <= nextSysInfoSend) {
        nextOp = RECHECK_CONF;
        timeRemained = nextRecheck - crtTime;
      } else {
        nextOp = SYS_INFO_SEND;
        timeRemained = nextSysInfoSend - crtTime;
      }
    } else {
      if (nextJobInfoSend > 0 && (nextSysInfoSend <= 0 ||
                                  nextJobInfoSend <= nextSysInfoSend)) {
        nextOp = JOB_INFO_SEND;
        timeRemained = nextJobInfoSend - crtTime;
      } else if (nextSysInfoSend > 0) {
        nextOp = SYS_INFO_SEND;
        timeRemained = nextSysInfoSend - crtTime;
      }
    }

    if (timeRemained == -1)
      timeRemained = RECHECK_INTERVAL;

#ifndef WIN32
    /* the moment when the next operation should be performed */
    delay.tv_sec = crtTime + timeRemained;
    delay.tv_nsec = 0;
#else
    delay = (/*crtTime +*/ timeRemained) * 1000;  // this is in millis
#endif

    pthread_mutex_lock(&(apm -> mutexBack));

    pthread_mutex_lock(&(apm -> mutexCond));
    /* check for changes in the settings */
    haveChange = false;
    if (apm -> jobMonChanged || apm -> sysMonChanged || apm -> recheckChanged)
      haveChange = true;
    if (apm -> jobMonChanged) {
      if (apm -> jobMonitoring)
        nextJobInfoSend = crtTime + apm -> jobMonitorInterval;
      else
        nextJobInfoSend = -1;
      apm -> jobMonChanged = false;
    }
    if (apm -> sysMonChanged) {
      if (apm -> sysMonitoring)
        nextSysInfoSend = crtTime + apm -> sysMonitorInterval;
      else
        nextSysInfoSend = -1;
      apm -> sysMonChanged = false;
    }
    if (apm -> recheckChanged) {
      if (apm -> confCheck) {
        nextRecheck = crtTime + apm -> crtRecheckInterval;
      }
      else
        nextRecheck = -1;
      apm -> recheckChanged = false;
    }
    pthread_mutex_unlock(&(apm -> mutexBack));

    if (haveChange) {
      pthread_mutex_unlock(&(apm -> mutexCond));
      continue;
    }

    /* wait until the next operation should be performed or until
       a change in the settings occurs */
#ifndef WIN32
    ret = pthread_cond_timedwait(&(apm -> confChangedCond),
                                &(apm -> mutexCond), &delay);
    pthread_mutex_unlock(&(apm -> mutexCond));
#else
    pthread_mutex_unlock(&(apm -> mutexCond));
    ret = WaitForSingleObject(apm->confChangedCond, delay);
#endif
    if (ret == ETIMEDOUT) {
//      printf("### ret TIMEDOUT\n");
      /* now perform the operation */
      if (nextOp == JOB_INFO_SEND) {
        apm -> sendJobInfo();
        crtTime = time(NULL);
        nextJobInfoSend = crtTime + apm -> getJobMonitorInterval();
      }

      if (nextOp == SYS_INFO_SEND) {
        apm -> sendSysInfo();
        if (apm -> getGenMonitoring()) {
          if (generalInfoCount <= 1)
            apm -> sendGeneralInfo();
          generalInfoCount = (generalInfoCount + 1) % apm -> genMonitorIntervals;
        }
        crtTime = time(NULL);
        nextSysInfoSend = crtTime + apm -> getSysMonitorInterval();
      }

      if (nextOp == RECHECK_CONF) {
        //logger(FINE, "### recheck conf");
        resourceChanged = false;
        try {
          if (apm -> initType == FILE_INIT) {
            sprintf(logmsg, "Checking for modifications for file %s ",
                    apm -> initSources[0]);
            logger(INFO, logmsg);
            stat(apm -> initSources[0], &st);
            if (st.st_mtime > apm -> lastModifFile) {
              sprintf(logmsg, "File %s modified ", apm -> initSources[0]);
              logger(INFO, logmsg);
              resourceChanged = true;
            }
          }

          // check the configuration URLs
          for (i = 0; i < apm -> confURLs.nConfURLs; i++) {
            sprintf(logmsg, "[Checking for modifications for URL %s ] ",
                   apm -> confURLs.vURLs[i]);
            logger(INFO, logmsg);
            if (urlModified(apm -> confURLs.vURLs[i], apm -> confURLs.lastModifURLs[i])) {
              sprintf(logmsg, "URL %s modified ", apm -> confURLs.vURLs[i]);
              logger(INFO, logmsg);
              resourceChanged = true;
              break;
            }
          }

          if (resourceChanged) {
            logger(INFO, "Reloading configuration...");
            if (apm -> initType == FILE_INIT)
              apm -> initialize(apm -> initSources[0], false);
            else
              apm -> initialize(apm -> nInitSources, apm -> initSources, false);
          }
          apm -> setCrtRecheckInterval(apm -> getRecheckInterval());
        } catch (runtime_error &err) {
          logger(WARNING, err.what());
          logger(WARNING, "Increasing the time interval for reloading the configuration...");
          apm -> setCrtRecheckInterval(apm -> getRecheckInterval() * 5);
        }
        crtTime = time(NULL);
        nextRecheck = crtTime + apm -> getCrtRecheckInterval();
        //sleep(apm -> getCrtRecheckInterval());
      }
    }

  } // while

#ifndef WIN32
  return NULL; // it doesn't matter what we return here
#else
  return 0;
#endif
}

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

Generated at Fri Sep 2 2011 16:25:08 for Gaudi Framework, version v22r4 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004