Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ internal class MosCpuDetector : ICpuDetector
double maxFrequency = 0;
double nominalFrequency = 0;

using (var mosProcessor = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"))
try
{
using var mosProcessor = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (var moProcessor in mosProcessor.Get().Cast<ManagementObject>())
{
string? name = moProcessor[WmiCpuInfoKeyNames.Name]?.ToString();
Expand All @@ -45,6 +46,12 @@ internal class MosCpuDetector : ICpuDetector
}
}
}
catch
{
// System.TypeInitializationException: The type initializer for 'System.Management.ManagementPath' threw an exception.
// --->System.NotSupportedException: Built-in COM has been disabled via a feature switch.See https://aka.ms/dotnet-illink/com for more information.
return null;
}

string? processorName = processorModelNames.Count > 0 ? string.Join(", ", processorModelNames) : null;
Frequency? maxFrequencyActual = maxFrequency > 0 && processorsCount > 0
Expand All @@ -64,4 +71,4 @@ internal class MosCpuDetector : ICpuDetector
MaxFrequencyHz = maxFrequencyActual?.Hertz.RoundToLong()
};
}
}
}
5 changes: 5 additions & 0 deletions src/BenchmarkDotNet/Portability/RuntimeInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public static bool IsNativeAOT
&& IsAot
&& !IsWasm && !IsMono; // Wasm and MonoAOTLLVM are also AOT

// File-based apps contains specific RuntimeHostConfigurationOptions.
// https://github.com/dotnet/dotnet/blob/v10.0.302/src/sdk/documentation/general/dotnet-run-file.md
public static bool IsFileBasedApp => AppContext.GetData("EntryPointFilePath") != null
&& AppContext.GetData("EntryPointFileDirectoryPath") != null;

public static readonly bool IsRunningInContainer = string.Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"), "true");

internal static string GetArchitecture() => GetCurrentPlatform().ToString();
Expand Down
9 changes: 8 additions & 1 deletion src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BenchmarkDotNet.Portability;
using System.Reflection;

namespace BenchmarkDotNet.Validators
Expand All @@ -11,7 +12,12 @@ private ShadowCopyValidator() { }
public bool TreatsWarningsAsErrors => false;

public IAsyncEnumerable<ValidationError> ValidateAsync(ValidationParameters validationParameters)
=> validationParameters
{
// Skip validation when running on file-based app. because it's executed on temp directory.
if (RuntimeInformation.IsFileBasedApp)
return AsyncEnumerable.Empty<ValidationError>();

return validationParameters
.Benchmarks
.Select(benchmark => benchmark.Descriptor.Type.GetTypeInfo().Assembly)
.Distinct()
Expand All @@ -22,5 +28,6 @@ public IAsyncEnumerable<ValidationError> ValidateAsync(ValidationParameters vali
$"Assembly {assembly} is located in temp. If you are running benchmarks from xUnit you need to disable shadow copy. It's not supported by design.")
)
.ToAsyncEnumerable();
}
}
}