-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Implement WakeLock so that the system does not enter sleep #2670
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a7b1edc
Implement WakeLock so that the system does not enter sleep while benc…
leonvandermeer 75b75c9
Apply rework from code review
leonvandermeer 868a4d2
Apply suggestions from code review
leonvandermeer a256b78
Apply rework from code review
leonvandermeer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
uid: BenchmarkDotNet.Samples.IntroWakeLock | ||
--- | ||
|
||
## Sample: IntroWakeLock | ||
|
||
Running benchmarks may sometimes take enough time such that the system enters sleep or turns off the display. | ||
|
||
Using a WakeLock prevents the Windows system doing so. | ||
|
||
### Source code | ||
|
||
[!code-csharp[IntroWakeLock.cs](../../../samples/BenchmarkDotNet.Samples/IntroWakeLock.cs)] | ||
|
||
### Command line | ||
|
||
``` | ||
--wakeLock None | ||
``` | ||
``` | ||
--wakeLock System | ||
``` | ||
``` | ||
--wakeLock Display | ||
``` | ||
|
||
### Links | ||
|
||
* @BenchmarkDotNet.Attributes.WakeLockAttribute | ||
* The permanent link to this sample: @BenchmarkDotNet.Samples.IntroWakeLock | ||
|
||
--- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Configs; | ||
using System; | ||
using System.Threading; | ||
|
||
// *** Attribute Style applied to Assembly *** | ||
[assembly: WakeLock(WakeLockType.System)] | ||
|
||
namespace BenchmarkDotNet.Samples; | ||
|
||
// *** Attribute Style *** | ||
[WakeLock(WakeLockType.Display)] | ||
public class IntroWakeLock | ||
{ | ||
[Benchmark] | ||
public void LongRunning() => Thread.Sleep(TimeSpan.FromSeconds(10)); | ||
} | ||
|
||
// *** Object Style *** | ||
[Config(typeof(Config))] | ||
public class IntroWakeLockObjectStyle | ||
{ | ||
private class Config : ManualConfig | ||
{ | ||
public Config() => WakeLock = WakeLockType.System; | ||
} | ||
|
||
[Benchmark] | ||
public void LongRunning() => Thread.Sleep(TimeSpan.FromSeconds(10)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using BenchmarkDotNet.Configs; | ||
using System; | ||
|
||
namespace BenchmarkDotNet.Attributes | ||
{ | ||
/// <summary> | ||
/// Placing a <see cref="WakeLockAttribute"/> on your assembly or class controls whether the | ||
/// Windows system enters sleep or turns off the display while benchmarks run. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class)] | ||
public sealed class WakeLockAttribute : Attribute, IConfigSource | ||
{ | ||
public WakeLockAttribute(WakeLockType wakeLockType) => | ||
Config = ManualConfig.CreateEmpty().WithWakeLock(wakeLockType); | ||
|
||
public IConfig Config { get; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace BenchmarkDotNet.Configs | ||
{ | ||
public enum WakeLockType | ||
{ | ||
/// <summary> | ||
/// Allows the system to enter sleep and/or turn off the display while benchmarks are running. | ||
/// </summary> | ||
None, | ||
|
||
/// <summary> | ||
/// Forces the system to be in the working state while benchmarks are running. | ||
/// </summary> | ||
System, | ||
|
||
/// <summary> | ||
/// Forces the display to be on while benchmarks are running. | ||
/// </summary> | ||
Display | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.ComponentModel; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace BenchmarkDotNet.Running; | ||
|
||
internal partial class WakeLock | ||
{ | ||
private static class PInvoke | ||
{ | ||
public static SafePowerHandle PowerCreateRequest(string reason) | ||
{ | ||
REASON_CONTEXT context = new REASON_CONTEXT() | ||
{ | ||
Version = POWER_REQUEST_CONTEXT_VERSION, | ||
Flags = POWER_REQUEST_CONTEXT_FLAGS.POWER_REQUEST_CONTEXT_SIMPLE_STRING, | ||
SimpleReasonString = reason | ||
}; | ||
SafePowerHandle safePowerHandle = PowerCreateRequest(context); | ||
if (safePowerHandle.IsInvalid) { throw new Win32Exception(); } | ||
leonvandermeer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return safePowerHandle; | ||
} | ||
|
||
[DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)] | ||
private static extern SafePowerHandle PowerCreateRequest(REASON_CONTEXT Context); | ||
|
||
public static void PowerSetRequest(SafePowerHandle safePowerHandle, POWER_REQUEST_TYPE requestType) | ||
{ | ||
if (!InvokePowerSetRequest(safePowerHandle, requestType)) | ||
{ | ||
throw new Win32Exception(); | ||
} | ||
} | ||
|
||
[DllImport("kernel32.dll", EntryPoint = "PowerSetRequest", ExactSpelling = true, SetLastError = true)] | ||
private static extern bool InvokePowerSetRequest(SafePowerHandle PowerRequest, POWER_REQUEST_TYPE RequestType); | ||
|
||
public static void PowerClearRequest(SafePowerHandle safePowerHandle, POWER_REQUEST_TYPE requestType) | ||
{ | ||
if (!InvokePowerClearRequest(safePowerHandle, requestType)) | ||
{ | ||
throw new Win32Exception(); | ||
} | ||
} | ||
|
||
[DllImport("kernel32.dll", EntryPoint = "PowerClearRequest", ExactSpelling = true, SetLastError = true)] | ||
private static extern bool InvokePowerClearRequest(SafePowerHandle PowerRequest, POWER_REQUEST_TYPE RequestType); | ||
|
||
[DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)] | ||
public static extern bool CloseHandle(nint hObject); | ||
|
||
private struct REASON_CONTEXT | ||
{ | ||
public uint Version; | ||
|
||
public POWER_REQUEST_CONTEXT_FLAGS Flags; | ||
|
||
[MarshalAs(UnmanagedType.LPWStr)] | ||
public string SimpleReasonString; | ||
} | ||
|
||
private const uint POWER_REQUEST_CONTEXT_VERSION = 0U; | ||
|
||
private enum POWER_REQUEST_CONTEXT_FLAGS : uint | ||
{ | ||
POWER_REQUEST_CONTEXT_DETAILED_STRING = 2U, | ||
POWER_REQUEST_CONTEXT_SIMPLE_STRING = 1U, | ||
} | ||
|
||
public enum POWER_REQUEST_TYPE | ||
{ | ||
PowerRequestDisplayRequired = 0, | ||
leonvandermeer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PowerRequestSystemRequired = 1, | ||
PowerRequestAwayModeRequired = 2, | ||
PowerRequestExecutionRequired = 3, | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.Win32.SafeHandles; | ||
|
||
namespace BenchmarkDotNet.Running; | ||
|
||
internal partial class WakeLock | ||
{ | ||
private sealed class SafePowerHandle : SafeHandleZeroOrMinusOneIsInvalid | ||
{ | ||
private SafePowerHandle() : base(true) { } | ||
|
||
protected override bool ReleaseHandle() => PInvoke.CloseHandle(handle); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.