(file) Return to Monitor.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

Diff for /pegasus/src/Pegasus/Common/Monitor.cpp between version 1.15 and 1.25

version 1.15, 2002/05/29 23:26:34 version 1.25, 2002/06/19 22:06:33
Line 1 
Line 1 
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
 // //
 // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM  // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
   // The Open Group, Tivoli Systems
 // //
 // Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
Line 38 
Line 39 
 # if defined(FD_SETSIZE) && FD_SETSIZE != 1024 # if defined(FD_SETSIZE) && FD_SETSIZE != 1024
 #  error "FD_SETSIZE was not set to 1024 prior to the last inclusion \ #  error "FD_SETSIZE was not set to 1024 prior to the last inclusion \
 of <winsock.h>. It may have been indirectly included (e.g., by including \ of <winsock.h>. It may have been indirectly included (e.g., by including \
 <windows.h>). Find the inclusion of that header which is visible to this \  <windows.h>). Finthe inclusion of that header which is visible to this \
 compilation unit and #define FD_SETZIE to 1024 prior to that inclusion; \ compilation unit and #define FD_SETZIE to 1024 prior to that inclusion; \
 otherwise, less than 64 clients (the default) will be able to connect to the \ otherwise, less than 64 clients (the default) will be able to connect to the \
 CIMOM. PLEASE DO NOT SUPPRESS THIS WARNING; PLEASE FIX THE PROBLEM." CIMOM. PLEASE DO NOT SUPPRESS THIS WARNING; PLEASE FIX THE PROBLEM."
Line 60 
Line 61 
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
   
   static AtomicInt _connections = 0;
   
   
   static struct timeval create_time = {0, 1};
   static struct timeval destroy_time = {5, 0};
   static struct timeval deadlock_time = {1000, 0};
   
 //////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
 // //
 // MonitorRep // MonitorRep
Line 86 
Line 95 
    : _module_handle(0), _controller(0), _async(false)    : _module_handle(0), _controller(0), _async(false)
 { {
     Socket::initializeInterface();     Socket::initializeInterface();
     _rep = new MonitorRep;      _rep = 0;
     FD_ZERO(&_rep->rd_fd_set);      _entries.reserve(128);
     FD_ZERO(&_rep->wr_fd_set);  
     FD_ZERO(&_rep->ex_fd_set);  }
     FD_ZERO(&_rep->active_rd_fd_set);  
     FD_ZERO(&_rep->active_wr_fd_set);  Monitor::Monitor(Boolean async)
     FD_ZERO(&_rep->active_ex_fd_set);     : _module_handle(0), _controller(0), _async(async)
   {
       Socket::initializeInterface();
       _rep = 0;
       _entries.reserve(128);
       if( _async == true )
       {
          _thread_pool = new ThreadPool(0,
                                        "Monitor",
                                        0,
                                        0,
                                        create_time,
                                        destroy_time,
                                        deadlock_time);
       }
       else
          _thread_pool = 0;
 } }
  
 Monitor::~Monitor() Monitor::~Monitor()
