-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
135 lines (116 loc) · 2.4 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
package main
import (
"bufio"
"fmt"
"log"
"lruwsr/lfu"
"lruwsr/lru"
"lruwsr/lruwsr"
"lruwsr/simulator"
"os"
"strconv"
"strings"
"time"
)
func main() {
var (
traces []simulator.Trace = make([]simulator.Trace, 0)
simulator simulator.Simulator
timeStart time.Time
out *os.File
fs os.FileInfo
filePath string
outPath string
algorithm string
err error
cacheList []int
)
if len(os.Args) < 4 {
fmt.Println("program [algorithm (LFU|LRU|LRUWSR)] [file trace] [cache size]...")
os.Exit(1)
}
algorithm = os.Args[1]
filePath = os.Args[2]
if fs, err = os.Stat(filePath); os.IsNotExist(err) {
fmt.Printf("%v does not exists\n", filePath)
os.Exit(1)
}
cacheList, err = validateCacheSize(os.Args[3:])
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
traces, err = readFile(filePath)
if err != nil {
log.Fatalf("error reading file: %v", err)
}
outPath = fmt.Sprintf("%v_%v_%v.txt", time.Now().Unix(), algorithm, fs.Name())
out, err = os.Create("output" + "/" + outPath)
if err != nil {
log.Fatal(err.Error())
}
defer out.Close()
for _, cache := range cacheList {
switch strings.ToLower(algorithm) {
case "lfu":
simulator = lfu.NewLFU(cache)
case "lru":
simulator = lru.NewLRU(cache)
case "lruwsr":
simulator = lruwsr.NewLRUWSR(cache)
default:
log.Fatal("algorithm not supported")
}
timeStart = time.Now()
for _, trace := range traces {
err = simulator.Get(trace)
if err != nil {
log.Fatal(err.Error())
}
}
simulator.PrintToFile(out, timeStart)
}
fmt.Println("Done")
}
func validateCacheSize(tracesize []string) (sizeList []int, err error) {
var (
cacheList []int
cache int
)
for _, size := range tracesize {
cache, err = strconv.Atoi(size)
if err != nil {
return sizeList, err
}
cacheList = append(cacheList, cache)
}
return cacheList, nil
}
func readFile(filePath string) (traces []simulator.Trace, err error) {
var (
file *os.File
scanner *bufio.Scanner
row []string
address int
)
file, err = os.Open(filePath)
if err != nil {
return traces, err
}
defer file.Close()
scanner = bufio.NewScanner(file)
for scanner.Scan() {
row = strings.Split(scanner.Text(), ",")
address, err = strconv.Atoi(row[0])
if err != nil {
return traces, err
}
traces = append(traces,
simulator.Trace{
Addr: address,
Op: row[1],
},
)
}
return traces, nil
}