drm/scheduler: implement hw time accounting
Subsystems (4)
Christian König
reviewerPATCH 1/3 drm/scheduler: implement hw time accounting
Multiple drivers came up with the requirement to measure how much time each submission spend on the hw.
A previous attempt of accounting this had to be reverted because hw submissions can live longer than the entity originally issuing them.
Amdgpu on the other hand solves this task by keeping track of all the submissions and calculating how much time they have used on demand.
Move this approach over into the scheduler to provide an easy to use interface for all drivers.
--- drivers/gpu/drm/scheduler/sched_entity.c | 89 ++++++++++++++++++++++-- drivers/gpu/drm/scheduler/sched_fence.c | 19 +++++ include/drm/gpu_scheduler.h | 29 ++++++++ 3 files changed, 133 insertions(+), 4 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +85 -4
@@ -62,7 +62,9 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,unsigned int num_sched_list, atomic_t *guilty) { -if (!(entity && sched_list && (num_sched_list ==0 || sched_list[0 ]))) + unsigned int i, num_submissions; + +if (!entity || !sched_list)return -EINVAL ;memset (entity,0 ,sizeof (struct drm_sched_entity ));
@@ -75,9 +77,6 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,RCU_INIT_POINTER (entity->last_scheduled,NULL );RB_CLEAR_NODE (&entity->rb_tree_node); -if (num_sched_list) - entity->rq = &sched_list[0 ]->sched_rq[entity->priority]; -init_completion (&entity->entity_idle);/* We start in an idle state. */
@@ -88,11 +87,68 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,atomic_set (&entity->fence_seq,0 ); entity->fence_context =dma_fence_context_alloc (2 ); +spin_lock_init (&entity->accounting_lock); + +/* We need to be able to init even unused entities */ +if (!num_sched_list) +return 0 ; + + entity->rq = &sched_list[0 ]->sched_rq[entity->priority]; + + num_submissions =0 ; +for (i =0 ; i < num_sched_list; ++i) { +if (!sched_list[i]) +return -EINVAL ; + + num_submissions =max (num_submissions, + sched_list[i]->hw_submission_limit); + } + +/* FIXME: Entity initialized before the scheduler. */ +if (!num_submissions) +return 0 ; + + entity->num_hw_submissions = num_submissions; + entity->hw_submissions =kcalloc (num_submissions,sizeof (void *), +GFP_KERNEL ); +if (!entity->hw_submissions) +return -ENOMEM ;return 0 ; }EXPORT_SYMBOL (drm_sched_entity_init); +/** +* drm_sched_entity_time_spend - Accumulated hw time used by this entity +* @entity: scheduler entity to check +* +* Get the current accumulated hw time used by all submissions made through this +* entity. +*/ +ktime_t drm_sched_entity_time_spend (struct drm_sched_entity *entity) +{ +ktime_t result; +unsigned int i; + +if (!entity->num_hw_submissions) +return ns_to_ktime (0 ); + +spin_lock (&entity->accounting_lock); + result = entity->hw_time_used; +for (i =0 ; i < entity->num_hw_submissions; ++i) { +struct drm_sched_fence *fence = entity->hw_submissions[i]; + +if (!fence) +continue ; + + result =ktime_add (result,drm_sched_fence_time_spend (fence)); + } +spin_unlock (&entity->accounting_lock); + +return result; +} +EXPORT_SYMBOL (drm_sched_entity_time_spend); +/** * drm_sched_entity_modify_sched - Modify sched of an entity * @entity: scheduler entity to init
@@ -288,6 +344,8 @@ EXPORT_SYMBOL(drm_sched_entity_flush);*/ void drm_sched_entity_fini (struct drm_sched_entity *entity) { +unsigned int i; +/* * If consumption of existing IBs wasn't completed. Forcefully remove * them here. Also makes sure that the scheduler won't touch this entity
@@ -303,6 +361,9 @@ void drm_sched_entity_fini(struct drm_sched_entity *entity)dma_fence_put (rcu_dereference_check (entity->last_scheduled, true));RCU_INIT_POINTER (entity->last_scheduled,NULL ); +for (i =0 ; i < entity->num_hw_submissions; ++i) +dma_fence_put (&entity->hw_submissions[i]->scheduled); +kfree (entity->hw_submissions); }EXPORT_SYMBOL (drm_sched_entity_fini);
@@ -427,6 +488,8 @@ drm_sched_job_dependency(struct drm_sched_job *job,struct drm_sched_job *drm_sched_entity_pop_job (struct drm_sched_entity *entity) {struct drm_sched_job *sched_job; +struct drm_sched_fence *fence; +unsigned int idx; sched_job =to_drm_sched_job (spsc_queue_peek (&entity->job_queue));if (!sched_job)
@@ -475,6 +538,24 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)*/ sched_job->entity =NULL ; +if (entity->num_hw_submissions) { + fence = sched_job->s_fence; +dma_fence_get (&fence->scheduled); + idx = fence->scheduled.seqno; + idx &= entity->num_hw_submissions -1 ; + +spin_lock (&entity->accounting_lock); +swap (fence, entity->hw_submissions[idx]); +if (fence) { +ktime_t spend =drm_sched_fence_time_spend (fence); + + entity->hw_time_used =ktime_add (entity->hw_time_used, + spend); +dma_fence_put (&fence->scheduled); + } +spin_unlock (&entity->accounting_lock); + } +return sched_job; }
drivers/gpu/drm/scheduler/sched_fence.c +19 -0
@@ -60,6 +60,25 @@ void drm_sched_fence_finished(struct drm_sched_fence *fence, int result)dma_fence_signal (&fence->finished); } +/** +* drm_sched_fence_time_spend - calculate time spend on the hw +* @fence: fence to use +* +* Calculate how much time this fence has spend using the hw. +*/ +ktime_t drm_sched_fence_time_spend (struct drm_sched_fence *fence) +{ +/* When the fence is not even scheduled, it can't have spend time */ +if (!test_bit (DMA_FENCE_FLAG_TIMESTAMP_BIT , &fence->scheduled.flags)) +return ns_to_ktime (0 ); + +/* When it is still running account how much already spend */ +if (!test_bit (DMA_FENCE_FLAG_TIMESTAMP_BIT , &fence->finished.flags)) +return ktime_sub (ktime_get (), fence->scheduled.timestamp); + +return ktime_sub (fence->finished.timestamp, fence->scheduled.timestamp); +} +static const char *drm_sched_fence_get_driver_name (struct dma_fence *fence) {return "drm_sched" ;
include/drm/gpu_scheduler.h +29 -0
@@ -239,6 +239,33 @@ struct drm_sched_entity {*/ struct rb_node rb_tree_node; +/** +* @accounting_lock: +* +* Protecting the array of hw submissions and the time spend +*/ +spinlock_t accounting_lock; + +/** +* @hw_time_used: +* +* how much hw time previous submissions have already used +*/ +ktime_t hw_time_used; + +/** +* @max_hw_submissions: +* +* Maximum number of submissions currently in worked on bye the hw +*/ +unsigned int num_hw_submissions; + +/** +* @hw_submissions: +* +* Scheduler fences of the hw submissions in flight +*/ +struct drm_sched_fence **hw_submissions; };/**
@@ -572,6 +599,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,struct drm_gpu_scheduler **sched_list, unsigned int num_sched_list, atomic_t *guilty); +ktime_t drm_sched_entity_time_spend (struct drm_sched_entity *entity);long drm_sched_entity_flush (struct drm_sched_entity *entity,long timeout);void drm_sched_entity_fini (struct drm_sched_entity *entity);void drm_sched_entity_destroy (struct drm_sched_entity *entity);
@@ -593,6 +621,7 @@ void drm_sched_fence_free(struct drm_sched_fence *fence);void drm_sched_fence_scheduled (struct drm_sched_fence *fence);void drm_sched_fence_finished (struct drm_sched_fence *fence,int result); +ktime_t drm_sched_fence_time_spend (struct drm_sched_fence *fence);unsigned long drm_sched_suspend_timeout (struct drm_gpu_scheduler *sched); void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched,
Signature · 1 line
2.34.1
Christian König
reviewerPATCH 2/3 drm/amdgpu: mostly revert "fix force APP kill hang(v4)"
This reverts commit 8ee3a52e3f35e064a3bf82f21dc74ddaf9843648.
The new amdgpu_ctx_mgr_entity_fini() was never called, so it was pure coincident that this patch didn't cause a crash. Since the workaround shouldn't be needed any more just mostly revert the changes to amdgpu.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 59 ++----------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 1 - drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 2 +- 3 files changed, 5 insertions(+), 57 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +4 -55
@@ -267,7 +267,7 @@ static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_ctx_entity *entity) res =ktime_add (res,amdgpu_ctx_fence_time (entity->fences[i]));dma_fence_put (entity->fences[i]); } - +drm_sched_entity_destroy (&entity->entity);kfree (entity);return res; }
@@ -476,24 +476,6 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,return r; } -static void amdgpu_ctx_do_release (struct kref *ref) -{ -struct amdgpu_ctx *ctx; -u32 i, j; - - ctx = container_of(ref, struct amdgpu_ctx, refcount); -for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) { -for (j =0 ; j < amdgpu_ctx_num_entities[i]; ++j) { -if (!ctx->entities[i][j]) -continue ; - -drm_sched_entity_destroy (&ctx->entities[i][j]->entity); - } - } - -amdgpu_ctx_fini (ref); -} -static int amdgpu_ctx_free (struct amdgpu_fpriv *fpriv,uint32_t id) {struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
@@ -502,7 +484,7 @@ static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)mutex_lock (&mgr->lock); ctx =idr_remove (&mgr->ctx_handles, id);if (ctx) -kref_put (&ctx->refcount, amdgpu_ctx_do_release); +kref_put (&ctx->refcount, amdgpu_ctx_fini);mutex_unlock (&mgr->lock);return ctx ?0 : -EINVAL ; }
@@ -712,7 +694,7 @@ int amdgpu_ctx_put(struct amdgpu_ctx *ctx)if (ctx ==NULL )return -EINVAL ; -kref_put (&ctx->refcount, amdgpu_ctx_do_release); +kref_put (&ctx->refcount, amdgpu_ctx_fini);return 0 ; }
@@ -881,45 +863,12 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)return timeout; } -void amdgpu_ctx_mgr_entity_fini (struct amdgpu_ctx_mgr *mgr) -{ -struct amdgpu_ctx *ctx; -struct idr *idp; -uint32_t id, i, j; - - idp = &mgr->ctx_handles; - -idr_for_each_entry (idp, ctx, id) { -if (kref_read (&ctx->refcount) !=1 ) { -DRM_ERROR ("ctx %p is still alive\n" , ctx); -continue ; - } - -for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) { -for (j =0 ; j < amdgpu_ctx_num_entities[i]; ++j) { -struct drm_sched_entity *entity; - -if (!ctx->entities[i][j]) -continue ; - - entity = &ctx->entities[i][j]->entity; -drm_sched_entity_fini (entity); - } - } - } -} -void amdgpu_ctx_mgr_fini (struct amdgpu_ctx_mgr *mgr) {struct amdgpu_ctx *ctx; -struct idr *idp;uint32_t id; -amdgpu_ctx_mgr_entity_fini (mgr); - - idp = &mgr->ctx_handles; - -idr_for_each_entry (idp, ctx, id) { +idr_for_each_entry (&mgr->ctx_handles, ctx, id) {if (kref_put (&ctx->refcount, amdgpu_ctx_fini) !=1 )DRM_ERROR ("ctx %p is still alive\n" , ctx); }
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -1
@@ -91,7 +91,6 @@ int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,void amdgpu_ctx_mgr_init (struct amdgpu_ctx_mgr *mgr,struct amdgpu_device *adev); -void amdgpu_ctx_mgr_entity_fini (struct amdgpu_ctx_mgr *mgr);long amdgpu_ctx_mgr_entity_flush (struct amdgpu_ctx_mgr *mgr,long timeout);void amdgpu_ctx_mgr_fini (struct amdgpu_ctx_mgr *mgr); void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1 -1
@@ -1278,6 +1278,7 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,return ;pm_runtime_get_sync (dev->dev); +amdgpu_ctx_mgr_fini (&fpriv->ctx_mgr);if (amdgpu_device_ip_get_ip_block (adev,AMD_IP_BLOCK_TYPE_UVD ) !=NULL )amdgpu_uvd_free_handles (adev, file_priv);
@@ -1299,7 +1300,6 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,amdgpu_bo_unreserve (pd); } -amdgpu_ctx_mgr_fini (&fpriv->ctx_mgr);amdgpu_vm_fini (adev, &fpriv->vm);if (pasid)
Signature · 1 line
2.34.1
Re: [PATCH 2/3] drm/amdgpu: mostly revert "fix force APP kill hang(v4)"
Quoted 7 lines · Christian König
This reverts commit
8ee3a52e3f35e064a3bf82f21dc74ddaf9843648.The new
amdgpu_ctx_mgr_entity_fini()was never called, so it was pure coincident that this patch didn't cause a crash. Since the workaround shouldn't be needed any more just mostly revert the changes to amdgpu.Signed-off-by Christian König <[email protected]>
Add a fixes-tag,
Regards,
Luben
Quoted 141 lines
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 59 ++----------------------- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h | 1 - drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c | 2 +- 3 files changed, 5 insertions(+), 57 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +4
-55@@ -267,7 +267,7 @@ static ktime_t amdgpu_ctx_fini_entity(struct amdgpu_ctx_entity *entity) res =ktime_add (res,amdgpu_ctx_fence_time (entity->fences[i]));dma_fence_put (entity->fences[i]); } - +drm_sched_entity_destroy (&entity->entity);kfree (entity);return res; }@@ -476,24 +476,6 @@ static int amdgpu_ctx_alloc(struct amdgpu_device *adev,return r; } -static void amdgpu_ctx_do_release (struct kref *ref) -{ -struct amdgpu_ctx *ctx; -u32 i, j; - - ctx = container_of(ref, struct amdgpu_ctx, refcount); -for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) { -for (j =0 ; j < amdgpu_ctx_num_entities[i]; ++j) { -if (!ctx->entities[i][j]) -continue ; - -drm_sched_entity_destroy (&ctx->entities[i][j]->entity); - } - } - -amdgpu_ctx_fini (ref); -} -static int amdgpu_ctx_free (struct amdgpu_fpriv *fpriv,uint32_t id) {struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;@@ -502,7 +484,7 @@ static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)mutex_lock (&mgr->lock); ctx =idr_remove (&mgr->ctx_handles, id);if (ctx) -kref_put (&ctx->refcount, amdgpu_ctx_do_release); +kref_put (&ctx->refcount, amdgpu_ctx_fini);mutex_unlock (&mgr->lock);return ctx ?0 : -EINVAL ; }@@ -712,7 +694,7 @@ int amdgpu_ctx_put(struct amdgpu_ctx *ctx)if (ctx ==NULL )return -EINVAL ; -kref_put (&ctx->refcount, amdgpu_ctx_do_release); +kref_put (&ctx->refcount, amdgpu_ctx_fini);return 0 ; }@@ -881,45 +863,12 @@ long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout)return timeout; } -void amdgpu_ctx_mgr_entity_fini (struct amdgpu_ctx_mgr *mgr) -{ -struct amdgpu_ctx *ctx; -struct idr *idp; -uint32_t id, i, j; - - idp = &mgr->ctx_handles; - -idr_for_each_entry (idp, ctx, id) { -if (kref_read (&ctx->refcount) !=1 ) { -DRM_ERROR ("ctx %p is still alive\n" , ctx); -continue ; - } - -for (i =0 ; i <AMDGPU_HW_IP_NUM ; ++i) { -for (j =0 ; j < amdgpu_ctx_num_entities[i]; ++j) { -struct drm_sched_entity *entity; - -if (!ctx->entities[i][j]) -continue ; - - entity = &ctx->entities[i][j]->entity; -drm_sched_entity_fini (entity); - } - } - } -} -void amdgpu_ctx_mgr_fini (struct amdgpu_ctx_mgr *mgr) {struct amdgpu_ctx *ctx; -struct idr *idp;uint32_t id; -amdgpu_ctx_mgr_entity_fini (mgr); - - idp = &mgr->ctx_handles; - -idr_for_each_entry (idp, ctx, id) { +idr_for_each_entry (&mgr->ctx_handles, ctx, id) {if (kref_put (&ctx->refcount, amdgpu_ctx_fini) !=1 )DRM_ERROR ("ctx %p is still alive\n" , ctx); }drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0
-1@@ -91,7 +91,6 @@ int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,void amdgpu_ctx_mgr_init (struct amdgpu_ctx_mgr *mgr,struct amdgpu_device *adev); -void amdgpu_ctx_mgr_entity_fini (struct amdgpu_ctx_mgr *mgr);long amdgpu_ctx_mgr_entity_flush (struct amdgpu_ctx_mgr *mgr,long timeout);void amdgpu_ctx_mgr_fini (struct amdgpu_ctx_mgr *mgr); void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +1
-1@@ -1278,6 +1278,7 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,return ;pm_runtime_get_sync (dev->dev); +amdgpu_ctx_mgr_fini (&fpriv->ctx_mgr);if (amdgpu_device_ip_get_ip_block (adev,AMD_IP_BLOCK_TYPE_UVD ) !=NULL )amdgpu_uvd_free_handles (adev, file_priv);@@ -1299,7 +1300,6 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,amdgpu_bo_unreserve (pd); } -amdgpu_ctx_mgr_fini (&fpriv->ctx_mgr);amdgpu_vm_fini (adev, &fpriv->vm);if (pasid)
Christian König
reviewerPATCH 3/3 drm/amdgpu: use new scheduler accounting
In reply to Christian KönigInstead of implementing this ourself.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 52 ++++--------------------- 1 file changed, 8 insertions(+), 44 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +8 -44
@@ -163,41 +163,6 @@ static unsigned int amdgpu_ctx_get_hw_prio(struct amdgpu_ctx *ctx, u32 hw_ip)return hw_prio; } -/* Calculate the time spend on the hw */ -static ktime_t amdgpu_ctx_fence_time (struct dma_fence *fence) -{ -struct drm_sched_fence *s_fence; - -if (!fence) -return ns_to_ktime (0 ); - -/* When the fence is not even scheduled it can't have spend time */ - s_fence =to_drm_sched_fence (fence); -if (!test_bit (DMA_FENCE_FLAG_TIMESTAMP_BIT , &s_fence->scheduled.flags)) -return ns_to_ktime (0 ); - -/* When it is still running account how much already spend */ -if (!test_bit (DMA_FENCE_FLAG_TIMESTAMP_BIT , &s_fence->finished.flags)) -return ktime_sub (ktime_get (), s_fence->scheduled.timestamp); - -return ktime_sub (s_fence->finished.timestamp, - s_fence->scheduled.timestamp); -} - -static ktime_t amdgpu_ctx_entity_time (struct amdgpu_ctx *ctx, -struct amdgpu_ctx_entity *centity) -{ -ktime_t res =ns_to_ktime (0 ); -uint32_t i; - -spin_lock (&ctx->ring_lock); -for (i =0 ; i < amdgpu_sched_jobs; i++) { - res =ktime_add (res,amdgpu_ctx_fence_time (centity->fences[i])); - } -spin_unlock (&ctx->ring_lock); -return res; -} -static int amdgpu_ctx_init_entity (struct amdgpu_ctx *ctx,u32 hw_ip,const u32 ring) {
@@ -257,16 +222,15 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,static ktime_t amdgpu_ctx_fini_entity (struct amdgpu_ctx_entity *entity) { -ktime_t res =ns_to_ktime (0 ); +ktime_t res;int i;if (!entity) -return res; +return ns_to_ktime (0 ); -for (i =0 ; i < amdgpu_sched_jobs; ++i) { - res =ktime_add (res,amdgpu_ctx_fence_time (entity->fences[i])); +for (i =0 ; i < amdgpu_sched_jobs; ++i)dma_fence_put (entity->fences[i]); - } + res =drm_sched_entity_time_spend (&entity->entity);drm_sched_entity_destroy (&entity->entity);kfree (entity);return res;
@@ -718,9 +682,6 @@ uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, centity->sequence++;spin_unlock (&ctx->ring_lock); -atomic64_add (ktime_to_ns (amdgpu_ctx_fence_time (other)), - &ctx->mgr->time_spend[centity->hw_ip]); -dma_fence_put (other);return seq; }
@@ -900,12 +861,15 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,for (hw_ip =0 ; hw_ip <AMDGPU_HW_IP_NUM ; ++hw_ip) {for (i =0 ; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {struct amdgpu_ctx_entity *centity; +struct drm_sched_entity *entity;ktime_t spend; centity = ctx->entities[hw_ip][i];if (!centity)continue ; - spend =amdgpu_ctx_entity_time (ctx, centity); + + entity = ¢ity->entity; + spend =drm_sched_entity_time_spend (entity); usage[hw_ip] =ktime_add (usage[hw_ip], spend); } }
Signature · 1 line
2.34.1
Re: [PATCH 3/3] drm/amdgpu: use new scheduler accounting
Quoted 1 line · Christian König
Instead of implementing this ourself.
Spellcheck: "ourselves".
Regards,
Luben
Quoted 100 lines
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 52 ++++--------------------- 1 file changed, 8 insertions(+), 44 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +8
-44@@ -163,41 +163,6 @@ static unsigned int amdgpu_ctx_get_hw_prio(struct amdgpu_ctx *ctx, u32 hw_ip)return hw_prio; } -/* Calculate the time spend on the hw */ -static ktime_t amdgpu_ctx_fence_time (struct dma_fence *fence) -{ -struct drm_sched_fence *s_fence; - -if (!fence) -return ns_to_ktime (0 ); - -/* When the fence is not even scheduled it can't have spend time */ - s_fence =to_drm_sched_fence (fence); -if (!test_bit (DMA_FENCE_FLAG_TIMESTAMP_BIT , &s_fence->scheduled.flags)) -return ns_to_ktime (0 ); - -/* When it is still running account how much already spend */ -if (!test_bit (DMA_FENCE_FLAG_TIMESTAMP_BIT , &s_fence->finished.flags)) -return ktime_sub (ktime_get (), s_fence->scheduled.timestamp); - -return ktime_sub (s_fence->finished.timestamp, - s_fence->scheduled.timestamp); -} - -static ktime_t amdgpu_ctx_entity_time (struct amdgpu_ctx *ctx, -struct amdgpu_ctx_entity *centity) -{ -ktime_t res =ns_to_ktime (0 ); -uint32_t i; - -spin_lock (&ctx->ring_lock); -for (i =0 ; i < amdgpu_sched_jobs; i++) { - res =ktime_add (res,amdgpu_ctx_fence_time (centity->fences[i])); - } -spin_unlock (&ctx->ring_lock); -return res; -} -static int amdgpu_ctx_init_entity (struct amdgpu_ctx *ctx,u32 hw_ip,const u32 ring) {@@ -257,16 +222,15 @@ static int amdgpu_ctx_init_entity(struct amdgpu_ctx *ctx, u32 hw_ip,static ktime_t amdgpu_ctx_fini_entity (struct amdgpu_ctx_entity *entity) { -ktime_t res =ns_to_ktime (0 ); +ktime_t res;int i;if (!entity) -return res; +return ns_to_ktime (0 ); -for (i =0 ; i < amdgpu_sched_jobs; ++i) { - res =ktime_add (res,amdgpu_ctx_fence_time (entity->fences[i])); +for (i =0 ; i < amdgpu_sched_jobs; ++i)dma_fence_put (entity->fences[i]); - } + res =drm_sched_entity_time_spend (&entity->entity);drm_sched_entity_destroy (&entity->entity);kfree (entity);return res;@@ -718,9 +682,6 @@ uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, centity->sequence++;spin_unlock (&ctx->ring_lock); -atomic64_add (ktime_to_ns (amdgpu_ctx_fence_time (other)), - &ctx->mgr->time_spend[centity->hw_ip]); -dma_fence_put (other);return seq; }@@ -900,12 +861,15 @@ void amdgpu_ctx_mgr_usage(struct amdgpu_ctx_mgr *mgr,for (hw_ip =0 ; hw_ip <AMDGPU_HW_IP_NUM ; ++hw_ip) {for (i =0 ; i < amdgpu_ctx_num_entities[hw_ip]; ++i) {struct amdgpu_ctx_entity *centity; +struct drm_sched_entity *entity;ktime_t spend; centity = ctx->entities[hw_ip][i];if (!centity)continue ; - spend =amdgpu_ctx_entity_time (ctx, centity); + + entity = ¢ity->entity; + spend =drm_sched_entity_time_spend (entity); usage[hw_ip] =ktime_add (usage[hw_ip], spend); } }
Quoted 2 lines · Christian König
Multiple drivers came up with the requirement to measure how much time each submission spend on the hw.
"spends"
Quoted 11 lines
A previous attempt of accounting this had to be reverted because hw submissions can live longer than the entity originally issuing them.
Amdgpu on the other hand solves this task by keeping track of all the submissions and calculating how much time they have used on demand.
Move this approach over into the scheduler to provide an easy to use interface for all drivers.
Yeah, that'd be helpful.
Quoted 60 lines
Signed-off-by Christian König <[email protected]>--- drivers/gpu/drm/scheduler/sched_entity.c | 89 ++++++++++++++++++++++-- drivers/gpu/drm/scheduler/sched_fence.c | 19 +++++ include/drm/gpu_scheduler.h | 29 ++++++++ 3 files changed, 133 insertions(+), 4 deletions(-)drivers/gpu/drm/scheduler/sched_entity.c +26
-4@@ -62,7 +62,9 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,unsigned int num_sched_list, atomic_t *guilty) { -if (!(entity && sched_list && (num_sched_list ==0 || sched_list[0 ]))) + unsigned int i, num_submissions; + +if (!entity || !sched_list)return -EINVAL ;memset (entity,0 ,sizeof (struct drm_sched_entity ));@@ -75,9 +77,6 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,RCU_INIT_POINTER (entity->last_scheduled,NULL );RB_CLEAR_NODE (&entity->rb_tree_node); -if (num_sched_list) - entity->rq = &sched_list[0 ]->sched_rq[entity->priority]; -init_completion (&entity->entity_idle);/* We start in an idle state. */ @@ -88,11 +87,68 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,atomic_set (&entity->fence_seq,0 ); entity->fence_context =dma_fence_context_alloc (2 ); +spin_lock_init (&entity->accounting_lock); + +/* We need to be able to init even unused entities */ +if (!num_sched_list) +return 0 ; + + entity->rq = &sched_list[0 ]->sched_rq[entity->priority]; + + num_submissions =0 ; +for (i =0 ; i < num_sched_list; ++i) { +if (!sched_list[i]) +return -EINVAL ; + + num_submissions =max (num_submissions, + sched_list[i]->hw_submission_limit); + } + +/* FIXME: Entity initialized before the scheduler. */ +if (!num_submissions) +return 0 ; + + entity->num_hw_submissions = num_submissions; + entity->hw_submissions = kcalloc(num_submissions, sizeof(void *),
drivers/gpu/drm/scheduler/sched_entity.c int drm_sched_entity_init(struct drm_sched_entity *entity, · line 122
Quoted 10 lines
drivers/gpu/drm/scheduler/sched_entity.c +5
-0+ GFP_KERNEL); +if (!entity->hw_submissions) +return -ENOMEM ;return 0 ; }EXPORT_SYMBOL (drm_sched_entity_init); +/** +* drm_sched_entity_time_spend - Accumulated hw time used by this entity
Use the adjective form to "time", "time spent":
drm_sched_entity_time_spent
drivers/gpu/drm/scheduler/sched_entity.c int drm_sched_entity_init(struct drm_sched_entity *entity, · line 128
Quoted 6 lines
drivers/gpu/drm/scheduler/sched_entity.c +6
-0+* @entity: scheduler entity to check +* +* Get the current accumulated hw time used by all submissions made through this +* entity. +*/ +ktime_t drm_sched_entity_time_spend (struct drm_sched_entity *entity)
"time_spend" --> "time_spent"
drm_sched_entity_time_spent
Quoted 12 lines
drivers/gpu/drm/scheduler/sched_entity.c +12
-0+{ +ktime_t result; +unsigned int i; + +if (!entity->num_hw_submissions) +return ns_to_ktime (0 ); + +spin_lock (&entity->accounting_lock); + result = entity->hw_time_used; +for (i =0 ; i < entity->num_hw_submissions; ++i) { +struct drm_sched_fence *fence = entity->hw_submissions[i]; +
drivers/gpu/drm/scheduler/sched_entity.c int drm_sched_entity_init(struct drm_sched_entity *entity, · line 150
Quoted 10 lines
drivers/gpu/drm/scheduler/sched_entity.c +10
-0+if (!fence) +continue ; + + result =ktime_add (result,drm_sched_fence_time_spend (fence)); + } +spin_unlock (&entity->accounting_lock); + +return result; +} +EXPORT_SYMBOL (drm_sched_entity_time_spend);
This is a good show-and-tell, ideally we want to add to entity->hw_time_used, so that that quantity is updated.
Otherwise, as it returns result = "new time spent" + entity->hw_time_used; we want to make sure that "result" is accounted for somewhere more permanent. But as I said, this is a good show-and-tell. I'd imagine we'd develop this function more once it lands in the tree.
Quoted 33 lines
drivers/gpu/drm/scheduler/sched_entity.c +8
-0+/** * drm_sched_entity_modify_sched - Modify sched of an entity * @entity: scheduler entity to init @@ -288,6 +344,8 @@ EXPORT_SYMBOL(drm_sched_entity_flush);*/ void drm_sched_entity_fini (struct drm_sched_entity *entity) { +unsigned int i; +/* * If consumption of existing IBs wasn't completed. Forcefully remove * them here. Also makes sure that the scheduler won't touch this entity @@ -303,6 +361,9 @@ void drm_sched_entity_fini(struct drm_sched_entity *entity)dma_fence_put (rcu_dereference_check (entity->last_scheduled, true));RCU_INIT_POINTER (entity->last_scheduled,NULL ); +for (i =0 ; i < entity->num_hw_submissions; ++i) +dma_fence_put (&entity->hw_submissions[i]->scheduled); +kfree (entity->hw_submissions); }EXPORT_SYMBOL (drm_sched_entity_fini);@@ -427,6 +488,8 @@ drm_sched_job_dependency(struct drm_sched_job *job,struct drm_sched_job *drm_sched_entity_pop_job (struct drm_sched_entity *entity) {struct drm_sched_job *sched_job; +struct drm_sched_fence *fence; +unsigned int idx; sched_job =to_drm_sched_job (spsc_queue_peek (&entity->job_queue));if (!sched_job)
drivers/gpu/drm/scheduler/sched_entity.c struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) · line 543
Quoted 6 lines
drivers/gpu/drm/scheduler/sched_entity.c +3
-0@@ -475,6 +538,24 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)*/ sched_job->entity =NULL ; +if (entity->num_hw_submissions) { + fence = sched_job->s_fence; +dma_fence_get (&fence->scheduled);
Okay, you're doing a get-op on the "scheduled"-fence for a job which is to be started. [1]
Presumably, temporally, the "scheduled"-fence gets a signal that it's been started, after this function.
drivers/gpu/drm/scheduler/sched_entity.c struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) · line 545
Quoted 2 lines
drivers/gpu/drm/scheduler/sched_entity.c +2
-0+ idx = fence->scheduled.seqno; + idx &= entity->num_hw_submissions -1 ;
Since num_hw_submissions isn't guaranteed to be a power of 2, we can mitigate this here by indexing by the remainder of integer division,
idx = fence->scheduled.seqno % entity->num_hw_submissions;
This would give you a uniform distribution indexing into hw_submission[], if the distribution of "seqno" is dense/continuous, and doesn't jump by num_hw_submissions or more.
drivers/gpu/drm/scheduler/sched_entity.c struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) · line 549
Quoted 4 lines
drivers/gpu/drm/scheduler/sched_entity.c +4
-0+ +spin_lock (&entity->accounting_lock); +swap (fence, entity->hw_submissions[idx]); +if (fence) {
This here needs a comment and an explanation of the intended purpose. You're swapping fence with hw_submission[idx], thus assigning the fence whose job is to be started into hw_submission[idx] and assigning to fence the old value of hw_submission[idx].
Now "fence" is either NULL (if that's the first time around) or the old "seqno" which mapped to that index. (Which would be seqno - num_hw_submissions, as you should use modulus above.)
drivers/gpu/drm/scheduler/sched_entity.c struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) · line 550
Quoted 1 line
drivers/gpu/drm/scheduler/sched_entity.c +1
-0+ktime_t spend =drm_sched_fence_time_spend (fence);
Change to the adjective:
ktime_t spent = drm_sched_fence_time_spent(fence);
drivers/gpu/drm/scheduler/sched_entity.c struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity) · line 554
Quoted 4 lines
drivers/gpu/drm/scheduler/sched_entity.c +4
-0+ + entity->hw_time_used =ktime_add (entity->hw_time_used, + spend); +dma_fence_put (&fence->scheduled);
Are you temporally relying on the fact that you did a get-op in [1] above? And are you doing a get-op above to keep this around until you get another seqno which maps to this, (thus you'll need seqno to increase by at least num_hw_submissions _and_ pop_job to be called for it,) so that you can time-spent account for it here?
So, unless seqno advances by num_hw_submissions, _and_ pop_job is called for it, we'd never put-op the "scheduled"-fence (and account for time spent)? (Which implies that we need a consistent feed of jobs in order to put-op the last num_hw_submissions - 1 "scheduled"-fences.)
If you're kref holding the fence, then wouldn't we only want to account for it if it's been completed only, in drm_sched_fence_time_spent()? And we'd somehow want to guarantee that it would achieve completion/done status by the time we wrap num_hw_submissions and the newly mapped entry maps to a done fence... Although, come to think of it, if we're submitting the num_hw_submissions-th job then we're not interested in the 0-th job since the hw cannot hold it anymore. So, I guess that seems fine, but low-level driver writers should be aware of this.
It would appear that the snippet of code here relies on the fact that by the time we reach seqno + num_hw_sumissions, then seqno is done, or more correctly put "uninteresting" to us anymore.
Which is an okay assumption since the hardware cannot hold more than num_hw_submissions jobs... or can it? :-)
So, whether it's done or not, we don't care, we just care to account for it when we "push it out of the hardware" by lieu of now having num_hw_submission new jobs in.
So, this snippet of code here needs very lucid comments in [1], also before the swap(), and also after the if (fence).
I'm concerned that if we don't get new jobs, then the last num_hw_submission - 1 "scheduled"-fences would lag behind since no put-op would be called for them unless we get new jobs, which could be indeterminate time later.
Quoted 12 lines
drivers/gpu/drm/scheduler/sched_entity.c +4
-0+ } +spin_unlock (&entity->accounting_lock); + } +return sched_job; }drivers/gpu/drm/scheduler/sched_fence.c +0
-0
drivers/gpu/drm/scheduler/sched_fence.c void drm_sched_fence_finished(struct drm_sched_fence *fence, int result) · line 69
Quoted 10 lines
drivers/gpu/drm/scheduler/sched_fence.c +7
-0@@ -60,6 +60,25 @@ void drm_sched_fence_finished(struct drm_sched_fence *fence, int result)dma_fence_signal (&fence->finished); } +/** +* drm_sched_fence_time_spend - calculate time spend on the hw +* @fence: fence to use +* +* Calculate how much time this fence has spend using the hw. +*/ +ktime_t drm_sched_fence_time_spend (struct drm_sched_fence *fence)
Use the adjective form:
ktime_t drm_sched_fence_time_spent(...
drivers/gpu/drm/scheduler/sched_fence.c void drm_sched_fence_finished(struct drm_sched_fence *fence, int result) · line 71
Quoted 2 lines
drivers/gpu/drm/scheduler/sched_fence.c +2
-0+{ +/* When the fence is not even scheduled, it can't have spend time */
"it can't have time spent (in the hardware)"
drivers/gpu/drm/scheduler/sched_fence.c void drm_sched_fence_finished(struct drm_sched_fence *fence, int result) · line 75
Quoted 4 lines
drivers/gpu/drm/scheduler/sched_fence.c +4
-0+if (!test_bit (DMA_FENCE_FLAG_TIMESTAMP_BIT , &fence->scheduled.flags)) +return ns_to_ktime (0 ); + +/* When it is still running account how much already spend */
/* If it is still running, then account for time spent. */
drivers/gpu/drm/scheduler/sched_fence.c void drm_sched_fence_finished(struct drm_sched_fence *fence, int result) · line 78
Quoted 3 lines
drivers/gpu/drm/scheduler/sched_fence.c +3
-0+if (!test_bit (DMA_FENCE_FLAG_TIMESTAMP_BIT , &fence->finished.flags)) +return ktime_sub (ktime_get (), fence->scheduled.timestamp); +
If someone calls this more than once while the same fence is alive and its task being worked on in the GPU, and then they use the result of this function to add to some counter for the fence or its container entity, then this would over-account the time spent for this fence.
So while this function is fine, the way its result is being used can result in over-accounting. So we should be careful how we use it.
I can imagine that if the fence is still alive, we don't accumulate; or if the fence is still alive we just assign the new value; or
we can subtract the time from the last measurement to this time (a bit more advanced, and this is what I used for my own accounting when we had "elapsed_ns", but I digress...)
Quoted 17 lines
drivers/gpu/drm/scheduler/sched_fence.c +3
-0+return ktime_sub (fence->finished.timestamp, fence->scheduled.timestamp); +} +static const char *drm_sched_fence_get_driver_name (struct dma_fence *fence) {return "drm_sched" ;include/drm/gpu_scheduler.h +3
-0@@ -239,6 +239,33 @@ struct drm_sched_entity {*/ struct rb_node rb_tree_node; +/** +* @accounting_lock: +*
include/drm/gpu_scheduler.h struct drm_sched_entity { · line 254
Quoted 10 lines
include/drm/gpu_scheduler.h +10
-0+* Protecting the array of hw submissions and the time spend +*/ +spinlock_t accounting_lock; + +/** +* @hw_time_used: +* +* how much hw time previous submissions have already used +*/ +ktime_t hw_time_used;
This needs to age, and we can add aging code once this lands in the tree, so no need to worry about it now.
Quoted 4 lines
include/drm/gpu_scheduler.h +4
-0+ +/** +* @max_hw_submissions: +*
include/drm/gpu_scheduler.h struct drm_sched_entity { · line 268
Quoted 10 lines
include/drm/gpu_scheduler.h +10
-0+* Maximum number of submissions currently in worked on bye the hw +*/ +unsigned int num_hw_submissions; + +/** +* @hw_submissions: +* +* Scheduler fences of the hw submissions in flight +*/ +struct drm_sched_fence **hw_submissions;
Perhaps just use singular, to mean:
hw_submission[i] --> pointer to the i-th hw submission fence.
include/drm/gpu_scheduler.h int drm_sched_entity_init(struct drm_sched_entity *entity, · line 602
Quoted 8 lines
include/drm/gpu_scheduler.h +1
-0};/** @@ -572,6 +599,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,struct drm_gpu_scheduler **sched_list, unsigned int num_sched_list, atomic_t *guilty); +ktime_t drm_sched_entity_time_spend (struct drm_sched_entity *entity);
drm_sched_entity_time_spent
include/drm/gpu_scheduler.h void drm_sched_fence_free(struct drm_sched_fence *fence); · line 624
Quoted 8 lines
include/drm/gpu_scheduler.h +1
-0long drm_sched_entity_flush (struct drm_sched_entity *entity,long timeout);void drm_sched_entity_fini (struct drm_sched_entity *entity);void drm_sched_entity_destroy (struct drm_sched_entity *entity);@@ -593,6 +621,7 @@ void drm_sched_fence_free(struct drm_sched_fence *fence);void drm_sched_fence_scheduled (struct drm_sched_fence *fence);void drm_sched_fence_finished (struct drm_sched_fence *fence,int result); +ktime_t drm_sched_fence_time_spend (struct drm_sched_fence *fence);
drm_sched_fence_time_spent
include/drm/gpu_scheduler.h void drm_sched_fence_free(struct drm_sched_fence *fence); · line 627
Quoted 3 lines
include/drm/gpu_scheduler.h +0
-0unsigned long drm_sched_suspend_timeout (struct drm_gpu_scheduler *sched); void drm_sched_resume_timeout(struct drm_gpu_scheduler *sched,
Regards,
Luben