Skip to content

Commit

Permalink
PerformanceMetrics: syncup from metro version
Browse files Browse the repository at this point in the history
  • Loading branch information
HaseenaSainul committed Mar 15, 2024
1 parent 422b785 commit 2f63032
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 58 deletions.
29 changes: 16 additions & 13 deletions PerformanceMetrics/PerformanceMetrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,35 @@ namespace Plugin {

string result;

if( ( config.ObservableCallsign.IsSet() == true ) && (config.ObservableClassname.IsSet() == true) ) {
if ((config.ObservableCallsign.IsSet() == true) && (config.ObservableClassname.IsSet() == true)) {
result = _T("Both callsign and classname set to observe for metrics");
}
else if( ( config.ObservableCallsign.IsSet() == true ) && ( config.ObservableCallsign.Value().empty() == false ) ) {
else if ((config.ObservableCallsign.IsSet() == true) && ( config.ObservableCallsign.Value().empty() == false)) {
_handler.reset(new CallsignPerfMetricsHandler(config.ObservableCallsign.Value()));
}
else if( ( config.ObservableClassname.IsSet() == true ) && ( config.ObservableClassname.Value().empty() == false ) ) {
else if ((config.ObservableClassname.IsSet() == true) && ( config.ObservableClassname.Value().empty() == false)) {
_handler.reset(new ClassnamePerfMetricsHandler(config.ObservableClassname.Value()));
} else {
result = _T("No callsign or classname set to observe for metrics");
}

if( result.empty() == true ) {
if (result.empty() == true) {
ASSERT(_handler);
_handler->Initialize();
service->Register(&_notification);
} else {
#ifndef USE_THUNDER_R4
Deinitialize(service);
#endif
}

return result;
}

void PerformanceMetrics::Deinitialize(PluginHost::IShell* service)
{
if( _handler ) {
ASSERT(service != nullptr);
if (_handler) {
service->Unregister(&_notification);
// as we do this after the unregister the call to Deinitialize should be threadsafe, no more notifications can be received
// if the deactivate of the observable did not happen we must clean up here
Expand Down Expand Up @@ -103,31 +106,31 @@ namespace Plugin {
Exchange::IWebBrowser* webbrowser = service.QueryInterface<Exchange::IWebBrowser>();
PluginHost::IStateControl* statecontrol = service.QueryInterface<PluginHost::IStateControl>();

if( webbrowser != nullptr ) {
TRACE(Trace::Information, (_T("Start oberserving %s as webbrowser"), Callsign().c_str()) );
if (webbrowser != nullptr) {
TRACE(Trace::Information, (_T("Start oberserving %s as webbrowser"), Callsign().c_str()));
_observable = Core::ProxyType<WebBrowserObservable<>>::Create(*this, service, *webbrowser, statecontrol);
webbrowser->Release();
} else {
Exchange::IBrowser* browser = service.QueryInterface<Exchange::IBrowser>();

if( browser != nullptr ) {
TRACE(Trace::Information, (_T("Start oberserving %s as browser"), Callsign().c_str()) );
if (browser != nullptr) {
TRACE(Trace::Information, (_T("Start oberserving %s as browser"), Callsign().c_str()));
_observable = Core::ProxyType<BrowserObservable<>>::Create(*this, service, *browser, statecontrol);
browser->Release();
}
else if( statecontrol != nullptr ) {
TRACE(Trace::Information, (_T("Start oberserving %s as statecontrol"), Callsign().c_str()) );
else if (statecontrol != nullptr) {
TRACE(Trace::Information, (_T("Start oberserving %s as statecontrol"), Callsign().c_str()));
_observable = Core::ProxyType<StateObservable<>>::Create(*this, service, statecontrol);
} else {
TRACE(Trace::Information, (_T("Start oberserving %s as basic"), Callsign().c_str()) );
TRACE(Trace::Information, (_T("Start oberserving %s as basic"), Callsign().c_str()));
_observable = Core::ProxyType<BasicObservable<>>::Create(*this, service);
}
}

ASSERT(_observable.IsValid() == true);
_observable->Enable();

if( statecontrol != nullptr ) {
if (statecontrol != nullptr) {
statecontrol->Release();
}
}
Expand Down
38 changes: 19 additions & 19 deletions PerformanceMetrics/PerformanceMetrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ namespace Plugin {

void Deinitialize() override
{
if( _observable.IsValid() == true ) {
if (_observable.IsValid() == true) {
_observable->Disable();
VARIABLE_IS_NOT_USED uint32_t result =_observable.Release();
ASSERT(result == Core::ERROR_DESTRUCTION_SUCCEEDED);
Expand All @@ -186,7 +186,7 @@ namespace Plugin {

void Activated(PluginHost::IShell& service) override
{
if( service.Callsign() == _callsign ) {
if (service.Callsign() == _callsign) {
ASSERT(_observable.IsValid() == false);

CreateObservable(service);
Expand All @@ -196,7 +196,7 @@ namespace Plugin {

void Deactivated(PluginHost::IShell& service) override
{
if( service.Callsign() == _callsign ) {
if (service.Callsign() == _callsign) {
ASSERT(_observable.IsValid() == true);

_observable->Deactivated(service);
Expand Down Expand Up @@ -247,31 +247,31 @@ namespace Plugin {
void Deinitialize() override
{
// no lock needed, no notification are possible here.
for( auto& observer : _observers ) {
for (auto& observer : _observers) {
observer.second.Deinitialize();
}
_observers.clear();
}

void Activated(PluginHost::IShell& service) override
{
if( service.ClassName() == Classname() ) {
if (service.ClassName() == Classname()) {
_adminLock.Lock();
auto result =_observers.emplace(std::piecewise_construct,
std::forward_as_tuple(service.Callsign()),
std::forward_as_tuple(service.Callsign()));
ASSERT( ( result.second == true ) && ( result.first != _observers.end() ) );
ASSERT((result.second == true) && (result.first != _observers.end()));
result.first->second.Initialize();
result.first->second.Activated(service);
_adminLock.Unlock();
}
}
void Deactivated(PluginHost::IShell& service) override
{
if( service.ClassName() == Classname() ) {
if (service.ClassName() == Classname()) {
_adminLock.Lock();
auto it =_observers.find(service.Callsign());
if( it != _observers.end() ) {
if (it != _observers.end()) {
it->second.Deactivated(service);
it->second.Deinitialize();
_observers.erase(it);
Expand Down Expand Up @@ -363,7 +363,7 @@ namespace Plugin {

void Deactivated(PluginHost::IShell&) override
{
Logger().Deactivated( Uptime() );
Logger().Deactivated(Uptime());
}

uint64_t ActivateTime() const
Expand All @@ -373,7 +373,7 @@ namespace Plugin {

uint32_t Uptime() const
{
return ( Core::Time::Now().Ticks() - ActivateTime() ) / Core::Time::MicroSecondsPerSecond;
return (Core::Time::Now().Ticks() - ActivateTime()) / Core::Time::MicroSecondsPerSecond;
}

CallsignPerfMetricsHandler& Parent() const
Expand Down Expand Up @@ -410,8 +410,8 @@ namespace Plugin {
, _statecontrol(statecontrol)
{
// not very likely but it could happen that we have a Browser without StatControl
if(_statecontrol != nullptr) {
TRACE(Trace::Information, (_T("Observable supports Statecontrol")) );
if (_statecontrol != nullptr) {
TRACE(Trace::Information, (_T("Observable supports Statecontrol")));
_statecontrol->AddRef();
}
}
Expand All @@ -425,7 +425,7 @@ namespace Plugin {
{
Base::Enable();

if(_statecontrol != nullptr) {
if (_statecontrol != nullptr) {

_statecontrol->Register(this);
}
Expand All @@ -439,9 +439,9 @@ namespace Plugin {

void StateChange(const PluginHost::IStateControl::state state) override
{
if( state == PluginHost::IStateControl::state::RESUMED ) {
if (state == PluginHost::IStateControl::state::RESUMED) {
Logger().Resumed();
} else if( state == PluginHost::IStateControl::state::SUSPENDED ) {
} else if (state == PluginHost::IStateControl::state::SUSPENDED) {
Logger().Suspended();
}
}
Expand All @@ -453,7 +453,7 @@ namespace Plugin {
private:
void Cleanup()
{
if(_statecontrol != nullptr) {
if (_statecontrol != nullptr) {
_statecontrol->Unregister(this);
_statecontrol->Release();
_statecontrol = nullptr;
Expand Down Expand Up @@ -520,7 +520,7 @@ namespace Plugin {

void LoadFinished(const string& URL) override
{
if( URL != IBrowserMetricsLogger::startURL ) {
if (URL != IBrowserMetricsLogger::startURL) {
++_nbrloaded;
}
Logger().LoadFinished(URL, 0, true, _nbrloaded, 0);
Expand Down Expand Up @@ -601,14 +601,14 @@ namespace Plugin {

void LoadFinished(const string& URL, const int32_t httpstatus) override
{
if( URL != IBrowserMetricsLogger::startURL ) {
if (URL != IBrowserMetricsLogger::startURL) {
++_nbrloadedsuccess;
}
Logger().LoadFinished(URL, httpstatus, true, _nbrloadedsuccess, _nbrloadedfailed);
}
void LoadFailed(const string& URL) override
{
if( URL != IBrowserMetricsLogger::startURL ) {
if (URL != IBrowserMetricsLogger::startURL) {
++_nbrloadedfailed;
}
Logger().LoadFinished(URL, 0, false, _nbrloadedsuccess, _nbrloadedfailed);
Expand Down
55 changes: 30 additions & 25 deletions PerformanceMetrics/SyslogOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {

URLLoadedMetrics& operator=(const URLLoadedMetrics& copy)
{
if( this != &copy ) {
if (this != &copy) {
_total = copy._total;
_free = copy._free;
_swapped = copy._swapped;
Expand Down Expand Up @@ -277,14 +277,14 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {
_service = &service;
_service->AddRef();
_memory = service.QueryInterface<Exchange::IMemory>();
if( _memory != nullptr ) {
if (_memory != nullptr) {
Exchange::IMemoryExtended* extended = _memory->QueryInterface<Exchange::IMemoryExtended>();
if( extended != nullptr ) {
if (extended != nullptr) {
Exchange::IMemoryExtended::IStringIterator* iterator = nullptr;
if( ( extended->Processes(iterator) == Core::ERROR_NONE ) && ( iterator != nullptr ) ) {
if ((extended->Processes(iterator) == Core::ERROR_NONE ) && ( iterator != nullptr)) {
string processname;
while( iterator->Next(processname) == true ) {
if( processname == webProcessName ) {
while (iterator->Next(processname) == true) {
if (processname == webProcessName) {
VARIABLE_IS_NOT_USED uint32_t result = extended->Process(webProcessName, _processmemory);
ASSERT( ( result == Core::ERROR_NONE ) && (_processmemory != nullptr ));
break;
Expand All @@ -299,15 +299,15 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {

void Disable() override
{
if(_service != nullptr) {
if (_service != nullptr) {
_service->Release();
_service = nullptr;
}
if(_memory != nullptr) {
if (_memory != nullptr) {
_memory->Release();
_memory = nullptr;
}
if(_processmemory != nullptr) {
if (_processmemory != nullptr) {
_processmemory->Release();
_processmemory = nullptr;
}
Expand All @@ -322,8 +322,7 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {
void Deactivated(const uint32_t) override
{
PluginHost::IShell::reason reason = _service->Reason();
if(reason == PluginHost::IShell::FAILURE || reason == PluginHost::IShell::MEMORY_EXCEEDED)
{
if (reason == PluginHost::IShell::FAILURE || reason == PluginHost::IShell::MEMORY_EXCEEDED) {
SYSLOG(Logging::Notification, (_T("Browser::Deactivated ( \"URL\": %s , \"Reason\": %d )"), getHostName(_lastURL).c_str(), reason));
string eventName("BrowserDeactivation_accum");
string eventValue;
Expand All @@ -347,29 +346,33 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {

string getHostName(string _URL){
std::size_t startIdx = _URL.find("://");
if(startIdx == std::string::npos)
if (startIdx == std::string::npos) {
return _URL;
}
else {
startIdx += 3; // skip "://"
size_t endIdx = _URL.find("/",startIdx);
if(endIdx == std::string::npos)
if (endIdx == std::string::npos) {
return _URL.substr(startIdx);
else
}
else {
return _URL.substr(startIdx, endIdx - startIdx);
}
}
}

void LoadFinished(const string& URL, const int32_t, const bool success, const uint32_t totalsuccess, const uint32_t totalfailed) override
{
if( URL != startURL ) {
if (URL != startURL) {
_adminLock.Lock();
URLLoadedMetrics metrics(_urloadmetrics);
_adminLock.Unlock();

uint64_t urllaunchtime_ms = ( ( Core::Time::Now().Ticks() - metrics.StartLoad() ) / Core::Time::TicksPerMillisecond);

if(strcmp(getHostName(URL).c_str(), _lastLoggedApp.c_str()))
_didLogLaunchMetrics = false;
if (strcmp(getHostName(URL).c_str(), _lastLoggedApp.c_str())) {
_didLogLaunchMetrics = false;
}

OutputLoadFinishedMetrics(metrics, getHostName(URL), urllaunchtime_ms, success, totalsuccess + totalfailed);

Expand All @@ -379,7 +382,7 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {

void URLChange(const string& URL, const bool) override
{
if( URL != startURL ) {
if (URL != startURL) {
URLLoadedMetrics metrics;
Core::SystemInfo::MemorySnapshot snapshot = Core::SystemInfo::Instance().TakeMemorySnapshot();
metrics.Total(snapshot.Total());
Expand All @@ -391,13 +394,13 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {
metrics.AverageLoad()[2] = Core::SystemInfo::Instance().GetCpuLoadAvg()[2];

uint64_t resident = 0;
if( _processmemory != nullptr ) {
if (_processmemory != nullptr) {
resident = _processmemory->Resident();
uint32_t pid = _processmemory->Identifier();
if( pid != 0 ) {
if (pid != 0) {
metrics.StatmLine(GetProcessStatmLine(pid));
}
} else if ( _memory != nullptr ) {
} else if (_memory != nullptr) {
resident = _memory->Resident();
}

Expand All @@ -407,8 +410,9 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {

uint64_t timeLaunched = 0;
timeLaunched = (Core::Time::Now().Ticks() - _timePluginStart) / Core::Time::MicroSecondsPerSecond;
if(timeLaunched < 2)
if (timeLaunched < 2) {
metrics.SetColdLaunch(true);
}

_adminLock.Lock();
_urloadmetrics = std::move(metrics);
Expand All @@ -430,8 +434,9 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {
const bool success,
const uint32_t totalloaded)
{
if(_didLogLaunchMetrics)
if (_didLogLaunchMetrics) {
return;
}

MetricsAsJson output;

Expand All @@ -453,7 +458,7 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {
output.ProcessRSS = urloadedmetrics.RSSMemProcess();

uint32_t pid = 0;
if( _processmemory != nullptr ) {
if (_processmemory != nullptr) {
pid = _processmemory->Identifier();
}
output.ProcessPID = pid;
Expand Down Expand Up @@ -481,7 +486,7 @@ class SysLogOuput : public PerformanceMetrics::IBrowserMetricsLogger {

Utils::Telemetry::sendMessage((char *)eventName.c_str(), (char *)eventValue.c_str());

SYSLOG(Logging::Notification, (_T( "%s Launch Metrics: %s "), _callsign.c_str(), outputstring.c_str()));
SYSLOG(Logging::Notification, (_T( "%s Launch Metrics: %s "), _callsign.c_str(), outputstring.c_str()));
_didLogLaunchMetrics = true;
_lastLoggedApp = URL;
}
Expand Down
Loading

0 comments on commit 2f63032

Please sign in to comment.