Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
To: igt-dev@lists.freedesktop.org
Cc: dri-devel@lists.freedesktop.org, amd-gfx@lists.freedesktop.org,
	intel-xe@lists.freedesktop.org,
	"Christian Koenig" <christian.koenig@amd.com>,
	maarten.lankhorst@linux.intel.com,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Natalie Vock" <natalie.vock@gmx.de>,
	kernel-dev@igalia.com,
	"Tvrtko Ursulin" <tvrtko.ursulin@igalia.com>,
	"Thadeu Lima de Souza Cascardo" <cascardo@igalia.com>
Subject: [PATCH i-g-t 6/8] dmem: add test for current/max
Date: Tue, 12 May 2026 18:51:53 -0300	[thread overview]
Message-ID: <20260512215156.4083082-7-cascardo@igalia.com> (raw)
In-Reply-To: <20260512215156.4083082-1-cascardo@igalia.com>

Add a test that checks for current usage after VRAM allocation and release.
Set max to different values and track that current usage is not above max,
given some slack.

Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
---
 tests/drv_dmem_cgroups.c | 136 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 133 insertions(+), 3 deletions(-)

diff --git a/tests/drv_dmem_cgroups.c b/tests/drv_dmem_cgroups.c
index 0e26b7e2bb9a..43331117854c 100644
--- a/tests/drv_dmem_cgroups.c
+++ b/tests/drv_dmem_cgroups.c
@@ -223,12 +223,142 @@ static void test_write_eviction(int fd, unsigned int flags, const struct igt_dme
 	igt_cgroup_free(cg);
 }
 
+static int allocate_vram(const struct igt_dmem_driver *drv, void *ctx, int fd, int max_bo, size_t len)
+{
+	int n_bo, err = 0;
+	for (n_bo = 0; n_bo < max_bo; n_bo++) {
+		err = drv->allocate_vram(ctx, n_bo, len);
+		if (err)
+			break;
+	}
+	return err ?: n_bo;
+}
+
+static void test_current(int fd, unsigned int flags, const struct igt_dmem_driver *drv)
+{
+	struct igt_cgroup *cg;
+	char *cg_region;
+	void *ctx;
+	uint64_t current, capacity, cg_max;
+	int n_bo = 0, max_bo;
+	int err;
+
+	cg_region = drv->get_region_name(fd);
+	igt_require_f(cg_region, "Region not tracked by dmem cgroup controller\n");
+
+	/* Check dmem cgroup controller is available before doing anything else */
+	igt_require_f(igt_cgroup_dmem_available(),
+		      "dmem cgroup controller not available (no cgroup v2 or no registered regions)\n");
+
+	igt_cgroup_dmem_get_capacity(cg_region, &capacity);
+	igt_require_f(capacity >= 4 * BO_SIZE,
+		      "VRAM capacity (%"PRIu64" MiB) too small to test\n",
+		      capacity / SZ_1M);
+
+	/*
+	 * Use up to 4 GiB, or the full capacity if the device has less.
+	 * Leave one BO_SIZE worth of headroom so the device isn't completely
+	 * exhausted before the cgroup limit is hit.
+	 */
+	cg_max = min(MAX_LIMIT, capacity - BO_SIZE);
+	cg_max = ALIGN_DOWN(cg_max, EVICT_STEP);
+
+	if (flags & TEST_INTERRUPTIBLE)
+		install_sigcont_counter();
+
+	/* Create cgroup and move into it */
+	cg = igt_cgroup_new("igt_cgroups_test");
+	igt_cgroup_move_current(cg);
+
+	max_bo = cg_max / BO_SIZE;
+
+	err = drv->init(&ctx, fd, max_bo);
+	igt_assert_f(!err, "Failed to initialize driver");
+
+	n_bo = allocate_vram(drv, ctx, fd, max_bo, BO_SIZE);
+	igt_assert_f(n_bo > 0, "failed to allocate VRAM\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current < cg_max + USAGE_SLACK && current > cg_max - USAGE_SLACK,
+		     "current usage (%"PRIu64" MiB) is not within margin of allocation (%"PRIu64" MiB)\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	drv->free_vram(ctx, n_bo, BO_SIZE);
+	sleep(1);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current < USAGE_SLACK,
+		     "current usage (%"PRIu64" MiB) is not within margin (%d MiB)\n",
+		     current / SZ_1M, USAGE_SLACK / SZ_1M);
+
+	igt_cgroup_dmem_set_max(cg, cg_region, 2 * BO_SIZE);
+
+	n_bo = allocate_vram(drv, ctx, fd, max_bo, BO_SIZE);
+	igt_assert_f(n_bo > 0, "failed to allocate VRAM\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current < 2 * BO_SIZE + USAGE_SLACK && current > 2 * BO_SIZE - USAGE_SLACK,
+		     "current usage (%"PRIu64" MiB) is not within margin of allocation (%"PRIu64" MiB)\n",
+		     current / SZ_1M, cg_max / SZ_1M);
+
+	drv->free_vram(ctx, n_bo, BO_SIZE);
+	sleep(1);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current < USAGE_SLACK,
+		     "current usage (%"PRIu64" MiB) is not within margin (%d MiB)\n",
+		     current / SZ_1M, USAGE_SLACK / SZ_1M);
+
+	igt_cgroup_dmem_set_max(cg, cg_region, 0);
+
+	n_bo = allocate_vram(drv, ctx, fd, max_bo, BO_SIZE);
+	igt_assert_f(n_bo != -ENOMEM, "VRAM allocation succeeded despite max set to 0\n");
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After fill: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current < USAGE_SLACK,
+		     "current usage (%"PRIu64" MiB) is not within margin\n",
+		     current / SZ_1M);
+
+	if (n_bo > 0)
+		drv->free_vram(ctx, n_bo, BO_SIZE);
+	sleep(1);
+
+	igt_cgroup_dmem_get_current(cg, cg_region, &current);
+	igt_debug("After free: cgroup current = %"PRIu64" MiB, "
+		  "max = %"PRIu64" MiB\n",
+		  current / SZ_1M, cg_max / SZ_1M);
+	igt_assert_f(current < USAGE_SLACK,
+		     "current usage (%"PRIu64" MiB) is not within margin (%d MiB)\n",
+		     current / SZ_1M, USAGE_SLACK / SZ_1M);
+
+	drv->deinit(ctx);
+	free(cg_region);
+	igt_cgroup_free(cg);
+}
+
 static const struct {
 	const char *name;
+	void (*test_fn)(int fd, unsigned int flags, const struct igt_dmem_driver *drv);
 	unsigned int flags;
 } subtests[] = {
-	{ "write_eviction",		0 },
-	{ "write_eviction_interruptible",	TEST_INTERRUPTIBLE },
+	{ "current", test_current, 0 },
+	{ "write_eviction", test_write_eviction, 0 },
+	{ "write_eviction_interruptible", test_write_eviction, TEST_INTERRUPTIBLE },
 	{ }
 };
 
