Blame SOURCES/gdb-rhbz1103894-slow-gstack-performance.patch

01917d
<https://sourceware.org/ml/gdb-patches/2014-10/msg00031.html>
01917d
01917d
Date: Thu, 2 Oct 2014 17:56:53 +0200
01917d
From: Jan Kratochvil <jan dot kratochvil at redhat dot com>
01917d
To: Doug Evans <dje at google dot com>
01917d
Cc: gdb-patches at sourceware dot org
01917d
Subject: [patchv2] Fix 100x slowdown regression on DWZ files
01917d
Message-ID: <20141002155653.GA9001@host2.jankratochvil.net>
01917d
01917d
01917d
--cNdxnHkX5QqsyA0e
01917d
Content-Type: text/plain; charset=us-ascii
01917d
Content-Disposition: inline
01917d
01917d
On Thu, 02 Oct 2014 01:51:38 +0200, Doug Evans wrote:
01917d
> I tested this patch with --target_board=dwarf4-gdb-index
01917d
> and got a failure in m-static.exp:
01917d
01917d
That is particularly with -fdebug-types-section.
01917d
01917d
01917d
> Type units read the line table in a separate path,
01917d
01917d
OK, therefore I dropped that separate struct dwarf2_lineinfo
01917d
and reused struct line_header instead.
01917d
01917d
01917d
> OTOH, I do want to avoid any confusion that this patch may inadvertently
01917d
> introduce. For example, IIUC with your patch as is,
01917d
> if we read a partial_unit first, before a compile_unit
01917d
> that has the same stmt_list value, we'll do more processing in
01917d
> dwarf_decode_lines than we really need to since we only need a file
01917d
> number to symtab mapping. And if we later read in a compile_unit
01917d
> with the same stmt_value we'll call dwarf_decode_lines again,
01917d
> and this time we need the pc/line mapping it computes.
01917d
> Whereas if we process these in the opposite order we'll only call
01917d
> dwarf_decode_lines once. I'm sure this will be confusing at first
01917d
> to some later developer going through this code.
01917d
> [I could be missing something of course, and I'm happy for any corrections.]
01917d
01917d
Implemented (omitting some story why I did not include it before).
01917d
01917d
01917d
> The code that processes stmt_list for type_units is in setup_type_unit_groups.
01917d
> Note that this code goes to the trouble of re-initializing the buildsym
01917d
> machinery (see the calls to restart_symtab in dwarf2read.c) when we process
01917d
> the second and subsequent type units that share a stmt_list value.
01917d
> This is something that used to be done before your patch and will no
01917d
> longer be done with your patch (since if we get a cache hit we exit).
01917d
> It may be that the type_unit support is doing this unnecessarily,
01917d
> which would be great because we can then simplify it.
01917d
01917d
I hope this patch should no longer break -fdebug-types-section.
01917d
If it additionally enables some future optimization for -fdebug-types-section
01917d
the better.
01917d
01917d
01917d
>  > +  /* Offset of line number information in .debug_line section.  */
01917d
>  > +  sect_offset offset;
01917d
>  > +  unsigned offset_in_dwz : 1;
01917d
> 
01917d
> IWBN to document why offset_in_dwz is here.
01917d
> It's not obvious why it's needed.
01917d
+
01917d
On Thu, 02 Oct 2014 01:57:03 +0200, Doug Evans wrote:
01917d
> Ah, I guess the offset_in_dwz flag will ensure dwarf_decode_lines gets called
01917d
> twice regardless of order.  But is that the only reason for the flag?
01917d
01917d
I have added there now:
01917d
+  /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile.  */
01917d
01917d
If one removes it regressions really happen.  What happens is that this
01917d
line_header_hash (former lineinfo_hash) is in struct dwarf2_per_objfile which
01917d
is common for both objfile and its objfile.dwz (that one is normally in
01917d
/usr/lib/debug/.dwz/ - common for multiple objfiles).  And there are two
01917d
different DIEs at offset 0xb - one in objfile and one in objfile.dwz - which
01917d
would match single line_header if offset_in_dwz was not there.
01917d
01917d
Also existing dwarf2read.c code usually transfers "dwz flag" together with DIE
01917d
offset, such as:
01917d
	dwarf2_find_containing_comp_unit (sect_offset offset,
01917d
					  unsigned int offset_in_dwz,
01917d
					  struct objfile *objfile)
01917d
This reminds me - why doesn't similar ambiguity happen also for dwp_file?
01917d
I am unfortunately not much aware of the dwp implementation details.
01917d
01917d
01917d
>  > -      struct line_header *line_header
01917d
>  > -	= dwarf_decode_line_header (line_offset, cu);
01917d
>  > +      dwarf2_per_objfile->lineinfo_hash =
01917d
> 
01917d
> As much as I prefer "=" going here, convention says to put it on the
01917d
> next line.
01917d
01917d
I have changed it but this was just blind copy from existing line 21818.
01917d
01917d
01917d
>  > +	htab_create_alloc_ex (127, dwarf2_lineinfo_hash, dwarf2_lineinfo_eq,
01917d
> 
01917d
> I don't have any data, but 127 seems high.
01917d
01917d
I have not changed it but this was just blind copy from existing line 21818.
01917d
01917d
01917d
> I wouldn't change it, I just wanted to ask if you have any data
01917d
> guiding this choice.
01917d
01917d
Tuning some constants really makes no sense when GDB has missing + insanely
01917d
complicated data structures and in consequence GDB is using inappropriate data
01917d
structures with bad algorithmic complexity.  One needs to switch GDB to C++
01917d
and its STL before one can start talking about data structures performance.
01917d
01917d
01917d
No regressions on {x86_64,x86_64-m32,i686}-fedora20-linux-gnu in DWZ mode and
01917d
in -fdebug-types-section mode.
01917d
01917d
01917d
Thanks,
01917d
Jan
01917d
01917d
--cNdxnHkX5QqsyA0e
01917d
Content-Type: text/plain; charset=us-ascii
01917d
Content-Disposition: inline; filename="partialunit5.patch"
01917d
01917d
gdb/
01917d
2014-10-02  Jan Kratochvil  <jan.kratochvil@redhat.com>
01917d
01917d
	Fix 100x slowdown regression on DWZ files.
01917d
	* dwarf2read.c (struct dwarf2_per_objfile): Add line_header_hash.
01917d
	(struct line_header): Add offset and offset_in_dwz.
01917d
	(dwarf_decode_lines): Add parameter decode_mapping to the declaration.
01917d
	(free_line_header_voidp): New declaration.
01917d
	(line_header_hash, line_header_eq): New functions.
01917d
	(dwarf2_build_include_psymtabs): Update dwarf_decode_lines caller.
01917d
	(handle_DW_AT_stmt_list): Use dwarf2_per_objfile->line_header_hash.
01917d
	(free_line_header_voidp): New function.
01917d
	(dwarf_decode_line_header): Initialize offset and offset_in_dwz.
01917d
	(dwarf_decode_lines): New parameter decode_mapping, use it.
01917d
01917d
Index: gdb-7.6.1/gdb/dwarf2read.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/dwarf2read.c
01917d
+++ gdb-7.6.1/gdb/dwarf2read.c
01917d
@@ -272,6 +272,9 @@ struct dwarf2_per_objfile
01917d
 
01917d
   /* The CUs we recently read.  */
01917d
   VEC (dwarf2_per_cu_ptr) *just_read_cus;
01917d
+
01917d
+  /* Table containing line_header indexed by offset and offset_in_dwz.  */
01917d
+  htab_t line_header_hash;
01917d
 };
01917d
 
01917d
 static struct dwarf2_per_objfile *dwarf2_per_objfile;
01917d
@@ -861,6 +864,12 @@ typedef void (die_reader_func_ftype) (co
01917d
    which contains the following information.  */
01917d
 struct line_header
01917d
 {
01917d
+  /* Offset of line number information in .debug_line section.  */
01917d
+  sect_offset offset;
01917d
+
01917d
+  /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile.  */
01917d
+  unsigned offset_in_dwz : 1;
01917d
+
01917d
   unsigned int total_length;
01917d
   unsigned short version;
01917d
   unsigned int header_length;
01917d
@@ -1413,7 +1422,7 @@ static struct line_header *dwarf_decode_
01917d
 
01917d
 static void dwarf_decode_lines (struct line_header *, const char *,
01917d
 				struct dwarf2_cu *, struct partial_symtab *,
01917d
-				int);
01917d
+				int, int decode_mapping);
01917d
 
01917d
 static void dwarf2_start_subfile (char *, const char *, const char *);
01917d
 
01917d
@@ -1746,6 +1755,8 @@ static void process_cu_includes (void);
01917d
 
01917d
 static void check_producer (struct dwarf2_cu *cu);
01917d
 
01917d
+static void free_line_header_voidp (void *arg);
01917d
+
01917d
 #if WORDS_BIGENDIAN
01917d
 
01917d
 /* Convert VALUE between big- and little-endian.  */
01917d
@@ -2141,6 +2152,29 @@ dwarf2_get_dwz_file (void)
01917d
   dwarf2_per_objfile->dwz_file = result;
01917d
   return result;
01917d
 }
01917d
+
01917d
+/* Hash function for line_header_hash.  */
01917d
+
01917d
+static hashval_t
01917d
+line_header_hash (const void *item)
01917d
+{
01917d
+  const struct line_header *ofs = item;
01917d
+
01917d
+  return ofs->offset.sect_off ^ ofs->offset_in_dwz;
01917d
+}
01917d
+
01917d
+/* Equality function for line_header_hash.  */
01917d
+
01917d
+static int
01917d
+line_header_eq (const void *item_lhs, const void *item_rhs)
01917d
+{
01917d
+  const struct line_header *ofs_lhs = item_lhs;
01917d
+  const struct line_header *ofs_rhs = item_rhs;
01917d
+
01917d
+  return (ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off
01917d
+	  && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
01917d
+}
01917d
+
01917d
 
01917d
 /* DWARF quick_symbols_functions support.  */
01917d
 
01917d
@@ -4139,7 +4173,7 @@ dwarf2_build_include_psymtabs (struct dw
01917d
     return;  /* No linetable, so no includes.  */
01917d
 
01917d
   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
01917d
-  dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
01917d
+  dwarf_decode_lines (lh, pst->dirname, cu, pst, 1, 1);
01917d
 
01917d
   free_line_header (lh);
01917d
 }
01917d
@@ -7969,24 +8003,64 @@ static void
01917d
 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
01917d
 			const char *comp_dir)
01917d
 {
01917d
+  struct objfile *objfile = dwarf2_per_objfile->objfile;
01917d
   struct attribute *attr;
01917d
+  unsigned int line_offset;
01917d
+  struct line_header *line_header, line_header_local;
01917d
+  unsigned u;
01917d
+  void **slot;
01917d
+  int decode_mapping;
01917d
 
01917d
   gdb_assert (! cu->per_cu->is_debug_types);
01917d
 
01917d
   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
01917d
-  if (attr)
01917d
+  if (attr == NULL)
01917d
+    return;
01917d
+
01917d
+  line_offset = DW_UNSND (attr);
01917d
+
01917d
+  if (dwarf2_per_objfile->line_header_hash == NULL)
01917d
     {
01917d
-      unsigned int line_offset = DW_UNSND (attr);
01917d
-      struct line_header *line_header
01917d
-	= dwarf_decode_line_header (line_offset, cu);
01917d
-
01917d
-      if (line_header)
01917d
-	{
01917d
-	  cu->line_header = line_header;
01917d
-	  make_cleanup (free_cu_line_header, cu);
01917d
-	  dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
01917d
-	}
01917d
+      dwarf2_per_objfile->line_header_hash
01917d
+	= htab_create_alloc_ex (127, line_header_hash, line_header_eq,
01917d
+				free_line_header_voidp,
01917d
+				&objfile->objfile_obstack,
01917d
+				hashtab_obstack_allocate,
01917d
+				dummy_obstack_deallocate);
01917d
+    }
01917d
+
01917d
+  line_header_local.offset.sect_off = line_offset;
01917d
+  line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
01917d
+  slot = htab_find_slot (dwarf2_per_objfile->line_header_hash,
01917d
+			 &line_header_local, NO_INSERT);
01917d
+
01917d
+  /* For DW_TAG_compile_unit we need info like symtab::linetable which
01917d
+     is not present in *SLOT.  */
01917d
+  if (die->tag == DW_TAG_partial_unit && slot != NULL)
01917d
+    {
01917d
+      gdb_assert (*slot != NULL);
01917d
+      cu->line_header = *slot;
01917d
+      return;
01917d
+    }
01917d
+
01917d
+  line_header = dwarf_decode_line_header (line_offset, cu);
01917d
+  if (line_header == NULL)
01917d
+    return;
01917d
+  cu->line_header = line_header;
01917d
+
01917d
+  slot = htab_find_slot (dwarf2_per_objfile->line_header_hash,
01917d
+			 &line_header_local, INSERT);
01917d
+  gdb_assert (slot != NULL);
01917d
+  if (*slot == NULL)
01917d
+    *slot = line_header;
01917d
+  else
01917d
+    {
01917d
+      gdb_assert (die->tag != DW_TAG_partial_unit);
01917d
+      make_cleanup (free_cu_line_header, cu);
01917d
     }
01917d
+  decode_mapping = (die->tag != DW_TAG_partial_unit);
01917d
+  dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1,
01917d
+		      decode_mapping);
01917d
 }
