func()

in components/otelopscol/receiver/nvmlreceiver/client.go [290:322]


func (client *nvmlClient) getAverageGpuUtilizationSinceLastQuery(device nvml.Device) (float64, error) {
	nvmlType, samples, ret := nvmlDeviceGetSamples(device, nvml.GPU_UTILIZATION_SAMPLES, client.deviceToLastSeenTimestamp[device])
	if ret != nvml.SUCCESS {
		return 0.0, fmt.Errorf("%v", nvml.ErrorString(ret))
	}

	var mean float64
	var count int64
	latestTimestamp := client.deviceToLastSeenTimestamp[device]
	for _, sample := range samples {
		value, err := nvmlSampleAsFloat64(sample.SampleValue, nvmlType)
		if err != nil {
			return 0.0, err
		}

		if sample.TimeStamp > client.deviceToLastSeenTimestamp[device] {
			mean += value
			count++
		}

		if sample.TimeStamp > latestTimestamp {
			latestTimestamp = sample.TimeStamp
		}
	}
	client.deviceToLastSeenTimestamp[device] = latestTimestamp

	if count == 0 {
		return 0.0, fmt.Errorf("No valid samples since last query")
	}

	mean /= 100.0 * float64(count)
	return mean, nil
}