1 From 2725d86da22e2d2e46d970f0b2f2193a0b8e653b Mon Sep 17 00:00:00 2001
2 From: Jose Maria Casanova Crespo <jmcasanova@igalia.com>
3 Date: Tue, 7 Feb 2023 13:54:02 +0100
4 Subject: [PATCH] drm/v3d: New debugfs end-points to query GPU usage
7 Two new debugfs interfaces are implemented:
9 - gpu_usage: exposes the total runtime since boot of each
10 of the 5 scheduling queues available at V3D (BIN, RENDER,
11 CSD, TFU, CACHE_CLEAN). So if the interface is queried at
12 two different points of time the usage percentage of each
13 of the queues can be calculated.
15 - gpu_pid_usage: exposes the same information but to the
16 level of detail of each process using the V3D driver. The
17 runtime for process using the driver is stored. So the
18 percentages of usage by PID can be calculated with
19 measures at different timestamps.
21 The storage of gpu_pid_usage stats is only done if
22 the debugfs interface is polled during the last 70 seconds.
23 If a process does not submit a GPU job during last 70
24 seconds its stats will also be purged.
26 Signed-off-by: Jose Maria Casanova Crespo <jmcasanova@igalia.com>
28 drivers/gpu/drm/v3d/v3d_debugfs.c | 79 +++++++++++++++++
29 drivers/gpu/drm/v3d/v3d_drv.h | 59 +++++++++++++
30 drivers/gpu/drm/v3d/v3d_gem.c | 1 +
31 drivers/gpu/drm/v3d/v3d_irq.c | 5 ++
32 drivers/gpu/drm/v3d/v3d_sched.c | 139 +++++++++++++++++++++++++++++-
33 5 files changed, 282 insertions(+), 1 deletion(-)
35 --- a/drivers/gpu/drm/v3d/v3d_debugfs.c
36 +++ b/drivers/gpu/drm/v3d/v3d_debugfs.c
38 #include <linux/debugfs.h>
39 #include <linux/seq_file.h>
40 #include <linux/string_helpers.h>
41 +#include <linux/sched/clock.h>
43 #include <drm/drm_debugfs.h>
45 @@ -202,6 +203,82 @@ static int v3d_debugfs_bo_stats(struct s
49 +static int v3d_debugfs_gpu_usage(struct seq_file *m, void *unused)
51 + struct drm_info_node *node = (struct drm_info_node *)m->private;
52 + struct drm_device *dev = node->minor->dev;
53 + struct v3d_dev *v3d = to_v3d_dev(dev);
54 + struct v3d_queue_stats *queue_stats;
55 + enum v3d_queue queue;
56 + u64 timestamp = local_clock();
59 + seq_printf(m, "timestamp;%llu;\n", local_clock());
60 + seq_printf(m, "\"QUEUE\";\"JOBS\";\"RUNTIME\";\"ACTIVE\";\n");
61 + for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
62 + if (!v3d->queue[queue].sched.ready)
65 + queue_stats = &v3d->gpu_queue_stats[queue];
66 + mutex_lock(&queue_stats->lock);
67 + v3d_sched_stats_update(queue_stats);
68 + if (queue_stats->last_pid)
69 + active_runtime = timestamp - queue_stats->last_exec_start;
73 + seq_printf(m, "%s;%d;%llu;%c;\n",
74 + v3d_queue_to_string(queue),
75 + queue_stats->jobs_sent,
76 + queue_stats->runtime + active_runtime,
77 + queue_stats->last_pid?'1':'0');
78 + mutex_unlock(&queue_stats->lock);
84 +static int v3d_debugfs_gpu_pid_usage(struct seq_file *m, void *unused)
86 + struct drm_info_node *node = (struct drm_info_node *)m->private;
87 + struct drm_device *dev = node->minor->dev;
88 + struct v3d_dev *v3d = to_v3d_dev(dev);
89 + struct v3d_queue_stats *queue_stats;
90 + struct v3d_queue_pid_stats *cur;
91 + enum v3d_queue queue;
93 + u64 timestamp = local_clock();
95 + seq_printf(m, "timestamp;%llu;\n", timestamp);
96 + seq_printf(m, "\"QUEUE\";\"PID\",\"JOBS\";\"RUNTIME\";\"ACTIVE\";\n");
97 + for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
99 + if (!v3d->queue[queue].sched.ready)
102 + queue_stats = &v3d->gpu_queue_stats[queue];
103 + mutex_lock(&queue_stats->lock);
104 + queue_stats->gpu_pid_stats_timeout = jiffies + V3D_QUEUE_STATS_TIMEOUT;
105 + v3d_sched_stats_update(queue_stats);
106 + list_for_each_entry(cur, &queue_stats->pid_stats_list, list) {
108 + if (cur->pid == queue_stats->last_pid)
109 + active_runtime = timestamp - queue_stats->last_exec_start;
111 + active_runtime = 0;
113 + seq_printf(m, "%s;%d;%d;%llu;%c;\n",
114 + v3d_queue_to_string(queue),
115 + cur->pid, cur->jobs_sent,
116 + cur->runtime + active_runtime,
117 + cur->pid == queue_stats->last_pid ? '1' : '0');
119 + mutex_unlock(&queue_stats->lock);
125 static int v3d_measure_clock(struct seq_file *m, void *unused)
127 struct drm_info_node *node = (struct drm_info_node *)m->private;
128 @@ -241,6 +318,8 @@ static const struct drm_info_list v3d_de
129 {"v3d_regs", v3d_v3d_debugfs_regs, 0},
130 {"measure_clock", v3d_measure_clock, 0},
131 {"bo_stats", v3d_debugfs_bo_stats, 0},
132 + {"gpu_usage", v3d_debugfs_gpu_usage, 0},
133 + {"gpu_pid_usage", v3d_debugfs_gpu_pid_usage, 0},
137 --- a/drivers/gpu/drm/v3d/v3d_drv.h
138 +++ b/drivers/gpu/drm/v3d/v3d_drv.h
139 @@ -21,6 +21,19 @@ struct reset_control;
141 #define V3D_MAX_QUEUES (V3D_CACHE_CLEAN + 1)
143 +static inline char *
144 +v3d_queue_to_string(enum v3d_queue queue)
147 + case V3D_BIN: return "v3d_bin";
148 + case V3D_RENDER: return "v3d_render";
149 + case V3D_TFU: return "v3d_tfu";
150 + case V3D_CSD: return "v3d_csd";
151 + case V3D_CACHE_CLEAN: return "v3d_cache_clean";
156 struct v3d_queue_state {
157 struct drm_gpu_scheduler sched;
159 @@ -28,6 +41,44 @@ struct v3d_queue_state {
163 +struct v3d_queue_pid_stats {
164 + struct list_head list;
166 + /* Time in jiffes.to purge the stats of this process. Every time a
167 + * process sends a new job to the queue, this timeout is delayed by
168 + * V3D_QUEUE_STATS_TIMEOUT while the gpu_pid_stats_timeout of the
169 + * queue is not reached.
171 + unsigned long timeout_purge;
176 +struct v3d_queue_stats {
178 + u64 last_exec_start;
182 + /* Time in jiffes to stop collecting gpu stats by process. This is
183 + * increased by every access to*the debugfs interface gpu_pid_usage.
184 + * If the debugfs is not used stats are not collected.
186 + unsigned long gpu_pid_stats_timeout;
188 + struct list_head pid_stats_list;
191 +/* pid_stats by process (v3d_queue_pid_stats) are recorded if there is an
192 + * access to the gpu_pid_usageare debugfs interface for the last
193 + * V3D_QUEUE_STATS_TIMEOUT (70s).
195 + * The same timeout is used to purge the stats by process for those process
196 + * that have not sent jobs this period.
198 +#define V3D_QUEUE_STATS_TIMEOUT (70 * HZ)
201 /* Performance monitor object. The perform lifetime is controlled by userspace
202 * using perfmon related ioctls. A perfmon can be attached to a submit_cl
203 * request, and when this is the case, HW perf counters will be activated just
204 @@ -147,6 +198,8 @@ struct v3d_dev {
209 + struct v3d_queue_stats gpu_queue_stats[V3D_MAX_QUEUES];
212 static inline struct v3d_dev *
213 @@ -244,6 +297,11 @@ struct v3d_job {
215 struct v3d_perfmon *perfmon;
217 + /* PID of the process that submitted the job that could be used to
218 + * for collecting stats by process of gpu usage.
222 /* Callback for the freeing of the job on refcount going to 0. */
223 void (*free)(struct kref *ref);
225 @@ -408,6 +466,7 @@ void v3d_mmu_remove_ptes(struct v3d_bo *
227 int v3d_sched_init(struct v3d_dev *v3d);
228 void v3d_sched_fini(struct v3d_dev *v3d);
229 +void v3d_sched_stats_update(struct v3d_queue_stats *queue_stats);
232 void v3d_perfmon_get(struct v3d_perfmon *perfmon);
233 --- a/drivers/gpu/drm/v3d/v3d_gem.c
234 +++ b/drivers/gpu/drm/v3d/v3d_gem.c
235 @@ -516,6 +516,7 @@ v3d_job_init(struct v3d_dev *v3d, struct
239 + job->client_pid = current->pid;
241 ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue],
243 --- a/drivers/gpu/drm/v3d/v3d_irq.c
244 +++ b/drivers/gpu/drm/v3d/v3d_irq.c
248 #include <linux/platform_device.h>
249 +#include <linux/sched/clock.h>
252 #include "v3d_regs.h"
253 @@ -100,6 +101,7 @@ v3d_irq(int irq, void *arg)
254 if (intsts & V3D_INT_FLDONE) {
255 struct v3d_fence *fence =
256 to_v3d_fence(v3d->bin_job->base.irq_fence);
257 + v3d->gpu_queue_stats[V3D_BIN].last_exec_end = local_clock();
259 trace_v3d_bcl_irq(&v3d->drm, fence->seqno);
260 dma_fence_signal(&fence->base);
261 @@ -109,6 +111,7 @@ v3d_irq(int irq, void *arg)
262 if (intsts & V3D_INT_FRDONE) {
263 struct v3d_fence *fence =
264 to_v3d_fence(v3d->render_job->base.irq_fence);
265 + v3d->gpu_queue_stats[V3D_RENDER].last_exec_end = local_clock();
267 trace_v3d_rcl_irq(&v3d->drm, fence->seqno);
268 dma_fence_signal(&fence->base);
269 @@ -118,6 +121,7 @@ v3d_irq(int irq, void *arg)
270 if (intsts & V3D_INT_CSDDONE) {
271 struct v3d_fence *fence =
272 to_v3d_fence(v3d->csd_job->base.irq_fence);
273 + v3d->gpu_queue_stats[V3D_CSD].last_exec_end = local_clock();
275 trace_v3d_csd_irq(&v3d->drm, fence->seqno);
276 dma_fence_signal(&fence->base);
277 @@ -154,6 +158,7 @@ v3d_hub_irq(int irq, void *arg)
278 if (intsts & V3D_HUB_INT_TFUC) {
279 struct v3d_fence *fence =
280 to_v3d_fence(v3d->tfu_job->base.irq_fence);
281 + v3d->gpu_queue_stats[V3D_TFU].last_exec_end = local_clock();
283 trace_v3d_tfu_irq(&v3d->drm, fence->seqno);
284 dma_fence_signal(&fence->base);
285 --- a/drivers/gpu/drm/v3d/v3d_sched.c
286 +++ b/drivers/gpu/drm/v3d/v3d_sched.c
290 #include <linux/kthread.h>
291 +#include <linux/sched/clock.h>
294 #include "v3d_regs.h"
295 @@ -72,6 +73,114 @@ v3d_switch_perfmon(struct v3d_dev *v3d,
296 v3d_perfmon_start(v3d, job->perfmon);
300 + * Updates the scheduling stats of the gpu queues runtime for completed jobs.
302 + * It should be called before any new job submission to the queue or before
303 + * accessing the stats from the debugfs interface.
305 + * It is expected that calls to this function are done with queue_stats->lock
309 +v3d_sched_stats_update(struct v3d_queue_stats *queue_stats)
311 + struct list_head *pid_stats_list = &queue_stats->pid_stats_list;
312 + struct v3d_queue_pid_stats *cur, *tmp;
314 + bool store_pid_stats =
315 + time_is_after_jiffies(queue_stats->gpu_pid_stats_timeout);
317 + /* If debugfs stats gpu_pid_usage has not been polled for a period,
318 + * the pid stats collection is stopped and we purge any existing
321 + * pid_stats are also purged for clients that have reached the
322 + * timeout_purge because the process probably does not exist anymore.
324 + list_for_each_entry_safe_reverse(cur, tmp, pid_stats_list, list) {
325 + if (!store_pid_stats || time_is_before_jiffies(cur->timeout_purge)) {
326 + list_del(&cur->list);
332 + /* If a job has finished its stats are updated. */
333 + if (queue_stats->last_pid && queue_stats->last_exec_end) {
334 + runtime = queue_stats->last_exec_end -
335 + queue_stats->last_exec_start;
336 + queue_stats->runtime += runtime;
338 + if (store_pid_stats) {
339 + struct v3d_queue_pid_stats *pid_stats;
340 + /* Last job info is always at the head of the list */
341 + pid_stats = list_first_entry_or_null(pid_stats_list,
342 + struct v3d_queue_pid_stats, list);
344 + pid_stats->pid == queue_stats->last_pid) {
345 + pid_stats->runtime += runtime;
348 + queue_stats->last_pid = 0;
353 + * Updates the queue usage adding the information of a new job that is
354 + * about to be sent to the GPU to be executed.
357 +v3d_sched_stats_add_job(struct v3d_queue_stats *queue_stats,
358 + struct drm_sched_job *sched_job)
361 + struct v3d_queue_pid_stats *pid_stats = NULL;
362 + struct v3d_job *job = sched_job?to_v3d_job(sched_job):NULL;
363 + struct v3d_queue_pid_stats *cur;
364 + struct list_head *pid_stats_list = &queue_stats->pid_stats_list;
367 + mutex_lock(&queue_stats->lock);
369 + /* Completion of previous job requires an update of its runtime stats */
370 + v3d_sched_stats_update(queue_stats);
372 + queue_stats->last_exec_start = local_clock();
373 + queue_stats->last_exec_end = 0;
374 + queue_stats->jobs_sent++;
375 + queue_stats->last_pid = job->client_pid;
377 + /* gpu usage stats by process are being collected */
378 + if (time_is_after_jiffies(queue_stats->gpu_pid_stats_timeout)) {
379 + list_for_each_entry(cur, pid_stats_list, list) {
380 + if (cur->pid == job->client_pid) {
385 + /* pid_stats of this client is moved to the head of the list. */
387 + list_move(&pid_stats->list, pid_stats_list);
389 + pid_stats = kzalloc(sizeof(struct v3d_queue_pid_stats),
395 + pid_stats->pid = job->client_pid;
396 + list_add(&pid_stats->list, pid_stats_list);
398 + pid_stats->jobs_sent++;
399 + pid_stats->timeout_purge = jiffies + V3D_QUEUE_STATS_TIMEOUT;
403 + mutex_unlock(&queue_stats->lock);
407 static struct dma_fence *v3d_bin_job_run(struct drm_sched_job *sched_job)
409 struct v3d_bin_job *job = to_bin_job(sched_job);
410 @@ -107,6 +216,7 @@ static struct dma_fence *v3d_bin_job_run
411 trace_v3d_submit_cl(dev, false, to_v3d_fence(fence)->seqno,
412 job->start, job->end);
414 + v3d_sched_stats_add_job(&v3d->gpu_queue_stats[V3D_BIN], sched_job);
415 v3d_switch_perfmon(v3d, &job->base);
417 /* Set the current and end address of the control list.
418 @@ -158,6 +268,7 @@ static struct dma_fence *v3d_render_job_
419 trace_v3d_submit_cl(dev, true, to_v3d_fence(fence)->seqno,
420 job->start, job->end);
422 + v3d_sched_stats_add_job(&v3d->gpu_queue_stats[V3D_RENDER], sched_job);
423 v3d_switch_perfmon(v3d, &job->base);
425 /* XXX: Set the QCFG */
426 @@ -190,6 +301,7 @@ v3d_tfu_job_run(struct drm_sched_job *sc
428 trace_v3d_submit_tfu(dev, to_v3d_fence(fence)->seqno);
430 + v3d_sched_stats_add_job(&v3d->gpu_queue_stats[V3D_TFU], sched_job);
431 V3D_WRITE(V3D_TFU_IIA, job->args.iia);
432 V3D_WRITE(V3D_TFU_IIS, job->args.iis);
433 V3D_WRITE(V3D_TFU_ICA, job->args.ica);
434 @@ -231,6 +343,7 @@ v3d_csd_job_run(struct drm_sched_job *sc
436 trace_v3d_submit_csd(dev, to_v3d_fence(fence)->seqno);
438 + v3d_sched_stats_add_job(&v3d->gpu_queue_stats[V3D_CSD], sched_job);
439 v3d_switch_perfmon(v3d, &job->base);
441 for (i = 1; i <= 6; i++)
442 @@ -247,7 +360,10 @@ v3d_cache_clean_job_run(struct drm_sched
443 struct v3d_job *job = to_v3d_job(sched_job);
444 struct v3d_dev *v3d = job->v3d;
446 + v3d_sched_stats_add_job(&v3d->gpu_queue_stats[V3D_CACHE_CLEAN],
448 v3d_clean_caches(v3d);
449 + v3d->gpu_queue_stats[V3D_CACHE_CLEAN].last_exec_end = local_clock();
453 @@ -385,8 +501,18 @@ v3d_sched_init(struct v3d_dev *v3d)
454 int hw_jobs_limit = 1;
455 int job_hang_limit = 0;
456 int hang_limit_ms = 500;
460 + for (q = 0; q < V3D_MAX_QUEUES; q++) {
461 + INIT_LIST_HEAD(&v3d->gpu_queue_stats[q].pid_stats_list);
462 + /* Setting timeout before current jiffies disables collecting
463 + * pid_stats on scheduling init.
465 + v3d->gpu_queue_stats[q].gpu_pid_stats_timeout = jiffies - 1;
466 + mutex_init(&v3d->gpu_queue_stats[q].lock);
469 ret = drm_sched_init(&v3d->queue[V3D_BIN].sched,
471 hw_jobs_limit, job_hang_limit,
472 @@ -440,9 +566,20 @@ void
473 v3d_sched_fini(struct v3d_dev *v3d)
476 + struct v3d_queue_stats *queue_stats;
478 for (q = 0; q < V3D_MAX_QUEUES; q++) {
479 - if (v3d->queue[q].sched.ready)
480 + if (v3d->queue[q].sched.ready) {
481 + queue_stats = &v3d->gpu_queue_stats[q];
482 + mutex_lock(&queue_stats->lock);
483 + /* Setting gpu_pid_stats_timeout to jiffies-1 will
484 + * make v3d_sched_stats_update to purge all
485 + * allocated pid_stats.
487 + queue_stats->gpu_pid_stats_timeout = jiffies - 1;
488 + v3d_sched_stats_update(queue_stats);
489 + mutex_unlock(&queue_stats->lock);
490 drm_sched_fini(&v3d->queue[q].sched);