Skip to contentSkip to conversation

PATCH In review David M Nieto 3 participants 20 messages May 6, 2021 – May 14, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

During stress testing we found that with some Vulkan applications the fence information displayed in the recently added fdinfo was not properly calculated, two issues were discovered:

(1) A missing dma_put_fence on the loop that calculates the usage ratios when the fence is being ignored.
(2) The approximation for the ratio calculation is not accurate when accounting for non-active contexts. The fix is to ignore those context if they have activity ratios lower than 0.01%

Attached is also a script demonstrating how the fdinfo can be used to monitor gpu usage on running processes.

#!/usr/bin/env python3

#
# Copyright (C) 2021 Advanced Micro Devices. All rights reserved. #
# Permission is hereby granted, free of charge, to any person obtaining

# a copy of
# this software and associated documentation files (the "Software"), to
# deal in
# the Software without restriction, including without limitation the
# rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of
# the Software, and to permit persons to whom the Software is furnished
# to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

from tokenize import tokenize
import sys
import os
import pwd

total_mem = dict()
total_usage = dict()
def can_access(path):
return os.access(path + "/fdinfo", os.X_OK)

def calc_perc(entry, metric):

    if not metric in entry:
        return 0.0
    if (type(entry[metric]) == list) :
        return sum(entry[metric])
    else :
        return entry[metric]

def process_pid(file):

    stat = dict()
    pasids = []

    for fd in os.scandir(file.path + "/fdinfo"):
        entry = {}
        with open(fd) as f:
            for line in f:
                entries = line.strip().split()
                if (entries[0] == "pdev:") :
                    entry["pdev"] = entries[1]
                elif (entries[0] == "pasid:") :
                    entry["pasid"] = entries[1]
                elif (entries[0] == "vram") :
                    entry["mem"] = int(entries[2])
                elif ("gfx" in entries[0]) :
                    if not "gfx" in entry :
                        entry["gfx"] = [0,0,0,0,0,0,0,0]
                    entry["gfx"][int(entries[0].lstrip("gfx").rstrip(":"))]
=
			float(entries[1].rstrip("%"))
                elif ("dma" in entries[0]) :
                    if not "dma" in entry :
                        entry["dma"] = [0,0,0,0,0,0,0,0]
                    entry["dma"][int(entries[0].lstrip("dma").rstrip(":"))]
=
			float(entries[1].rstrip("%"))
                elif ("dec" in entries[0]) :
                    if not "dec" in entry :
                        entry["dec"] = [0,0,0,0,0,0,0,0]
                    entry["dec"][int(entries[0].lstrip("dec").rstrip(":"))]
=
			float(entries[1].rstrip("%"))
                elif ("enc" in entries[0]) :
                    if not "enc" in entry :
                        entry["enc"] = [0,0,0,0,0,0,0,0]
                    entry["enc"][int(entries[0].lstrip("enc").rstrip(":"))]
=
			float(entries[1].rstrip("%"))
                elif ("compute" in entries[0]) :
                    if not "compute" in entry :
                        entry["compute"] = [0,0,0,0,0,0,0,0]
                    entry["compute"][int(entries[0].lstrip("compute").rstrip(":"))]

=
float(entries[1].rstrip("%"))

if not "pdev" in entry:
continue
if not "pasid" in entry :
continue
if (entry["pdev"], entry["pasid"]) in pasids:
continue
pasids.append((entry["pdev"], entry["pasid"]))

pdev = entry["pdev"]

if not pdev in stat:
stat[pdev] = dict()

if "mem" in entry :
if "mem" in stat[pdev] :
stat[pdev]["mem"] = stat[pdev]["mem"] +
entry["mem"];
else :
stat[pdev]["mem"] = entry["mem"]

if "gfx" in entry :
if "gfx" in stat[pdev] :
stat[pdev]["gfx"] = [a + b for a, b in
zip(stat[pdev]["gfx"],

			entry["gfx"])]
                else :
                    stat[pdev]["gfx"] = entry["gfx"]

            if "enc" in entry :
                if "enc" in stat[pdev] :
                    stat[pdev]["enc"] = [a + b for a, b in
zip(stat[pdev]["enc"],
			entry["enc"])]
                else :
                    stat[pdev]["enc"] = entry["enc"]

            if "dec" in entry :
                if "dec" in stat[pdev] :
                    stat[pdev]["dec"] = [a + b for a, b in
zip(stat[pdev]["dec"],
			entry["dec"])]
                else :
                    stat[pdev]["dec"] = entry["dec"]

            if "dma" in entry :
                if "dma" in stat[pdev] :
                    stat[pdev]["dma"] = [a + b for a, b in

zip(stat[pdev]["dma"],
entry["dma"])]
else :
stat[pdev]["dma"] = entry["dma"]

if "compute" in entry :
if "compute" in stat[pdev] :
stat[pdev]["compute"] = [a + b for a, b in
zip(stat[pdev]["compute"],
entry["compute"])]
else :
stat[pdev]["compute"] = entry["compute"]

for gpu in stat:
stat[gpu]["pid"] = file.name
with open(file.path + "/comm") as f:
stat[gpu]["name"] = f.readline().strip()

