-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
258 lines (232 loc) · 6.24 KB
/
main.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
package main
import (
crypto_rand "crypto/rand"
"encoding/binary"
"fmt"
"io"
"log"
math_rand "math/rand"
"os"
"path"
"path/filepath"
"strconv"
"sync/atomic"
"syscall"
"time"
)
const TableSize = 4096
const RandDataSize = 4096
const StatRecordSize = 10
type RandDataProvider struct {
mprngData *math_rand.Rand
mprngIndex *math_rand.Rand
tableUsage [TableSize]int32
tableData [TableSize][]byte
chanRefill chan int
refillThreshold int32
}
func (c *RandDataProvider) Close() error {
close(c.chanRefill)
return nil
}
func (c *RandDataProvider) shouldRefill(count int32) bool {
return count > c.refillThreshold
}
func (c *RandDataProvider) UseRandData() []byte {
idx := c.mprngIndex.Intn(TableSize)
count := atomic.AddInt32(&c.tableUsage[idx], 1)
if c.shouldRefill(count) {
c.chanRefill <- idx
}
return c.tableData[idx]
}
func newRandDataProvider() *RandDataProvider {
rdp := &RandDataProvider{}
rdp.chanRefill = make(chan int, TableSize)
bufMprngSeed := make([]byte, 8)
seedMprng := func(mprng *math_rand.Rand) {
_, err := io.ReadFull(crypto_rand.Reader, bufMprngSeed)
if err != nil {
log.Printf("can not read random data: %v", err)
}
seed := binary.LittleEndian.Uint64(bufMprngSeed)
mprng.Seed(int64(seed))
}
fillRandData := func(i int) {
seedMprng(rdp.mprngData)
_, err := io.ReadFull(rdp.mprngData, rdp.tableData[i])
if err != nil {
log.Printf("can not read random data: %v", err)
}
rdp.tableUsage[i] = 0
}
refillLoop := func() {
stateCount := 0
for i := range rdp.chanRefill {
if len(rdp.chanRefill) > TableSize/2 {
stateCount++
if stateCount > TableSize {
stateCount = 0
atomic.AddInt32(&rdp.refillThreshold, 1)
}
} else {
stateCount++
if stateCount > TableSize {
stateCount = 0
if rdp.refillThreshold > 0 {
atomic.AddInt32(&rdp.refillThreshold, -1)
}
}
}
if rdp.shouldRefill(rdp.tableUsage[i]) {
fillRandData(i)
}
}
}
rdp.mprngIndex = math_rand.New(math_rand.NewSource(0))
seedMprng(rdp.mprngIndex)
rdp.mprngData = math_rand.New(math_rand.NewSource(0))
seedMprng(rdp.mprngData)
for i := 0; i < TableSize; i++ {
rdp.tableData[i] = make([]byte, RandDataSize)
fillRandData(i)
}
go refillLoop()
return rdp
}
func getDiskSpaceAvail(path string) uint64 {
fs := syscall.Statfs_t{}
err := syscall.Statfs(path, &fs)
if err != nil {
return 0
}
return fs.Bavail * uint64(fs.Bsize)
}
func formatSize(size uint64) string {
v := float64(size/1024) / 1024.0
u := "MiB"
if v > 1024 {
v = v / 1024
u = "GiB"
}
if v > 1024 {
v = v / 1024
u = "TiB"
}
if v > 1024 {
v = v / 1024
u = "PiB"
}
return fmt.Sprintf("%.2f %s", v, u)
}
func main() {
if len(os.Args) < 3 {
fmt.Printf("Usage: %s {count} {file} [size]\n", os.Args[0])
os.Exit(1)
}
fillCount, _ := strconv.Atoi(os.Args[1])
if fillCount <= 0 {
fmt.Printf("invalid count\n")
os.Exit(1)
}
filePath := os.Args[2]
fileDir := path.Dir(filePath)
if st, err := os.Stat(fileDir); err != nil || !st.IsDir() {
fmt.Printf("invalid parent directory for %s\n", filePath)
os.Exit(1)
}
if _, err := os.Stat(filePath); err == nil || !os.IsNotExist(err) {
fmt.Printf("invalid fill file, already exists: %s\n", filePath)
os.Exit(1)
}
var fillSize uint64
if len(os.Args) >= 4 {
var err error
fillSize, err = strconv.ParseUint(os.Args[3], 10, 64)
if fillSize == 0 || err != nil {
fmt.Printf("invalid fill size, err=%v\n", err)
os.Exit(1)
}
}
diskAvailSize := getDiskSpaceAvail(fileDir)
log.Printf("fill %d times to file: %s, disk avail=%s", fillCount, filePath, formatSize(diskAvailSize))
if fillSize > 0 {
log.Printf("fill up to %s bytes", formatSize(fillSize))
}
type statRecord struct {
recordTime time.Time
writtenBytes uint64
}
statRecords := make([]statRecord, 0)
rdp := newRandDataProvider()
for fillStep := 1; fillStep <= fillCount; fillStep++ {
log.Printf("fill step %d, write to %s", fillStep, filePath)
f, err := os.Create(filePath)
if err != nil {
log.Panicf("can not create file: %s, err=%v", filePath, err)
}
writtenTotal := uint64(0)
statRecords = append(statRecords, statRecord{time.Now(), writtenTotal})
loop := true
for loop {
d := rdp.UseRandData()
var n int
n, err = f.Write(d)
if err != nil {
log.Printf("fill step %d stops, err=%v", fillStep, err)
break
}
writtenTotal += uint64(n)
now := time.Now()
dur := now.Sub(statRecords[len(statRecords)-1].recordTime)
if dur > time.Second {
statRecords = append(statRecords, statRecord{now, writtenTotal})
speed := float64(writtenTotal-statRecords[len(statRecords)-2].writtenBytes) / dur.Seconds()
msg := fmt.Sprintf("step: %d, speed: %s/s (written: %s) ...", fillStep, formatSize(uint64(speed)), formatSize(writtenTotal))
if len(statRecords) >= StatRecordSize {
durAvg := now.Sub(statRecords[0].recordTime)
speedAvg := float64(writtenTotal-statRecords[0].writtenBytes) / durAvg.Seconds()
var remainingSize uint64
var remainingRatio float64
if fillSize > 0 {
remainingSize = fillSize - writtenTotal
remainingRatio = float64(remainingSize) / float64(fillSize)
} else if diskAvailSize > 0 {
remainingSize = diskAvailSize - writtenTotal
remainingRatio = float64(remainingSize) / float64(diskAvailSize)
}
if speedAvg > 0 {
msg = msg + ", estimated time: " + (time.Duration(remainingSize/uint64(speedAvg)) * time.Second).String()
} else {
msg = msg + ", no speed"
}
if speedAvg < 50*1024 && remainingRatio < 0.001 {
msg = msg + ", nearly fill to be written in"
loop = false
}
statRecords = statRecords[1:]
}
print("\033[2K\r", msg)
}
if fillSize > 0 {
if writtenTotal >= fillSize {
loop = false
}
}
}
print("\n")
_ = f.Close()
filePathAbs, _ := filepath.Abs(filePath)
log.Printf("fill step %d written %s bytes to %s", fillStep, formatSize(writtenTotal), filePathAbs)
if fillStep != fillCount {
log.Printf("fill step %d removes file %s", fillStep, filePathAbs)
err = os.Remove(filePath)
if err != nil {
log.Panicf("can not remove file: %s, err=%v", filePath, err)
}
} else {
log.Printf("fill step %d (final) keeps file %s", fillStep, filePathAbs)
}
}
_ = rdp.Close()
}