1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * IOInterposeObserver recording statistics of main-thread I/O during execution,
9 * aimed at consumption by TelemetryImpl
12 #ifndef TelemetryIOInterposeObserver_h__
13 #define TelemetryIOInterposeObserver_h__
15 #include "core/TelemetryCommon.h"
16 #include "js/RootingAPI.h"
17 #include "js/TypeDecls.h"
18 #include "mozilla/IOInterposer.h"
19 #include "nsBaseHashtable.h"
20 #include "nsHashKeys.h"
26 class TelemetryIOInterposeObserver
: public IOInterposeObserver
{
27 /** File-level statistics structure */
30 : creates(0), reads(0), writes(0), fsyncs(0), stats(0), totalTime(0) {}
31 uint32_t creates
; /** Number of create/open operations */
32 uint32_t reads
; /** Number of read operations */
33 uint32_t writes
; /** Number of write operations */
34 uint32_t fsyncs
; /** Number of fsync operations */
35 uint32_t stats
; /** Number of stat operations */
36 double totalTime
; /** Accumulated duration of all operations */
40 SafeDir(const nsAString
& aPath
, const nsAString
& aSubstName
)
41 : mPath(aPath
), mSubstName(aSubstName
) {}
42 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const {
43 return mPath
.SizeOfExcludingThisIfUnshared(aMallocSizeOf
) +
44 mSubstName
.SizeOfExcludingThisIfUnshared(aMallocSizeOf
);
46 nsString mPath
; /** Path to the directory */
47 nsString mSubstName
; /** Name to substitute with */
51 explicit TelemetryIOInterposeObserver(nsIFile
* aXreDir
);
54 * An implementation of Observe that records statistics of all
57 void Observe(Observation
& aOb
) override
;
60 * Reflect recorded file IO statistics into Javascript
62 bool ReflectIntoJS(JSContext
* cx
, JS::Handle
<JSObject
*> rootObj
);
65 * Adds a path for inclusion in main thread I/O report.
66 * @param aPath Directory path
67 * @param aSubstName Name to substitute for aPath for privacy reasons
69 void AddPath(const nsAString
& aPath
, const nsAString
& aSubstName
);
72 * Get size of hash table with file stats
74 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
76 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
79 enum Stage
{ STAGE_STARTUP
= 0, STAGE_NORMAL
, STAGE_SHUTDOWN
, NUM_STAGES
};
80 static inline Stage
NextStage(Stage aStage
) {
85 return STAGE_SHUTDOWN
;
87 return STAGE_SHUTDOWN
;
93 struct FileStatsByStage
{
94 FileStats mStats
[NUM_STAGES
];
96 typedef nsBaseHashtableET
<nsStringHashKey
, FileStatsByStage
> FileIOEntryType
;
98 // Statistics for each filename
99 Common::AutoHashtable
<FileIOEntryType
> mFileStats
;
100 // Container for allowed directories
101 nsTArray
<SafeDir
> mSafeDirs
;
105 * Reflect a FileIOEntryType object to a Javascript property on obj with
106 * filename as key containing array:
107 * [totalTime, creates, reads, writes, fsyncs, stats]
109 static bool ReflectFileStats(FileIOEntryType
* entry
, JSContext
* cx
,
110 JS::Handle
<JSObject
*> obj
);
113 } // namespace Telemetry
114 } // namespace mozilla
116 #endif // TelemetryIOInterposeObserver_h__