@@ -259,7 +389,7 @@ int igt_main()
 
 			for (int i = 0; subtests[i].name; i++)
 				igt_subtest_f("%s-%s", drivers[d].driver->name, subtests[i].name)
-					test_write_eviction(fd, subtests[i].flags, drivers[d].driver);
+					subtests[i].test_fn(fd, subtests[i].flags, drivers[d].driver);
 
 			igt_fixture() {
 				if (fd >= 0)
-- 
2.47.3


  parent reply	other threads:[~2026-05-12 21:52 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-12 21:51 [PATCH i-g-t 0/8] dmem: add amdgpu support and one more test Thadeu Lima de Souza Cascardo
2026-05-12 21:51 ` [PATCH i-g-t 1/8] Introduce dmem driver and implement Xe support Thadeu Lima de Souza Cascardo
2026-05-12 21:51 ` [PATCH i-g-t 2/8] Adjust xe_cgroups test to use igt_dmem_driver Thadeu Lima de Souza Cascardo
2026-05-13 15:18   ` Kamil Konieczny
2026-05-12 21:51 ` [PATCH i-g-t 3/8] Make xe_cgroup test a generic test Thadeu Lima de Souza Cascardo
2026-05-13 15:21   ` Kamil Konieczny
2026-05-12 21:51 ` [PATCH i-g-t 4/8] amdgpu: add amdgpu_cgroup_region_name Thadeu Lima de Souza Cascardo
2026-05-12 21:51 ` [PATCH i-g-t 5/8] igt_dmem_driver: add amdgpu support Thadeu Lima de Souza Cascardo
2026-05-13 15:26   ` Kamil Konieczny
2026-05-12 21:51 ` Thadeu Lima de Souza Cascardo [this message]
2026-05-13 15:31   ` [PATCH i-g-t 6/8] dmem: add test for current/max Kamil Konieczny
2026-05-12 21:51 ` [PATCH i-g-t 7/8] dmem: only check for dmem availability once Thadeu Lima de Souza Cascardo
2026-05-12 21:51 ` [PATCH i-g-t 8/8] dmem: get region once per driver Thadeu Lima de Souza Cascardo
2026-05-12 22:27 ` [PATCH i-g-t 0/8] dmem: add amdgpu support and one more test Thadeu Lima de Souza Cascardo
2026-05-13  8:06 ` Christian König
2026-05-13  8:54   ` Thomas Hellström

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260512215156.4083082-7-cascardo@igalia.com \
    --to=cascardo@igalia.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=christian.koenig@amd.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=kernel-dev@igalia.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=natalie.vock@gmx.de \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tvrtko.ursulin@igalia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox