From patchwork Wed Feb 3 10:26:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 12063945 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,HK_RANDOM_FROM,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AD505C433E0 for ; Wed, 3 Feb 2021 10:27:23 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 4BDAE64E34 for ; Wed, 3 Feb 2021 10:27:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4BDAE64E34 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.intel.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=intel-gfx-bounces@lists.freedesktop.org Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9B0EA6EA4E; Wed, 3 Feb 2021 10:27:22 +0000 (UTC) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4A9B26EA4E; Wed, 3 Feb 2021 10:27:21 +0000 (UTC) IronPort-SDR: f/CtZH2MK9Kob9PiWyUKmk2pNlue0pMSL3K5qf+EIm0mo/xrL2gapun6LQU01rk92RQn1hsZwm 722Au09jzf8A== X-IronPort-AV: E=McAfee;i="6000,8403,9883"; a="199974100" X-IronPort-AV: E=Sophos;i="5.79,398,1602572400"; d="scan'208";a="199974100" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 02:27:20 -0800 IronPort-SDR: 9ftZiqcUk/HsgvXoLisPtjECmaanZHZDQqCfaESSaa4rGZRKrwE+dc5QvZKs3xsU0VjTylS+hu fINNLJtxbPQw== X-IronPort-AV: E=Sophos;i="5.79,398,1602572400"; d="scan'208";a="371371741" Received: from eransege-mobl.ger.corp.intel.com (HELO localhost.localdomain) ([10.214.208.106]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2021 02:27:19 -0800 From: Tvrtko Ursulin To: igt-dev@lists.freedesktop.org Date: Wed, 3 Feb 2021 10:26:01 +0000 Message-Id: <20210203102601.895197-1-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t] intel_gpu_top: Always sort the clients array after update X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Intel-gfx@lists.freedesktop.org Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" From: Tvrtko Ursulin Walking the client "list" makes assumptions about the order of active and free slots which means we need to sort the array after every update. Patch is mostly just code movement with the only functional difference of eliminating two subsequent scans with no sort in between This closes a very short window there list iteration could get confused if sysfs clients would change rapidly and unfavourably during tool startup. Signed-off-by: Tvrtko Ursulin Reviewed-by: Chris Wilson --- tools/intel_gpu_top.c | 132 +++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c index dffc6ebecc57..1ffdf5ee64b4 100644 --- a/tools/intel_gpu_top.c +++ b/tools/intel_gpu_top.c @@ -899,71 +899,6 @@ read_client_sysfs(char *buf, int bufsize, const char *sysfs_root, return __read_client_field(*client_root, field, buf, bufsize); } -static void scan_clients(struct clients *clients) -{ - struct dirent *dent; - struct client *c; - unsigned int id; - int tmp; - DIR *d; - - if (!clients) - return; - - for_each_client(clients, c, tmp) { - assert(c->status != PROBE); - if (c->status == ALIVE) - c->status = PROBE; - else - break; /* Free block at the end of array. */ - } - - d = opendir(clients->sysfs_root); - if (!d) - return; - - while ((dent = readdir(d)) != NULL) { - char name[24], pid[24]; - int ret, root = -1, *pr; - - if (dent->d_type != DT_DIR) - continue; - if (!isdigit(dent->d_name[0])) - continue; - - id = atoi(dent->d_name); - - c = find_client(clients, PROBE, id); - - if (c) - pr = &c->sysfs_root; - else - pr = &root; - - ret = read_client_sysfs(name, sizeof(name), clients->sysfs_root, - id, "name", pr); - ret |= read_client_sysfs(pid, sizeof(pid), clients->sysfs_root, - id, "pid", pr); - if (!ret) { - if (!c) - add_client(clients, id, atoi(pid), name, root); - else - update_client(c, atoi(pid), name); - } else if (c) { - c->status = PROBE; /* Will be deleted below. */ - } - } - - closedir(d); - - for_each_client(clients, c, tmp) { - if (c->status == PROBE) - free_client(c); - else if (c->status == FREE) - break; - } -} - static int client_last_cmp(const void *_a, const void *_b) { const struct client *a = _a; @@ -1060,6 +995,72 @@ static void sort_clients(struct clients *clients) } } +static void scan_clients(struct clients *clients) +{ + struct dirent *dent; + struct client *c; + unsigned int id; + int tmp; + DIR *d; + + if (!clients) + return; + + for_each_client(clients, c, tmp) { + assert(c->status != PROBE); + if (c->status == ALIVE) + c->status = PROBE; + else + break; /* Free block at the end of array. */ + } + + d = opendir(clients->sysfs_root); + if (!d) + return; + + while ((dent = readdir(d)) != NULL) { + char name[24], pid[24]; + int ret, root = -1, *pr; + + if (dent->d_type != DT_DIR) + continue; + if (!isdigit(dent->d_name[0])) + continue; + + id = atoi(dent->d_name); + + c = find_client(clients, PROBE, id); + + if (c) + pr = &c->sysfs_root; + else + pr = &root; + + ret = read_client_sysfs(name, sizeof(name), clients->sysfs_root, + id, "name", pr); + ret |= read_client_sysfs(pid, sizeof(pid), clients->sysfs_root, + id, "pid", pr); + if (!ret) { + if (!c) + add_client(clients, id, atoi(pid), name, root); + else + update_client(c, atoi(pid), name); + } else if (c) { + c->status = PROBE; /* Will be deleted below. */ + } + } + + closedir(d); + + for_each_client(clients, c, tmp) { + if (c->status == PROBE) + free_client(c); + else if (c->status == FREE) + break; + } + + sort_clients(clients); +} static const char *bars[] = { " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█" }; @@ -2305,7 +2306,6 @@ int main(int argc, char **argv) t = (double)(engines->ts.cur - engines->ts.prev) / 1e9; scan_clients(clients); - sort_clients(clients); if (stop_top) break;