Skip to content

Commit

Permalink
Use hand written custom stack size pool task scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
keyboardDrummer committed Mar 30, 2023
1 parent 9e8fc99 commit 1aab6f6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 549 deletions.
73 changes: 73 additions & 0 deletions Source/ExecutionEngine/CustomStackSizePoolTaskScheduler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Boogie;

public class CustomStackSizePoolTaskScheduler : TaskScheduler, IDisposable
{
private readonly int threadCount;
private readonly ConcurrentQueue<Task> queue = new();
private readonly SemaphoreSlim isWorkAvailable = new(0);
private readonly Thread[] threads;

public static CustomStackSizePoolTaskScheduler Create(int stackSize, int threadCount)
{
return new CustomStackSizePoolTaskScheduler(stackSize, threadCount);
}

private CustomStackSizePoolTaskScheduler(int stackSize, int threadCount)
{
this.threadCount = threadCount;

threads = new Thread[this.threadCount];
for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(WorkLoop, stackSize) { IsBackground = true };
threads[i].Start();
}
}

protected override void QueueTask(Task task)
{
queue.Enqueue(task);
isWorkAvailable.Release(1);
}

protected override bool TryExecuteTaskInline(Task task, bool taskWasPreviouslyQueued)
{
return TryExecuteTask(task);
}

public override int MaximumConcurrencyLevel => threadCount;

protected override IEnumerable<Task> GetScheduledTasks()
{
return queue;
}

private void WorkLoop()
{
while (true)
{
var task = BlockUntilTaskIsAvailable();
TryExecuteTask(task);
}
}

private Task BlockUntilTaskIsAvailable()
{
isWorkAvailable.Wait();
queue.TryDequeue(out var task);
return task;
}

public void Dispose()
{
for (int i = 0; i < threads.Length; i++) {
threads[i].Join();
}
}
}
10 changes: 5 additions & 5 deletions Source/ExecutionEngine/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public ProcessedProgram(Program program) : this(program, (_, _, _) => { }) {

public class ExecutionEngine : IDisposable
{
private static readonly WorkStealingTaskScheduler LargeThreadScheduler = new(16 * 1024 * 1024);
private static readonly TaskFactory LargeThreadTaskFactory = new(
private static readonly CustomStackSizePoolTaskScheduler largeThreadScheduler = CustomStackSizePoolTaskScheduler.Create(16 * 1024 * 1024, Environment.ProcessorCount);
private static readonly TaskFactory largeThreadTaskFactory = new(
CancellationToken.None, TaskCreationOptions.DenyChildAttach,
TaskContinuationOptions.None, LargeThreadScheduler);
TaskContinuationOptions.None, largeThreadScheduler);

static int autoRequestIdCount;

static readonly string AutoRequestIdPrefix = "auto_request_id_";
private const string AutoRequestIdPrefix = "auto_request_id_";

public static string FreshRequestId() {
var id = Interlocked.Increment(ref autoRequestIdCount);
Expand Down Expand Up @@ -874,7 +874,7 @@ private IObservable<IVerificationStatus> VerifyImplementationWithoutCaching(Proc
var verificationResult = new VerificationResult(impl, programId);

var batchCompleted = new Subject<(Split split, VCResult vcResult)>();
var completeVerification = LargeThreadTaskFactory.StartNew(async () =>
var completeVerification = largeThreadTaskFactory.StartNew(async () =>
{
var vcgen = new VCGen(processedProgram.Program, checkerPool);
vcgen.CachingActionCounts = stats.CachingActionCounts;
Expand Down
219 changes: 0 additions & 219 deletions Source/ExecutionEngine/WorkStealingQueue.cs

This file was deleted.

Loading

0 comments on commit 1aab6f6

Please sign in to comment.