if stat:
for s in stat:
if not s in total_mem:
total_mem[s] = int(stat[s]["mem"])
else:
total_mem[s] = total_mem[s] + int(stat[s]["mem"])

if not s in total_usage:
total_usage[s] = dict()

for key in stat[s]:
if key == "mem":
continue
if key == "name":
continue
if key == "pid":
continue
total = calc_perc(stat[s], key)

if not key in total_usage[s]:
total_usage[s][key] = total
else:
total_usage[s][key] = total + total_usage[s][key]

# the /proc/PID is owned by process creator proc_stat_file = os.stat("/proc/%d" % int(stat[s]['pid'])) # get UID via stat call
uid = proc_stat_file.st_uid
# look up the username from uid
username = pwd.getpwuid(uid)[0]

print("| {0:5s} | {1:16s} | {9:10s} | {2} | {3:7d} KiB |
{4:6.2f} {5:6.2f} {6:6.2f} {7:6.2f} {8:6.2f} |"

                .format(stat[s]["pid"].ljust(5),
stat[s]["name"].ljust(16), s,
                stat[s]["mem"],
                calc_perc(stat[s], 'gfx'),
                calc_perc(stat[s], 'compute'),
                calc_perc(stat[s], 'dma'),
                calc_perc(stat[s], 'enc'),
                calc_perc(stat[s], 'dec'),
                username
                ))
            print("+-------+------------------+------------+--------------+-------------+-----------------------------------------+")

path = "/proc/" print("+=======+==================+============+==============+=============+=========================================+")
print("| pid | name | user | gpu bdf | fb usage

| ring usage (%)                          |")
print("|       |                  |            |              |
| gfx     comp    dma     enc     dec     |")

print("+=======+==================+============+==============+=============+=========================================+")

for file in os.scandir(path):
if (file.is_dir() and file.name.isnumeric()) :
if (can_access(file.path)):
process_pid(file)

for gpu in total_mem:
print("| TOTAL:| {0} | {1:7d} KiB |
{2:6.2f} {3:6.2f} {4:6.2f} {5:6.2f} {6:6.2f} |".format(gpu, total_mem[gpu],

        calc_perc(total_usage[gpu], 'gfx'),
        calc_perc(total_usage[gpu], 'compute'),
        calc_perc(total_usage[gpu], 'dma'),
        calc_perc(total_usage[gpu], 'enc'),
        calc_perc(total_usage[gpu], 'dec'),
        ))

print("+=======+==================+============+==============+=============+=====================+++=================+")

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#1 lore raw

PATCH 1/2 drm/amdgpu: free resources on fence usage query

Free the resources if the fence needs to be ignored during the ratio calculation

Change-Id Ibfc55a94c53d4b3a1dba8fff4c53fd893195bb96
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6 -2
index 01fe60fedcbe..9036c93b4a0c 100644
@@ -669,11 +669,15 @@ void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *cen
 		if (!fence)
 			continue;
 		s_fence = to_drm_sched_fence(fence);
-		if (!dma_fence_is_signaled(&s_fence->scheduled))
+		if (!dma_fence_is_signaled(&s_fence->scheduled)) {
+			dma_fence_put(fence);
 			continue;
+		}
 		t1 = s_fence->scheduled.timestamp;
-		if (t1 >= now)
+		if (!ktime_before(t1, now)) {
+			dma_fence_put(fence);
 			continue;
+		}
 		if (dma_fence_is_signaled(&s_fence->finished) &&
 			s_fence->finished.timestamp < now)
 			*total += ktime_sub(s_fence->finished.timestamp, t1);
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#2 lore raw

PATCH 2/2 drm/amdgpu: fix fence calculation

In reply to David M Nieto

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +17 -1
index 9036c93b4a0c..a26496735080 100644
@@ -689,6 +689,8 @@ void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *cen
 	}
 }
 
+#define FENCE_USAGE_MIN_RATIO(max, total) (max > 16384ULL*total)
+
 ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
 		uint32_t idx, uint64_t *elapsed)
 {
@@ -697,17 +699,29 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
 	uint32_t id;
 	struct amdgpu_ctx_entity *centity;
 	ktime_t total = 0, max = 0;
+	ktime_t ttotal = 0, tmax = 0;
+
 
 	if (idx >= AMDGPU_MAX_ENTITY_NUM)
 		return 0;
 	idp = &mgr->ctx_handles;
 	mutex_lock(&mgr->lock);
 	idr_for_each_entry(idp, ctx, id) {
+		ttotal = tmax = ktime_set(0, 0);
 		if (!ctx->entities[hwip][idx])
 			continue;
 
 		centity = ctx->entities[hwip][idx];
-		amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+		amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+		/* Harmonic mean approximation diverges for very small
+		 * values. If ratio < 0.01% ignore
+		 */
+		if (FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+			continue;
+
+		total = ktime_add(total, ttotal);
+		max = ktime_after(tmax, max) ? tmax : max;
 	}
 
 	mutex_unlock(&mgr->lock);
@@ -716,3 +730,5 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
 
 	return total;
 }
+
+#undef FENCE_USAGE_MIN_RATIO
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#3 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

Am 07.05.21 um 00:37 schrieb David M Nieto:

Quoted 19 lines

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 18 +++++++++++++++++-
  1 file changed, 17 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
index 9036c93b4a0c..a26496735080 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *cen · line 692

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
@@ -689,6 +689,8 @@ void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *cen
  	}
  }
 
