Bug 1432429 Improve the macros, logging, and comments in Fuzzyfox.cpp 10/14
MozReview-Commit-ID: 28abz1Lm0Qv
--- a/toolkit/components/fuzzyfox/Fuzzyfox.cpp
+++ b/toolkit/components/fuzzyfox/Fuzzyfox.cpp
@@ -24,24 +24,24 @@
using namespace mozilla;
static LazyLogModule sFuzzyfoxLog("Fuzzyfox");
#define PT_STATIC_CLOCK_GRAIN 80
#define PT_DURATION_CENTER 100000
-#define US_TO_NS(x) (x*1000)
-#define NS_TO_US(x) (x/1000)
+#define US_TO_NS(x) ((x)*1000)
+#define NS_TO_US(x) ((x)/1000)
#ifdef LOG
#undef LOG
#endif
-#define LOG(args) MOZ_LOG(sFuzzyfoxLog, mozilla::LogLevel::Debug, args)
+#define LOG(level, args) MOZ_LOG(sFuzzyfoxLog, mozilla::LogLevel::level, args)
/* static */ void
Fuzzyfox::Start()
{
MOZ_ASSERT(NS_IsMainThread());
RefPtr<Fuzzyfox> r = new Fuzzyfox();
SystemGroup::Dispatch(TaskCategory::Other, r.forget());
@@ -54,46 +54,53 @@ Fuzzyfox::Fuzzyfox()
, mTickType(eUptick)
{
MOZ_ASSERT(NS_IsMainThread());
}
Fuzzyfox::~Fuzzyfox() = default;
NS_IMETHODIMP
+
+#define DISPATCH_AND_RETURN() \
+ nsCOMPtr<nsIRunnable> r = this; \
+ SystemGroup::Dispatch(TaskCategory::Other, r.forget()); \
+ return NS_OK
+
+NS_IMETHODIMP
Fuzzyfox::Run()
{
MOZ_ASSERT(NS_IsMainThread());
// We need to check how long its been since we ran
uint64_t endTime = ActualTime();
uint64_t remaining = 0;
uint64_t durationCount = 1;
// Pick the amount to sleep
if ((endTime - mStartTime) > mDuration) {
// We ran over our budget!
uint64_t over = (endTime - mStartTime) - mDuration;
- LOG(("[Fuzzyfox][FuzzyfoxEvent] PT(%p) Overran budget of %" PRIu32 " by %" PRIu64 " \n",
+ LOG(Verbose, ("[FuzzyfoxEvent] PT(%p) Overran budget of %" PRIu32 " by %" PRIu64 " \n",
this, mDuration, over));
//TODO: this should probably influence uptick/downtick stuff, and it doesn't
uint64_t nextDuration = PickDuration();
while (over > nextDuration) {
durationCount++;
over -= nextDuration;
nextDuration = PickDuration();
}
remaining = nextDuration - over;
} else {
// Didn't go over budget
remaining = mDuration - (endTime - mStartTime);
- LOG(("[Fuzzyfox][FuzzyfoxEvent] PT(%p) Finishing budget of %" PRIu32 " with %" PRIu64 " \n",
+ LOG(Verbose, ("[FuzzyfoxEvent] PT(%p) Finishing budget of %" PRIu32 " with %" PRIu64 " \n",
this, mDuration, remaining));
}
// Sleep for now
#ifdef XP_WIN
Sleep(remaining);
#else
@@ -105,22 +112,22 @@ Fuzzyfox::Run()
// Update clocks (and fire pending events etc)
UpdateClocks();
// Reset values
mTickType = mTickType == eUptick ? eDowntick : eUptick;
mStartTime = ActualTime();
mDuration = PickDuration();
- // Queue next event
- nsCOMPtr<nsIRunnable> r = this;
- SystemGroup::Dispatch(TaskCategory::Other, r.forget());
- return NS_OK;
+ DISPATCH_AND_RETURN();
}
+/*
+ * ActualTime returns a value in microseconds.
+ */
uint64_t
Fuzzyfox::ActualTime()
{
return PR_Now();
}
uint64_t
Fuzzyfox::PickDuration()
@@ -138,17 +145,22 @@ Fuzzyfox::UpdateClocks()
{
uint64_t time = ActualTime();
TimeStamp timestamp = TimeStamp::NowReally();
uint64_t newTime = RoundToGrain(time);
TimeStamp newTimeStamp = RoundToGrain(timestamp);
// newTime is the new canonical time for this scope!
- LOG(("[Fuzzyfox][Fuzzyfox][Time] New time is %" PRIu64 "\n", newTimeStamp));
+ #ifndef XP_WIN
+ LOG(Verbose, ("[Time] New time is %" PRIu64 " (compare to %" PRIu64 ") and timestamp is %" PRIu64 " (compare to %" PRIu64 ")\n",
+ aNewTime, ActualTime(), aNewTimeStamp.mValue, TimeStamp::NowReally().mValue));
+ #else
+ LOG(Verbose, ("[Time] New time is %" PRIu64 " (compare to %" PRIu64 ") \n", aNewTime, ActualTime()));
+ #endif
// Fire notifications
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
if (NS_WARN_IF(!os)) {
return;
}
// Event firings on occur on downticks and have no data
@@ -159,17 +171,17 @@ Fuzzyfox::UpdateClocks()
nsCOMPtr<nsISupportsPRInt64> wrapper =
do_CreateInstance(NS_SUPPORTS_PRINT64_CONTRACTID);
if (NS_WARN_IF(!wrapper)) {
return;
}
wrapper->SetData(newTime);
- // Clocks get the official 'realtime' time
+ // Clocks get the official 'realtime' time in microseconds
// This happens on all ticks
os->NotifyObservers(wrapper, "fuzzyfox-update-clocks", nullptr);
// Update the timestamp canonicaltime
TimeStamp::UpdateFuzzyTimeStamp(newTimeStamp);
}
uint64_t