Fix typo in 9b54bd30006c008b4a951331b273613d5bac3abf
[pm.git] / xpcom / build / IOInterposer.h
blob9d7ad8818f0c6546d8dbddb8958541027a1b14ba
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_IOInterposer_h
6 #define mozilla_IOInterposer_h
8 #include "mozilla/Attributes.h"
9 #include "mozilla/TimeStamp.h"
10 #include "mozilla/XPCOM.h"
12 namespace mozilla {
14 /**
15 * Interface for I/O interposer observers. This is separate from the
16 * IOInterposer because we have multiple uses for these observations.
18 class IOInterposeObserver
20 public:
21 enum Operation
23 OpNone = 0,
24 OpCreateOrOpen = (1 << 0),
25 OpRead = (1 << 1),
26 OpWrite = (1 << 2),
27 OpFSync = (1 << 3),
28 OpStat = (1 << 4),
29 OpClose = (1 << 5),
30 OpNextStage = (1 << 6), // Meta - used when leaving startup, entering shutdown
31 OpWriteFSync = (OpWrite | OpFSync),
32 OpAll = (OpCreateOrOpen | OpRead | OpWrite | OpFSync | OpStat | OpClose),
33 OpAllWithStaging = (OpAll | OpNextStage)
36 /** A representation of an I/O observation */
37 class Observation
39 protected:
40 /**
41 * This constructor is for use by subclasses that are intended to take
42 * timing measurements via RAII. The |aShouldReport| parameter may be
43 * used to make the measurement and reporting conditional on the
44 * satisfaction of an arbitrary predicate that was evaluated
45 * in the subclass. Note that IOInterposer::IsObservedOperation() is
46 * always ANDed with aShouldReport, so the subclass does not need to
47 * include a call to that function explicitly.
49 Observation(Operation aOperation, const char* aReference,
50 bool aShouldReport = true);
52 public:
53 /**
54 * Since this constructor accepts start and end times, it does *not* take
55 * its own timings, nor does it report itself.
57 Observation(Operation aOperation, const TimeStamp& aStart,
58 const TimeStamp& aEnd, const char* aReference);
60 /**
61 * Operation observed, this is one of the individual Operation values.
62 * Combinations of these flags are only used when registering observers.
64 Operation ObservedOperation() const { return mOperation; }
66 /**
67 * Return the observed operation as a human-readable string.
69 const char* ObservedOperationString() const;
71 /** Time at which the I/O operation was started */
72 TimeStamp Start() const { return mStart; }
74 /**
75 * Time at which the I/O operation ended, for asynchronous methods this is
76 * the time at which the call initiating the asynchronous request returned.
78 TimeStamp End() const { return mEnd; }
80 /**
81 * Duration of the operation, for asynchronous I/O methods this is the
82 * duration of the call initiating the asynchronous request.
84 TimeDuration Duration() const { return mEnd - mStart; }
86 /**
87 * IO reference, function name or name of component (sqlite) that did IO
88 * this is in addition the generic operation. This attribute may be platform
89 * specific, but should only take a finite number of distinct values.
90 * E.g. sqlite-commit, CreateFile, NtReadFile, fread, fsync, mmap, etc.
91 * I.e. typically the platform specific function that did the IO.
93 const char* Reference() const { return mReference; }
95 /** Request filename associated with the I/O operation, null if unknown */
96 virtual const char16_t* Filename() { return nullptr; }
98 virtual ~Observation() {}
100 protected:
101 void
102 Report();
104 Operation mOperation;
105 TimeStamp mStart;
106 TimeStamp mEnd;
107 const char* mReference; // Identifies the source of the Observation
108 bool mShouldReport; // Measure and report if true
112 * Invoked whenever an implementation of the IOInterposeObserver should
113 * observe aObservation. Implement this and do your thing...
114 * But do consider if it is wise to use IO functions in this method, they are
115 * likely to cause recursion :)
116 * At least, see PoisonIOInterposer.h and register your handle as a debug file
117 * even, if you don't initialize the poison IO interposer, someone else might.
119 * Remark: Observations may occur on any thread.
121 virtual void Observe(Observation& aObservation) = 0;
123 virtual ~IOInterposeObserver() {}
125 protected:
127 * We don't use NS_IsMainThread() because we need to be able to determine the
128 * main thread outside of XPCOM Initialization. IOInterposer observers should
129 * call this function instead.
131 static bool IsMainThread();
135 * These functions are responsible for ensuring that events are routed to the
136 * appropriate observers.
138 namespace IOInterposer {
141 * This function must be called from the main-thread when no other threads are
142 * running before any of the other methods on this class may be used.
144 * IO reports can however, safely assume that IsObservedOperation() will
145 * return false until the IOInterposer is initialized.
147 * Remark, it's safe to call this method multiple times, so just call it when
148 * you to utilize IO interposing.
150 * Using the IOInterposerInit class is preferred to calling this directly.
152 bool Init();
155 * This function must be called from the main thread, and furthermore
156 * it must be called when no other threads are executing. Effectively
157 * restricting us to calling it only during shutdown.
159 * Callers should take care that no other consumers are subscribed to events,
160 * as these events will stop when this function is called.
162 * In practice, we don't use this method as the IOInterposer is used for
163 * late-write checks.
165 void Clear();
168 * This function immediately disables IOInterposer functionality in a fast,
169 * thread-safe manner. Primarily for use by the crash reporter.
171 void Disable();
174 * Report IO to registered observers.
175 * Notice that the reported operation must be either OpRead, OpWrite or
176 * OpFSync. You are not allowed to report an observation with OpWriteFSync or
177 * OpAll, these are just auxiliary values for use with Register().
179 * If the IO call you're reporting does multiple things, write and fsync, you
180 * can choose to call Report() twice once with write and once with FSync. You
181 * may not call Report() with OpWriteFSync! The Observation::mOperation
182 * attribute is meant to be generic, not perfect.
184 * Notice that there is no reason to report an observation with an operation
185 * which is not being observed. Use IsObservedOperation() to check if the
186 * operation you are about to report is being observed. This is especially
187 * important if you are constructing expensive observations containing
188 * filename and full-path.
190 * Remark: Init() must be called before any IO is reported. But
191 * IsObservedOperation() will return false until Init() is called.
193 void Report(IOInterposeObserver::Observation& aObservation);
196 * Return whether or not an operation is observed. Reporters should not
197 * report operations that are not being observed by anybody. This mechanism
198 * allows us to avoid reporting I/O when no observers are registered.
200 bool IsObservedOperation(IOInterposeObserver::Operation aOp);
203 * Register IOInterposeObserver, the observer object will receive all
204 * observations for the given operation aOp.
206 * Remark: Init() must be called before observers are registered.
208 void Register(IOInterposeObserver::Operation aOp,
209 IOInterposeObserver* aObserver);
212 * Unregister an IOInterposeObserver for a given operation
213 * Remark: It is always safe to unregister for all operations, even if yoú
214 * didn't register for them all.
215 * I.e. IOInterposer::Unregister(IOInterposeObserver::OpAll, aObserver)
217 * Remark: Init() must be called before observers are unregistered.
219 void Unregister(IOInterposeObserver::Operation aOp,
220 IOInterposeObserver* aObserver);
223 * Registers the current thread with the IOInterposer. This must be done to
224 * ensure that per-thread data is created in an orderly fashion.
225 * We could have written this to initialize that data lazily, however this
226 * could have unintended consequences if a thread that is not aware of
227 * IOInterposer was implicitly registered: its per-thread data would never
228 * be deleted because it would not know to unregister itself.
230 * @param aIsMainThread true if IOInterposer should treat the current thread
231 * as the main thread.
233 void RegisterCurrentThread(bool aIsMainThread = false);
236 * Unregisters the current thread with the IOInterposer. This is important
237 * to call when a thread is shutting down because it cleans up data that
238 * is stored in a TLS slot.
240 void UnregisterCurrentThread();
243 * Called to inform observers that the process has transitioned out of the
244 * startup stage or into the shutdown stage. Main thread only.
246 void EnteringNextStage();
248 } // namespace IOInterposer
250 class IOInterposerInit
252 public:
253 IOInterposerInit()
257 ~IOInterposerInit()
262 } // namespace mozilla
264 #endif // mozilla_IOInterposer_h