mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
- Introduce ExecutionTimer/ExecutionTimerConfig to allow limiting evaluating time. - To amortize time checking costs, checking interval can be configured via the notion of work units - A global fallback time limit can be set to universally limit all evaluation in addition to engine level limit setting. - Implement limnits in interpreter and RVM. In RVM, also handle suspend/resume so that time during pause is not counted. - Add engine-level APIs to set/clear per-engine timer configuration and apply global fallback defaults. - Surface execution-time limits through FFI and C# bindings - Add C# tests and example usage to validate engine overrides, global fallback behavior, and compiled policy enforcement. - Expand docs for execution-time limit - Add interpreter YAML cases and VM unit tests for time-limit behavior and deterministic time sources. Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
63 lines
2.2 KiB
C#
63 lines
2.2 KiB
C#
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
using System;
|
|
|
|
namespace Regorus
|
|
{
|
|
/// <summary>
|
|
/// Managed representation of the execution timer configuration used by the engine.
|
|
/// </summary>
|
|
public readonly struct ExecutionTimerConfig
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="ExecutionTimerConfig"/> struct.
|
|
/// </summary>
|
|
/// <param name="limit">Maximum wall-clock duration allowed for evaluation. Must be non-negative.</param>
|
|
/// <param name="checkInterval">Number of work units between timer checks. Must be non-zero.</param>
|
|
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="limit"/> is negative or <paramref name="checkInterval"/> is zero.</exception>
|
|
public ExecutionTimerConfig(TimeSpan limit, uint checkInterval)
|
|
{
|
|
if (limit < TimeSpan.Zero)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(limit), "Execution timer limit must be non-negative.");
|
|
}
|
|
|
|
if (checkInterval == 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(checkInterval), "Execution timer check interval must be non-zero.");
|
|
}
|
|
|
|
Limit = limit;
|
|
CheckInterval = checkInterval;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Maximum wall-clock duration allowed for an evaluation.
|
|
/// </summary>
|
|
public TimeSpan Limit { get; }
|
|
|
|
/// <summary>
|
|
/// Number of work units between timer checks.
|
|
/// </summary>
|
|
public uint CheckInterval { get; }
|
|
|
|
internal Regorus.Internal.RegorusExecutionTimerConfig ToNative()
|
|
{
|
|
if (Limit < TimeSpan.Zero)
|
|
{
|
|
throw new InvalidOperationException("Execution timer limit must be non-negative.");
|
|
}
|
|
|
|
ulong ticks = checked((ulong)Limit.Ticks);
|
|
ulong limitNanoseconds = checked(ticks * 100UL);
|
|
|
|
return new Regorus.Internal.RegorusExecutionTimerConfig
|
|
{
|
|
limit_ns = limitNanoseconds,
|
|
check_interval = CheckInterval,
|
|
};
|
|
}
|
|
}
|
|
}
|