cgma
AppUtil Class Reference

#include <AppUtil.hpp>

List of all members.

Public Member Functions

void report_resource_usage () const
bool get_terminal_size (int &rows, int &cols)
 ~AppUtil ()
CubitBoolean interrupt ()
void set_interrupt (CubitBoolean)
void clear_interrupt ()
void startup ()
int shutdown ()
void apputil_getrusage (struct rusage &r_usage) const
void set_terminal_size (int rows, int cols)
ProgressToolprogress_tool ()
void progress_tool (ProgressTool *pTool)
CubitString get_app_dir ()
CubitEventDispatcherevent_dispatcher ()
template<class T >
void send_event (const T &event)
 send an event
bool concurrent_enabled ()
CubitThreadPool * thread_pool ()

Static Public Member Functions

static AppUtilinstance_no_create ()
static AppUtilinstance ()
static void delete_instance ()
static CubitBoolean catch_interrupt ()
static void catch_interrupt (CubitBoolean yesno)
static void initialize_settings ()

Private Member Functions

void send_event (CubitObservable *obs, const CubitEvent &event)
 AppUtil ()

Private Attributes

int term_width
int term_height
CubitBoolean mAppStarted
CubitString cubitDir
ProgressToolmProgressTool
CubitEventDispatcher mEventDispatcher
clock_t mInterruptThrottle
CubitBoolean concurrentSupport
CubitThreadPool * mThreadPool

Static Private Attributes

static CubitBoolean catching_sigint_ = CUBIT_FALSE
static AppUtilinstance_ = NULL

Detailed Description

Definition at line 35 of file AppUtil.hpp.


Constructor & Destructor Documentation

Definition at line 217 of file AppUtil.cpp.

{
  instance_ = NULL;
  catch_interrupt( CUBIT_FALSE );
  if (mProgressTool)
      delete mProgressTool;
  mProgressTool = NULL;
#ifdef CUBIT_USE_THREADS
  delete mThreadPool;
#endif
}
AppUtil::AppUtil ( ) [private]

Definition at line 191 of file AppUtil.cpp.

{
    term_width = 0;
    term_height = 0;
    mAppStarted = CUBIT_FALSE;

      // reset catching_sigint_ here, even though it's static, 'cuz it used
      // to be reset to false on construction before going static

    catching_sigint_ = CUBIT_FALSE;

    // create a default progress tool that does nothing
    // an app can replace this with an app specific progress tool
    mProgressTool = new StubProgressTool();

    concurrentSupport = false;

    this->mInterruptThrottle = clock();

#ifdef CUBIT_USE_THREADS
    mThreadPool = new CubitThreadPool;
#else
    mThreadPool = NULL;
#endif
}

Member Function Documentation

void AppUtil::apputil_getrusage ( struct rusage &  r_usage) const

Definition at line 418 of file AppUtil.cpp.

{
    // get the resource usage as defined by getrusage
#ifndef JANUS
#ifndef _WIN32
   getrusage(RUSAGE_SELF, &r_usage);

   if (r_usage.ru_maxrss == 0) {
       // this machine doesn't return rss - try going to /proc
       // print the file name to open
#ifdef SOLARIS
     char buffer[120];
     strcpy(buffer, "/proc/self/psinfo");
     int file_ptr = -1;
     file_ptr = open(buffer, O_RDONLY);
     if (file_ptr < 0) return;
     struct psinfo myps_info;
     read(file_ptr, &myps_info, sizeof(myps_info));
     static int page_size = getpagesize();
     r_usage.ru_maxrss = myps_info.pr_rssize*1024/page_size;
     r_usage.ru_idrss = myps_info.pr_size*1024/page_size;
     close (file_ptr);
#endif // SOLARIS
#ifdef CUBIT_LINUX
     char file_str[4096], dum_str[4096];
     int file_ptr = -1, file_len;
     file_ptr = open("/proc/self/stat", O_RDONLY);
     file_len = read(file_ptr, file_str, sizeof(file_str)-1);
     if (file_len == 0) return;
     close(file_ptr);
     file_str[file_len] = '\0';
       // read the preceeding fields and the ones we really want...
     int dum_int;
     unsigned int dum_uint, vm_size, rss;
     static int page_size = getpagesize();
     int num_fields = sscanf(file_str,
                             "%d " // pid
                             "%s " // comm
                             "%c " // state
                             "%d %d %d %d %d " // ppid, pgrp, session, tty, tpgid
                             "%u %u %u %u %u " // flags, minflt, cminflt, majflt, cmajflt
                             "%d %d %d %d %d %d " // utime, stime, cutime, cstime, counter, priority
                             "%u %u " // timeout, itrealvalue
                             "%d " // starttime
                             "%u %u", // vsize, rss
                             &dum_int,
                             dum_str,
                             dum_str,
                             &dum_int, &dum_int, &dum_int, &dum_int, &dum_int,
                             &dum_uint, &dum_uint, &dum_uint, &dum_uint, &dum_uint,
                             &dum_int, &dum_int, &dum_int, &dum_int, &dum_int, &dum_int,
                             &dum_uint, &dum_uint,
                             &dum_int,
                             &vm_size, &rss);
     if (num_fields == 24) {
       r_usage.ru_maxrss = rss/page_size;
       r_usage.ru_idrss = vm_size/page_size;
     }
#endif // CUBIT_LINUX
   }
#endif // _WIN32
#endif // JANUS
}
static CubitBoolean AppUtil::catch_interrupt ( ) [inline, static]

