-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
259 lines (227 loc) · 4.78 KB
/
utils.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package golog
import (
"bytes"
"runtime"
"sync"
"time"
_ "unsafe"
)
const (
recordBufSize = 128
dateTimeBufSize = 10 // length of date string
)
var (
recordPool = sync.Pool{
New: func() interface{} {
return &Record{}
},
}
bufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, recordBufSize))
},
}
uintBytes2 [60][]byte // 0 - 59
uintBytes4 [69][]byte // 1970 - 2038
uintBytes [999][]byte // 2 - 1000
frameCache sync.Map
now = time.Now
fastTimer = FastTimer{}
)
func init() {
for i := 0; i < 60; i++ { // hour / minute / second is between 0 and 59
uintBytes2[i] = uint2Bytes(i, 2)
}
for i := 0; i < 69; i++ { // year is between 1970 and 2038
uintBytes4[i] = uint2Bytes(1970+i, 4)
}
for i := 0; i < 999; i++ { // source code line number is usually between 2 and 1000
uintBytes[i] = uint2DynamicBytes(i + 2)
}
}
func uint2Bytes(x, size int) []byte {
// x and size should be uint32
result := make([]byte, size)
for i := 0; i < size; i++ {
r := x % 10
result[size-i-1] = byte(r) + '0'
x /= 10
}
return result
}
func uint2DynamicBytes(x int) []byte {
// x should be uint32
size := 0
switch {
case x < 10:
return []byte{byte(x) + '0'}
case x < 100:
size = 2
case x < 1000:
size = 3
case x < 10000:
size = 4
case x < 100000:
size = 5
case x < 1000000:
size = 6
case x < 10000000:
size = 7
case x < 100000000:
size = 8
case x < 1000000000:
size = 9
default:
size = 10
}
result := make([]byte, size)
for i := 0; i < size; i++ {
r := x % 10
result[size-i-1] = byte(r) + '0'
x /= 10
}
return result
}
func uint2Bytes2(x int) []byte {
// x should between 0 and 59
return uintBytes2[x]
}
func uint2Bytes4(x int) []byte {
// x should between 1970 and 2038
return uintBytes4[x-1970]
}
func fastUint2DynamicBytes(x int) []byte {
// x should be uint32
size := 0
switch {
case x < 2:
return []byte{byte(x) + '0'}
case x <= 1000:
return uintBytes[x-2]
case x < 10000:
size = 4
case x < 100000:
size = 5
case x < 1000000:
size = 6
case x < 10000000:
size = 7
case x < 100000000:
size = 8
case x < 1000000000:
size = 9
default:
size = 10
}
result := make([]byte, size)
for i := 0; i < size; i++ {
r := x % 10
result[size-i-1] = byte(r) + '0'
x /= 10
}
return result
}
func stopTimer(timer *time.Timer) {
if !timer.Stop() {
select {
case <-timer.C:
default:
}
}
}
func logError(err error) {
if internalLogger != nil {
file, line := Caller(1)
internalLogger.Log(ErrorLevel, file, line, err.Error())
}
}
func setNowFunc(nowFunc func() time.Time) {
now = nowFunc
}
//go:noescape
//go:linkname callers runtime.callers
func callers(skip int, pcbuf []uintptr) int
// Caller caches the result for runtime.Caller().
// Inspired by https://zhuanlan.zhihu.com/p/403417640
func Caller(skip int) (file string, line int) {
rpc := [1]uintptr{}
n := callers(skip+1, rpc[:])
if n < 1 {
return
}
var frame runtime.Frame
pc := rpc[0]
if f, ok := frameCache.Load(pc); ok {
frame = f.(runtime.Frame)
} else {
frame, _ = runtime.CallersFrames([]uintptr{pc}).Next()
frameCache.Store(pc, frame)
}
return frame.File, frame.Line
}
// FastTimer is not thread-safe for performance reason, but all the threads will notice its changes in a few milliseconds.
type FastTimer struct {
date string
time string
stopChan chan struct{}
isRunning bool
}
func (t *FastTimer) update(tm time.Time, buf *bytes.Buffer) {
buf.Reset()
year, mon, day := tm.Date()
buf.Write(uint2Bytes4(year))
buf.WriteByte('-')
buf.Write(uint2Bytes2(int(mon)))
buf.WriteByte('-')
buf.Write(uint2Bytes2(day))
t.date = buf.String()
buf.Reset()
hour, min, sec := tm.Clock()
buf.Write(uint2Bytes2(hour))
buf.WriteByte(':')
buf.Write(uint2Bytes2(min))
buf.WriteByte(':')
buf.Write(uint2Bytes2(sec))
t.time = buf.String()
}
func (t *FastTimer) start() {
buf := bytes.NewBuffer(make([]byte, 0, dateTimeBufSize))
t.update(now(), buf)
t.isRunning = true
t.stopChan = make(chan struct{})
go func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case tm := <-ticker.C:
t.update(tm, buf)
case <-t.stopChan:
defer func() { recover() }() // t.stopChan might be closed twice during exiting
close(t.stopChan)
return
}
}
}()
}
func (t *FastTimer) stop() {
if t.isRunning {
defer func() { recover() }() // t.stopChan might be closed during exiting
t.stopChan <- struct{}{}
t.isRunning = false
}
}
// StartFastTimer starts the fastTimer.
func StartFastTimer() {
fastTimer.start()
}
// StopFastTimer stops the fastTimer.
func StopFastTimer() {
fastTimer.stop()
}
//go:noescape
//go:linkname runtime_procPin runtime.procPin
func runtime_procPin() int
//go:noescape
//go:linkname runtime_procUnpin runtime.procUnpin
func runtime_procUnpin()