file_tracing.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_FILES_FILE_TRACING_H_
  5. #define BASE_FILES_FILE_TRACING_H_
  6. #include <stdint.h>
  7. #include "base/base_export.h"
  8. #include "base/macros.h"
  9. #define FILE_TRACING_PREFIX "File"
  10. #define SCOPED_FILE_TRACE_WITH_SIZE(name, size) \
  11. FileTracing::ScopedTrace scoped_file_trace; \
  12. if (FileTracing::IsCategoryEnabled()) \
  13. scoped_file_trace.Initialize(FILE_TRACING_PREFIX "::" name, this, size)
  14. #define SCOPED_FILE_TRACE(name) SCOPED_FILE_TRACE_WITH_SIZE(name, 0)
  15. namespace base {
  16. class File;
  17. class FilePath;
  18. class BASE_EXPORT FileTracing {
  19. public:
  20. // Whether the file tracing category is enabled.
  21. static bool IsCategoryEnabled();
  22. class Provider {
  23. public:
  24. virtual ~Provider() = default;
  25. // Whether the file tracing category is currently enabled.
  26. virtual bool FileTracingCategoryIsEnabled() const = 0;
  27. // Enables file tracing for |id|. Must be called before recording events.
  28. virtual void FileTracingEnable(const void* id) = 0;
  29. // Disables file tracing for |id|.
  30. virtual void FileTracingDisable(const void* id) = 0;
  31. // Begins an event for |id| with |name|. |path| tells where in the directory
  32. // structure the event is happening (and may be blank). |size| is the number
  33. // of bytes involved in the event.
  34. virtual void FileTracingEventBegin(const char* name,
  35. const void* id,
  36. const FilePath& path,
  37. int64_t size) = 0;
  38. // Ends an event for |id| with |name|.
  39. virtual void FileTracingEventEnd(const char* name, const void* id) = 0;
  40. };
  41. // Sets a global file tracing provider to query categories and record events.
  42. static void SetProvider(Provider* provider);
  43. // Enables file tracing while in scope.
  44. class ScopedEnabler {
  45. public:
  46. ScopedEnabler();
  47. ~ScopedEnabler();
  48. };
  49. class ScopedTrace {
  50. public:
  51. ScopedTrace();
  52. ~ScopedTrace();
  53. // Called only if the tracing category is enabled. |name| is the name of the
  54. // event to trace (e.g. "Read", "Write") and must have an application
  55. // lifetime (e.g. static or literal). |file| is the file being traced; must
  56. // outlive this class. |size| is the size (in bytes) of this event.
  57. void Initialize(const char* name, const File* file, int64_t size);
  58. private:
  59. // The ID of this trace. Based on the |file| passed to |Initialize()|. Must
  60. // outlive this class.
  61. const void* id_;
  62. // The name of the event to trace (e.g. "Read", "Write"). Prefixed with
  63. // "File".
  64. const char* name_;
  65. DISALLOW_COPY_AND_ASSIGN(ScopedTrace);
  66. };
  67. DISALLOW_COPY_AND_ASSIGN(FileTracing);
  68. };
  69. } // namespace base
  70. #endif // BASE_FILES_FILE_TRACING_H_