@@ -21,6 +21,39 @@ func init() {
21
21
flag .Parse ()
22
22
}
23
23
24
+ func linearGrow (data []byte , interval time.Duration , length int , startTime time.Time , timeLine int ) {
25
+ fmt .Println (interval )
26
+ sysPageSize := os .Getpagesize ()
27
+ minPageQuantity := int (time .Millisecond * 100 / interval )
28
+ pageCount := 0
29
+ resLength := length
30
+ for i := 0 ; i < length ; i += sysPageSize {
31
+ data [i ] = 1
32
+ if minPageQuantity > 0 {
33
+ pageCount += 1
34
+ acculatedPage := pageCount % minPageQuantity
35
+ if acculatedPage == 0 {
36
+ time .Sleep (time .Duration (minPageQuantity ) * interval )
37
+ resLength = length - i
38
+ interval = updateInterval (timeLine - int (time .Since (startTime )), resLength )
39
+ }
40
+ } else {
41
+ time .Sleep (interval )
42
+ }
43
+ }
44
+
45
+ resTime := time .Duration (resLength / sysPageSize * int (interval ))
46
+ if resTime > 100 * time .Millisecond {
47
+ time .Sleep (resTime )
48
+ }
49
+ }
50
+
51
+ func updateInterval (timeLine int , length int ) time.Duration {
52
+ sysPageSize := os .Getpagesize ()
53
+ interval := time .Duration (timeLine ) / time .Duration (length / sysPageSize )
54
+ return interval
55
+ }
56
+
24
57
func main () {
25
58
memSize = strings .ToUpper (memSize )
26
59
length , err := strconv .Atoi (memSize [:len (memSize )- 2 ])
@@ -45,9 +78,17 @@ func main() {
45
78
// TODO
46
79
print (err )
47
80
}
48
- // timeUnit := growthTime[len(growthTime)]
49
- // if timeUnit =
50
- timeLine = int (time .Second ) * timeLine
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
+
51
92
52
93
data , err := syscall .Mmap (- 1 , 0 , length , syscall .PROT_READ | syscall .PROT_WRITE , syscall .MAP_PRIVATE | syscall .MAP_ANONYMOUS )
53
94
if err != nil {
@@ -56,17 +97,15 @@ func main() {
56
97
}
57
98
58
99
sysPageSize := os .Getpagesize ()
59
- duration := time .Duration (timeLine ) / time .Duration (length / sysPageSize )
60
- fmt .Println (duration , length / sysPageSize )
61
- fmt .Println (time .Now ())
62
- for i := 0 ; i < length ; i += sysPageSize {
63
- // fmt.Println(time.Now())
64
- data [i ] = 1
65
- time .Sleep (duration )
100
+ interval := time .Duration (timeLine ) / time .Duration (length / sysPageSize )
101
+
102
+ if interval > time .Nanosecond {
103
+ linearGrow (data , interval , length , time .Now (), timeLine )
104
+ } else {
105
+ for i := 0 ; i < length ; i += sysPageSize {
106
+ data [i ] = 1
107
+ }
66
108
}
67
- fmt .Println (time .Now ())
68
- time .Sleep (time .Microsecond * 1 )
69
- fmt .Println (time .Now ())
70
109
for {
71
110
time .Sleep (time .Second * 2 )
72
111
}
0 commit comments