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

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

Issue 2920223002: Add additional histograms with suffixes to ImportantFileWriter. (Closed)
Patch Set: Fix more 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
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 void UmaHistogramExactLinearWithSuffix(const char* histogram_name,
58 const char* histogram_suffix,
dcheng 2017/06/14 19:56:13 And histogram_suffix would use const std::string&
xaerox 2017/06/19 06:39:41 Done.
59 SampleType add_sample,
60 SampleType max_sample) {
61 static_assert(std::is_convertible<SampleType, int>::value,
62 "SampleType should be convertible to int");
63 DCHECK(histogram_name);
64 std::string histogram_full_name(histogram_name);
65 if (histogram_suffix && *histogram_suffix) {
66 histogram_full_name.append(".");
67 histogram_full_name.append(histogram_suffix);
68 }
69 UmaHistogramExactLinear(histogram_full_name, static_cast<int>(add_sample),
70 static_cast<int>(max_sample));
71 }
72
73 void LogFailure(const FilePath& path,
74 StringPiece histogram_suffix,
75 TempFileFailure failure_code,
53 StringPiece message) { 76 StringPiece message) {
54 UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code, 77 UmaHistogramExactLinearWithSuffix("ImportantFile.TempFileFailures",
55 TEMP_FILE_FAILURE_MAX); 78 histogram_suffix.data(), failure_code,
79 TEMP_FILE_FAILURE_MAX);
56 DPLOG(WARNING) << "temp file failure: " << path.value() << " : " << message; 80 DPLOG(WARNING) << "temp file failure: " << path.value() << " : " << message;
57 } 81 }
58 82
59 // Helper function to call WriteFileAtomically() with a 83 // Helper function to call WriteFileAtomically() with a
60 // std::unique_ptr<std::string>. 84 // std::unique_ptr<std::string>.
61 void WriteScopedStringToFileAtomically( 85 void WriteScopedStringToFileAtomically(
62 const FilePath& path, 86 const FilePath& path,
63 std::unique_ptr<std::string> data, 87 std::unique_ptr<std::string> data,
64 Closure before_write_callback, 88 Closure before_write_callback,
65 Callback<void(bool success)> after_write_callback) { 89 Callback<void(bool success)> after_write_callback,
90 const std::string& histogram_suffix) {
66 if (!before_write_callback.is_null()) 91 if (!before_write_callback.is_null())
67 before_write_callback.Run(); 92 before_write_callback.Run();
68 93
69 bool result = ImportantFileWriter::WriteFileAtomically(path, *data); 94 bool result =
95 ImportantFileWriter::WriteFileAtomically(path, *data, histogram_suffix);
70 96
71 if (!after_write_callback.is_null()) 97 if (!after_write_callback.is_null())
72 after_write_callback.Run(result); 98 after_write_callback.Run(result);
73 } 99 }
74 100
101 base::File::Error GetLastFileError() {
102 #if defined(OS_WIN)
103 return base::File::OSErrorToFileError(::GetLastError());
104 #elif defined(OS_POSIX)
105 return base::File::OSErrorToFileError(errno);
106 #else
107 return base::File::FILE_OK;
108 #endif
109 }
110
111 void DeleteTmpFile(const FilePath& tmp_file_path,
112 StringPiece histogram_suffix) {
113 if (!DeleteFile(tmp_file_path, false)) {
114 UmaHistogramExactLinearWithSuffix(
115 "ImportantFile.FileDeleteError", histogram_suffix.data(),
116 -GetLastFileError(), -base::File::FILE_ERROR_MAX);
117 }
118 }
119
75 } // namespace 120 } // namespace
76 121
77 // static 122 // static
78 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path, 123 bool ImportantFileWriter::WriteFileAtomically(const FilePath& path,
79 StringPiece data) { 124 StringPiece data,
125 StringPiece histogram_suffix) {
80 #if defined(OS_CHROMEOS) 126 #if defined(OS_CHROMEOS)
81 // On Chrome OS, chrome gets killed when it cannot finish shutdown quickly, 127 // 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. 128 // and this function seems to be one of the slowest shutdown steps.
83 // Include some info to the report for investigation. crbug.com/418627 129 // Include some info to the report for investigation. crbug.com/418627
84 // TODO(hashimoto): Remove this. 130 // TODO(hashimoto): Remove this.
85 struct { 131 struct {
86 size_t data_size; 132 size_t data_size;
87 char path[128]; 133 char path[128];
88 } file_info; 134 } file_info;
89 file_info.data_size = data.size(); 135 file_info.data_size = data.size();
90 strlcpy(file_info.path, path.value().c_str(), arraysize(file_info.path)); 136 strlcpy(file_info.path, path.value().c_str(), arraysize(file_info.path));
91 debug::Alias(&file_info); 137 debug::Alias(&file_info);
92 #endif 138 #endif
93 139
94 // Write the data to a temp file then rename to avoid data loss if we crash 140 // 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 141 // 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 142 // as target file, so it can be moved in one step, and that the temp file
97 // is securely created. 143 // is securely created.
98 FilePath tmp_file_path; 144 FilePath tmp_file_path;
99 if (!CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) { 145 if (!CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) {
100 LogFailure(path, FAILED_CREATING, "could not create temporary file"); 146 UmaHistogramExactLinearWithSuffix(
147 "ImportantFile.FileCreateError", histogram_suffix.data(),
148 -GetLastFileError(), -base::File::FILE_ERROR_MAX);
149 LogFailure(path, histogram_suffix, FAILED_CREATING,
150 "could not create temporary file");
101 return false; 151 return false;
102 } 152 }
103 153
104 File tmp_file(tmp_file_path, File::FLAG_OPEN | File::FLAG_WRITE); 154 File tmp_file(tmp_file_path, File::FLAG_OPEN | File::FLAG_WRITE);
105 if (!tmp_file.IsValid()) { 155 if (!tmp_file.IsValid()) {
106 LogFailure(path, FAILED_OPENING, "could not open temporary file"); 156 UmaHistogramExactLinearWithSuffix(
157 "ImportantFile.FileOpenError", histogram_suffix.data(),
158 -tmp_file.error_details(), -base::File::FILE_ERROR_MAX);
159 LogFailure(path, histogram_suffix, FAILED_OPENING,
160 "could not open temporary file");
107 DeleteFile(tmp_file_path, false); 161 DeleteFile(tmp_file_path, false);
108 return false; 162 return false;
109 } 163 }
110 164
111 // If this fails in the wild, something really bad is going on. 165 // If this fails in the wild, something really bad is going on.
112 const int data_length = checked_cast<int32_t>(data.length()); 166 const int data_length = checked_cast<int32_t>(data.length());
113 int bytes_written = tmp_file.Write(0, data.data(), data_length); 167 int bytes_written = tmp_file.Write(0, data.data(), data_length);
168 if (bytes_written < data_length) {
169 UmaHistogramExactLinearWithSuffix(
170 "ImportantFile.FileWriteError", histogram_suffix.data(),
171 -GetLastFileError(), -base::File::FILE_ERROR_MAX);
172 }
114 bool flush_success = tmp_file.Flush(); 173 bool flush_success = tmp_file.Flush();
115 tmp_file.Close(); 174 tmp_file.Close();
116 175
117 if (bytes_written < data_length) { 176 if (bytes_written < data_length) {
118 LogFailure(path, FAILED_WRITING, "error writing, bytes_written=" + 177 LogFailure(path, histogram_suffix, FAILED_WRITING,
119 IntToString(bytes_written)); 178 "error writing, bytes_written=" + IntToString(bytes_written));
120 DeleteFile(tmp_file_path, false); 179 DeleteTmpFile(tmp_file_path, histogram_suffix.data());
121 return false; 180 return false;
122 } 181 }
123 182
124 if (!flush_success) { 183 if (!flush_success) {
125 LogFailure(path, FAILED_FLUSHING, "error flushing"); 184 LogFailure(path, histogram_suffix, FAILED_FLUSHING, "error flushing");
126 DeleteFile(tmp_file_path, false); 185 DeleteTmpFile(tmp_file_path, histogram_suffix);
127 return false; 186 return false;
128 } 187 }
129 188
130 if (!ReplaceFile(tmp_file_path, path, nullptr)) { 189 base::File::Error replace_file_error = base::File::FILE_OK;
131 LogFailure(path, FAILED_RENAMING, "could not rename temporary file"); 190 if (!ReplaceFile(tmp_file_path, path, &replace_file_error)) {
132 DeleteFile(tmp_file_path, false); 191 UmaHistogramExactLinearWithSuffix(
192 "ImportantFile.FileRenameError", histogram_suffix.data(),
193 -replace_file_error, -base::File::FILE_ERROR_MAX);
194 LogFailure(path, histogram_suffix, FAILED_RENAMING,
195 "could not rename temporary file");
196 DeleteTmpFile(tmp_file_path, histogram_suffix);
133 return false; 197 return false;
134 } 198 }
135 199
136 return true; 200 return true;
137 } 201 }
138 202
139 ImportantFileWriter::ImportantFileWriter( 203 ImportantFileWriter::ImportantFileWriter(
140 const FilePath& path, 204 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, 205 scoped_refptr<SequencedTaskRunner> task_runner,
149 TimeDelta interval) 206 StringPiece histogram_suffix)
207 : ImportantFileWriter(path,
208 std::move(task_runner),
209 kDefaultCommitInterval,
210 histogram_suffix) {}
211
212 ImportantFileWriter::ImportantFileWriter(
213 const FilePath& path,
214 scoped_refptr<SequencedTaskRunner> task_runner,
215 TimeDelta interval,
216 StringPiece histogram_suffix)
150 : path_(path), 217 : path_(path),
151 task_runner_(std::move(task_runner)), 218 task_runner_(std::move(task_runner)),
152 serializer_(nullptr), 219 serializer_(nullptr),
153 commit_interval_(interval), 220 commit_interval_(interval),
221 histogram_suffix_(histogram_suffix),
154 weak_factory_(this) { 222 weak_factory_(this) {
155 DCHECK(task_runner_); 223 DCHECK(task_runner_);
156 } 224 }
157 225
158 ImportantFileWriter::~ImportantFileWriter() { 226 ImportantFileWriter::~ImportantFileWriter() {
159 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 227 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
160 // We're usually a member variable of some other object, which also tends 228 // 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 229 // to be our serializer. It may not be safe to call back to the parent object
162 // being destructed. 230 // being destructed.
163 DCHECK(!HasPendingWrite()); 231 DCHECK(!HasPendingWrite());
164 } 232 }
165 233
166 bool ImportantFileWriter::HasPendingWrite() const { 234 bool ImportantFileWriter::HasPendingWrite() const {
167 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 235 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
168 return timer().IsRunning(); 236 return timer().IsRunning();
169 } 237 }
170 238
171 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) { 239 void ImportantFileWriter::WriteNow(std::unique_ptr<std::string> data) {
172 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 240 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
173 if (!IsValueInRangeForNumericType<int32_t>(data->length())) { 241 if (!IsValueInRangeForNumericType<int32_t>(data->length())) {
174 NOTREACHED(); 242 NOTREACHED();
175 return; 243 return;
176 } 244 }
177 245
178 Closure task = AdaptCallbackForRepeating( 246 Closure task = AdaptCallbackForRepeating(
179 BindOnce(&WriteScopedStringToFileAtomically, path_, std::move(data), 247 BindOnce(&WriteScopedStringToFileAtomically, path_, std::move(data),
180 std::move(before_next_write_callback_), 248 std::move(before_next_write_callback_),
181 std::move(after_next_write_callback_))); 249 std::move(after_next_write_callback_), histogram_suffix_));
182 250
183 if (!task_runner_->PostTask(FROM_HERE, MakeCriticalClosure(task))) { 251 if (!task_runner_->PostTask(FROM_HERE, MakeCriticalClosure(task))) {
184 // Posting the task to background message loop is not expected 252 // 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 253 // to fail, but if it does, avoid losing data and just hit the disk
186 // on the current thread. 254 // on the current thread.
187 NOTREACHED(); 255 NOTREACHED();
188 256
189 task.Run(); 257 task.Run();
190 } 258 }
191 ClearPendingWrite(); 259 ClearPendingWrite();
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 void ImportantFileWriter::ClearPendingWrite() { 294 void ImportantFileWriter::ClearPendingWrite() {
227 timer().Stop(); 295 timer().Stop();
228 serializer_ = nullptr; 296 serializer_ = nullptr;
229 } 297 }
230 298
231 void ImportantFileWriter::SetTimerForTesting(Timer* timer_override) { 299 void ImportantFileWriter::SetTimerForTesting(Timer* timer_override) {
232 timer_override_ = timer_override; 300 timer_override_ = timer_override;
233 } 301 }
234 302
235 } // namespace base 303 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698