#include "utilities.h"
extern bool _Sleeping ;
+static Engines_Component_i * theEngines_Component ;
Engines_Component_i::Engines_Component_i()
{
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);
: _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);
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 ") ;
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 ;
}
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 ) ) {
return false ;
}
else {
- MESSAGE("Killer : ThreadId " << ThreadId << " pthread_canceled") ;
+ MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId << " pthread_canceled") ;
}
}
else {
return false ;
}
else {
- MESSAGE("Killer : ThreadId " << ThreadId << " pthread_killed("
+ MESSAGE(pthread_self() << "Killer : ThreadId " << ThreadId << " pthread_killed("
<< 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 ) ;
}
+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 ;
}