This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
/
util.go
144 lines (126 loc) · 3.62 KB
/
util.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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cpustat
import (
"bytes"
"log"
"os"
"strconv"
"strings"
)
// crude protection against rollover. This will miss the last portion of the previous sample
// before the overflow, but capturing that is complicated because of the various number types
// involved and their inconsistent documentation.
func SafeSub(a, b uint64) uint64 {
if a < b {
return a
}
return a - b
}
func SafeSubFloat(a, b float64) float64 {
if a < b {
return a
}
return a - b
}
func ScaledSub(cur, prev uint64, scale float64) uint64 {
return uint64((float64(SafeSub(cur, prev)) * scale) + 0.5)
}
// note that this is not thread safe
var buf *bytes.Buffer
// ReadSmallFile is like os.ReadFile but dangerously optimized for reading files from /proc.
// The file is not statted first, and the same buffer is used every time.
func ReadSmallFile(filename string) ([]byte, error) {
f, err := os.Open(filename)
if err != nil {
f.Close()
return nil, err
}
if buf == nil {
buf = bytes.NewBuffer(make([]byte, 0, 8192))
} else {
buf.Reset()
}
_, err = buf.ReadFrom(f)
f.Close()
return buf.Bytes(), err
}
// ReadSmallFileStat is like ReadSmallFile except it also returns a FileInfo from os.Stat
func ReadSmallFileStat(filename string) ([]byte, os.FileInfo, error) {
f, err := os.Open(filename)
if err != nil {
f.Close()
return nil, nil, err
}
info, err := f.Stat()
if err != nil {
f.Close()
return nil, nil, err
}
if buf == nil {
buf = bytes.NewBuffer(make([]byte, 0, 8192))
} else {
buf.Reset()
}
_, err = buf.ReadFrom(f)
f.Close()
return buf.Bytes(), info, err
}
// Read a small file and split on newline
func ReadFileLines(filename string) ([]string, error) {
file, err := ReadSmallFile(filename)
if err != nil {
return nil, err
}
// TODO - these next two lines cause more GC than I expected
fileStr := strings.TrimSpace(string(file))
return strings.Split(fileStr, "\n"), nil
}
// pull a float64 out of a string
func ReadFloat(str string) float64 {
val, err := strconv.ParseFloat(str, 64)
if err != nil {
log.Fatal(err)
}
return val
}
// pull a uint64 out of a string
func ReadUInt(str string) uint64 {
val, err := strconv.ParseUint(str, 10, 64)
if err != nil {
panic(err)
}
return val
}
// pull an int64 out of a string
func ReadInt(str string) int64 {
val, err := strconv.ParseInt(str, 10, 64)
if err != nil {
log.Fatal(err)
}
return val
}
// remove grouping characters that confuse the termui parser
func StripSpecial(r rune) rune {
if r == '[' || r == ']' || r == '(' || r == ')' {
return -1
}
return r
}