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 #include "mozilla/SchedulerGroup.h"
8 
9 #include <utility>
10 
11 #include "jsfriendapi.h"
12 #include "mozilla/Atomics.h"
13 #include "mozilla/Telemetry.h"
14 #include "mozilla/Unused.h"
15 #include "mozilla/dom/DocGroup.h"
16 #include "mozilla/dom/ScriptSettings.h"
17 #include "nsINamed.h"
18 #include "nsQueryObject.h"
19 #include "nsThreadUtils.h"
20 
21 using namespace mozilla;
22 
23 namespace {
24 
25 static Atomic<uint64_t> gEarliestUnprocessedVsync(0);
26 
27 }  // namespace
28 
29 /* static */
UnlabeledDispatch(TaskCategory aCategory,already_AddRefed<nsIRunnable> && aRunnable)30 nsresult SchedulerGroup::UnlabeledDispatch(
31     TaskCategory aCategory, already_AddRefed<nsIRunnable>&& aRunnable) {
32   if (NS_IsMainThread()) {
33     return NS_DispatchToCurrentThread(std::move(aRunnable));
34   } else {
35     return NS_DispatchToMainThread(std::move(aRunnable));
36   }
37 }
38 
39 /* static */
MarkVsyncReceived()40 void SchedulerGroup::MarkVsyncReceived() {
41   // May be called on any thread when a vsync is received and scheduled to be
42   // processed. This may occur on the main thread due to queued messages when
43   // the channel is connected.
44   bool inconsistent = false;
45   TimeStamp creation = TimeStamp::ProcessCreation(&inconsistent);
46   if (inconsistent) {
47     return;
48   }
49 
50   // Attempt to set gEarliestUnprocessedVsync to our new value. If we've seen a
51   // vsync already, but haven't handled it, the `compareExchange` will fail and
52   // the static won't be updated.
53   uint64_t unprocessedVsync =
54       uint64_t((TimeStamp::Now() - creation).ToMicroseconds());
55   gEarliestUnprocessedVsync.compareExchange(0, unprocessedVsync);
56 }
57 
58 /* static */
MarkVsyncRan()59 void SchedulerGroup::MarkVsyncRan() { gEarliestUnprocessedVsync = 0; }
60 
SchedulerGroup()61 SchedulerGroup::SchedulerGroup() : mIsRunning(false) {}
62 
63 /* static */
Dispatch(TaskCategory aCategory,already_AddRefed<nsIRunnable> && aRunnable)64 nsresult SchedulerGroup::Dispatch(TaskCategory aCategory,
65                                   already_AddRefed<nsIRunnable>&& aRunnable) {
66   return LabeledDispatch(aCategory, std::move(aRunnable), nullptr);
67 }
68 
69 /* static */
LabeledDispatch(TaskCategory aCategory,already_AddRefed<nsIRunnable> && aRunnable,mozilla::PerformanceCounter * aPerformanceCounter)70 nsresult SchedulerGroup::LabeledDispatch(
71     TaskCategory aCategory, already_AddRefed<nsIRunnable>&& aRunnable,
72     mozilla::PerformanceCounter* aPerformanceCounter) {
73   nsCOMPtr<nsIRunnable> runnable(aRunnable);
74   if (XRE_IsContentProcess()) {
75     RefPtr<Runnable> internalRunnable =
76         new Runnable(runnable.forget(), aPerformanceCounter);
77     return InternalUnlabeledDispatch(aCategory, internalRunnable.forget());
78   }
79   return UnlabeledDispatch(aCategory, runnable.forget());
80 }
81 
82 /*static*/
InternalUnlabeledDispatch(TaskCategory aCategory,already_AddRefed<Runnable> && aRunnable)83 nsresult SchedulerGroup::InternalUnlabeledDispatch(
84     TaskCategory aCategory, already_AddRefed<Runnable>&& aRunnable) {
85   if (NS_IsMainThread()) {
86     // NS_DispatchToCurrentThread will not leak the passed in runnable
87     // when it fails, so we don't need to do anything special.
88     return NS_DispatchToCurrentThread(std::move(aRunnable));
89   }
90 
91   RefPtr<Runnable> runnable(aRunnable);
92   nsresult rv = NS_DispatchToMainThread(do_AddRef(runnable));
93   if (NS_FAILED(rv)) {
94     // Dispatch failed.  This is a situation where we would have used
95     // NS_DispatchToMainThread rather than calling into the SchedulerGroup
96     // machinery, and the caller would be expecting to leak the nsIRunnable
97     // originally passed in.  But because we've had to wrap things up
98     // internally, we were going to leak the nsIRunnable *and* our Runnable
99     // wrapper.  But there's no reason that we have to leak our Runnable
100     // wrapper; we can just leak the wrapped nsIRunnable, and let the caller
101     // take care of unleaking it if they need to.
102     Unused << runnable->mRunnable.forget().take();
103     nsrefcnt refcnt = runnable.get()->Release();
104     MOZ_RELEASE_ASSERT(refcnt == 1, "still holding an unexpected reference!");
105   }
106 
107   return rv;
108 }
109 
Runnable(already_AddRefed<nsIRunnable> && aRunnable,mozilla::PerformanceCounter * aPerformanceCounter)110 SchedulerGroup::Runnable::Runnable(
111     already_AddRefed<nsIRunnable>&& aRunnable,
112     mozilla::PerformanceCounter* aPerformanceCounter)
113     : mozilla::Runnable("SchedulerGroup::Runnable"),
114       mRunnable(std::move(aRunnable)),
115       mPerformanceCounter(aPerformanceCounter) {}
116 
GetPerformanceCounter() const117 mozilla::PerformanceCounter* SchedulerGroup::Runnable::GetPerformanceCounter()
118     const {
119   return mPerformanceCounter;
120 }
121 
122 #ifdef MOZ_COLLECTING_RUNNABLE_TELEMETRY
123 NS_IMETHODIMP
GetName(nsACString & aName)124 SchedulerGroup::Runnable::GetName(nsACString& aName) {
125   // Try to get a name from the underlying runnable.
126   nsCOMPtr<nsINamed> named = do_QueryInterface(mRunnable);
127   if (named) {
128     named->GetName(aName);
129   }
130   if (aName.IsEmpty()) {
131     aName.AssignLiteral("anonymous");
132   }
133 
134   return NS_OK;
135 }
136 #endif
137 
138 NS_IMETHODIMP
Run()139 SchedulerGroup::Runnable::Run() {
140   MOZ_RELEASE_ASSERT(NS_IsMainThread());
141   // The runnable's destructor can have side effects, so try to execute it in
142   // the scope of the SchedulerGroup.
143   nsCOMPtr<nsIRunnable> runnable(std::move(mRunnable));
144   return runnable->Run();
145 }
146 
147 NS_IMETHODIMP
GetPriority(uint32_t * aPriority)148 SchedulerGroup::Runnable::GetPriority(uint32_t* aPriority) {
149   *aPriority = nsIRunnablePriority::PRIORITY_NORMAL;
150   nsCOMPtr<nsIRunnablePriority> runnablePrio = do_QueryInterface(mRunnable);
151   return runnablePrio ? runnablePrio->GetPriority(aPriority) : NS_OK;
152 }
153 
154 NS_IMPL_ISUPPORTS_INHERITED(SchedulerGroup::Runnable, mozilla::Runnable,
155                             nsIRunnablePriority, SchedulerGroup::Runnable)
156