Merge changes I8f46f597,I74e17e92 into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / base / performance-logging / performance-server.cpp
index 79cf214..d6e68fe 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
 
 // INTERNAL INCLUDES
 #include <base/environment-options.h>
+#include <base/time-service.h>
 
 namespace Dali
 {
@@ -33,23 +34,50 @@ namespace Internal
 namespace Adaptor
 {
 
+namespace
+{
+const unsigned int NANOSECONDS_PER_MICROSECOND = 1000u;
+const float        MICROSECONDS_TO_SECOND = 1e-6;
+} // unnamed namespace
+
 PerformanceServer::PerformanceServer( AdaptorInternalServices& adaptorServices,
                                       const EnvironmentOptions& environmentOptions)
-:mPlatformAbstraction( adaptorServices.GetPlatformAbstractionInterface() ),
- mEnvironmentOptions( environmentOptions ),
- mKernelTrace( adaptorServices.GetKernelTraceInterface() ),
- mStatContextManager( *this ),
- mStatisticsLogBitmask( 0 ),
- mLoggingEnabled( false ),
- mLogFunctionInstalled( false )
+: mEnvironmentOptions( environmentOptions ),
+  mKernelTrace( adaptorServices.GetKernelTraceInterface() ),
+  mSystemTrace( adaptorServices.GetSystemTraceInterface() ),
+  mLogMutex(),
+#if defined(NETWORK_LOGGING_ENABLED)
+  mNetworkServer( adaptorServices, environmentOptions ),
+  mNetworkControlEnabled( mEnvironmentOptions.GetNetworkControlMode()),
+#endif
+  mStatContextManager( *this ),
+  mStatisticsLogBitmask( 0 ),
+  mPerformanceOutputBitmask( 0 ),
+  mLoggingEnabled( false ),
+  mLogFunctionInstalled( false )
 {
   SetLogging( mEnvironmentOptions.GetPerformanceStatsLoggingOptions(),
               mEnvironmentOptions.GetPerformanceTimeStampOutput(),
               mEnvironmentOptions.GetPerformanceStatsLoggingFrequency());
+
+#if defined(NETWORK_LOGGING_ENABLED)
+  if( mNetworkControlEnabled )
+  {
+    mLoggingEnabled  = true;
+    mNetworkServer.Start();
+  }
+#endif
 }
 
 PerformanceServer::~PerformanceServer()
 {
+#if defined(NETWORK_LOGGING_ENABLED)
+  if( mNetworkControlEnabled )
+  {
+    mNetworkServer.Stop();
+  }
+#endif
+
   if( mLogFunctionInstalled )
   {
     mEnvironmentOptions.UnInstallLogFunction();
@@ -60,18 +88,19 @@ void PerformanceServer::SetLogging( unsigned int statisticsLogOptions,
                                     unsigned int timeStampOutput,
                                     unsigned int logFrequency )
 {
-  if( ( statisticsLogOptions == 0) && ( timeStampOutput == 0 ))
-  {
-    mLoggingEnabled = false;
-    return;
-  }
-
   mStatisticsLogBitmask = statisticsLogOptions;
   mPerformanceOutputBitmask = timeStampOutput;
 
   mStatContextManager.SetLoggingLevel( mStatisticsLogBitmask, logFrequency);
 
-  mLoggingEnabled = true;
+  if( ( mStatisticsLogBitmask == 0) && ( mPerformanceOutputBitmask == 0 ))
+  {
+    mLoggingEnabled = false;
+  }
+  else
+  {
+    mLoggingEnabled = true;
+  }
 }
 
 void PerformanceServer::SetLoggingFrequency( unsigned int logFrequency, ContextId contextId )
@@ -104,17 +133,13 @@ void PerformanceServer::AddMarker( MarkerType markerType, ContextId contextId )
     return;
   }
 
-  // This is only called from main event thread, but may overlap with internal AddMarker calls
-  // from other threads ( update, render etc).
-  boost::mutex::scoped_lock sharedDatalock( mDataMutex );
-
   // Get the time stamp
-  unsigned int seconds = 0;
-  unsigned int microseconds = 0;
-  mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
+  uint64_t timeStamp = 0;
+  TimeService::GetNanoseconds( timeStamp );
+  timeStamp /= NANOSECONDS_PER_MICROSECOND; // Convert to microseconds
 
   // Create a marker
-  PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ) );
+  PerformanceMarker marker( markerType, FrameTimeStamp( 0, timeStamp ) );
 
   // get the marker description for this context, e.g SIZE_NEGOTIATION_START
   const char* const description = mStatContextManager.GetMarkerDescription( markerType, contextId );
@@ -124,7 +149,6 @@ void PerformanceServer::AddMarker( MarkerType markerType, ContextId contextId )
 
   // Add custom marker to statistics context manager
   mStatContextManager.AddCustomMarker( marker, contextId );
-
 }
 
 void PerformanceServer::AddMarker( MarkerType markerType )
