aboutsummaryrefslogtreecommitdiff
path: root/gold/dwarf_reader.cc
diff options
context:
space:
mode:
authorCraig Silverstein <csilvers@google.com>2008-05-01 00:25:33 +0000
committerCraig Silverstein <csilvers@google.com>2008-05-01 00:25:33 +0000
commite4e5049b7bb41c36b6696a59f59eaafee95aedb1 (patch)
tree461b3fb5fa13581bb234dbd13c08d19160b35c9c /gold/dwarf_reader.cc
parentf089c43345c097a7f604adcb0dcbef301d57ef93 (diff)
downloadgdb-e4e5049b7bb41c36b6696a59f59eaafee95aedb1.zip
gdb-e4e5049b7bb41c36b6696a59f59eaafee95aedb1.tar.gz
gdb-e4e5049b7bb41c36b6696a59f59eaafee95aedb1.tar.bz2
* dwarf_reader.cc (next_generation_count): New static var.
(Addr2line_cache_entry): New struct. (addr2line_cache): New static var. (Dwarf_line_info::one_addr2line): Added caching. (Dwarf_line_info::clear_addr2line_cache): New function. * dwarf_reader.h (Dwarf_line_info::one_addr2line): Add cache-size parameter. (Dwarf_line_info::one_addr2line_cache): New function. * symtab.cc (Symbol_table::detect_odr_violations): Pass new cache-size argument to one_addr2line(), and clear cache.
Diffstat (limited to 'gold/dwarf_reader.cc')
-rw-r--r--gold/dwarf_reader.cc118
1 files changed, 102 insertions, 16 deletions
diff --git a/gold/dwarf_reader.cc b/gold/dwarf_reader.cc
index d3faeeb..ec697e4 100644
--- a/gold/dwarf_reader.cc
+++ b/gold/dwarf_reader.cc
@@ -23,6 +23,7 @@
#include "gold.h"
#include <algorithm>
+#include <vector>
#include "elfcpp_swap.h"
#include "dwarf.h"
@@ -799,35 +800,120 @@ Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx,
// Dwarf_line_info routines.
+static unsigned int next_generation_count = 0;
+
+struct Addr2line_cache_entry
+{
+ Object* object;
+ unsigned int shndx;
+ Dwarf_line_info* dwarf_line_info;
+ unsigned int generation_count;
+ unsigned int access_count;
+
+ Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d)
+ : object(o), shndx(s), dwarf_line_info(d),
+ generation_count(next_generation_count), access_count(0)
+ {
+ if (next_generation_count < (1U << 31))
+ ++next_generation_count;
+ }
+};
+// We expect this cache to be small, so don't bother with a hashtable
+// or priority queue or anything: just use a simple vector.
+static std::vector<Addr2line_cache_entry> addr2line_cache;
+
std::string
Dwarf_line_info::one_addr2line(Object* object,
- unsigned int shndx, off_t offset)
+ unsigned int shndx, off_t offset,
+ size_t cache_size)
{
- switch (parameters->size_and_endianness())
+ Dwarf_line_info* lineinfo = NULL;
+ std::vector<Addr2line_cache_entry>::iterator it;
+
+ // First, check the cache. If we hit, update the counts.
+ for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
{
+ if (it->object == object && it->shndx == shndx)
+ {
+ lineinfo = it->dwarf_line_info;
+ it->generation_count = next_generation_count;
+ // We cap generation_count at 2^31 -1 to avoid overflow.
+ if (next_generation_count < (1U << 31))
+ ++next_generation_count;
+ // We cap access_count at 31 so 2^access_count doesn't overflow
+ if (it->access_count < 31)
+ ++it->access_count;
+ break;
+ }
+ }
+
+ // If we don't hit the cache, create a new object and insert into the
+ // cache.
+ if (lineinfo == NULL)
+ {
+ switch (parameters->size_and_endianness())
+ {
#ifdef HAVE_TARGET_32_LITTLE
- case Parameters::TARGET_32_LITTLE:
- return Sized_dwarf_line_info<32, false>(object, shndx).addr2line(shndx,
- offset);
+ case Parameters::TARGET_32_LITTLE:
+ lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
#endif
#ifdef HAVE_TARGET_32_BIG
- case Parameters::TARGET_32_BIG:
- return Sized_dwarf_line_info<32, true>(object, shndx).addr2line(shndx,
- offset);
+ case Parameters::TARGET_32_BIG:
+ lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
#endif
#ifdef HAVE_TARGET_64_LITTLE
- case Parameters::TARGET_64_LITTLE:
- return Sized_dwarf_line_info<64, false>(object, shndx).addr2line(shndx,
- offset);
+ case Parameters::TARGET_64_LITTLE:
+ lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
#endif
#ifdef HAVE_TARGET_64_BIG
- case Parameters::TARGET_64_BIG:
- return Sized_dwarf_line_info<64, true>(object, shndx).addr2line(shndx,
- offset);
+ case Parameters::TARGET_64_BIG:
+ lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
#endif
- default:
- gold_unreachable();
+ default:
+ gold_unreachable();
+ }
+ addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
+ }
+
+ // Now that we have our object, figure out the answer
+ std::string retval = lineinfo->addr2line(shndx, offset);
+
+ // Finally, if our cache has grown too big, delete old objects. We
+ // assume the common (probably only) case is deleting only one object.
+ // We use a pretty simple scheme to evict: function of LRU and MFU.
+ while (addr2line_cache.size() > cache_size)
+ {
+ unsigned int lowest_score = ~0U;
+ std::vector<Addr2line_cache_entry>::iterator lowest
+ = addr2line_cache.end();
+ for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
+ {
+ const unsigned int score = (it->generation_count
+ + (1U << it->access_count));
+ if (score < lowest_score)
+ {
+ lowest_score = score;
+ lowest = it;
+ }
+ }
+ if (lowest != addr2line_cache.end())
+ {
+ delete lowest->dwarf_line_info;
+ addr2line_cache.erase(lowest);
+ }
}
+
+ return retval;
+}
+
+void
+Dwarf_line_info::clear_addr2line_cache()
+{
+ for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
+ it != addr2line_cache.end();
+ ++it)
+ delete it->dwarf_line_info;
+ addr2line_cache.clear();
}
#ifdef HAVE_TARGET_32_LITTLE