forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtime.go
More file actions
58 lines (47 loc) · 1.6 KB
/
time.go
File metadata and controls
58 lines (47 loc) · 1.6 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package metering
import (
_ "runtime" //for nanotime() and walltime()
"time"
_ "unsafe" //for go:linkname
)
//go:linkname Monotime runtime.nanotime
func Monotime() int64
// MonoToEpoch converts monotonic time nanos to unix epoch time nanos.
func MonoToEpoch(t int64) int64 {
monoNsec := Monotime()
wallNsec := time.Now().UnixNano()
clockOffset := wallNsec - monoNsec
return t + clockOffset
}
func TimeToMono(t time.Time) int64 {
durNs := time.Since(t).Nanoseconds()
return Monotime() - durNs
}
type ExtensionsResetDurationProfiler struct {
NumAgentsRegisteredForShutdown int
AvailableNs int64
extensionsResetStartTimeNs int64
extensionsResetEndTimeNs int64
}
func (p *ExtensionsResetDurationProfiler) Start() {
p.extensionsResetStartTimeNs = Monotime()
}
func (p *ExtensionsResetDurationProfiler) Stop() {
p.extensionsResetEndTimeNs = Monotime()
}
func (p *ExtensionsResetDurationProfiler) CalculateExtensionsResetMs() (int64, bool) {
var extensionsResetDurationNs = p.extensionsResetEndTimeNs - p.extensionsResetStartTimeNs
var extensionsResetMs int64
timedOut := false
if p.NumAgentsRegisteredForShutdown == 0 || p.AvailableNs < 0 || extensionsResetDurationNs < 0 {
extensionsResetMs = 0
} else if extensionsResetDurationNs > p.AvailableNs {
extensionsResetMs = p.AvailableNs / time.Millisecond.Nanoseconds()
timedOut = true
} else {
extensionsResetMs = extensionsResetDurationNs / time.Millisecond.Nanoseconds()
}
return extensionsResetMs, timedOut
}