Salome HOME
PR : merge branch V1_2c dans branche principale pour V1_3_0_b1
[modules/kernel.git] / src / Container / Component_i.cxx
index 400072d0f32a15650fa51c36ca632881427d6104..09d463416e59a143acb2e0ed96544e6453dd044d 100644 (file)
@@ -35,6 +35,7 @@ using namespace std;
 #include "utilities.h"
 
 extern bool _Sleeping ;
+static Engines_Component_i * theEngines_Component ;
 
 Engines_Component_i::Engines_Component_i()
 {
@@ -47,8 +48,10 @@ Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
                                         const char *instanceName,
                                         const char *interfaceName,
                                          bool notif) :
-  _instanceName(instanceName), _interfaceName(interfaceName),
-  _myConnexionToRegistry(0), _ThreadId(0) , _graphName("") , _nodeName("") {
+  _instanceName(instanceName),
+  _interfaceName(interfaceName),
+  _myConnexionToRegistry(0),
+  _ThreadId(0) , _ThreadCpuUsed(0) , _Executed(false) , _graphName("") , _nodeName("") {
   MESSAGE("Component constructor with instanceName "<< _instanceName);
   _orb = CORBA::ORB::_duplicate(orb);
   _poa = PortableServer::POA::_duplicate(poa);
@@ -71,8 +74,7 @@ Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
   : _instanceName(instanceName),
     _interfaceName(interfaceName),
     _myConnexionToRegistry(0),
-    _ThreadId(0)
-{
+    _ThreadId(0) , _ThreadCpuUsed(0) , _Executed(false) , _graphName("") , _nodeName("") {
 //  MESSAGE("Component constructor with instanceName "<< _instanceName);
   _orb = CORBA::ORB::_duplicate(orb);
   _poa = PortableServer::POA::_duplicate(poa);
@@ -134,11 +136,13 @@ PortableServer::ObjectId * Engines_Component_i::getId()
 
 void Engines_Component_i::beginService(const char *serviceName)
 {
-  MESSAGE("Send BeginService notification for " << serviceName << endl
+  MESSAGE(pthread_self() << "Send BeginService notification for " << serviceName << endl
          << "Component instance : " << _instanceName << endl << endl);
   _ThreadId = pthread_self() ;
   _StartUsed = 0 ;
   _StartUsed = CpuUsed_impl() ;
+  _ThreadCpuUsed = 0 ;
+  _Executed = true ;
   _serviceName = serviceName ;
   if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) ) {
     perror("pthread_setcanceltype ") ;
@@ -148,15 +152,17 @@ void Engines_Component_i::beginService(const char *serviceName)
     perror("pthread_setcancelstate ") ;
     exit(0) ;
   }
-  MESSAGE("Return from BeginService for " << serviceName
-          << " ThreadId " << _ThreadId
-          << " _graphName " << _graphName << " _nodeName " << _nodeName );
+//  MESSAGE(pthread_self() << " Return from BeginService for " << serviceName
+//          << " ThreadId " << _ThreadId << " StartUsed " << _StartUsed
+//          << " _graphName " << _graphName << " _nodeName " << _nodeName );
 }
 
 void Engines_Component_i::endService(const char *serviceName)
 {
-  MESSAGE("Send EndService notification for " << serviceName << endl
-         << "Component instance : " << _instanceName << endl << endl);
+  _ThreadCpuUsed = CpuUsed_impl() ;
+  MESSAGE(pthread_self() << " Send EndService notification for " << serviceName << endl
+         << " Component instance : " << _instanceName << " StartUsed " << _StartUsed << " _ThreadCpuUsed "
+          << _ThreadCpuUsed << endl << endl);
   _ThreadId = 0 ;
 }
 
@@ -176,7 +182,7 @@ char* Engines_Component_i::nodeName() {
   return CORBA::string_dup( _nodeName.c_str() ) ;
 }
 
-bool Killer( int ThreadId , int signum ) {
+bool Engines_Component_i::Killer( int ThreadId , int signum ) {
   if ( ThreadId ) {
     if ( signum == 0 ) {
       if ( pthread_cancel( ThreadId ) ) {
@@ -184,7 +190,7 @@ bool Killer( int ThreadId , int signum ) {
         return false ;
       }
       else {
-        MESSAGE("Killer : ThreadId " << ThreadId << " pthread_canceled") ;
+        MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId << " pthread_canceled") ;
       }
     }
     else {
@@ -193,7 +199,7 @@ bool Killer( int ThreadId , int signum ) {
         return false ;
       }
       else {
-        MESSAGE("Killer : ThreadId " << ThreadId << " pthread_killed("
+        MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId << " pthread_killed("
                 << signum << ")") ;
       }
     }
@@ -202,12 +208,12 @@ bool Killer( int ThreadId , int signum ) {
 }
 
 bool Engines_Component_i::Kill_impl() {
-  MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
-          << " pid " << getpid() << " instanceName "
-          << _instanceName.c_str() << " interface " << _interfaceName.c_str()
-          << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
-          << dec << " _ThreadId " << _ThreadId << " this " << hex << this
-          << dec ) ;
+//  MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
+//          << " pid " << getpid() << " instanceName "
+//          << _instanceName.c_str() << " interface " << _interfaceName.c_str()
+//          << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
+//          << dec << " _ThreadId " << _ThreadId << " this " << hex << this
+//          << dec ) ;
   bool RetVal = false ;
   if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
     RetVal = Killer( _ThreadId , 0 ) ;
@@ -268,21 +274,68 @@ bool Engines_Component_i::Resume_impl() {
 
 }
 
+void SetCpuUsed() {
+  theEngines_Component->SetCurCpu() ;
+}
+void Engines_Component_i::SetCurCpu() {
+  _ThreadCpuUsed =  CpuUsed() ;
+//  MESSAGE(pthread_self() << " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
+}
+
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <unistd.h>
 
-long Engines_Component_i::CpuUsed_impl() {
+long Engines_Component_i::CpuUsed() {
+  long cpu = 0 ;
   struct rusage usage ;
-  long cpu ;
-  if ( getrusage( RUSAGE_SELF , &usage ) == -1 ) {
-    perror("GraphBase::CpuUsed") ;
-    return 0 ;
+  if ( _ThreadId || _Executed ) {
+    if ( getrusage( RUSAGE_SELF , &usage ) == -1 ) {
+      perror("Engines_Component_i::CpuUsed") ;
+      return 0 ;
+    }
+    cpu = usage.ru_utime.tv_sec - _StartUsed ;
+//    cout << pthread_self() << " Engines_Component_i::CpuUsed " << " " << _serviceName
+//         << usage.ru_utime.tv_sec << " - " << _StartUsed << " = " << cpu << endl ;
+  }
+  else {
+//    cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId " << _ThreadId << " " << _serviceName
+//         << " _StartUsed " << _StartUsed << endl ;
+  }
+  return cpu ;
+}
+
+long Engines_Component_i::CpuUsed_impl() {
+  long cpu = 0 ;
+  if ( _ThreadId || _Executed ) {
+    if ( _ThreadId > 0 ) {
+      if ( pthread_self() != _ThreadId ) {
+        if ( _Sleeping ) {
+        }
+        else {
+// Get Cpu in the appropriate thread with that object !...
+          theEngines_Component = this ;
+          Killer( _ThreadId ,SIGUSR1 ) ;
+        }
+        cpu = _ThreadCpuUsed ;
+      }
+      else {
+        _ThreadCpuUsed = CpuUsed() ;
+        cpu = _ThreadCpuUsed ;
+//        cout << pthread_self() << " Engines_Component_i::CpuUsed_impl " << _serviceName << " " << cpu
+//             << endl ;
+      }
+    }
+    else {
+      cpu = _ThreadCpuUsed ;
+//      cout << pthread_self() << " Engines_Component_i::CpuUsed_impl " << _serviceName << " " << cpu
+//           << endl ;
+    }
+  }
+  else {
+//    cout << pthread_self() << "Engines_Component_i::CpuUsed_impl _ThreadId " << _ThreadId << " "
+//         << _serviceName << " _StartUsed " << _StartUsed << endl ;
   }
-//  return usage.ru_utime.__time_t tv_sec ;
-  cout << "CpuUsed " << usage.ru_utime.tv_sec << " " << usage.ru_utime.tv_usec << " "
-       << usage.ru_stime.tv_sec << " " << usage.ru_stime.tv_usec << endl ;
-  cpu = usage.ru_utime.tv_sec - _StartUsed ;
   return cpu ;
 }