-
Notifications
You must be signed in to change notification settings - Fork 86
/
scheduler.scm
223 lines (195 loc) · 8 KB
/
scheduler.scm
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#|
LambdaNative - a cross-platform Scheme framework
Copyright (c) 2009-2020, University of British Columbia
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the University of British Columbia nor
the names of its contributors may be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|#
;; task scheduler: Manages and runs the plugins
(include "plugin.scm")
(include "instance.scm")
(define scheduler:debuglevel 10)
(define (scheduler:log level . x) (if (>= scheduler:debuglevel level) (apply log-system (append (list "scheduler: ") x))))
(define (scheduler:directory name)
(let* ((sysdir (system-directory))
(localdir (string-append sysdir (system-pathseparator) name)))
;; android might not have an existing system directory.
(if (not (file-exists? sysdir)) (create-directory sysdir))
(if (file-exists? localdir)
localdir
(begin
(create-directory localdir)
(if (not (file-exists? localdir)) (log-error "scheduler: failed to create path: " localdir))
localdir
)
)
))
(define scheduler:configpath (scheduler:directory "config"))
(define scheduler:inputpath (scheduler:directory "input"))
(define scheduler:outputpath (scheduler:directory "results"))
(define scheduler:logpath (scheduler:directory "log"))
(define scheduler:defaultoutputpath scheduler:outputpath)
;; ---- MAIN PROCESSING
;; this function runs all inputs
;; returns false if one input returns false
(define (scheduler:doinputs)
(let loop ((sl (store-list))(ret #t))
(if (= (length sl) 0)
ret
(loop (cdr sl) (and ret
(let loop2 ((is (instance:allinputs (car sl)))(ret2 #t))
(if (= (length is) 0)
ret2
(loop2 (cdr is) (and ret2 (instance:run (car sl) (car is))))
)
)
))
)
))
(define scheduler:pendingstart '())
(define scheduler:pendingend '())
(define (scheduler-startcase store name)
(store-event-clear! store)
(scheduler:log 1 "startcase " name)
(store-set! store "CaseStartPending" name)
(set! scheduler:pendingstart (append scheduler:pendingstart (list (list store name)))))
(define (scheduler-endcase store)
(scheduler:log 1 "endcase")
(store-set! store "CaseEndPending" #t)
(set! scheduler:pendingend (append scheduler:pendingend (list store))))
;; run case init on all stores with new cases
(define (scheduler:initnewcases now nowstr)
(for-each (lambda (p)
(let ((s (car p)) (c (cadr p)))
(scheduler:log 1 "starting case " c)
(store-set! s "CaseID" c)
(store-set! s "Start" now)
(store-set! s "StartStr" nowstr)
(for-each (lambda (i) (instance:caseinit s i)) (instance:all s))
)
) scheduler:pendingstart)
(set! scheduler:pendingstart '())
)
;; run case end on all stores with finished cases
(define (scheduler:endoldcases)
(for-each (lambda (s)
(for-each (lambda (i) (instance:caseend s i)) (instance:all s))
(scheduler:log 1 "ending case " (store-ref s "CaseID" #f))
(store-set! s "CaseID" #f)
) scheduler:pendingend)
(set! scheduler:pendingend '())
)
;; set store timestamps (with offset for better performance)
(define (scheduler:initdispatchtimes)
(let ((timedelta (/ 5.0 (if (fx> (length (store-list)) 0) (length (store-list)) 1.))))
(let loop ((sl (store-list))(n 0))
(if (> (length sl) 0) (begin
(store:instance-set! (car sl) "DispatchStart" (flo (+ ##now (* timedelta n))))
(store:instance-set! (car sl) "DispatchCount" 0.)
(loop (cdr sl) (fx+ n 1))
))
)
))
(define scheduler:initflag #f)
(define (scheduler-initialized?) scheduler:initflag)
(define (scheduler-init . timefunc)
(scheduler:log 2 "initializing all instances")
(for-each (lambda (s) (for-each (lambda (i) (instance:init s i)) (instance:all s))) (store-list))
(if (fx= (length timefunc) 1) (begin
(set! current-time-seconds (car timefunc))
;; If this is not done and we are replaying file scheduler:initdispatchtimes prevents stores from ever dispatching.
(set! ##now (current-time-seconds))
))
(scheduler:log 2 "setting store dispatch timing")
(scheduler:initdispatchtimes)
(set! scheduler:initflag #t)
)
(define (scheduler-iterate . guiwaveproc)
(if (not (scheduler-initialized?)) (begin
(log-error "scheduler: (scheduler-init) was not called. Initializing with default timefunc.")
(scheduler-init)
))
(for-each (lambda (s) (store-set! s "Now" ##now)) (store-list))
(scheduler:endoldcases)
(scheduler:initnewcases ##now (seconds->string (fix ##now) "%T"))
(if (scheduler:doinputs)
(begin
(for-each (lambda (s)
(if (and (store? s) (fl>= (fl- ##now (store:instance-ref s "DispatchStart" 0.)) (store:instance-ref s "DispatchCount" 0.)))
;; run every 1 second
(begin
(store:instance-set! s "DispatchCount" (fl+ (store:instance-ref s "DispatchCount" 0.) 1.))
(store:waveform-dispatch s)
(store:raw-dispatch s)
;; provide waveform access to gui here
(if (= (length guiwaveproc) 1) ((car guiwaveproc)))
;; run all algorithms, even if case not active
(for-each (lambda (i) (instance:run s i)) (instance:allalgs s))
(if (store-ref s "CaseID" #f) (begin
;; run all dse instances
(for-each (lambda (i) (instance:run s i)) (instance:alldses s))
;; run all output instances
(for-each (lambda (i) (instance:run s i)) (instance:alloutputs s))
))
)
))
(store-list))
;; (watchdog)
;; (##gc)
#t
)
#f
)
)
(define (scheduler-cleanup)
(scheduler:log 2 "cleanup on all instances")
(for-each (lambda (s) (for-each (lambda (i) (instance:end s i)) (instance:all s))) (store-list))
)
;; All rolling logfiles at start of cases.. useful for a separate log of each case
(define log:lastrolled 0.)
(define log:lastcaseid #f)
(define (log-rollcase store)
(let ((caseid (store-ref store "CaseID" #f)))
(if (fl> (- ##now log:lastrolled) 1.0) (begin
(if (not (equal? caseid log:lastcaseid)) (begin
(log-system "Log continues at log_" caseid ".txt")
(set! log:file (string-append log:path (system-pathseparator) "log_"
(if caseid caseid (time->string (current-time) "%Y%m%d_%H%M%S")) ".txt"))
(log-system "Application " (system-appname) " built " (system-builddatetime))
(log-system "Git hash " (system-buildhash))
(log-system "Log file rolled at " (if caseid "start" "end") " of case: " caseid)
))
(set! log:lastrolled ##now)
(set! log:lastcaseid caseid)
))
))
;; As the timing accuracy of scheduler-iterate depends on frequent runs
;; make sure this behavior does not change with new glgui-event sleeps.
(if (function-exists? "glgui-timings-at-10msec!")
(eval '(glgui-timings-at-10msec!)))
;; eof