Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/telemetry/other/TelemetryIOInterposeObserver.h
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
8
/**
9
 * IOInterposeObserver recording statistics of main-thread I/O during execution,
10
 * aimed at consumption by TelemetryImpl
11
 */
12
13
#ifndef TelemetryIOInterposeObserver_h__
14
#define TelemetryIOInterposeObserver_h__
15
16
#include "jsapi.h"
17
#include "nsTArray.h"
18
#include "nsTHashtable.h"
19
#include "nsHashKeys.h"
20
#include "nsBaseHashtable.h"
21
#include "nsClassHashtable.h"
22
23
#include "core/TelemetryCommon.h"
24
#include "mozilla/IOInterposer.h"
25
26
namespace mozilla {
27
namespace Telemetry {
28
29
class TelemetryIOInterposeObserver : public IOInterposeObserver
30
{
31
  /** File-level statistics structure */
32
  struct FileStats {
33
    FileStats()
34
      : creates(0)
35
      , reads(0)
36
      , writes(0)
37
      , fsyncs(0)
38
      , stats(0)
39
      , totalTime(0)
40
0
    {}
41
    uint32_t  creates;      /** Number of create/open operations */
42
    uint32_t  reads;        /** Number of read operations */
43
    uint32_t  writes;       /** Number of write operations */
44
    uint32_t  fsyncs;       /** Number of fsync operations */
45
    uint32_t  stats;        /** Number of stat operations */
46
    double    totalTime;    /** Accumulated duration of all operations */
47
  };
48
49
  struct SafeDir {
50
    SafeDir(const nsAString& aPath, const nsAString& aSubstName)
51
      : mPath(aPath)
52
      , mSubstName(aSubstName)
53
3
    {}
54
0
    size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
55
0
      return mPath.SizeOfExcludingThisIfUnshared(aMallocSizeOf) +
56
0
             mSubstName.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
57
0
    }
58
    nsString  mPath;        /** Path to the directory */
59
    nsString  mSubstName;   /** Name to substitute with */
60
  };
61
62
public:
63
  explicit TelemetryIOInterposeObserver(nsIFile* aXreDir);
64
65
  /**
66
   * An implementation of Observe that records statistics of all
67
   * file IO operations.
68
   */
69
  void Observe(Observation& aOb) override;
70
71
  /**
72
   * Reflect recorded file IO statistics into Javascript
73
   */
74
  bool ReflectIntoJS(JSContext *cx, JS::Handle<JSObject*> rootObj);
75
76
  /**
77
   * Adds a path for inclusion in main thread I/O report.
78
   * @param aPath Directory path
79
   * @param aSubstName Name to substitute for aPath for privacy reasons
80
   */
81
  void AddPath(const nsAString& aPath, const nsAString& aSubstName);
82
83
  /**
84
   * Get size of hash table with file stats
85
   */
86
  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
87
88
  size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
89
90
private:
91
  enum Stage
92
  {
93
    STAGE_STARTUP = 0,
94
    STAGE_NORMAL,
95
    STAGE_SHUTDOWN,
96
    NUM_STAGES
97
  };
98
  static inline Stage NextStage(Stage aStage)
99
  {
100
    switch (aStage) {
101
      case STAGE_STARTUP:
102
        return STAGE_NORMAL;
103
      case STAGE_NORMAL:
104
        return STAGE_SHUTDOWN;
105
      case STAGE_SHUTDOWN:
106
        return STAGE_SHUTDOWN;
107
      default:
108
        return NUM_STAGES;
109
    }
110
  }
111
112
  struct FileStatsByStage
113
  {
114
    FileStats mStats[NUM_STAGES];
115
  };
116
  typedef nsBaseHashtableET<nsStringHashKey, FileStatsByStage> FileIOEntryType;
117
118
  // Statistics for each filename
119
  Common::AutoHashtable<FileIOEntryType> mFileStats;
120
  // Container for whitelisted directories
121
  nsTArray<SafeDir> mSafeDirs;
122
  Stage             mCurStage;
123
124
  /**
125
   * Reflect a FileIOEntryType object to a Javascript property on obj with
126
   * filename as key containing array:
127
   * [totalTime, creates, reads, writes, fsyncs, stats]
128
   */
129
  static bool ReflectFileStats(FileIOEntryType* entry, JSContext *cx,
130
                               JS::Handle<JSObject*> obj);
131
};
132
133
} // namespace Telemetry
134
} // namespace mozilla
135
136
#endif // TelemetryIOInterposeObserver_h__