forked from ngaut/sqltop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.go
153 lines (131 loc) · 3.01 KB
/
stat.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
package main
import (
"database/sql"
"fmt"
"strings"
"sync"
"time"
"code.cloudfoundry.org/bytefmt"
)
var (
stat *sync.Map
once sync.Once
)
var (
kProcessListSQL = `SELECT
ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO
FROM
PROCESSLIST
WHERE
COMMAND != 'Sleep'
ORDER BY TIME DESC LIMIT %d`
kIOStatSQL = `SELECT
table_name, index_name, SUM(written_bytes) AS w, SUM(read_bytes) AS r
FROM
TIKV_REGION_STATUS
WHERE
db_name != "INFORMATION_SCHEMA" AND db_name != "PERFORMANCE_SCHEMA" AND db_name != "mysql"
GROUP BY table_name, index_name ORDER BY w DESC LIMIT 10`
)
// stat keys
const (
TOTAL_PROCESSES = "total_processes"
USING_DBS = "using_dbs"
TOTAL_READ = "total_read"
TOTAL_WRITE = "total_write"
TABLES_IO_STATUS = "table_io_status"
PROCESS_LIST = "process_list"
)
func Stat() *sync.Map {
once.Do(func() {
stat = &sync.Map{}
})
return stat
}
type ProcessRecord struct {
id, time int
user, host, command string
dbName, state, sqlText sql.NullString
}
type TableRegionStatus struct {
tableName, indexName sql.NullString
wbytes uint64
rbytes uint64
}
func (r TableRegionStatus) String() string {
return fmt.Sprintf("Table: %-20s Index: %-20s Write: %-10s Read: %-10s",
r.tableName.String, r.indexName.String, bytefmt.ByteSize(r.wbytes), bytefmt.ByteSize(r.rbytes))
}
func refresh() {
// update process info
if err := refreshProcessList(); err != nil {
cleanExit(err)
}
if err := refreshIOStat(); err != nil {
cleanExit(err)
}
}
func refreshProcessList() error {
q := fmt.Sprintf(kProcessListSQL, Config().NumProcessToShow)
rows, err := DB().Query(q)
if err != nil {
return err
}
defer rows.Close()
totalProcesses := 0
usingDBs := make(map[string]struct{})
var records []ProcessRecord
for rows.Next() {
var r ProcessRecord
err := rows.Scan(&r.id, &r.user, &r.host, &r.dbName, &r.command, &r.time, &r.state, &r.sqlText)
if err != nil {
return err
}
if r.dbName.Valid {
usingDBs[strings.ToLower(r.dbName.String)] = struct{}{}
}
records = append(records, r)
totalProcesses++
}
err = rows.Err()
if err != nil {
return err
}
Stat().Store(TOTAL_PROCESSES, totalProcesses)
Stat().Store(USING_DBS, len(usingDBs))
Stat().Store(PROCESS_LIST, records)
return nil
}
func refreshIOStat() error {
rows, err := DB().Query(kIOStatSQL)
if err != nil {
return err
}
defer rows.Close()
var records []TableRegionStatus
var totalRead, totalWrite uint64
for rows.Next() {
var r TableRegionStatus
err := rows.Scan(&r.tableName, &r.indexName, &r.wbytes, &r.rbytes)
if err != nil {
return err
}
records = append(records, r)
totalRead += r.rbytes
totalWrite += r.wbytes
}
err = rows.Err()
if err != nil {
return err
}
Stat().Store(TOTAL_READ, totalRead)
Stat().Store(TOTAL_WRITE, totalWrite)
Stat().Store(TABLES_IO_STATUS, records)
return nil
}
func refreshWorker() {
for {
refresh()
time.Sleep(1 * time.Second)
}
}