Bug 1941046 - Part 4: Send a callback request for impression and clicks of MARS Top...
[gecko.git] / toolkit / components / telemetry / other / TelemetryIOInterposeObserver.h
blob54e3caf9b4f3c2f31fb14e8e5c0421c7e164aed9
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/. */
7 /**
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"
21 #include "nsTArray.h"
23 namespace mozilla {
24 namespace Telemetry {
26 class TelemetryIOInterposeObserver : public IOInterposeObserver {
27 /** File-level statistics structure */
28 struct FileStats {
29 FileStats()
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 */
39 struct SafeDir {
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 */
50 public:
51 explicit TelemetryIOInterposeObserver(nsIFile* aXreDir);
53 /**
54 * An implementation of Observe that records statistics of all
55 * file IO operations.
57 void Observe(Observation& aOb) override;
59 /**
60 * Reflect recorded file IO statistics into Javascript
62 bool ReflectIntoJS(JSContext* cx, JS::Handle<JSObject*> rootObj);
64 /**
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);
71 /**
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;
78 private:
79 enum Stage { STAGE_STARTUP = 0, STAGE_NORMAL, STAGE_SHUTDOWN, NUM_STAGES };
80 static inline Stage NextStage(Stage aStage) {
81 switch (aStage) {
82 case STAGE_STARTUP:
83 return STAGE_NORMAL;
84 case STAGE_NORMAL:
85 return STAGE_SHUTDOWN;
86 case STAGE_SHUTDOWN:
87 return STAGE_SHUTDOWN;
88 default:
89 return NUM_STAGES;
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;
102 Stage mCurStage;
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__