Source file src/runtime/time_fake.go

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build faketime && !windows
     6  
     7  // Faketime isn't currently supported on Windows. This would require
     8  // modifying syscall.Write to call syscall.faketimeWrite,
     9  // translating the Stdout and Stderr handles into FDs 1 and 2.
    10  // (See CL 192739 PS 3.)
    11  
    12  package runtime
    13  
    14  import "unsafe"
    15  
    16  // faketime is the simulated time in nanoseconds since 1970 for the
    17  // playground.
    18  var faketime int64 = 1257894000000000000
    19  
    20  var faketimeState struct {
    21  	lock mutex
    22  
    23  	// lastfaketime is the last faketime value written to fd 1 or 2.
    24  	lastfaketime int64
    25  
    26  	// lastfd is the fd to which lastfaketime was written.
    27  	//
    28  	// Subsequent writes to the same fd may use the same
    29  	// timestamp, but the timestamp must increase if the fd
    30  	// changes.
    31  	lastfd uintptr
    32  }
    33  
    34  //go:nosplit
    35  func nanotime() int64 {
    36  	return faketime
    37  }
    38  
    39  //go:linkname time_now time.now
    40  func time_now() (sec int64, nsec int32, mono int64) {
    41  	return faketime / 1e9, int32(faketime % 1e9), faketime
    42  }
    43  
    44  // write is like the Unix write system call.
    45  // We have to avoid write barriers to avoid potential deadlock
    46  // on write calls.
    47  //
    48  //go:nowritebarrierrec
    49  func write(fd uintptr, p unsafe.Pointer, n int32) int32 {
    50  	if !(fd == 1 || fd == 2) {
    51  		// Do an ordinary write.
    52  		return write1(fd, p, n)
    53  	}
    54  
    55  	// Write with the playback header.
    56  
    57  	// First, lock to avoid interleaving writes.
    58  	lock(&faketimeState.lock)
    59  
    60  	// If the current fd doesn't match the fd of the previous write,
    61  	// ensure that the timestamp is strictly greater. That way, we can
    62  	// recover the original order even if we read the fds separately.
    63  	t := faketimeState.lastfaketime
    64  	if fd != faketimeState.lastfd {
    65  		t++
    66  		faketimeState.lastfd = fd
    67  	}
    68  	if faketime > t {
    69  		t = faketime
    70  	}
    71  	faketimeState.lastfaketime = t
    72  
    73  	// Playback header: 0 0 P B <8-byte time> <4-byte data length> (big endian)
    74  	var buf [4 + 8 + 4]byte
    75  	buf[2] = 'P'
    76  	buf[3] = 'B'
    77  	tu := uint64(t)
    78  	buf[4] = byte(tu >> (7 * 8))
    79  	buf[5] = byte(tu >> (6 * 8))
    80  	buf[6] = byte(tu >> (5 * 8))
    81  	buf[7] = byte(tu >> (4 * 8))
    82  	buf[8] = byte(tu >> (3 * 8))
    83  	buf[9] = byte(tu >> (2 * 8))
    84  	buf[10] = byte(tu >> (1 * 8))
    85  	buf[11] = byte(tu >> (0 * 8))
    86  	nu := uint32(n)
    87  	buf[12] = byte(nu >> (3 * 8))
    88  	buf[13] = byte(nu >> (2 * 8))
    89  	buf[14] = byte(nu >> (1 * 8))
    90  	buf[15] = byte(nu >> (0 * 8))
    91  	write1(fd, unsafe.Pointer(&buf[0]), int32(len(buf)))
    92  
    93  	// Write actual data.
    94  	res := write1(fd, p, n)
    95  
    96  	unlock(&faketimeState.lock)
    97  	return res
    98  }
    99  

View as plain text