-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.js
205 lines (162 loc) · 5.87 KB
/
simulation.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"use strict";
/*
Copyright (C) 2014, Brigham Young University
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var Fsl = {};
Fsl.CpuTimeRemaining = {};
Fsl.CpuTimeRemaining.Job = function(walltime, cores) {
this.cores = cores;
this.walltime = walltime;
this.time_to_deduct = cores;
this.cpu_time = cores * walltime;
};
Fsl.CpuTimeRemaining.Form = function(id, color, renderer) {
this.html_id = 'simulation' + id;
this.id = id;
this.color = color;
this.attach = function(to) {
var previous = this.form.lastChild;
to.appendChild(this.form);
//if (!form.nextSibling) {
//previous.appendChild(this.createButton('+', 'inc', function() {
//}));
//}
//if (form.previousSibling) {
//previous.appendChild(this.createButton('-', 'dec', removeForm);
//}
}
this.detach = function() {
this.form.parentNode.removeChild(this.form);
}
// private
function createInputWithLabel(name, description, required) {
var input = document.createElement('input');
input.setAttribute('name', name);
input.setAttribute('size', 8);
input.setAttribute('type', 'text');
if (required) {
input.setAttribute('required', '');
}
var label = document.createElement('label');
label.appendChild(document.createTextNode(description));
label.appendChild(input);
return label;
}
this.createButton = function(value, button_class, listener) {
var button = document.createElement('button');
button.classList.add(button_class);
button.appendChild(document.createTextNode(value));
button.addEventListener('click', listener);
return button;
}
this.createForm = function() {
var form = document.createElement('form');
form.setAttribute('id', this.html_id);
form.classList.add(color);
var fieldset = document.createElement('fieldset');
var legend = document.createElement('legend');
legend.appendChild(document.createTextNode('Simulation #' + id));
fieldset.appendChild(legend);
fieldset.appendChild(createInputWithLabel('JobWalltimeHours', 'Job Walltime (hours)', true));
fieldset.appendChild(createInputWithLabel('JobCores', 'Cores per job', true));
fieldset.appendChild(createInputWithLabel('GrpCPURunMins', 'Group CPU Run Mins', true));
fieldset.appendChild(createInputWithLabel('jobs', 'Jobs', false));
var submit = document.createElement('button');
submit.setAttribute('form', this.html_id);
submit.setAttribute('type', 'submit');
submit.appendChild(document.createTextNode('Update Chart'));
var formadd = document.createElement('span');
formadd.setAttribute('id', 'formadd' + id);
formadd.classList.add('formadd');
form.appendChild(fieldset);
form.appendChild(submit);
form.appendChild(formadd);
form.addEventListener('submit', function(event) {
event.preventDefault();
renderer(form, id);
});
return form;
}
this.form = this.createForm();
}
Fsl.CpuTimeRemaining.calculate_number_of_jobs_to_run = function(walltime, cores, GRPCpuRunHrs) {
var MAGIC_CONSTANT = 5;
return GRPCpuRunHrs / (walltime / 2 ) / cores * MAGIC_CONSTANT;
};
/**
* This class implements the ECMAScript 6 protocol for iterators, as
* documented by MDN:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol
*
* At the time of this writing the iterator API is not available in mainstream
* browsers, so instead of using the for..of syntax of ES6 you need to
* manually iterate by calling .next()
*/
Fsl.CpuTimeRemaining.Simulation = function(walltime, cores, jobs, GRPCpuRunHrs) {
//public API:
this.next = function() {
var time_remaining = this.update_running_jobs_and_sum_time_remaining();
this.move_finished_jobs();
this.start_new_jobs(time_remaining);
return {
value: this.calculate_cpus_in_use(),
done: !this.has_next()
};
};
//private API:
this.jobs = [];
this.job = new Fsl.CpuTimeRemaining.Job(walltime, cores);
this.run_ndx = 0;
this.queue_ndx = 0;
this.update_running_jobs_and_sum_time_remaining = function() {
var sum = 0;
for (var i = this.run_ndx; i < this.queue_ndx; ++i) {
var j = Math.max(0, this.jobs[i] - this.job.time_to_deduct);
this.jobs[i] = j;
sum += j;
}
return sum;
};
this.move_finished_jobs = function() {
var i;
for (i = this.run_ndx; i < this.queue_ndx; ++i) {
if (this.jobs[i] > 0) {
break;
}
}
this.run_ndx = i;
};
this.calculate_time_to_fill_with_new_jobs = function(time_remaining) {
return GRPCpuRunHrs - time_remaining;
};
this.calculate_jobs_that_fit_in = function(time_to_fill) {
return Math.floor(time_to_fill / this.job.cpu_time);
};
this.start_n_jobs = function(n) {
this.queue_ndx += Math.min(n, this.jobs.length - this.queue_ndx);
};
this.start_new_jobs = function(time_remaining) {
var time_to_fill = this.calculate_time_to_fill_with_new_jobs(time_remaining);
var jobs_to_start = this.calculate_jobs_that_fit_in(time_to_fill);
this.start_n_jobs(jobs_to_start);
};
this.calculate_cpus_in_use = function() {
return (this.queue_ndx - this.run_ndx) * this.job.cores;
};
this.has_next = function() {
return this.run_ndx < this.jobs.length;
};
for (var i = 0; i < jobs; ++i) {
this.jobs.push(this.job.cpu_time);
}
};