public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom de Vries <vries@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] [gdbsupport] Add parallel_for_each_debug
Date: Mon, 18 Jul 2022 03:34:04 +0000 (GMT)	[thread overview]
Message-ID: <20220718033404.C87F53858CDA@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=53944a3bf51cdff9ad30a0c3740b8124213fdab9

commit 53944a3bf51cdff9ad30a0c3740b8124213fdab9
Author: Tom de Vries <tdevries@suse.de>
Date:   Mon Jul 18 05:34:01 2022 +0200

    [gdbsupport] Add parallel_for_each_debug
    
    Add a parallel_for_each_debug variable, set to false by default.
    
    With an a.out compiled from hello world, we get with
    parallel_for_each_debug == true:
    ...
    $ gdb -q -batch a.out -ex start
      ...
    Parallel for: n_elements: 7271
    Parallel for: minimum elements per thread: 10
    Parallel for: elts_per_thread: 1817
    Parallel for: elements on worker thread 0       : 1817
    Parallel for: elements on worker thread 1       : 1817
    Parallel for: elements on worker thread 2       : 1817
    Parallel for: elements on worker thread 3       : 0
    Parallel for: elements on main thread           : 1820
    
    Temporary breakpoint 1, main () at /home/vries/hello.c:6
    6         printf ("hello\n");
    ...
    
    Tested on x86_64-linux.

Diff:
---
 gdbsupport/parallel-for.h | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h
index a614fc35766..cfe8a6e4f09 100644
--- a/gdbsupport/parallel-for.h
+++ b/gdbsupport/parallel-for.h
@@ -139,7 +139,12 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last,
   using result_type
     = typename std::result_of<RangeFunction (RandomIt, RandomIt)>::type;
 
-  size_t n_threads = thread_pool::g_thread_pool->thread_count ();
+  /* If enabled, print debug info about how the work is distributed across
+     the threads.  */
+  const int parallel_for_each_debug = false;
+
+  size_t n_worker_threads = thread_pool::g_thread_pool->thread_count ();
+  size_t n_threads = n_worker_threads;
   size_t n_elements = last - first;
   size_t elts_per_thread = 0;
   if (n_threads > 1)
@@ -155,9 +160,19 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last,
   size_t count = n_threads == 0 ? 0 : n_threads - 1;
   gdb::detail::par_for_accumulator<result_type> results (count);
 
+  if (parallel_for_each_debug)
+    {
+      debug_printf (_("Parallel for: n_elements: %zu\n"), n_elements);
+      debug_printf (_("Parallel for: minimum elements per thread: %u\n"), n);
+      debug_printf (_("Parallel for: elts_per_thread: %zu\n"), elts_per_thread);
+    }
+
   for (int i = 0; i < count; ++i)
     {
       RandomIt end = first + elts_per_thread;
+      if (parallel_for_each_debug)
+	debug_printf (_("Parallel for: elements on worker thread %i\t: %zu\n"),
+		      i, (size_t)(end - first));
       results.post (i, [=] ()
         {
 	  return callback (first, end);
@@ -165,7 +180,14 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last,
       first = end;
     }
 
+  for (int i = count; i < n_worker_threads; ++i)
+    if (parallel_for_each_debug)
+      debug_printf (_("Parallel for: elements on worker thread %i\t: 0\n"), i);
+
   /* Process all the remaining elements in the main thread.  */
+  if (parallel_for_each_debug)
+    debug_printf (_("Parallel for: elements on main thread\t\t: %zu\n"),
+		  (size_t)(last - first));
   return results.finish ([=] ()
     {
       return callback (first, last);


                 reply	other threads:[~2022-07-18  3:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220718033404.C87F53858CDA@sourceware.org \
    --to=vries@sourceware.org \
    --cc=gdb-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).