-
Notifications
You must be signed in to change notification settings - Fork 5
/
cpu.go
72 lines (59 loc) · 1.6 KB
/
cpu.go
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
package extension
import (
"encoding/binary"
"encoding/json"
"github.com/dop251/goja"
"github.com/dterei/gotsc"
"github.com/klauspost/cpuid/v2"
"github.com/smartbch/egvm/egvm-script/utils"
)
type cpuInfoOutput struct {
cpuid.CPUInfo
Features []string
X64Level int
}
func GetCPUID() string {
info := cpuInfoOutput{
CPUInfo: cpuid.CPU,
Features: cpuid.CPU.FeatureSet(),
X64Level: cpuid.CPU.X64Level(),
}
bz, err := json.Marshal(info)
if err != nil {
panic(goja.NewSymbol("Failed to marshal CPU info: " + err.Error()))
}
return string(bz)
}
func GetTSC(f goja.FunctionCall, vm *goja.Runtime) goja.Value {
if len(f.Arguments) != 0 {
// BIP-44
// m / purpose' / coin' / account' / change / address_index
panic(utils.IncorrectArgumentCount)
}
tsc := gotsc.TSCOverhead()
var result [8]byte
binary.BigEndian.PutUint64(result[:], tsc)
return vm.ToValue(vm.NewArrayBuffer(result[:]))
}
func GetTSCBenchStart(f goja.FunctionCall, vm *goja.Runtime) goja.Value {
if len(f.Arguments) != 0 {
// BIP-44
// m / purpose' / coin' / account' / change / address_index
panic(utils.IncorrectArgumentCount)
}
start := gotsc.BenchStart()
var result [8]byte
binary.BigEndian.PutUint64(result[:], start)
return vm.ToValue(vm.NewArrayBuffer(result[:]))
}
func GetTSCBenchEnd(f goja.FunctionCall, vm *goja.Runtime) goja.Value {
if len(f.Arguments) != 0 {
// BIP-44
// m / purpose' / coin' / account' / change / address_index
panic(utils.IncorrectArgumentCount)
}
end := gotsc.BenchEnd()
var result [8]byte
binary.BigEndian.PutUint64(result[:], end)
return vm.ToValue(vm.NewArrayBuffer(result[:]))
}