Line 108 
Line 133 
     }     }
     Tracer::trace(TRC_HTTP, Tracer::LEVEL4, "deleting rep");     Tracer::trace(TRC_HTTP, Tracer::LEVEL4, "deleting rep");
  
     delete _rep;  
     Tracer::trace(TRC_HTTP, Tracer::LEVEL4, "uninitializing interface");     Tracer::trace(TRC_HTTP, Tracer::LEVEL4, "uninitializing interface");
     Socket::uninitializeInterface();     Socket::uninitializeInterface();
     Tracer::trace(TRC_HTTP, Tracer::LEVEL4,     Tracer::trace(TRC_HTTP, Tracer::LEVEL4,
                   "returning from monitor destructor");                   "returning from monitor destructor");
       if(_async == true)
          delete _thread_pool;
 } }
  
  
   int Monitor::kill_idle_threads()
 //<<< Tue May 14 20:38:26 2002 mdd >>>  
 //  register with module controller  
 //  when it is time to enqueue the message,  
 // use an async_thread_exec call to  
 // isolate the entire if(events) { enqueue -> fd_clear } block  
 //  let the thread pool grow and shrink according to load.  
   
 Boolean Monitor::run(Uint32 milliseconds)  
 { {
    // register the monitor as a module to gain access to the cimserver's thread pool     static struct timeval now, last;
    // <<< Wed May 15 09:52:16 2002 mdd >>>     gettimeofday(&now, NULL);
    while( _module_handle == NULL)     int dead_threads = 0;
   
      if( now.tv_sec - last.tv_sec > 0 )
    {    {
         gettimeofday(&last, NULL);
       try       try
       {       {
          _controller = &(ModuleController::register_module(PEGASUS_QUEUENAME_CONTROLSERVICE,  
                                                            PEGASUS_MODULENAME_MONITOR,  
                                                            (void *)this,  
                                                            0,  
                                                            0,  
                                                            0,  
                                                            &_module_handle));  
  
            dead_threads =  _thread_pool->kill_dead_threads();
       }       }
       catch(IncompatibleTypes &)        catch(IPCException& )
       {       {
          ModuleController* controlService =  
             new ModuleController(PEGASUS_QUEUENAME_CONTROLSERVICE);  
       }       }
       catch( AlreadyExists & )  
       {  
          break;  
       }       }
      return dead_threads;
    }    }
  
 #ifdef PEGASUS_OS_TYPE_WINDOWS  
  
     // Windows select() has a strange little bug. It returns immediately if  Boolean Monitor::run(Uint32 milliseconds)
     // there are no descriptors in the set even if the timeout is non-zero.  {
     // To work around this, we call Sleep() for now:  
   
     if (_entries.size() == 0)  
         Sleep(milliseconds);  
   
 #endif  
   
     // Check for events on the selected file descriptors. Only do this if  
     // there were no undispatched events from last time.  
   
     int count = 0;  
  
       Boolean handled_events = false;
       int i = 0;
  
     memcpy(&_rep->active_rd_fd_set, &_rep->rd_fd_set, sizeof(fd_set));      struct timeval tv = {0,1};
     memcpy(&_rep->active_wr_fd_set, &_rep->wr_fd_set, sizeof(fd_set));      fd_set fdread;
     memcpy(&_rep->active_ex_fd_set, &_rep->ex_fd_set, sizeof(fd_set));      FD_ZERO(&fdread);
  
     const Uint32 SECONDS = milliseconds / 1000;      _entries_mut.lock(pegasus_thread_self());
     const Uint32 MICROSECONDS = (milliseconds % 1000) * 1000;  
     struct timeval tv = { SECONDS, MICROSECONDS };  
  
     count = select(      for( int indx = 0; indx < (int)_entries.size(); indx++)
        FD_SETSIZE,      {
        &_rep->active_rd_fd_set,         if(_entries[indx]._status == _MonitorEntry::IDLE)
        &_rep->active_wr_fd_set,  
        &_rep->active_ex_fd_set,  
        &tv);  
     if (count == 0)  
     {     {
        return false;            FD_SET(_entries[indx].socket, &fdread);
          }
     }     }
       _entries_mut.unlock();
   
       int events = select(FD_SETSIZE, &fdread, NULL, NULL, &tv);
  
 #ifdef PEGASUS_OS_TYPE_WINDOWS #ifdef PEGASUS_OS_TYPE_WINDOWS
     else if (count == SOCKET_ERROR)      if(events && events != SOCKET_ERROR )
 #else #else
     else if (count == -1)      if(events && events != -1 )
 #endif #endif
     {     {
        return false;         for( int indx = 0; indx < (int)_entries.size(); indx++)
     }  
   
     Boolean handled_events = false;  
     for (Uint32 i = 0, n = _entries.size(); i < _entries.size(); i++)  
     {     {
         Sint32 socket = _entries[i].socket;            if(FD_ISSET(_entries[indx].socket, &fdread))
         Uint32 events = 0;  
   
         if(_entries[i].dying.value() > 0 )  
         {         {
            if(_entries[i]._type == Monitor::CONNECTION)               MessageQueue *q = MessageQueue::lookup(_entries[indx].queueId);
                if(q == 0)
            {            {
                   unsolicitSocketMessages(indx);
                   return true;
                }
  
               MessageQueue *q = MessageQueue::lookup(_entries[i].queueId);               if(_entries[indx]._type == Monitor::CONNECTION)
               if(q && static_cast<HTTPConnection *>(q)->is_dying() &&  
                  (0 == static_cast<HTTPConnection *>(q)->refcount.value()))  
               {               {
                  static_cast<HTTPConnection *>(q)->lock_connection();                  static_cast<HTTPConnection *>(q)->_entry_index = indx;
                  static_cast<HTTPConnection *>(q)->unlock_connection();                  if(static_cast<HTTPConnection *>(q)->_dying.value() > 0 )
                   {
                      _entries[indx]._status = _MonitorEntry::DYING;
                  MessageQueue & o = static_cast<HTTPConnection *>(q)->get_owner();                  MessageQueue & o = static_cast<HTTPConnection *>(q)->get_owner();
                  Message* message= new CloseConnectionMessage(static_cast<HTTPConnection *>(q)->getSocket());                     Message* message= new CloseConnectionMessage(_entries[indx].socket);
                  message->dest = o.getQueueId();                  message->dest = o.getQueueId();
                  o.enqueue(message);                  o.enqueue(message);
                  i--;                     return true;
                  n = _entries.size();  
               }  
            }  
         }         }
  
         if (FD_ISSET(socket, &_rep->active_rd_fd_set))                  _entries[indx]._status = _MonitorEntry::BUSY;
             events |= SocketMessage::READ;                  _thread_pool->allocate_and_awaken((void *)q, _dispatch);
   
         if (FD_ISSET(socket, &_rep->active_wr_fd_set))  
             events |= SocketMessage::WRITE;  
   
         if (FD_ISSET(socket, &_rep->active_ex_fd_set))  
             events |= SocketMessage::EXCEPTION;  
   
         if (events)  
         {  
             Tracer::trace(TRC_HTTP, Tracer::LEVEL4,  
                "Monitor::run - Socket Event Detected events = %d", events);  
             if (events & SocketMessage::WRITE)  
             {  
                FD_CLR(socket, &_rep->active_wr_fd_set);  
                Tracer::trace(TRC_HTTP, Tracer::LEVEL4,  
                              "Monitor::run FD_CLR WRITE");  
             }             }
             if (events & SocketMessage::EXCEPTION)               else
             {  
                FD_CLR(socket, &_rep->active_ex_fd_set);  
                Tracer::trace(TRC_HTTP, Tracer::LEVEL4,  
                              "Monitor::run FD_CLR EXECEPTION");  
             }  
             if (events & SocketMessage::READ)  
             {  
                FD_CLR(socket, &_rep->active_rd_fd_set);  
                Tracer::trace(TRC_HTTP, Tracer::LEVEL4,  
                              "Monitor::run FD_CLR READ");  
             }  
             MessageQueue* queue = MessageQueue::lookup(_entries[i].queueId);  
             if( ! queue )  
             {  
                unsolicitSocketMessages(socket);  
                break;  
             }  
   
             if(_entries[i]._type == Monitor::CONNECTION)  
             {  
                if( static_cast<HTTPConnection *>(queue)->refcount.value() == 0 )  
                {                {
                   int events = 0;
  
                   static_cast<HTTPConnection *>(queue)->refcount++;                  events |= SocketMessage::READ;
                   if( false == static_cast<HTTPConnection *>(queue)->is_dying())                  Message *msg = new SocketMessage(_entries[indx].socket, events);
                      _controller->async_thread_exec(*_module_handle, _dispatch, (void *)queue);                  _entries[indx]._status = _MonitorEntry::BUSY;
                   else                  q->enqueue(msg);
                      static_cast<HTTPConnection *>(queue)->refcount--;                  _entries[indx]._status = _MonitorEntry::IDLE;
                }                  return true;
             }             }
             else               handled_events = true;
             {  
                Message* message = new SocketMessage(socket, events);  
                queue->enqueue(message);  
             }             }
             count--;  
             pegasus_yield();  
         }         }
         handled_events = true;  
     }     }
   
     return(handled_events);     return(handled_events);
 } }
  
 Boolean Monitor::solicitSocketMessages(  
   int  Monitor::solicitSocketMessages(
     Sint32 socket,     Sint32 socket,
     Uint32 events,     Uint32 events,
     Uint32 queueId,     Uint32 queueId,
     int type)     int type)
 { {
   
     PEG_METHOD_ENTER(TRC_HTTP, "Monitor::solictSocketMessage");     PEG_METHOD_ENTER(TRC_HTTP, "Monitor::solictSocketMessage");
  
     // See whether a handler is already registered for this one:     _MonitorEntry entry(socket, queueId, type);
     Uint32 pos = _findEntry(socket);     entry._status = _MonitorEntry::IDLE;
      _entries_mut.lock(pegasus_thread_self());
   
      Boolean found = false;
  
     if (pos != PEGASUS_NOT_FOUND)     int index ;
      for(index = 0; index < (int)_entries.size(); index++)
     {     {
         PEG_METHOD_EXIT();        if(_entries[index]._status == _MonitorEntry::EMPTY)
         return false;        {
            _entries[index] = entry;
            found = true;
            break;
     }     }
      }
     // Set the events:     if(found == false)
      {
     if (events & SocketMessage::READ)  
         FD_SET(socket, &_rep->rd_fd_set);  
   
     if (events & SocketMessage::WRITE)  
         FD_SET(socket, &_rep->wr_fd_set);  
   
     if (events & SocketMessage::EXCEPTION)  
         FD_SET(socket, &_rep->ex_fd_set);  
   
     // Add the entry to the list:  
     _MonitorEntry entry(socket, queueId, type);  
     _entries.append(entry);     _entries.append(entry);
         index = _entries.size() - 1;
     // Success!     }
      _entries_mut.unlock();
      _connections++;
     PEG_METHOD_EXIT();     PEG_METHOD_EXIT();
     return true;     return index;
 } }
  
 Boolean Monitor::unsolicitSocketMessages(Sint32 socket)  void Monitor::unsolicitSocketMessages(Sint32 socket)
 { {
     PEG_METHOD_ENTER(TRC_HTTP, "Monitor::unsolicitSocketMessage");      PEG_METHOD_ENTER(TRC_HTTP, "Monitor::unsolicitSocketMessages");
  
     // Look for the given entry and remove it:      _entries_mut.lock(pegasus_thread_self());
  
     for (Uint32 i = 0, n = _entries.size(); i < n; i++)      for(int index = 0; index < (int)_entries.size(); index++)
     {     {
         if (_entries[i].socket == socket)         if(_entries[index].socket == socket)
         {         {
             Sint32 socket = _entries[i].socket;            _entries[index]._status = _MonitorEntry::EMPTY;
             FD_CLR(socket, &_rep->rd_fd_set);  
             FD_CLR(socket, &_rep->wr_fd_set);  
             FD_CLR(socket, &_rep->ex_fd_set);  
             _entries.remove(i);  
             // ATTN-RK-P3-20020521: Need "Socket::close(socket);" here?  
             PEG_METHOD_EXIT();  
             return true;  
         }  
     }     }
     PEG_METHOD_EXIT();  
     return false;  
 } }
       _entries_mut.unlock();
  
 Uint32 Monitor::_findEntry(Sint32 socket)      PEG_METHOD_EXIT();
 {  
    for (Uint32 i = 0, n = _entries.size(); i < n; i++)  
     {  
         if (_entries[i].socket == socket)  
             return i;  
     }     }
  
     return PEG_NOT_FOUND;  
 }  
  
  
 PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL Monitor::_dispatch(void *parm) PEGASUS_THREAD_RETURN PEGASUS_THREAD_CDECL Monitor::_dispatch(void *parm)
 { {
    HTTPConnection *dst = reinterpret_cast<HTTPConnection *>(parm);    HTTPConnection *dst = reinterpret_cast<HTTPConnection *>(parm);
    if( true == dst->is_dying())  
    {  
       dst->refcount--;  
       return 0;  
    }  
    if( false == dst->is_dying())  
    {  
       if(false == dst->run(1))  
          pegasus_sleep(1);  
  
    }     dst->run(1);
    dst->refcount--;     if( dst->_monitor->_entries.size() > dst->_entry_index )
         dst->_monitor->_entries[dst->_entry_index]._status = _MonitorEntry::IDLE;
   
    return 0;    return 0;
 } }
  


Legend:
Removed from v.1.15  
changed lines
  Added in v.1.25

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2