From 89cbeb14b1c909b484753c6f978b161361c55abe Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Thu, 27 Dec 2018 09:49:02 -0800 Subject: [PATCH] Do not hold synchronization locks if tracing is not enabled Change-Id: Ia17e2269d088da472dce2e248fc9f85877be5d77 --- .../android/launcher3/util/TraceHelper.java | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/src/com/android/launcher3/util/TraceHelper.java b/src/com/android/launcher3/util/TraceHelper.java index a321bcc205..4fea2e92ae 100644 --- a/src/com/android/launcher3/util/TraceHelper.java +++ b/src/com/android/launcher3/util/TraceHelper.java @@ -39,35 +39,39 @@ public class TraceHelper { private static final boolean SYSTEM_TRACE = false; private static final ArrayMap sUpTimes = ENABLED ? new ArrayMap<>() : null; - public static synchronized void beginSection(String sectionName) { + public static void beginSection(String sectionName) { if (ENABLED) { - MutableLong time = sUpTimes.get(sectionName); - if (time == null) { - time = new MutableLong(isLoggable(sectionName, VERBOSE) ? 0 : -1); - sUpTimes.put(sectionName, time); - } - if (time.value >= 0) { - if (SYSTEM_TRACE) { - Trace.beginSection(sectionName); + synchronized (sUpTimes) { + MutableLong time = sUpTimes.get(sectionName); + if (time == null) { + time = new MutableLong(isLoggable(sectionName, VERBOSE) ? 0 : -1); + sUpTimes.put(sectionName, time); + } + if (time.value >= 0) { + if (SYSTEM_TRACE) { + Trace.beginSection(sectionName); + } + time.value = SystemClock.uptimeMillis(); } - time.value = SystemClock.uptimeMillis(); } } } - public static synchronized void partitionSection(String sectionName, String partition) { + public static void partitionSection(String sectionName, String partition) { if (ENABLED) { - MutableLong time = sUpTimes.get(sectionName); - if (time != null && time.value >= 0) { + synchronized (sUpTimes) { + MutableLong time = sUpTimes.get(sectionName); + if (time != null && time.value >= 0) { - if (SYSTEM_TRACE) { - Trace.endSection(); - Trace.beginSection(sectionName); + if (SYSTEM_TRACE) { + Trace.endSection(); + Trace.beginSection(sectionName); + } + + long now = SystemClock.uptimeMillis(); + Log.d(sectionName, partition + " : " + (now - time.value)); + time.value = now; } - - long now = SystemClock.uptimeMillis(); - Log.d(sectionName, partition + " : " + (now - time.value)); - time.value = now; } } } @@ -78,14 +82,16 @@ public class TraceHelper { } } - public static synchronized void endSection(String sectionName, String msg) { + public static void endSection(String sectionName, String msg) { if (ENABLED) { - MutableLong time = sUpTimes.get(sectionName); - if (time != null && time.value >= 0) { - if (SYSTEM_TRACE) { - Trace.endSection(); + synchronized (sUpTimes) { + MutableLong time = sUpTimes.get(sectionName); + if (time != null && time.value >= 0) { + if (SYSTEM_TRACE) { + Trace.endSection(); + } + Log.d(sectionName, msg + " : " + (SystemClock.uptimeMillis() - time.value)); } - Log.d(sectionName, msg + " : " + (SystemClock.uptimeMillis() - time.value)); } } }