public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH v2 7/8] Demangle minsyms in parallel
Date: Sat, 18 May 2019 21:00:00 -0000	[thread overview]
Message-ID: <20190518210010.27697-8-tom@tromey.com> (raw)
In-Reply-To: <20190518210010.27697-1-tom@tromey.com>

This patch introduces a simple parallel for_each and changes the
minimal symbol reader to use it when computing the demangled name for
a minimal symbol.  This yields a speedup when reading minimal symbols.

gdb/ChangeLog
2019-05-18  Tom Tromey  <tom@tromey.com>

	* minsyms.c (minimal_symbol_reader::install): Use
	parallel_for_each.
	* common/parallel-for.h: New file.
	* common/parallel-for.c: New file.
	* Makefile.in (HFILES_NO_SRCDIR): Add common/parallel-for.h.
	(COMMON_SFILES): Add common/parallel-for.c.
---
 gdb/ChangeLog             |  9 +++++
 gdb/Makefile.in           |  2 +
 gdb/common/parallel-for.c | 27 +++++++++++++
 gdb/common/parallel-for.h | 79 +++++++++++++++++++++++++++++++++++++++
 gdb/minsyms.c             | 23 +++++++-----
 5 files changed, 130 insertions(+), 10 deletions(-)
 create mode 100644 gdb/common/parallel-for.c
 create mode 100644 gdb/common/parallel-for.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 2dd69f3f0ba..15c7a6e2536 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -971,6 +971,7 @@ COMMON_SFILES = \
 	common/gdb_vecs.c \
 	common/netstuff.c \
 	common/new-op.c \
+	common/parallel-for.c \
 	common/pathstuff.c \
 	common/print-utils.c \
 	common/ptid.c \
@@ -1471,6 +1472,7 @@ HFILES_NO_SRCDIR = \
 	common/common-inferior.h \
 	common/netstuff.h \
 	common/host-defs.h \
+	common/parallel-for.h \
 	common/pathstuff.h \
 	common/print-utils.h \
 	common/ptid.h \
diff --git a/gdb/common/parallel-for.c b/gdb/common/parallel-for.c
new file mode 100644
index 00000000000..acec77c9fa4
--- /dev/null
+++ b/gdb/common/parallel-for.c
@@ -0,0 +1,27 @@
+/* Parallel for loops
+
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "common/common-defs.h"
+#include "common/parallel-for.h"
+
+namespace gdb
+{
+/* See parallel-for.h.  */
+int enable_threads = 1;
+}
diff --git a/gdb/common/parallel-for.h b/gdb/common/parallel-for.h
new file mode 100644
index 00000000000..db90137127d
--- /dev/null
+++ b/gdb/common/parallel-for.h
@@ -0,0 +1,79 @@
+/* Parallel for loops
+
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef COMMON_PARALLEL_FOR_H
+#define COMMON_PARALLEL_FOR_H
+
+#include <algorithm>
+#if CXX_STD_THREAD
+#include <thread>
+#endif
+
+namespace gdb
+{
+
+/* True if threading should be enabled.  */
+
+extern int enable_threads;
+
+/* A very simple "parallel for".  This iterates over the elements
+   given by the range of iterators, which must be random access
+   iterators.  For each element, it calls the callback function.  The
+   work may or may not be done by separate threads.  */
+
+template<class RandomIt, class UnaryFunction>
+void parallel_for_each (RandomIt first, RandomIt last, UnaryFunction f)
+{
+#if CXX_STD_THREAD
+  unsigned n_threads = std::thread::hardware_concurrency ();
+  /* So we can use a local array below.  */
+  const unsigned max_threads = 16;
+  if (n_threads > max_threads)
+    n_threads = max_threads;
+
+  if (!enable_threads || n_threads == 0 || last - first < 2 * n_threads)
+#endif /* CXX_STD_THREAD */
+    {
+      /* Don't bother.  */
+      std::for_each (first, last, f);
+      return;
+    }
+
+#if CXX_STD_THREAD
+  auto body = [&] (RandomIt start)
+    {
+      for (; start < last; start += n_threads)
+	f (*start);
+    };
+
+  std::thread threads[max_threads];
+  for (unsigned i = 0; i < n_threads; ++i)
+    {
+      threads[i] = std::thread (body, first);
+      ++first;
+    }
+
+  for (unsigned i = 0; i < n_threads; ++i)
+    threads[i].join ();
+#endif /* CXX_STD_THREAD */
+}
+
+}
+
+#endif /* COMMON_PARALLEL_FOR_H */
diff --git a/gdb/minsyms.c b/gdb/minsyms.c
index 488201a1886..4c0ee11c47e 100644
--- a/gdb/minsyms.c
+++ b/gdb/minsyms.c
@@ -53,6 +53,7 @@
 #include "common/symbol.h"
 #include <algorithm>
 #include "safe-ctype.h"
