@@ -2,10 +2,10 @@ package main
2
2
3
3
import (
4
4
"flag"
5
- "fmt"
6
5
"os"
7
6
"syscall"
8
7
"time"
8
+
9
9
humanize "github.com/dustin/go-humanize"
10
10
)
11
11
@@ -20,37 +20,33 @@ func init() {
20
20
flag .Parse ()
21
21
}
22
22
23
- func linearGrow (data []byte , interval time.Duration , length uint64 , startTime time.Time , timeLine time.Duration ) {
24
- fmt .Println (interval )
25
- sysPageSize := os .Getpagesize ()
26
- minPageQuantity := int (time .Millisecond * 100 / interval )
27
- pageCount := 0
28
- resLength := length
29
- for i := 0 ; uint64 (i ) < length ; i += sysPageSize {
30
- data [i ] = 1
31
- if minPageQuantity > 0 {
32
- pageCount += 1
33
- acculatedPage := pageCount % minPageQuantity
34
- if acculatedPage == 0 {
35
- time .Sleep (time .Duration (minPageQuantity ) * interval )
36
- resLength = length - uint64 (i )
37
- interval = updateInterval (timeLine - time .Since (startTime ), resLength )
38
- }
23
+ func linearGrow (data []byte , length uint64 , timeLine time.Duration ) {
24
+ startTime := time .Now ()
25
+ endTime := startTime .Add (timeLine )
26
+
27
+ var allocated uint64 = 0
28
+ pageSize := uint64 (syscall .Getpagesize ())
29
+ interval := time .Millisecond * 10
30
+
31
+ for {
32
+ now := time .Now ()
33
+ if now .After (endTime ) {
34
+ now = endTime
35
+ }
36
+ expected := length * uint64 (now .Sub (startTime ).Milliseconds ()) / uint64 (endTime .Sub (startTime ).Milliseconds ()) / pageSize
37
+
38
+ for i := allocated ; uint64 (i ) < expected ; i ++ {
39
+ data [uint64 (i )* pageSize ] = 0
40
+ }
41
+
42
+ allocated = expected
43
+ if now .Equal (endTime ) {
44
+ break
39
45
} else {
40
46
time .Sleep (interval )
41
47
}
42
48
}
43
49
44
- resTime := time .Duration (resLength / uint64 (sysPageSize ) * uint64 (interval ))
45
- if resTime > 100 * time .Millisecond {
46
- time .Sleep (resTime )
47
- }
48
- }
49
-
50
- func updateInterval (timeLine time.Duration , length uint64 ) time.Duration {
51
- sysPageSize := uint64 (os .Getpagesize ())
52
- interval := time .Duration (timeLine ) / time .Duration (length / sysPageSize )
53
- return interval
54
50
}
55
51
56
52
func main () {
@@ -71,17 +67,15 @@ func main() {
71
67
print (err )
72
68
}
73
69
74
- sysPageSize := os .Getpagesize ()
75
- interval := time .Duration (timeLine ) / time .Duration (length / uint64 (sysPageSize ))
76
-
77
- if interval > time .Nanosecond {
78
- linearGrow (data , interval , length , time .Now (), timeLine )
70
+ if timeLine > time .Nanosecond {
71
+ linearGrow (data , length , timeLine )
79
72
} else {
73
+ sysPageSize := os .Getpagesize ()
80
74
for i := 0 ; uint64 (i ) < length ; i += sysPageSize {
81
75
data [i ] = 1
82
76
}
83
77
}
84
-
78
+
85
79
for {
86
80
time .Sleep (time .Second * 2 )
87
81
}
0 commit comments