Skip to content

Commit 0435e88

Browse files
committed
runtime: revert "do not call timeBeginPeriod on windows"
This reverts commit ab4c929. Sysmon critically depends on system timer resolution for retaking of Ps blocked in system calls. See #14790 for an example of a program where execution time goes from 2ms to 30ms if timeBeginPeriod(1) is not used. We can remove timeBeginPeriod(1) when we support UMS (#7876). Update #14790 Change-Id: I362b56154359b2c52d47f9f2468fe012b481cf6d Reviewed-on: https://go-review.googlesource.com/20834 Reviewed-by: Austin Clements <[email protected]> Run-TryBot: Dmitry Vyukov <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Alex Brainman <[email protected]>
1 parent 9d4efdf commit 0435e88

File tree

4 files changed

+25
-59
lines changed

4 files changed

+25
-59
lines changed

misc/cgo/testcarchive/carchive_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func goEnv(key string) string {
120120
func compilemain(t *testing.T, libgo string) {
121121
ccArgs := append(cc, "-o", "testp"+exeSuffix, "main.c")
122122
if GOOS == "windows" {
123-
ccArgs = append(ccArgs, "main_windows.c", libgo, "-lntdll", "-lws2_32")
123+
ccArgs = append(ccArgs, "main_windows.c", libgo, "-lntdll", "-lws2_32", "-lwinmm")
124124
} else {
125125
ccArgs = append(ccArgs, "main_unix.c", libgo)
126126
}

src/runtime/export_windows_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ package runtime
88

99
import "unsafe"
1010

11-
var TestingWER = &testingWER
12-
var OsYield = osyield
11+
var (
12+
TestingWER = &testingWER
13+
OsYield = osyield
14+
TimeBeginPeriodRetValue = &timeBeginPeriodRetValue
15+
)
1316

1417
func NumberOfProcessors() int32 {
1518
var info systeminfo

src/runtime/os_windows.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const (
5353
//go:cgo_import_dynamic runtime._WaitForSingleObject WaitForSingleObject%2 "kernel32.dll"
5454
//go:cgo_import_dynamic runtime._WriteConsoleW WriteConsoleW%5 "kernel32.dll"
5555
//go:cgo_import_dynamic runtime._WriteFile WriteFile%5 "kernel32.dll"
56+
//go:cgo_import_dynamic runtime._timeBeginPeriod timeBeginPeriod%1 "winmm.dll"
5657

5758
type stdFunction unsafe.Pointer
5859

@@ -98,7 +99,9 @@ var (
9899
_WSAGetOverlappedResult,
99100
_WaitForSingleObject,
100101
_WriteConsoleW,
101-
_WriteFile stdFunction
102+
_WriteFile,
103+
_timeBeginPeriod,
104+
_ stdFunction
102105

103106
// Following syscalls are only available on some Windows PCs.
104107
// We will load syscalls, if available, before using them.
@@ -228,6 +231,8 @@ func setlasterror(err uint32)
228231
// flags can be used with LoadLibraryEx."
229232
var useLoadLibraryEx bool
230233

234+
var timeBeginPeriodRetValue uint32
235+
231236
func osinit() {
232237
asmstdcallAddr = unsafe.Pointer(funcPC(asmstdcall))
233238
usleep2Addr = unsafe.Pointer(funcPC(usleep2))
@@ -247,6 +252,8 @@ func osinit() {
247252

248253
stdcall2(_SetConsoleCtrlHandler, funcPC(ctrlhandler), 1)
249254

255+
timeBeginPeriodRetValue = uint32(stdcall1(_timeBeginPeriod, 1))
256+
250257
ncpu = getproccount()
251258

252259
// Windows dynamic priority boosting assumes that a process has different types

src/runtime/syscall_windows_test.go

+11-55
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,13 @@ uintptr_t cfunc(callback f, uintptr_t n) {
622622
}
623623
}
624624

625+
func TestTimeBeginPeriod(t *testing.T) {
626+
const TIMERR_NOERROR = 0
627+
if *runtime.TimeBeginPeriodRetValue != TIMERR_NOERROR {
628+
t.Fatalf("timeBeginPeriod failed: it returned %d", *runtime.TimeBeginPeriodRetValue)
629+
}
630+
}
631+
625632
// removeOneCPU removes one (any) cpu from affinity mask.
626633
// It returns new affinity mask.
627634
func removeOneCPU(mask uintptr) (uintptr, error) {
@@ -874,21 +881,10 @@ var (
874881
modwinmm = syscall.NewLazyDLL("winmm.dll")
875882
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
876883

877-
proctimeBeginPeriod = modwinmm.NewProc("timeBeginPeriod")
878-
proctimeEndPeriod = modwinmm.NewProc("timeEndPeriod")
879-
880884
procCreateEvent = modkernel32.NewProc("CreateEventW")
881885
procSetEvent = modkernel32.NewProc("SetEvent")
882886
)
883887

884-
func timeBeginPeriod(period uint32) {
885-
syscall.Syscall(proctimeBeginPeriod.Addr(), 1, uintptr(period), 0, 0)
886-
}
887-
888-
func timeEndPeriod(period uint32) {
889-
syscall.Syscall(proctimeEndPeriod.Addr(), 1, uintptr(period), 0, 0)
890-
}
891-
892888
func createEvent() (syscall.Handle, error) {
893889
r0, _, e0 := syscall.Syscall6(procCreateEvent.Addr(), 4, 0, 0, 0, 0, 0, 0)
894890
if r0 == 0 {
@@ -905,7 +901,7 @@ func setEvent(h syscall.Handle) error {
905901
return nil
906902
}
907903

908-
func benchChanToSyscallPing(b *testing.B) {
904+
func BenchmarkChanToSyscallPing(b *testing.B) {
909905
n := b.N
910906
ch := make(chan int)
911907
event, err := createEvent()
@@ -927,17 +923,7 @@ func benchChanToSyscallPing(b *testing.B) {
927923
}
928924
}
929925

930-
func BenchmarkChanToSyscallPing1ms(b *testing.B) {
931-
timeBeginPeriod(1)
932-
benchChanToSyscallPing(b)
933-
timeEndPeriod(1)
934-
}
935-
936-
func BenchmarkChanToSyscallPing15ms(b *testing.B) {
937-
benchChanToSyscallPing(b)
938-
}
939-
940-
func benchSyscallToSyscallPing(b *testing.B) {
926+
func BenchmarkSyscallToSyscallPing(b *testing.B) {
941927
n := b.N
942928
event1, err := createEvent()
943929
if err != nil {
@@ -965,17 +951,7 @@ func benchSyscallToSyscallPing(b *testing.B) {
965951
}
966952
}
967953

968-
func BenchmarkSyscallToSyscallPing1ms(b *testing.B) {
969-
timeBeginPeriod(1)
970-
benchSyscallToSyscallPing(b)
971-
timeEndPeriod(1)
972-
}
973-
974-
func BenchmarkSyscallToSyscallPing15ms(b *testing.B) {
975-
benchSyscallToSyscallPing(b)
976-
}
977-
978-
func benchChanToChanPing(b *testing.B) {
954+
func BenchmarkChanToChanPing(b *testing.B) {
979955
n := b.N
980956
ch1 := make(chan int)
981957
ch2 := make(chan int)
@@ -991,28 +967,8 @@ func benchChanToChanPing(b *testing.B) {
991967
}
992968
}
993969

994-
func BenchmarkChanToChanPing1ms(b *testing.B) {
995-
timeBeginPeriod(1)
996-
benchChanToChanPing(b)
997-
timeEndPeriod(1)
998-
}
999-
1000-
func BenchmarkChanToChanPing15ms(b *testing.B) {
1001-
benchChanToChanPing(b)
1002-
}
1003-
1004-
func benchOsYield(b *testing.B) {
970+
func BenchmarkOsYield(b *testing.B) {
1005971
for i := 0; i < b.N; i++ {
1006972
runtime.OsYield()
1007973
}
1008974
}
1009-
1010-
func BenchmarkOsYield1ms(b *testing.B) {
1011-
timeBeginPeriod(1)
1012-
benchOsYield(b)
1013-
timeEndPeriod(1)
1014-
}
1015-
1016-
func BenchmarkOsYield15ms(b *testing.B) {
1017-
benchOsYield(b)
1018-
}

0 commit comments

Comments
 (0)