@@ -136,10 +160,6 @@ void PerformanceServer::AddMarker( MarkerType markerType )
     return;
   }
 
-  // AddMarker can be called from multiple threads, to avoid problems
-  // with updating contexts and the kernel trace, lock here.
-  boost::mutex::scoped_lock sharedDatalock( mDataMutex );
-
   if( markerType == VSYNC )
   {
     // make sure log function is installed, note this will be called only from v-sync thread
@@ -152,12 +172,12 @@ void PerformanceServer::AddMarker( MarkerType markerType )
   }
 
   // Get the time
-  unsigned int seconds = 0;
-  unsigned int microseconds = 0;
-  mPlatformAbstraction.GetTimeMicroseconds( seconds, microseconds );
+  uint64_t timeStamp = 0;
+  TimeService::GetNanoseconds( timeStamp );
+  timeStamp /= NANOSECONDS_PER_MICROSECOND; // Convert to microseconds
 
   // Create a marker
-  PerformanceMarker marker( markerType, FrameTimeStamp( 0, seconds, microseconds ) );
+  PerformanceMarker marker( markerType, FrameTimeStamp( 0, timeStamp ) );
 
   // log it
   LogMarker(marker, marker.GetName() );
@@ -174,24 +194,40 @@ void PerformanceServer::LogContextStatistics( const char* const text )
 
 void PerformanceServer::LogMarker( const PerformanceMarker& marker, const char* const description )
 {
-
-
+#if defined(NETWORK_LOGGING_ENABLED)
+  // log to the network ( this is thread safe )
+  if( mNetworkControlEnabled )
+  {
+    mNetworkServer.TransmitMarker( marker, description );
+  }
+#endif
 
   // log to kernel trace
   if( mPerformanceOutputBitmask & OUTPUT_KERNEL_TRACE )
   {
-    // name will be something like UPDATE_START or UPDATE_END
-    mKernelTrace.Trace( description );
+    // Kernel tracing implementation may not be thread safe
+    Mutex::ScopedLock lock( mLogMutex );
+    // description will be something like UPDATE_START or UPDATE_END
+    mKernelTrace.Trace( marker, description );
   }
 
-  // log to Dali log
+  // log to system trace
+  if( mPerformanceOutputBitmask & OUTPUT_SYSTEM_TRACE )
+  {
+    // System  tracing implementation may not be thread safe
+    Mutex::ScopedLock lock( mLogMutex );
+
+    mSystemTrace.Trace( marker, description );
+  }
+
+  // log to Dali log ( this is thread safe )
   if ( mPerformanceOutputBitmask & OUTPUT_DALI_LOG )
   {
     Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo,
-                                    "%d.%06d (seconds), %s\n",
-                                    marker.GetTimeStamp().seconds,
-                                    marker.GetTimeStamp().microseconds,
+                                    "%.6f (seconds), %s\n",
+                                    (float)( marker.GetTimeStamp().microseconds * MICROSECONDS_TO_SECOND ),
                                     description);
+
   }
 }