aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/perf.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/lwtrace/perf.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/lwtrace/perf.cpp')
-rw-r--r--library/cpp/lwtrace/perf.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/library/cpp/lwtrace/perf.cpp b/library/cpp/lwtrace/perf.cpp
new file mode 100644
index 0000000000..03b68586ea
--- /dev/null
+++ b/library/cpp/lwtrace/perf.cpp
@@ -0,0 +1,84 @@
+#include "perf.h"
+
+#include "probes.h"
+
+#include <util/system/datetime.h>
+#include <util/system/hp_timer.h>
+
+namespace NLWTrace {
+ LWTRACE_USING(LWTRACE_INTERNAL_PROVIDER);
+
+ TCpuTracker::TCpuTracker()
+ : MinReportPeriod(NHPTimer::GetCyclesPerSecond())
+ {
+ ResetStats();
+ }
+
+ void TCpuTracker::Enter() {
+ LastTs = GetCycleCount();
+ }
+
+ void TCpuTracker::Exit(const TProbe* probe) {
+ ui64 exitTs = GetCycleCount();
+ if (exitTs < LastTs) {
+ return; // probably TSC was reset
+ }
+ ui64 cycles = exitTs - LastTs;
+ LastTs = exitTs;
+
+ AddStats(probe, cycles);
+ }
+
+ void TCpuTracker::AddStats(const TProbe* probe, ui64 cycles) {
+ if (MaxCycles < cycles) {
+ MaxProbe = probe;
+ MaxCycles = cycles;
+ }
+ if (MinCycles > cycles) {
+ MinCycles = cycles;
+ }
+ ProbeCycles += cycles;
+ Count++;
+
+ if (LastTs - LastReportTs > MinReportPeriod) {
+ Report();
+ }
+ }
+
+ void TCpuTracker::ResetStats() {
+ MaxCycles = 0;
+ MaxProbe = nullptr;
+ MinCycles = ui64(-1);
+ ProbeCycles = 0;
+ Count = 0;
+ }
+
+ void TCpuTracker::Report() {
+ if (!Reporting) {
+ Reporting = true;
+ ReportNotReentrant();
+ Reporting = false;
+ }
+ }
+
+ void TCpuTracker::ReportNotReentrant() {
+ if (LastReportTs && Count > 0 && LastTs > LastReportTs) {
+ ui64 reportPeriod = LastTs - LastReportTs;
+ double share = double(ProbeCycles) / reportPeriod;
+ double minMs = MilliSeconds(MinCycles);
+ double maxMs = MilliSeconds(MaxCycles);
+ double avgMs = MilliSeconds(ProbeCycles) / Count;
+ LastReportTs = LastTs;
+ ResetStats();
+ LWPROBE(PerfReport, share, minMs, maxMs, avgMs);
+ } else {
+ LastReportTs = LastTs;
+ ResetStats();
+ }
+ }
+
+ double TCpuTracker::MilliSeconds(ui64 cycles) {
+ return NHPTimer::GetSeconds(cycles) * 1000.0;
+ }
+
+}