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 #include "mozilla/Logging.h"
8 #include "mozilla/PerformanceCounter.h"
10 using mozilla::DispatchCategory
;
11 using mozilla::DispatchCounter
;
12 using mozilla::PerformanceCounter
;
14 static mozilla::LazyLogModule
sPerformanceCounter("PerformanceCounter");
18 #define LOG(args) MOZ_LOG(sPerformanceCounter, mozilla::LogLevel::Debug, args)
20 // Global counter used by PerformanceCounter CTOR via NextCounterID().
21 static mozilla::Atomic
<uint64_t> gNextCounterID(0);
23 static uint64_t NextCounterID() {
24 // This can return the same value on different processes but
25 // we're fine with this behavior because consumers can use a (pid, counter_id)
26 // tuple to make instances globally unique in a browser session.
27 return ++gNextCounterID
;
30 // this instance is the extension for the worker
31 const DispatchCategory
DispatchCategory::Worker
=
32 DispatchCategory((uint32_t)TaskCategory::Count
);
34 PerformanceCounter::PerformanceCounter(const nsACString
& aName
)
35 : mExecutionDuration(0),
36 mTotalDispatchCount(0),
39 mID(NextCounterID()) {
40 LOG(("PerformanceCounter created with ID %" PRIu64
, mID
));
43 void PerformanceCounter::IncrementDispatchCounter(DispatchCategory aCategory
) {
44 mDispatchCounter
[aCategory
.GetValue()] += 1;
45 mTotalDispatchCount
+= 1;
46 LOG(("[%s][%" PRIu64
"] Total dispatch %" PRIu64
, mName
.get(), GetID(),
47 uint64_t(mTotalDispatchCount
)));
50 void PerformanceCounter::IncrementExecutionDuration(uint32_t aMicroseconds
) {
51 mExecutionDuration
+= aMicroseconds
;
52 LOG(("[%s][%" PRIu64
"] Total duration %" PRIu64
, mName
.get(), GetID(),
53 uint64_t(mExecutionDuration
)));
56 const DispatchCounter
& PerformanceCounter::GetDispatchCounter() const {
57 return mDispatchCounter
;
60 uint64_t PerformanceCounter::GetExecutionDuration() const {
61 return mExecutionDuration
;
64 uint64_t PerformanceCounter::GetTotalDispatchCount() const {
65 return mTotalDispatchCount
;
68 uint32_t PerformanceCounter::GetDispatchCount(
69 DispatchCategory aCategory
) const {
70 return mDispatchCounter
[aCategory
.GetValue()];
73 uint64_t PerformanceCounter::GetID() const { return mID
; }