Skip to content

Commit 4af3c17

Browse files
committed
runtime: wrap nanotime, walltime, and write
In preparation for general faketime support, this renames the existing nanotime, walltime, and write functions to nanotime1, walltime1, and write1 and wraps them with trivial Go functions. This will let us inject different implementations on all platforms when faketime is enabled. Updates #30439. Change-Id: Ice5ccc513a32a6d89ea051638676d3ee05b00418 Reviewed-on: https://go-review.googlesource.com/c/go/+/192738 Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 6719d88 commit 4af3c17

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+164
-133
lines changed

misc/wasm/wasm_exec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,13 @@
248248
this.mem = new DataView(this._inst.exports.mem.buffer);
249249
},
250250

251-
// func nanotime() int64
252-
"runtime.nanotime": (sp) => {
251+
// func nanotime1() int64
252+
"runtime.nanotime1": (sp) => {
253253
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
254254
},
255255

256-
// func walltime() (sec int64, nsec int32)
257-
"runtime.walltime": (sp) => {
256+
// func walltime1() (sec int64, nsec int32)
257+
"runtime.walltime1": (sp) => {
258258
const msec = (new Date).getTime();
259259
setInt64(sp + 8, msec / 1000);
260260
this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);

src/runtime/os2_aix.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,10 @@ func exit(code int32) {
390390
exit1(code)
391391
}
392392

393-
func write1(fd, p uintptr, n int32) int32
393+
func write2(fd, p uintptr, n int32) int32
394394

395395
//go:nosplit
396-
func write(fd uintptr, p unsafe.Pointer, n int32) int32 {
396+
func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
397397
_g_ := getg()
398398

399399
// Check the validity of g because without a g during
@@ -402,7 +402,7 @@ func write(fd uintptr, p unsafe.Pointer, n int32) int32 {
402402
r, _ := syscall3(&libc_write, uintptr(fd), uintptr(p), uintptr(n))
403403
return int32(r)
404404
}
405-
return write1(fd, uintptr(p), n)
405+
return write2(fd, uintptr(p), n)
406406

407407
}
408408

src/runtime/os3_solaris.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,11 @@ func munmap(addr unsafe.Pointer, n uintptr) {
393393
sysvicall2(&libc_munmap, uintptr(addr), uintptr(n))
394394
}
395395

396-
func nanotime1()
396+
func nanotime2()
397397

398398
//go:nosplit
399-
func nanotime() int64 {
400-
return int64(sysvicall0((*libcFunc)(unsafe.Pointer(funcPC(nanotime1)))))
399+
func nanotime1() int64 {
400+
return int64(sysvicall0((*libcFunc)(unsafe.Pointer(funcPC(nanotime2)))))
401401
}
402402

403403
//go:nosplit
@@ -499,7 +499,7 @@ func usleep(µs uint32) {
499499
}
500500

501501
//go:nosplit
502-
func write(fd uintptr, buf unsafe.Pointer, nbyte int32) int32 {
502+
func write1(fd uintptr, buf unsafe.Pointer, nbyte int32) int32 {
503503
return int32(sysvicall3(&libc_write, uintptr(fd), uintptr(buf), uintptr(nbyte)))
504504
}
505505

src/runtime/os_aix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,15 @@ const (
323323
)
324324

325325
//go:nosplit
326-
func nanotime() int64 {
326+
func nanotime1() int64 {
327327
tp := &timespec{}
328328
if clock_gettime(_CLOCK_REALTIME, tp) != 0 {
329329
throw("syscall clock_gettime failed")
330330
}
331331
return tp.tv_sec*1000000000 + tp.tv_nsec
332332
}
333333

334-
func walltime() (sec int64, nsec int32) {
334+
func walltime1() (sec int64, nsec int32) {
335335
ts := &timespec{}
336336
if clock_gettime(_CLOCK_REALTIME, ts) != 0 {
337337
throw("syscall clock_gettime failed")

src/runtime/os_js.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func exit(code int32)
1414

15-
func write(fd uintptr, p unsafe.Pointer, n int32) int32 {
15+
func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
1616
if fd > 2 {
1717
throw("runtime.write to fd > 2 is unsupported")
1818
}

src/runtime/os_nacl.go

+2
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ var lastfaketime int64
300300
// but the timestamp must increase if the fd changes.
301301
var lastfaketimefd int32
302302

303+
func walltime() (sec int64, nsec int32)
304+
303305
/*
304306
An attempt at IRT. Doesn't work. See end of sys_nacl_amd64.s.
305307

src/runtime/os_plan9.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func usleep(µs uint32) {
328328
}
329329

330330
//go:nosplit
331-
func nanotime() int64 {
331+
func nanotime1() int64 {
332332
var scratch int64
333333
ns := nsec(&scratch)
334334
// TODO(aram): remove hack after I fix _nsec in the pc64 kernel.
@@ -373,7 +373,7 @@ func postnote(pid uint64, msg []byte) int {
373373
return -1
374374
}
375375
len := findnull(&msg[0])
376-
if write(uintptr(fd), unsafe.Pointer(&msg[0]), int32(len)) != int64(len) {
376+
if write1(uintptr(fd), unsafe.Pointer(&msg[0]), int32(len)) != int32(len) {
377377
closefd(fd)
378378
return -1
379379
}
@@ -451,8 +451,8 @@ func read(fd int32, buf unsafe.Pointer, n int32) int32 {
451451
}
452452

453453
//go:nosplit
454-
func write(fd uintptr, buf unsafe.Pointer, n int32) int64 {
455-
return int64(pwrite(int32(fd), buf, n, -1))
454+
func write1(fd uintptr, buf unsafe.Pointer, n int32) int32 {
455+
return pwrite(int32(fd), buf, n, -1)
456456
}
457457

458458
var _badsignal = []byte("runtime: signal received on thread not created by Go.\n")

src/runtime/os_windows.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,6 @@ func osinit() {
416416
stdcall2(_SetProcessPriorityBoost, currentProcess, 1)
417417
}
418418

419-
func nanotime() int64
420-
421419
// useQPCTime controls whether time.now and nanotime use QueryPerformanceCounter.
422420
// This is only set to 1 when running under Wine.
423421
var useQPCTime uint8
@@ -542,8 +540,12 @@ func exit(code int32) {
542540
stdcall1(_ExitProcess, uintptr(code))
543541
}
544542

543+
// write1 must be nosplit because it's used as a last resort in
544+
// functions like badmorestackg0. In such cases, we'll always take the
545+
// ASCII path.
546+
//
545547
//go:nosplit
546-
func write(fd uintptr, buf unsafe.Pointer, n int32) int32 {
548+
func write1(fd uintptr, buf unsafe.Pointer, n int32) int32 {
547549
const (
548550
_STD_OUTPUT_HANDLE = ^uintptr(10) // -11
549551
_STD_ERROR_HANDLE = ^uintptr(11) // -12
@@ -640,6 +642,9 @@ func writeConsoleUTF16(handle uintptr, b []uint16) {
640642
return
641643
}
642644

645+
// walltime1 isn't implemented on Windows, but will never be called.
646+
func walltime1() (sec int64, nsec int32)
647+
643648
//go:nosplit
644649
func semasleep(ns int64) int32 {
645650
const (

src/runtime/stubs2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func exit(code int32)
2121
func usleep(usec uint32)
2222

2323
//go:noescape
24-
func write(fd uintptr, p unsafe.Pointer, n int32) int32
24+
func write1(fd uintptr, p unsafe.Pointer, n int32) int32
2525

2626
//go:noescape
2727
func open(name *byte, mode, perm int32) int32

src/runtime/stubs3.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
// +build !plan9
66
// +build !solaris
7-
// +build !windows
87
// +build !nacl
98
// +build !freebsd
109
// +build !darwin
1110
// +build !aix
1211

1312
package runtime
1413

15-
func nanotime() int64
14+
func nanotime1() int64

src/runtime/sys_aix_ppc64.s

+2-2
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ TEXT runtime·exit1(SB),NOSPLIT,$0-4
258258
CSYSCALL()
259259
RET
260260

261-
// Runs on OS stack, called from runtime·write.
262-
TEXT runtime·write1(SB),NOSPLIT,$0-28
261+
// Runs on OS stack, called from runtime·write1.
262+
TEXT runtime·write2(SB),NOSPLIT,$0-28
263263
MOVD fd+0(FP), R3
264264
MOVD p+8(FP), R4
265265
MOVW n+16(FP), R5

src/runtime/sys_darwin.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func usleep_trampoline()
230230

231231
//go:nosplit
232232
//go:cgo_unsafe_args
233-
func write(fd uintptr, p unsafe.Pointer, n int32) int32 {
233+
func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
234234
return libcCall(unsafe.Pointer(funcPC(write_trampoline)), unsafe.Pointer(&fd))
235235
}
236236
func write_trampoline()
@@ -244,7 +244,7 @@ func open_trampoline()
244244

245245
//go:nosplit
246246
//go:cgo_unsafe_args
247-
func nanotime() int64 {
247+
func nanotime1() int64 {
248248
var r struct {
249249
t int64 // raw timer
250250
numer, denom uint32 // conversion factors. nanoseconds = t * numer / denom.
@@ -266,7 +266,7 @@ func nanotime_trampoline()
266266

267267
//go:nosplit
268268
//go:cgo_unsafe_args
269-
func walltime() (int64, int32) {
269+
func walltime1() (int64, int32) {
270270
var t timeval
271271
libcCall(unsafe.Pointer(funcPC(walltime_trampoline)), unsafe.Pointer(&t))
272272
return int64(t.tv_sec), 1000 * t.tv_usec

src/runtime/sys_dragonfly_amd64.s

+4-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ TEXT runtime·read(SB),NOSPLIT,$-8
108108
MOVL AX, ret+24(FP)
109109
RET
110110

111-
TEXT runtime·write(SB),NOSPLIT,$-8
111+
TEXT runtime·write1(SB),NOSPLIT,$-8
112112
MOVQ fd+0(FP), DI // arg 1 fd
113113
MOVQ p+8(FP), SI // arg 2 buf
114114
MOVL n+16(FP), DX // arg 3 count
@@ -146,8 +146,8 @@ TEXT runtime·setitimer(SB), NOSPLIT, $-8
146146
SYSCALL
147147
RET
148148

149-
// func walltime() (sec int64, nsec int32)
150-
TEXT runtime·walltime(SB), NOSPLIT, $32
149+
// func walltime1() (sec int64, nsec int32)
150+
TEXT runtime·walltime1(SB), NOSPLIT, $32
151151
MOVL $232, AX // clock_gettime
152152
MOVQ $0, DI // CLOCK_REALTIME
153153
LEAQ 8(SP), SI
@@ -160,7 +160,7 @@ TEXT runtime·walltime(SB), NOSPLIT, $32
160160
MOVL DX, nsec+8(FP)
161161
RET
162162

163-
TEXT runtime·nanotime(SB), NOSPLIT, $32
163+
TEXT runtime·nanotime1(SB), NOSPLIT, $32
164164
MOVL $232, AX
165165
MOVQ $4, DI // CLOCK_MONOTONIC
166166
LEAQ 8(SP), SI

src/runtime/sys_freebsd_386.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ TEXT runtime·read(SB),NOSPLIT,$-4
9797
MOVL AX, ret+12(FP)
9898
RET
9999

100-
TEXT runtime·write(SB),NOSPLIT,$-4
100+
TEXT runtime·write1(SB),NOSPLIT,$-4
101101
MOVL $4, AX
102102
INT $0x80
103103
JAE 2(PC)

src/runtime/sys_freebsd_amd64.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ TEXT runtime·read(SB),NOSPLIT,$-8
9797
MOVL AX, ret+24(FP)
9898
RET
9999

100-
TEXT runtime·write(SB),NOSPLIT,$-8
100+
TEXT runtime·write1(SB),NOSPLIT,$-8
101101
MOVQ fd+0(FP), DI // arg 1 fd
102102
MOVQ p+8(FP), SI // arg 2 buf
103103
MOVL n+16(FP), DX // arg 3 count

src/runtime/sys_freebsd_arm.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ TEXT runtime·read(SB),NOSPLIT|NOFRAME,$0
119119
MOVW R0, ret+12(FP)
120120
RET
121121

122-
TEXT runtime·write(SB),NOSPLIT|NOFRAME,$0
122+
TEXT runtime·write1(SB),NOSPLIT|NOFRAME,$0
123123
MOVW fd+0(FP), R0 // arg 1 fd
124124
MOVW p+4(FP), R1 // arg 2 buf
125125
MOVW n+8(FP), R2 // arg 3 count

src/runtime/sys_linux_386.s

+4-4
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ TEXT runtime·closefd(SB),NOSPLIT,$0
107107
MOVL AX, ret+4(FP)
108108
RET
109109

110-
TEXT runtime·write(SB),NOSPLIT,$0
110+
TEXT runtime·write1(SB),NOSPLIT,$0
111111
MOVL $SYS_write, AX
112112
MOVL fd+0(FP), BX
113113
MOVL p+4(FP), CX
@@ -192,8 +192,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$0-16
192192
MOVL AX, ret+12(FP)
193193
RET
194194

195-
// func walltime() (sec int64, nsec int32)
196-
TEXT runtime·walltime(SB), NOSPLIT, $0-12
195+
// func walltime1() (sec int64, nsec int32)
196+
TEXT runtime·walltime1(SB), NOSPLIT, $0-12
197197
// We don't know how much stack space the VDSO code will need,
198198
// so switch to g0.
199199

@@ -257,7 +257,7 @@ finish:
257257

258258
// int64 nanotime(void) so really
259259
// void nanotime(int64 *nsec)
260-
TEXT runtime·nanotime(SB), NOSPLIT, $0-8
260+
TEXT runtime·nanotime1(SB), NOSPLIT, $0-8
261261
// Switch to g0 stack. See comment above in runtime·walltime.
262262

263263
MOVL SP, BP // Save old SP; BP unchanged by C code.

src/runtime/sys_linux_amd64.s

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ TEXT runtime·closefd(SB),NOSPLIT,$0-12
8989
MOVL AX, ret+8(FP)
9090
RET
9191

92-
TEXT runtime·write(SB),NOSPLIT,$0-28
92+
TEXT runtime·write1(SB),NOSPLIT,$0-28
9393
MOVQ fd+0(FP), DI
9494
MOVQ p+8(FP), SI
9595
MOVL n+16(FP), DX
@@ -175,8 +175,8 @@ TEXT runtime·mincore(SB),NOSPLIT,$0-28
175175
MOVL AX, ret+24(FP)
176176
RET
177177

178-
// func walltime() (sec int64, nsec int32)
179-
TEXT runtime·walltime(SB),NOSPLIT,$0-12
178+
// func walltime1() (sec int64, nsec int32)
179+
TEXT runtime·walltime1(SB),NOSPLIT,$0-12
180180
// We don't know how much stack space the VDSO code will need,
181181
// so switch to g0.
182182
// In particular, a kernel configured with CONFIG_OPTIMIZE_INLINING=n
@@ -233,7 +233,7 @@ fallback:
233233
MOVL DX, nsec+8(FP)
234234
RET
235235

236-
TEXT runtime·nanotime(SB),NOSPLIT,$0-8
236+
TEXT runtime·nanotime1(SB),NOSPLIT,$0-8
237237
// Switch to g0 stack. See comment above in runtime·walltime.
238238

239239
MOVQ SP, BP // Save old SP; BP unchanged by C code.

src/runtime/sys_linux_arm.s

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ TEXT runtime·closefd(SB),NOSPLIT,$0
7575
MOVW R0, ret+4(FP)
7676
RET
7777

78-
TEXT runtime·write(SB),NOSPLIT,$0
78+
TEXT runtime·write1(SB),NOSPLIT,$0
7979
MOVW fd+0(FP), R0
8080
MOVW p+4(FP), R1
8181
MOVW n+8(FP), R2
@@ -215,7 +215,7 @@ TEXT runtime·mincore(SB),NOSPLIT,$0
215215
MOVW R0, ret+12(FP)
216216
RET
217217

218-
TEXT runtime·walltime(SB),NOSPLIT,$0-12
218+
TEXT runtime·walltime1(SB),NOSPLIT,$0-12
219219
// We don't know how much stack space the VDSO code will need,
220220
// so switch to g0.
221221

@@ -266,8 +266,8 @@ finish:
266266
MOVW R2, nsec+8(FP)
267267
RET
268268

269-
// int64 nanotime(void)
270-
TEXT runtime·nanotime(SB),NOSPLIT,$0-8
269+
// int64 nanotime1(void)
270+
TEXT runtime·nanotime1(SB),NOSPLIT,$0-8
271271
// Switch to g0 stack. See comment above in runtime·walltime.
272272

273273
// Save old SP. Use R13 instead of SP to avoid linker rewriting the offsets.

src/runtime/sys_linux_arm64.s

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ done:
9191
MOVW R0, ret+8(FP)
9292
RET
9393

94-
TEXT runtime·write(SB),NOSPLIT|NOFRAME,$0-28
94+
TEXT runtime·write1(SB),NOSPLIT|NOFRAME,$0-28
9595
MOVD fd+0(FP), R0
9696
MOVD p+8(FP), R1
9797
MOVW n+16(FP), R2
@@ -181,8 +181,8 @@ TEXT runtime·mincore(SB),NOSPLIT|NOFRAME,$0-28
181181
MOVW R0, ret+24(FP)
182182
RET
183183

184-
// func walltime() (sec int64, nsec int32)
185-
TEXT runtime·walltime(SB),NOSPLIT,$24-12
184+
// func walltime1() (sec int64, nsec int32)
185+
TEXT runtime·walltime1(SB),NOSPLIT,$24-12
186186
MOVD RSP, R20 // R20 is unchanged by C code
187187
MOVD RSP, R1
188188

@@ -225,7 +225,7 @@ finish:
225225
MOVW R5, nsec+8(FP)
226226
RET
227227

228-
TEXT runtime·nanotime(SB),NOSPLIT,$24-8
228+
TEXT runtime·nanotime1(SB),NOSPLIT,$24-8
229229
MOVD RSP, R20 // R20 is unchanged by C code
230230
MOVD RSP, R1
231231

0 commit comments

Comments
 (0)