GPUs do not always implement preemption and DRM scheduler definitely
does not support it at the front end scheduling level. This means
execution quanta can be quite long and is controlled by userspace,
consequence of which is picking the "wrong" entity to run can have a
larger negative effect than it would have with a virtual runtime based CPU
scheduler.
Another important consideration is that rendering clients often have
shallow submission queues, meaning they will be entering and exiting the
scheduler's runnable queue often.
Relevant scenario here is what happens when an entity re-joins the
runnable queue with other entities already present. One cornerstone of the
virtual runtime algorithm is to let it re-join at the head and rely on the
virtual runtime accounting and timeslicing to sort it out.
However, as explained above, this may not work perfectly in the GPU world.
Entity could always get to overtake the existing entities, or not,
depending on the submission order and rbtree equal key insertion
behaviour.
Allow interactive jobs to overtake entities already queued up for the
limited case when interactive entity is re-joining the queue after
being idle.
This gives more opportunity for the compositors to have their rendering
executed before the GPU hogs even if they have been configured with the
same scheduling priority.
To classify a client as interactive we look at its average job duration
versus the average for the whole scheduler. We can track this easily by
plugging into the existing job runtime tracking and applying the
exponential moving average window on the past submissions. Then, all other
things being equal, we let the more interactive jobs go first.
Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
Cc: Christian König <christian.koenig@amd.com>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: Matthew Brost <matthew.brost@intel.com>
Cc: Philipp Stanner <phasta@kernel.org>
Cc: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
Acked-by: Danilo Krummrich <dakr@kernel.org>
Tested-by: Vitaly Prosyak <vitaly.prosyak@amd.com>
Signed-off-by: Philipp Stanner <phasta@kernel.org>
Link: https://patch.msgid.link/20260417103744.76020-12-tvrtko.ursulin@igalia.com
kref_init(&stats->kref);
spin_lock_init(&stats->lock);
+ ewma_drm_sched_avgtime_init(&stats->avg_job_us);
return stats;
}
* @job: Scheduler job to account.
*
* Accounts the execution time of @job to its respective entity stats object.
+ *
+ * Return: Job's real duration in micro seconds.
*/
-void drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job)
+ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job)
{
struct drm_sched_entity_stats *stats = job->entity_stats;
struct drm_sched_fence *s_fence = job->s_fence;
- ktime_t start, end;
+ ktime_t start, end, duration;
start = dma_fence_timestamp(&s_fence->scheduled);
end = dma_fence_timestamp(&s_fence->finished);
+ duration = ktime_sub(end, start);
spin_lock(&stats->lock);
- stats->runtime = ktime_add(stats->runtime, ktime_sub(end, start));
+ stats->runtime = ktime_add(stats->runtime, duration);
+ ewma_drm_sched_avgtime_add(&stats->avg_job_us, ktime_to_us(duration));
spin_unlock(&stats->lock);
+
+ return duration;
}
/**
* @runtime: time entity spent on the GPU.
* @prev_runtime: previous @runtime used to get the runtime delta.
* @vruntime: virtual runtime as accumulated by the fair algorithm.
+ * @avg_job_us: average job duration.
*
* Because jobs and entities have decoupled lifetimes, ie. we cannot access the
* entity once the job has been de-queued, and we do need know how much GPU time
ktime_t runtime;
ktime_t prev_runtime;
ktime_t vruntime;
+
+ struct ewma_drm_sched_avgtime avg_job_us;
};
/* Used to choose between FIFO and RR job-scheduling */
kref_put(&stats->kref, drm_sched_entity_stats_release);
}
-void drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job);
+ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job);
#endif
struct drm_sched_job *job;
while ((job = drm_sched_get_finished_job(sched))) {
- drm_sched_entity_stats_job_add_gpu_time(job);
+ ktime_t duration = drm_sched_entity_stats_job_add_gpu_time(job);
+
+ /* Serialized by the worker. */
+ ewma_drm_sched_avgtime_add(&sched->avg_job_us,
+ ktime_to_us(duration));
+
sched->ops->free_job(job);
}
atomic_set(&sched->_score, 0);
atomic64_set(&sched->job_id_count, 0);
sched->pause_submit = false;
+ ewma_drm_sched_avgtime_init(&sched->avg_job_us);
sched->ready = true;
return 0;
enum drm_sched_priority rq_prio)
{
struct drm_sched_entity_stats *stats = entity->stats;
+ struct drm_gpu_scheduler *sched = entity->rq->sched;
enum drm_sched_priority prio = entity->priority;
+ unsigned long avg_us, sched_avg_us;
ktime_t vruntime;
BUILD_BUG_ON(DRM_SCHED_PRIORITY_NORMAL < DRM_SCHED_PRIORITY_HIGH);
spin_lock(&stats->lock);
vruntime = stats->vruntime;
+ avg_us = ewma_drm_sched_avgtime_read(&stats->avg_job_us);
+ /*
+ * Unlocked read of the scheduler average is fine since it is just
+ * heuristics and data type is a natural word size.
+ */
+ sched_avg_us = ewma_drm_sched_avgtime_read(&sched->avg_job_us);
/*
* Special handling for entities which were picked from the top of the
if (prio > rq_prio) {
/*
* Lower priority should not overtake higher when re-
- * joining at the top of the queue.
+ * joining at the top of the queue so push it back
+ * somewhere behind the "middle" of the run-queue,
+ * proportional to the scheduler and entity average job
+ * durations.
*/
- vruntime = ns_to_ktime(prio - rq_prio);
+ vruntime = us_to_ktime((1 + avg_us + sched_avg_us) <<
+ vruntime_shift[prio]);
} else if (prio < rq_prio) {
/*
* Higher priority can go first.
*/
vruntime = -ns_to_ktime(rq_prio - prio);
+ } else {
+ /* Favour entity with shorter jobs (interactivity). */
+ if (avg_us <= sched_avg_us)
+ vruntime = -ns_to_ktime(1);
+ else
+ vruntime = ns_to_ktime(1);
}
}
#define _DRM_GPU_SCHEDULER_H_
#include <drm/spsc_queue.h>
+#include <linux/average.h>
#include <linux/dma-fence.h>
#include <linux/completion.h>
#include <linux/xarray.h>
#include <linux/workqueue.h>
+DECLARE_EWMA(drm_sched_avgtime, 6, 4);
+
#define MAX_WAIT_SCHED_ENTITY_Q_EMPTY msecs_to_jiffies(1000)
/**
* @job_id_count: used to assign unique id to the each job.
* @submit_wq: workqueue used to queue @work_run_job and @work_free_job
* @timeout_wq: workqueue used to queue @work_tdr
+ * @avg_job_us: Average job duration.
* @work_run_job: work which calls run_job op of each scheduler.
* @work_free_job: work which calls free_job op of each scheduler.
* @work_tdr: schedules a delayed call to @drm_sched_job_timedout after the
atomic64_t job_id_count;
struct workqueue_struct *submit_wq;
struct workqueue_struct *timeout_wq;
+ struct ewma_drm_sched_avgtime avg_job_us;
struct work_struct work_run_job;
struct work_struct work_free_job;
struct delayed_work work_tdr;