-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPerformanceLabAutomationRunner.cs
More file actions
426 lines (355 loc) · 17.1 KB
/
PerformanceLabAutomationRunner.cs
File metadata and controls
426 lines (355 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using TidyWindow.Core.Automation;
using TidyWindow.Core.Performance;
namespace TidyWindow.App.Services;
/// <summary>
/// Replays selected Performance Lab steps automatically after reboot using the last-known presets.
/// </summary>
public sealed class PerformanceLabAutomationRunner : IDisposable
{
private const string UltimateSchemeGuid = "e9a42b02-d5df-448d-aa00-03f14749eb61";
private static readonly Regex GuidRegex = new("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}", RegexOptions.Compiled | RegexOptions.CultureInvariant);
private readonly PerformanceLabAutomationSettingsStore _store;
private readonly IPerformanceLabService _service;
private readonly ActivityLogService _activityLog;
private readonly IAutomationWorkTracker _workTracker;
private readonly SemaphoreSlim _runLock = new(1, 1);
private PerformanceLabAutomationSettings _settings;
private bool _disposed;
public PerformanceLabAutomationRunner(
PerformanceLabAutomationSettingsStore store,
IPerformanceLabService service,
ActivityLogService activityLog,
IAutomationWorkTracker workTracker)
{
_store = store ?? throw new ArgumentNullException(nameof(store));
_service = service ?? throw new ArgumentNullException(nameof(service));
_activityLog = activityLog ?? throw new ArgumentNullException(nameof(activityLog));
_workTracker = workTracker ?? throw new ArgumentNullException(nameof(workTracker));
_settings = _store.Get().Normalize();
_ = RunIfDueAsync();
}
public PerformanceLabAutomationSettings CurrentSettings => _settings;
public event EventHandler<PerformanceLabAutomationSettings>? SettingsChanged;
public async Task<PerformanceLabAutomationRunResult?> ApplySettingsAsync(PerformanceLabAutomationSettings settings, bool runIfDue, CancellationToken cancellationToken = default)
{
ThrowIfDisposed();
var normalized = settings.Normalize();
_settings = normalized;
_store.Save(normalized);
OnSettingsChanged();
if (runIfDue && ShouldRunOnCurrentBoot(normalized))
{
return await RunInternalAsync(normalized, isBackground: false, ignoreBootCheck: false, cancellationToken).ConfigureAwait(false);
}
return null;
}
public Task<PerformanceLabAutomationRunResult?> RunIfDueAsync(CancellationToken cancellationToken = default)
{
ThrowIfDisposed();
if (!ShouldRunOnCurrentBoot(_settings))
{
return Task.FromResult<PerformanceLabAutomationRunResult?>(null);
}
return RunInternalAsync(_settings, isBackground: true, ignoreBootCheck: false, cancellationToken)
.ContinueWith<PerformanceLabAutomationRunResult?>(static task => task.Result, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
public Task<PerformanceLabAutomationRunResult> RunNowAsync(CancellationToken cancellationToken = default)
{
ThrowIfDisposed();
return RunInternalAsync(_settings, isBackground: false, ignoreBootCheck: true, cancellationToken);
}
public long GetCurrentBootMarker() => ComputeBootMarker();
private bool ShouldRunOnCurrentBoot(PerformanceLabAutomationSettings settings)
{
if (!settings.AutomationEnabled || settings.Snapshot is null || !settings.Snapshot.HasActions)
{
return false;
}
var currentBoot = ComputeBootMarker();
return currentBoot != settings.LastBootMarker;
}
private async Task<PerformanceLabAutomationRunResult> RunInternalAsync(PerformanceLabAutomationSettings settings, bool isBackground, bool ignoreBootCheck, CancellationToken cancellationToken)
{
if (!ignoreBootCheck && !ShouldRunOnCurrentBoot(settings))
{
return PerformanceLabAutomationRunResult.Skipped(DateTimeOffset.UtcNow, ComputeBootMarker(), "Not scheduled for this boot.");
}
var timeout = isBackground ? 0 : Timeout.Infinite;
if (!await _runLock.WaitAsync(timeout, cancellationToken).ConfigureAwait(false))
{
return PerformanceLabAutomationRunResult.Skipped(DateTimeOffset.UtcNow, ComputeBootMarker(), "Automation already running.");
}
Guid workToken = Guid.Empty;
var actions = new List<PerformanceLabAutomationActionResult>();
var bootMarker = ComputeBootMarker();
try
{
workToken = _workTracker.BeginWork(AutomationWorkType.Performance, "Performance Lab boot automation");
actions.AddRange(await RunStepsAsync(settings.Snapshot, cancellationToken).ConfigureAwait(false));
var timestamp = DateTimeOffset.UtcNow;
_settings = settings.WithRun(timestamp, bootMarker).Normalize();
_store.Save(_settings);
OnSettingsChanged();
var result = PerformanceLabAutomationRunResult.Completed(timestamp, bootMarker, actions);
LogRunResult(result);
return result;
}
catch (Exception ex)
{
var failure = new PerformanceLabAutomationActionResult("Unhandled", false, ex.Message);
actions.Add(failure);
var timestamp = DateTimeOffset.UtcNow;
var result = PerformanceLabAutomationRunResult.Completed(timestamp, bootMarker, actions);
_activityLog.LogError("Performance Lab automation", "Automation failed", new[] { ex.ToString() });
return result;
}
finally
{
if (workToken != Guid.Empty)
{
_workTracker.CompleteWork(workToken);
}
_runLock.Release();
}
}
private async Task<IReadOnlyList<PerformanceLabAutomationActionResult>> RunStepsAsync(PerformanceLabAutomationSnapshot snapshot, CancellationToken cancellationToken)
{
var actions = new List<PerformanceLabAutomationActionResult>();
async Task AddStepAsync(string name, Func<Task<PowerShellInvocationResult>> action)
{
var result = await InvokeSafelyAsync(action, cancellationToken).ConfigureAwait(false);
actions.Add(MapResult(name, result));
}
if (snapshot.ApplyUltimatePlan)
{
actions.Add(await ApplyUltimatePlanWithVerificationAsync(cancellationToken).ConfigureAwait(false));
}
if (snapshot.ApplyServiceTemplate)
{
var template = string.IsNullOrWhiteSpace(snapshot.ServiceTemplateId) ? "Balanced" : snapshot.ServiceTemplateId;
await AddStepAsync("Service template", () => _service.ApplyServiceSlimmingAsync(template, cancellationToken)).ConfigureAwait(false);
}
if (snapshot.ApplyHardwareFix)
{
await AddStepAsync("Hardware reserved fix", () => _service.ApplyHardwareReservedFixAsync(cancellationToken)).ConfigureAwait(false);
}
if (snapshot.ApplyKernelPreset)
{
await AddStepAsync("Kernel preset", () => _service.ApplyKernelBootActionAsync("Recommended", skipRestorePoint: true, cancellationToken)).ConfigureAwait(false);
}
if (snapshot.ApplyVbsDisable)
{
await AddStepAsync("Disable VBS/HVCI", () => _service.DisableVbsHvciAsync(cancellationToken)).ConfigureAwait(false);
}
if (snapshot.ApplyEtwCleanup)
{
var mode = string.IsNullOrWhiteSpace(snapshot.EtwMode) ? "Minimal" : snapshot.EtwMode;
await AddStepAsync("ETW cleanup", () => _service.CleanupEtwTracingAsync(mode, cancellationToken)).ConfigureAwait(false);
}
if (snapshot.ApplySchedulerPreset)
{
var preset = string.IsNullOrWhiteSpace(snapshot.SchedulerPresetId) ? "Balanced" : snapshot.SchedulerPresetId;
var processes = snapshot.SchedulerProcessNames ?? string.Empty;
await AddStepAsync("Scheduler preset", () => _service.ApplySchedulerAffinityAsync(preset, processes, cancellationToken)).ConfigureAwait(false);
}
if (snapshot.ApplyAutoTune)
{
var preset = string.IsNullOrWhiteSpace(snapshot.AutoTunePresetId) ? "LatencyBoost" : snapshot.AutoTunePresetId;
var processes = snapshot.AutoTuneProcessNames ?? string.Empty;
await AddStepAsync("Auto-tune watcher", () => _service.StartAutoTuneAsync(processes, preset, cancellationToken)).ConfigureAwait(false);
}
return actions;
}
private static PerformanceLabAutomationActionResult MapResult(string name, PowerShellInvocationResult result)
{
if (result is null)
{
return new PerformanceLabAutomationActionResult(name, false, "No result returned.");
}
if (result.IsSuccess)
{
var summary = result.Output?.FirstOrDefault() ?? "Completed";
return new PerformanceLabAutomationActionResult(name, true, summary);
}
var error = result.Errors?.FirstOrDefault() ?? "Operation failed.";
return new PerformanceLabAutomationActionResult(name, false, error);
}
private async Task<PerformanceLabAutomationActionResult> ApplyUltimatePlanWithVerificationAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var steps = new List<string>();
async Task<bool> DetectAsync(string label)
{
var status = await _service.GetPowerPlanStatusAsync(cancellationToken).ConfigureAwait(false);
var name = string.IsNullOrWhiteSpace(status.ActiveSchemeName) ? "unknown" : status.ActiveSchemeName;
steps.Add($"{label}: {(status.IsUltimateActive ? "Ultimate active" : name)}");
return status.IsUltimateActive;
}
if (await DetectAsync("Detect before apply").ConfigureAwait(false))
{
return new PerformanceLabAutomationActionResult("Ultimate Performance plan", true, "Ultimate plan already active.");
}
async Task<bool> TryPrimaryAsync()
{
var primary = await InvokeSafelyAsync(() => _service.EnableUltimatePowerPlanAsync(cancellationToken), cancellationToken).ConfigureAwait(false);
steps.Add(primary.IsSuccess
? "Primary enable: success"
: $"Primary enable failed: {primary.Errors?.FirstOrDefault() ?? "unknown"}");
await Task.Delay(600, cancellationToken).ConfigureAwait(false);
return await DetectAsync("Detect after primary").ConfigureAwait(false);
}
async Task<bool> TryForceAsync()
{
var forced = await ForceUltimatePlanAsync(cancellationToken).ConfigureAwait(false);
steps.Add(forced.Message);
await Task.Delay(600, cancellationToken).ConfigureAwait(false);
return await DetectAsync("Detect after force").ConfigureAwait(false);
}
async Task<bool> TryDuplicateAsync()
{
var duplicate = await RunPowerCfgAsync($"-duplicatescheme {UltimateSchemeGuid}", cancellationToken).ConfigureAwait(false);
var dupGuid = GuidRegex.Match(duplicate.Output ?? string.Empty).Value;
var target = string.IsNullOrWhiteSpace(dupGuid) ? UltimateSchemeGuid : dupGuid;
var activate = await RunPowerCfgAsync($"-setactive {target}", cancellationToken).ConfigureAwait(false);
steps.Add($"Duplicate attempt exit {duplicate.ExitCode}; activate exit {activate.ExitCode}");
await Task.Delay(600, cancellationToken).ConfigureAwait(false);
return await DetectAsync("Detect after duplicate").ConfigureAwait(false);
}
if (await TryPrimaryAsync().ConfigureAwait(false))
{
return new PerformanceLabAutomationActionResult("Ultimate Performance plan", true, string.Join(" | ", steps));
}
if (await TryForceAsync().ConfigureAwait(false))
{
return new PerformanceLabAutomationActionResult("Ultimate Performance plan", true, string.Join(" | ", steps));
}
if (await TryDuplicateAsync().ConfigureAwait(false))
{
return new PerformanceLabAutomationActionResult("Ultimate Performance plan", true, string.Join(" | ", steps));
}
return new PerformanceLabAutomationActionResult("Ultimate Performance plan", false, string.Join(" | ", steps));
}
private async Task<PerformanceLabAutomationActionResult> ForceUltimatePlanAsync(CancellationToken cancellationToken)
{
try
{
var activate = await RunPowerCfgAsync($"-setactive {UltimateSchemeGuid}", cancellationToken).ConfigureAwait(false);
if (activate.ExitCode != 0)
{
var duplicate = await RunPowerCfgAsync($"-duplicatescheme {UltimateSchemeGuid}", cancellationToken).ConfigureAwait(false);
var duplicatedGuid = GuidRegex.Match(duplicate.Output ?? string.Empty).Value;
var target = string.IsNullOrWhiteSpace(duplicatedGuid) ? UltimateSchemeGuid : duplicatedGuid;
activate = await RunPowerCfgAsync($"-setactive {target}", cancellationToken).ConfigureAwait(false);
}
var status = await _service.GetPowerPlanStatusAsync(cancellationToken).ConfigureAwait(false);
if (status.IsUltimateActive)
{
var message = activate.ExitCode == 0 ? "Ultimate plan forced via powercfg." : "Ultimate plan forced after duplicate creation.";
return new PerformanceLabAutomationActionResult("Ultimate Performance plan", true, message);
}
var errorMessage = string.IsNullOrWhiteSpace(activate.Errors)
? "powercfg fallback failed to activate Ultimate plan."
: activate.Errors.Trim();
return new PerformanceLabAutomationActionResult("Ultimate Performance plan", false, errorMessage);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
return new PerformanceLabAutomationActionResult("Ultimate Performance plan", false, ex.Message);
}
}
private static async Task<(int ExitCode, string Output, string Errors)> RunPowerCfgAsync(string arguments, CancellationToken cancellationToken)
{
var startInfo = new ProcessStartInfo
{
FileName = "powercfg",
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var process = new Process { StartInfo = startInfo };
process.Start();
var outputTask = process.StandardOutput.ReadToEndAsync();
var errorTask = process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false);
var output = await outputTask.ConfigureAwait(false);
var errors = await errorTask.ConfigureAwait(false);
return (process.ExitCode, output, errors);
}
private static async Task<PowerShellInvocationResult> InvokeSafelyAsync(Func<Task<PowerShellInvocationResult>> action, CancellationToken cancellationToken)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
return await action().ConfigureAwait(false);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
return new PowerShellInvocationResult(Array.Empty<string>(), new[] { ex.Message }, 1);
}
}
private void LogRunResult(PerformanceLabAutomationRunResult result)
{
if (result.WasSkipped)
{
_activityLog.LogInformation("Performance Lab automation", $"Skipped: {result.SkipReason}");
return;
}
var successes = result.Actions.Count(a => a.Succeeded);
var failures = result.Actions.Count - successes;
var message = failures == 0
? $"Automation completed ({successes} steps)"
: $"Automation completed with {failures} failure(s) ({successes} succeeded)";
var details = result.Actions.Select(a => $"{a.Name}: {(a.Succeeded ? "Success" : "Fail")} - {a.Message}").ToArray();
_activityLog.LogInformation("Performance Lab automation", message, details);
}
private static long ComputeBootMarker()
{
try
{
var uptime = TimeSpan.FromMilliseconds(Environment.TickCount64);
var boot = DateTimeOffset.UtcNow - uptime;
return boot.ToUnixTimeSeconds();
}
catch
{
return 0;
}
}
private void OnSettingsChanged()
{
SettingsChanged?.Invoke(this, _settings);
}
private void ThrowIfDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(PerformanceLabAutomationRunner));
}
}
public void Dispose()
{
if (_disposed)
{
return;
}
_disposed = true;
_runLock.Dispose();
}
}