1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "NativeProfilerImpl.h"
8
9 #include "UncensoredAllocator.h"
10
11 namespace mozilla {
12
NativeProfilerImpl()13 NativeProfilerImpl::NativeProfilerImpl()
14 {
15 mLock = PR_NewLock();
16 }
17
~NativeProfilerImpl()18 NativeProfilerImpl::~NativeProfilerImpl()
19 {
20 if (mLock) {
21 PR_DestroyLock(mLock);
22 }
23 }
24
25 nsTArray<nsCString>
GetNames() const26 NativeProfilerImpl::GetNames() const
27 {
28 return mTraceTable.GetNames();
29 }
30
31 nsTArray<TrieNode>
GetTraces() const32 NativeProfilerImpl::GetTraces() const
33 {
34 return mTraceTable.GetTraces();
35 }
36
37 const nsTArray<AllocEvent>&
GetEvents() const38 NativeProfilerImpl::GetEvents() const
39 {
40 return mAllocEvents;
41 }
42
43 void
reset()44 NativeProfilerImpl::reset()
45 {
46 mTraceTable.Reset();
47 mAllocEvents.Clear();
48 mNativeEntries.Clear();
49 }
50
51 void
sampleNative(void * addr,uint32_t size)52 NativeProfilerImpl::sampleNative(void* addr, uint32_t size)
53 {
54 AutoUseUncensoredAllocator ua;
55 AutoMPLock lock(mLock);
56 size_t nSamples = AddBytesSampled(size);
57 if (nSamples > 0) {
58 nsTArray<nsCString> trace = GetStacktrace();
59 AllocEvent ai(mTraceTable.Insert(trace), nSamples * mSampleSize, TimeStamp::Now());
60 mNativeEntries.Put(addr, AllocEntry(mAllocEvents.Length()));
61 mAllocEvents.AppendElement(ai);
62 }
63 }
64
65 void
removeNative(void * addr)66 NativeProfilerImpl::removeNative(void* addr)
67 {
68 AutoUseUncensoredAllocator ua;
69 AutoMPLock lock(mLock);
70
71 AllocEntry entry;
72 if (!mNativeEntries.Get(addr, &entry)) {
73 return;
74 }
75
76 AllocEvent& oldEvent = mAllocEvents[entry.mEventIdx];
77 AllocEvent newEvent(oldEvent.mTraceIdx, -oldEvent.mSize, TimeStamp::Now());
78 mAllocEvents.AppendElement(newEvent);
79 mNativeEntries.Remove(addr);
80 }
81
82 } // namespace mozilla
83