Revert of Revert some other possible suspects of a 0.43% size increase of setup.exe: https://build.chromium.o… (patchset #1 id:1 of https://codereview.chromium.org/1124563003/)

Reason for revert:
Broke the compile because one of these CLs is already depended on (rch@'s bce10d97d6fc5233071427cf622aaad7abde9e19).

Original issue's description:
> Revert some other possible suspects of a 0.43% size increase of setup.exe: https://build.chromium.org/p/chromium/builders/Win/builds/31562
>
>   Revert "Add granular base::File tracing."
>
>   This reverts commit a6e05c977096a03774e5406d63ad80c0166f9adc.
>
>   Revert "Add AllReadDataConsumed and AllWriteDataConsumed methods to SocketDataProvider"
>
>   This reverts commit bce10d97d6fc5233071427cf622aaad7abde9e19.
>
>   Revert "Avoid unnecessary memory allocations at PlatformThread::SetName()"
>
>   This reverts commit 4839a142bf95776323647c82ca9dc0725f7c4f15.
>
> TBR=tdresser@chromium.org
> BUG=none
> TEST=green sizes
>
> Committed: https://chromium.googlesource.com/chromium/src/+/c41830d6f55f85e2f5c8841db4b6ed81239b671d

TBR=tdresser@chromium.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=none

Review URL: https://codereview.chromium.org/1123833003

Cr-Commit-Position: refs/heads/master@{#328207}


CrOS-Libchrome-Original-Commit: eb1e4d1dd2511083e1d5506d1edfbdff68e133cc
diff --git a/base/files/file.cc b/base/files/file.cc
index 8030bf1..774539f 100644
--- a/base/files/file.cc
+++ b/base/files/file.cc
@@ -4,6 +4,7 @@
 
 #include "base/files/file.h"
 #include "base/files/file_path.h"
+#include "base/files/file_tracing.h"
 #include "base/metrics/histogram.h"
 #include "base/timer/elapsed_timer.h"
 
@@ -25,11 +26,11 @@
 }
 
 #if !defined(OS_NACL)
-File::File(const FilePath& name, uint32 flags)
+File::File(const FilePath& path, uint32 flags)
     : error_details_(FILE_OK),
       created_(false),
       async_(false) {
-  Initialize(name, flags);
+  Initialize(path, flags);
 }
 #endif
 
@@ -51,6 +52,7 @@
 
 File::File(RValue other)
     : file_(other.object->TakePlatformFile()),
+      path_(other.object->path_),
       error_details_(other.object->error_details()),
       created_(other.object->created()),
       async_(other.object->async_) {
@@ -65,6 +67,7 @@
   if (this != other.object) {
     Close();
     SetPlatformFile(other.object->TakePlatformFile());
+    path_ = other.object->path_;
     error_details_ = other.object->error_details();
     created_ = other.object->created();
     async_ = other.object->async_;
@@ -73,12 +76,14 @@
 }
 
 #if !defined(OS_NACL)
-void File::Initialize(const FilePath& name, uint32 flags) {
-  if (name.ReferencesParent()) {
+void File::Initialize(const FilePath& path, uint32 flags) {
+  if (path.ReferencesParent()) {
     error_details_ = FILE_ERROR_ACCESS_DENIED;
     return;
   }
-  DoInitialize(name, flags);
+  path_ = path;
+  SCOPED_FILE_TRACE("Initialize");
+  DoInitialize(flags);
 }
 #endif
 
@@ -128,9 +133,18 @@
 
 bool File::Flush() {
   ElapsedTimer timer;
+  SCOPED_FILE_TRACE("Flush");
   bool return_value = DoFlush();
   UMA_HISTOGRAM_TIMES("PlatformFile.FlushTime", timer.Elapsed());
   return return_value;
 }
 
+File::TraceEnabler::TraceEnabler() {
+  FILE_TRACING_BEGIN();
+}
+
+File::TraceEnabler::~TraceEnabler() {
+  FILE_TRACING_END();
+}
+
 }  // namespace base
diff --git a/base/files/file.h b/base/files/file.h
index 89077b4..589403d 100644
--- a/base/files/file.h
+++ b/base/files/file.h
@@ -18,6 +18,7 @@
 
 #include "base/base_export.h"
 #include "base/basictypes.h"
+#include "base/files/file_path.h"
 #include "base/files/scoped_file.h"
 #include "base/gtest_prod_util.h"
 #include "base/move.h"
@@ -31,8 +32,6 @@
 
 namespace base {
 
-class FilePath;
-
 #if defined(OS_WIN)
 typedef HANDLE PlatformFile;
 #elif defined(OS_POSIX)
@@ -162,8 +161,8 @@
   File();
 
   // Creates or opens the given file. This will fail with 'access denied' if the
-  // |name| contains path traversal ('..') components.
-  File(const FilePath& name, uint32 flags);
+  // |path| contains path traversal ('..') components.
+  File(const FilePath& path, uint32 flags);
 
   // Takes ownership of |platform_file|.
   explicit File(PlatformFile platform_file);
@@ -180,7 +179,7 @@
   File& operator=(RValue other);
 
   // Creates or opens the given file.
-  void Initialize(const FilePath& name, uint32 flags);
+  void Initialize(const FilePath& path, uint32 flags);
 
   bool IsValid() const;
 
@@ -191,7 +190,7 @@
 
   // Returns the OS result of opening this file. Note that the way to verify
   // the success of the operation is to use IsValid(), not this method:
-  //   File file(name, flags);
+  //   File file(path, flags);
   //   if (!file.IsValid())
   //     return;
   Error error_details() const { return error_details_; }
@@ -305,6 +304,8 @@
  private:
   FRIEND_TEST_ALL_PREFIXES(::FileTest, MemoryCorruption);
 
+  friend class ScopedFileTrace;
+
 #if defined(OS_POSIX)
   // Encloses a single ScopedFD, saving a cheap tamper resistent memory checksum
   // alongside it. This checksum is validated at every access, allowing early
@@ -350,9 +351,9 @@
   };
 #endif
 
-  // Creates or opens the given file. Only called if |name| has no traversal
-  // ('..') components.
-  void DoInitialize(const FilePath& name, uint32 flags);
+  // Creates or opens the given file. Only called if |path_| has no
+  // traversal ('..') components.
+  void DoInitialize(uint32 flags);
 
   // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore,
   // cf. issue 473337.
@@ -366,6 +367,18 @@
   MemoryCheckingScopedFD file_;
 #endif
 
+  // Path that |Initialize()| was called with. Only set if safe (i.e. no '..').
+  FilePath path_;
+
+  class TraceEnabler {
+   public:
+    TraceEnabler();
+    ~TraceEnabler();
+  };
+
+  // Object tied to the lifetime of this File that enables/disables tracing.
+  TraceEnabler trace_enabler_;
+
   Error error_details_;
   bool created_;
   bool async_;
diff --git a/base/files/file_posix.cc b/base/files/file_posix.cc
index 4c79057..563dba1 100644
--- a/base/files/file_posix.cc
+++ b/base/files/file_posix.cc
@@ -10,6 +10,7 @@
 #include <unistd.h>
 
 #include "base/files/file_path.h"
+#include "base/files/file_tracing.h"
 #include "base/logging.h"
 #include "base/metrics/sparse_histogram.h"
 #include "base/posix/eintr_wrapper.h"
@@ -173,6 +174,7 @@
   if (!IsValid())
     return;
 
+  SCOPED_FILE_TRACE("Close");
   ThreadRestrictions::AssertIOAllowed();
   file_.reset();
 }
@@ -181,6 +183,8 @@
   ThreadRestrictions::AssertIOAllowed();
   DCHECK(IsValid());
 
+  SCOPED_FILE_TRACE_WITH_SIZE("Seek", offset);
+
 #if defined(OS_ANDROID)
   COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit);
   return lseek64(file_.get(), static_cast<off64_t>(offset),
@@ -198,6 +202,8 @@
   if (size < 0)
     return -1;
 
+  SCOPED_FILE_TRACE_WITH_SIZE("Read", size);
+
   int bytes_read = 0;
   int rv;
   do {
@@ -218,6 +224,8 @@
   if (size < 0)
     return -1;
 
+  SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPos", size);
+
   int bytes_read = 0;
   int rv;
   do {
@@ -234,7 +242,7 @@
 int File::ReadNoBestEffort(int64 offset, char* data, int size) {
   ThreadRestrictions::AssertIOAllowed();
   DCHECK(IsValid());
-
+  SCOPED_FILE_TRACE_WITH_SIZE("ReadNoBestEffort", size);
   return HANDLE_EINTR(pread(file_.get(), data, size, offset));
 }
 
@@ -244,6 +252,7 @@
   if (size < 0)
     return -1;
 
+  SCOPED_FILE_TRACE_WITH_SIZE("ReadAtCurrentPosNoBestEffort", size);
   return HANDLE_EINTR(read(file_.get(), data, size));
 }
 
@@ -257,6 +266,8 @@
   if (size < 0)
     return -1;
 
+  SCOPED_FILE_TRACE_WITH_SIZE("Write", size);
+
   int bytes_written = 0;
   int rv;
   do {
@@ -277,6 +288,8 @@
   if (size < 0)
     return -1;
 
+  SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPos", size);
+
   int bytes_written = 0;
   int rv;
   do {
@@ -297,12 +310,15 @@
   if (size < 0)
     return -1;
 
+  SCOPED_FILE_TRACE_WITH_SIZE("WriteAtCurrentPosNoBestEffort", size);
   return HANDLE_EINTR(write(file_.get(), data, size));
 }
 
 int64 File::GetLength() {
   DCHECK(IsValid());
 
+  SCOPED_FILE_TRACE("GetLength");
+
   stat_wrapper_t file_info;
   if (CallFstat(file_.get(), &file_info))
     return false;
@@ -313,6 +329,8 @@
 bool File::SetLength(int64 length) {
   ThreadRestrictions::AssertIOAllowed();
   DCHECK(IsValid());
+
+  SCOPED_FILE_TRACE_WITH_SIZE("SetLength", length);
   return !CallFtruncate(file_.get(), length);
 }
 
@@ -320,6 +338,8 @@
   ThreadRestrictions::AssertIOAllowed();
   DCHECK(IsValid());
 
+  SCOPED_FILE_TRACE("SetTimes");
+
   timeval times[2];
   times[0] = last_access_time.ToTimeVal();
   times[1] = last_modified_time.ToTimeVal();
@@ -330,6 +350,8 @@
 bool File::GetInfo(Info* info) {
   DCHECK(IsValid());
 
+  SCOPED_FILE_TRACE("GetInfo");
+
   stat_wrapper_t file_info;
   if (CallFstat(file_.get(), &file_info))
     return false;
@@ -339,10 +361,12 @@
 }
 
 File::Error File::Lock() {
+  SCOPED_FILE_TRACE("Lock");
   return CallFctnlFlock(file_.get(), true);
 }
 
 File::Error File::Unlock() {
+  SCOPED_FILE_TRACE("Unlock");
   return CallFctnlFlock(file_.get(), false);
 }
 
@@ -350,6 +374,8 @@
   if (!IsValid())
     return File();
 
+  SCOPED_FILE_TRACE("Duplicate");
+
   PlatformFile other_fd = dup(GetPlatformFile());
   if (other_fd == -1)
     return File(OSErrorToFileError(errno));
@@ -442,7 +468,7 @@
 // NaCl doesn't implement system calls to open files directly.
 #if !defined(OS_NACL)
 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
-void File::DoInitialize(const FilePath& name, uint32 flags) {
+void File::DoInitialize(uint32 flags) {
   ThreadRestrictions::AssertIOAllowed();
   DCHECK(!IsValid());
 
@@ -497,7 +523,7 @@
   mode |= S_IRGRP | S_IROTH;
 #endif
 
-  int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
+  int descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode));
 
   if (flags & FLAG_OPEN_ALWAYS) {
     if (descriptor < 0) {
@@ -505,7 +531,7 @@
       if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE)
         open_flags |= O_EXCL;   // together with O_CREAT implies O_NOFOLLOW
 
-      descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
+      descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode));
       if (descriptor >= 0)
         created_ = true;
     }
@@ -520,7 +546,7 @@
     created_ = true;
 
   if (flags & FLAG_DELETE_ON_CLOSE)
-    unlink(name.value().c_str());
+    unlink(path_.value().c_str());
 
   async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
   error_details_ = FILE_OK;
@@ -531,6 +557,7 @@
 bool File::DoFlush() {
   ThreadRestrictions::AssertIOAllowed();
   DCHECK(IsValid());
+
 #if defined(OS_NACL)
   NOTIMPLEMENTED();  // NaCl doesn't implement fsync.
   return true;
diff --git a/base/files/file_tracing.cc b/base/files/file_tracing.cc
new file mode 100644
index 0000000..3326e2d
--- /dev/null
+++ b/base/files/file_tracing.cc
@@ -0,0 +1,47 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/files/file_tracing.h"
+
+#include "base/files/file.h"
+#include "base/files/file_path.h"
+
+namespace base {
+
+const char kFileTracingEventCategoryGroup[] = TRACE_DISABLED_BY_DEFAULT("file");
+
+ScopedFileTrace::ScopedFileTrace() : initialized_(false) {}
+
+ScopedFileTrace::~ScopedFileTrace() {
+  if (initialized_) {
+    if (size_) {
+      TRACE_EVENT_NESTABLE_ASYNC_END2(
+          kFileTracingEventCategoryGroup, name_, &file_->trace_enabler_,
+          "path", file_->path_.AsUTF8Unsafe(), "size", size_);
+    } else {
+      TRACE_EVENT_NESTABLE_ASYNC_END1(
+          kFileTracingEventCategoryGroup, name_, &file_->trace_enabler_,
+          "path", file_->path_.AsUTF8Unsafe());
+    }
+  }
+}
+
+void ScopedFileTrace::Initialize(const char* name, File* file, int64 size) {
+  file_ = file;
+  name_ = name;
+  size_ = size;
+  initialized_ = true;
+
+  if (size_) {
+    TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(
+        kFileTracingEventCategoryGroup, name_, &file_->trace_enabler_,
+        "path", file_->path_.AsUTF8Unsafe(), "size", size_);
+  } else {
+    TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(
+        kFileTracingEventCategoryGroup, name_, &file_->trace_enabler_,
+        "path", file_->path_.AsUTF8Unsafe());
+  }
+}
+
+}  // namespace base
diff --git a/base/files/file_tracing.h b/base/files/file_tracing.h
new file mode 100644
index 0000000..f3bb631
--- /dev/null
+++ b/base/files/file_tracing.h
@@ -0,0 +1,68 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_FILES_FILE_TRACING_H_
+#define BASE_FILES_FILE_TRACING_H_
+
+#include "base/basictypes.h"
+#include "base/macros.h"
+#include "base/trace_event/trace_event.h"
+
+#define FILE_TRACING_PREFIX "File"
+
+#define FILE_TRACING_BEGIN() \
+    TRACE_EVENT_NESTABLE_ASYNC_BEGIN0( \
+        base::kFileTracingEventCategoryGroup, FILE_TRACING_PREFIX, this)
+
+#define FILE_TRACING_END() \
+    TRACE_EVENT_NESTABLE_ASYNC_END0( \
+        base::kFileTracingEventCategoryGroup, FILE_TRACING_PREFIX, this)
+
+#define SCOPED_FILE_TRACE_WITH_SIZE(name, size) \
+    base::ScopedFileTrace scoped_file_trace; \
+    do { \
+      bool category_enabled; \
+      TRACE_EVENT_CATEGORY_GROUP_ENABLED( \
+          base::kFileTracingEventCategoryGroup, &category_enabled); \
+      if (category_enabled) \
+        scoped_file_trace.Initialize( \
+            FILE_TRACING_PREFIX "::" name, this, size); \
+    } while (0)
+
+#define SCOPED_FILE_TRACE(name) SCOPED_FILE_TRACE_WITH_SIZE(name, 0)
+
+namespace base {
+
+class File;
+
+extern const char kFileTracingEventCategoryGroup[];
+
+class ScopedFileTrace {
+ public:
+  ScopedFileTrace();
+  ~ScopedFileTrace();
+
+  // Called only if the tracing category is enabled.
+  void Initialize(const char* event, File* file, int64 size);
+
+ private:
+  // True if |Initialize()| has been called. Don't touch |path_|, |event_|,
+  // or |bytes_| if |initialized_| is false.
+  bool initialized_;
+
+  // The event name to trace (e.g. "Read", "Write"). Prefixed with "base::File".
+  const char* name_;
+
+  // The file being traced. Must outlive this class.
+  File* file_;
+
+  // The size (in bytes) of this trace. Not reported if 0.
+  int64 size_;
+
+  DISALLOW_COPY_AND_ASSIGN(ScopedFileTrace);
+};
+
+}  // namespace base
+
+#endif  // BASE_FILES_FILE_TRACING_H_