mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
feat(memory): Allocator-backed global memory limits (#544)
Policy evaluation at scale needs to be able to set memory limits so that a bad policy does not hog memory or to ensure that policy evaluation itself does not use too much memory which could cause other components to suffer. This PR introduces capability to set and enforce global memory limits. It also lays the groundwork for enabling per evaluation limits in future. Once a global memory limit is set, Regorus maintains per thread counters to track memory activity (allocation, deallocation) of a thread. These counters are periodically flushed to global memory counters. Per thread counters avoid the contention that updating global counters on each alloc/free would cause. Policy evaluation periodically checks these counters and raises errors if allocated memory has exceeded the configured limit. Currently memory limit capability is exposed only to FFI and C#. Also update mimalloc to v2.2.6 Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
GitHub
parent
80686d6ed1
commit
fd59bb5a91
@@ -8,7 +8,7 @@ use crate::interpreter::*;
|
||||
use crate::lexer::*;
|
||||
use crate::parser::*;
|
||||
use crate::scheduler::*;
|
||||
use crate::utils::gather_functions;
|
||||
use crate::utils::{gather_functions, limits};
|
||||
use crate::value::*;
|
||||
use crate::*;
|
||||
use crate::{Extension, QueryResults};
|
||||
@@ -131,6 +131,7 @@ impl Engine {
|
||||
let source = Source::from_contents(path, rego)?;
|
||||
let mut parser = self.make_parser(&source)?;
|
||||
let module = Ref::new(parser.parse()?);
|
||||
limits::enforce_memory_limit().map_err(|err| anyhow!(err))?;
|
||||
Rc::make_mut(&mut self.modules).push(module.clone());
|
||||
// if policies change, interpreter needs to be prepared again
|
||||
self.prepared = false;
|
||||
@@ -164,6 +165,7 @@ impl Engine {
|
||||
let source = Source::from_file(path)?;
|
||||
let mut parser = self.make_parser(&source)?;
|
||||
let module = Ref::new(parser.parse()?);
|
||||
limits::enforce_memory_limit().map_err(|err| anyhow!(err))?;
|
||||
Rc::make_mut(&mut self.modules).push(module.clone());
|
||||
// if policies change, interpreter needs to be prepared again
|
||||
self.prepared = false;
|
||||
@@ -942,6 +944,9 @@ impl Engine {
|
||||
|
||||
#[doc(hidden)]
|
||||
fn prepare_for_eval(&mut self, enable_tracing: bool, for_target: bool) -> Result<()> {
|
||||
// Fail fast if the engine already exceeds the global memory limit before evaluation work.
|
||||
limits::enforce_memory_limit().map_err(|err| anyhow!(err))?;
|
||||
|
||||
self.interpreter.set_traces(enable_tracing);
|
||||
|
||||
// if the data/policies have changed or the interpreter has never been prepared
|
||||
|
||||
Reference in New Issue
Block a user