-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPUInfo.cs
88 lines (69 loc) · 2.29 KB
/
GPUInfo.cs
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
using System.Diagnostics;
using System.Runtime.Versioning;
namespace MKVToMP4Converter
{
[SupportedOSPlatform("windows")]
public static class GPUInfo
{
public static void WaitForGPULoadToLighten()
{
float gpuUsage;
do
{
Thread.Sleep(10000);
float gpuUsage1 = GetGPUUsage();
Thread.Sleep(10000);
float gpuUsage2 = GetGPUUsage();
Thread.Sleep(10000);
float gpuUsage3 = GetGPUUsage();
gpuUsage = (gpuUsage1 + gpuUsage2 + gpuUsage3) / 3;
} while (gpuUsage > 30);
Thread.Sleep(20000);
}
private static List<PerformanceCounter> GetGPUCounters()
{
var gpuCounters = new List<PerformanceCounter>();
bool gotCounters = false;
do
{
try
{
var category = new PerformanceCounterCategory("GPU Engine");
string[] counterNames = category.GetInstanceNames();
gpuCounters = counterNames
.Where(counterName => counterName.EndsWith("engtype_3D"))
.SelectMany(counterName => category.GetCounters(counterName))
.Where(counter => counter.CounterName.Equals("Utilization Percentage"))
.ToList();
gotCounters = true;
}
catch
{
gotCounters = false;
}
} while (!gotCounters);
return gpuCounters;
}
private static float GetGPUUsage()
{
float gpuUsage = 0;
bool gotUsage = false;
do
{
List <PerformanceCounter> gpuCounters = GetGPUCounters();
try
{
gpuCounters.ForEach(x => x.NextValue());
Thread.Sleep(1000);
gpuUsage = gpuCounters.Sum(x => x.NextValue());
gotUsage = true;
}
catch
{
gotUsage = false;
}
} while (!gotUsage);
return gpuUsage;
}
}
}