Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef KeyedStackCapturer_h__
7 : #define KeyedStackCapturer_h__
8 :
9 : #include "Telemetry.h"
10 : #include "nsString.h"
11 : #include "nsClassHashtable.h"
12 : #include "mozilla/Mutex.h"
13 : #include "CombinedStacks.h"
14 :
15 : struct JSContext;
16 :
17 : namespace mozilla {
18 : namespace Telemetry {
19 :
20 : /**
21 : * Allows taking a snapshot of a call stack on demand. Captured stacks are
22 : * indexed by a string key in a hash table. The stack is only captured Once
23 : * for each key. Consequent captures with the same key result in incrementing
24 : * capture counter without re-capturing the stack.
25 : */
26 0 : class KeyedStackCapturer
27 : {
28 : public:
29 3 : KeyedStackCapturer()
30 3 : : mStackCapturerMutex("Telemetry::StackCapturerMutex")
31 3 : {}
32 :
33 : /**
34 : * Captures a stack for the given key.
35 : */
36 : void Capture(const nsACString& aKey);
37 :
38 : /**
39 : * Transforms captured stacks into a JS object.
40 : */
41 : NS_IMETHODIMP ReflectCapturedStacks(
42 : JSContext *cx, JS::MutableHandle<JS::Value> ret);
43 :
44 : /**
45 : * Resets captured stacks and the information related to them.
46 : */
47 : void Clear();
48 :
49 : private:
50 : /**
51 : * Describes how often a stack was captured.
52 : */
53 : struct StackFrequencyInfo {
54 : // A number of times the stack was captured.
55 : uint32_t mCount;
56 : // Index of the stack inside stacks array.
57 : uint32_t mIndex;
58 :
59 0 : StackFrequencyInfo(uint32_t aCount, uint32_t aIndex)
60 0 : : mCount(aCount)
61 0 : , mIndex(aIndex)
62 0 : {}
63 : };
64 :
65 : typedef nsClassHashtable<nsCStringHashKey, StackFrequencyInfo> FrequencyInfoMapType;
66 :
67 : FrequencyInfoMapType mStackInfos;
68 : CombinedStacks mStacks;
69 : Mutex mStackCapturerMutex;
70 : };
71 :
72 : } // namespace Telemetry
73 : } // namespace mozilla
74 :
75 : #endif // KeyedStackCapturer_h__
|