-
Notifications
You must be signed in to change notification settings - Fork 1
/
diskstat.go
144 lines (128 loc) · 5.18 KB
/
diskstat.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) 2020. aberic - All Rights Reserved.
*
* 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 proc
import (
"github.com/aberic/gnomon"
"io/ioutil"
"strconv"
"strings"
"sync"
"time"
)
var (
diskStatInstance *DiskStats
diskStatInstanceOnce sync.Once
)
// DiskStat is disk statistics to help measure disk activity.
//
// Note:
// * On a very busy or long-lived system values may wrap.
// * No kernel locks are held while modifying these counters. This implies that
// minor inaccuracies may occur.
//
// More more info see:
// https://www.kernel.org/doc/Documentation/iostats.txt and
// https://www.kernel.org/doc/Documentation/block/stat.txt
type DiskStat struct {
Major int `json:"major"` // major device number
Minor int `json:"minor"` // minor device number
Name string `json:"name"` // device name
ReadIOs uint64 `json:"read_ios"` // number of read I/Os processed
ReadMerges uint64 `json:"read_merges"` // number of read I/Os merged with in-queue I/O
ReadSectors uint64 `json:"read_sectors"` // number of 512 byte sectors read
ReadTicks uint64 `json:"read_ticks"` // total wait time for read requests in milliseconds
WriteIOs uint64 `json:"write_ios"` // number of write I/Os processed
WriteMerges uint64 `json:"write_merges"` // number of write I/Os merged with in-queue I/O
WriteSectors uint64 `json:"write_sectors"` // number of 512 byte sectors written
WriteTicks uint64 `json:"write_ticks"` // total wait time for write requests in milliseconds
InFlight uint64 `json:"in_flight"` // number of I/Os currently in flight
IOTicks uint64 `json:"io_ticks"` // total time this block device has been active in milliseconds
TimeInQueue uint64 `json:"time_in_queue"` // total wait time for all requests in milliseconds
}
type DiskStats struct {
diskStatArr []DiskStat
}
func obtainDiskStats() *DiskStats {
diskStatInstanceOnce.Do(func() {
if nil == diskStatInstance {
diskStatInstance = &DiskStats{diskStatArr: []DiskStat{}}
}
})
return diskStatInstance
}
// Info DiskStats 对象
func (ds *DiskStats) Info() error {
return ds.readDiskStats(gnomon.StringBuild(FileRootPath(), "/diskstats"))
}
// ReadDiskStats reads and parses the file.
//
// Note:
// * Assumes a well formed file and will panic if it isn't.
func (ds *DiskStats) readDiskStats(filePath string) error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
devices := strings.Split(string(data), "\n")
devicesLen := len(devices)
statArrLen := len(ds.diskStatArr)
if devicesLen > statArrLen {
lenDis := devicesLen - statArrLen
ds.diskStatArr = append(ds.diskStatArr, make([]DiskStat, lenDis)...)
}
for i := range ds.diskStatArr {
fields := strings.Fields(devices[i])
Major, _ := strconv.ParseInt(fields[0], 10, strconv.IntSize)
ds.diskStatArr[i].Major = int(Major)
Minor, _ := strconv.ParseInt(fields[1], 10, strconv.IntSize)
ds.diskStatArr[i].Minor = int(Minor)
ds.diskStatArr[i].Name = fields[2]
ds.diskStatArr[i].ReadIOs, _ = strconv.ParseUint(fields[3], 10, 64)
ds.diskStatArr[i].ReadMerges, _ = strconv.ParseUint(fields[4], 10, 64)
ds.diskStatArr[i].ReadSectors, _ = strconv.ParseUint(fields[5], 10, 64)
ds.diskStatArr[i].ReadTicks, _ = strconv.ParseUint(fields[6], 10, 64)
ds.diskStatArr[i].WriteIOs, _ = strconv.ParseUint(fields[7], 10, 64)
ds.diskStatArr[i].WriteMerges, _ = strconv.ParseUint(fields[8], 10, 64)
ds.diskStatArr[i].WriteSectors, _ = strconv.ParseUint(fields[9], 10, 64)
ds.diskStatArr[i].WriteTicks, _ = strconv.ParseUint(fields[10], 10, 64)
ds.diskStatArr[i].InFlight, _ = strconv.ParseUint(fields[11], 10, 64)
ds.diskStatArr[i].IOTicks, _ = strconv.ParseUint(fields[12], 10, 64)
ds.diskStatArr[i].TimeInQueue, _ = strconv.ParseUint(fields[13], 10, 64)
}
return nil
}
// GetReadBytes 返回读取的字节数
func (d *DiskStat) GetReadBytes() int64 {
return int64(d.ReadSectors) * 512
}
// GetReadTicks 返回等待读取请求的持续时间
func (d *DiskStat) GetReadTicks() time.Duration {
return time.Duration(d.ReadTicks) * time.Millisecond
}
// GetWriteBytes 返回写入的字节数
func (d *DiskStat) GetWriteBytes() int64 {
return int64(d.WriteSectors) * 512
}
// GetWriteTicks 返回写请求等待的持续时间
func (d *DiskStat) GetWriteTicks() time.Duration {
return time.Duration(d.WriteTicks) * time.Millisecond
}
// GetIOTicks 返回磁盘一直处于活动状态的持续时间
func (d *DiskStat) GetIOTicks() time.Duration {
return time.Duration(d.IOTicks) * time.Millisecond
}
// GetTimeInQueue 返回所有请求等待的持续时间
func (d *DiskStat) GetTimeInQueue() time.Duration {
return time.Duration(d.TimeInQueue) * time.Millisecond
}