From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id GdkrCyryoGqN9TgAWB0awg (envelope-from ) for ; Wed, 09 Sep 2026 01:44:10 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=ibdrOiQN; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 286511E09E; Wed, 09 Sep 2026 01:44:10 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=unavailable autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id C91C11E091 for ; Wed, 09 Sep 2026 01:44:05 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7F3844BB24E6 for ; Wed, 9 Sep 2026 05:44:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F3844BB24E6 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=ibdrOiQN Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id 0979C4BB1C2B for ; Wed, 9 Sep 2026 05:43:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0979C4BB1C2B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0979C4BB1C2B Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::535 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788932619; cv=none; b=qpTDFiWmkytILbLpAeN8HAs/OZFAIkidxIuagCeEH+Ne5yGfXxpvIYqe1drt/JRsGZ0uIZUO9YniTbS808JroudK3ft+QJlVemy72GJ3BDsDyLOXZBal+fuv9IkoZUmJtkdeZn+m3jg0c/CWRozJhF2s3UfPnSmlQAspUUb/5qM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788932619; c=relaxed/simple; bh=4M331fZvIZf65mhftc/Ac14rPxfBCK3fbRNb6otGnlc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PflI2x1qGAWz1QaUyg6QqviywLecsbCmtx6yLpgommxFUpn1bug5ggFHNcT2BFmCbX9TTVW4hTd2aeMN+N88vN2OG/V3TvlWCfHcYwmEEBKXhNVxOU0g+pVbWAXkJpgmn1IH96sObcqj5DE2LuW/dYltJ16E6gmPDFcnqfm6HAY= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=ibdrOiQN DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0979C4BB1C2B Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-cc1c8d4a959so3733524a12.3 for ; Tue, 08 Sep 2026 22:43:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1788932618; x=1789537418; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=QxnIeqWF1n+Zm3oAeLYyti7CbWvH3DTBCrtenT796Qo=; b=ibdrOiQNkBPkMUZliTpn5yXc8vrjKvq0B9FaIk8iUYzyJ7zSgMG4UNebm1jqkdvq6C DguXv9Ecf4GMVbFxqD4obLk64B+ontTj/CRc7PZ/7OeGH/dcUl8594VnaU5s/XUbYFN5 9z0dOt+6An8ZifMp5A/wXhSgGbePjs2/ocMjsYL9ezvDsz0HJWSStLS/uRLpXdgk3con 88P0en2BFKPfTJFB/UjeBSNqxQnmTKpvDE7xZ1A2K4x/riGFDRvOlCnKAS6Wtmj7xDsF 3agxjLduTSsJjKtRQRMFvwz+dLczTY29albtDdj5fFil2eb6iyf4EaZ6nydDQMlWSDIb TcCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788932618; x=1789537418; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=QxnIeqWF1n+Zm3oAeLYyti7CbWvH3DTBCrtenT796Qo=; b=UD8AG3pC3AmJV/H7hO9HpR2u6nl+UZDcuOv9W6XC0bZsFO0Qa5zFu/ENS+Hr/DlXVP qLQvroZm+U5hGf0m6tCj47u81PUroa0JU5705o2d0dAz6Gs9SzEe0XUeVFakO1kVIUqq ZwHbfwd0Nn2yPb1oGvwRKh03yZ9UDzY8+5dNOosHcjABCj/xePEwKlt+Ecy0gJz6qMdM lAzAKBqfyWJ2IrCSQ7TPeHwHkivvwOtIjrBW69Zg9t35I5272h8NFbeDyIFw3w+ZzmLc G1JSl1DJvmoqIga50dHjAQabMyHggOSbAnK+gNdzOZpQvAusMFqp3GsAqoGNs3HSeylD Y9fA== X-Gm-Message-State: AFuF++kXECq8KAU19WO1OIsBJqhj0dimhWDghu1QAGXsql0Y1ESlCiT2 BluYPLfaf3gq2qwI2Fa7XBVgLED6nRL9GTup5XMUbAATStIIWhTSsicI X-Gm-Gg: AYBFou3wDSMkjW1hTl5PqneSJ7wsydFRWlewS/L1YLBG4vkKtOE9gkcyRVo9S2y6hCU Kbr441hzUF+twgATVw+fO2cb0wa3Uz+vL6D52LnrtUtsvicxI7za0mTAGamF6ICDk+miZoyMY+Y v68400J8zJOOPCKDNBbGmIzBVZ9ksMey1H4sRaOkZoXlzpija85oRPloJQVDxH82JLDgRi9yf+S B15dqTDMCV03COP6i3wUliLlDfmHxZatBBp6ykp3Mf34c93OhpFyrJFrRIG873bVWeaSzUhIUNO +jsI0oWu5vNVMRoxbRXdVZ/QG8wZY2U4O2m3QZXzgXGYAzd262tWdvCgp9VqCq54F2NLSEzNX00 2xqxDhGNypZ/TgFvecIlA+XU4bX7v8MIk0mqcLXaNlA3puycNauuJwqFWRsrIDgm+Pu3aBVp9dK gIhbrWcoeR2cwJq8oPmU+UAGkR2Z1rM5+Rhyu8ho0s6Wlrl20FHCOqFBS2+byiHzAHl8qS8z3TZ rk8MCFRReyYdroMt0fDK7AnwTiO0iM7adpkyw== X-Received: by 2002:a17:90b:1c0a:b0:396:41d1:cbc7 with SMTP id 98e67ed59e1d1-39b2614e099mr47331025a91.3.1788932617614; Tue, 08 Sep 2026 22:43:37 -0700 (PDT) Received: from dhcp-9-123-9-154.bl1-in.ibm.com ([129.41.58.2]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3339af25062sm64453636eec.16.2026.09.08.22.43.35 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 08 Sep 2026 22:43:37 -0700 (PDT) From: Aditya Vidyadhar Kamath To: ulrich.weigand@de.ibm.com, simon.marchi@polymtl.ca, tom@tromey.com Cc: gdb-patches@sourceware.org, Aditya.Kamath1@ibm.com, sangamesh.swamy@in.ibm.com, Aditya Vidyadhar Kamath Subject: [PATCH v1][RFC] Speed up next/step while debugging multithreaded programs on AIX. Date: Wed, 9 Sep 2026 11:13:07 +0530 Message-ID: <20260909054306.73173-2-akamath996@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org From: Aditya Vidyadhar Kamath During go compiler debugging on AIX my collegue and I noticed that the next command was pretty slow compared to Linux - around 7 seconds per step when program has atleast 3 or more threads running. A simple walk through a function took around a minute. In compiler codes this is slow. TO measure this properly I wrote a benchmark pasted below. The program spawns 20 worker threads , then stops inside a function with 10 simple that GDB steps over one by one. The GDB batch script breaks at that function and issues 10 next commands and then quits. Timing the whole run gives a fair picture of how much overhead each `next` carries. Both the .c file and batch file are pasted below. Before this patch, on AIX 7.3 ppc64 with 20 background threads: real 1m10.05s user 0m17.59s sys 0m20.43s That is roughly 7 seconds per `next` step. After this patch: real 0m20.37s user 0m5.94s sys 0m6.53s About 2 seconds per step. 3.5x faster for the same workload. Now why was it slow? Every time the inferior stops -- even for a single-step trap from next - the AIX thread layer called pd_update(), which did two very expensive things unconditionally: 1. pthdb_session_update () This function walks through all of the pthread library's internal data structures inside the inferior process, reading them out via pdc_read_data() callbacks. With 20 threads that is over 300 memory reads from the inferior on every single stop, even when absolutely nothing about the threads has changed. 2. sync_threadlists() This follows the session update with a full rebuild of GDB's thread list: iterate every thread via pthdb_pthread(), call pthdb_pthread_tid() for each one, allocate two sorted arrays, diff them against GDB's internal list, and reconcile the differences. Again, all of this on every stop, even when the thread population has not changed at all since the last stop. On top of that, get_signaled_thread() was calling getthrds() asking for one kernel thread descriptor at a time, which costs one syscall per thread in the process. None of this work is wrong but it is needed when threads actually come and go. But during a next or step sequence through a function, the thread list does not change. Paying for a full rescan on every stop is something not needed I think. So what changes this patch does is: 1. Batch getthrds() calls get_signaled_thread () The original code passed 1 as the count argument to getthrds(), so it did one syscall per kernel thread to find the one that stopped. Changed to fetch 64 descriptors per call and scan the batch in a loop. This alone cuts the getthrds() cost from O(N) syscalls to O(N/64). 2. Track the thread count - aix_thread_variables, pd_activate Added a last_thread_count field to aix_thread_variables. It records how many threads libpthdebug reported the last time a full sync was done. Initialised to -1 in the constructor so the very first stop always does a full sync. Also reset to -1 in pd_activate() so that whenever a new debug session is opened the first stop is always fully synced regardless of the stop kind. 3. Skip the sync on clean step stops pd_update() now takes a bool step_stop parameter. When that is true and last_thread_count is not -1, it skips pthdb_session_update() and sync_threadlists() entirely. When a sync is needed (not a step stop, or first stop after activation), a new helper count_pthdb_threads() does a lightweight pass through pthdb_pthread() just to count threads. It only calls sync_threadlists() if that count differs from last_thread_count. This avoids the expensive pthdb_pthread_tid() calls and array allocations on stops where the thread count has not changed. 4. Detect step stops in wait() and pass the flag down wait() already has the stop kind and signal number before it calls pd_update(). A TARGET_WAITKIND_STOPPED with GDB_SIGNAL_TRAP is what the kernel reports for a completed single-step. wait() now computes step_stop from those two fields and passes it to pd_update(). int current_count = count_pthdb_threads (data); if (current_count != data->last_thread_count) This makes sure we never miss any genuinely coming new threads and are in sync. So these are the things I thought and did. Let me know what you think. ================== Benchmark program (bench_next.c): /* Spawns NUM_THREADS worker threads that spin, then steps through 10 assignments in do_steps() so GDB can time each next. */ #include #include #include #include #ifndef NUM_THREADS #define NUM_THREADS 20 #endif static volatile int keep_running = 1; static void * worker (void *arg) { while (keep_running) sched_yield (); return NULL; } void do_steps (void) { volatile int a = 1; volatile int b = 2; volatile int c = a + b; volatile int d = c * 2; volatile int e = d - a; volatile int f = e + c; volatile int g = f / 2; volatile int h = g + 1; volatile int i2 = h * h; volatile int j = i2 - b; (void)j; } int main (void) { pthread_t threads[NUM_THREADS]; int i; for (i = 0; i < NUM_THREADS; i++) pthread_create (&threads[i], NULL, worker, NULL); do_steps (); keep_running = 0; for (i = 0; i < NUM_THREADS; i++) pthread_join (threads[i], NULL); return 0; } ==================== Build done with gcc -O0 -g -gdwarf -maix64 -DNUM_THREADS=20 -o bench_next_bin bench_next.c -lpthread ===================== GDB batch script (bench_next.gdb): set pagination off set confirm off break do_steps run next next next next next next next next next next quit ======================== Then run, time gdb -batch -x bench_next.gdb ./bench_next_bin --- gdb/aix-thread.c | 117 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 94 insertions(+), 23 deletions(-) diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c index 5ac71d22237..5c43e1130e4 100644 --- a/gdb/aix-thread.c +++ b/gdb/aix-thread.c @@ -173,6 +173,11 @@ static pthdb_callbacks_t pd_callbacks = { /* Aix variable structure. */ struct aix_thread_variables { + aix_thread_variables () + : pd_able (0), pd_active (0), pd_session (0), pd_brk_addr (0), + arch64 (0), last_thread_count (-1) + {} + /* Whether the current application is debuggable by pthdb. */ int pd_able; @@ -192,6 +197,11 @@ struct aix_thread_variables /* Describes the number of thread exit events reported. */ std::unordered_set exited_threads; + + /* Last known libpthdebug thread count. Used to skip sync_threadlists() + when the thread population has not changed since the previous stop, + which is the common case during stepping. -1 means unknown/force sync. */ + int last_thread_count; }; /* Key to our per-inferior data. */ @@ -733,6 +743,11 @@ state2str (pthdb_state_t state) } } +/* Number of thread descriptors to fetch per getthrds() call. The original + code fetched one at a time, which costs one syscall per thread. Fetching + in batches reduces that to one syscall per GETTHRDS_BATCH threads. */ +#define GETTHRDS_BATCH 64 + /* Search through the list of all kernel threads for the thread that has stopped on a SIGTRAP signal, and return its TID. Return 0 if none found. */ @@ -740,20 +755,18 @@ state2str (pthdb_state_t state) static pthdb_tid_t get_signaled_thread (int pid) { - struct thrdsinfo64 thrinf; + struct thrdsinfo64 thrinf[GETTHRDS_BATCH]; tid_t ktid = 0; + int count, i; - while (1) + while ((count = getthrds (pid, thrinf, sizeof (thrinf[0]), + &ktid, GETTHRDS_BATCH)) > 0) { - if (getthrds (pid, &thrinf, - sizeof (thrinf), &ktid, 1) != 1) - break; - /* We also need to keep in mind Trap and interrupt or any signal that needs to be handled in pd_update (). */ - - if (thrinf.ti_cursig) - return thrinf.ti_tid; + for (i = 0; i < count; i++) + if (thrinf[i].ti_cursig) + return thrinf[i].ti_tid; } /* Didn't find any thread stopped on a SIGTRAP signal. */ @@ -861,13 +874,39 @@ sync_threadlists (pid_t pid) } } +/* Count the number of live pthreads visible to libpthdebug. + Used before sync_threadlists() to check whether the thread population + has changed since the last stop. */ + +static int +count_pthdb_threads (struct aix_thread_variables *data) +{ + pthdb_pthread_t pdtid; + int cmd, n = 0; + + for (cmd = PTHDB_LIST_FIRST;; cmd = PTHDB_LIST_NEXT) + { + int status = pthdb_pthread (data->pd_session, &pdtid, cmd); + if (status != PTHDB_SUCCESS || pdtid == PTHDB_INVALID_PTHREAD) + break; + n++; + } + return n; +} + /* Synchronize libpthdebug's state with the inferior and with GDB, generate a composite process/thread for the current thread, - Return the ptid of the event thread if one can be found, else - return a pid-only ptid with PID. */ + return the ptid of the event thread if one can be found, else + return a pid-only ptid with PID. + + STEP_STOP should be set when the stop is a single-step SIGTRAP. + In that case, if the thread list was already synced at a previous + stop, both pthdb_session_update() and sync_threadlists() are skipped. + Those two calls dominate the cost of every stop in a multithreaded + program, so skipping them during stepping gives a large speedup. */ static ptid_t -pd_update (pid_t pid) +pd_update (pid_t pid, bool step_stop = false) { int status; ptid_t ptid; @@ -880,15 +919,36 @@ pd_update (pid_t pid) if (!data->pd_active) return ptid_t (pid); - status = pthdb_session_update (data->pd_session); - if (status != PTHDB_SUCCESS) - return ptid_t (pid); - - /* Attempt to sync_threadlists () only when debugging object files - and not core files since list of threads never change for core - files. */ - if (target_has_execution ()) - sync_threadlists (pid); + /* Skip the session update and thread list sync when stepping. + pthdb_session_update() reads through all pthread data structures in + the inferior on every call, and sync_threadlists() follows it with + O(N) kernel calls per thread. During a next/step sequence the thread + list does not change, so both calls can be omitted as long as we have + done at least one full sync since activation (last_thread_count >= 0). + Any other stop kind (breakpoints, real signals) always does a full + sync because threads may have been created or destroyed. */ + bool need_sync = !step_stop || (data->last_thread_count < 0); + + if (need_sync) + { + status = pthdb_session_update (data->pd_session); + if (status != PTHDB_SUCCESS) + return ptid_t (pid); + + /* Attempt to sync_threadlists() only when debugging object files + and not core files since the list of threads never changes for + core files. Even for live inferiors, skip the sync when the + thread count matches the previous stop. */ + if (target_has_execution ()) + { + int current_count = count_pthdb_threads (data); + if (current_count != data->last_thread_count) + { + sync_threadlists (pid); + data->last_thread_count = current_count; + } + } + } /* Define "current thread" as one that just received a trap signal. */ @@ -921,7 +981,12 @@ pd_activate (pid_t pid) PTHDB_FLAG_REGS, &pd_callbacks, &data->pd_session); if (status == PTHDB_SUCCESS) - data->pd_active = 1; + { + data->pd_active = 1; + /* Reset last_thread_count so that the very first stop after + activation always does a full sync_threadlists() pass. */ + data->last_thread_count = -1; + } } /* AIX implementation of update_thread_list. */ @@ -1125,7 +1190,13 @@ aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status, pd_activate (ptid.pid ()); } - return pd_update (ptid.pid ()); + /* Tell pd_update() whether this is a single-step SIGTRAP so it can + skip the expensive pthdb_session_update() and sync_threadlists() + calls. Breakpoints, real signals and other stop kinds always get + the full sync. */ + bool step_stop = (status->kind () == TARGET_WAITKIND_STOPPED + && status->sig () == GDB_SIGNAL_TRAP); + return pd_update (ptid.pid (), step_stop); } /* Supply AIX altivec registers, both 64 and 32 bit. */ -- 2.51.2