Skip to content

Commit d5df580

Browse files
FelixYBWmarin-ma
authored andcommitted
finish io
fix
1 parent 85c6ccb commit d5df580

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

velox/common/io/IoStatistics.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,34 @@ class IoStatistics {
127127
return queryThreadIoLatency_;
128128
}
129129

130+
void startIO() {
131+
std::lock_guard<std::mutex> lock(mtx_);
132+
if (active_requests_ == 0 && tracking_idle_) {
133+
auto now = std::chrono::steady_clock::now();
134+
queryThreadIoLatency_.increment(std::chrono::duration_cast<std::chrono::milliseconds>(now - idle_start_).count()*1000);
135+
tracking_idle_ = false;
136+
}
137+
++active_requests_;
138+
}
139+
140+
void endIO() {
141+
std::lock_guard<std::mutex> lock(mtx_);
142+
--active_requests_;
143+
if (active_requests_ == 0) {
144+
idle_start_ = std::chrono::steady_clock::now();
145+
tracking_idle_ = true;
146+
}
147+
}
148+
149+
void finish() {
150+
std::lock_guard<std::mutex> lock(mtx_);
151+
if (tracking_idle_) {
152+
auto now = std::chrono::steady_clock::now();
153+
queryThreadIoLatency_.increment(std::chrono::duration_cast<std::chrono::milliseconds>(now - idle_start_).count()*1000);
154+
idle_start_ = now;
155+
}
156+
}
157+
130158
void incOperationCounters(
131159
const std::string& operation,
132160
const uint64_t resourceThrottleCount,
@@ -173,6 +201,13 @@ class IoStatistics {
173201

174202
std::unordered_map<std::string, OperationCounters> operationStats_;
175203
mutable std::mutex operationStatsMutex_;
204+
205+
std::mutex mtx_;
206+
int active_requests_{0};
207+
std::chrono::steady_clock::time_point idle_start_;
208+
long idle_time_{0};
209+
bool tracking_idle_{false};
210+
176211
};
177212

178213
} // namespace facebook::velox::io

velox/connectors/hive/HiveDataSource.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ void HiveDataSource::addDynamicFilter(
421421
std::unordered_map<std::string, RuntimeMetric>
422422
HiveDataSource::getRuntimeStats() {
423423
auto res = runtimeStats_.toRuntimeMetricMap();
424+
ioStats_->finish();
424425
res.insert(
425426
{{"numPrefetch", RuntimeMetric(ioStats_->prefetch().count())},
426427
{"prefetchBytes",

velox/dwio/common/DirectBufferedInput.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,14 @@ std::vector<cache::CachePin> DirectCoalescedLoad::loadData(bool prefetch) {
318318
uint64_t usecs = 0;
319319
{
320320
MicrosecondTimer timer(&usecs);
321+
ioStats_->startIO();
321322
input_->read(buffers, requests_[0].region.offset, LogType::FILE);
323+
ioStats_->endIO();
322324
}
323325

324326
ioStats_->read().increment(size + overread);
325327
ioStats_->incRawBytesRead(size);
326328
ioStats_->incTotalScanTime(usecs * 1'000);
327-
ioStats_->queryThreadIoLatency().increment(usecs);
328329
ioStats_->incRawOverreadBytes(overread);
329330
if (prefetch) {
330331
ioStats_->prefetch().increment(size + overread);

velox/dwio/common/DirectInputStream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ void DirectInputStream::loadSync() {
150150
uint64_t usecs = 0;
151151
{
152152
MicrosecondTimer timer(&usecs);
153+
ioStats_->startIO();
153154
input_->read(ranges, loadedRegion_.offset, LogType::FILE);
155+
ioStats_->endIO();
154156
}
155157
ioStats_->read().increment(loadedRegion_.length);
156-
ioStats_->queryThreadIoLatency().increment(usecs);
157158
ioStats_->incTotalScanTime(usecs * 1'000);
158159
}
159160

@@ -173,7 +174,6 @@ void DirectInputStream::loadPosition() {
173174
loadedRegion_.offset = region_.offset;
174175
loadedRegion_.length = load->getData(region_.offset, data_, tinyData_);
175176
}
176-
ioStats_->queryThreadIoLatency().increment(loadUs);
177177
} else {
178178
// Standalone stream, not part of coalesced load.
179179
loadedRegion_.offset = 0;

0 commit comments

Comments
 (0)