@@ -4,10 +4,9 @@ import (
4
4
"flag"
5
5
"fmt"
6
6
"os"
7
- "strconv"
8
- "strings"
9
7
"syscall"
10
8
"time"
9
+ humanize "github.com/dustin/go-humanize"
11
10
)
12
11
13
12
var (
@@ -21,91 +20,68 @@ func init() {
21
20
flag .Parse ()
22
21
}
23
22
24
- func linearGrow (data []byte , interval time.Duration , length int , startTime time.Time , timeLine int ) {
23
+ func linearGrow (data []byte , interval time.Duration , length uint64 , startTime time.Time , timeLine time. Duration ) {
25
24
fmt .Println (interval )
26
25
sysPageSize := os .Getpagesize ()
27
26
minPageQuantity := int (time .Millisecond * 100 / interval )
28
27
pageCount := 0
29
28
resLength := length
30
- for i := 0 ; i < length ; i += sysPageSize {
29
+ for i := 0 ; uint64 ( i ) < length ; i += sysPageSize {
31
30
data [i ] = 1
32
31
if minPageQuantity > 0 {
33
32
pageCount += 1
34
33
acculatedPage := pageCount % minPageQuantity
35
34
if acculatedPage == 0 {
36
35
time .Sleep (time .Duration (minPageQuantity ) * interval )
37
- resLength = length - i
38
- interval = updateInterval (timeLine - int ( time .Since (startTime ) ), resLength )
36
+ resLength = length - uint64 ( i )
37
+ interval = updateInterval (timeLine - time .Since (startTime ), resLength )
39
38
}
40
39
} else {
41
40
time .Sleep (interval )
42
41
}
43
42
}
44
43
45
- resTime := time .Duration (resLength / sysPageSize * int (interval ))
44
+ resTime := time .Duration (resLength / uint64 ( sysPageSize ) * uint64 (interval ))
46
45
if resTime > 100 * time .Millisecond {
47
46
time .Sleep (resTime )
48
47
}
49
48
}
50
49
51
- func updateInterval (timeLine int , length int ) time.Duration {
52
- sysPageSize := os .Getpagesize ()
50
+ func updateInterval (timeLine time. Duration , length uint64 ) time.Duration {
51
+ sysPageSize := uint64 ( os .Getpagesize () )
53
52
interval := time .Duration (timeLine ) / time .Duration (length / sysPageSize )
54
53
return interval
55
54
}
56
55
57
56
func main () {
58
- memSize = strings .ToUpper (memSize )
59
- length , err := strconv .Atoi (memSize [:len (memSize )- 2 ])
57
+ length , err := humanize .ParseBytes (memSize )
60
58
if err != nil {
61
59
// TODO
62
60
print (err )
63
61
}
64
- sizeUnit := memSize [len (memSize )- 2 :]
65
- if sizeUnit == "KB" {
66
- length *= 1024
67
- } else if sizeUnit == "MB" {
68
- length *= 1024 * 1024
69
- } else if sizeUnit == "GB" {
70
- length *= 1024 * 1024 * 1024
71
- } else {
72
- // TODO
73
- }
74
62
75
- growthTime = strings .ToLower (growthTime )
76
- timeLine , err := strconv .Atoi (growthTime [:len (growthTime )- 1 ])
63
+ timeLine , err := time .ParseDuration (growthTime )
77
64
if err != nil {
78
65
// TODO
79
- print (err )
80
66
}
81
- timeUnit := growthTime [len (growthTime )- 1 :]
82
- if timeUnit == "s" {
83
- timeLine = int (time .Second ) * timeLine
84
- } else if timeUnit == "m" {
85
- timeLine = int (time .Minute ) * timeLine
86
- } else if timeUnit == "h" {
87
- timeLine = int (time .Hour ) * timeLine
88
- } else {
89
- // TODO
90
- }
91
-
92
67
93
- data , err := syscall .Mmap (- 1 , 0 , length , syscall .PROT_READ | syscall .PROT_WRITE , syscall .MAP_PRIVATE | syscall .MAP_ANONYMOUS )
68
+ data , err := syscall .Mmap (- 1 , 0 , int ( length ) , syscall .PROT_READ | syscall .PROT_WRITE , syscall .MAP_PRIVATE | syscall .MAP_ANONYMOUS )
94
69
if err != nil {
95
70
// TODO
96
71
print (err )
97
72
}
98
73
99
74
sysPageSize := os .Getpagesize ()
100
- interval := time .Duration (timeLine ) / time .Duration (length / sysPageSize )
75
+ interval := time .Duration (timeLine ) / time .Duration (length / uint64 ( sysPageSize ) )
101
76
102
77
if interval > time .Nanosecond {
103
78
linearGrow (data , interval , length , time .Now (), timeLine )
104
79
} else {
105
- for i := 0 ; i < length ; i += sysPageSize {
80
+ for i := 0 ; uint64 ( i ) < length ; i += sysPageSize {
106
81
data [i ] = 1
107
82
}
108
83
}
84
+
109
85
for {
110
86
time .Sleep (time .Second * 2 )
111
87
}
0 commit comments