[RFC PATCH] drm/scheduler: use idle time to do better loadbalance

Nirmoy Das nirmoy.aiemd at gmail.com
Tue Jan 14 20:06:17 UTC 2020


This patch adds required fields to drm_sched_job and drm_gpu_scheduler
structure to cumulatively calculate amount of time a drm_gpu_scheduler
spend on serving a job.

Using least used drm scheduler to choose a run queue
improves drm_sched_entity_get_free_sched()'s job distribution

Below are test results after running amdgpu_test from mesa drm

Before this patch:

sched_name     num of many times it got scheduled
=========      ==================================
sdma0	       314
sdma1          32
comp_1.0.0     56

After this patch:

sched_name     num of many times it got scheduled
=========      ==================================
sdma0	       210
sdma1          211
comp_1.0.0     10
comp_1.0.1     10
comp_1.1.0     14
comp_1.1.1     6
comp_1.2.0     12
comp_1.2.1     14
comp_1.3.0     8
comp_1.3.1     10

Signed-off-by: Nirmoy Das <nirmoy.das at amd.com>
---
 drivers/gpu/drm/scheduler/sched_entity.c | 23 +++++++++++++++++------
 drivers/gpu/drm/scheduler/sched_main.c   |  7 +++++--
 include/drm/gpu_scheduler.h              | 11 ++++++++---
 3 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c
index 2e3a058fc239..e71cfe47b0e1 100644
--- a/drivers/gpu/drm/scheduler/sched_entity.c
+++ b/drivers/gpu/drm/scheduler/sched_entity.c
@@ -130,9 +130,10 @@ static struct drm_sched_rq *
 drm_sched_entity_get_free_sched(struct drm_sched_entity *entity)
 {
 	struct drm_sched_rq *rq = NULL;
-	unsigned int min_jobs = UINT_MAX, num_jobs;
-	int i;
+	uint64_t min_time_consumed = -1, total_consumed_time;
+	int i, skip_idx = 0;
 
+	spin_lock(&entity->sched_list[0]->last_picked_lock);
 	for (i = 0; i < entity->num_sched_list; ++i) {
 		struct drm_gpu_scheduler *sched = entity->sched_list[i];
 
@@ -141,13 +142,23 @@ drm_sched_entity_get_free_sched(struct drm_sched_entity *entity)
 			continue;
 		}
 
-		num_jobs = atomic_read(&sched->num_jobs);
-		if (num_jobs < min_jobs) {
-			min_jobs = num_jobs;
+		total_consumed_time = atomic64_read(&sched->total_consumed_time);
+		if (total_consumed_time < min_time_consumed && !sched->last_picked) {
+			min_time_consumed = total_consumed_time;
 			rq = &entity->sched_list[i]->sched_rq[entity->priority];
+			sched->last_picked = true;
+			skip_idx = i;
 		}
 	}
 
+	for (i = 0; i < entity->num_sched_list; ++i) {
+		struct drm_gpu_scheduler *sched = entity->sched_list[i];
+
+		if (i != skip_idx)
+			sched->last_picked = false;
+	}
+
+	spin_unlock(&entity->sched_list[0]->last_picked_lock);
 	return rq;
 }
 
@@ -498,7 +509,7 @@ void drm_sched_entity_push_job(struct drm_sched_job *sched_job,
 	bool first;
 
 	trace_drm_sched_job(sched_job, entity);
-	atomic_inc(&entity->rq->sched->num_jobs);
+	sched_job->start_time = ktime_get_ns();
 	WRITE_ONCE(entity->last_user, current->group_leader);
 	first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
 
diff --git a/drivers/gpu/drm/scheduler/sched_main.c b/drivers/gpu/drm/scheduler/sched_main.c
index 3fad5876a13f..8f3c6f62c7dc 100644
--- a/drivers/gpu/drm/scheduler/sched_main.c
+++ b/drivers/gpu/drm/scheduler/sched_main.c
@@ -653,13 +653,14 @@ static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb)
 	struct drm_sched_job *s_job = container_of(cb, struct drm_sched_job, cb);
 	struct drm_sched_fence *s_fence = s_job->s_fence;
 	struct drm_gpu_scheduler *sched = s_fence->sched;
+	uint64_t end = ktime_get_ns();
 
 	atomic_dec(&sched->hw_rq_count);
-	atomic_dec(&sched->num_jobs);
 
 	trace_drm_sched_process_job(s_fence);
 
 	drm_sched_fence_finished(s_fence);
+	atomic64_add((end - s_job->start_time), &sched->total_consumed_time);
 	wake_up_interruptible(&sched->wake_up_worker);
 }
 
@@ -828,9 +829,10 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
 	init_waitqueue_head(&sched->job_scheduled);
 	INIT_LIST_HEAD(&sched->ring_mirror_list);
 	spin_lock_init(&sched->job_list_lock);
+	spin_lock_init(&sched->last_picked_lock);
 	atomic_set(&sched->hw_rq_count, 0);
+	atomic64_set(&sched->total_consumed_time, 0);
 	INIT_DELAYED_WORK(&sched->work_tdr, drm_sched_job_timedout);
-	atomic_set(&sched->num_jobs, 0);
 	atomic64_set(&sched->job_id_count, 0);
 
 	/* Each scheduler will run on a seperate kernel thread */
@@ -843,6 +845,7 @@ int drm_sched_init(struct drm_gpu_scheduler *sched,
 	}
 
 	sched->ready = true;
+	sched->last_picked = false;
 	return 0;
 }
 EXPORT_SYMBOL(drm_sched_init);
diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h
index 96a1a1b7526e..9b71facad1cd 100644
--- a/include/drm/gpu_scheduler.h
+++ b/include/drm/gpu_scheduler.h
@@ -191,6 +191,7 @@ struct drm_sched_job {
 	struct dma_fence_cb		finish_cb;
 	struct list_head		node;
 	uint64_t			id;
+	uint64_t			start_time;
 	atomic_t			karma;
 	enum drm_sched_priority		s_priority;
 	struct drm_sched_entity  *entity;
@@ -261,9 +262,11 @@ struct drm_sched_backend_ops {
  * @job_list_lock: lock to protect the ring_mirror_list.
  * @hang_limit: once the hangs by a job crosses this limit then it is marked
  *              guilty and it will be considered for scheduling further.
- * @num_jobs: the number of jobs in queue in the scheduler
  * @ready: marks if the underlying HW is ready to work
  * @free_guilty: A hit to time out handler to free the guilty job.
+ * @last_picked: Indicate if the sched was pick up on last loadbalance decision
+ * @last_picked_lock: lock to protect the last_picked
+ * @total_consumed_time: Total time consumed by this sched executing jobs
  *
  * One scheduler is implemented for each hardware ring.
  */
@@ -282,9 +285,11 @@ struct drm_gpu_scheduler {
 	struct list_head		ring_mirror_list;
 	spinlock_t			job_list_lock;
 	int				hang_limit;
-	atomic_t                        num_jobs;
-	bool			ready;
+	bool				ready;
 	bool				free_guilty;
+	bool				last_picked;
+	spinlock_t			last_picked_lock;
+	atomic64_t			total_consumed_time;
 };
 
 int drm_sched_init(struct drm_gpu_scheduler *sched,
-- 
2.24.1



More information about the amd-gfx mailing list