Commit 85c39b61 authored by Eric Roman's avatar Eric Roman Committed by Commit Bot

Use a more efficient implementation for flattening a bounded-size NetLog to a single file.

Only read 64KiB of the events file at a time.

Bug: 679030
Change-Id: If063b547d547240f79024350c1cead13e0f6426e
Reviewed-on: https://chromium-review.googlesource.com/572185
Commit-Queue: Eric Roman <eroman@chromium.org>
Reviewed-by: default avatarHelen Li <xunjieli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487591}
parent 189ddf60
...@@ -77,16 +77,24 @@ size_t WriteToFile(FILE* file, ...@@ -77,16 +77,24 @@ size_t WriteToFile(FILE* file,
// Copies all of the data at |source_path| and appends it to |destination_file|, // Copies all of the data at |source_path| and appends it to |destination_file|,
// then deletes |source_path|. // then deletes |source_path|.
void AppendToFileThenDelete(const base::FilePath& source_path, void AppendToFileThenDelete(const base::FilePath& source_path,
FILE* destination_file) { FILE* destination_file,
// TODO(eroman): This is a bad implementation, as it reads the entire source char* read_buffer,
// file into memory. Should read it in chunks to bound memory usage, as source size_t read_buffer_size) {
// files could be large. base::ScopedFILE source_file(base::OpenFile(source_path, "rb"));
std::string contents; if (!source_file)
if (!base::ReadFileToString(source_path, &contents))
return; return;
// Append the event file contents to the log file. // Read |source_path|'s contents in chunks of read_buffer_size and append
WriteToFile(destination_file, contents); // to |destination_file|.
size_t num_bytes_read;
while ((num_bytes_read =
fread(read_buffer, 1, read_buffer_size, source_file.get())) > 0) {
WriteToFile(destination_file,
base::StringPiece(read_buffer, num_bytes_read));
}
// Now that it has been copied, delete the source file.
source_file.reset();
base::DeleteFile(source_path, false); base::DeleteFile(source_path, false);
} }
...@@ -707,11 +715,17 @@ void FileNetLogObserver::BoundedFileWriter::StitchFinalLogFile() { ...@@ -707,11 +715,17 @@ void FileNetLogObserver::BoundedFileWriter::StitchFinalLogFile() {
// Make sure all the events files are flushed (as will read them next). // Make sure all the events files are flushed (as will read them next).
current_event_file_.reset(); current_event_file_.reset();
// Allocate a 64K buffer used for reading the files. At most kReadBufferSize
// bytes will be in memory at a time.
const size_t kReadBufferSize = 1 << 16; // 64KiB
std::unique_ptr<char[]> read_buffer(new char[kReadBufferSize]);
// Re-open the final log file in order to truncate it. // Re-open the final log file in order to truncate it.
final_log_file_ = OpenFileForWrite(final_log_path_); final_log_file_ = OpenFileForWrite(final_log_path_);
// Append the constants file. // Append the constants file.
AppendToFileThenDelete(GetConstantsFilePath(), final_log_file_.get()); AppendToFileThenDelete(GetConstantsFilePath(), final_log_file_.get(),
read_buffer.get(), kReadBufferSize);
// Iterate over the events files, from oldest to most recent, and append them // Iterate over the events files, from oldest to most recent, and append them
// to the final destination. Note that "file numbers" start at 1 not 0. // to the final destination. Note that "file numbers" start at 1 not 0.
...@@ -722,7 +736,8 @@ void FileNetLogObserver::BoundedFileWriter::StitchFinalLogFile() { ...@@ -722,7 +736,8 @@ void FileNetLogObserver::BoundedFileWriter::StitchFinalLogFile() {
for (size_t filenumber = begin_filenumber; filenumber < end_filenumber; for (size_t filenumber = begin_filenumber; filenumber < end_filenumber;
++filenumber) { ++filenumber) {
AppendToFileThenDelete(GetEventFilePath(FileNumberToIndex(filenumber)), AppendToFileThenDelete(GetEventFilePath(FileNumberToIndex(filenumber)),
final_log_file_.get()); final_log_file_.get(), read_buffer.get(),
kReadBufferSize);
} }
// Account for the final event line ending in a ",\n". Strip it to form valid // Account for the final event line ending in a ",\n". Strip it to form valid
...@@ -730,7 +745,8 @@ void FileNetLogObserver::BoundedFileWriter::StitchFinalLogFile() { ...@@ -730,7 +745,8 @@ void FileNetLogObserver::BoundedFileWriter::StitchFinalLogFile() {
RewindIfWroteEventBytes(final_log_file_.get()); RewindIfWroteEventBytes(final_log_file_.get());
// Append the polled data. // Append the polled data.
AppendToFileThenDelete(GetClosingFilePath(), final_log_file_.get()); AppendToFileThenDelete(GetClosingFilePath(), final_log_file_.get(),
read_buffer.get(), kReadBufferSize);
// Delete the inprogress directory (and anything that may still be left inside // Delete the inprogress directory (and anything that may still be left inside
// it). // it).
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment