-
Notifications
You must be signed in to change notification settings - Fork 4
/
mem_query.go
49 lines (44 loc) · 1.14 KB
/
mem_query.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
package psutilsql
import (
"github.com/noborus/trdsql"
"github.com/shirou/gopsutil/mem"
)
// VirtualMemoryReader returns mem.VirtualMemory result as trdsql.SliceReader.
func VirtualMemoryReader() (*trdsql.SliceReader, error) {
v, err := mem.VirtualMemory()
if err != nil {
return nil, err
}
return trdsql.NewSliceReader(psVirtualMemory, v), nil
}
// SwapMemoryReader returns mem.SwapMemory result as trdsql.SliceReader.
func SwapMemoryReader() (*trdsql.SliceReader, error) {
v, err := mem.SwapMemory()
if err != nil {
return nil, err
}
return trdsql.NewSliceReader(psSwapMemory, v), nil
}
// MEMQuery executes SQL on mem.VirtualMemory or mem.SwapMemory.
func MEMQuery(memory bool, query string, w trdsql.Writer) error {
if memory {
defaultQuery := "SELECT * FROM " + psVirtualMemory
if query == "" {
query = defaultQuery
}
reader, err := VirtualMemoryReader()
if err != nil {
return err
}
return readerExec(reader, query, w)
}
defaultQuery := "SELECT * FROM " + psSwapMemory
if query == "" {
query = defaultQuery
}
reader, err := SwapMemoryReader()
if err != nil {
return err
}
return readerExec(reader, query, w)
}