Skip to content

runtime/pprof: incorrect mutex contention time #73760

Open
@func25

Description

@func25

Go version

go1.23.9

Output of go env in your module/workspace:

none

What did you do?

I tried a simple snippet to evaluate the mutex profile:

var mtx sync.Mutex
var wg sync.WaitGroup

func acquire(n time.Duration) {
	mtx.Lock()
	defer mtx.Unlock()

	time.Sleep(n)
	wg.Done()
}

func main() {
	runtime.SetMutexProfileFraction(1)

	wg.Add(5)
	go acquire(1 * time.Second) // gowrap1
	go acquire(2 * time.Second) // gowrap2
	go acquire(3 * time.Second) // gowrap3
	go acquire(4 * time.Second) // gowrap4
	go acquire(5 * time.Second) // gowrap5

	wg.Wait()

	// Write mutex profile to file
	f, err := os.Create("mutex.prof")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	if err := pprof.Lookup("mutex").WriteTo(f, 0); err != nil {
		log.Fatal(err)
	}
}

What did you see happen?

The order in which goroutines acquire the lock is: gowrap1gowrap5gowrap2gowrap3gowrap4. However, the result shows an overshoot in gowrap3's contention time and underestimates gowrap2:

Image

What did you expect to see?

The expected contention time for gowrap2 should be 4 seconds. The contention time for gowrap3 should be 3 seconds. Not sure if this behavior is intentional.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions