// Copyright 2011-2019 Molecular Matters GmbH, all rights reserved. #include "LC_Telemetry.h" #include "LC_Logging.h" #include #include #include "Windows/WindowsHWrapper.h" #include "Windows/AllowWindowsPlatformAtomics.h" namespace { template static double ReadChrono(std::chrono::high_resolution_clock::time_point start) { const std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now(); const std::chrono::duration timeSpan = now - start; return timeSpan.count(); } static void Print(const char* name, std::chrono::high_resolution_clock::time_point start) { const double seconds = ReadChrono>(start); LC_LOG_TELEMETRY("Scope \"%s\" took %.3fs (%.3fms)", name, seconds, seconds*1000.0); } } telemetry::Scope::Scope(const char* name) : m_name(name) , m_start(std::chrono::high_resolution_clock::now()) , m_cs() { } telemetry::Scope::~Scope(void) { CriticalSection::ScopedLock lock(&m_cs); if (m_name) { Print(m_name, m_start); } } double telemetry::Scope::ReadSeconds(void) const { return ::ReadChrono>(m_start); } double telemetry::Scope::ReadMilliSeconds(void) const { return ::ReadChrono(m_start); } double telemetry::Scope::ReadMicroSeconds(void) const { return ::ReadChrono(m_start); } void telemetry::Scope::Restart(void) { m_start = std::chrono::high_resolution_clock::now(); } void telemetry::Scope::End(void) { CriticalSection::ScopedLock lock(&m_cs); Print(m_name, m_start); // do not print again when going out of scope m_name = nullptr; } telemetry::Accumulator::Accumulator(const char* name) : m_name(name) , m_current(0ull) , m_accumulated(0ull) { } void telemetry::Accumulator::Accumulate(uint64_t value) { ::InterlockedAdd64(reinterpret_cast(&m_current), static_cast(value)); ::InterlockedAdd64(reinterpret_cast(&m_accumulated), static_cast(value)); } void telemetry::Accumulator::ResetCurrent(void) { m_current = 0ull; } uint64_t telemetry::Accumulator::ReadCurrent(void) const { return m_current; } uint64_t telemetry::Accumulator::ReadAccumulated(void) const { return m_accumulated; } void telemetry::Accumulator::Print(void) { LC_LOG_TELEMETRY("Accumulator \"%s\"", m_name); LC_LOG_INDENT_TELEMETRY; LC_LOG_TELEMETRY("Current: %" PRId64 " (%.3f KB, %.3f MB)", m_current, m_current / 1024.0f, m_current / 1048576.0f); LC_LOG_TELEMETRY("Accumulated: %" PRId64 " (%.3f KB, %.3f MB)", m_accumulated, m_accumulated / 1024.0f, m_accumulated / 1048576.0f); } #include "Windows/HideWindowsPlatformAtomics.h"