private void GetLatestInternal() { if (CompareExchange(ref _dcgmHandle, IntPtr.Zero, IntPtr.Zero) == IntPtr.Zero) { throw new InvalidOperationException("Failed operation because " + nameof(Metrics) + " has not been initialized."); } ushort[] fieldIds = _gpuMetricsFieldGroup.GetInfo().FieldIds; var groupInfo = _gpuGroup.GetInfo(); // Update dcgm fields libdcgm.dcgmUpdateAllFields(_dcgmHandle, 0); var latest = new List <LatestGpuMetrics>(); fixed(ushort *pFieldIds = fieldIds) { foreach (var gpuId in groupInfo.DeviceIds) { var gpuMetrics = new LatestGpuMetrics { DeviceId = gpuId }; dcgm_field_value_v1 *values = stackalloc dcgm_field_value_v1[fieldIds.Length]; var status = libdcgm.dcgmGetLatestValuesForFields(_dcgmHandle, (uint)gpuId, pFieldIds, (uint)fieldIds.Length, values); if (status != dcgm_return.Ok) { throw new InvalidOperationException($"Error fetching latest values for watches. {Utils.errorString(status)}."); } // Store fetched values in GpuMetrics struct gpuMetrics.Data = new GpuMetricsData { MemoryUtilization = values[0].i64, GpuUtilization = values[1].i64, FreeBar1 = values[2].i64, UsedBar1 = values[3].i64, FreeFrameBuffer = values[4].i64, UsedFrameBuffer = values[5].i64, }; latest.Add(gpuMetrics); } } lock (_syncpoint) { _latestMetrics.Clear(); _latestMetrics.AddRange(latest); } }
public IList <LatestGpuMetrics> GetLatest() { LatestGpuMetrics[] metrics; lock (_syncpoint) { metrics = new LatestGpuMetrics[_latestMetrics.Count]; _latestMetrics.CopyTo(metrics, 0); } return(metrics); }