-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
171 lines (137 loc) · 3.19 KB
/
job.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
package trace
import (
"fmt"
"math"
"math/rand"
"os"
"github.com/dsfalves/gdsim/job"
"gonum.org/v1/gonum/stat/distuv"
)
type Job struct {
id string
job.Job
}
type JobCreator struct {
NTG NumTasksGenerator
TDG TaskDurationGenerator
CGen CPUGenerator
DGen DelayGenerator
FSel FileSelector
}
type sizeDist interface {
Rand() float64
}
func Size(s sizeDist) uint {
return uint(math.Ceil(s.Rand()))
}
type NumTasksGenerator interface {
CreateNumTasks() uint
}
type SimpleNumTasksGenerator struct {
Small, Medium float64
}
func (gen SimpleNumTasksGenerator) CreateNumTasks() uint {
uni := distuv.Uniform{Min: 0, Max: 100}
num := uni.Rand()
var x sizeDist
if num < gen.Small {
x = &distuv.Uniform{Min: 0, Max: 150}
} else if num < gen.Small+gen.Medium {
x = &distuv.Uniform{Min: 150, Max: 600}
} else {
x = &distuv.Uniform{Min: 600, Max: 7000}
}
return Size(x)
}
type TaskDurationGenerator interface {
Duration() uint64
}
type ParetoTDG struct {
Pareto distuv.Pareto
}
func (p ParetoTDG) Duration() uint64 {
return uint64(math.Trunc(p.Pareto.Rand()))
}
func StandardPareto() ParetoTDG {
return ParetoTDG{distuv.Pareto{Xm: 1.259, Alpha: 2.7}}
}
type CPUGenerator interface {
CPUs() uint
}
type SimpleCPUGen struct {
Uniform distuv.Uniform
}
func CreateSimpleCG() SimpleCPUGen {
return SimpleCPUGen{distuv.Uniform{Min: 1, Max: 32}}
}
func (gen SimpleCPUGen) CPUs() uint {
return uint(math.Trunc(gen.Uniform.Rand()))
}
type DelayGenerator interface {
Delay() uint64
}
type PoissonDelayGenerator struct {
Poisson distuv.Poisson
}
func CreatePoissonDG() PoissonDelayGenerator {
return PoissonDelayGenerator{distuv.Poisson{Lambda: 5}}
}
func (gen PoissonDelayGenerator) Delay() uint64 {
return uint64(math.Trunc(gen.Poisson.Rand()))
}
type FileSelector interface {
File([]File) File
}
type ZipfFileSelector struct {
Zipf *rand.Zipf
}
func CreateZipfFS(source rand.Source, max uint64) ZipfFileSelector {
zipf := rand.NewZipf(rand.New(source), float64(2), 1, max)
return ZipfFileSelector{zipf}
}
func (gen ZipfFileSelector) File(files []File) File {
selected := gen.Zipf.Uint64()
for int(selected) >= len(files) {
selected = gen.Zipf.Uint64()
}
return files[selected]
}
func (jc JobCreator) createJob(files []File) Job {
//ntg := SimpleNumTasksGenerator{Small: 6.93, Medium: 23.15}
numTasks := jc.NTG.CreateNumTasks()
j := Job{"", job.Job{Tasks: make([]job.Task, numTasks)}}
j.Cpus = jc.CGen.CPUs()
j.Submission = jc.DGen.Delay()
j.File = jc.FSel.File(files).File
for i := range j.Tasks {
t := job.Task{Duration: jc.TDG.Duration()}
j.Tasks[i] = t
}
return j
}
func (j Job) String() string {
s := fmt.Sprintf("%v %v %v %v %v", j.id, j.Cpus, j.Submission, j.File.Id(), j.File.Size())
for _, t := range j.Tasks {
s = fmt.Sprintf("%v %v", s, t.Duration)
}
return s
}
func (jc JobCreator) CreateJobs(total uint, files []File) []Job {
jobs := make([]Job, total)
for i := range jobs {
jobs[i] = jc.createJob(files)
jobs[i].id = fmt.Sprintf("job%v", i+1)
}
return jobs
}
func SaveJobs(filename string, data []Job) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
for _, obj := range data {
fmt.Fprintf(f, "%v\n", obj)
}
return nil
}