Definition at line 71 of file AppUtil.hpp.

{ return catching_sigint_; }
void AppUtil::catch_interrupt ( CubitBoolean  yesno) [static]

Definition at line 316 of file AppUtil.cpp.

{
#ifndef CUBIT_NO_SIGNAL
  if( yesno )
  {
    if( signal( SIGINT, sigint_handler ) == SIG_ERR )
      PRINT_ERROR("Can't catch SIGINT!\n");
    else
      catching_sigint_ = CUBIT_TRUE;
  }
  else
  {
    if( signal( SIGINT, SIG_DFL ) == SIG_ERR )
      PRINT_ERROR("Can't reset SIGINT handler!\n");
    else
      catching_sigint_ = CUBIT_FALSE;
  }
#endif
}

Definition at line 551 of file AppUtil.cpp.

bool AppUtil::concurrent_enabled ( ) [inline]

Definition at line 122 of file AppUtil.hpp.

  {
    return concurrentSupport;
  }
void AppUtil::delete_instance ( ) [static]

Definition at line 229 of file AppUtil.cpp.

{
  if( NULL != instance_ )
  {
    delete instance_;
    instance_ = NULL;
  }
}    

Definition at line 112 of file AppUtil.hpp.

{ return mEventDispatcher; }

Get the location of the shared library or executable Returns the location of the cubit_util shared library if it is a shared library or if a static library compiled into an executable, gives the location of the executable.

Definition at line 162 of file AppUtil.hpp.

{
  return cubitDir;
}
bool AppUtil::get_terminal_size ( int &  rows,
int &  cols 
)

Definition at line 482 of file AppUtil.cpp.

{
  cols = term_width;
  rows = term_height;
  return rows || cols;
}
void AppUtil::initialize_settings ( ) [static]

Definition at line 520 of file AppUtil.cpp.

AppUtil * AppUtil::instance ( void  ) [static]

Definition at line 169 of file AppUtil.cpp.

{
   if (instance_ == NULL)
   {
      instance_ = new AppUtil();

      if ( !instance_ )
      {
         PRINT_ERROR(" *** Unable to initialize application ***\n");
         exit(1);
      }

        // Initialize interrupted flag to false.
      cubit_intr = CUBIT_FALSE;

      cubit_update_terminal_size(0);

      initialize_settings();
   }
   return instance_;
}

Definition at line 161 of file AppUtil.cpp.

{
  return instance_;
}

Definition at line 525 of file AppUtil.cpp.

{
  if(this->mProgressTool)
  {
    clock_t new_clock = clock();
    clock_t diff = new_clock - this->mInterruptThrottle;
    if((diff / static_cast<double>(CLOCKS_PER_SEC)) > 0.1)
    {
      this->mInterruptThrottle = new_clock;
      mProgressTool->check_interrupt();
    }
  }

  return cubit_intr;
}

Definition at line 495 of file AppUtil.cpp.

{
  assert(mProgressTool != NULL);

  return mProgressTool;
}

Definition at line 502 of file AppUtil.cpp.

{
  assert(pTool != NULL);  // existence is assumed elsewhere in the code
  if (pTool)
  {
      if (mProgressTool)
      {
          delete mProgressTool;
      }
      mProgressTool = pTool;
  }
}

Definition at line 277 of file AppUtil.cpp.

{

#ifndef JANUS
#ifndef _WIN32
   struct rusage r_usage;
   float utime, stime;
   apputil_getrusage(r_usage);
   utime = (float)r_usage.ru_utime.tv_sec +
      ((float)r_usage.ru_utime.tv_usec/1.e6);
   stime = (float)r_usage.ru_stime.tv_sec +
      ((float)r_usage.ru_stime.tv_usec/1.e6);
   static int pagesize = getpagesize();
   PRINT_INFO("\nEnd of Execution\n");
   PRINT_INFO("User time             = %f\n", utime);
   PRINT_INFO("System time           = %f\n", stime);
   PRINT_INFO("Total time            = %f\n", utime+stime);
   PRINT_INFO("Max resident set size = %ld bytes\n", r_usage.ru_maxrss*pagesize);
   PRINT_INFO("Int resident set size = %ld\n", r_usage.ru_idrss);
   PRINT_INFO("Minor Page Faults     = %ld\n", r_usage.ru_minflt);
   PRINT_INFO("Major Page Faults     = %ld\n", r_usage.ru_majflt);
   PRINT_INFO("Swaps                 = %ld\n", r_usage.ru_nswap);
#endif// _WIN32
#endif//JANUS
}
template<class T >
void AppUtil::send_event ( const T &  event) [inline]

