This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
[PATCH] gold: add an option to mmap whole files (defaults to true on 64-bit linkers)
- From: Mikolaj Zalewski <mikolajz at google dot com>
- To: binutils at sourceware dot org
- Date: Wed, 21 Oct 2009 17:56:47 -0700
- Subject: [PATCH] gold: add an option to mmap whole files (defaults to true on 64-bit linkers)
I have created a patch adding an option to mmap whole file in
File_read::open and making it default on 64-bit linker builds. This
seems to speed up large builds. The changes were very similar to the
already existing support to open pseudo-files (for tests) with
contents passed as an argument, so I've merged both features.
MikoÅaj
2009-10-21 Mikolaj Zalewski <mikolajz@google.com>
* fileread.cc: (File_read::View::~View): Use the new data_ownership_ filed.
(File_read::~File_read): Dispose the new whole_file_view_.
(File_read::open): Mmap the whole file if needed.
(File_read::open): Use whole_file_view_ instead of contents_.
(File_read::find_view): Use whole_file_view_ if applicable.
(File_read::do_read): Use whole_file_view_ instead of contents_.
(File_read::make_view): Use whole_file_view_ instead of contents_,
update File_read::View::View call.
(File_read::find_or_make_view): Update File_read::View::View call.
* fileread.h: (File_read::File_read): Initialize whole_file_view_,
remove contents_.
(File_read::View::Data_ownership): New enum.
(File_read::View::View): Replace bool mapped with Data_ownership argument.
(File_read::View::mapped_): Remove (replaced by data_ownership_).
(File_read::View::data_ownership_): New field.
(File_read::contents_): Remove (replaced by whole_file_view_).
(File_read::whole_file_view_): New field.
From e934e85aa1224b4bc48f023f8417c0f01cf6e3ef Mon Sep 17 00:00:00 2001
From: Mikolaj Zalewski <mikolajz@google.com>
Date: Wed, 21 Oct 2009 17:16:47 -0700
Subject: [PATCH] gold: add an option to mmap whole files (defaults to true on 64-bit machines)
---
gold/fileread.cc | 51 ++++++++++++++++++++++++++++++++++++++++-----------
gold/fileread.h | 31 ++++++++++++++++++++++++-------
gold/options.h | 12 ++++++++++++
3 files changed, 76 insertions(+), 18 deletions(-)
diff --git a/gold/fileread.cc b/gold/fileread.cc
index 7067b0b..d117a1c 100644
--- a/gold/fileread.cc
+++ b/gold/fileread.cc
@@ -57,15 +57,17 @@ namespace gold
File_read::View::~View()
{
gold_assert(!this->is_locked());
- if (!this->mapped_)
+ if (this->data_ownership_ == DATA_ALLOCATED_ARRAY)
delete[] this->data_;
- else
+ else if (this->data_ownership_ == DATA_MMAPPED)
{
if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
gold_warning(_("munmap failed: %s"), strerror(errno));
File_read::current_mapped_bytes -= this->size_;
}
+ else
+ gold_assert(this->data_ownership_ == DATA_NOT_OWNED); // Last enum value
}
void
@@ -105,6 +107,8 @@ File_read::~File_read()
}
this->name_.clear();
this->clear_views(true);
+ if (this->whole_file_view_)
+ delete this->whole_file_view_;
}
// Open the file.
@@ -132,6 +136,22 @@ File_read::open(const Task* task, const std::string& name)
gold_debug(DEBUG_FILES, "Attempt to open %s succeeded",
this->name_.c_str());
+ // Options may not yet be ready e.g. when reading a version
+ // script. We then default to --no-mmap-whole-files.
+ if (parameters->options_valid() &&
+ parameters->options().mmap_whole_files())
+ {
+ const unsigned char* contents = static_cast<const unsigned char*>(
+ ::mmap(NULL, this->size_, PROT_READ, MAP_PRIVATE,
+ this->descriptor_, 0));
+ if (contents == MAP_FAILED)
+ gold_fatal(_("%s: mmap failed: %s"), this->filename().c_str(),
+ strerror(errno));
+ this->whole_file_view_ = new View(0, this->size_, contents, 0, false,
+ View::DATA_MMAPPED);
+ this->mapped_bytes_ += this->size_;
+ }
+
this->token_.add_writer(task);
}
@@ -149,7 +169,8 @@ File_read::open(const Task* task, const std::string& name,
&& !this->is_descriptor_opened_
&& this->name_.empty());
this->name_ = name;
- this->contents_ = contents;
+ this->whole_file_view_ = new View(0, size, contents, 0, false,
+ View::DATA_NOT_OWNED);
this->size_ = size;
this->token_.add_writer(task);
return true;
@@ -246,6 +267,12 @@ File_read::find_view(off_t start, section_size_type size,
if (vshifted != NULL)
*vshifted = NULL;
+ // If we have the whole file mmapped, and the alignment is right,
+ // we can return it.
+ if (this->whole_file_view_)
+ if (byteshift == -1U || byteshift == 0)
+ return this->whole_file_view_;
+
off_t page = File_read::page_offset(start);
unsigned int bszero = 0;
@@ -281,12 +308,12 @@ void
File_read::do_read(off_t start, section_size_type size, void* p)
{
ssize_t bytes;
- if (this->contents_ != NULL)
+ if (this->whole_file_view_ != NULL)
{
bytes = this->size_ - start;
if (static_cast<section_size_type>(bytes) >= size)
{
- memcpy(p, this->contents_ + start, size);
+ memcpy(p, this->whole_file_view_->data() + start, size);
return;
}
}
@@ -386,12 +413,13 @@ File_read::make_view(off_t start, section_size_type size,
}
File_read::View* v;
- if (this->contents_ != NULL || byteshift != 0)
+ if (this->whole_file_view_ != NULL || byteshift != 0)
{
unsigned char* p = new unsigned char[psize + byteshift];
memset(p, 0, byteshift);
this->do_read(poff, psize, p + byteshift);
- v = new File_read::View(poff, psize, p, byteshift, cache, false);
+ v = new File_read::View(poff, psize, p, byteshift, cache,
+ View::DATA_ALLOCATED_ARRAY);
}
else
{
@@ -408,7 +436,8 @@ File_read::make_view(off_t start, section_size_type size,
this->mapped_bytes_ += psize;
const unsigned char* pbytes = static_cast<const unsigned char*>(p);
- v = new File_read::View(poff, psize, pbytes, 0, cache, true);
+ v = new File_read::View(poff, psize, pbytes, 0, cache,
+ View::DATA_MMAPPED);
}
this->add_view(v);
@@ -463,9 +492,9 @@ File_read::find_or_make_view(off_t offset, off_t start,
memset(pbytes, 0, byteshift);
memcpy(pbytes + byteshift, v->data() + v->byteshift(), v->size());
- File_read::View* shifted_view = new File_read::View(v->start(), v->size(),
- pbytes, byteshift,
- cache, false);
+ File_read::View* shifted_view =
+ new File_read::View(v->start(), v->size(), pbytes, byteshift,
+ cache, View::DATA_ALLOCATED_ARRAY);
this->add_view(shifted_view);
return shifted_view;
diff --git a/gold/fileread.h b/gold/fileread.h
index bdffdd1..235907d 100644
--- a/gold/fileread.h
+++ b/gold/fileread.h
@@ -65,8 +65,8 @@ class File_read
public:
File_read()
: name_(), descriptor_(-1), is_descriptor_opened_(false), object_count_(0),
- size_(0), token_(false), views_(), saved_views_(), contents_(NULL),
- mapped_bytes_(0), released_(true)
+ size_(0), token_(false), views_(), saved_views_(), mapped_bytes_(0),
+ released_(true), whole_file_view_(NULL)
{ }
~File_read();
@@ -234,10 +234,23 @@ class File_read
class View
{
public:
+ // Specifies how to dispose the data on destruction of the view.
+ enum Data_ownership
+ {
+ // Data owned by File object - nothing done in destructor.
+ DATA_NOT_OWNED = 0,
+ // Data alocated with new[] and owned by this object - should
+ // use delete[].
+ DATA_ALLOCATED_ARRAY = 1,
+ // Data mmapped and owned by this object - should munmap.
+ DATA_MMAPPED = 2
+ };
+
View(off_t start, section_size_type size, const unsigned char* data,
- unsigned int byteshift, bool cache, bool mapped)
+ unsigned int byteshift, bool cache, Data_ownership data_ownership)
: start_(start), size_(size), data_(data), lock_count_(0),
- byteshift_(byteshift), cache_(cache), mapped_(mapped), accessed_(true)
+ byteshift_(byteshift), cache_(cache), data_ownership_(data_ownership),
+ accessed_(true)
{ }
~View();
@@ -311,7 +324,7 @@ class File_read
bool cache_;
// Whether the view is mapped into memory. If not, data_ points
// to memory allocated using new[].
- bool mapped_;
+ Data_ownership data_ownership_;
// Whether the view has been accessed recently.
bool accessed_;
};
@@ -400,14 +413,18 @@ class File_read
// List of views which were locked but had to be removed from views_
// because they were not large enough.
Saved_views saved_views_;
- // Specified file contents. Used only for testing purposes.
- const unsigned char* contents_;
// Total amount of space mapped into memory. This is only changed
// while the file is locked. When we unlock the file, we transfer
// the total to total_mapped_bytes, and reset this to zero.
size_t mapped_bytes_;
// Whether the file was released.
bool released_;
+ // A view containing the whole file. May be NULL is we mmap only
+ // the relevant parts of the file. Not NULL if:
+ // - Flag --mmap_whole_files is set (default on 64-bit systems).
+ // - The contents was specified in the constructor. Used only for
+ // testing purposes).
+ View* whole_file_view_;
};
// A view of file data that persists even when the file is unlocked.
diff --git a/gold/options.h b/gold/options.h
index cbb45b5..0eb71a4 100644
--- a/gold/options.h
+++ b/gold/options.h
@@ -732,6 +732,18 @@ class General_options
DEFINE_string(m, options::EXACTLY_ONE_DASH, 'm', "",
N_("Ignored for compatibility"), N_("EMULATION"));
+#ifdef __LP64__
+ DEFINE_bool(mmap_whole_files, options::TWO_DASHES, '\0', true,
+ N_("Map whole files to memory (default on 64-bit systems)"),
+ N_("Map relevant files parts to memory (default on 32-bit "
+ "systems)"));
+#else
+ DEFINE_bool(mmap_whole_files, options::TWO_DASHES, '\0', false,
+ N_("Map whole files to memory (default on 64-bit systems)"),
+ N_("Map relevant files parts to memory (default on 32-bit "
+ "systems)"));
+#endif
+
DEFINE_bool(print_map, options::TWO_DASHES, 'M', false,
N_("Write map file on standard output"), NULL);
DEFINE_string(Map, options::ONE_DASH, '\0', NULL, N_("Write map file"),
--
1.5.4.3