From patchwork Wed Nov 9 18:51:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lancelot SIX X-Patchwork-Id: 60290 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 787133AA9832 for ; Wed, 9 Nov 2022 18:52:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 787133AA9832 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668019937; bh=TWxYZBF0gscb3FgR1YfQdmO55LnadEFn0YQHplazNV8=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=x5g7FgzZ+s5vw5ZduZak5HUy41zKTQEPOcRaHS3DmG6pf3uLiIhXG3m30dFV33Zbq zC2oWsvtI/+XF2PoO0JzoXnPVwoS+AF4qa8woyuoEUHONGUkfZpMS/dzql6gVgKFGx w6ymOxMcVJdnHhafK6DzWb/e6GKExqRZVHM4NLeo= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2059.outbound.protection.outlook.com [40.107.237.59]) by sourceware.org (Postfix) with ESMTPS id B58C03AA9822 for ; Wed, 9 Nov 2022 18:51:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B58C03AA9822 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l8kp+THMwZ/dtWHm1iGMx1OGP5q0WJwcNmb5HPbF8VHsHe2aJDYpC13q4tTeLyWUkoZIAQR1ZEsx3Y6MG3n68kgYOracCp1myvh+LcmRQzAKPAd9+3sTllCU3TcTyxWypsV/BdtWetWFlKnP103D+/m0PeWFRHdZSsuFpOI0jH8vcdeIjRREGvZHeipDlpvMX56z1vc3XC1ySZM/C+fv30qGFprWFMS/UGJHaU0fk1wRpkmLQAHVFP9aIWUaXF88xmsz3J+PEE8nOFmjlQgKATepsiGflfCVpspbY02AovxYgdDJvYsLySJuKbeIMVyKEEo90b6yGTDN75Qk6qRn/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=TWxYZBF0gscb3FgR1YfQdmO55LnadEFn0YQHplazNV8=; b=KcFk4tigWtG74NVfAY6oW9Ibkq5qQP9hcnBnhzeEsZv+kj5+AsIOShkFelnN4es7uWZ0wipekwhkpktraoRdXByO7TJXANcB70NUcRVnot9gBNp98zxT2sj1Q0UQLHqWKRr1NlUeGYhG/UBIZ3nHbE58O7l6oPHc/HwRNUpiDsIguAxAnOysMY9xrNMPH+Mbo4VMOINFEFcdGNdHhgXxvK9JT9BUrlwb9h8oxyRjYY6Tr/ZpcPKTWc1fgEwyVLTqy/bhZJPteNpRT+l6m36TyQRtba0uVSNIS2pIYRkJX7mPTM5QN5+GNIDlMWocY83tQrH2O/ZYHdNqUFBq8ODWmw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=sourceware.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none Received: from BN9PR03CA0189.namprd03.prod.outlook.com (2603:10b6:408:f9::14) by BY5PR12MB4146.namprd12.prod.outlook.com (2603:10b6:a03:20d::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.12; Wed, 9 Nov 2022 18:51:47 +0000 Received: from BN8NAM11FT104.eop-nam11.prod.protection.outlook.com (2603:10b6:408:f9:cafe::97) by BN9PR03CA0189.outlook.office365.com (2603:10b6:408:f9::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.27 via Frontend Transport; Wed, 9 Nov 2022 18:51:47 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by BN8NAM11FT104.mail.protection.outlook.com (10.13.177.160) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5813.12 via Frontend Transport; Wed, 9 Nov 2022 18:51:47 +0000 Received: from khazad-dum.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 9 Nov 2022 12:51:43 -0600 To: CC: , , , Lancelot SIX Subject: [PATCH v2] gdb/py-inferior: Keep inferior threads in a map Date: Wed, 9 Nov 2022 18:51:20 +0000 Message-ID: <20221109185120.1828613-1-lancelot.six@amd.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN8NAM11FT104:EE_|BY5PR12MB4146:EE_ X-MS-Office365-Filtering-Correlation-Id: 74762da7-ef4a-49a6-c9b6-08dac283743d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EpB9OMhIeTzIIhvAujs1rxn4RCC2TixZVYGPk8aAq37QLSAoSV6o1oW+55vuxgLAVRuwvTKeMi7nbONazRhed7Pv9decj9xfhvZiMnbm23JnlRswwAn4+OTIp+bFNaRiIBkBPc99LCw8XKqvmL/Tum2F5QHkTtrkuNV0sbtP78O8OEZbciwazqTsgy1/frs77VbJpVbsmE/bNYjukfBtkxwbeHe30ZsADWBqKsXcPk5H6RU6rvJA6QYH0Joh0OAjCXss3znXNSynL28e9OwtSctkxQ3qOuY24QGF50HsrvrW2BBQQpnQbPStVintczVox4cau/qQKPncR1gtXha5DUUsMnRqHYxRSMQI1Zo0vJOW/9rroOSjbSOwuDpG0ueB3jfFdECzygmcVI+q8FhG/yOC9YSUbTGLpxZRAh/ffEthYgfrwoyiEckaA/8WzkcfzsalY0glwOuCWkuZovpkTeTsC80CwC3cYFVY0NML9PpZfHylTOOLPzyrvBLbpLSkGYNZQgkl1uQ1tU5GmXsSVc9AVCZGHFMOfObZ0am5Et1Mq9nHjNKG2ODVO/ya3CnST3Xu51RAB2kha77OOGWriB032PXvWbZxl4v/Ir4swhVk9DmTE73MI3ojCkxpXjZj07XA+qDb8HozTnSWC3hE/afHNDaI2JfiQ8pJOf1yql3DGltB2AIEy9hicGGUbqZLJqaVvVNJXeuZeuzKogN5/zKxXAp/u2VJFSi3583YYDS6e91dtlwiQICkXBqjOaze01BqIonRWmvwh/RsnQuBYFkBiTLSf5YhtnksYWj8WO9ZkIigZck2XhWGjRmVUoNwv7kGqFULZIiqmXlQUQ5eFw== X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:SATLEXMB04.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230022)(4636009)(376002)(136003)(346002)(39860400002)(396003)(451199015)(40470700004)(36840700001)(46966006)(8676002)(70586007)(70206006)(82740400003)(4326008)(316002)(7696005)(40480700001)(83380400001)(336012)(40460700003)(2906002)(426003)(47076005)(41300700001)(86362001)(26005)(36860700001)(16526019)(186003)(5660300002)(2616005)(36756003)(1076003)(8936002)(6916009)(66899015)(966005)(82310400005)(478600001)(356005)(6666004)(54906003)(81166007)(36900700001); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Nov 2022 18:51:47.2588 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 74762da7-ef4a-49a6-c9b6-08dac283743d X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT104.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4146 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Lancelot SIX via Gdb-patches From: Lancelot SIX Reply-To: Lancelot SIX Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Changes since V1: - Changed the implementation (and description) to use std::unordered_map instead of std::map. - Added a comment in delete_thread_object to explain why we do "it->second->thread = nullptr" as this is easy to miss. - In infpy_threads: move i's increment outside of PyTuple_SET_ITEM to avoid issues if this is or becomes a macro evaluating its arguments more than once. The python code maintains a list of threads for each inferior. This list is implemented as a linked list. When the number of threads grows high, this implementation can begin to be a performance bottleneck as finding a particular thread_object in the list has a complexity of O(N). We see this in ROCgdb[1], a downstream port of GDB for AMDGUP. On AMDGPU devices, the number of threads can get significantly higher than on usual GDB workloads. In some situations, we can reach the end of the inferior process with GDB still having a substantial list of known threads. While running target_mourn_inferior, we end up in inferior::clear_thread_list which iterates over all remaining threads and marks each thread exited. This fires the gdb::observers::thread_exit observer and eventually py-inferior.c:set_thread_exited gets called. This function searches in the linked list with poor performances. This patch proposes to change the linked list that keeps the per inferior_object list of thread_objects into a std::unordered_map. This allows to have the search operation complexity be O(1) on average instead of O(N). With this patch, we can complete clear_thread_list in about 2.5 seconds compared to 10 minutes without it. Except for the performance change, no user visible change is expected. Regression tested on Ubuntu-22.04 x86_64. [1] https://github.com/ROCm-Developer-Tools/ROCgdb --- gdb/python/py-inferior.c | 99 ++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 65 deletions(-) base-commit: 2d4989e98ee5e63b76ce3e35b0e02baaee2f50cb diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 8847a6d9308..4d5e09db680 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -30,17 +30,10 @@ #include "gdbsupport/gdb_signals.h" #include "py-event.h" #include "py-stopevent.h" +#include -struct threadlist_entry -{ - threadlist_entry (gdbpy_ref &&ref) - : thread_obj (std::move (ref)) - { - } - - gdbpy_ref thread_obj; - struct threadlist_entry *next; -}; +using thread_map_t + = std::unordered_map>; struct inferior_object { @@ -49,12 +42,9 @@ struct inferior_object /* The inferior we represent. */ struct inferior *inferior; - /* thread_object instances under this inferior. This list owns a + /* thread_object instances under this inferior. This owns a reference to each object it contains. */ - struct threadlist_entry *threads; - - /* Number of threads in the list. */ - int nthreads; + thread_map_t *threads; }; extern PyTypeObject inferior_object_type @@ -65,8 +55,6 @@ struct infpy_deleter { void operator() (inferior_object *obj) { - struct threadlist_entry *th_entry, *th_tmp; - if (!gdb_python_initialized) return; @@ -75,15 +63,7 @@ struct infpy_deleter inf_obj->inferior = NULL; - /* Deallocate threads list. */ - for (th_entry = inf_obj->threads; th_entry != NULL;) - { - th_tmp = th_entry; - th_entry = th_entry->next; - delete th_tmp; - } - - inf_obj->nthreads = 0; + delete inf_obj->threads; } }; @@ -257,8 +237,7 @@ inferior_to_inferior_object (struct inferior *inferior) return NULL; inf_obj->inferior = inferior; - inf_obj->threads = NULL; - inf_obj->nthreads = 0; + inf_obj->threads = new thread_map_t (); /* PyObject_New initializes the new object with a refcount of 1. This counts for the reference we are keeping in the inferior data. */ @@ -333,11 +312,10 @@ thread_to_thread_object (thread_info *thr) if (inf_obj == NULL) return NULL; - for (threadlist_entry *thread = inf_obj->threads; - thread != NULL; - thread = thread->next) - if (thread->thread_obj->thread == thr) - return gdbpy_ref<>::new_reference ((PyObject *) thread->thread_obj.get ()); + auto thread_it = inf_obj->threads->find (thr); + if (thread_it != inf_obj->threads->end ()) + return gdbpy_ref<>::new_reference + ((PyObject *) (thread_it->second.get ())); PyErr_SetString (PyExc_SystemError, _("could not find gdb thread object")); @@ -348,7 +326,6 @@ static void add_thread_object (struct thread_info *tp) { inferior_object *inf_obj; - struct threadlist_entry *entry; if (!gdb_python_initialized) return; @@ -364,18 +341,19 @@ add_thread_object (struct thread_info *tp) inf_obj = (inferior_object *) thread_obj->inf_obj; - entry = new threadlist_entry (std::move (thread_obj)); - entry->next = inf_obj->threads; + auto ins_result = inf_obj->threads->emplace + (thread_map_t::value_type (tp, std::move (thread_obj))); - inf_obj->threads = entry; - inf_obj->nthreads++; + if (!ins_result.second) + return; if (evregpy_no_listeners_p (gdb_py_events.new_thread)) return; - gdbpy_ref<> event = create_thread_event_object (&new_thread_event_object_type, - (PyObject *) - entry->thread_obj.get ()); + gdbpy_ref<> event = create_thread_event_object + (&new_thread_event_object_type, + (PyObject *) ins_result.first->second.get ()); + if (event == NULL || evpy_emit_event (event.get (), gdb_py_events.new_thread) < 0) gdbpy_print_stack (); @@ -384,8 +362,6 @@ add_thread_object (struct thread_info *tp) static void delete_thread_object (struct thread_info *tp, int ignore) { - struct threadlist_entry **entry, *tmp; - if (!gdb_python_initialized) return; @@ -395,29 +371,22 @@ delete_thread_object (struct thread_info *tp, int ignore) if (inf_obj == NULL) return; - /* Find thread entry in its inferior's thread_list. */ - for (entry = &inf_obj->threads; *entry != NULL; entry = - &(*entry)->next) - if ((*entry)->thread_obj->thread == tp) - break; - - if (!*entry) - return; - - tmp = *entry; - tmp->thread_obj->thread = NULL; - - *entry = (*entry)->next; - inf_obj->nthreads--; - - delete tmp; + auto it = inf_obj->threads->find (tp); + if (it != inf_obj->threads->end ()) + { + /* Some python code can still hold a reference to the thread_object + instance. Make sure to remove the link to the associated + thread_info object as it will be freed soon. This makes the python + object invalid (i.e. gdb.InfThread.is_valid returns False). */ + it->second->thread = nullptr; + inf_obj->threads->erase (it); + } } static PyObject * infpy_threads (PyObject *self, PyObject *args) { - int i; - struct threadlist_entry *entry; + int i = 0; inferior_object *inf_obj = (inferior_object *) self; PyObject *tuple; @@ -432,16 +401,16 @@ infpy_threads (PyObject *self, PyObject *args) GDB_PY_HANDLE_EXCEPTION (except); } - tuple = PyTuple_New (inf_obj->nthreads); + tuple = PyTuple_New (inf_obj->threads->size ()); if (!tuple) return NULL; - for (i = 0, entry = inf_obj->threads; i < inf_obj->nthreads; - i++, entry = entry->next) + for (const thread_map_t::value_type &entry : *inf_obj->threads) { - PyObject *thr = (PyObject *) entry->thread_obj.get (); + PyObject *thr = (PyObject *) entry.second.get (); Py_INCREF (thr); PyTuple_SET_ITEM (tuple, i, thr); + i = i + 1; } return tuple;