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
@@ -30,3 +30,33 @@ Once the workflow run completes, the generated Nuget can be downloaded by follow
|
||||
## Local
|
||||
|
||||
TODO
|
||||
|
||||
## Memory Usage Safeguards
|
||||
|
||||
The C# bindings expose allocator-backed memory tracking utilities via the static `Regorus.MemoryLimits` helper. Typical usage:
|
||||
|
||||
```csharp
|
||||
// Restrict total allocations to 128 MiB for the process
|
||||
Regorus.MemoryLimits.SetGlobalMemoryLimit(128 * 1024 * 1024);
|
||||
|
||||
// Optional: tune how frequently each thread flushes its allocation counters
|
||||
Regorus.MemoryLimits.SetThreadFlushThresholdOverride(256 * 1024);
|
||||
|
||||
// Engine operations throw InvalidOperationException with the allocator message if the budget is exceeded
|
||||
using var engine = new Regorus.Engine();
|
||||
var veryLargeJson = new string('x', 128 * 1024);
|
||||
try
|
||||
{
|
||||
engine.SetInputJson(veryLargeJson);
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
Console.WriteLine($"Allocator reported: {ex.Message}");
|
||||
}
|
||||
|
||||
// Restore defaults once done
|
||||
Regorus.MemoryLimits.SetGlobalMemoryLimit(null);
|
||||
Regorus.MemoryLimits.SetThreadFlushThresholdOverride(null);
|
||||
```
|
||||
|
||||
See `bindings/csharp/Regorus.Tests/RegorusTests.cs` for scenario coverage and `bindings/csharp/TargetExampleApp/Program.cs` for end-to-end usage.
|
||||
|
||||
Reference in New Issue
Block a user