forked from rai-project/joefriday
/
membasic_unix.go
278 lines (262 loc) · 7.38 KB
/
membasic_unix.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2016 Joel Scoble and The JoeFriday authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package membasic processes a subset of the /proc/meminfo file. Instead of
// returning a Go struct, it returns Flatbuffer serialized bytes. A function to
// deserialize the Flatbuffer serialized bytes into a membasic.Info struct is
// provided. For more detailed information about a system's memory, use the
// meminfo package.
//
// Note: the package name is membasic and not the final element of the import
// path (flat).
package membasic
import (
"io"
"sync"
"time"
fb "github.com/google/flatbuffers/go"
joe "github.com/c3sr/joefriday"
"github.com/c3sr/joefriday/helpers"
basic "github.com/c3sr/joefriday/mem/membasic"
"github.com/c3sr/joefriday/mem/membasic/flat/structs"
)
// Profiler is used to get the basic memory information as Flatbuffer
// serialized by processing the /proc/meminfo file.
type Profiler struct {
*basic.Profiler
*fb.Builder
}
// Returns an initialized Profiler that utilizes FlatBuffers; ready to use.
func NewProfiler() (prof *Profiler, err error) {
p, err := basic.NewProfiler()
if err != nil {
return nil, err
}
return &Profiler{Profiler: p, Builder: fb.NewBuilder(0)}, nil
}
// Get returns the current basic memory information as Flatbuffer serialized
// bytes.
func (prof *Profiler) Get() ([]byte, error) {
inf, err := prof.Profiler.Get()
if err != nil {
return nil, err
}
return prof.Serialize(inf), nil
}
var std *Profiler
var stdMu sync.Mutex //protects standard to prevent a data race on checking/instantiation
// Get returns the current basic memory information as Flatbuffer serialized
// bytes using the package's global Profiler.
func Get() (p []byte, err error) {
stdMu.Lock()
defer stdMu.Unlock()
if std == nil {
std, err = NewProfiler()
if err != nil {
return nil, err
}
}
return std.Get()
}
// Serialize the basic memory information using Flatbuffers.
func (prof *Profiler) Serialize(inf *basic.Info) []byte {
// ensure the Builder is in a usable state.
prof.Builder.Reset()
structs.InfoStart(prof.Builder)
structs.InfoAddTimestamp(prof.Builder, inf.Timestamp)
structs.InfoAddActive(prof.Builder, inf.Active)
structs.InfoAddInactive(prof.Builder, inf.Inactive)
structs.InfoAddMapped(prof.Builder, inf.Mapped)
structs.InfoAddMemAvailable(prof.Builder, inf.MemAvailable)
structs.InfoAddMemFree(prof.Builder, inf.MemFree)
structs.InfoAddMemTotal(prof.Builder, inf.MemTotal)
structs.InfoAddSwapCached(prof.Builder, inf.SwapCached)
structs.InfoAddSwapFree(prof.Builder, inf.SwapFree)
structs.InfoAddSwapTotal(prof.Builder, inf.SwapTotal)
prof.Builder.Finish(structs.InfoEnd(prof.Builder))
p := prof.Builder.Bytes[prof.Builder.Head():]
// copy them (otherwise gets lost in reset)
tmp := make([]byte, len(p))
copy(tmp, p)
return tmp
}
// Serialize the basic memory information using Flatbuffers with the package's
// global Profiler.
func Serialize(inf *basic.Info) (p []byte, err error) {
stdMu.Lock()
defer stdMu.Unlock()
if std == nil {
std, err = NewProfiler()
if err != nil {
return nil, err
}
}
return std.Serialize(inf), nil
}
// Deserialize takes some Flatbuffer serialized bytes and deserializes them
// as membasic.Info.
func Deserialize(p []byte) *basic.Info {
infoFlat := structs.GetRootAsInfo(p, 0)
info := &basic.Info{}
info.Timestamp = infoFlat.Timestamp()
info.Active = infoFlat.Active()
info.Inactive = infoFlat.Inactive()
info.Mapped = infoFlat.Mapped()
info.MemAvailable = infoFlat.MemAvailable()
info.MemFree = infoFlat.MemFree()
info.MemTotal = infoFlat.MemTotal()
info.SwapCached = infoFlat.SwapCached()
info.SwapFree = infoFlat.SwapFree()
info.SwapTotal = infoFlat.SwapTotal()
return info
}
// Ticker delivers the system's basic memory information at intervals.
type Ticker struct {
*joe.Ticker
Data chan []byte
*Profiler
}
// NewTicker returns a new Ticker containing a Data channel that delivers the
// data at intervals and an error channel that delivers any errors encountered.
// Stop the ticker to signal the ticker to stop running. Stopping the ticker
// does not close the Data channel; call Close to close both the ticker and the
// data channel.
func NewTicker(d time.Duration) (joe.Tocker, error) {
p, err := NewProfiler()
if err != nil {
return nil, err
}
t := Ticker{Ticker: joe.NewTicker(d), Data: make(chan []byte), Profiler: p}
go t.Run()
return &t, nil
}
// Run runs the ticker.
func (t *Ticker) Run() {
// predeclare some vars
var (
i, pos, nameLen int
v byte
n uint64
err error
)
// ticker
Tick:
for {
select {
case <-t.Done:
return
case <-t.C:
t.Builder.Reset()
err = t.Profiler.Profiler.Reset()
if err != nil {
t.Errs <- err
continue
}
structs.InfoStart(t.Builder)
structs.InfoAddTimestamp(t.Builder, time.Now().UTC().UnixNano())
for {
t.Val = t.Val[:0]
t.Line, err = t.ReadSlice('\n')
if err != nil {
if err == io.EOF {
break
}
// An error results in sending error message and stop processing of this tick.
t.Errs <- &joe.ReadError{Err: err}
continue Tick
}
// first grab the key name (everything up to the ':')
for i, v = range t.Line {
if v == 0x3A {
t.Val = t.Line[:i]
pos = i + 1 // skip the :
break
}
}
nameLen = len(t.Val)
// skip all spaces
for i, v = range t.Line[pos:] {
if v != 0x20 {
pos += i
break
}
}
// grab the numbers
for _, v = range t.Line[pos:] {
if v == 0x20 || v == '\n' {
break
}
t.Val = append(t.Val, v)
}
// any conversion error results in 0
n, err = helpers.ParseUint(t.Val[nameLen:])
if err != nil {
t.Errs <- &joe.ParseError{Info: string(t.Val[:nameLen]), Err: err}
continue
}
v = t.Val[0]
if v == 'A' {
if t.Val[5] == 'e' && nameLen == 6 {
structs.InfoAddActive(t.Builder, n)
}
continue
}
if v == 'I' {
if nameLen == 8 {
structs.InfoAddInactive(t.Builder, n)
}
continue
}
if v == 'M' {
v = t.Val[3]
if nameLen < 8 {
if v == 'p' {
structs.InfoAddMapped(t.Builder, n)
continue
}
if v == 'F' {
structs.InfoAddMemFree(t.Builder, n)
}
continue
}
if v == 'A' {
structs.InfoAddMemAvailable(t.Builder, n)
continue
}
structs.InfoAddMemTotal(t.Builder, n)
continue
}
if v == 'S' {
v = t.Val[1]
if v == 'w' {
if t.Val[4] == 'C' {
structs.InfoAddSwapCached(t.Builder, n)
continue
}
if t.Val[4] == 'F' {
structs.InfoAddSwapFree(t.Builder, n)
continue
}
structs.InfoAddSwapTotal(t.Builder, n)
}
}
}
t.Builder.Finish(structs.InfoEnd(t.Builder))
t.Data <- t.Profiler.Builder.Bytes[t.Builder.Head():]
}
}
}
// Close closes the ticker resources.
func (t *Ticker) Close() {
t.Ticker.Close()
close(t.Data)
}