using System;
using System.Linq;
using NvAPIWrapper.Native.Exceptions;
using NvAPIWrapper.Native.General;
using NvAPIWrapper.Native.GPU;
using NvAPIWrapper.Native.GPU.Structures;
using NvAPIWrapper.Native.Helpers;
using NvAPIWrapper.Native.Helpers.Structures;
namespace NvAPIWrapper.Native
{
public static partial class GPUApi
{
///
/// [PRIVATE]
/// Enables the dynamic performance states
///
/// The handle of the GPU to perform the operation on.
public static void EnableDynamicPStates(PhysicalGPUHandle gpuHandle)
{
var status = DelegateFactory.GetDelegate()(
gpuHandle
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
}
///
/// This function retrieves the dynamic performance states information from specific GPU
///
/// Handle of the physical GPU for which the memory information is to be extracted.
/// The device utilizations information array.
/// This operation is not supported.
/// Status.NvidiaDeviceNotFound: No NVIDIA GPU driving a display was found.
/// A delegate callback throws an exception.
public static DynamicPerformanceStatesInfoV1 GetDynamicPerformanceStatesInfoEx(
PhysicalGPUHandle physicalGPUHandle)
{
var getDynamicPerformanceStatesInfoEx =
DelegateFactory.GetDelegate();
foreach (var acceptType in getDynamicPerformanceStatesInfoEx.Accepts())
{
var instance = acceptType.Instantiate();
using (var gpuDynamicPStateInfo = ValueTypeReference.FromValueType(instance, acceptType))
{
var status = getDynamicPerformanceStatesInfoEx(physicalGPUHandle, gpuDynamicPStateInfo);
if (status == Status.IncompatibleStructureVersion)
{
continue;
}
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return gpuDynamicPStateInfo.ToValueType(acceptType);
}
}
throw new NVIDIANotSupportedException("This operation is not supported.");
}
///
/// Gets the reason behind the current decrease in performance.
///
/// The handle of the GPU to perform the operation on.
/// A value indicating the reason of current performance decrease.
public static PerformanceDecreaseReason GetPerformanceDecreaseInfo(PhysicalGPUHandle gpuHandle)
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
out var decreaseReason
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return decreaseReason;
}
///
/// [PRIVATE]
/// Gets the GPU usage metrics for the passed GPU handle.
///
/// The handle of the GPU to perform the operation on.
/// The usage information for the selected GPU.
public static PrivateUsagesInfoV1 GetUsages(PhysicalGPUHandle gpuHandle)
{
var instance = typeof(PrivateUsagesInfoV1).Instantiate();
using (var usageInfoReference = ValueTypeReference.FromValueType(instance))
{
var status = DelegateFactory.GetDelegate()(
gpuHandle,
usageInfoReference
);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return usageInfoReference.ToValueType(typeof(PrivateUsagesInfoV1));
}
}
///
/// Queries active applications.
///
/// The physical GPU handle.
/// The list of active applications.
public static PrivateActiveApplicationV2[] QueryActiveApps(PhysicalGPUHandle gpuHandle)
{
var queryActiveApps = DelegateFactory.GetDelegate();
// ReSharper disable once EventExceptionNotDocumented
if (!queryActiveApps.Accepts().Contains(typeof(PrivateActiveApplicationV2)))
{
throw new NVIDIANotSupportedException("This operation is not supported.");
}
uint count = PrivateActiveApplicationV2.MaximumNumberOfApplications;
var instances = typeof(PrivateActiveApplicationV2).Instantiate()
.Repeat((int) count);
using (var applications = ValueTypeArray.FromArray(instances))
{
// ReSharper disable once EventExceptionNotDocumented
var status = queryActiveApps(gpuHandle, applications, ref count);
if (status != Status.Ok)
{
throw new NVIDIAApiException(status);
}
return applications.ToArray((int) count);
}
}
}
}