From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id FZTQGOHpompkWz0AWB0awg (envelope-from ) for ; Thu, 10 Sep 2026 13:33:21 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=VBBbxVj6; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 3EBF51E033; Thu, 10 Sep 2026 13:33:21 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.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 DF1641E033 for ; Thu, 10 Sep 2026 13:33:15 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 62BA348FDED2 for ; Thu, 10 Sep 2026 17:33:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 62BA348FDED2 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=VBBbxVj6 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id AFC1148FE552 for ; Thu, 10 Sep 2026 17:32:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AFC1148FE552 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AFC1148FE552 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789061561; cv=none; b=p4gW0VsL50abI1m0AGeeVXWDE+pQ7YR/SVTAuSkWMMWl55nZra+46Zeugb70ffodPMC3//7gDGeT/7V0pTt9My0Cu4grm5KiNtoIPJiSTnYKj2WuKVSVsmtbdW61k6EEtGjxoToD+zyGiB/cm97q2e8DgO14g9cCZ9CT24vTosU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789061561; c=relaxed/simple; bh=ZfetJZamB4JkZ2WWehH893YZoY8NtNUPFIeZT6zM8a8=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=gBdcER2bc1HRts4mN0ZVqkpMB6AdRHD6RiyFmlB9whmr1fSRHIo07p/lL9QjWZ6cy/2x6SGY7YosiY5dgBEheWouP/me0HfWCu3+p+OnKSklOJioGwI0ZYctY/jOSph7dZJvpNfCG3Of3ZkqMD+udjxa3O2qR58VOgj0Xev4pTw= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=VBBbxVj6 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AFC1148FE552 Received: from pps.filterd (m0360083.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 68AFDRJR3266751; Thu, 10 Sep 2026 17:32:37 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to; s=pp1; bh=6tVDdGAcfXxOe1V+C9mpvhRGXvBZfN G3A6o+Jx+KfaI=; b=VBBbxVj6PYHkzFSSLHAsPVpXf+VFtJm9UP2VcoEwznRk3d qpPov4a7I67LdMlzokILnKVmkefw+Vb0D1EZVvPgm5PSer9ahki06IpIxB5AETmV Nwb45KO2NUTfYMaYAcFx4Xjj3Een9qizBvLlCVwiXlrfmGA6fd8Yw7O1qSJK2cP7 8CjK9Ca5I22r9KNzevhLrIfdoiNbY79z656OThWBw2JcVI755caL/s2ubxwhTw6M VyRF9shEYDdmdpIq0pA8cEUcplwJJXulTG0fLX/T2owQAshSgbTQDIha9U0kdUGU cOC0GtkepHj3zYWwrV3dc2gODQ76GqMMDWJmPzKQ== Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4gkd8qpbnk-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 10 Sep 2026 17:32:36 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.18.1.11/8.18.1.11) with ESMTP id 68AF82c8202079; Thu, 10 Sep 2026 17:32:36 GMT Received: from smtprelay05.wdc07v.mail.ibm.com ([172.16.1.72]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 4gkvq2shf1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 10 Sep 2026 17:32:36 +0000 (GMT) Received: from smtpav06.dal12v.mail.ibm.com (smtpav06.dal12v.mail.ibm.com [10.241.53.105]) by smtprelay05.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 68AHWWXx25756202 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Sep 2026 17:32:33 GMT Received: from smtpav06.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B01565805D; Thu, 10 Sep 2026 17:32:32 +0000 (GMT) Received: from smtpav06.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5D86C58043; Thu, 10 Sep 2026 17:32:26 +0000 (GMT) Received: from [9.111.203.125] (unknown [9.111.203.125]) by smtpav06.dal12v.mail.ibm.com (Postfix) with ESMTP; Thu, 10 Sep 2026 17:32:25 +0000 (GMT) Content-Type: multipart/alternative; boundary="------------AtLzs59smnqPUhgsC9960wh4" Message-ID: Date: Thu, 10 Sep 2026 23:02:24 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] Speed up next/step while debugging multithreaded programs on AIX. To: Aditya Vidyadhar Kamath , 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 References: <20260910101643.85955-2-akamath996@gmail.com> From: Abhay Kandpal Content-Language: en-GB In-Reply-To: <20260910101643.85955-2-akamath996@gmail.com> X-TM-AS-GCONF: 00 X-Proofpoint-Reinject: loops=2 maxloops=12 X-Authority-Analysis: v=2.4 cv=SpUFe/O0 c=1 sm=1 tr=0 ts=6aa2e9b5 cx=c_pps a=aDMHemPKRhS1OARIsFnwRA==:117 a=aDMHemPKRhS1OARIsFnwRA==:17 a=VdqzKS8jKosA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=iQ6ETzBq9ecOQQE5vZCe:22 a=r77TgQKjGQsHNAKrUKIA:9 a=VnNF1IyMAAAA:8 a=p2W5CfPGfxX7itRxq2sA:9 a=QEXdDO2ut3YA:10 a=pGLkceISAAAA:8 a=JD7jMR5HjBWmKbhrg5IA:9 a=UnaaTJ0YCMigRw2L:21 a=_W_S_7VecoQA:10 a=lqcHg5cX4UMA:10 X-Proofpoint-ORIG-GUID: O_t2BhS7mDK-PqdtX-L0BhZ_eGqNQi_3 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwOTEwMDIxNSBTYWx0ZWRfX+Mp4ZfZHk+9y mpoWaiTTSREEepcHTjGlJN13Zi/SAohD5zBXxaPqoYl+MByURZAVIpxL3kiLrc3uE2LyUvuCy4x +pVJyoqH92qrPokhnWTJvn9v/CKzeuGtQc8Kjng+3NyFQzcWFwyFkqNYGAVflAwT2JBnnSzwX3j TrjsOCwQmjs6RQXdKCOzdbub3PPXwO8n09jVTDbSDhBEbz+bQG4713Fmub8PC6kFyZd8xMhzt4Q 0yaVGBqbu6EC08vIo+FRsU9ibrIS+gJVHVPAuiS60u3ybw0uD1DH1erE9A8nIfaQ7968jyPLfah 24xySkFvgOIoD23fkOaFKb9go4jAuDrYFZp8XI2O0UwUi4uQgdGAs7+cQsdR00bRBjkbENp5ggk JDITIybelUaPiSWhHzVBTARb4xQUCoYEbJmgvkyrLkyZ8NGhbttWUhlVtVaSxcaMNiODp4lAgDH aBlobzp/pj+6P+C4HXA== X-Proofpoint-Spam-Info: AW1haW4tMjYwOTEwMDIxNSBTYWx0ZWRfX+jLwFHNmVDQb 5g34XGpWbw/wih9AySil+pA1qIDJqW4aPS/nK968lp6UCYdDDfQGVLlPSZW/Z9Y491Xm6vibnDl 7dUm9j4lL9W3PDPQlabIOlVsq+G+/mQ= X-Proofpoint-GUID: FYyRPgImHqtL2BFUlcZdT5lJXyM_diSx X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-09-10_05,2026-09-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 adultscore=0 lowpriorityscore=0 suspectscore=0 spamscore=0 clxscore=1015 phishscore=0 malwarescore=0 impostorscore=0 bulkscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2609040000 definitions=main-2609100215 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 This is a multi-part message in MIME format. --------------AtLzs59smnqPUhgsC9960wh4 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Hi Aditya, On 10/09/26 15:46, Aditya Vidyadhar Kamath wrote: > 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 I changed in this patch 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. > > ================== > 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 | 153 ++++++++++++++++++++++++++++++++++++++++------- > 1 file changed, 130 insertions(+), 23 deletions(-) > > diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c > index 5ac71d22237..3370736810a 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), last_resume_step (0) > + {} > + > /* Whether the current application is debuggable by pthdb. */ > int pd_able; > > @@ -192,6 +197,19 @@ 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; > + > + /* Set to non-zero by resume() when the resume was a software > + single-step, i.e. single-step breakpoints were inserted for the > + current thread before the inferior was set running. Cleared to > + zero for any other kind of resume. Used by wait() to distinguish > + a SIGTRAP from a completed single-step from one caused by a > + breakpoint, since both look identical at the signal level. */ > + int last_resume_step; > }; > > /* Key to our per-inferior data. */ > @@ -733,6 +751,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 +763,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 > +882,43 @@ 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 only when the last resume() was a software > + single-step and the resulting stop is a 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. When > + STEP_STOP is false - meaning the resume was a free continue, or a > + temporary breakpoint was used, or the signal is not SIGTRAP - the > + full sync always runs so that newly created threads are not missed. */ > > static ptid_t > -pd_update (pid_t pid) > +pd_update (pid_t pid, bool step_stop = false) > { > int status; > ptid_t ptid; > @@ -880,15 +931,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 +993,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. */ > @@ -1056,6 +1133,21 @@ aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig) > > data = get_thread_data_helper_for_ptid (ptid); > > + /* Remember whether this resume is a software single-step so that > + wait() can correctly classify the matching stop. On AIX the > + architecture uses software single-stepping (rs6000_software_single_step), > + so the step argument passed down to target_resume is always 0. > + The only reliable indicator that GDB is single-stepping is whether > + single-step breakpoints were inserted for the current thread just > + before this resume. A breakpoint SIGTRAP from a user breakpoint > + and a SIGTRAP from a completed software single-step are otherwise > + indistinguishable at this layer. */ > + if (data != nullptr) > + { > + struct thread_info *tp = inferior_thread (); > + data->last_resume_step = thread_has_single_step_breakpoints_set (tp); > + } > + inferior_thread () returns the currently selected thread, which is not necessarily the thread named by the ptid argument. Since thread_has_single_step_breakpoints_set () is a per-thread query, asking it about the wrong thread would give the wrong answer for last_resume_step. The else branch a few lines below already resolves the thread from the argument: thread = current_inferior ()->find_thread (ptid); Would that be more appropriate here? I am also unsure what the flag should be when ptid.tid () == 0 and the resume covers more than one thread. Separately, would it be worth adding testsuite coverage for this? The benchmark program creates all 20 threads before do_steps (), so the thread population never changes after the first stop and it cannot exercise the case the v1 logic got wrong. Something that creates threads between two breakpoint stops and checks "info threads" at the second one would cover it, and a "next" over a call that spawns a thread would cover Simon's point. I don't have an AIX machine to run it on, so I can't offer a patch for it myself. Regards Abhay > if (ptid.tid () == 0) > { > scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); > @@ -1125,7 +1217,22 @@ aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status, > pd_activate (ptid.pid ()); > } > > - return pd_update (ptid.pid ()); > + /* Decide whether to skip the expensive pthdb_session_update() and > + sync_threadlists() calls in pd_update(). We can only skip them > + when we know for certain this stop came from a software single-step. > + On AIX, single-stepping is implemented by inserting breakpoints at > + the next instruction(s) and resuming with step=0, so the step > + argument to target_resume is always 0 and cannot be used here. > + Instead, resume() records whether single-step breakpoints were set > + at the time of the resume in last_resume_step. If that flag is set > + and the stop is a SIGTRAP, this is a completed single-step and the > + thread list has not changed. If last_resume_step is 0, GDB either > + continued freely or planted a different kind of breakpoint, so a > + new thread could exist and the sync must run. */ > + bool step_stop = (data->last_resume_step > + && 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. */ --------------AtLzs59smnqPUhgsC9960wh4 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit
Hi Aditya,
On 10/09/26 15:46, Aditya Vidyadhar Kamath wrote:
From: Aditya Vidyadhar Kamath <aditya.kamath1@ibm.com>

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 I changed in this patch 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.

