Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: Tom Tromey <tom@tromey.com>
Cc: Pedro Alves <pedro@palves.net>,
	Tom de Vries via Gdb-patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH][gdbsupport] Use task size in parallel_for_each
Date: Fri, 22 Jul 2022 15:21:44 -0600	[thread overview]
Message-ID: <87zgh0sv13.fsf@tromey.com> (raw)
In-Reply-To: <87leslj786.fsf@tromey.com> (Tom Tromey's message of "Fri, 22 Jul 2022 13:08:25 -0600")

Tom> Then, rather than using parallel_for, the DWARF reader could send N jobs
Tom> to the thread pool, and each job would simply take the next available CU
Tom> by incrementing an atomic counter.  When the counter reached the number
Tom> of CUs, a job would stop.

Here's a patch.  I didn't test it much, though according to "maint time 1",
it is ~10% faster on gdb itself.  I pushed it as "t/work-stealing" on my
github as well, in case you want to try it out.

Tom

commit 0d530b3a072fd7acd9fc89c755ab4ded2719a17d
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Jul 22 15:16:22 2022 -0600

    patch

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 42230607fe0..7768d02b4f3 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -7074,28 +7074,44 @@ dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
        prompt, which looks weird.  */
     using result_type = std::pair<std::unique_ptr<cooked_index>,
 				  std::vector<gdb_exception>>;
-    std::vector<result_type> results
-      = gdb::parallel_for_each (1, per_bfd->all_comp_units.begin (),
-				per_bfd->all_comp_units.end (),
-				[=] (iter_type iter, iter_type end)
+    const size_t n_units = per_bfd->all_comp_units.size ();
+    std::vector<result_type> results (n_units);
+
+    const size_t n_threads
+      = std::min (gdb::thread_pool::g_thread_pool->thread_count (), n_units);
+    gdb::future<void> futures[n_threads];
+
+    std::atomic<size_t> next_cu = n_threads;
+    for (size_t i = 0; i < n_threads; ++i)
       {
-	std::vector<gdb_exception> errors;
-	cooked_index_storage thread_storage;
-	for (; iter != end; ++iter)
+	futures[i] = gdb::thread_pool::g_thread_pool->post_task ([&, i] ()
 	  {
-	    dwarf2_per_cu_data *per_cu = iter->get ();
-	    try
-	      {
-		process_psymtab_comp_unit (per_cu, per_objfile,
-					   &thread_storage);
-	      }
-	    catch (gdb_exception &except)
+	    std::vector<gdb_exception> &errors = results[i].second;
+	    cooked_index_storage thread_storage;
+
+	    size_t this_cu = i;
+	    while (this_cu < n_units)
 	      {
-		errors.push_back (std::move (except));
+		dwarf2_per_cu_data *per_cu = per_bfd->get_cu (this_cu);
+		try
+		  {
+		    process_psymtab_comp_unit (per_cu, per_objfile,
+					       &thread_storage);
+		  }
+		catch (gdb_exception &except)
+		  {
+		    errors.push_back (std::move (except));
+		  }
+
+		this_cu = next_cu++;
 	      }
-	  }
-	return result_type (thread_storage.release (), std::move (errors));
-      });
+
+	    results[i].first = thread_storage.release ();
+	  });
+      }
+
+    for (size_t i = 0; i < n_threads; ++i)
+      futures[i].get ();
 
     /* Only show a given exception a single time.  */
     std::unordered_set<gdb_exception> seen_exceptions;

  parent reply	other threads:[~2022-07-22 21:22 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-18 19:42 Tom de Vries via Gdb-patches
2022-07-21 17:35 ` Pedro Alves
2022-07-21 20:23   ` Tom de Vries via Gdb-patches
2022-07-22  0:03     ` Pedro Alves
2022-07-22 11:07       ` Pedro Alves
2022-07-22 17:07       ` Tom de Vries via Gdb-patches
2022-07-22 19:08     ` Tom Tromey
2022-07-22 19:38       ` Simon Marchi via Gdb-patches
2022-07-23  3:17         ` Tom Tromey
2022-07-22 21:21       ` Tom Tromey [this message]
2022-07-23  6:51         ` Tom de Vries via Gdb-patches
2022-07-31  8:38           ` Tom de Vries via Gdb-patches
2022-07-23  5:55       ` Tom de Vries via Gdb-patches

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=87zgh0sv13.fsf@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    --cc=pedro@palves.net \
    /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