blob: 92324c9475abe0646d834c21a0eabc80b87eef42 [file] [log] [blame]
dbeamfe14ece2015-05-11 16:53:47 +09001// 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; \
dbeam8054d4e2015-07-02 12:33:08 +090016 if (FileTracing::IsCategoryEnabled()) \
dbeamfe14ece2015-05-11 16:53:47 +090017 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
21namespace base {
22
23class File;
24class FilePath;
25
26class BASE_EXPORT FileTracing {
27 public:
dbeam8054d4e2015-07-02 12:33:08 +090028 // Whether the file tracing category is enabled.
29 static bool IsCategoryEnabled();
30
dbeamfe14ece2015-05-11 16:53:47 +090031 class Provider {
32 public:
33 // Whether the file tracing category is currently enabled.
34 virtual bool FileTracingCategoryIsEnabled() const = 0;
35
36 // Enables file tracing for |id|. Must be called before recording events.
37 virtual void FileTracingEnable(void* id) = 0;
38
39 // Disables file tracing for |id|.
40 virtual void FileTracingDisable(void* id) = 0;
41
42 // Begins an event for |id| with |name|. |path| tells where in the directory
dbeamae926512015-06-10 01:08:16 +090043 // structure the event is happening (and may be blank). |size| is the number
44 // of bytes involved in the event.
dbeamfe14ece2015-05-11 16:53:47 +090045 virtual void FileTracingEventBegin(
46 const char* name, void* id, const FilePath& path, int64 size) = 0;
47
dbeamae926512015-06-10 01:08:16 +090048 // Ends an event for |id| with |name|.
49 virtual void FileTracingEventEnd(const char* name, void* id) = 0;
dbeamfe14ece2015-05-11 16:53:47 +090050 };
51
52 // Sets a global file tracing provider to query categories and record events.
53 static void SetProvider(Provider* provider);
54
55 // Enables file tracing while in scope.
56 class ScopedEnabler {
57 public:
58 ScopedEnabler();
59 ~ScopedEnabler();
60 };
61
62 class ScopedTrace {
63 public:
64 ScopedTrace();
65 ~ScopedTrace();
66
dbeamae926512015-06-10 01:08:16 +090067 // Called only if the tracing category is enabled. |name| is the name of the
68 // event to trace (e.g. "Read", "Write") and must have an application
69 // lifetime (e.g. static or literal). |file| is the file being traced; must
70 // outlive this class. |size| is the size (in bytes) of this event.
71 void Initialize(const char* name, File* file, int64 size);
dbeamfe14ece2015-05-11 16:53:47 +090072
73 private:
dbeamae926512015-06-10 01:08:16 +090074 // The ID of this trace. Based on the |file| passed to |Initialize()|. Must
75 // outlive this class.
76 void* id_;
dbeamfe14ece2015-05-11 16:53:47 +090077
dbeamae926512015-06-10 01:08:16 +090078 // The name of the event to trace (e.g. "Read", "Write"). Prefixed with
79 // "File".
dbeamfe14ece2015-05-11 16:53:47 +090080 const char* name_;
81
dbeamfe14ece2015-05-11 16:53:47 +090082 DISALLOW_COPY_AND_ASSIGN(ScopedTrace);
83 };
84
85 DISALLOW_COPY_AND_ASSIGN(FileTracing);
86};
87
88} // namespace base
89
90#endif // BASE_FILES_FILE_TRACING_H_