-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
365 lines (311 loc) · 14.3 KB
/
Copy pathProgram.cs
File metadata and controls
365 lines (311 loc) · 14.3 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
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Security.Principal;
using System.Threading;
using Spectre.Console;
using GpuThermalController.Core;
using GpuThermalController.Dashboard;
using GpuPowerControl.Devices;
using GpuThermalController.Nvml;
using GpuThermalController.Notifications;
namespace GpuThermalController
{
class Program
{
static void Main(string[] args)
{
// Set console title for profiling scripts to identify this process
Console.Title = "GpuPowerControl";
// Parse command-line arguments
bool simulateMode = false;
bool disableNotifications = false;
string? scenarioArg = null;
int? baseTempArg = null;
int? peakTempArg = null;
int? seedArg = null;
for (int i = 0; i < args.Length; i++)
{
switch (args[i].ToLowerInvariant())
{
case "--simulate":
simulateMode = true;
break;
case "--scenario":
if (i + 1 < args.Length) scenarioArg = args[i + 1];
break;
case "--base-temp":
if (i + 1 < args.Length && int.TryParse(args[i + 1], out int baseTemp)) baseTempArg = baseTemp;
break;
case "--peak-temp":
if (i + 1 < args.Length) if (int.TryParse(args[i + 1], out int pt)) peakTempArg = pt;
break;
case "--seed":
if (i + 1 < args.Length) if (int.TryParse(args[i + 1], out int sd)) seedArg = sd;
break;
case "--no-notifications":
disableNotifications = true;
break;
case "--test-error":
ErrorConsole.Error("This is a test error message");
ErrorConsole.Warning("This is a test warning message");
return;
}
}
IGpuDevice device;
bool nvmlInitialized = false;
if (simulateMode)
{
// === SIMULATION MODE ===
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("==========================================================");
Console.WriteLine("*** SIMULATION MODE - No real GPU affected ***");
Console.WriteLine("==========================================================");
Console.ResetColor();
// Build simulation config
var simConfig = new SimulatedGpuConfig();
if (scenarioArg != null)
{
if (Enum.TryParse<SimulationScenario>(scenarioArg, true, out var scenario))
simConfig.Scenario = scenario;
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Invalid scenario: {scenarioArg}. Valid: Default, Idle, Spike, SustainedLoad, Emergency, GradualWarmup");
Console.ResetColor();
return;
}
}
simConfig.BaseTemp = baseTempArg;
simConfig.PeakTemp = peakTempArg;
simConfig.Seed = seedArg;
device = new SimulatedGpuDevice(simConfig);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine($"Scenario: {simConfig.Scenario}");
if (baseTempArg.HasValue) Console.WriteLine($"Base Temp: {baseTempArg}C");
if (peakTempArg.HasValue) Console.WriteLine($"Peak Temp: {peakTempArg}C");
if (seedArg.HasValue) Console.WriteLine($"Random Seed: {seedArg}");
Console.ResetColor();
}
else
{
// === REAL GPU MODE ===
if (!IsAdministrator())
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("ERROR: This application must be run as Administrator to set power limits.");
Console.ResetColor();
return;
}
Console.WriteLine("Initializing NVIDIA Management Library (NVML)...");
int nvmlResult = NVML.nvmlInit_v2();
if (nvmlResult != 0)
{
Console.WriteLine($"Failed to initialize NVML: {NVML.GetErrorMessage(nvmlResult)}. Ensure NVIDIA drivers are installed.");
return;
}
nvmlInitialized = true;
device = SelectGpu();
}
// Load external configuration (appsettings.json)
ThermalControllerConfig config = ThermalControllerConfig.Load();
// Initialize toast notifications (default ON, use --no-notifications to disable)
ToastNotificationService.Initialize();
bool notificationsEnabled = !disableNotifications;
ToastNotificationService.SetConfig(new NotificationConfig { Enabled = notificationsEnabled });
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Started Thermal Controller.");
Console.WriteLine($"Device: {device.Name}");
Console.WriteLine($"Trigger: {config.TriggerTemp}C | Target Stability: {config.TargetTemp}C | Emergency Limit: {config.EmergencyTemp}C");
Console.WriteLine($"Power Range: {device.MinPower}W - {device.MaxPower}W");
Console.ResetColor();
var pidController = new PidController(
config.Kp, config.Ki, config.Kd,
config.TargetTemp, device.MaxPower, device.MinPower,
config.IntegralMax, config.IntegralMin, config.IntegralBand, config.MinimumDt);
var triggerEvaluator = new TriggerEvaluator(
config.TriggerTemp, config.PredictiveFloor, config.LookaheadSeconds);
var controller = new ThermalController(device, pidController, triggerEvaluator, config);
// === DASHBOARD WIRING ===
// 1. Create the data provider (collects metrics from controller via events)
var dataProvider = new DashboardDataProvider(device, config);
dataProvider.Subscribe(controller);
// 2. Create the console dashboard (renders to terminal)
var dashboard = new ConsoleDashboard(dataProvider);
// Create file system abstraction
IFileSystem fileSystem = new FileSystem();
// 3. Create the JSON publisher (writes to disk, togglable)
var jsonPublisher = new JsonPublisher(dataProvider, fileSystem);
jsonPublisher.Start(enabled: false);
// 4. Create the key handler (keyboard shortcuts)
var keyHandler = new KeyHandler();
// Create cancellation token source before wiring events that capture it
var cancellationTokenSource = new CancellationTokenSource();
// Wire up key handler events
keyHandler.QuitRequested += () =>
{
Console.CancelKeyPress -= CancelKeyHandler;
controller.Stop();
cancellationTokenSource.Cancel();
};
keyHandler.ToggleLogRequested += () =>
{
dashboard.ToggleLog();
};
keyHandler.ToggleJsonRequested += () =>
{
jsonPublisher.Toggle();
dashboard.SetJsonStatus(jsonPublisher.IsEnabled);
};
// Create CSV exporter instance
var csvExporter = new CsvExporter(fileSystem);
// P4: CSV export on background thread to avoid blocking the key handler thread
keyHandler.ExportCsvRequested += () =>
{
var events = dataProvider.GetEvents(500);
Task.Run(() =>
{
try
{
var timestamp = DateTime.UtcNow.ToString("yyyyMMdd-HHmmss-fff");
var filePath = $"data/export-{timestamp}.csv";
csvExporter.ExportToCsv(events, filePath);
}
catch (Exception ex)
{
ErrorConsole.Error($"CSV export failed: {ex.Message}");
}
});
};
keyHandler.ToggleConfigRequested += () =>
{
dashboard.ToggleConfig();
};
// Test error output (press T during dashboard to verify ErrorConsole works)
keyHandler.TestErrorRequested += () =>
{
ErrorConsole.Error("Test error triggered (press T during runtime)");
ErrorConsole.Warning("Test warning triggered (press T during runtime)");
};
// PID coefficient adjustment (press P during dashboard)
keyHandler.AdjustPidRequested += () =>
{
// Pause dashboard Live display so TextPrompt can safely use the console
dashboard.Pause();
try
{
AnsiConsole.MarkupLine("");
AnsiConsole.MarkupLine("[bold cyan]Adjust PID Coefficients[/] [gray](press Enter to keep current value)[/]");
var newKp = AnsiConsole.Ask<double>($" Kp [gray](current: {config.Kp:F1})[/]:", config.Kp);
var newKi = AnsiConsole.Ask<double>($" Ki [gray](current: {config.Ki:F1})[/]:", config.Ki);
var newKd = AnsiConsole.Ask<double>($" Kd [gray](current: {config.Kd:F1})[/]:", config.Kd);
// Update the PID controller (also resets integral to prevent windup)
pidController.SetCoefficients(newKp, newKi, newKd);
// Keep config in sync
config.Kp = newKp;
config.Ki = newKi;
config.Kd = newKd;
// Update dashboard config display
dataProvider.UpdatePidCoefficients(newKp, newKi, newKd);
AnsiConsole.MarkupLine($"[green]PID updated: Kp={newKp:F1}, Ki={newKi:F1}, Kd={newKd:F1}[/]");
}
finally
{
dashboard.Resume();
}
};
// Setup graceful exit
Console.CancelKeyPress += CancelKeyHandler;
void CancelKeyHandler(object? sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
controller.Stop();
cancellationTokenSource.Cancel();
}
try
{
// Start dashboard first (it renders immediately with initial empty data)
dashboard.Start();
keyHandler.Start();
// Show startup toast after dashboard is running
//ToastNotificationService.ShowInfoToast("Dashboard Active", "Press ESC to quit, P to adjust PID");
// Small delay to let dashboard initialize
Thread.Sleep(500);
// Then start the controller
controller.RunAsync(cancellationTokenSource.Token).Wait();
}
finally
{
// Cleanup dashboard resources
keyHandler.Dispose();
dashboard.Dispose();
jsonPublisher.Dispose();
dataProvider.Dispose();
if (nvmlInitialized)
{
Console.WriteLine("\nShutting down NVML...");
NVML.nvmlShutdown();
}
// Show shutdown toast
ToastNotificationService.ShowInfoToast("GpuPowerControl", "Application shut down");
}
}
static IGpuDevice SelectGpu()
{
uint deviceCount;
int result = NVML.nvmlDeviceGetCount_v2(out deviceCount);
if (result != 0)
{
Console.WriteLine($"Failed to get GPU device count: {NVML.GetErrorMessage(result)}.");
NVML.nvmlShutdown();
Environment.Exit(1);
}
var gpus = new List<(uint index, string name, NvmlGpuDevice? device)>();
for (uint i = 0; i < deviceCount; i++)
{
NvmlGpuDevice? device = NvmlGpuDevice.Create(i);
if (device != null)
{
gpus.Add((i, device.Name, device));
}
}
if (gpus.Count == 0)
{
Console.WriteLine("No NVIDIA GPUs found.");
NVML.nvmlShutdown();
Environment.Exit(1);
}
if (gpus.Count == 1)
{
Console.WriteLine($"Found 1 NVIDIA GPU: {gpus[0].name}");
return gpus[0].device!;
}
// Multiple GPUs — prompt user to select
Console.WriteLine($"Found {gpus.Count} NVIDIA GPUs:");
for (int i = 0; i < gpus.Count; i++)
{
Console.WriteLine($" [{i + 1}] {gpus[i].name} (index {gpus[i].index})");
}
int selectedIndex;
while (true)
{
Console.Write($"Select GPU (1-{gpus.Count}): ");
string? input = Console.ReadLine();
if (int.TryParse(input, out selectedIndex) && selectedIndex >= 1 && selectedIndex <= gpus.Count)
break;
Console.WriteLine("Invalid selection. Please try again.");
}
int gpuIndex = selectedIndex - 1;
Console.WriteLine($"Selected: {gpus[gpuIndex].name}");
return gpus[gpuIndex].device!;
}
static bool IsAdministrator()
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}
}