==================
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 <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <unistd.h>

    #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 | 153 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 130 insertions(+), 23 deletions(-)

diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c
index 5ac71d22237..3370736810a 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), last_resume_step (0)
+  {}
+
   /* Whether the current application is debuggable by pthdb.  */
   int pd_able;
 
@@ -192,6 +197,19 @@ struct aix_thread_variables
 
   /* Describes the number of thread exit events reported.  */
   std::unordered_set<pthdb_pthread_t> 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;
+
+  /* Set to non-zero by resume() when the resume was a software
+     single-step, i.e. single-step breakpoints were inserted for the
+     current thread before the inferior was set running.  Cleared to
+     zero for any other kind of resume.  Used by wait() to distinguish
+     a SIGTRAP from a completed single-step from one caused by a
+     breakpoint, since both look identical at the signal level.  */
+  int last_resume_step;
 };
 
 /* Key to our per-inferior data.  */
@@ -733,6 +751,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 +763,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 +882,43 @@ 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 <pid> 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 only when the last resume() was a software
+   single-step and the resulting stop is a 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.  When
+   STEP_STOP is false - meaning the resume was a free continue, or a
+   temporary breakpoint was used, or the signal is not SIGTRAP - the
+   full sync always runs so that newly created threads are not missed.  */
 
 static ptid_t
