forked from GreptimeTeam/greptimedb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: collecting memory usage during scan (GreptimeTeam#2353)
* chore: try custom metrics * chore: fix header * chore: minor change
- Loading branch information
1 parent
d27b872
commit b346eeb
Showing
3 changed files
with
80 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
// limitations under the License. | ||
|
||
pub mod adapter; | ||
mod metrics; | ||
pub mod numbers; | ||
pub mod scan; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2023 Greptime Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use datafusion::physical_plan::metrics::{ | ||
Count, ExecutionPlanMetricsSet, Gauge, MetricBuilder, Timestamp, | ||
}; | ||
|
||
/// This metrics struct is used to record and hold memory usage | ||
/// of result batch in [`crate::table::scan::StreamWithMetricWrapper`] | ||
/// during query execution, indicating size of the dataset. | ||
#[derive(Debug, Clone)] | ||
pub struct MemoryUsageMetrics { | ||
end_time: Timestamp, | ||
// used memory in bytes | ||
mem_used: Gauge, | ||
// number of rows in output | ||
output_rows: Count, | ||
} | ||
|
||
impl MemoryUsageMetrics { | ||
/// Create a new MemoryUsageMetrics structure, and set `start_time` to now | ||
pub fn new(metrics: &ExecutionPlanMetricsSet, partition: usize) -> Self { | ||
let start_time = MetricBuilder::new(metrics).start_timestamp(partition); | ||
start_time.record(); | ||
|
||
Self { | ||
end_time: MetricBuilder::new(metrics).end_timestamp(partition), | ||
mem_used: MetricBuilder::new(metrics).mem_used(partition), | ||
output_rows: MetricBuilder::new(metrics).output_rows(partition), | ||
} | ||
} | ||
|
||
pub fn record_mem_usage(&self, mem_used: usize) { | ||
self.mem_used.add(mem_used); | ||
} | ||
|
||
pub fn record_output(&self, num_rows: usize) { | ||
self.output_rows.add(num_rows); | ||
} | ||
|
||
/// Record the end time of the query | ||
pub fn try_done(&self) { | ||
if self.end_time.value().is_none() { | ||
self.end_time.record() | ||
} | ||
} | ||
} | ||
|
||
impl Drop for MemoryUsageMetrics { | ||
fn drop(&mut self) { | ||
self.try_done() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters