summaryrefslogtreecommitdiff
path: root/cmd/ioworkload/scenario_sleep.go
blob: c004c4b36e333704ef6a3de9121224f9008aa710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main

import (
	"fmt"
	"runtime"
	"syscall"
	"time"
	"unsafe"

	"golang.org/x/sys/unix"
)

const sleepSyscallsEmitFor = 2 * time.Second

func sleepSyscalls() error {
	deadline := time.Now().Add(sleepSyscallsEmitFor)
	for time.Now().Before(deadline) {
		if err := syscall.Nanosleep(&syscall.Timespec{Sec: 0, Nsec: 2_000_000}, nil); err != nil && err != syscall.EINTR {
			return fmt.Errorf("nanosleep: %w", err)
		}
		if err := callClockNanosleep(3_000_000); err != nil {
			return err
		}
		// Also exercise the TIMER_ABSTIME (absolute) path so the tracer's
		// absolute-sleep handling is covered: the request timespec here is an
		// absolute CLOCK_MONOTONIC wakeup time, not a relative duration, so the
		// tracer must NOT report it as a sleep duration (see task a20).
		if err := callClockNanosleepAbs(2_000_000); err != nil {
			return err
		}
	}
	return nil
}

// callClockNanosleep issues a relative clock_nanosleep (flags = 0), sleeping for
// requestedNs nanoseconds.
func callClockNanosleep(requestedNs int64) error {
	req := unix.Timespec{Sec: requestedNs / 1_000_000_000, Nsec: requestedNs % 1_000_000_000}
	return invokeClockNanosleep(0, &req)
}

// callClockNanosleepAbs issues an absolute clock_nanosleep (flags =
// TIMER_ABSTIME). The request timespec is an absolute CLOCK_MONOTONIC wakeup
// time computed as "now + aheadNs", so the call blocks for roughly aheadNs but
// the request value itself is a multi-decade absolute timestamp, not a
// duration.
func callClockNanosleepAbs(aheadNs int64) error {
	var now unix.Timespec
	if err := unix.ClockGettime(unix.CLOCK_MONOTONIC, &now); err != nil {
		return fmt.Errorf("clock_gettime: %w", err)
	}
	target := now.Nano() + aheadNs
	req := unix.Timespec{Sec: target / 1_000_000_000, Nsec: target % 1_000_000_000}
	return invokeClockNanosleep(unix.TIMER_ABSTIME, &req)
}

// invokeClockNanosleep is the shared raw clock_nanosleep syscall wrapper used by
// both the relative and absolute callers.
func invokeClockNanosleep(flags uintptr, req *unix.Timespec) error {
	_, _, errno := syscall.RawSyscall6(
		unix.SYS_CLOCK_NANOSLEEP,
		uintptr(unix.CLOCK_MONOTONIC),
		flags,
		uintptr(unsafe.Pointer(req)),
		0,
		0,
		0,
	)
	runtime.KeepAlive(req)
	if errno != 0 && errno != syscall.EINTR {
		return fmt.Errorf("clock_nanosleep: %w", errno)
	}
	return nil
}