01917d
 
01917d
 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit.  */
01917d
@@ -15203,6 +15277,16 @@ free_line_header (struct line_header *lh
01917d
   xfree (lh);
01917d
 }
01917d
 
01917d
+/* Stub for free_line_header to match void * callback types.  */
01917d
+
01917d
+static void
01917d
+free_line_header_voidp (void *arg)
01917d
+{
01917d
+  struct line_header *lh = arg;
01917d
+
01917d
+  free_line_header (lh);
01917d
+}
01917d
+
01917d
 /* Add an entry to LH's include directory table.  */
01917d
 
01917d
 static void
01917d
@@ -15333,6 +15417,9 @@ dwarf_decode_line_header (unsigned int o
01917d
   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
01917d
                           (void *) lh);
01917d
 
01917d
+  lh->offset.sect_off = offset;
01917d
+  lh->offset_in_dwz = cu->per_cu->is_dwz;
01917d
+
01917d
   line_ptr = section->buffer + offset;
01917d
 
01917d
   /* Read in the header.  */
01917d
@@ -15826,18 +15913,22 @@ dwarf_decode_lines_1 (struct line_header
01917d
    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
01917d
    symtab we don't use it in the name of the psymtabs we create.
01917d
    E.g. expand_line_sal requires this when finding psymtabs to expand.
01917d
-   A good testcase for this is mb-inline.exp.  */
01917d
+   A good testcase for this is mb-inline.exp.
01917d
+
01917d
+   Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
01917d
+   for its PC<->lines mapping information.  Otherwise only filenames
01917d
+   tables is read in.  */
01917d
 
01917d
 static void
01917d
 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
01917d
 		    struct dwarf2_cu *cu, struct partial_symtab *pst,
01917d
-		    int want_line_info)
01917d
+		    int want_line_info, int decode_mapping)
01917d
 {
01917d
   struct objfile *objfile = cu->objfile;
01917d
   const int decode_for_pst_p = (pst != NULL);
01917d
   struct subfile *first_subfile = current_subfile;
01917d
 
01917d
-  if (want_line_info)
01917d
+  if (want_line_info && decode_mapping)
01917d
     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
01917d
 
01917d
   if (decode_for_pst_p)