Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: base/files/important_file_writer.cc

Issue 2920223002: Add additional histograms with suffixes to ImportantFileWriter. (Closed)
Patch Set: Fix problems pointed by code reviewers. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/files/important_file_writer.h ('k') | base/files/important_file_writer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/important_file_writer.h" 5 #include "base/files/important_file_writer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <stdio.h> 9 #include <stdio.h>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/callback_helpers.h" 14 #include "base/callback_helpers.h"
15 #include "base/critical_closure.h" 15 #include "base/critical_closure.h"
16 #include "base/debug/alias.h" 16 #include "base/debug/alias.h"
17 #include "base/files/file.h" 17 #include "base/files/file.h"
18 #include "base/files/file_path.h" 18 #include "base/files/file_path.h"
19 #include "base/files/file_util.h" 19 #include "base/files/file_util.h"
20 #include "base/logging.h" 20 #include "base/logging.h"
21 #include "base/macros.h" 21 #include "base/macros.h"
22 #include "base/metrics/histogram_functions.h"
22 #include "base/metrics/histogram_macros.h" 23 #include "base/metrics/histogram_macros.h"
23 #include "base/numerics/safe_conversions.h" 24 #include "base/numerics/safe_conversions.h"
24 #include "base/strings/string_number_conversions.h" 25 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_util.h" 26 #include "base/strings/string_util.h"
26 #include "base/task_runner.h" 27 #include "base/task_runner.h"
27 #include "base/task_runner_util.h" 28 #include "base/task_runner_util.h"
28 #include "base/threading/thread.h" 29 #include "base/threading/thread.h"
29 #include "base/time/time.h" 30 #include "base/time/time.h"
30 #include "build/build_config.h" 31 #include "build/build_config.h"
31 32
(...skipping 10 matching lines...) Expand all
42 enum TempFileFailure { 43 enum TempFileFailure {
43 FAILED_CREATING, 44 FAILED_CREATING,
44 FAILED_OPENING, 45 FAILED_OPENING,
45 FAILED_CLOSING, // Unused. 46 FAILED_CLOSING, // Unused.
46 FAILED_WRITING, 47 FAILED_WRITING,
47 FAILED_RENAMING, 48 FAILED_RENAMING,
48 FAILED_FLUSHING, 49 FAILED_FLUSHING,
49 TEMP_FILE_FAILURE_MAX 50 TEMP_FILE_FAILURE_MAX
50 }; 51 };
51 52
52 void LogFailure(const FilePath& path, TempFileFailure failure_code, 53 // Helper function to write samples to a histogram with a dynamically assigned
54 // histogram name. Works with different error code types convertible to int
55 // which is the actual argument type of UmaHistogramExactLinear.
56 template <typename SampleType>
57 typename std::enable_if<std::is_convertible<SampleType, int>::value>::type
58 UmaHistogramExactLinearWithSuffix(const char* histogram_name,
59 const char* histogram_suffix,
60 SampleType add_sample,
61 SampleType max_sample) {
62 DCHECK(histogram_name);
63 auto histogram_full_name_length = strlen(histogram_name);
64 if (histogram_suffix && *histogram_suffix)
65 histogram_full_name_length +=
66 strlen(histogram_suffix) + 1; // Also account for a dot.
dcheng 2017/06/07 22:56:39 To be honest, I don't know that it's worth the eff
xaerox 2017/06/08 05:52:53 I agree that one additional reallocation is worth
67 std::string histogram_full_name;
68 histogram_full_name.reserve(histogram_full_name_length);
69 histogram_full_name.append(histogram_name);
70 if (histogram_suffix && *histogram_suffix) {
71 histogram_full_name.append(".");
72 histogram_full_name.append(histogram_suffix);
73 }
74 UmaHistogramExactLinear(histogram_full_name, static_cast<int>(add_sample),
75 static_cast<int>(max_sample));
76 }
77
78 void LogFailure(const FilePath& path,
79 StringPiece histogram_suffix,
80 TempFileFailure failure_code,
53 StringPiece message) { 81 StringPiece message) {
54 UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code, 82 UmaHistogramExactLinearWithSuffix("ImportantFile.TempFileFailures",
55 TEMP_FILE_FAILURE_MAX); 83 histogram_suffix.data(), failure_code,
84 TEMP_FILE_FAILURE_MAX);
56 DPLOG(WARNING) << "temp file failure: " << path.value() << " : " << message; 85 DPLOG(WARNING) << "temp file failure: " << path.value() << " : " << message;
57 } 86 }
58 87
59 // Helper function to call WriteFileAtomically() with a 88 // Helper function to call WriteFileAtomically() with a
60 // std::unique_ptr<std::string>. 89 // std::unique_ptr<std::string>.
61 void WriteScopedStringToFileAtomically( 90 void WriteScopedStringToFileAtomically(
62 const FilePath& path, 91 const FilePath& path,
63 std::unique_ptr<std::string> data, 92 std::unique_ptr<std::string> data,
64 Closure before_write_callback, 93 Closure before_write_callback,
65 Callback<void(bool success)> after_write_callback) { 94 Callback<void(bool success)> after_write_callback,
95 const std::string& histogram_suffix) {
66 if (!before_write_callback.is_null()) 96 if (!before_write_callback.is_null())
67 before_write_callback.Run(); 97 before_write_callback.Run();
68 98
69 bool result = ImportantFileWriter::WriteFileAtomically(path, *data); 99 bool result = ImportantFileWriter::WriteFileAtomically(
100 path, *data, histogram_suffix.c_str());
dcheng 2017/06/07 22:56:39 No need to call c_str() for this
xaerox 2017/06/08 05:52:53 Done.
70 101
71 if (!after_write_callback.is_null()) 102 if (!after_write_callback.is_null())
72 after_write_callback.Run(result); 103 after_write_callback.Run(result);
73 } 104 }
74 105
106 base::File::Error GetLastFileError() {
107 #if defined(OS_WIN)
108 return base::File::OSErrorToFileError(::GetLastError());
109 #elif defined(OS_POSIX)
110 return base::File::OSErrorToFileError(errno);
111 #else
112 return base::File::FILE_OK;
113 #endif
114 }
115
116 void DeleteTmpFile(const FilePath& tmp_file_path,
117 StringPiece histogram_suffix) {
118 if (!DeleteFile(tmp_file_path, false)) {
119 UmaHistogramExactLinearWithSuffix(
120 "ImportantFile.FileDeleteError", histogram_suffix.data(),
121 -GetLastFileError(), -base::File::FILE_ERROR_MAX);
122 }
123 }
124
75 } // namespace 125 } // namespace
76 126
77 // static 127 // static
78 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, 128 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path,
79 StringPiece data) { 129 StringPiece data,
130 StringPiece histogram_suffix) {
80 #if defined(OS_CHROMEOS) 131 #if defined(OS_CHROMEOS)
81 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly, 132 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly,
82 // and this function seems to be one of the slowest shutdown steps. 133 // and this function seems to be one of the slowest shutdown steps.
83 // Include some info to the report for investigation. crbug.com/418627 134 // Include some info to the report for investigation. crbug.com/418627
84 // TODO(hashimoto): Remove this. 135 // TODO(hashimoto): Remove this.
85 struct { 136 struct {
86 size_t data_size; 137 size_t data_size;
87 char path[128]; 138 char path[128];
88 } file_info; 139 } file_info;
89 file_info.data_size = data.size(); 140 file_info.data_size = data.size();
90 strlcpy(file_info.path, path.value().c_str(), arraysize(file_info.path)); 141 strlcpy(file_info.path, path.value().c_str(), arraysize(file_info.path));
91 debug::Alias(&file_info); 142 debug::Alias(&file_info);
92 #endif 143 #endif
93 144
94 // Write the data to a temp file then rename to avoid data loss if we crash 145 // Write the data to a temp file then rename to avoid data loss if we crash
95 // while writing the file. Ensure that the temp file is on the same volume 146 // while writing the file. Ensure that the temp file is on the same volume
96 // as target file, so it can be moved in one step, and that the temp file 147 // as target file, so it can be moved in one step, and that the temp file
97 // is securely created. 148 // is securely created.
98 FilePath tmp_file_path; 149 FilePath tmp_file_path;
99 if (!CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) { 150 if (!CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) {
100 LogFailure(path, FAILED_CREATING, "could not create temporary file"); 151 UmaHistogramExactLinearWithSuffix(
152 "ImportantFile.FileCreateError", histogram_suffix.data(),
153 -GetLastFileError(), -base::File::FILE_ERROR_MAX);
154 LogFailure(path, histogram_suffix, FAILED_CREATING,
155 "could not create temporary file");
101 return false; 156 return false;
102 } 157 }
103 158
104 File tmp_file(tmp_file_path, File::FLAG_OPEN | File::FLAG_WRITE); 159 File tmp_file(tmp_file_path, File::FLAG_OPEN | File::FLAG_WRITE);
105 if (!tmp_file.IsValid()) { 160 if (!tmp_file.IsValid()) {
106 LogFailure(path, FAILED_OPENING, "could not open temporary file"); 161 UmaHistogramExactLinearWithSuffix(
162 "ImportantFile.FileOpenError", histogram_suffix.data(),
163 -tmp_file.error_details(), -base::File::FILE_ERROR_MAX);
164 LogFailure(path, histogram_suffix, FAILED_OPENING,
165 "could not open temporary file");
107 DeleteFile(tmp_file_path, false); 166 DeleteFile(tmp_file_path, false);
108 return false; 167 return false;
109 } 168 }
110 169
111 // If this fails in the wild, something really bad is going on. 170 // If this fails in the wild, something really bad is going on.
112 const int data_length = checked_cast<int32_t>(data.length()); 171 const int data_length = checked_cast<int32_t>(data.length());
113 int bytes_written = tmp_file.Write(0, data.data(), data_length); 172 int bytes_written = tmp_file.Write(0, data.data(), data_length);
173 if (bytes_written < data_length) {
174 UmaHistogramExactLinearWithSuffix(
175 "ImportantFile.FileWriteError", histogram_suffix.data(),
176 -GetLastFileError(), -base::File::FILE_ERROR_MAX);
177 }
114 bool flush_success = tmp_file.Flush(); 178 bool flush_success = tmp_file.Flush();
115 tmp_file.Close(); 179 tmp_file.Close();
116 180
117 if (bytes_written < data_length) { 181 if (bytes_written < data_length) {
118 LogFailure(path, FAILED_WRITING, "error writing, bytes_written=" + 182 LogFailure(path, histogram_suffix, FAILED_WRITING,
119 IntToString(bytes_written)); 183 "error writing, bytes_written=" + IntToString(bytes_written));
120 DeleteFile(tmp_file_path, false); 184 DeleteTmpFile(tmp_file_path, histogram_suffix.data());
121 return false; 185 return false;
122 } 186 }
123 187
124 if (!flush_success) { 188 if (!flush_success) {
125 LogFailure(path, FAILED_FLUSHING, "error flushing"); 189 LogFailure(path, histogram_suffix, FAILED_FLUSHING, "error flushing");
126 DeleteFile(tmp_file_path, false); 190 DeleteTmpFile(tmp_file_path, histogram_suffix);
127 return false; 191 return false;
128 } 192 }
129 193
130 if (!ReplaceFile(tmp_file_path, path, nullptr)) { 194 base::File::Error replace_file_error = base::File::FILE_OK;
131 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); 195 if (!ReplaceFile(tmp_file_path, path, &replace_file_error)) {
132 DeleteFile(tmp_file_path, false); 196 UmaHistogramExactLinearWithSuffix(
197 "ImportantFile.FileRenameError", histogram_suffix.data(),
198 -replace_file_error, -base::File::FILE_ERROR_MAX);
199 LogFailure(path, histogram_suffix, FAILED_RENAMING,
200 "could not rename temporary file");
201 DeleteTmpFile(tmp_file_path, histogram_suffix);
133 return false; 202 return false;
134 } 203 }
135 204
136 return true; 205 return true;
137 } 206 }
138 207
139 ImportantFileWriter::ImportantFileWriter( 208 ImportantFileWriter::ImportantFileWriter(
140 const FilePath& path, 209 const FilePath& path,
141 scoped_refptr<SequencedTaskRunner> task_runner)
142 : ImportantFileWriter(path,
143 std::move(task_runner),
144 kDefaultCommitInterval) {}
145
146 ImportantFileWriter::ImportantFileWriter(
147 const FilePath& path,
148 scoped_refptr<SequencedTaskRunner> task_runner, 210 scoped_refptr<SequencedTaskRunner> task_runner,
149 TimeDelta interval) 211 StringPiece histogram_suffix)
212 : ImportantFileWriter(path,
213 std::move(task_runner),
214 kDefaultCommitInterval,
215 histogram_suffix) {}
216
217 ImportantFileWriter::ImportantFileWriter(
218 const FilePath& path,
219 scoped_refptr<SequencedTaskRunner> task_runner,
220 TimeDelta interval,
221 StringPiece histogram_suffix)
150 : path_(path), 222 : path_(path),
151 task_runner_(std::move(task_runner)), 223 task_runner_(std::move(task_runner)),
152 serializer_(nullptr), 224 serializer_(nullptr),
153 commit_interval_(interval), 225 commit_interval_(interval),
226 histogram_suffix_(histogram_suffix),
154 weak_factory_(this) { 227 weak_factory_(this) {
155 DCHECK(task_runner_); 228 DCHECK(task_runner_);
156 } 229 }
157 230
158 ImportantFileWriter::~ImportantFileWriter() { 231 ImportantFileWriter::~ImportantFileWriter() {
159 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 232 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
160 // We're usually a member variable of some other object, which also tends 233 // We're usually a member variable of some other object, which also tends
161 // to be our serializer. It may not be safe to call back to the parent object 234 // to be our serializer. It may not be safe to call back to the parent object
162 // being destructed. 235 // being destructed.
163 DCHECK(!HasPendingWrite()); 236 DCHECK(!HasPendingWrite());
164 } 237 }
165 238
166 bool ImportantFileWriter::HasPendingWrite() const { 239 bool ImportantFileWriter::HasPendingWrite() const {
167 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 240 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
168 return timer().IsRunning(); 241 return timer().IsRunning();
169 } 242 }
170 243
171 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) { 244 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) {
172 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 245 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
173 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { 246 if (!IsValueInRangeForNumericType<int32_t>(data->length())) {
174 NOTREACHED(); 247 NOTREACHED();
175 return; 248 return;
176 } 249 }
177 250
178 Closure task = AdaptCallbackForRepeating( 251 Closure task = AdaptCallbackForRepeating(
179 BindOnce(&WriteScopedStringToFileAtomically, path_, std::move(data), 252 BindOnce(&WriteScopedStringToFileAtomically, path_, std::move(data),
180 std::move(before_next_write_callback_), 253 std::move(before_next_write_callback_),
181 std::move(after_next_write_callback_))); 254 std::move(after_next_write_callback_), histogram_suffix_));
182 255
183 if (!task_runner_->PostTask(FROM_HERE, MakeCriticalClosure(task))) { 256 if (!task_runner_->PostTask(FROM_HERE, MakeCriticalClosure(task))) {
184 // Posting the task to background message loop is not expected 257 // Posting the task to background message loop is not expected
185 // to fail, but if it does, avoid losing data and just hit the disk 258 // to fail, but if it does, avoid losing data and just hit the disk
186 // on the current thread. 259 // on the current thread.
187 NOTREACHED(); 260 NOTREACHED();
188 261
189 task.Run(); 262 task.Run();
190 } 263 }
191 ClearPendingWrite(); 264 ClearPendingWrite();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 void ImportantFileWriter::ClearPendingWrite() { 299 void ImportantFileWriter::ClearPendingWrite() {
227 timer().Stop(); 300 timer().Stop();
228 serializer_ = nullptr; 301 serializer_ = nullptr;
229 } 302 }
230 303
231 void ImportantFileWriter::SetTimerForTesting(Timer* timer_override) { 304 void ImportantFileWriter::SetTimerForTesting(Timer* timer_override) {
232 timer_override_ = timer_override; 305 timer_override_ = timer_override;
233 } 306 }
234 307
235 } // namespace base 308 } // namespace base
OLDNEW
« no previous file with comments | « base/files/important_file_writer.h ('k') | base/files/important_file_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698