kazuyukitanimura commented on code in PR #1369: URL: https://github.com/apache/datafusion-comet/pull/1369#discussion_r1945320310
########## native/core/src/execution/fair_memory_pool.rs: ########## @@ -0,0 +1,159 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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 std::{ + fmt::{Debug, Formatter, Result as FmtResult}, + sync::Arc, +}; + +use jni::objects::GlobalRef; + +use crate::{ + errors::CometResult, + jvm_bridge::{jni_call, JVMClasses}, +}; +use datafusion::{ + common::{resources_datafusion_err, DataFusionError}, + execution::memory_pool::{MemoryPool, MemoryReservation}, +}; +use datafusion_execution::memory_pool::MemoryConsumer; +use parking_lot::Mutex; + +/// A DataFusion fair `MemoryPool` implementation for Comet. Internally this is +/// implemented via delegating calls to [`crate::jvm_bridge::CometTaskMemoryManager`]. +pub struct CometFairMemoryPool { + task_memory_manager_handle: Arc<GlobalRef>, + pool_size: usize, + state: Mutex<CometFairPoolState>, +} + +struct CometFairPoolState { + used: usize, + num: usize, +} + +impl Debug for CometFairMemoryPool { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let state = self.state.lock(); + f.debug_struct("CometFairMemoryPool") + .field("pool_size", &self.pool_size) + .field("used", &state.used) + .field("num", &state.num) + .finish() + } +} + +impl CometFairMemoryPool { + pub fn new( + task_memory_manager_handle: Arc<GlobalRef>, + pool_size: usize, + ) -> CometFairMemoryPool { + Self { + task_memory_manager_handle, + pool_size, + state: Mutex::new(CometFairPoolState { used: 0, num: 0 }), + } + } + + fn acquire(&self, additional: usize) -> CometResult<i64> { + let mut env = JVMClasses::get_env()?; + let handle = self.task_memory_manager_handle.as_obj(); + unsafe { + jni_call!(&mut env, + comet_task_memory_manager(handle).acquire_memory(additional as i64) -> i64) + } + } + + fn release(&self, size: usize) -> CometResult<()> { + let mut env = JVMClasses::get_env()?; + let handle = self.task_memory_manager_handle.as_obj(); + unsafe { + jni_call!(&mut env, comet_task_memory_manager(handle).release_memory(size as i64) -> ()) + } + } +} + +unsafe impl Send for CometFairMemoryPool {} +unsafe impl Sync for CometFairMemoryPool {} + +impl MemoryPool for CometFairMemoryPool { + fn register(&self, _: &MemoryConsumer) { + let mut state = self.state.lock(); + state.num = state.num.checked_add(1).unwrap(); Review Comment: This won't happen in reality, `num` is `usize`. I wouldn't worry about it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org