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

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

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