Skip to content

Commit 786b128

Browse files
pzuevuniquelogin
authored andcommitted
thread cpu time
1 parent 717a794 commit 786b128

File tree

5 files changed

+61
-20
lines changed

5 files changed

+61
-20
lines changed

ydb/core/kqp/tools/combiner_perf/printout.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include <sys/resource.h>
44

5+
#if defined (_darwin_)
6+
#include <mach/mach.h>
7+
#endif
8+
59
long GetMaxRSS()
610
{
711
rusage usage;
@@ -16,7 +20,7 @@ long GetMaxRSSDelta(const long prevMaxRss)
1620
long maxRss = GetMaxRSS();
1721

1822
if (maxRss <= 0 || prevMaxRss <= 0 || maxRss < prevMaxRss) {
19-
ythrow yexception() << "Bad maxRSS measurement, before: " << prevMaxRss << ", after: " << maxRss << Endl;
23+
ythrow yexception() << "Bad maxRSS measurement, before: " << prevMaxRss << ", after: " << maxRss << Endl;
2024
}
2125

2226
#if defined (_darwin_)
@@ -28,6 +32,39 @@ long GetMaxRSSDelta(const long prevMaxRss)
2832
return (maxRss - prevMaxRss) * factor;
2933
}
3034

35+
TDuration GetThreadCPUTime()
36+
{
37+
ui64 us = 0;
38+
39+
#if defined (_darwin_)
40+
thread_basic_info_data_t info = {};
41+
mach_msg_type_number_t info_count = THREAD_BASIC_INFO_COUNT;
42+
kern_return_t kern_err = thread_info(
43+
mach_thread_self(),
44+
THREAD_BASIC_INFO,
45+
(thread_info_t)&info,
46+
&info_count
47+
);
48+
if (kern_err == KERN_SUCCESS) {
49+
us = 1000000ULL * info.user_time.seconds + info.user_time.microseconds + 1000000ULL*info.system_time.seconds + info.system_time.microseconds;
50+
}
51+
#else
52+
us = ThreadCPUTime();
53+
#endif
54+
55+
if (!us) {
56+
ythrow yexception() << "Failed to obtain thread CPU time";
57+
}
58+
59+
return TDuration::MicroSeconds(us);
60+
}
61+
62+
TDuration GetThreadCPUTimeDelta(const TDuration startTime)
63+
{
64+
TDuration current = GetThreadCPUTime();
65+
return current - startTime;
66+
}
67+
3168
void MergeRunResults(const TRunResult& src, TRunResult& dst)
3269
{
3370
dst.ResultTime = Min(src.ResultTime, dst.ResultTime);

ydb/core/kqp/tools/combiner_perf/printout.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ class TTestResultCollector {
4040
};
4141

4242
// ru_maxrss from rusage, converted to bytes
43-
long GetMaxRSS();
43+
long Y_NO_INLINE GetMaxRSS();
4444

45-
long GetMaxRSSDelta(const long prevMaxRss);
45+
long Y_NO_INLINE GetMaxRSSDelta(const long prevMaxRss);
46+
47+
TDuration Y_NO_INLINE GetThreadCPUTime();
48+
49+
TDuration Y_NO_INLINE GetThreadCPUTimeDelta(const TDuration startTime);

ydb/core/kqp/tools/combiner_perf/simple.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,22 @@ TRunResult RunTestOverGraph(const TRunParams& params, const bool needsVerificati
6363
// Compute node implementation
6464

6565
Cerr << "Compute graph result" << Endl;
66-
const auto graphTimeStart = TInstant::Now();
66+
const auto graphTimeStart = GetThreadCPUTime();
6767
size_t lineCount = CountWideStreamOutputs<2>(computeGraphPtr->GetValue());
6868
Cerr << lineCount << Endl;
6969

70-
return TInstant::Now() - graphTimeStart;
70+
return GetThreadCPUTimeDelta(graphTimeStart);
7171
};
7272

7373
auto measureRefTime = [&](auto& computeGraphPtr, IDataSampler& sampler) {
7474
// Reference implementation (sum via an std::unordered_map)
7575

7676
Cerr << "Compute reference result" << Endl;
7777
auto referenceStream = sampler.MakeStream(computeGraphPtr->GetHolderFactory());
78-
const auto cppTimeStart = TInstant::Now();
78+
const auto cppTimeStart = GetThreadCPUTime();
7979
sampler.ComputeReferenceResult(*referenceStream);
8080

81-
return TInstant::Now() - cppTimeStart;
81+
return GetThreadCPUTimeDelta(cppTimeStart);
8282
};
8383

8484
auto graphRun1 = BuildGraph(setup, *sampler, params.WideCombinerMemLimit);
@@ -106,13 +106,13 @@ TRunResult RunTestOverGraph(const TRunParams& params, const bool needsVerificati
106106
// Measure the input stream run time
107107
Cerr << "Generator run" << Endl;
108108
const auto devnullStream = sampler->MakeStream(graphRun1->GetHolderFactory());
109-
const auto devnullStart = TInstant::Now();
109+
const auto devnullStart = GetThreadCPUTime();
110110
{
111111
NUdf::TUnboxedValue columns[2];
112112
while (devnullStream->WideFetch(columns, 2) == NUdf::EFetchStatus::Ok) {
113113
}
114114
}
115-
result.GeneratorTime = TInstant::Now() - devnullStart;
115+
result.GeneratorTime = GetThreadCPUTimeDelta(devnullStart);
116116

117117
if (params.TestMode == ETestMode::GeneratorOnly) {
118118
return result;

ydb/core/kqp/tools/combiner_perf/simple_block.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,33 +70,33 @@ TRunResult RunTestOverGraph(const TRunParams& params, const bool measureReferenc
7070
// Compute graph implementation
7171
auto stream = NUdf::TUnboxedValuePod(sampler->MakeStream(computeGraphPtr->GetContext()).Release());
7272
computeGraphPtr->GetEntryPoint(0, true)->SetValue(computeGraphPtr->GetContext(), std::move(stream));
73-
const auto graphStart = TInstant::Now();
73+
const auto graphStart = GetThreadCPUTime();
7474
resultList = computeGraphPtr->GetValue();
75-
return TInstant::Now() - graphStart;
75+
return GetThreadCPUTimeDelta(graphStart);
7676
};
7777

7878
auto measureRefTime = [&](auto& computeGraphPtr) {
7979
// Reference implementation (sum via an std::unordered_map)
80-
const auto cppStart = TInstant::Now();
80+
const auto cppStart = GetThreadCPUTime();
8181
sampler->ComputeReferenceResult(computeGraphPtr->GetContext());
82-
return TInstant::Now() - cppStart;
82+
return GetThreadCPUTimeDelta(cppStart);
8383
};
8484

8585
auto measureGeneratorTime = [&](auto& computeGraphPtr) {
8686
// Generator-only loop
8787
const auto devnullStreamPtr = sampler->MakeStream(computeGraphPtr->GetContext());
8888
auto& devnullStream = *devnullStreamPtr;
8989
size_t numBlocks = 0;
90-
const auto timeStart = TInstant::Now();
90+
const auto timeStart = GetThreadCPUTime();
9191
{
9292
NUdf::TUnboxedValue columns[3];
9393
while (devnullStream.WideFetch(columns, 3) == NUdf::EFetchStatus::Ok) {
9494
++numBlocks;
9595
}
9696
}
97-
auto timeEnd = TInstant::Now();
97+
auto duration = GetThreadCPUTimeDelta(timeStart);
9898
Cerr << "Blocks generated: " << numBlocks << Endl;
99-
return timeEnd - timeStart;
99+
return duration;
100100
};
101101

102102
const auto graph = setup.BuildGraph(pgmReturn, {streamCallable});

ydb/core/kqp/tools/combiner_perf/simple_last.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,22 @@ TRunResult RunTestOverGraph(const TRunParams& params, const bool needsVerificati
9090
// Compute node implementation
9191

9292
Cerr << "Compute graph result" << Endl;
93-
const auto graphTimeStart = TInstant::Now();
93+
const auto graphTimeStart = GetThreadCPUTime();
9494
size_t lineCount = CountWideStreamOutputs<2>(computeGraphPtr->GetValue());
9595
Cerr << lineCount << Endl;
9696

97-
return TInstant::Now() - graphTimeStart;
97+
return GetThreadCPUTimeDelta(graphTimeStart);
9898
};
9999

100100
auto measureRefTime = [&](auto& computeGraphPtr, IDataSampler& sampler) {
101101
// Reference implementation (sum via an std::unordered_map)
102102

103103
Cerr << "Compute reference result" << Endl;
104104
auto referenceStream = sampler.MakeStream(computeGraphPtr->GetHolderFactory());
105-
const auto cppTimeStart = TInstant::Now();
105+
const auto cppTimeStart = GetThreadCPUTime();
106106
sampler.ComputeReferenceResult(*referenceStream);
107107

108-
return TInstant::Now() - cppTimeStart;
108+
return GetThreadCPUTimeDelta(cppTimeStart);
109109
};
110110

111111
auto graphRun1 = BuildGraph(setup, spillerFactory, *sampler);

0 commit comments

Comments
 (0)