Kstars

greedyscheduler.cpp
1/* Ekos Scheduler Greedy Algorithm
2 SPDX-FileCopyrightText: Hy Murveit <hy@murveit.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "greedyscheduler.h"
8
9#include <ekos_scheduler_debug.h>
10
11#include "Options.h"
12#include "scheduler.h"
13#include "ekos/ekos.h"
14#include "ui_scheduler.h"
15#include "schedulerjob.h"
16#include "schedulerutils.h"
17
18#define TEST_PRINT if (false) fprintf
19
20// Can make the scheduling a bit faster by sampling every other minute instead of every minute.
21constexpr int SCHEDULE_RESOLUTION_MINUTES = 2;
22
23namespace Ekos
24{
25
26GreedyScheduler::GreedyScheduler()
27{
28}
29
30void GreedyScheduler::setParams(bool restartImmediately, bool restartQueue,
31 bool rescheduleErrors, int abortDelay,
32 int errorHandlingDelay)
33{
34 setRescheduleAbortsImmediate(restartImmediately);
35 setRescheduleAbortsQueue(restartQueue);
36 setRescheduleErrors(rescheduleErrors);
37 setAbortDelaySeconds(abortDelay);
38 setErrorDelaySeconds(errorHandlingDelay);
39}
40
41// The possible changes made to a job in jobs are:
42// Those listed in prepareJobsForEvaluation()
43// Those listed in selectNextJob
44// job->clearCache()
45// job->updateJobCells()
46
47void GreedyScheduler::scheduleJobs(const QList<SchedulerJob *> &jobs,
48 const QDateTime &now,
49 const QMap<QString, uint16_t> &capturedFramesCount,
50 ModuleLogger *logger)
51{
52 for (auto job : jobs)
53 job->clearCache();
54
55 QDateTime when;
56 QElapsedTimer timer;
57 timer.start();
58 scheduledJob = nullptr;
59 schedule.clear();
60
61 prepareJobsForEvaluation(jobs, now, capturedFramesCount, logger);
62
63 // consider only lead jobs for scheduling, scheduling data is propagated to its follower jobs
64 const QList<SchedulerJob *> leadJobs = SchedulerUtils::filterLeadJobs(jobs);
65
66 scheduledJob = selectNextJob(leadJobs, now, nullptr, SIMULATE, &when, nullptr, nullptr, &capturedFramesCount);
67 auto schedule = getSchedule();
68 if (logger != nullptr)
69 {
70 if (!schedule.empty())
71 {
72 // Print in reverse order ?! The log window at the bottom of the screen
73 // prints "upside down" -- most recent on top -- and I believe that view
74 // is more important than the log file (where we can invert when debugging).
75 for (int i = schedule.size() - 1; i >= 0; i--)
76 logger->appendLogText(GreedyScheduler::jobScheduleString(schedule[i]));
77 logger->appendLogText(QString("Greedy Scheduler plan for the next 48 hours starting %1 (%2)s:")
78 .arg(now.toString()).arg(timer.elapsed() / 1000.0));
79 }
80 else logger->appendLogText(QString("Greedy Scheduler: empty plan (%1s)").arg(timer.elapsed() / 1000.0));
81 }
82 if (scheduledJob != nullptr)
83 {
84 qCDebug(KSTARS_EKOS_SCHEDULER)
85 << QString("Greedy Scheduler scheduling next job %1 at %2")
86 .arg(scheduledJob->getName(), when.toString("hh:mm"));
87 scheduledJob->setState(SCHEDJOB_SCHEDULED);
88 scheduledJob->setStartupTime(when);
89 }
90
91 for (auto job : jobs)
92 job->clearCache();
93}
94
95// The changes made to a job in jobs are:
96// Those listed in selectNextJob()
97// Not a const method because it sets the schedule class variable.
98bool GreedyScheduler::checkJob(const QList<SchedulerJob *> &jobs,
99 const QDateTime &now,
100 const SchedulerJob * const currentJob)
101{
102 // Don't interrupt a job that just started.
103 if (currentJob && currentJob->getStateTime().secsTo(now) < 5)
104 return true;
105
106 QDateTime startTime;
107
108 // Simulating in checkJob() is only done to update the schedule which is a GUI convenience.
109 // Do it only if its quick and not more frequently than once per minute.
110 SimulationType simType = SIMULATE_EACH_JOB_ONCE;
111 if (m_SimSeconds > 0.2 ||
112 (m_LastCheckJobSim.isValid() && m_LastCheckJobSim.secsTo(now) < 60))
113 simType = DONT_SIMULATE;
114
115 const SchedulerJob *next = selectNextJob(jobs, now, currentJob, simType, &startTime);
116 if (next == currentJob && now.secsTo(startTime) <= 1)
117 {
118 if (simType != DONT_SIMULATE)
119 m_LastCheckJobSim = now;
120
121 return true;
122 }
123 else
124 {
125 // We need to interrupt the current job. There's a higher-priority one to run.
126 qCDebug(KSTARS_EKOS_SCHEDULER)
127 << QString("Greedy Scheduler bumping current job %1 for %2 at %3")
128 .arg(currentJob->getName(), next ? next->getName() : "---", now.toString("hh:mm"));
129 return false;
130 }
131}
132
133// The changes made to a job in jobs are:
134// job->setState(JOB_COMPLETE|JOB_EVALUATION|JOB_INVALID|JOB_COMPLETEno_change)
135// job->setEstimatedTime(0|-1|-2|time)
136// job->setInitialFilter(filter)
137// job->setLightFramesRequired(bool)
138// job->setInSequenceFocus(bool);
139// job->setCompletedIterations(completedIterations);
140// job->setCapturedFramesMap(capture_map);
141// job->setSequenceCount(count);
142// job->setEstimatedTimePerRepeat(time);
143// job->setEstimatedTimeLeftThisRepeat(time);
144// job->setEstimatedStartupTime(time);
145// job->setCompletedCount(count);
146
147void GreedyScheduler::prepareJobsForEvaluation(
148 const QList<SchedulerJob *> &jobs, const QDateTime &now,
149 const QMap<QString, uint16_t> &capturedFramesCount, ModuleLogger *logger, bool reestimateJobTimes) const
150{
151 // Remove some finished jobs from eval.
152 foreach (SchedulerJob *job, jobs)
153 {
154 switch (job->getCompletionCondition())
155 {
156 case FINISH_AT:
157 /* If planned finishing time has passed, the job is set to IDLE waiting for a next chance to run */
158 if (job->getFinishAtTime().isValid() && job->getFinishAtTime() < now)
159 {
160 job->setState(SCHEDJOB_COMPLETE);
161 continue;
162 }
163 break;
164
165 case FINISH_REPEAT:
166 // In case of a repeating jobs, let's make sure we have more runs left to go
167 // If we don't, re-estimate imaging time for the scheduler job before concluding
168 if (job->getRepeatsRemaining() == 0)
169 {
170 if (logger != nullptr) logger->appendLogText(i18n("Job '%1' has no more batches remaining.", job->getName()));
171 job->setState(SCHEDJOB_COMPLETE);
172 job->setEstimatedTime(0);
173 continue;
174 }
175 break;
176
177 default:
178 break;
179 }
180 }
181
182 // Change the state to eval or ERROR/ABORTED for all jobs that will be evaluated.
183 foreach (SchedulerJob *job, jobs)
184 {
185 switch (job->getState())
186 {
187 case SCHEDJOB_INVALID:
189 // If job is invalid or complete, bypass evaluation.
190 break;
191
192 case SCHEDJOB_ERROR:
193 case SCHEDJOB_ABORTED:
194 // These will be evaluated, but we'll have a delay to start.
195 break;
196 case SCHEDJOB_IDLE:
197 case SCHEDJOB_BUSY:
200 default:
201 job->setState(SCHEDJOB_EVALUATION);
202 break;
203 }
204 }
205
206 // Estimate the job times
207 foreach (SchedulerJob *job, jobs)
208 {
209 if (job->getState() == SCHEDJOB_INVALID || job->getState() == SCHEDJOB_COMPLETE)
210 continue;
211
212 // -1 = Job is not estimated yet
213 // -2 = Job is estimated but time is unknown
214 // > 0 Job is estimated and time is known
215 if (reestimateJobTimes)
216 {
217 job->setEstimatedTime(-1);
218 if (SchedulerUtils::estimateJobTime(job, capturedFramesCount, logger) == false)
219 {
220 job->setState(SCHEDJOB_INVALID);
221 continue;
222 }
223 }
224 if (job->getEstimatedTime() == 0)
225 {
226 job->setRepeatsRemaining(0);
227 // set job including its followers to complete state
228 job->setState(SCHEDJOB_COMPLETE, true);
229 continue;
230 }
231 }
232}
233
234namespace
235{
236// Don't Allow INVALID or COMPLETE jobs to be scheduled.
237// Allow ABORTED if one of the rescheduleAbort... options are true.
238// Allow ERROR if rescheduleErrors is true.
239bool allowJob(const SchedulerJob *job, bool rescheduleAbortsImmediate, bool rescheduleAbortsQueue, bool rescheduleErrors)
240{
241 if (job->getState() == SCHEDJOB_INVALID || job->getState() == SCHEDJOB_COMPLETE)
242 return false;
243 if (job->getState() == SCHEDJOB_ABORTED && !rescheduleAbortsImmediate && !rescheduleAbortsQueue)
244 return false;
245 if (job->getState() == SCHEDJOB_ERROR && !rescheduleErrors)
246 return false;
247 return true;
248}
249
250// Returns the first possible time a job may be scheduled. That is, it doesn't
251// evaluate the job, but rather just computes the needed delay (for ABORT and ERROR jobs)
252// or returns now for other jobs.
253QDateTime firstPossibleStart(const SchedulerJob *job, const QDateTime &now,
254 bool rescheduleAbortsQueue, int abortDelaySeconds,
255 bool rescheduleErrors, int errorDelaySeconds)
256{
257 QDateTime possibleStart = now;
258 const QDateTime &abortTime = job->getLastAbortTime();
259 const QDateTime &errorTime = job->getLastErrorTime();
260
261 if (abortTime.isValid() && rescheduleAbortsQueue)
262 {
263 auto abortStartTime = abortTime.addSecs(abortDelaySeconds);
264 if (abortStartTime > now)
265 possibleStart = abortStartTime;
266 }
267
268
269 if (errorTime.isValid() && rescheduleErrors)
270 {
271 auto errorStartTime = errorTime.addSecs(errorDelaySeconds);
272 if (errorStartTime > now)
273 possibleStart = errorStartTime;
274 }
275
276 if (!possibleStart.isValid() || possibleStart < now)
277 possibleStart = now;
278 return possibleStart;
279}
280} // namespace
281
282// Consider all jobs marked as JOB_EVALUATION/ABORT/ERROR. Assume ordered by highest priority first.
283// - Find the job with the earliest start time (given constraints like altitude, twilight, ...)
284// that can run for at least 10 minutes before a higher priority job.
285// - START_AT jobs are given the highest priority, whereever on the list they may be,
286// as long as they can start near their designated start times.
287// - Compute a schedule for the next 2 days, if fullSchedule is true, otherwise
288// just look for the next job.
289// - If currentJob is not nullptr, this method is really evaluating whether
290// that job can continue to be run, or if can't meet constraints, or if it
291// should be preempted for another job.
292//
293// This does not modify any of the jobs in jobs if there is no simType is DONT_SIMULATE.
294// If we are simulating, then jobs may change in the following ways:
295// job->setGreedyCompletionTime()
296// job->setState(state);
297// job->setStartupTime(time);
298// job->setStopReason(reason);
299// The only reason this isn't a const method is because it sets the schedule class variable.
300SchedulerJob *GreedyScheduler::selectNextJob(const QList<SchedulerJob *> &jobs, const QDateTime &now,
301 const SchedulerJob * const currentJob, SimulationType simType, QDateTime *when,
302 QDateTime *nextInterruption, QString *interruptReason,
303 const QMap<QString, uint16_t> *capturedFramesCount)
304{
305 // Don't schedule a job that will be preempted in less than MIN_RUN_SECS.
306 constexpr int MIN_RUN_SECS = 10 * 60;
307
308 // Don't preempt a job for another job that is more than MAX_INTERRUPT_SECS in the future.
309 constexpr int MAX_INTERRUPT_SECS = 30;
310
311 // Don't interrupt START_AT jobs unless they can no longer run, or they're interrupted by another START_AT.
312 bool currentJobIsStartAt = (currentJob && currentJob->getFileStartupCondition() == START_AT &&
313 currentJob->getStartAtTime().isValid());
314 QDateTime nextStart;
315 SchedulerJob * nextJob = nullptr;
316 QString interruptStr;
317
318 for (int i = 0; i < jobs.size(); ++i)
319 {
320 SchedulerJob * const job = jobs[i];
321 const bool evaluatingCurrentJob = (currentJob && (job == currentJob));
322
323 if (!allowJob(job, rescheduleAbortsImmediate, rescheduleAbortsQueue, rescheduleErrors))
324 continue;
325
326 // If the job state is abort or error, might have to delay the first possible start time.
327 QDateTime startSearchingtAt = firstPossibleStart(
328 job, now, rescheduleAbortsQueue, abortDelaySeconds, rescheduleErrors, errorDelaySeconds);
329
330 // Find the first time this job can meet all its constraints.
331 // I found that passing in an "until" 4th argument actually hurt performance, as it reduces
332 // the effectiveness of the cache that getNextPossibleStartTime uses.
333 const QDateTime startTime = job->getNextPossibleStartTime(startSearchingtAt, SCHEDULE_RESOLUTION_MINUTES,
334 evaluatingCurrentJob);
335 if (startTime.isValid())
336 {
337 if (nextJob == nullptr)
338 {
339 // We have no other solutions--this is our best solution so far.
340 nextStart = startTime;
341 nextJob = job;
342 if (nextInterruption) *nextInterruption = QDateTime();
343 interruptStr = "";
344 }
345 else if (Options::greedyScheduling())
346 {
347 // Allow this job to be scheduled if it can run this many seconds
348 // before running into a higher priority job.
349 const int runSecs = evaluatingCurrentJob ? MAX_INTERRUPT_SECS : MIN_RUN_SECS;
350
351 // Don't interrupt a START_AT for higher priority job
352 if (evaluatingCurrentJob && currentJobIsStartAt)
353 {
354 if (nextInterruption) *nextInterruption = QDateTime();
355 nextStart = startTime;
356 nextJob = job;
357 interruptStr = "";
358 }
359 else if (startTime.secsTo(nextStart) > runSecs)
360 {
361 // We can start a lower priority job if it can run for at least runSecs
362 // before getting bumped by the previous higher priority job.
363 if (nextInterruption) *nextInterruption = nextStart;
364 interruptStr = QString("interrupted by %1").arg(nextJob->getName());
365 nextStart = startTime;
366 nextJob = job;
367 }
368 }
369 // If scheduling, and we have a solution close enough to now, none of the lower priority
370 // jobs can possibly be scheduled.
371 if (!currentJob && nextStart.isValid() && now.secsTo(nextStart) < MIN_RUN_SECS)
372 break;
373 }
374 else if (evaluatingCurrentJob)
375 {
376 // No need to keep searching past the current job if we're evaluating it
377 // and it had no startTime. It needs to be stopped.
378 *when = QDateTime();
379 return nullptr;
380 }
381
382 if (evaluatingCurrentJob) break;
383 }
384 if (nextJob != nullptr)
385 {
386 // The exception to the simple scheduling rules above are START_AT jobs, which
387 // are given highest priority, irrespective of order. If nextJob starts less than
388 // MIN_RUN_SECS before an on-time START_AT job, then give the START_AT job priority.
389 // However, in order for the START_AT job to interrupt a current job, it must start now.
390 for (int i = 0; i < jobs.size(); ++i)
391 {
392 SchedulerJob * const atJob = jobs[i];
393 if (atJob == nextJob)
394 continue;
395 const QDateTime atTime = atJob->getStartAtTime();
396 if (atJob->getFileStartupCondition() == START_AT && atTime.isValid())
397 {
398 if (!allowJob(atJob, rescheduleAbortsImmediate, rescheduleAbortsQueue, rescheduleErrors))
399 continue;
400 // If the job state is abort or error, might have to delay the first possible start time.
401 QDateTime startSearchingtAt = firstPossibleStart(
402 atJob, now, rescheduleAbortsQueue, abortDelaySeconds, rescheduleErrors,
403 errorDelaySeconds);
404 // atTime above is the user-specified start time. atJobStartTime is the time it can
405 // actually start, given all the constraints (altitude, twilight, etc).
406 const QDateTime atJobStartTime = atJob->getNextPossibleStartTime(startSearchingtAt, SCHEDULE_RESOLUTION_MINUTES, currentJob
407 && (atJob == currentJob));
408 if (atJobStartTime.isValid())
409 {
410 // This difference between the user-specified start time, and the time it can really start.
411 const double startDelta = atJobStartTime.secsTo(atTime);
412 if (fabs(startDelta) < 20 * 60)
413 {
414 // If we're looking for a new job to start, then give the START_AT priority
415 // if it's within 10 minutes of its user-specified time.
416 // However, if we're evaluating the current job (called from checkJob() above)
417 // then only interrupt it if the START_AT job can start very soon.
418 const int gap = currentJob == nullptr ? MIN_RUN_SECS : 30;
419 if (nextStart.secsTo(atJobStartTime) <= gap)
420 {
421 nextJob = atJob;
422 nextStart = atJobStartTime;
423 if (nextInterruption) *nextInterruption = QDateTime(); // Not interrupting atJob
424 }
425 else if (nextInterruption)
426 {
427 // The START_AT job was not chosen to start now, but it's still possible
428 // that this atJob will be an interrupter.
429 if (!nextInterruption->isValid() ||
430 atJobStartTime.secsTo(*nextInterruption) < 0)
431 {
432 *nextInterruption = atJobStartTime;
433 interruptStr = QString("interrupted by %1").arg(atJob->getName());
434 }
435 }
436 }
437 }
438 }
439 }
440
441 // If the selected next job is part of a group, then we may schedule other members of the group if
442 // - the selected job is a repeating job and
443 // - another group member is runnable now and
444 // - that group mnember is behind the selected job's iteration.
445 if (nextJob && !nextJob->getGroup().isEmpty() && Options::greedyScheduling() && nextJob->getCompletedIterations() > 0)
446 {
447 // Iterate through the jobs list, first finding the selected job, the looking at all jobs after that.
448 bool foundSelectedJob = false;
449 for (int i = 0; i < jobs.size(); ++i)
450 {
451 SchedulerJob * const job = jobs[i];
452 if (job == nextJob)
453 {
454 foundSelectedJob = true;
455 continue;
456 }
457
458 // Only jobs with lower priority than nextJob--higher priority jobs already have been considered and rejected.
459 // Only consider jobs in the same group as nextJob
460 // Only consider jobs with fewer iterations than nextJob.
461 // Only consider jobs that are allowed.
462 if (!foundSelectedJob ||
463 (job->getGroup() != nextJob->getGroup()) ||
464 (job->getCompletedIterations() >= nextJob->getCompletedIterations()) ||
465 !allowJob(job, rescheduleAbortsImmediate, rescheduleAbortsQueue, rescheduleErrors))
466 continue;
467
468 const bool evaluatingCurrentJob = (currentJob && (job == currentJob));
469
470 // If the job state is abort or error, might have to delay the first possible start time.
471 QDateTime startSearchingtAt = firstPossibleStart(
472 job, now, rescheduleAbortsQueue, abortDelaySeconds, rescheduleErrors, errorDelaySeconds);
473
474 // Find the first time this job can meet all its constraints.
475 const QDateTime startTime = job->getNextPossibleStartTime(startSearchingtAt, SCHEDULE_RESOLUTION_MINUTES,
476 evaluatingCurrentJob);
477
478 // Only consider jobs that can start soon.
479 if (!startTime.isValid() || startTime.secsTo(nextStart) > MAX_INTERRUPT_SECS)
480 continue;
481
482 // Don't interrupt a START_AT for higher priority job
483 if (evaluatingCurrentJob && currentJobIsStartAt)
484 {
485 if (nextInterruption) *nextInterruption = QDateTime();
486 nextStart = startTime;
487 nextJob = job;
488 interruptStr = "";
489 }
490 else if (startTime.secsTo(nextStart) >= -MAX_INTERRUPT_SECS)
491 {
492 // Use this group member, keeping the old interruption variables.
493 nextStart = startTime;
494 nextJob = job;
495 }
496 }
497 }
498 }
499 if (when != nullptr) *when = nextStart;
500 if (interruptReason != nullptr) *interruptReason = interruptStr;
501
502 // Needed so display says "Idle" for unscheduled jobs.
503 // This will also happen in simulate, but that isn't called if nextJob is null.
504 // Must test for !nextJob. setState() inside unsetEvaluation has a nasty side effect
505 // of clearing the estimated time.
506 if (!nextJob)
507 unsetEvaluation(jobs);
508
509 QElapsedTimer simTimer;
510 simTimer.start();
511 constexpr int twoDays = 48 * 3600;
512 if (simType != DONT_SIMULATE && nextJob != nullptr)
513 {
514 QDateTime simulationLimit = now.addSecs(twoDays);
515 schedule.clear();
516 QDateTime simEnd = simulate(jobs, now, simulationLimit, capturedFramesCount, simType);
517
518 // This covers the scheduler's "repeat after completion" option,
519 // which only applies if rememberJobProgress is false.
520 if (!Options::rememberJobProgress() && Options::schedulerRepeatEverything())
521 {
522 int repeats = 0, maxRepeats = 5;
523 while (simEnd.isValid() && simEnd.secsTo(simulationLimit) > 0 && ++repeats < maxRepeats)
524 {
525 simEnd = simEnd.addSecs(60);
526 simEnd = simulate(jobs, simEnd, simulationLimit, nullptr, simType);
527 }
528 }
529 }
530 m_SimSeconds = simTimer.elapsed() / 1000.0;
531
532 return nextJob;
533}
534
535// The only reason this isn't a const method is because it sets the schedule class variable
536QDateTime GreedyScheduler::simulate(const QList<SchedulerJob *> &jobs, const QDateTime &time, const QDateTime &endTime,
537 const QMap<QString, uint16_t> *capturedFramesCount, SimulationType simType)
538{
539 TEST_PRINT(stderr, "%d simulate()\n", __LINE__);
540 // Make a deep copy of jobs
541 QList<SchedulerJob *> copiedJobs;
542 QList<SchedulerJob *> scheduledJobs;
543 QDateTime simEndTime;
544
545 foreach (SchedulerJob *job, jobs)
546 {
547 SchedulerJob *newJob = new SchedulerJob();
548 // Make sure the copied class pointers aren't affected!
549 *newJob = *job;
550 // clear follower job lists to avoid links to existing jobs
551 newJob->followerJobs().clear();
552 copiedJobs.append(newJob);
553 job->setStopTime(QDateTime());
554 }
555
556 // The number of jobs we have that can be scheduled,
557 // and the number of them where a simulated start has been scheduled.
558 int numStartupCandidates = 0, numStartups = 0;
559 // Reset the start times.
560 foreach (SchedulerJob *job, copiedJobs)
561 {
562 job->setStartupTime(QDateTime());
563 const auto state = job->getState();
564 if (state == SCHEDJOB_SCHEDULED || state == SCHEDJOB_EVALUATION ||
565 state == SCHEDJOB_BUSY || state == SCHEDJOB_IDLE)
566 numStartupCandidates++;
567 }
568
569 QMap<QString, uint16_t> capturedFramesCopy;
570 if (capturedFramesCount != nullptr)
571 capturedFramesCopy = *capturedFramesCount;
572 QList<SchedulerJob *>simJobs = copiedJobs;
573 prepareJobsForEvaluation(copiedJobs, time, capturedFramesCopy, nullptr, false);
574
575 QDateTime simTime = time;
576 int iterations = 0;
577 bool exceededIterations = false;
579 QHash<SchedulerJob*, int> originalIteration, originalSecsLeftIteration;
580
581 for(int i = 0; i < simJobs.size(); ++i)
582 workDone[simJobs[i]] = 0.0;
583
584 while (true)
585 {
586 QDateTime jobStartTime;
587 QDateTime jobInterruptTime;
588 QString interruptReason;
589 // Find the next job to be scheduled, when it starts, and when a higher priority
590 // job might preempt it, why it would be preempted.
591 // Note: 4th arg, fullSchedule, must be false or we'd loop forever.
592 SchedulerJob *selectedJob = selectNextJob(
593 simJobs, simTime, nullptr, DONT_SIMULATE, &jobStartTime, &jobInterruptTime, &interruptReason);
594 if (selectedJob == nullptr)
595 break;
596
597 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString("%1 starting at %2 interrupted at \"%3\" reason \"%4\"")
598 .arg(selectedJob->getName()).arg(jobStartTime.toString("MM/dd hh:mm"))
599 .arg(jobInterruptTime.toString("MM/dd hh:mm")).arg(interruptReason).toLatin1().data());
600 // Are we past the end time?
601 if (endTime.isValid() && jobStartTime.secsTo(endTime) < 0) break;
602
603 // It's possible there are start_at jobs that can preempt this job.
604 // Find the next start_at time, and use that as an end constraint to getNextEndTime
605 // if it's before jobInterruptTime.
606 QDateTime nextStartAtTime;
607 foreach (SchedulerJob *job, simJobs)
608 {
609 if (job != selectedJob &&
610 job->getStartupCondition() == START_AT &&
611 jobStartTime.secsTo(job->getStartupTime()) > 0 &&
612 (job->getState() == SCHEDJOB_EVALUATION ||
613 job->getState() == SCHEDJOB_SCHEDULED))
614 {
615 QDateTime startAtTime = job->getStartupTime();
616 if (!nextStartAtTime.isValid() || nextStartAtTime.secsTo(startAtTime) < 0)
617 nextStartAtTime = startAtTime;
618 }
619 }
620 // Check to see if the above start-at stop time is before the interrupt stop time.
621 QDateTime constraintStopTime = jobInterruptTime;
622 if (nextStartAtTime.isValid() &&
623 (!constraintStopTime.isValid() ||
624 nextStartAtTime.secsTo(constraintStopTime) < 0))
625 constraintStopTime = nextStartAtTime;
626
627 QString constraintReason;
628 // Get the time that this next job would fail its constraints, and a human-readable explanation.
629 QDateTime jobConstraintTime = selectedJob->getNextEndTime(jobStartTime, SCHEDULE_RESOLUTION_MINUTES, &constraintReason,
630 constraintStopTime);
631 if (nextStartAtTime.isValid() && jobConstraintTime.isValid() &&
632 std::abs(jobConstraintTime.secsTo(nextStartAtTime)) < 2 * SCHEDULE_RESOLUTION_MINUTES)
633 constraintReason = "interrupted by start-at job";
634 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString(" constraint \"%1\" reason \"%2\"")
635 .arg(jobConstraintTime.toString("MM/dd hh:mm")).arg(constraintReason).toLatin1().data());
636 QDateTime jobCompletionTime;
637 if (selectedJob->getEstimatedTime() > 0)
638 {
639 // Estimate when the job might complete, if it was allowed to run without interruption.
640 const int timeLeft = selectedJob->getEstimatedTime() - workDone[selectedJob];
641 jobCompletionTime = jobStartTime.addSecs(timeLeft);
642 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString(" completion \"%1\" time left %2s")
643 .arg(jobCompletionTime.toString("MM/dd hh:mm")).arg(timeLeft).toLatin1().data());
644 }
645 // Consider the 3 stopping times computed above (preemption, constraints missed, and completion),
646 // see which comes soonest, and set the jobStopTime and jobStopReason.
647 QDateTime jobStopTime = jobInterruptTime;
648 QString stopReason = jobStopTime.isValid() ? interruptReason : "";
649 if (jobConstraintTime.isValid() && (!jobStopTime.isValid() || jobStopTime.secsTo(jobConstraintTime) < 0))
650 {
651 stopReason = constraintReason;
652 jobStopTime = jobConstraintTime;
653 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString(" picked constraint").toLatin1().data());
654 }
655 if (jobCompletionTime.isValid() && (!jobStopTime.isValid() || jobStopTime.secsTo(jobCompletionTime) < 0))
656 {
657 stopReason = "job completion";
658 jobStopTime = jobCompletionTime;
659 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString(" picked completion").toLatin1().data());
660 }
661
662 // This if clause handles the simulation of scheduler repeat groups
663 // which applies to scheduler jobs with repeat-style completion conditions.
664 if (!selectedJob->getGroup().isEmpty() &&
665 (selectedJob->getCompletionCondition() == FINISH_LOOP ||
666 selectedJob->getCompletionCondition() == FINISH_REPEAT ||
667 selectedJob->getCompletionCondition() == FINISH_AT))
668 {
669 if (originalIteration.find(selectedJob) == originalIteration.end())
670 originalIteration[selectedJob] = selectedJob->getCompletedIterations();
671 if (originalSecsLeftIteration.find(selectedJob) == originalSecsLeftIteration.end())
672 originalSecsLeftIteration[selectedJob] = selectedJob->getEstimatedTimeLeftThisRepeat();
673
674 // Estimate the time it would take to complete the current repeat, if this is a repeated job.
675 int leftThisRepeat = selectedJob->getEstimatedTimeLeftThisRepeat();
676 int secsPerRepeat = selectedJob->getEstimatedTimePerRepeat();
677 int secsLeftThisRepeat = (workDone[selectedJob] < leftThisRepeat) ?
678 leftThisRepeat - workDone[selectedJob] : secsPerRepeat;
679
680 if (workDone[selectedJob] == 0)
681 secsLeftThisRepeat += selectedJob->getEstimatedStartupTime();
682
683 // If it would finish a repeat, run one repeat and see if it would still be scheduled.
684 if (secsLeftThisRepeat > 0 &&
685 (!jobStopTime.isValid() || secsLeftThisRepeat < jobStartTime.secsTo(jobStopTime)))
686 {
687 auto tempStart = jobStartTime;
688 auto tempInterrupt = jobInterruptTime;
689 auto tempReason = stopReason;
690 SchedulerJob keepJob = *selectedJob;
691
692 auto t = jobStartTime.addSecs(secsLeftThisRepeat);
693 int iteration = selectedJob->getCompletedIterations();
694 int iters = 0, maxIters = 20; // just in case...
695 while ((!jobStopTime.isValid() || t.secsTo(jobStopTime) > 0) && iters++ < maxIters)
696 {
697 selectedJob->setCompletedIterations(++iteration);
698 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString(" iteration=%1").arg(iteration).toLatin1().data());
699 SchedulerJob *next = selectNextJob(simJobs, t, nullptr, DONT_SIMULATE, &tempStart, &tempInterrupt, &tempReason);
700 if (next != selectedJob)
701 {
702 stopReason = "interrupted for group member";
703 jobStopTime = t;
704 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString(" switched to group member %1 at %2")
705 .arg(next == nullptr ? "null" : next->getName()).arg(t.toString("MM/dd hh:mm")).toLatin1().data());
706
707 break;
708 }
709 t = t.addSecs(secsPerRepeat);
710 }
711 *selectedJob = keepJob;
712 }
713 }
714
715 // Increment the work done, for the next time this job might be scheduled in this simulation.
716 if (jobStopTime.isValid())
717 {
718 const int secondsRun = jobStartTime.secsTo(jobStopTime);
719 workDone[selectedJob] += secondsRun;
720
721 if ((originalIteration.find(selectedJob) != originalIteration.end()) &&
722 (originalSecsLeftIteration.find(selectedJob) != originalSecsLeftIteration.end()))
723 {
724 int completedIterations = originalIteration[selectedJob];
725 if (workDone[selectedJob] >= originalSecsLeftIteration[selectedJob] &&
726 selectedJob->getEstimatedTimePerRepeat() > 0)
727 completedIterations +=
728 1 + (workDone[selectedJob] - originalSecsLeftIteration[selectedJob]) / selectedJob->getEstimatedTimePerRepeat();
729 TEST_PRINT(stderr, "%d %s\n", __LINE__,
730 QString(" work sets interations=%1").arg(completedIterations).toLatin1().data());
731 selectedJob->setCompletedIterations(completedIterations);
732 }
733 }
734
735 // Set the job's startupTime, but only for the first time the job will be scheduled.
736 // This will be used by the scheduler's UI when displaying the job schedules.
737 if (!selectedJob->getStartupTime().isValid())
738 {
739 numStartups++;
740 selectedJob->setStartupTime(jobStartTime);
741 selectedJob->setStopTime(jobStopTime);
742 selectedJob->setStopReason(stopReason);
743 selectedJob->setState(SCHEDJOB_SCHEDULED);
744 scheduledJobs.append(selectedJob);
745 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString(" Scheduled: %1 %2 -> %3 %4 work done %5s")
746 .arg(selectedJob->getName()).arg(selectedJob->getStartupTime().toString("MM/dd hh:mm"))
747 .arg(selectedJob->getStopTime().toString("MM/dd hh:mm")).arg(selectedJob->getStopReason())
748 .arg(workDone[selectedJob]).toLatin1().data());
749 }
750 else
751 {
752 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString(" Added: %1 %2 -> %3 %4 work done %5s")
753 .arg(selectedJob->getName()).arg(selectedJob->getStartupTime().toString("MM/dd hh:mm"))
754 .arg(selectedJob->getStopTime().toString("MM/dd hh:mm")).arg(selectedJob->getStopReason())
755 .arg(workDone[selectedJob]).toLatin1().data());
756 }
757
758 // Compute if the simulated job should be considered complete because of work done.
759 if (selectedJob->getEstimatedTime() >= 0 &&
760 workDone[selectedJob] >= selectedJob->getEstimatedTime())
761 {
762 selectedJob->setState(SCHEDJOB_COMPLETE);
763 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString(" job %1 is complete")
764 .arg(selectedJob->getName()).toLatin1().data());
765 }
766 schedule.append(JobSchedule(jobs[copiedJobs.indexOf(selectedJob)], jobStartTime, jobStopTime, stopReason));
767 simEndTime = jobStopTime;
768 simTime = jobStopTime.addSecs(60);
769
770 // End the simulation if we've crossed endTime, or no further jobs could be started,
771 // or if we've simply run too long.
772 if (!simTime.isValid()) break;
773 if (endTime.isValid() && simTime.secsTo(endTime) < 0) break;
774
775 if (++iterations > std::max(20, numStartupCandidates))
776 {
777 exceededIterations = true;
778 TEST_PRINT(stderr, "%d %s\n", __LINE__, QString("ending simulation after %1 iterations")
779 .arg(iterations).toLatin1().data());
780
781 break;
782 }
783 if (simType == SIMULATE_EACH_JOB_ONCE)
784 {
785 bool allJobsProcessedOnce = true;
786 for (const auto job : simJobs)
787 {
788 if (allowJob(job, rescheduleAbortsImmediate, rescheduleAbortsQueue, rescheduleErrors) &&
789 !job->getStartupTime().isValid())
790 {
791 allJobsProcessedOnce = false;
792 break;
793 }
794 }
795 if (allJobsProcessedOnce)
796 {
797 TEST_PRINT(stderr, "%d ending simulation, all jobs processed once\n", __LINE__);
798 break;
799 }
800 }
801 }
802
803 // This simulation has been run using a deep-copy of the jobs list, so as not to interfere with
804 // some of their stored data. However, we do wish to update several fields of the "real" scheduleJobs.
805 // Note that the original jobs list and "copiedJobs" should be in the same order..
806 for (int i = 0; i < jobs.size(); ++i)
807 {
808 if (scheduledJobs.indexOf(copiedJobs[i]) >= 0)
809 {
810 // If this is a simulation where the job is already running, don't change its state or startup time.
811 if (jobs[i]->getState() != SCHEDJOB_BUSY)
812 {
813 jobs[i]->setState(SCHEDJOB_SCHEDULED);
814 jobs[i]->setStartupTime(copiedJobs[i]->getStartupTime());
815 }
816 // Can't set the standard completionTime as it affects getEstimatedTime()
817 jobs[i]->setStopTime(copiedJobs[i]->getStopTime());
818 jobs[i]->setStopReason(copiedJobs[i]->getStopReason());
819 }
820 }
821 // This should go after above loop. unsetEvaluation calls setState() which clears
822 // certain fields from the state for IDLE states.
823 unsetEvaluation(jobs);
824
825 return exceededIterations ? QDateTime() : simEndTime;
826}
827
828void GreedyScheduler::unsetEvaluation(const QList<SchedulerJob *> &jobs) const
829{
830 for (int i = 0; i < jobs.size(); ++i)
831 {
832 if (jobs[i]->getState() == SCHEDJOB_EVALUATION)
833 jobs[i]->setState(SCHEDJOB_IDLE);
834 }
835}
836
837QString GreedyScheduler::jobScheduleString(const JobSchedule &jobSchedule)
838{
839 return QString("%1\t%2 --> %3 \t%4")
840 .arg(jobSchedule.job->getName(), -10)
841 .arg(jobSchedule.startTime.toString("MM/dd hh:mm"),
842 jobSchedule.stopTime.toString("hh:mm"), jobSchedule.stopReason);
843}
844
845void GreedyScheduler::printSchedule(const QList<JobSchedule> &schedule)
846{
847 foreach (auto &line, schedule)
848 {
849 fprintf(stderr, "%s\n", QString("%1 %2 --> %3 (%4)")
850 .arg(jobScheduleString(line)).toLatin1().data());
851 }
852}
853
854} // namespace Ekos
QString i18n(const char *text, const TYPE &arg...)
Ekos is an advanced Astrophotography tool for Linux.
Definition align.cpp:83
@ SCHEDJOB_ABORTED
Job encountered a transitory issue while processing, and will be rescheduled.
@ SCHEDJOB_INVALID
Job has an incorrect configuration, and cannot proceed.
@ SCHEDJOB_ERROR
Job encountered a fatal issue while processing, and must be reset manually.
@ SCHEDJOB_COMPLETE
Job finished all required captures.
@ SCHEDJOB_EVALUATION
Job is being evaluated.
@ SCHEDJOB_SCHEDULED
Job was evaluated, and has a schedule.
@ SCHEDJOB_BUSY
Job is being processed.
@ SCHEDJOB_IDLE
Job was just created, and is not evaluated yet.
QAction * next(const QObject *recvr, const char *slot, QObject *parent)
QCA_EXPORT Logger * logger()
char * data()
QDateTime addSecs(qint64 s) const const
bool isValid() const const
qint64 secsTo(const QDateTime &other) const const
QString toString(QStringView format, QCalendar cal) const const
qint64 elapsed() const const
iterator end()
iterator find(const Key &key)
void append(QList< T > &&value)
void clear()
bool empty() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
qsizetype size() const const
QString arg(Args &&... args) const const
QByteArray toLatin1() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.