Skip to content

Commit eda1a41

Browse files
authored
remove dependency to Mono.Posix.NETStandard to unblock WASM benchmarks runs (#2154)
* remove dependency to Mono.Posix.NETStandard to unblock WASM benchmark runs * Unix samples fix * increase the default timeout to 5 minutes
1 parent b758d20 commit eda1a41

File tree

8 files changed

+46
-18
lines changed

8 files changed

+46
-18
lines changed

samples/BenchmarkDotNet.Samples/IntroLargeAddressAware.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.InteropServices;
23
using BenchmarkDotNet.Attributes;
34
using BenchmarkDotNet.Configs;
45
using BenchmarkDotNet.Environments;
@@ -17,7 +18,7 @@ public Config()
1718
AddJob(Job.Default
1819
.WithRuntime(ClrRuntime.Net462)
1920
.WithPlatform(Platform.X86)
20-
.WithLargeAddressAware()
21+
.WithLargeAddressAware(value: RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
2122
.WithId("Framework"));
2223
}
2324
}

src/BenchmarkDotNet/Attributes/PerfCollectProfilerAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace BenchmarkDotNet.Attributes
88
public class PerfCollectProfilerAttribute : Attribute, IConfigSource
99
{
1010
/// <param name="performExtraBenchmarksRun">When set to true, benchmarks will be executed one more time with the profiler attached. If set to false, there will be no extra run but the results will contain overhead. False by default.</param>
11-
/// <param name="timeoutInSeconds">How long should we wait for the perfcollect script to finish processing the trace. 120s by default.</param>
12-
public PerfCollectProfilerAttribute(bool performExtraBenchmarksRun = false, int timeoutInSeconds = 120)
11+
/// <param name="timeoutInSeconds">How long should we wait for the perfcollect script to finish processing the trace. 300s by default.</param>
12+
public PerfCollectProfilerAttribute(bool performExtraBenchmarksRun = false, int timeoutInSeconds = 300)
1313
{
1414
Config = ManualConfig.CreateEmpty().AddDiagnoser(new PerfCollectProfiler(new PerfCollectProfilerConfig(performExtraBenchmarksRun, timeoutInSeconds)));
1515
}

src/BenchmarkDotNet/BenchmarkDotNet.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<PackageReference Include="Iced" Version="1.17.0" />
2222
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="2.2.332302" />
2323
<PackageReference Include="Perfolizer" Version="0.2.1" />
24-
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
2524
<PackageReference Include="System.Management" Version="6.0.0" />
2625
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
2726
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />

src/BenchmarkDotNet/Diagnosers/PerfCollectProfiler.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6+
using System.Runtime.InteropServices;
67
using BenchmarkDotNet.Analysers;
78
using BenchmarkDotNet.Engines;
89
using BenchmarkDotNet.Exporters;
@@ -20,7 +21,7 @@
2021
using BenchmarkDotNet.Toolchains.NativeAot;
2122
using BenchmarkDotNet.Validators;
2223
using JetBrains.Annotations;
23-
using Mono.Unix.Native;
24+
using RuntimeInformation = BenchmarkDotNet.Portability.RuntimeInformation;
2425

2526
namespace BenchmarkDotNet.Diagnosers
2627
{
@@ -58,7 +59,7 @@ public IEnumerable<ValidationError> Validate(ValidationParameters validationPara
5859
yield break;
5960
}
6061

61-
if (Syscall.getuid() != 0)
62+
if (libc.getuid() != 0)
6263
{
6364
yield return new ValidationError(true, "You must run as root to use PerfCollectProfiler.");
6465
yield break;
@@ -102,9 +103,10 @@ private bool TryInstallPerfCollect(ValidationParameters validationParameters)
102103
string script = ResourceHelper.LoadTemplate(perfCollectFile.Name);
103104
File.WriteAllText(perfCollectFile.FullName, script);
104105

105-
if (Syscall.chmod(perfCollectFile.FullName, FilePermissions.S_IXUSR) != 0)
106+
if (libc.chmod(perfCollectFile.FullName, libc.FilePermissions.S_IXUSR) != 0)
106107
{
107-
logger.WriteError($"Unable to make perfcollect script an executable, the last error was: {Syscall.GetLastError()}");
108+
int lastError = Marshal.GetLastWin32Error();
109+
logger.WriteError($"Unable to make perfcollect script an executable, the last error was: {lastError}");
108110
}
109111
else
110112
{
@@ -158,9 +160,9 @@ private void StopCollection(DiagnoserActionParameters parameters)
158160
{
159161
if (!perfCollectProcess.HasExited)
160162
{
161-
if (Syscall.kill(perfCollectProcess.Id, Signum.SIGINT) != 0)
163+
if (libc.kill(perfCollectProcess.Id, libc.Signals.SIGINT) != 0)
162164
{
163-
var lastError = Stdlib.GetLastError();
165+
int lastError = Marshal.GetLastWin32Error();
164166
logger.WriteLineError($"kill(perfcollect, SIGINT) failed with {lastError}");
165167
}
166168

src/BenchmarkDotNet/Diagnosers/PerfCollectProfilerConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace BenchmarkDotNet.Diagnosers
55
public class PerfCollectProfilerConfig
66
{
77
/// <param name="performExtraBenchmarksRun">When set to true, benchmarks will be executed one more time with the profiler attached. If set to false, there will be no extra run but the results will contain overhead. False by default.</param>
8-
/// <param name="timeoutInSeconds">How long should we wait for the perfcollect script to finish processing the trace. 120s by default.</param>
9-
public PerfCollectProfilerConfig(bool performExtraBenchmarksRun = false, int timeoutInSeconds = 120)
8+
/// <param name="timeoutInSeconds">How long should we wait for the perfcollect script to finish processing the trace. 300s by default.</param>
9+
public PerfCollectProfilerConfig(bool performExtraBenchmarksRun = false, int timeoutInSeconds = 300)
1010
{
1111
RunMode = performExtraBenchmarksRun ? RunMode.ExtraRun : RunMode.NoOverhead;
1212
Timeout = TimeSpan.FromSeconds(timeoutInSeconds);

src/BenchmarkDotNet/Jobs/EnvironmentMode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public bool LargeAddressAware
107107
get => LargeAddressAwareCharacteristic[this];
108108
set
109109
{
110-
if (!RuntimeInformation.IsWindows())
110+
if (value && !RuntimeInformation.IsWindows())
111111
{
112112
throw new NotSupportedException("LargeAddressAware is a Windows-specific concept.");
113113
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace BenchmarkDotNet.Portability
4+
{
5+
internal static class libc
6+
{
7+
[DllImport(nameof(libc))]
8+
internal static extern int getppid();
9+
10+
[DllImport(nameof(libc))]
11+
internal static extern uint getuid();
12+
13+
[DllImport(nameof(libc), SetLastError = true)]
14+
internal static extern int kill(int pid, int sig);
15+
16+
[DllImport(nameof(libc), SetLastError = true)]
17+
internal static extern int chmod(string path, uint mode);
18+
19+
internal static class Signals
20+
{
21+
internal const int SIGINT = 2;
22+
}
23+
24+
internal static class FilePermissions
25+
{
26+
internal const uint S_IXUSR = 0x40u;
27+
}
28+
}
29+
}

src/BenchmarkDotNet/Toolchains/DotNetCli/DotNetCliCommandExecutor.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
using System.ComponentModel;
44
using System.Diagnostics;
55
using System.Linq;
6-
using System.Runtime.InteropServices;
76
using System.Text;
87
using System.Text.RegularExpressions;
98
using BenchmarkDotNet.Extensions;
109
using BenchmarkDotNet.Helpers;
1110
using BenchmarkDotNet.Jobs;
1211
using BenchmarkDotNet.Loggers;
12+
using BenchmarkDotNet.Portability;
1313
using JetBrains.Annotations;
1414

1515
namespace BenchmarkDotNet.Toolchains.DotNetCli
@@ -140,7 +140,7 @@ private static string GetDefaultDotNetCliPath()
140140
if (!Portability.RuntimeInformation.IsLinux())
141141
return "dotnet";
142142

143-
using (var parentProcess = Process.GetProcessById(getppid()))
143+
using (var parentProcess = Process.GetProcessById(libc.getppid()))
144144
{
145145
string parentPath = parentProcess.MainModule?.FileName ?? string.Empty;
146146
// sth like /snap/dotnet-sdk/112/dotnet and we should use the exact path instead of just "dotnet"
@@ -154,9 +154,6 @@ private static string GetDefaultDotNetCliPath()
154154
}
155155
}
156156

157-
[DllImport("libc")]
158-
private static extern int getppid();
159-
160157
internal static string GetSdkPath(string cliPath)
161158
{
162159
DotNetCliCommand cliCommand = new (

0 commit comments

Comments
 (0)