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