dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 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 | |
| 5 | #ifndef BASE_FILES_FILE_TRACING_H_ |
| 6 | #define BASE_FILES_FILE_TRACING_H_ |
| 7 | |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/basictypes.h" |
| 10 | #include "base/macros.h" |
| 11 | |
| 12 | #define FILE_TRACING_PREFIX "File" |
| 13 | |
| 14 | #define SCOPED_FILE_TRACE_WITH_SIZE(name, size) \ |
| 15 | FileTracing::ScopedTrace scoped_file_trace; \ |
dbeam | 8054d4e | 2015-07-02 12:33:08 +0900 | [diff] [blame] | 16 | if (FileTracing::IsCategoryEnabled()) \ |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 17 | scoped_file_trace.Initialize(FILE_TRACING_PREFIX "::" name, this, size) |
| 18 | |
| 19 | #define SCOPED_FILE_TRACE(name) SCOPED_FILE_TRACE_WITH_SIZE(name, 0) |
| 20 | |
| 21 | namespace base { |
| 22 | |
| 23 | class File; |
| 24 | class FilePath; |
| 25 | |
| 26 | class BASE_EXPORT FileTracing { |
| 27 | public: |
dbeam | 8054d4e | 2015-07-02 12:33:08 +0900 | [diff] [blame] | 28 | // Whether the file tracing category is enabled. |
| 29 | static bool IsCategoryEnabled(); |
| 30 | |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 31 | class Provider { |
| 32 | public: |
derat | feeb167 | 2015-07-14 00:22:24 +0900 | [diff] [blame] | 33 | virtual ~Provider() = default; |
| 34 | |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 35 | // Whether the file tracing category is currently enabled. |
| 36 | virtual bool FileTracingCategoryIsEnabled() const = 0; |
| 37 | |
| 38 | // Enables file tracing for |id|. Must be called before recording events. |
| 39 | virtual void FileTracingEnable(void* id) = 0; |
| 40 | |
| 41 | // Disables file tracing for |id|. |
| 42 | virtual void FileTracingDisable(void* id) = 0; |
| 43 | |
| 44 | // Begins an event for |id| with |name|. |path| tells where in the directory |
dbeam | ae92651 | 2015-06-10 01:08:16 +0900 | [diff] [blame] | 45 | // structure the event is happening (and may be blank). |size| is the number |
| 46 | // of bytes involved in the event. |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 47 | virtual void FileTracingEventBegin( |
| 48 | const char* name, void* id, const FilePath& path, int64 size) = 0; |
| 49 | |
dbeam | ae92651 | 2015-06-10 01:08:16 +0900 | [diff] [blame] | 50 | // Ends an event for |id| with |name|. |
| 51 | virtual void FileTracingEventEnd(const char* name, void* id) = 0; |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // Sets a global file tracing provider to query categories and record events. |
| 55 | static void SetProvider(Provider* provider); |
| 56 | |
| 57 | // Enables file tracing while in scope. |
| 58 | class ScopedEnabler { |
| 59 | public: |
| 60 | ScopedEnabler(); |
| 61 | ~ScopedEnabler(); |
| 62 | }; |
| 63 | |
| 64 | class ScopedTrace { |
| 65 | public: |
| 66 | ScopedTrace(); |
| 67 | ~ScopedTrace(); |
| 68 | |
dbeam | ae92651 | 2015-06-10 01:08:16 +0900 | [diff] [blame] | 69 | // Called only if the tracing category is enabled. |name| is the name of the |
| 70 | // event to trace (e.g. "Read", "Write") and must have an application |
| 71 | // lifetime (e.g. static or literal). |file| is the file being traced; must |
| 72 | // outlive this class. |size| is the size (in bytes) of this event. |
| 73 | void Initialize(const char* name, File* file, int64 size); |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 74 | |
| 75 | private: |
dbeam | ae92651 | 2015-06-10 01:08:16 +0900 | [diff] [blame] | 76 | // The ID of this trace. Based on the |file| passed to |Initialize()|. Must |
| 77 | // outlive this class. |
| 78 | void* id_; |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 79 | |
dbeam | ae92651 | 2015-06-10 01:08:16 +0900 | [diff] [blame] | 80 | // The name of the event to trace (e.g. "Read", "Write"). Prefixed with |
| 81 | // "File". |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 82 | const char* name_; |
| 83 | |
dbeam | fe14ece | 2015-05-11 16:53:47 +0900 | [diff] [blame] | 84 | DISALLOW_COPY_AND_ASSIGN(ScopedTrace); |
| 85 | }; |
| 86 | |
| 87 | DISALLOW_COPY_AND_ASSIGN(FileTracing); |
| 88 | }; |
| 89 | |
| 90 | } // namespace base |
| 91 | |
| 92 | #endif // BASE_FILES_FILE_TRACING_H_ |