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 phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8537CC54EBC for ; Sun, 8 Jan 2023 02:52:18 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 8B45C8567D; Sun, 8 Jan 2023 03:51:29 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="iAaFCRzm"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B1F5D85648; Sun, 8 Jan 2023 03:51:17 +0100 (CET) Received: from mail-il1-x130.google.com (mail-il1-x130.google.com [IPv6:2607:f8b0:4864:20::130]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6AB5A85514 for ; Sun, 8 Jan 2023 03:51:02 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x130.google.com with SMTP id c20so3284358ilj.10 for ; Sat, 07 Jan 2023 18:51:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DWh4tl0Z7c3S041LkBdFgdgrcdG3Gbn5+kGk2XtIX6E=; b=iAaFCRzmth3DuSSm6z3l6XBUjR0VWF2+AQ6JX6qZJHWHDMItGQP3pmg+XNbi56dLAy DElDBgBdYETIwTyUx7b0cUsLHIIqMeK4NnjjuuyUiStx27IyNxYsaeAOcHKEgEgys4kV 9I+DnK9ItdeN+hEaGo4TswKEcJClsUBM74FRA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DWh4tl0Z7c3S041LkBdFgdgrcdG3Gbn5+kGk2XtIX6E=; b=Fqgx2d7IJSxY8kF74m3wmh8cSvZWxjNaHRqUQCP3fUoMoNeRdrD7+hS1H+Le9It21i CDkKWtRunzJHGGxaITj0/jQ6lRoRsIHDJi5p2MO3iPDrkxTU3zHl76otTKGc6w+pny/u UyaTxVZcQzb7aboF5QDfeeJBf63d2ZDqs/MqAMXiJf0dBqY7z1J8gVf23e2XSVEjUD/k NmSZcrkEoJstQO7lzsHwSRKW+8xLMtRU9Xv7DSYMA9Wz0Cto5W2aziZJr+ctnwWyuYMX 76pzNN6Q91No0IOPWFu6J3dQ0Bye3tYGBtSYew3w4cvE0pUpEPdNjBtyAd6wZ8p9R8qe KHgg== X-Gm-Message-State: AFqh2kpIXSknaMkGl+hXxGaXVDBQkAl/TnyPjplN0TyrWLB5c4HnF/lA L3bo4OHz6/P7KDNFAwatEk4SwEo6zGLItlfZ X-Google-Smtp-Source: AMrXdXtP4SjndhMvGUxw7m7fz886Z8+0AdDbBkuMrSGc48pjP+BW/pmd0fh136OfNUbgNEMLYUIIVw== X-Received: by 2002:a92:cac1:0:b0:30c:c42:cf9b with SMTP id m1-20020a92cac1000000b0030c0c42cf9bmr22970650ilq.16.1673146260737; Sat, 07 Jan 2023 18:51:00 -0800 (PST) Received: from sjg1.roam.corp.google.com (c-71-56-217-229.hsd1.co.comcast.net. [71.56.217.229]) by smtp.gmail.com with ESMTPSA id c17-20020a92bd11000000b00302a00aaefasm1622174ile.26.2023.01.07.18.51.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Jan 2023 18:51:00 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: U-Boot Custodians , Simon Glass , Marek Vasut , Pavel Herrmann Subject: [PATCH v2 02/71] dm: core: Support sorting devices with dm tree Date: Sat, 7 Jan 2023 19:49:38 -0700 Message-Id: <20230108025047.522240-3-sjg@chromium.org> X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog In-Reply-To: <20230108025047.522240-1-sjg@chromium.org> References: <20230108025047.522240-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Add a -s flag to sort the top-level devices in order of uclass ID. Signed-off-by: Simon Glass --- (no changes since v1) cmd/dm.c | 10 +++++-- doc/usage/cmd/dm.rst | 5 +++- drivers/core/dump.c | 65 ++++++++++++++++++++++++++++++++++++---- include/dm/util.h | 8 +++-- test/py/tests/test_dm.py | 38 +++++++++++++++++++++++ 5 files changed, 114 insertions(+), 12 deletions(-) diff --git a/cmd/dm.c b/cmd/dm.c index 218be85795d..979cd36061e 100644 --- a/cmd/dm.c +++ b/cmd/dm.c @@ -59,7 +59,11 @@ static int do_dm_dump_static_driver_info(struct cmd_tbl *cmdtp, int flag, static int do_dm_dump_tree(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { - dm_dump_tree(); + bool sort; + + sort = argc > 1 && !strcmp(argv[1], "-s"); + + dm_dump_tree(sort); return 0; } @@ -87,7 +91,7 @@ static char dm_help_text[] = "dm drivers Dump list of drivers with uclass and instances\n" DM_MEM_HELP "dm static Dump list of drivers with static platform data\n" - "dm tree Dump tree of driver model devices ('*' = activated)\n" + "dm tree [-s] Dump tree of driver model devices (-s=sort)\n" "dm uclass Dump list of instances for each uclass" ; #endif @@ -98,5 +102,5 @@ U_BOOT_CMD_WITH_SUBCMDS(dm, "Driver model low level access", dm_help_text, U_BOOT_SUBCMD_MKENT(drivers, 1, 1, do_dm_dump_drivers), DM_MEM U_BOOT_SUBCMD_MKENT(static, 1, 1, do_dm_dump_static_driver_info), - U_BOOT_SUBCMD_MKENT(tree, 1, 1, do_dm_dump_tree), + U_BOOT_SUBCMD_MKENT(tree, 2, 1, do_dm_dump_tree), U_BOOT_SUBCMD_MKENT(uclass, 1, 1, do_dm_dump_uclass)); diff --git a/doc/usage/cmd/dm.rst b/doc/usage/cmd/dm.rst index 7bc1962a754..236cd02bd62 100644 --- a/doc/usage/cmd/dm.rst +++ b/doc/usage/cmd/dm.rst @@ -12,7 +12,7 @@ Synopis dm devres dm drivers dm static - dm tree + dm tree [-s] dm uclass Description @@ -123,6 +123,9 @@ Name Shows the device name as well as the tree structure, since child devices are shown attached to their parent. +If -s is given, the top-level devices (those which are children of the root +device) are shown sorted in order of uclass ID, so it is easier to find a +particular device type. dm uclass ~~~~~~~~~ diff --git a/drivers/core/dump.c b/drivers/core/dump.c index 1c1f7e4d308..0c7d2ec4d0c 100644 --- a/drivers/core/dump.c +++ b/drivers/core/dump.c @@ -5,12 +5,34 @@ #include #include +#include #include +#include #include #include #include -static void show_devices(struct udevice *dev, int depth, int last_flag) +/** + * struct sort_info - information used for sorting + * + * @dev: List of devices + * @alloced: Maximum number of devices in @dev + */ +struct sort_info { + struct udevice **dev; + int size; +}; + +static int h_cmp_uclass_id(const void *d1, const void *d2) +{ + const struct udevice *const *dev1 = d1; + const struct udevice *const *dev2 = d2; + + return device_get_uclass_id(*dev1) - device_get_uclass_id(*dev2); +} + +static void show_devices(struct udevice *dev, int depth, int last_flag, + struct udevice **devs) { int i, is_last; struct udevice *child; @@ -39,21 +61,52 @@ static void show_devices(struct udevice *dev, int depth, int last_flag) printf("%s\n", dev->name); - device_foreach_child(child, dev) { - is_last = list_is_last(&child->sibling_node, &dev->child_head); - show_devices(child, depth + 1, (last_flag << 1) | is_last); + if (devs) { + int count; + int i; + + count = 0; + device_foreach_child(child, dev) + devs[count++] = child; + qsort(devs, count, sizeof(struct udevice *), h_cmp_uclass_id); + + for (i = 0; i < count; i++) { + show_devices(devs[i], depth + 1, + (last_flag << 1) | (i == count - 1), + devs + count); + } + } else { + device_foreach_child(child, dev) { + is_last = list_is_last(&child->sibling_node, + &dev->child_head); + show_devices(child, depth + 1, + (last_flag << 1) | is_last, NULL); + } } } -void dm_dump_tree(void) +void dm_dump_tree(bool sort) { struct udevice *root; root = dm_root(); if (root) { + int dev_count, uclasses; + struct udevice **devs = NULL; + + dm_get_stats(&dev_count, &uclasses); + printf(" Class Index Probed Driver Name\n"); printf("-----------------------------------------------------------\n"); - show_devices(root, -1, 0); + if (sort) { + devs = calloc(dev_count, sizeof(struct udevice *)); + if (!devs) { + printf("(out of memory)\n"); + return; + } + } + show_devices(root, -1, 0, devs); + free(devs); } } diff --git a/include/dm/util.h b/include/dm/util.h index e10c6060ce0..4bb49e9e8c0 100644 --- a/include/dm/util.h +++ b/include/dm/util.h @@ -26,8 +26,12 @@ struct list_head; */ int list_count_items(struct list_head *head); -/* Dump out a tree of all devices */ -void dm_dump_tree(void); +/** + * Dump out a tree of all devices + * + * @sort: Sort by uclass name + */ +void dm_dump_tree(bool sort); /* Dump out a list of uclasses and their devices */ void dm_dump_uclass(void); diff --git a/test/py/tests/test_dm.py b/test/py/tests/test_dm.py index ea93061fdfa..68d4ea12235 100644 --- a/test/py/tests/test_dm.py +++ b/test/py/tests/test_dm.py @@ -16,6 +16,44 @@ def test_dm_compat(u_boot_console): for driver in drivers: assert driver in response + # check sorting - output looks something like this: + # testacpi 0 [ ] testacpi_drv |-- acpi-test + # testacpi 1 [ ] testacpi_drv | `-- child + # pci_emul_p 1 [ ] pci_emul_parent_drv |-- pci-emul2 + # pci_emul 5 [ ] sandbox_swap_case_em | `-- emul2@1f,0 + + # The number of '| ' and '--' matches indicate the indent level. We start + # checking sorting only after UCLASS_AXI_EMUL after which the names should + # be sorted. + + response = u_boot_console.run_command('dm tree -s') + lines = response.split('\n')[2:] + stack = [] # holds where we were up to at the previous indent level + prev = '' # uclass name of previous line + start = False + for line in lines: + indent = line.count('| ') + ('--' in line) + cur = line.split()[0] + if not start: + if cur != 'axi_emul': + continue + start = True + + # Handle going up or down an indent level + if indent > len(stack): + stack.append(prev) + prev = '' + elif indent < len(stack): + prev = stack.pop() + + # Check that the current uclass name is not alphabetically before the + # previous one + if 'emul' not in cur and cur < prev: + print('indent', cur >= prev, indent, prev, cur, stack) + assert cur >= prev + prev = cur + + @pytest.mark.buildconfigspec('cmd_dm') def test_dm_drivers(u_boot_console): """Test that each driver in `dm compat` is also listed in `dm drivers`.""" -- 2.39.0.314.g84b9a713c41-goog