-pd_update (pid_t pid)
+pd_update (pid_t pid, bool step_stop = false)
 {
   int status;
   ptid_t ptid;
@@ -880,15 +931,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 +993,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.  */
@@ -1056,6 +1133,21 @@ aix_thread_target::resume (ptid_t ptid, int step, enum gdb_signal sig)
 
   data = get_thread_data_helper_for_ptid (ptid);
 
+  /* Remember whether this resume is a software single-step so that
+     wait() can correctly classify the matching stop.  On AIX the
+     architecture uses software single-stepping (rs6000_software_single_step),
+     so the step argument passed down to target_resume is always 0.
+     The only reliable indicator that GDB is single-stepping is whether
+     single-step breakpoints were inserted for the current thread just
+     before this resume.  A breakpoint SIGTRAP from a user breakpoint
+     and a SIGTRAP from a completed software single-step are otherwise
+     indistinguishable at this layer.  */
+  if (data != nullptr)
+    {
+      struct thread_info *tp = inferior_thread ();
+      data->last_resume_step = thread_has_single_step_breakpoints_set (tp);
+    }
+
inferior_thread () returns the currently selected thread, which is not
necessarily the thread named by the ptid argument.  Since
thread_has_single_step_breakpoints_set () is a per-thread query, asking
it about the wrong thread would give the wrong answer for
last_resume_step.

The else branch a few lines below already resolves the thread from the
argument:

  thread = current_inferior ()->find_thread (ptid);

Would that be more appropriate here?  I am also unsure what the flag
should be when ptid.tid () == 0 and the resume covers more than one
thread.

Separately, would it be worth adding testsuite coverage for this?  The
benchmark program creates all 20 threads before do_steps (), so the
thread population never changes after the first stop and it cannot
exercise the case the v1 logic got wrong.

Something that creates threads between two breakpoint stops and checks
"info threads" at the second one would cover it, and a "next" over a
call that spawns a thread would cover Simon's point.  I don't have an
AIX machine to run it on, so I can't offer a patch for it myself.

Regards
Abhay
   if (ptid.tid () == 0)
     {
       scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
@@ -1125,7 +1217,22 @@ aix_thread_target::wait (ptid_t ptid, struct target_waitstatus *status,
 	pd_activate (ptid.pid ());
     }
 
-  return pd_update (ptid.pid ());
+  /* Decide whether to skip the expensive pthdb_session_update() and
+     sync_threadlists() calls in pd_update().  We can only skip them
+     when we know for certain this stop came from a software single-step.
+     On AIX, single-stepping is implemented by inserting breakpoints at
+     the next instruction(s) and resuming with step=0, so the step
+     argument to target_resume is always 0 and cannot be used here.
+     Instead, resume() records whether single-step breakpoints were set
+     at the time of the resume in last_resume_step.  If that flag is set
+     and the stop is a SIGTRAP, this is a completed single-step and the
+     thread list has not changed.  If last_resume_step is 0, GDB either
+     continued freely or planted a different kind of breakpoint, so a
+     new thread could exist and the sync must run.  */
+  bool step_stop = (data->last_resume_step
+		    && 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.  */
--------------AtLzs59smnqPUhgsC9960wh4--