[PR,gdb/17143] Improve memory usage of major obstacks

Message ID m37fnkae0l.fsf@seba.sebabeach.org
State New, archived
Headers

Commit Message

Doug Evans Sept. 20, 2015, 11:04 p.m. UTC
  Hi.

The default obstack alignment on amd64-linux is 16 if one is using
glibc's obstack instead of libiberty's (glibc's includes long double
in the default alignment calculation).

This wastes 10s of megabytes when debugging large programs.

I couldn't find any case where we use long double or DOUBLEST in
either of objfile's two obstacks: objfile_obstack, per_bfd->storage_obstack,
so I'd like to propose this patch.

Regression tested on amd64-linux.

[btw, for another significant memory hit there is pr 14108.
I think(!) it's still an issue.]

2015-09-20  Doug Evans  <xdje42@gmail.com>

	PR gdb/17143
	* gdb_obstack.c (gdb_obstack_init): New function.
	* gdb_obstack.h (gdb_obstack_init): Declare.
	* objfiles.c (get_objfile_bfd_data): Call it.
	(allocate_objfile): Ditto.
  

Comments

Pedro Alves Sept. 29, 2015, 11:40 a.m. UTC | #1
On 09/21/2015 12:04 AM, Doug Evans wrote:
> Hi.
> 
> The default obstack alignment on amd64-linux is 16 if one is using
> glibc's obstack instead of libiberty's (glibc's includes long double
> in the default alignment calculation).
> 
> This wastes 10s of megabytes when debugging large programs.
> 
> I couldn't find any case where we use long double or DOUBLEST in
> either of objfile's two obstacks: objfile_obstack, per_bfd->storage_obstack,
> so I'd like to propose this patch.
> 

How about putting a gdb_static_assert in OBSTACK_ZALLOC, etc. that
ensures the alignment of the type is <= 10 ?  We could restrict that
to gcc/clang and use __alignof__.  (gnulib/import/stddef.in.h conditions
the __alignof__ usage on #ifdef __GNUC__, which both define.).

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/gdb_obstack.c b/gdb/gdb_obstack.c
index d8d03df..4cff52e 100644
--- a/gdb/gdb_obstack.c
+++ b/gdb/gdb_obstack.c
@@ -18,8 +18,30 @@ 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include <stddef.h>
 #include "gdb_obstack.h"
 
+/* See gdb_obstack.h.  */
+
+void
+gdb_obstack_init (struct obstack *obstack)
+{
+  struct find_alignment
+  {
+    char c;
+    union
+    {
+      ULONGEST i;
+      void *p;
+    } u;
+  };
+  int needed_alignment = offsetof (struct find_alignment, u);
+
+  obstack_specify_allocation (obstack, 0, needed_alignment,
+			      /* Note: These are macros in gdb_obstack.h.  */
+			      obstack_chunk_alloc, obstack_chunk_free);
+}
+
 /* Concatenate NULL terminated variable argument list of `const char *'
    strings; return the new string.  Space is found in the OBSTACKP.
    Argument list must be terminated by a sentinel expression `(char *)
diff --git a/gdb/gdb_obstack.h b/gdb/gdb_obstack.h
index 3272721..518a593 100644
--- a/gdb/gdb_obstack.h
+++ b/gdb/gdb_obstack.h
@@ -22,6 +22,13 @@ 
 
 #include "obstack.h"
 
+/* Initialize an obstack for gdb's use.
+   N.B. The alignment of the obstack is set for pointers and LONGEST,
+   but not long double.  This saves 10s of megabytes when debugging large
+   programs.  */
+
+extern void gdb_obstack_init (struct obstack *);
+
 /* Utility macros - wrap obstack alloc into something more robust.  */
 
 #define OBSTACK_ZALLOC(OBSTACK,TYPE) \
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index f6be91e..4364247 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -147,7 +147,7 @@  get_objfile_bfd_data (struct objfile *objfile, struct bfd *abfd)
       if (abfd != NULL)
 	storage->gdbarch = gdbarch_from_bfd (abfd);
 
-      obstack_init (&storage->storage_obstack);
+      gdb_obstack_init (&storage->storage_obstack);
       storage->filename_cache = bcache_xmalloc (NULL, NULL);
       storage->macro_cache = bcache_xmalloc (NULL, NULL);
       storage->language_of_main = language_unknown;
@@ -375,9 +375,7 @@  allocate_objfile (bfd *abfd, const char *name, int flags)
 
   objfile = XCNEW (struct objfile);
   objfile->psymbol_cache = psymbol_bcache_init ();
-  /* We could use obstack_specify_allocation here instead, but
-     gdb_obstack.h specifies the alloc/dealloc functions.  */
-  obstack_init (&objfile->objfile_obstack);
+  gdb_obstack_init (&objfile->objfile_obstack);
 
   objfile_alloc_data (objfile);