2 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "PerActivityStateCPUUsageSampler.h"
30 #include "WebProcessPool.h"
31 #include "WebProcessProxy.h"
32 #include <WebCore/DiagnosticLoggingKeys.h>
33 #include <wtf/DecimalNumber.h>
37 using namespace WebCore;
39 static const Seconds loggingInterval { 60_min };
41 PerActivityStateCPUUsageSampler::PerActivityStateCPUUsageSampler(WebProcessPool& processPool)
42 : m_processPool(processPool)
43 , m_loggingTimer(RunLoop::main(), this, &PerActivityStateCPUUsageSampler::loggingTimerFired)
45 m_lastCPUTime = MonotonicTime::now();
46 m_loggingTimer.startRepeating(loggingInterval);
49 PerActivityStateCPUUsageSampler::~PerActivityStateCPUUsageSampler()
53 void PerActivityStateCPUUsageSampler::reportWebContentCPUTime(Seconds cpuTime, ActivityStateForCPUSampling activityState)
55 auto result = m_cpuTimeInActivityState.add(activityState, cpuTime);
56 if (!result.isNewEntry)
57 result.iterator->value += cpuTime;
60 static inline String loggingKeyForActivityState(ActivityStateForCPUSampling state)
63 case ActivityStateForCPUSampling::NonVisible:
64 return DiagnosticLoggingKeys::nonVisibleStateKey();
65 case ActivityStateForCPUSampling::VisibleNonActive:
66 return DiagnosticLoggingKeys::visibleNonActiveStateKey();
67 case ActivityStateForCPUSampling::VisibleAndActive:
68 return DiagnosticLoggingKeys::visibleAndActiveStateKey();
72 void PerActivityStateCPUUsageSampler::loggingTimerFired()
74 auto* page = pageForLogging();
76 m_cpuTimeInActivityState.clear();
80 MonotonicTime currentCPUTime = MonotonicTime::now();
81 Seconds cpuTimeDelta = currentCPUTime - m_lastCPUTime;
83 for (auto& pair : m_cpuTimeInActivityState) {
84 double cpuUsage = pair.value.value() * 100. / cpuTimeDelta.value();
85 String activityStateKey = loggingKeyForActivityState(pair.key);
86 page->logDiagnosticMessageWithValue(DiagnosticLoggingKeys::cpuUsageKey(), activityStateKey, cpuUsage, 2, ShouldSample::No);
87 RELEASE_LOG(PerformanceLogging, "WebContent processes used %.1f%% CPU in %s state", cpuUsage, activityStateKey.utf8().data());
90 m_cpuTimeInActivityState.clear();
91 m_lastCPUTime = currentCPUTime;
94 WebPageProxy* PerActivityStateCPUUsageSampler::pageForLogging() const
96 for (auto& webProcess : m_processPool.processes()) {
97 if (!webProcess->pageCount())
99 return *webProcess->pages().begin();
104 } // namespace WebKit