Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A07CED0D164 for ; Wed, 7 Jan 2026 18:32:06 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4D07910E64F; Wed, 7 Jan 2026 18:32:06 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.b="JBNi5ajD"; dkim-atps=neutral Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) by gabe.freedesktop.org (Postfix) with ESMTPS id 064B010E24E for ; Wed, 7 Jan 2026 18:32:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1767810724; x=1799346724; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=j0wWtv8gM0SIa5Hcy+RNNlswGSUh+jra7pxri9o1L3k=; b=JBNi5ajD48lDILBLzYSQ+HS/VowWeSM8KEOLydS3vla+IEH5u7qU4onE +u7eTxqgjaeFX4BS8Wai0UTlv3udImpXW1J7xkOe8MN0NQa/zxc/3T5fY OEa4ZQbkuu4qfKQlHI6xGpjAfVCDjxIcbzysWj6v2TVBUiHM7a4uIZ/GC O5XyaTC1f59igxCrxqijavAqm9qzh8JlkxiqcyWz68CemVVFxZIFi+eEg Rbg0KOBpuStW9P7IvLw+xLhizOdBnq2u4fjTV2RppZUpTtTsVFiH+lM6p jjBVKgI4GMO+88hwzxWVJVl7LwmTIsq5dBmxlMS7fNZ5F1r7/pm+itSGH w==; X-CSE-ConnectionGUID: iYX56LCTSY6+xD2XYJEELA== X-CSE-MsgGUID: rWzmA5gKT2qJRidX9in6TA== X-IronPort-AV: E=McAfee;i="6800,10657,11664"; a="80549394" X-IronPort-AV: E=Sophos;i="6.21,208,1763452800"; d="scan'208";a="80549394" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Jan 2026 10:32:03 -0800 X-CSE-ConnectionGUID: lB6i8uogTTKDvX3E/xQo6A== X-CSE-MsgGUID: u0P5wQguQFGlPFYnPDKFjA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.21,208,1763452800"; d="scan'208";a="207845961" Received: from psoham-nuc7i7bnh.iind.intel.com ([10.190.216.151]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Jan 2026 10:31:33 -0800 From: Soham Purkait To: igt-dev@lists.freedesktop.org, riana.tauro@intel.com, badal.nilawar@intel.com, kamil.konieczny@intel.com Cc: anshuman.gupta@intel.com, soham.purkait@intel.com, umesh.nerlige.ramappa@intel.com Subject: [PATCH i-g-t v4 3/6] tools/gputop.src/xe_gputop: Close card_fd after engine population in xe_populate_engines() Date: Wed, 7 Jan 2026 23:56:51 +0530 Message-Id: <20260107182654.84173-4-soham.purkait@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260107182654.84173-1-soham.purkait@intel.com> References: <20260107182654.84173-1-soham.purkait@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: igt-dev@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development mailing list for IGT GPU Tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: igt-dev-bounces@lists.freedesktop.org Sender: "igt-dev" During the engine initialization for a specific device in GPUTOP, IGT helper functions need card_fd to access each engine and respective configs. These are required in obtaining the busyness of the respective engines through PMU in GPUTOP. But for KMS tests no other DRM client should run while it is running. So once the required configs and engines are obtained, the card_fd is closed as opened card_fd in xe_populate_engines() makes appear itself as DRM client. v1: - Initialize pmu_device_obj to null. v2: - Remove dependency on lib/xe/* libraries. (Kamil) Signed-off-by: Soham Purkait --- tools/gputop.src/xe_gputop.c | 64 +++++++++++++++++++++++++++++------- tools/gputop.src/xe_gputop.h | 2 +- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/tools/gputop.src/xe_gputop.c b/tools/gputop.src/xe_gputop.c index bb2caa6ea..55a867130 100644 --- a/tools/gputop.src/xe_gputop.c +++ b/tools/gputop.src/xe_gputop.c @@ -3,6 +3,7 @@ * Copyright © 2025 Intel Corporation */ +#include #include "xe_gputop.h" #define engine_ptr(pmu_device, n) (&(pmu_device)->engine + (n)) @@ -92,6 +93,7 @@ void xe_gputop_init(void *ptr, int index, obj = ((struct xe_gputop *)ptr) + index; obj->card = card; + obj->pmu_device_obj = NULL; } static int pmu_format_shift(int xe, const char *name) @@ -127,10 +129,11 @@ void *xe_populate_engines(const void *obj, int index) struct igt_device_card *card = ptr->card; uint64_t engine_active_config, engine_total_config; uint64_t engine_class, engine_instance, gt_shift; - struct drm_xe_engine_class_instance *hwe; + struct drm_xe_device_query q; + struct drm_xe_query_engines *engine_q; struct xe_pmu_device *engines; char device[30]; - int ret = 0; + int ret = 0, num_eng; int card_fd; if (!card || !strlen(card->card) || !strlen(card->render)) @@ -144,14 +147,45 @@ void *xe_populate_engines(const void *obj, int index) fprintf(stderr, "Failed to detect device!\n"); return NULL; } - xe_device_get(card_fd); + + memset(&q, 0, sizeof(q)); + q.query = DRM_XE_DEVICE_QUERY_ENGINES; + + ret = ioctl(card_fd, DRM_IOCTL_XE_DEVICE_QUERY, &q); + if (ret != 0) { + if (errno != ENOSPC && errno != E2BIG) + return NULL; + if (q.size == 0) + return NULL; + } + if (q.size == 0) + return NULL; + + num_eng = (int)((q.size - sizeof(struct drm_xe_query_engines)) / + sizeof(struct drm_xe_engine_class_instance)); + engines = malloc(sizeof(struct xe_pmu_device) + - xe_number_engines(card_fd) * sizeof(struct xe_engine)); + num_eng * sizeof(struct xe_engine)); if (!engines) return NULL; + engine_q = (struct drm_xe_query_engines *)calloc(1, q.size); + if (!engine_q) { + free(engines); + return NULL; + } + + q.data = (uintptr_t)engine_q; + + ret = ioctl(card_fd, DRM_IOCTL_XE_DEVICE_QUERY, &q); + if (ret != 0) { + free(engines); + free(engine_q); + return NULL; + } + memset(engines, 0, sizeof(struct xe_pmu_device) + - xe_number_engines(card_fd) * sizeof(struct xe_engine)); + num_eng * sizeof(struct xe_engine)); engines->num_engines = 0; gt_shift = pmu_format_shift(card_fd, "gt"); @@ -167,14 +201,17 @@ void *xe_populate_engines(const void *obj, int index) if (ret < 0) return NULL; - xe_for_each_engine(card_fd, hwe) { + while (engines->num_engines < engine_q->num_engines) { uint64_t param_config; struct xe_engine *engine; + struct drm_xe_engine_class_instance hwe; + + hwe = engine_q->engines[engines->num_engines].instance; engine = engine_ptr(engines, engines->num_engines); - param_config = (uint64_t)hwe->gt_id << gt_shift | hwe->engine_class << engine_class - | hwe->engine_instance << engine_instance; - engine->drm_xe_engine = *hwe; + param_config = (uint64_t)hwe.gt_id << gt_shift | hwe.engine_class << engine_class + | hwe.engine_instance << engine_instance; + engine->drm_xe_engine = hwe; engine->engine_active_ticks.config = engine_active_config | param_config; engine->engine_total_ticks.config = engine_total_config | param_config; @@ -185,7 +222,7 @@ void *xe_populate_engines(const void *obj, int index) } ret = asprintf(&engine->display_name, "GT:%u %s/%u", - hwe->gt_id, + hwe.gt_id, class_display_name(engine->drm_xe_engine.engine_class), engine->drm_xe_engine.engine_instance); @@ -197,6 +234,9 @@ void *xe_populate_engines(const void *obj, int index) engines->num_engines++; } + free(engine_q); + close(card_fd); + if (!ret) { errno = ret; return NULL; @@ -263,11 +303,11 @@ int xe_pmu_init(const void *obj, int index) fd = _open_pmu(type, &engines->num_counters, &engine->engine_active_ticks, &engines->fd); if (fd < 0) - return -1; + return fd; fd = _open_pmu(type, &engines->num_counters, &engine->engine_total_ticks, &engines->fd); if (fd < 0) - return -1; + return fd; } return 0; } diff --git a/tools/gputop.src/xe_gputop.h b/tools/gputop.src/xe_gputop.h index 1e3856071..ed2bdad97 100644 --- a/tools/gputop.src/xe_gputop.h +++ b/tools/gputop.src/xe_gputop.h @@ -7,11 +7,11 @@ #define __XE_GPUTOP_H__ #include +#include #include "igt_device_scan.h" #include "igt_perf.h" #include "utils.h" -#include "xe/xe_query.h" struct xe_pmu_pair { uint64_t cur; -- 2.34.1