send an event

Definition at line 116 of file AppUtil.hpp.

  {
    CubitObservable* obs = dynamic_cast<CubitObservable*>(event.get_entity());
    this->send_event(obs, event);
  }
void AppUtil::send_event ( CubitObservable obs,
const CubitEvent event 
) [private]

Definition at line 556 of file AppUtil.cpp.

{
    this->mEventDispatcher.send_event(observable, event);
}

Definition at line 541 of file AppUtil.cpp.

{
  if(f)
  {
    // call raise so it works regardless of the current signal handler
    raise(SIGINT);
  }
  cubit_intr = f;
}
void AppUtil::set_terminal_size ( int  rows,
int  cols 
)

Definition at line 489 of file AppUtil.cpp.

{
  term_width = cols;
  term_height = rows;
}

Definition at line 392 of file AppUtil.cpp.

{
    // When the user is done, print out additional info if requested
   if (DEBUG_FLAG(3))
      report_resource_usage();

     // Return the number of errors in the session
   int ret_val = ( CubitMessage::instance()->error_count() );
   if ( ret_val > 0 )
   {
     PRINT_ERROR("Errors found during CUBIT session.\n");
   }

     // Delete the singletons
   CubitMessage::delete_instance();

   SettingHandler::delete_instance();

#ifdef BUILD_WITH_CONCURRENT_SUPPORT
   delete mConcurrentInstance;
#endif

   mAppStarted = CUBIT_FALSE;

   return ret_val;
}
void AppUtil::startup ( )

Definition at line 337 of file AppUtil.cpp.

{
#ifndef NOT_AN_APP
    if (mAppStarted)
        return;
   
    // get paths to binaries that AppUtil is compiled into
    // we'll make this our "cubitDir" and can be used a reference for 
    // where Cubit is installed
#ifdef _WIN32
    wchar_t path_buffer[PATH_MAX];
    if(GetModuleFileNameW((HINSTANCE)&__ImageBase, path_buffer, PATH_MAX))
    {
      *wcsrchr(path_buffer, '\\') = '\0';
      cubitDir = CubitString::toUtf8(path_buffer);
    }
#else
    Dl_info dl_info;

    // Note: this is a nasty hack to avoid the warning:
    // ISO C++ forbids casting between pointer-to-function and pointer-to-object
    // Even with reinterpret_cast, this warning is still generated
    // GCC accepts reinterpret_cast for purposes like in dlsym calls as an extension,
    // but that might not work for other compilers
    // This union-trick has been applied to some open source code, like:
    // https://mail.gnome.org/archives/commits-list/2011-February/msg06409.html
    // http://sourceforge.net/p/scstudio/mailman/message/29083334/
    union {
      void* ptr;
      AppUtil* (*func)();
    } cast_u;
    cast_u.func = AppUtil::instance;

    dladdr(cast_u.ptr, &dl_info);
    CubitString path_buffer = dl_info.dli_fname;
    size_t idx = path_buffer.find_last('/');
    if(idx != CubitString::npos)
    {
      path_buffer = path_buffer.substr(0, idx);
    }
    cubitDir = path_buffer;
#endif

#ifdef BUILD_WITH_CONCURRENT_SUPPORT
    mConcurrentInstance = new CubitQtConcurrent;
    this->concurrentSupport = true;
#endif

    mAppStarted = CUBIT_TRUE;
    // add startup code here
    // nothing to do for now
#endif /*NOT_AN_APP*/
    return;
}
CubitThreadPool * AppUtil::thread_pool ( )

Definition at line 515 of file AppUtil.cpp.

{
  return mThreadPool;
}

Member Data Documentation

Definition at line 133 of file AppUtil.hpp.

Definition at line 150 of file AppUtil.hpp.

Definition at line 141 of file AppUtil.hpp.

AppUtil * AppUtil::instance_ = NULL [static, private]

Definition at line 138 of file AppUtil.hpp.

Definition at line 139 of file AppUtil.hpp.

Definition at line 146 of file AppUtil.hpp.

clock_t AppUtil::mInterruptThrottle [private]

Definition at line 148 of file AppUtil.hpp.

Definition at line 144 of file AppUtil.hpp.

CubitThreadPool* AppUtil::mThreadPool [private]

Definition at line 155 of file AppUtil.hpp.

int AppUtil::term_height [private]

Definition at line 136 of file AppUtil.hpp.

int AppUtil::term_width [private]

Definition at line 136 of file AppUtil.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines