golang/go · error

profile does not contain a sample index with value/type "sam

Error message

profile does not contain a sample index with value/type "samples/count" or cpu/nanoseconds"

What it means

Thrown when the pprof profile's SampleType list contains no entry matching "samples"/"count" or "cpu"/"nanoseconds". The deserializer iterates p.SampleType looking for either of these two recognized sample type/unit pairs to determine which value index to use for edge weights. If neither is found, valueIndex stays -1 and the error fires.

Source

Thrown at src/cmd/internal/pgo/pprof.go:46

	if len(p.Sample) == 0 {
		// We accept empty profiles, but there is nothing to do.
		return emptyProfile(), nil
	}

	valueIndex := -1
	for i, s := range p.SampleType {
		// Samples count is the raw data collected, and CPU nanoseconds is just
		// a scaled version of it, so either one we can find is fine.
		if (s.Type == "samples" && s.Unit == "count") ||
			(s.Type == "cpu" && s.Unit == "nanoseconds") {
			valueIndex = i
			break
		}
	}

	if valueIndex == -1 {
		return nil, fmt.Errorf(`profile does not contain a sample index with value/type "samples/count" or cpu/nanoseconds"`)
	}

	g := profile.NewGraph(p, &profile.Options{
		SampleValue: func(v []int64) int64 { return v[valueIndex] },
	})

	if len(g.Nodes) == 0 {
		// If all sample values are 0, the graph will have no nodes.
		// In this case, treat it as an empty profile.
		return emptyProfile(), nil
	}

	namedEdgeMap, totalWeight, err := createNamedEdgeMap(g)
	if err != nil {
		return nil, err
	}

	if totalWeight == 0 {

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Ensure the profile is a CPU profile with SampleType entries of either "samples"/"count" or "cpu"/"nanoseconds".
  2. Use Go's built-in CPU profiling (go test -cpuprofile or runtime/pprof.StartCPUProfile) which emits the correct sample types.
  3. Verify the profile type with `go tool pprof -top <file>` and confirm the sample types listed in the header.
  4. Do not feed heap, goroutine, or other non-CPU profiles as PGO input.

Example fix

// Correct: generate a CPU profile
// f, _ := os.Create("cpu.prof")
// pprof.StartCPUProfile(f)
// ... run workload ...
// pprof.StopCPUProfile()

// Wrong: this produces a heap profile (wrong sample type)
// pprof.WriteHeapProfile(f)
Defensive patterns

Strategy: validation

Validate before calling

// Check sample types before converting to PGO Profile:
func hasValidSampleType(p *profile.Profile) bool {
    for _, st := range p.SampleType {
        if (st.Type == "samples" && st.Unit == "count") ||
           (st.Type == "cpu" && st.Unit == "nanoseconds") {
            return true
        }
    }
    return false
}

// Usage:
// if !hasValidSampleType(pprofProfile) {
//     return errors.New("profile must contain samples/count or cpu/nanoseconds sample type")
// }

Prevention

When it happens

Trigger: The pprof profile's SampleType array only contains entries with different type/unit combinations (e.g. "inclusions"/"samples", "allocations"/"bytes", or custom sample types from a third-party profiler). Neither the "samples/count" nor "cpu/nanoseconds" pair is present.

Common situations: Using a profile from a non-CPU profiler (heap, goroutine, block, mutex profiles) as a PGO input. Also happens with profiles generated by third-party profilers (e.g. Java perf, Python cProfile exported to pprof) that use different sample type naming. Custom runtime/pprof profiles with custom SampleType names also trigger this.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/79d564c194853f34. Report an issue: GitHub.