-
Notifications
You must be signed in to change notification settings - Fork 21
/
disk-manager.go
86 lines (66 loc) · 1.48 KB
/
disk-manager.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
package main
import (
"fmt"
"time"
"github.com/shirou/gopsutil/process"
)
type DiskManager struct{}
type Process struct {
Pid int32
Name string
}
func createDiskManager() *DiskManager {
return &DiskManager{}
}
func getProcessByPID(pid int32) (*process.Process, error) {
processes, err := process.Processes()
if err != nil {
return nil, err
}
for _, p := range processes {
if p.Pid == pid {
return p, nil
}
}
return nil, fmt.Errorf("No process found with PID: %d", pid)
}
func (d *DiskManager) GetDiskWriteSpeed(processPID int32) (float64, error) {
process, err := getProcessByPID(processPID)
if err != nil {
return 0, err
}
initialIOCounters, err := process.IOCounters()
if err != nil {
return 0, err
}
time.Sleep(sampleInterval)
finalIOCounters, err := process.IOCounters()
if err != nil {
return 0, err
}
bytesWritten := finalIOCounters.WriteBytes - initialIOCounters.WriteBytes
writeDuration := sampleInterval.Seconds()
if writeDuration == 0 {
return 0, nil
}
writeSpeed := float64(bytesWritten) / writeDuration
return writeSpeed, nil
}
func (d *DiskManager) GetProcesses() ([]*Process, error) {
var processList []*Process = make([]*Process, 0)
processes, err := process.Processes()
if err != nil {
return nil, err
}
for _, process := range processes {
name, err := process.Name()
if err != nil {
return nil, err
}
processList = append(processList, &Process{
Pid: process.Pid,
Name: name,
})
}
return processList, nil
}