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

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

Issue 1072133006: Add granular file tracing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@do-initialize
Patch Set: take 2 Created 5 years, 8 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/file.h" 5 #include "base/files/file.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <unistd.h> 10 #include <unistd.h>
11 11
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/metrics/sparse_histogram.h" 14 #include "base/metrics/sparse_histogram.h"
15 #include "base/posix/eintr_wrapper.h" 15 #include "base/posix/eintr_wrapper.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/thread_restrictions.h" 17 #include "base/threading/thread_restrictions.h"
18 #include "base/trace_event/trace_event.h"
18 19
19 #if defined(OS_ANDROID) 20 #if defined(OS_ANDROID)
20 #include "base/os_compat_android.h" 21 #include "base/os_compat_android.h"
21 #endif 22 #endif
22 23
23 namespace base { 24 namespace base {
24 25
25 // Make sure our Whence mappings match the system headers. 26 // Make sure our Whence mappings match the system headers.
26 COMPILE_ASSERT(File::FROM_BEGIN == SEEK_SET && 27 COMPILE_ASSERT(File::FROM_BEGIN == SEEK_SET &&
27 File::FROM_CURRENT == SEEK_CUR && 28 File::FROM_CURRENT == SEEK_CUR &&
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 168
168 PlatformFile File::TakePlatformFile() { 169 PlatformFile File::TakePlatformFile() {
169 return file_.release(); 170 return file_.release();
170 } 171 }
171 172
172 void File::Close() { 173 void File::Close() {
173 if (!IsValid()) 174 if (!IsValid())
174 return; 175 return;
175 176
176 ThreadRestrictions::AssertIOAllowed(); 177 ThreadRestrictions::AssertIOAllowed();
178 TRACE_EVENT1(kTraceGroup, "Close", "path", path_.AsUTF8Unsafe());
177 file_.reset(); 179 file_.reset();
178 } 180 }
179 181
180 int64 File::Seek(Whence whence, int64 offset) { 182 int64 File::Seek(Whence whence, int64 offset) {
181 ThreadRestrictions::AssertIOAllowed(); 183 ThreadRestrictions::AssertIOAllowed();
182 DCHECK(IsValid()); 184 DCHECK(IsValid());
185 TRACE_EVENT1(kTraceGroup, "Seek", "path", path_.AsUTF8Unsafe())
183 186
184 #if defined(OS_ANDROID) 187 #if defined(OS_ANDROID)
185 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit); 188 COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit);
186 return lseek64(file_.get(), static_cast<off64_t>(offset), 189 return lseek64(file_.get(), static_cast<off64_t>(offset),
187 static_cast<int>(whence)); 190 static_cast<int>(whence));
188 #else 191 #else
189 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit); 192 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit);
190 return lseek(file_.get(), static_cast<off_t>(offset), 193 return lseek(file_.get(), static_cast<off_t>(offset),
191 static_cast<int>(whence)); 194 static_cast<int>(whence));
192 #endif 195 #endif
193 } 196 }
194 197
195 int File::Read(int64 offset, char* data, int size) { 198 int File::Read(int64 offset, char* data, int size) {
196 ThreadRestrictions::AssertIOAllowed(); 199 ThreadRestrictions::AssertIOAllowed();
197 DCHECK(IsValid()); 200 DCHECK(IsValid());
198 if (size < 0) 201 if (size < 0)
199 return -1; 202 return -1;
200 203
204 TRACE_EVENT2(kTraceGroup, "Read", "path", path_.AsUTF8Unsafe(), "size", size);
groby-ooo-7-16 2015/04/28 20:26:55 Given that you call this every read/seek/etc, migh
Dan Beam 2015/04/29 02:48:03 this is only called while tracing (in which case a
205
201 int bytes_read = 0; 206 int bytes_read = 0;
202 int rv; 207 int rv;
203 do { 208 do {
204 rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read, 209 rv = HANDLE_EINTR(pread(file_.get(), data + bytes_read,
205 size - bytes_read, offset + bytes_read)); 210 size - bytes_read, offset + bytes_read));
206 if (rv <= 0) 211 if (rv <= 0)
207 break; 212 break;
208 213
209 bytes_read += rv; 214 bytes_read += rv;
210 } while (bytes_read < size); 215 } while (bytes_read < size);
211 216
212 return bytes_read ? bytes_read : rv; 217 return bytes_read ? bytes_read : rv;
213 } 218 }
214 219
215 int File::ReadAtCurrentPos(char* data, int size) { 220 int File::ReadAtCurrentPos(char* data, int size) {
216 ThreadRestrictions::AssertIOAllowed(); 221 ThreadRestrictions::AssertIOAllowed();
217 DCHECK(IsValid()); 222 DCHECK(IsValid());
218 if (size < 0) 223 if (size < 0)
219 return -1; 224 return -1;
220 225
226 TRACE_EVENT2(kTraceGroup, "ReadAtCurrentPost", "path", path_.AsUTF8Unsafe(),
227 "size", size);
228
221 int bytes_read = 0; 229 int bytes_read = 0;
222 int rv; 230 int rv;
223 do { 231 do {
224 rv = HANDLE_EINTR(read(file_.get(), data + bytes_read, size - bytes_read)); 232 rv = HANDLE_EINTR(read(file_.get(), data + bytes_read, size - bytes_read));
225 if (rv <= 0) 233 if (rv <= 0)
226 break; 234 break;
227 235
228 bytes_read += rv; 236 bytes_read += rv;
229 } while (bytes_read < size); 237 } while (bytes_read < size);
230 238
231 return bytes_read ? bytes_read : rv; 239 return bytes_read ? bytes_read : rv;
232 } 240 }
233 241
234 int File::ReadNoBestEffort(int64 offset, char* data, int size) { 242 int File::ReadNoBestEffort(int64 offset, char* data, int size) {
235 ThreadRestrictions::AssertIOAllowed(); 243 ThreadRestrictions::AssertIOAllowed();
236 DCHECK(IsValid()); 244 DCHECK(IsValid());
237 245 TRACE_EVENT2(kTraceGroup, "ReadNoBestEffort", "path", path_.AsUTF8Unsafe(),
246 "size", size);
238 return HANDLE_EINTR(pread(file_.get(), data, size, offset)); 247 return HANDLE_EINTR(pread(file_.get(), data, size, offset));
239 } 248 }
240 249
241 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) { 250 int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {
242 ThreadRestrictions::AssertIOAllowed(); 251 ThreadRestrictions::AssertIOAllowed();
243 DCHECK(IsValid()); 252 DCHECK(IsValid());
244 if (size < 0) 253 if (size < 0)
245 return -1; 254 return -1;
246 255
256 TRACE_EVENT2(kTraceGroup, "ReadAtCurrentPosNoBestEffort",
257 "path", path_.AsUTF8Unsafe(), "size", size);
247 return HANDLE_EINTR(read(file_.get(), data, size)); 258 return HANDLE_EINTR(read(file_.get(), data, size));
248 } 259 }
249 260
250 int File::Write(int64 offset, const char* data, int size) { 261 int File::Write(int64 offset, const char* data, int size) {
251 ThreadRestrictions::AssertIOAllowed(); 262 ThreadRestrictions::AssertIOAllowed();
252 263
253 if (IsOpenAppend(file_.get())) 264 if (IsOpenAppend(file_.get()))
254 return WriteAtCurrentPos(data, size); 265 return WriteAtCurrentPos(data, size);
255 266
256 DCHECK(IsValid()); 267 DCHECK(IsValid());
257 if (size < 0) 268 if (size < 0)
258 return -1; 269 return -1;
259 270
271 TRACE_EVENT2(kTraceGroup, "Write", "path", path_.AsUTF8Unsafe(),
272 "size", size);
273
260 int bytes_written = 0; 274 int bytes_written = 0;
261 int rv; 275 int rv;
262 do { 276 do {
263 rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written, 277 rv = HANDLE_EINTR(pwrite(file_.get(), data + bytes_written,
264 size - bytes_written, offset + bytes_written)); 278 size - bytes_written, offset + bytes_written));
265 if (rv <= 0) 279 if (rv <= 0)
266 break; 280 break;
267 281
268 bytes_written += rv; 282 bytes_written += rv;
269 } while (bytes_written < size); 283 } while (bytes_written < size);
270 284
271 return bytes_written ? bytes_written : rv; 285 return bytes_written ? bytes_written : rv;
272 } 286 }
273 287
274 int File::WriteAtCurrentPos(const char* data, int size) { 288 int File::WriteAtCurrentPos(const char* data, int size) {
275 ThreadRestrictions::AssertIOAllowed(); 289 ThreadRestrictions::AssertIOAllowed();
276 DCHECK(IsValid()); 290 DCHECK(IsValid());
277 if (size < 0) 291 if (size < 0)
278 return -1; 292 return -1;
279 293
294 TRACE_EVENT2(kTraceGroup, "WriteAtCurrentPos", "path", path_.AsUTF8Unsafe(),
295 "size", size);
296
280 int bytes_written = 0; 297 int bytes_written = 0;
281 int rv; 298 int rv;
282 do { 299 do {
283 rv = HANDLE_EINTR(write(file_.get(), data + bytes_written, 300 rv = HANDLE_EINTR(write(file_.get(), data + bytes_written,
284 size - bytes_written)); 301 size - bytes_written));
285 if (rv <= 0) 302 if (rv <= 0)
286 break; 303 break;
287 304
288 bytes_written += rv; 305 bytes_written += rv;
289 } while (bytes_written < size); 306 } while (bytes_written < size);
290 307
291 return bytes_written ? bytes_written : rv; 308 return bytes_written ? bytes_written : rv;
292 } 309 }
293 310
294 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) { 311 int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) {
295 ThreadRestrictions::AssertIOAllowed(); 312 ThreadRestrictions::AssertIOAllowed();
296 DCHECK(IsValid()); 313 DCHECK(IsValid());
297 if (size < 0) 314 if (size < 0)
298 return -1; 315 return -1;
299 316
317 TRACE_EVENT2(kTraceGroup, "WriteAtCurrentPosNoBestEffort",
318 "path", path_.AsUTF8Unsafe(), "size", size);
300 return HANDLE_EINTR(write(file_.get(), data, size)); 319 return HANDLE_EINTR(write(file_.get(), data, size));
301 } 320 }
302 321
303 int64 File::GetLength() { 322 int64 File::GetLength() {
304 DCHECK(IsValid()); 323 DCHECK(IsValid());
305 324
325 TRACE_EVENT1(kTraceGroup, "GetLength", "path", path_.AsUTF8Unsafe())
326
306 stat_wrapper_t file_info; 327 stat_wrapper_t file_info;
307 if (CallFstat(file_.get(), &file_info)) 328 if (CallFstat(file_.get(), &file_info))
308 return false; 329 return false;
309 330
310 return file_info.st_size; 331 return file_info.st_size;
311 } 332 }
312 333
313 bool File::SetLength(int64 length) { 334 bool File::SetLength(int64 length) {
314 ThreadRestrictions::AssertIOAllowed(); 335 ThreadRestrictions::AssertIOAllowed();
315 DCHECK(IsValid()); 336 DCHECK(IsValid());
337
338 TRACE_EVENT2(kTraceGroup, "SetLength", "path", path_.AsUTF8Unsafe(),
339 "length", length);
340
316 return !CallFtruncate(file_.get(), length); 341 return !CallFtruncate(file_.get(), length);
317 } 342 }
318 343
319 bool File::SetTimes(Time last_access_time, Time last_modified_time) { 344 bool File::SetTimes(Time last_access_time, Time last_modified_time) {
320 ThreadRestrictions::AssertIOAllowed(); 345 ThreadRestrictions::AssertIOAllowed();
321 DCHECK(IsValid()); 346 DCHECK(IsValid());
322 347
348 TRACE_EVENT1(kTraceGroup, "SetTimes", "path", path_.AsUTF8Unsafe())
349
323 timeval times[2]; 350 timeval times[2];
324 times[0] = last_access_time.ToTimeVal(); 351 times[0] = last_access_time.ToTimeVal();
325 times[1] = last_modified_time.ToTimeVal(); 352 times[1] = last_modified_time.ToTimeVal();
326 353
327 return !CallFutimes(file_.get(), times); 354 return !CallFutimes(file_.get(), times);
328 } 355 }
329 356
330 bool File::GetInfo(Info* info) { 357 bool File::GetInfo(Info* info) {
331 DCHECK(IsValid()); 358 DCHECK(IsValid());
332 359
360 TRACE_EVENT1(kTraceGroup, "GetInfo", "path", path_.AsUTF8Unsafe())
361
333 stat_wrapper_t file_info; 362 stat_wrapper_t file_info;
334 if (CallFstat(file_.get(), &file_info)) 363 if (CallFstat(file_.get(), &file_info))
335 return false; 364 return false;
336 365
337 info->FromStat(file_info); 366 info->FromStat(file_info);
338 return true; 367 return true;
339 } 368 }
340 369
341 File::Error File::Lock() { 370 File::Error File::Lock() {
371 TRACE_EVENT1(kTraceGroup, "Lock", "path", path_.AsUTF8Unsafe())
342 return CallFctnlFlock(file_.get(), true); 372 return CallFctnlFlock(file_.get(), true);
343 } 373 }
344 374
345 File::Error File::Unlock() { 375 File::Error File::Unlock() {
376 TRACE_EVENT1(kTraceGroup, "Unlock", "path", path_.AsUTF8Unsafe())
346 return CallFctnlFlock(file_.get(), false); 377 return CallFctnlFlock(file_.get(), false);
347 } 378 }
348 379
349 File File::Duplicate() { 380 File File::Duplicate() {
350 if (!IsValid()) 381 if (!IsValid())
351 return File(); 382 return File();
352 383
384 TRACE_EVENT1(kTraceGroup, "Duplicate", "path", path_.AsUTF8Unsafe())
385
353 PlatformFile other_fd = dup(GetPlatformFile()); 386 PlatformFile other_fd = dup(GetPlatformFile());
354 if (other_fd == -1) 387 if (other_fd == -1)
355 return File(OSErrorToFileError(errno)); 388 return File(OSErrorToFileError(errno));
356 389
357 File other(other_fd); 390 File other(other_fd);
358 if (async()) 391 if (async())
359 other.async_ = true; 392 other.async_ = true;
360 return other.Pass(); 393 return other.Pass();
361 } 394 }
362 395
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory"; 468 CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory";
436 } 469 }
437 470
438 void File::MemoryCheckingScopedFD::UpdateChecksum() { 471 void File::MemoryCheckingScopedFD::UpdateChecksum() {
439 ComputeMemoryChecksum(&file_memory_checksum_); 472 ComputeMemoryChecksum(&file_memory_checksum_);
440 } 473 }
441 474
442 // NaCl doesn't implement system calls to open files directly. 475 // NaCl doesn't implement system calls to open files directly.
443 #if !defined(OS_NACL) 476 #if !defined(OS_NACL)
444 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? 477 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
445 void File::DoInitialize(const FilePath& name, uint32 flags) { 478 void File::DoInitialize(uint32 flags) {
446 ThreadRestrictions::AssertIOAllowed(); 479 ThreadRestrictions::AssertIOAllowed();
447 DCHECK(!IsValid()); 480 DCHECK(!IsValid());
448 481
482 TRACE_EVENT1(kTraceGroup, "Initialize", "path", path_.AsUTF8Unsafe())
483
449 int open_flags = 0; 484 int open_flags = 0;
450 if (flags & FLAG_CREATE) 485 if (flags & FLAG_CREATE)
451 open_flags = O_CREAT | O_EXCL; 486 open_flags = O_CREAT | O_EXCL;
452 487
453 created_ = false; 488 created_ = false;
454 489
455 if (flags & FLAG_CREATE_ALWAYS) { 490 if (flags & FLAG_CREATE_ALWAYS) {
456 DCHECK(!open_flags); 491 DCHECK(!open_flags);
457 DCHECK(flags & FLAG_WRITE); 492 DCHECK(flags & FLAG_WRITE);
458 open_flags = O_CREAT | O_TRUNC; 493 open_flags = O_CREAT | O_TRUNC;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 else if (flags & FLAG_APPEND) 525 else if (flags & FLAG_APPEND)
491 open_flags |= O_APPEND | O_WRONLY; 526 open_flags |= O_APPEND | O_WRONLY;
492 527
493 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero); 528 COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero);
494 529
495 int mode = S_IRUSR | S_IWUSR; 530 int mode = S_IRUSR | S_IWUSR;
496 #if defined(OS_CHROMEOS) 531 #if defined(OS_CHROMEOS)
497 mode |= S_IRGRP | S_IROTH; 532 mode |= S_IRGRP | S_IROTH;
498 #endif 533 #endif
499 534
500 int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); 535 int descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode));
501 536
502 if (flags & FLAG_OPEN_ALWAYS) { 537 if (flags & FLAG_OPEN_ALWAYS) {
503 if (descriptor < 0) { 538 if (descriptor < 0) {
504 open_flags |= O_CREAT; 539 open_flags |= O_CREAT;
505 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE) 540 if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE)
506 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW 541 open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW
507 542
508 descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode)); 543 descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode));
509 if (descriptor >= 0) 544 if (descriptor >= 0)
510 created_ = true; 545 created_ = true;
511 } 546 }
512 } 547 }
513 548
514 if (descriptor < 0) { 549 if (descriptor < 0) {
515 error_details_ = File::OSErrorToFileError(errno); 550 error_details_ = File::OSErrorToFileError(errno);
516 return; 551 return;
517 } 552 }
518 553
519 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) 554 if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE))
520 created_ = true; 555 created_ = true;
521 556
522 if (flags & FLAG_DELETE_ON_CLOSE) 557 if (flags & FLAG_DELETE_ON_CLOSE)
523 unlink(name.value().c_str()); 558 unlink(path_.value().c_str());
524 559
525 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); 560 async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
526 error_details_ = FILE_OK; 561 error_details_ = FILE_OK;
527 file_.reset(descriptor); 562 file_.reset(descriptor);
528 } 563 }
529 #endif // !defined(OS_NACL) 564 #endif // !defined(OS_NACL)
530 565
531 bool File::DoFlush() { 566 bool File::DoFlush() {
532 ThreadRestrictions::AssertIOAllowed(); 567 ThreadRestrictions::AssertIOAllowed();
533 DCHECK(IsValid()); 568 DCHECK(IsValid());
569
534 #if defined(OS_NACL) 570 #if defined(OS_NACL)
535 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. 571 NOTIMPLEMENTED(); // NaCl doesn't implement fsync.
536 return true; 572 return true;
537 #elif defined(OS_LINUX) || defined(OS_ANDROID) 573 #else
574 TRACE_EVENT1(kTraceGroup, "Flush", "path", path_.AsUTF8Unsafe())
575 #if defined(OS_LINUX) || defined(OS_ANDROID)
538 return !HANDLE_EINTR(fdatasync(file_.get())); 576 return !HANDLE_EINTR(fdatasync(file_.get()));
539 #else 577 #else
540 return !HANDLE_EINTR(fsync(file_.get())); 578 return !HANDLE_EINTR(fsync(file_.get()));
541 #endif 579 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
580 #endif // defined(OS_NACL)
542 } 581 }
543 582
544 void File::SetPlatformFile(PlatformFile file) { 583 void File::SetPlatformFile(PlatformFile file) {
545 DCHECK(!file_.is_valid()); 584 DCHECK(!file_.is_valid());
546 file_.reset(file); 585 file_.reset(file);
547 } 586 }
548 587
549 } // namespace base 588 } // namespace base
OLDNEW
« base/files/file.h ('K') | « base/files/file.cc ('k') | base/files/file_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698