Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable passing a percentage as a value for --cores #3357

Merged
merged 10 commits into from
Jan 17, 2023
34 changes: 32 additions & 2 deletions Source/DafnyCore/Options/BoogieOptionBag.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Linq;
using System.Transactions;
using Microsoft.Boogie;

namespace Microsoft.Dafny;
Expand All @@ -17,8 +19,34 @@ public static class BoogieOptionBag {
ArgumentHelpName = "arguments",
};

public static readonly Option<uint> Cores = new("--cores", () => 0,
"Run the Dafny CLI using <n> cores. Specifying 0 will use half of the available processor cores.") {
public static readonly Option<uint> Cores = new("--cores", result => {
if (result.Tokens.Count != 1) {
result.ErrorMessage = $"Expected only one value for option {Cores.Name}";
return 1;
}

var value = result.Tokens[0].Value;
if (value.EndsWith('%')) {
if (double.TryParse(value.Substring(0, value.Length - 1), out var percentage)) {
return Math.Max(1U, (uint)(percentage / 100.0 * Environment.ProcessorCount));
}

result.ErrorMessage = $"Could not parse percentage {value}";
return 1;
}

if (uint.TryParse(value, out var number)) {
if (number > 0) {
return number;
}

result.ErrorMessage = $"Number of cores to use must be greater than 0";
return 1;
}
result.ErrorMessage = $"Could not parse number {value}";
return 1;
}, true,
"Run the Dafny CLI using <n> cores, or using <XX%> of the machine's logical cores.") {
keyboardDrummer marked this conversation as resolved.
Show resolved Hide resolved
ArgumentHelpName = "count"
};

Expand All @@ -33,6 +61,8 @@ public static class BoogieOptionBag {
};

static BoogieOptionBag() {
Cores.SetDefaultValue((Environment.ProcessorCount + 1) / 2);

DafnyOptions.RegisterLegacyBinding(BoogieFilter, (o, f) => o.ProcsToCheck.AddRange(f));
DafnyOptions.RegisterLegacyBinding(BoogieArguments, (o, boogieOptions) => {

Expand Down
8 changes: 7 additions & 1 deletion Source/XUnitExtensions/Lit/LitCommandWithRedirection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public static LitCommandWithRedirection Parse(string[] tokens, LitTestConfigurat
errorFile = config.ApplySubstitutions(argumentsList[redirectErrorIndex + 1]).Single();
argumentsList.RemoveRange(redirectErrorIndex, 2);
}
var redirectErrorAppendIndex = argumentsList.IndexOf("2>>");
if (redirectErrorAppendIndex >= 0) {
errorFile = config.ApplySubstitutions(argumentsList[redirectErrorAppendIndex + 1]).Single();
Copy link
Member

@MikaelMayer MikaelMayer Jan 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this code part, can you please explain it to me? I get that you want to redirect the standard error to the file as well, but I'm surprised that you have to introduce a new rule for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get that you want to redirect the standard error to the file as well, but I'm surprised that you have to introduce a new rule for that.

How would it know what 2>> means without a new rule? It's a from scratch parser for lit commands so it doesn't understand anything by default. Maybe you could make it smart so if it understand 2 and >> separately it can also combine them, but it can't do that right now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would it know what 2>> means without a new rule? I

I mean, I'm surprised we hadn't had the need tor redirect error output before this PR, that's what I meant.

My comprehension problem was simpler. I just looked at the code above to see what it was doing, and I understand you are generalizing "2>", which I remembered was used in test cases. So that's good for me.

appendOutput = true;
argumentsList.RemoveRange(redirectErrorIndex, 2);
}

var arguments = argumentsList.SelectMany(config.ApplySubstitutions);

Expand Down Expand Up @@ -95,4 +101,4 @@ public override string ToString() {
return builder.ToString();
}
}
}
}
8 changes: 7 additions & 1 deletion Test/cli/zeroCores.dfy
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
// RUN: ! %baredafny verify --use-basename-for-filename --cores=0 "%s" > "%t"
// RUN: ! %baredafny verify --use-basename-for-filename --cores=0 "%s" 2> "%t"
// RUN: ! %baredafny verify --use-basename-for-filename --cores=earga "%s" 2>> "%t"
// RUN: ! %baredafny verify --use-basename-for-filename --cores=earga% "%s" 2>> "%t"
// RUN: ! %baredafny verify --use-basename-for-filename "%s" &> "%t"
// RUN: ! %baredafny verify --use-basename-for-filename --cores=1 "%s" >> "%t"
// RUN: ! %baredafny verify --use-basename-for-filename --cores=0% "%s" >> "%t"
// RUN: ! %baredafny verify --use-basename-for-filename --cores=50% "%s" >> "%t"
// RUN: %diff "%s.expect" "%t"

method Foo() ensures false {
Expand Down
18 changes: 16 additions & 2 deletions Test/cli/zeroCores.dfy.expect
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
zeroCores.dfy(4,27): Error: A postcondition might not hold on this return path.
zeroCores.dfy(4,21): Related location: This is the postcondition that might not hold.
Number of cores to use must be greater than 0

Could not parse number earga

Could not parse percentage earga%

zeroCores.dfy(9,27): Error: A postcondition might not hold on this return path.
zeroCores.dfy(9,21): Related location: This is the postcondition that might not hold.

Dafny program verifier finished with 0 verified, 1 error
zeroCores.dfy(9,27): Error: A postcondition might not hold on this return path.
zeroCores.dfy(9,21): Related location: This is the postcondition that might not hold.

Dafny program verifier finished with 0 verified, 1 error
zeroCores.dfy(9,27): Error: A postcondition might not hold on this return path.
zeroCores.dfy(9,21): Related location: This is the postcondition that might not hold.

Dafny program verifier finished with 0 verified, 1 error