+#define FENCE_USAGE_MIN_RATIO(max, total) (max > 16384ULL*total)

An AMDGPU_CTX_ prefix looks appropriate here and defines should be at the beginning of the file.

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
+
  ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
  		uint32_t idx, uint64_t *elapsed)
  {
@@ -697,17 +699,29 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
  	uint32_t id;
  	struct amdgpu_ctx_entity *centity;

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 710

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +3 -0
  	ktime_t total = 0, max = 0;
+	ktime_t ttotal = 0, tmax = 0;
+
 
  	if (idx >= AMDGPU_MAX_ENTITY_NUM)
  		return 0;
  	idp = &mgr->ctx_handles;
  	mutex_lock(&mgr->lock);
  	idr_for_each_entry(idp, ctx, id) {
+		ttotal = tmax = ktime_set(0, 0);

Rather define the variable in the loop in the first place.

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +10 -1
  		if (!ctx->entities[hwip][idx])
  			continue;
 
  		centity = ctx->entities[hwip][idx];
-		amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+		amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+		/* Harmonic mean approximation diverges for very small
+		 * values. If ratio < 0.01% ignore
+		 */
+		if (FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+			continue;
+
+		total = ktime_add(total, ttotal);
+		max = ktime_after(tmax, max) ? tmax : max;
  	}
 
  	mutex_unlock(&mgr->lock);

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 734

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +2 -0
@@ -716,3 +730,5 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
 
  	return total;
  }
+
+#undef FENCE_USAGE_MIN_RATIO

Please don't undef macros if not necessary.

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#4 lore raw

PATCH 1/2 drm/amdgpu: free resources on fence usage query

Free the resources if the fence needs to be ignored during the ratio calculation

Change-Id Ibfc55a94c53d4b3a1dba8fff4c53fd893195bb96
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6 -2
index 01fe60fedcbe..9036c93b4a0c 100644
@@ -669,11 +669,15 @@ void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *cen
 		if (!fence)
 			continue;
 		s_fence = to_drm_sched_fence(fence);
-		if (!dma_fence_is_signaled(&s_fence->scheduled))
+		if (!dma_fence_is_signaled(&s_fence->scheduled)) {
+			dma_fence_put(fence);
 			continue;
+		}
 		t1 = s_fence->scheduled.timestamp;
-		if (t1 >= now)
+		if (!ktime_before(t1, now)) {
+			dma_fence_put(fence);
 			continue;
+		}
 		if (dma_fence_is_signaled(&s_fence->finished) &&
 			s_fence->finished.timestamp < now)
 			*total += ktime_sub(s_fence->finished.timestamp, t1);
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#5 lore raw

PATCH 2/2 drm/amdgpu: fix fence calculation

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 13 ++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
 2 files changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +12 -1
index 9036c93b4a0c..89ee464b9424 100644
@@ -698,16 +698,27 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
 	struct amdgpu_ctx_entity *centity;
 	ktime_t total = 0, max = 0;
 
+
 	if (idx >= AMDGPU_MAX_ENTITY_NUM)
 		return 0;
 	idp = &mgr->ctx_handles;
 	mutex_lock(&mgr->lock);
 	idr_for_each_entry(idp, ctx, id) {
+		ktime_t ttotal = tmax = ktime_set(0, 0);
 		if (!ctx->entities[hwip][idx])
 			continue;
 
 		centity = ctx->entities[hwip][idx];
-		amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+		amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+		/* Harmonic mean approximation diverges for very small
+		 * values. If ratio < 0.01% ignore
+		 */
+		if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+			continue;
+
+		total = ktime_add(total, ttotal);
+		max = ktime_after(tmax, max) ? tmax : max;
 	}
 
 	mutex_unlock(&mgr->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
index 10dcf59a5c6b..3541dfb059ec 100644
@@ -30,6 +30,7 @@ struct drm_file;
 struct amdgpu_fpriv;
 
 #define AMDGPU_MAX_ENTITY_NUM 4
+#define AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(max, total) (max > 16384ULL*total)
 
 struct amdgpu_ctx_entity {
 	uint64_t		sequence;
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#6 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

Am 10.05.21 um 22:29 schrieb David M Nieto:

Quoted 20 lines

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 13 ++++++++++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
  2 files changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
index 9036c93b4a0c..89ee464b9424 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 701

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
@@ -698,16 +698,27 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
  	struct amdgpu_ctx_entity *centity;
  	ktime_t total = 0, max = 0;
 
+

Unrelated white space change.

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 707

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
  	if (idx >= AMDGPU_MAX_ENTITY_NUM)
  		return 0;
  	idp = &mgr->ctx_handles;
  	mutex_lock(&mgr->lock);
  	idr_for_each_entry(idp, ctx, id) {
+		ktime_t ttotal = tmax = ktime_set(0, 0);

There should be a blank line between decleration and code and please don't initialize local variables if it isn't necessary.

Christian.

Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +10 -1
  		if (!ctx->entities[hwip][idx])
  			continue;
 
  		centity = ctx->entities[hwip][idx];
-		amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+		amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+		/* Harmonic mean approximation diverges for very small
+		 * values. If ratio < 0.01% ignore
+		 */
+		if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+			continue;
+
+		total = ktime_add(total, ttotal);
+		max = ktime_after(tmax, max) ? tmax : max;
  	}
 
  	mutex_unlock(&mgr->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -0
index 10dcf59a5c6b..3541dfb059ec 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h struct drm_file; · line 36

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
@@ -30,6 +30,7 @@ struct drm_file;
  struct amdgpu_fpriv;
 
  #define AMDGPU_MAX_ENTITY_NUM 4
+#define AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(max, total) (max > 16384ULL*total)
 
  struct amdgpu_ctx_entity {
  	uint64_t		sequence;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#7 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

[AMD Official Use Only - Internal Distribution Only]

The local variables need to be initialized to zero, since amdgpu_ctx_fence_time accumulates and does not initialize

David
________________________________
From: Christian König <[email protected]>
Sent: Tuesday, May 11, 2021 12:53 AM
To: Nieto, David M <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

Am 10.05.21 um 22:29 schrieb David M Nieto:

Quoted 20 lines

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 13 ++++++++++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
  2 files changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
index 9036c93b4a0c..89ee464b9424 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 701

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
@@ -698,16 +698,27 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
       struct amdgpu_ctx_entity *centity;
       ktime_t total = 0, max = 0;
 
+

Unrelated white space change.

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 707

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
       if (idx >= AMDGPU_MAX_ENTITY_NUM)
               return 0;
       idp = &mgr->ctx_handles;
       mutex_lock(&mgr->lock);
       idr_for_each_entry(idp, ctx, id) {
+             ktime_t ttotal = tmax = ktime_set(0, 0);

There should be a blank line between decleration and code and please don't initialize local variables if it isn't necessary.

Christian.

Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +10 -1
               if (!ctx->entities[hwip][idx])
                       continue;
 
               centity = ctx->entities[hwip][idx];
-             amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+             amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+             /* Harmonic mean approximation diverges for very small
+              * values. If ratio < 0.01% ignore
+              */
+             if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+                     continue;
+
+             total = ktime_add(total, ttotal);
+             max = ktime_after(tmax, max) ? tmax : max;
       }
 
       mutex_unlock(&mgr->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -0
index 10dcf59a5c6b..3541dfb059ec 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h struct drm_file; · line 36

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
@@ -30,6 +30,7 @@ struct drm_file;
  struct amdgpu_fpriv;
 
  #define AMDGPU_MAX_ENTITY_NUM 4
+#define AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(max, total) (max > 16384ULL*total)
 
  struct amdgpu_ctx_entity {
       uint64_t                sequence;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#8 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

In this case amdgpu_ctx_fence_time should probably be changed to initialize the variable itself.

That is really bad coding style otherwise.

Christian.

Am 11.05.21 um 20:14 schrieb Nieto, David M:

Quoted 80 lines

[AMD Official Use Only - Internal Distribution Only]

The local variables need to be initialized to zero, since amdgpu_ctx_fence_time accumulates and does not initialize

David
------------------------------------------------------------------------
*From:* Christian König <[email protected]>
*Sent:* Tuesday, May 11, 2021 12:53 AM
*To:* Nieto, David M <[email protected]>; [email protected] <[email protected]> *Subject:* Re: [PATCH 2/2] drm/amdgpu: fix fence calculation
Am 10.05.21 um 22:29 schrieb David M Nieto:

Quoted 8 lines

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Review

Quoted 8 lines
Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 13 ++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +

   2 files changed, 13 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct

Quoted 4 lines

index 9036c93b4a0c..89ee464b9424 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
@@ -698,16 +698,27 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct 

amdgpu_ctx_mgr *mgr, uint32_t hwip,

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 701

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
         struct amdgpu_ctx_entity *centity;
         ktime_t total = 0, max = 0;
 
+

Unrelated white space change.

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 707

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
         if (idx >= AMDGPU_MAX_ENTITY_NUM)
                 return 0;
         idp = &mgr->ctx_handles;
         mutex_lock(&mgr->lock);
         idr_for_each_entry(idp, ctx, id) {
+             ktime_t ttotal = tmax = ktime_set(0, 0);

There should be a blank line between decleration and code and please don't initialize local variables if it isn't necessary.

Christian.

Review

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +10 -1
                 if (!ctx->entities[hwip][idx])
                         continue;
 
                 centity = ctx->entities[hwip][idx];
-             amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+             amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+             /* Harmonic mean approximation diverges for very small
+              * values. If ratio < 0.01% ignore
+              */
+             if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+                     continue;
+
+             total = ktime_add(total, ttotal);
+             max = ktime_after(tmax, max) ? tmax : max;
         }
 
         mutex_unlock(&mgr->lock);
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h

Quoted 6 lines

index 10dcf59a5c6b..3541dfb059ec 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -0
@@ -30,6 +30,7 @@ struct drm_file;
    struct amdgpu_fpriv;
 
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h struct drm_file; · line 33

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
    #define AMDGPU_MAX_ENTITY_NUM 4
+#define AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(max, total) (max > 

16384ULL*total)

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -0
    struct amdgpu_ctx_entity {
         uint64_t                sequence;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#9 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

And BTW amdgpu_ctx_fence_time() should probably be static.

Christian.

Am 12.05.21 um 08:55 schrieb Christian König:

Quoted 95 lines

In this case amdgpu_ctx_fence_time should probably be changed to initialize the variable itself.

That is really bad coding style otherwise.

Christian.

Am 11.05.21 um 20:14 schrieb Nieto, David M:

Quoted 87 lines

[AMD Official Use Only - Internal Distribution Only]

The local variables need to be initialized to zero, since amdgpu_ctx_fence_time accumulates and does not initialize

David
------------------------------------------------------------------------
*From:* Christian König <[email protected]>
*Sent:* Tuesday, May 11, 2021 12:53 AM
*To:* Nieto, David M <[email protected]>; [email protected] <[email protected]> *Subject:* Re: [PATCH 2/2] drm/amdgpu: fix fence calculation
Am 10.05.21 um 22:29 schrieb David M Nieto:

Quoted 8 lines

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Review

Quoted 8 lines
Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 13 ++++++++++++- drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +

   2 files changed, 13 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct

Quoted 4 lines

index 9036c93b4a0c..89ee464b9424 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
@@ -698,16 +698,27 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct 

amdgpu_ctx_mgr *mgr, uint32_t hwip,

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 701

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
         struct amdgpu_ctx_entity *centity;
         ktime_t total = 0, max = 0;
 
+

Unrelated white space change.

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 707

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
         if (idx >= AMDGPU_MAX_ENTITY_NUM)
                 return 0;
         idp = &mgr->ctx_handles;
         mutex_lock(&mgr->lock);
         idr_for_each_entry(idp, ctx, id) {
+             ktime_t ttotal = tmax = ktime_set(0, 0);

There should be a blank line between decleration and code and please don't initialize local variables if it isn't necessary.

Christian.

Review

Quoted 19 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +10 -1
                 if (!ctx->entities[hwip][idx])
                         continue;
 
                 centity = ctx->entities[hwip][idx];
-             amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+             amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+             /* Harmonic mean approximation diverges for very small
+              * values. If ratio < 0.01% ignore
+              */
+             if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+                     continue;
+
+             total = ktime_add(total, ttotal);
+             max = ktime_after(tmax, max) ? tmax : max;
         }
 
         mutex_unlock(&mgr->lock);
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h struct drm_file; · line 33

Quoted 8 lines

index 10dcf59a5c6b..3541dfb059ec 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
@@ -30,6 +30,7 @@ struct drm_file;
    struct amdgpu_fpriv;
 
    #define AMDGPU_MAX_ENTITY_NUM 4
+#define AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(max, total) (max > 

16384ULL*total)

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -0
    struct amdgpu_ctx_entity {
         uint64_t                sequence;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#10 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

[AMD Official Use Only - Internal Distribution Only]

yep, you are right...

I'll make those changes.

David
________________________________
From: Christian König <[email protected]>
Sent: Tuesday, May 11, 2021 11:56 PM
To: Nieto, David M <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

And BTW amdgpu_ctx_fence_time() should probably be static.

Christian.

Am 12.05.21 um 08:55 schrieb Christian König:
In this case amdgpu_ctx_fence_time should probably be changed to initialize the variable itself.

That is really bad coding style otherwise.

Christian.

Am 11.05.21 um 20:14 schrieb Nieto, David M:

[AMD Official Use Only - Internal Distribution Only]

The local variables need to be initialized to zero, since amdgpu_ctx_fence_time accumulates and does not initialize

David
________________________________
From: Christian König <[email protected]><mailto:[email protected]>
Sent: Tuesday, May 11, 2021 12:53 AM
To: Nieto, David M <[email protected]><mailto:[email protected]>; [email protected]<mailto:[email protected]> <[email protected]><mailto:[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

Am 10.05.21 um 22:29 schrieb David M Nieto:

Quoted 20 lines

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 13 ++++++++++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
  2 files changed, 13 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +0 -0
index 9036c93b4a0c..89ee464b9424 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 701

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
@@ -698,16 +698,27 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
       struct amdgpu_ctx_entity *centity;
       ktime_t total = 0, max = 0;
 
+

Unrelated white space change.

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip, · line 707

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +1 -0
       if (idx >= AMDGPU_MAX_ENTITY_NUM)
               return 0;
       idp = &mgr->ctx_handles;
       mutex_lock(&mgr->lock);
       idr_for_each_entry(idp, ctx, id) {
+             ktime_t ttotal = tmax = ktime_set(0, 0);

There should be a blank line between decleration and code and please don't initialize local variables if it isn't necessary.

Christian.

Quoted 23 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +10 -1
               if (!ctx->entities[hwip][idx])
                       continue;
 
               centity = ctx->entities[hwip][idx];
-             amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+             amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+             /* Harmonic mean approximation diverges for very small
+              * values. If ratio < 0.01% ignore
+              */
+             if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+                     continue;
+
+             total = ktime_add(total, ttotal);
+             max = ktime_after(tmax, max) ? tmax : max;
       }
 
       mutex_unlock(&mgr->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -0
index 10dcf59a5c6b..3541dfb059ec 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h struct drm_file; · line 36

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
@@ -30,6 +30,7 @@ struct drm_file;
  struct amdgpu_fpriv;
 
  #define AMDGPU_MAX_ENTITY_NUM 4
+#define AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(max, total) (max > 16384ULL*total)
 
  struct amdgpu_ctx_entity {
       uint64_t                sequence;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#11 lore raw

PATCH 1/2 drm/amdgpu: free resources on fence usage query

In reply to Christian König

Free the resources if the fence needs to be ignored during the ratio calculation

Change-Id Ibfc55a94c53d4b3a1dba8fff4c53fd893195bb96
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6 -2
index 01fe60fedcbe..9036c93b4a0c 100644
@@ -669,11 +669,15 @@ void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *cen
 		if (!fence)
 			continue;
 		s_fence = to_drm_sched_fence(fence);
-		if (!dma_fence_is_signaled(&s_fence->scheduled))
+		if (!dma_fence_is_signaled(&s_fence->scheduled)) {
+			dma_fence_put(fence);
 			continue;
+		}
 		t1 = s_fence->scheduled.timestamp;
-		if (t1 >= now)
+		if (!ktime_before(t1, now)) {
+			dma_fence_put(fence);
 			continue;
+		}
 		if (dma_fence_is_signaled(&s_fence->finished) &&
 			s_fence->finished.timestamp < now)
 			*total += ktime_sub(s_fence->finished.timestamp, t1);
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#12 lore raw

PATCH 2/2 drm/amdgpu: fix fence calculation

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 17 +++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
 2 files changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +15 -2
index 9036c93b4a0c..b919615e6644 100644
@@ -652,12 +652,14 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
 	mutex_destroy(&mgr->lock);
 }
 
-void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *centity,
+static void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *centity,
 		ktime_t *total, ktime_t *max)
 {
 	ktime_t now, t1;
 	uint32_t i;
 
+	*total = *max = 0;
+
 	now = ktime_get();
 	for (i = 0; i < amdgpu_sched_jobs; i++) {
 		struct dma_fence *fence;
@@ -703,11 +705,22 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
 	idp = &mgr->ctx_handles;
 	mutex_lock(&mgr->lock);
 	idr_for_each_entry(idp, ctx, id) {
+		ktime_t ttotal, tmax;
+
 		if (!ctx->entities[hwip][idx])
 			continue;
 
 		centity = ctx->entities[hwip][idx];
-		amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+		amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+		/* Harmonic mean approximation diverges for very small
+		 * values. If ratio < 0.01% ignore
+		 */
+		if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+			continue;
+
+		total = ktime_add(total, ttotal);
+		max = ktime_after(tmax, max) ? tmax : max;
 	}
 
 	mutex_unlock(&mgr->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
index 10dcf59a5c6b..3541dfb059ec 100644
@@ -30,6 +30,7 @@ struct drm_file;
 struct amdgpu_fpriv;
 
 #define AMDGPU_MAX_ENTITY_NUM 4
+#define AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(max, total) (max > 16384ULL*total)
 
 struct amdgpu_ctx_entity {
 	uint64_t		sequence;
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#13 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

Am 12.05.21 um 21:45 schrieb David M Nieto:

Quoted 10 lines

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b

Looks good to me now, but the automated tools complain about "DOS line endings" plus a couple of other nit picks and won't let me push it :)

Please use the checkpatch.pl script found in the Linux kernel to fix those errors and resend.

Thanks,
Christian.

Quoted 54 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 17 +++++++++++++++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
  2 files changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +15 -2
index 9036c93b4a0c..b919615e6644 100644
@@ -652,12 +652,14 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
  	mutex_destroy(&mgr->lock);
  }
 
-void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *centity,
+static void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *centity,
  		ktime_t *total, ktime_t *max)
  {
  	ktime_t now, t1;
  	uint32_t i;
 
+	*total = *max = 0;
+
  	now = ktime_get();
  	for (i = 0; i < amdgpu_sched_jobs; i++) {
  		struct dma_fence *fence;
@@ -703,11 +705,22 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
  	idp = &mgr->ctx_handles;
  	mutex_lock(&mgr->lock);
  	idr_for_each_entry(idp, ctx, id) {
+		ktime_t ttotal, tmax;
+
  		if (!ctx->entities[hwip][idx])
  			continue;
 
  		centity = ctx->entities[hwip][idx];
-		amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+		amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+		/* Harmonic mean approximation diverges for very small
+		 * values. If ratio < 0.01% ignore
+		 */
+		if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+			continue;
+
+		total = ktime_add(total, ttotal);
+		max = ktime_after(tmax, max) ? tmax : max;
  	}
 
  	mutex_unlock(&mgr->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -0
index 10dcf59a5c6b..3541dfb059ec 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h struct drm_file; · line 36

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
@@ -30,6 +30,7 @@ struct drm_file;
  struct amdgpu_fpriv;
 
  #define AMDGPU_MAX_ENTITY_NUM 4
+#define AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(max, total) (max > 16384ULL*total)
 
  struct amdgpu_ctx_entity {
  	uint64_t		sequence;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#14 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

[AMD Official Use Only - Internal Distribution Only]

DOS line endings? That is weird....

I corrected the issues that showed in checkpatch.pl (the > 80 lines)

I'll re-upload
________________________________
From: Christian König <[email protected]>
Sent: Thursday, May 13, 2021 1:11 AM
To: Nieto, David M <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH 2/2] drm/amdgpu: fix fence calculation

Am 12.05.21 um 21:45 schrieb David M Nieto:

Quoted 10 lines

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

Change-Id I5b6e0ce5f489a5f55855d35354a6a3653e9d613b

Looks good to me now, but the automated tools complain about "DOS line endings" plus a couple of other nit picks and won't let me push it :)

Please use the checkpatch.pl script found in the Linux kernel to fix those errors and resend.

Thanks,
Christian.

Quoted 54 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 17 +++++++++++++++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
  2 files changed, 16 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +15 -2
index 9036c93b4a0c..b919615e6644 100644
@@ -652,12 +652,14 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
       mutex_destroy(&mgr->lock);
  }
 
-void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *centity,
+static void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *centity,
               ktime_t *total, ktime_t *max)
  {
       ktime_t now, t1;
       uint32_t i;
 
+     *total = *max = 0;
+
       now = ktime_get();
       for (i = 0; i < amdgpu_sched_jobs; i++) {
               struct dma_fence *fence;
@@ -703,11 +705,22 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
       idp = &mgr->ctx_handles;
       mutex_lock(&mgr->lock);
       idr_for_each_entry(idp, ctx, id) {
+             ktime_t ttotal, tmax;
+
               if (!ctx->entities[hwip][idx])
                       continue;
 
               centity = ctx->entities[hwip][idx];
-             amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+             amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+             /* Harmonic mean approximation diverges for very small
+              * values. If ratio < 0.01% ignore
+              */
+             if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+                     continue;
+
+             total = ktime_add(total, ttotal);
+             max = ktime_after(tmax, max) ? tmax : max;
       }
 
       mutex_unlock(&mgr->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +0 -0
index 10dcf59a5c6b..3541dfb059ec 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h struct drm_file; · line 36

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
@@ -30,6 +30,7 @@ struct drm_file;
  struct amdgpu_fpriv;
 
  #define AMDGPU_MAX_ENTITY_NUM 4
+#define AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(max, total) (max > 16384ULL*total)
 
  struct amdgpu_ctx_entity {
       uint64_t                sequence;

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#15 lore raw

PATCH 1/2 drm/amdgpu: free resources on fence usage query

In reply to Christian König

Free the resources if the fence needs to be ignored during the ratio calculation

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6 -2
index 01fe60fedcbe..9036c93b4a0c 100644
@@ -669,11 +669,15 @@ void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *cen
 		if (!fence)
 			continue;
 		s_fence = to_drm_sched_fence(fence);
-		if (!dma_fence_is_signaled(&s_fence->scheduled))
+		if (!dma_fence_is_signaled(&s_fence->scheduled)) {
+			dma_fence_put(fence);
 			continue;
+		}
 		t1 = s_fence->scheduled.timestamp;
-		if (t1 >= now)
+		if (!ktime_before(t1, now)) {
+			dma_fence_put(fence);
 			continue;
+		}
 		if (dma_fence_is_signaled(&s_fence->finished) &&
 			s_fence->finished.timestamp < now)
 			*total += ktime_sub(s_fence->finished.timestamp, t1);
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#16 lore raw

PATCH 2/2 drm/amdgpu: fix fence calculation (v2)

The proper metric for fence utilization over several contexts is an harmonic mean, but such calculation is prohibitive in kernel space, so the code approximates it.

Because the approximation diverges when one context has a very small ratio compared with the other context, this change filter out ratios smaller that 0.01%

v2: make the fence calculation static and initialize variables within that function

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 19 ++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h |  1 +
 2 files changed, 17 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +16 -3
index 9036c93b4a0c..fc83445fbc40 100644
@@ -652,12 +652,14 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
 	mutex_destroy(&mgr->lock);
 }
 
-void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *centity,
-		ktime_t *total, ktime_t *max)
+static void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx,
+		struct amdgpu_ctx_entity *centity, ktime_t *total, ktime_t *max)
 {
 	ktime_t now, t1;
 	uint32_t i;
 
+	*total = *max = 0;
+
 	now = ktime_get();
 	for (i = 0; i < amdgpu_sched_jobs; i++) {
 		struct dma_fence *fence;
@@ -703,11 +705,22 @@ ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,
 	idp = &mgr->ctx_handles;
 	mutex_lock(&mgr->lock);
 	idr_for_each_entry(idp, ctx, id) {
+		ktime_t ttotal, tmax;
+
 		if (!ctx->entities[hwip][idx])
 			continue;
 
 		centity = ctx->entities[hwip][idx];
-		amdgpu_ctx_fence_time(ctx, centity, &total, &max);
+		amdgpu_ctx_fence_time(ctx, centity, &ttotal, &tmax);
+
+		/* Harmonic mean approximation diverges for very small
+		 * values. If ratio < 0.01% ignore
+		 */
+		if (AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(tmax, ttotal))
+			continue;
+
+		total = ktime_add(total, ttotal);
+		max = ktime_after(tmax, max) ? tmax : max;
 	}
 
 	mutex_unlock(&mgr->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h +1 -0
index 10dcf59a5c6b..3541dfb059ec 100644
@@ -30,6 +30,7 @@ struct drm_file;
 struct amdgpu_fpriv;
 
 #define AMDGPU_MAX_ENTITY_NUM 4
+#define AMDGPU_CTX_FENCE_USAGE_MIN_RATIO(max, total) (max > 16384ULL*total)
 
 struct amdgpu_ctx_entity {
 	uint64_t		sequence;
Signature · 6 lines
2.17.1

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
Reviewed-by on v1 1/2
#17 lore raw

Re: [PATCH 1/2] drm/amdgpu: free resources on fence usage query

In reply to David M Nieto
Quoted 5 lines · David M Nieto

Free the resources if the fence needs to be ignored during the ratio calculation

Series is:

Will push it momentarily.

Alex

Quoted 29 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6 -3
index 01fe60fedcbe..9036c93b4a0c 100644
@@ -669,11 +669,15 @@ void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *cen
                if (!fence)
                        continue;
                s_fence = to_drm_sched_fence(fence);
-               if (!dma_fence_is_signaled(&s_fence->scheduled))
+               if (!dma_fence_is_signaled(&s_fence->scheduled)) {
+                       dma_fence_put(fence);
                        continue;
+               }
                t1 = s_fence->scheduled.timestamp;
-               if (t1 >= now)
+               if (!ktime_before(t1, now)) {
+                       dma_fence_put(fence);
                        continue;
+               }
                if (dma_fence_is_signaled(&s_fence->finished) &&
                        s_fence->finished.timestamp < now)
                        *total += ktime_sub(s_fence->finished.timestamp, t1);
--

2.17.1

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Reviewed-by
#18 lore raw

Re: [PATCH 1/2] drm/amdgpu: free resources on fence usage query

Am 13.05.21 um 20:00 schrieb Alex Deucher:

Quoted 7 lines · David M Nieto
Quoted 4 lines

Free the resources if the fence needs to be ignored during the ratio calculation

Series is:

Quoted 1 line

Will push it momentarily.

To drm-misc-next or amd-staging-drm-next? You need to rebase on upstream for the later.

Christian.

Quoted 33 lines

Alex

Quoted 29 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6 -3
index 01fe60fedcbe..9036c93b4a0c 100644
@@ -669,11 +669,15 @@ void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *cen
                 if (!fence)
                         continue;
                 s_fence = to_drm_sched_fence(fence);
-               if (!dma_fence_is_signaled(&s_fence->scheduled))
+               if (!dma_fence_is_signaled(&s_fence->scheduled)) {
+                       dma_fence_put(fence);
                         continue;
+               }
                 t1 = s_fence->scheduled.timestamp;
-               if (t1 >= now)
+               if (!ktime_before(t1, now)) {
+                       dma_fence_put(fence);
                         continue;
+               }
                 if (dma_fence_is_signaled(&s_fence->finished) &&
                         s_fence->finished.timestamp < now)
                         *total += ktime_sub(s_fence->finished.timestamp, t1);
--

2.17.1

Quoted 8 lines
Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#19 lore raw

Re: [PATCH 1/2] drm/amdgpu: free resources on fence usage query

Quoted 10 lines · Christian König

Am 13.05.21 um 20:00 schrieb Alex Deucher:

Quoted 7 lines · David M Nieto
Quoted 4 lines

Free the resources if the fence needs to be ignored during the ratio calculation

Series is:

Quoted 6 lines
Quoted 1 line

Will push it momentarily.

To drm-misc-next or amd-staging-drm-next? You need to rebase on upstream for the later.

drm-misc-next since that is where the relevant prior patches landed.

Alex

Quoted 45 lines

Christian.

Quoted 41 lines

Alex

Quoted 29 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6 -3
index 01fe60fedcbe..9036c93b4a0c 100644
@@ -669,11 +669,15 @@ void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *cen
                 if (!fence)
                         continue;
                 s_fence = to_drm_sched_fence(fence);
-               if (!dma_fence_is_signaled(&s_fence->scheduled))
+               if (!dma_fence_is_signaled(&s_fence->scheduled)) {
+                       dma_fence_put(fence);
                         continue;
+               }
                 t1 = s_fence->scheduled.timestamp;
-               if (t1 >= now)
+               if (!ktime_before(t1, now)) {
+                       dma_fence_put(fence);
                         continue;
+               }
                 if (dma_fence_is_signaled(&s_fence->finished) &&
                         s_fence->finished.timestamp < now)
                         *total += ktime_sub(s_fence->finished.timestamp, t1);
--

2.17.1

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx