2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-06-14 22:01:02 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
#ifndef mozilla_IOInterposer_h
|
|
|
|
#define mozilla_IOInterposer_h
|
2013-06-14 22:01:02 +04:00
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
2013-09-12 18:47:37 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2017-10-17 00:22:12 +03:00
|
|
|
#include "nsString.h"
|
2013-06-14 22:01:02 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
2013-09-12 18:47:37 +04:00
|
|
|
* Interface for I/O interposer observers. This is separate from the
|
|
|
|
* IOInterposer because we have multiple uses for these observations.
|
2013-06-14 22:01:02 +04:00
|
|
|
*/
|
|
|
|
class IOInterposeObserver {
|
|
|
|
public:
|
|
|
|
enum Operation {
|
|
|
|
OpNone = 0,
|
2013-10-01 23:53:19 +04:00
|
|
|
OpCreateOrOpen = (1 << 0),
|
|
|
|
OpRead = (1 << 1),
|
|
|
|
OpWrite = (1 << 2),
|
|
|
|
OpFSync = (1 << 3),
|
|
|
|
OpStat = (1 << 4),
|
|
|
|
OpClose = (1 << 5),
|
2014-04-20 00:28:02 +04:00
|
|
|
OpNextStage =
|
|
|
|
(1 << 6), // Meta - used when leaving startup, entering shutdown
|
2013-06-14 22:01:02 +04:00
|
|
|
OpWriteFSync = (OpWrite | OpFSync),
|
2014-04-20 00:28:02 +04:00
|
|
|
OpAll = (OpCreateOrOpen | OpRead | OpWrite | OpFSync | OpStat | OpClose),
|
|
|
|
OpAllWithStaging = (OpAll | OpNextStage)
|
2013-06-14 22:01:02 +04:00
|
|
|
};
|
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
/** A representation of an I/O observation */
|
|
|
|
class Observation {
|
|
|
|
protected:
|
2014-02-01 07:14:03 +04:00
|
|
|
/**
|
|
|
|
* This constructor is for use by subclasses that are intended to take
|
|
|
|
* timing measurements via RAII. The |aShouldReport| parameter may be
|
|
|
|
* used to make the measurement and reporting conditional on the
|
|
|
|
* satisfaction of an arbitrary predicate that was evaluated
|
|
|
|
* in the subclass. Note that IOInterposer::IsObservedOperation() is
|
|
|
|
* always ANDed with aShouldReport, so the subclass does not need to
|
|
|
|
* include a call to that function explicitly.
|
|
|
|
*/
|
|
|
|
Observation(Operation aOperation, const char* aReference,
|
|
|
|
bool aShouldReport = true);
|
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
public:
|
2014-02-01 07:14:03 +04:00
|
|
|
/**
|
|
|
|
* Since this constructor accepts start and end times, it does *not* take
|
|
|
|
* its own timings, nor does it report itself.
|
|
|
|
*/
|
2013-09-12 18:47:37 +04:00
|
|
|
Observation(Operation aOperation, const TimeStamp& aStart,
|
2014-02-01 07:14:03 +04:00
|
|
|
const TimeStamp& aEnd, const char* aReference);
|
2013-09-12 18:47:37 +04:00
|
|
|
|
|
|
|
/**
|
2014-04-20 00:28:02 +04:00
|
|
|
* Operation observed, this is one of the individual Operation values.
|
|
|
|
* Combinations of these flags are only used when registering observers.
|
2013-09-12 18:47:37 +04:00
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
Operation ObservedOperation() const { return mOperation; }
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-04-20 00:28:02 +04:00
|
|
|
/**
|
|
|
|
* Return the observed operation as a human-readable string.
|
|
|
|
*/
|
|
|
|
const char* ObservedOperationString() const;
|
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
/** Time at which the I/O operation was started */
|
2014-08-13 22:45:37 +04:00
|
|
|
TimeStamp Start() const { return mStart; }
|
2013-09-12 18:47:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Time at which the I/O operation ended, for asynchronous methods this is
|
|
|
|
* the time at which the call initiating the asynchronous request returned.
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
TimeStamp End() const { return mEnd; }
|
2013-09-12 18:47:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Duration of the operation, for asynchronous I/O methods this is the
|
|
|
|
* duration of the call initiating the asynchronous request.
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
TimeDuration Duration() const { return mEnd - mStart; }
|
2013-09-12 18:47:37 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* IO reference, function name or name of component (sqlite) that did IO
|
|
|
|
* this is in addition the generic operation. This attribute may be platform
|
|
|
|
* specific, but should only take a finite number of distinct values.
|
|
|
|
* E.g. sqlite-commit, CreateFile, NtReadFile, fread, fsync, mmap, etc.
|
|
|
|
* I.e. typically the platform specific function that did the IO.
|
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
const char* Reference() const { return mReference; }
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2020-07-07 02:42:55 +03:00
|
|
|
virtual const char* FileType() const { return "File"; }
|
|
|
|
|
2017-10-17 00:22:12 +03:00
|
|
|
/** Request filename associated with the I/O operation, empty if unknown */
|
|
|
|
virtual void Filename(nsAString& aString) { aString.Truncate(); }
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2020-02-12 14:13:33 +03:00
|
|
|
virtual ~Observation() = default;
|
2014-02-01 07:14:03 +04:00
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
protected:
|
2014-02-01 07:14:03 +04:00
|
|
|
void Report();
|
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
Operation mOperation;
|
|
|
|
TimeStamp mStart;
|
|
|
|
TimeStamp mEnd;
|
2014-02-01 07:14:03 +04:00
|
|
|
const char* mReference; // Identifies the source of the Observation
|
|
|
|
bool mShouldReport; // Measure and report if true
|
2013-09-12 18:47:37 +04:00
|
|
|
};
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
/**
|
|
|
|
* Invoked whenever an implementation of the IOInterposeObserver should
|
|
|
|
* observe aObservation. Implement this and do your thing...
|
|
|
|
* But do consider if it is wise to use IO functions in this method, they are
|
|
|
|
* likely to cause recursion :)
|
|
|
|
* At least, see PoisonIOInterposer.h and register your handle as a debug file
|
|
|
|
* even, if you don't initialize the poison IO interposer, someone else might.
|
|
|
|
*
|
|
|
|
* Remark: Observations may occur on any thread.
|
|
|
|
*/
|
|
|
|
virtual void Observe(Observation& aObservation) = 0;
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2020-02-12 14:13:33 +03:00
|
|
|
virtual ~IOInterposeObserver() = default;
|
2014-03-15 02:35:01 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* We don't use NS_IsMainThread() because we need to be able to determine the
|
|
|
|
* main thread outside of XPCOM Initialization. IOInterposer observers should
|
|
|
|
* call this function instead.
|
|
|
|
*/
|
|
|
|
static bool IsMainThread();
|
2013-06-14 22:01:02 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-04-20 00:28:02 +04:00
|
|
|
* These functions are responsible for ensuring that events are routed to the
|
|
|
|
* appropriate observers.
|
2013-06-14 22:01:02 +04:00
|
|
|
*/
|
2014-08-13 22:45:37 +04:00
|
|
|
namespace IOInterposer {
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
/**
|
|
|
|
* This function must be called from the main-thread when no other threads are
|
|
|
|
* running before any of the other methods on this class may be used.
|
|
|
|
*
|
|
|
|
* IO reports can however, safely assume that IsObservedOperation() will
|
|
|
|
* return false until the IOInterposer is initialized.
|
|
|
|
*
|
|
|
|
* Remark, it's safe to call this method multiple times, so just call it when
|
|
|
|
* you to utilize IO interposing.
|
|
|
|
*
|
|
|
|
* Using the IOInterposerInit class is preferred to calling this directly.
|
|
|
|
*/
|
|
|
|
bool Init();
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
/**
|
|
|
|
* This function must be called from the main thread, and furthermore
|
|
|
|
* it must be called when no other threads are executing. Effectively
|
|
|
|
* restricting us to calling it only during shutdown.
|
|
|
|
*
|
|
|
|
* Callers should take care that no other consumers are subscribed to events,
|
|
|
|
* as these events will stop when this function is called.
|
|
|
|
*
|
|
|
|
* In practice, we don't use this method as the IOInterposer is used for
|
|
|
|
* late-write checks.
|
|
|
|
*/
|
|
|
|
void Clear();
|
2014-03-15 10:10:39 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
/**
|
|
|
|
* This function immediately disables IOInterposer functionality in a fast,
|
|
|
|
* thread-safe manner. Primarily for use by the crash reporter.
|
|
|
|
*/
|
|
|
|
void Disable();
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2017-01-11 00:31:25 +03:00
|
|
|
/**
|
|
|
|
* This function re-enables IOInterposer functionality in a fast, thread-safe
|
|
|
|
* manner. Primarily for use by the crash reporter.
|
|
|
|
*/
|
|
|
|
void Enable();
|
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
/**
|
|
|
|
* Report IO to registered observers.
|
|
|
|
* Notice that the reported operation must be either OpRead, OpWrite or
|
|
|
|
* OpFSync. You are not allowed to report an observation with OpWriteFSync or
|
|
|
|
* OpAll, these are just auxiliary values for use with Register().
|
|
|
|
*
|
|
|
|
* If the IO call you're reporting does multiple things, write and fsync, you
|
|
|
|
* can choose to call Report() twice once with write and once with FSync. You
|
|
|
|
* may not call Report() with OpWriteFSync! The Observation::mOperation
|
|
|
|
* attribute is meant to be generic, not perfect.
|
|
|
|
*
|
|
|
|
* Notice that there is no reason to report an observation with an operation
|
|
|
|
* which is not being observed. Use IsObservedOperation() to check if the
|
|
|
|
* operation you are about to report is being observed. This is especially
|
|
|
|
* important if you are constructing expensive observations containing
|
|
|
|
* filename and full-path.
|
|
|
|
*
|
|
|
|
* Remark: Init() must be called before any IO is reported. But
|
|
|
|
* IsObservedOperation() will return false until Init() is called.
|
|
|
|
*/
|
|
|
|
void Report(IOInterposeObserver::Observation& aObservation);
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
/**
|
|
|
|
* Return whether or not an operation is observed. Reporters should not
|
|
|
|
* report operations that are not being observed by anybody. This mechanism
|
|
|
|
* allows us to avoid reporting I/O when no observers are registered.
|
|
|
|
*/
|
|
|
|
bool IsObservedOperation(IOInterposeObserver::Operation aOp);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register IOInterposeObserver, the observer object will receive all
|
|
|
|
* observations for the given operation aOp.
|
|
|
|
*
|
|
|
|
* Remark: Init() must be called before observers are registered.
|
|
|
|
*/
|
|
|
|
void Register(IOInterposeObserver::Operation aOp,
|
|
|
|
IOInterposeObserver* aObserver);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister an IOInterposeObserver for a given operation
|
|
|
|
* Remark: It is always safe to unregister for all operations, even if yoú
|
|
|
|
* didn't register for them all.
|
|
|
|
* I.e. IOInterposer::Unregister(IOInterposeObserver::OpAll, aObserver)
|
|
|
|
*
|
|
|
|
* Remark: Init() must be called before observers are unregistered.
|
|
|
|
*/
|
|
|
|
void Unregister(IOInterposeObserver::Operation aOp,
|
2014-04-20 00:28:02 +04:00
|
|
|
IOInterposeObserver* aObserver);
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
/**
|
|
|
|
* Registers the current thread with the IOInterposer. This must be done to
|
|
|
|
* ensure that per-thread data is created in an orderly fashion.
|
|
|
|
* We could have written this to initialize that data lazily, however this
|
|
|
|
* could have unintended consequences if a thread that is not aware of
|
|
|
|
* IOInterposer was implicitly registered: its per-thread data would never
|
|
|
|
* be deleted because it would not know to unregister itself.
|
|
|
|
*
|
|
|
|
* @param aIsMainThread true if IOInterposer should treat the current thread
|
|
|
|
* as the main thread.
|
|
|
|
*/
|
|
|
|
void RegisterCurrentThread(bool aIsMainThread = false);
|
2014-03-15 02:35:01 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
/**
|
|
|
|
* Unregisters the current thread with the IOInterposer. This is important
|
|
|
|
* to call when a thread is shutting down because it cleans up data that
|
|
|
|
* is stored in a TLS slot.
|
|
|
|
*/
|
|
|
|
void UnregisterCurrentThread();
|
2014-04-09 08:57:52 +04:00
|
|
|
|
2014-08-13 22:45:37 +04:00
|
|
|
/**
|
|
|
|
* Called to inform observers that the process has transitioned out of the
|
|
|
|
* startup stage or into the shutdown stage. Main thread only.
|
|
|
|
*/
|
|
|
|
void EnteringNextStage();
|
2014-04-20 00:28:02 +04:00
|
|
|
|
|
|
|
} // namespace IOInterposer
|
2013-09-12 18:47:37 +04:00
|
|
|
|
2014-03-15 02:35:01 +04:00
|
|
|
class IOInterposerInit {
|
|
|
|
public:
|
|
|
|
IOInterposerInit() {
|
2020-09-22 00:32:07 +03:00
|
|
|
#if defined(EARLY_BETA_OR_EARLIER)
|
2014-03-15 02:35:01 +04:00
|
|
|
IOInterposer::Init();
|
2014-03-19 02:24:36 +04:00
|
|
|
#endif
|
2014-03-15 02:35:01 +04:00
|
|
|
}
|
|
|
|
|
2014-04-29 21:29:43 +04:00
|
|
|
~IOInterposerInit() {
|
2020-09-22 00:32:07 +03:00
|
|
|
#if defined(EARLY_BETA_OR_EARLIER)
|
2014-04-29 21:29:43 +04:00
|
|
|
IOInterposer::Clear();
|
|
|
|
#endif
|
|
|
|
}
|
2014-03-15 02:35:01 +04:00
|
|
|
};
|
|
|
|
|
2017-01-11 00:31:25 +03:00
|
|
|
class MOZ_RAII AutoIOInterposerDisable final {
|
|
|
|
public:
|
2020-07-30 17:22:38 +03:00
|
|
|
explicit AutoIOInterposerDisable() { IOInterposer::Disable(); }
|
2017-01-11 00:31:25 +03:00
|
|
|
~AutoIOInterposerDisable() { IOInterposer::Enable(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
} // namespace mozilla
|
2013-06-14 22:01:02 +04:00
|
|
|
|
2013-09-12 18:47:37 +04:00
|
|
|
#endif // mozilla_IOInterposer_h
|