+#include "common/parallel-for.h"
 
 /* See minsyms.h.  */
 
@@ -1372,16 +1373,18 @@ minimal_symbol_reader::install ()
       m_objfile->per_bfd->msymbols = std::move (msym_holder);
 
       msymbols = m_objfile->per_bfd->msymbols.get ();
-      for (int i = 0; i < mcount; ++i)
-	{
-	  if (!msymbols[i].name_set)
-	    {
-	      symbol_set_names (&msymbols[i], msymbols[i].name,
-				strlen (msymbols[i].name), 0,
-				m_objfile->per_bfd);
-	      msymbols[i].name_set = 1;
-	    }
-	}
+      gdb::parallel_for_each
+	(&msymbols[0], &msymbols[mcount],
+	 [&] (minimal_symbol &msym)
+	 {
+	   if (!msym.name_set)
+	     {
+	       symbol_set_names (&msym, msym.name,
+				 strlen (msym.name), 0,
+				 m_objfile->per_bfd);
+	       msym.name_set = 1;
+	     }
+	 });
 
       build_minimal_symbol_hash_tables (m_objfile);
     }
-- 
2.17.2

  parent reply	other threads:[~2019-05-18 21:00 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-18 21:00 [PATCH v2 0/8] Demangle minimal symbol names in worker threads Tom Tromey
2019-05-18 21:00 ` [PATCH v2 8/8] Add maint set/show enable-threads Tom Tromey
2019-05-22  5:01   ` Eli Zaretskii
2019-05-26 20:46     ` Tom Tromey
2019-05-27  2:32       ` Eli Zaretskii
2019-05-18 21:00 ` [PATCH v2 4/8] Lock the demangled hash table Tom Tromey
2019-05-18 21:00 ` [PATCH v2 6/8] Introduce thread-safe way to handle SIGSEGV Tom Tromey
2019-05-18 21:00 ` [PATCH v2 3/8] Add configure check for std::thread Tom Tromey
2019-05-18 21:00 ` [PATCH v2 1/8] Defer minimal symbol name-setting Tom Tromey
2019-05-18 21:00 ` Tom Tromey [this message]
2019-05-18 21:00 ` [PATCH v2 2/8] Remove static buffer from ada_decode Tom Tromey
2019-05-18 21:00 ` [PATCH v2 5/8] Introduce run_on_main_thread Tom Tromey
2019-05-19 13:59 ` [PATCH v2 0/8] Demangle minimal symbol names in worker threads Philippe Waroquiers
2019-05-19 18:55   ` Tom Tromey
2019-05-21  0:35     ` Philippe Waroquiers
2019-05-21  7:35     ` Andrew Burgess
2019-05-21 15:45       ` Tom Tromey
2019-05-21 16:21         ` Andrew Burgess
2019-05-31  2:48     ` Tom Tromey
2019-05-31 17:13       ` Philippe Waroquiers
2019-09-29  0:35         ` [PATCH] Don't use the mutex for each symbol_set_names call Christian Biesinger via gdb-patches
2019-09-30 14:18           ` Tom Tromey
2019-09-30 16:55             ` Christian Biesinger via gdb-patches
2019-10-02 17:18               ` Tom Tromey
2019-10-02 18:20                 ` Christian Biesinger via gdb-patches
2019-10-02 22:02                   ` Christian Biesinger via gdb-patches
2019-10-03 18:15                     ` [PATCH v2 2/2] Precompute hash value for symbol_set_names Christian Biesinger via gdb-patches
2019-10-03 18:15                     ` [PATCH v2 1/2] Don't use the mutex for each symbol_set_names call Christian Biesinger via gdb-patches
2019-09-30 21:45             ` [PATCH] " Christian Biesinger via gdb-patches
2019-10-01 17:02               ` Tom Tromey

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=20190518210010.27697-8-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@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).