Loading services/core/java/com/android/server/audio/AudioSystemAdapter.java +41 −22 Original line number Diff line number Diff line Loading @@ -29,8 +29,12 @@ import android.util.Pair; import com.android.internal.annotations.GuardedBy; import java.io.PrintWriter; import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; Loading Loading @@ -60,8 +64,12 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, private String[] mMethodNames = {"getDevicesForAttributes"}; private static final boolean USE_CACHE_FOR_GETDEVICES = true; private static final Object sDeviceCacheLock = new Object(); @GuardedBy("sDeviceCacheLock") private ConcurrentHashMap<Pair<AudioAttributes, Boolean>, ArrayList<AudioDeviceAttributes>> mDevicesForAttrCache; @GuardedBy("sDeviceCacheLock") private long mDevicesForAttributesCacheClearTimeMs = System.currentTimeMillis(); private int[] mMethodCacheHit; private static final Object sRoutingListenerLock = new Object(); @GuardedBy("sRoutingListenerLock") Loading Loading @@ -147,10 +155,12 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, AudioSystem.setRoutingCallback(sSingletonDefaultAdapter); AudioSystem.setVolumeRangeInitRequestCallback(sSingletonDefaultAdapter); if (USE_CACHE_FOR_GETDEVICES) { synchronized (sDeviceCacheLock) { sSingletonDefaultAdapter.mDevicesForAttrCache = new ConcurrentHashMap<>(AudioSystem.getNumStreamTypes()); sSingletonDefaultAdapter.mMethodCacheHit = new int[NB_MEASUREMENTS]; } } if (ENABLE_GETDEVICES_STATS) { sSingletonDefaultAdapter.mMethodCallCounter = new int[NB_MEASUREMENTS]; sSingletonDefaultAdapter.mMethodTimeNs = new long[NB_MEASUREMENTS]; Loading @@ -163,8 +173,9 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, if (DEBUG_CACHE) { Log.d(TAG, "---- clearing cache ----------"); } synchronized (sDeviceCacheLock) { if (mDevicesForAttrCache != null) { synchronized (mDevicesForAttrCache) { mDevicesForAttributesCacheClearTimeMs = System.currentTimeMillis(); mDevicesForAttrCache.clear(); } } Loading Loading @@ -193,7 +204,7 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, if (USE_CACHE_FOR_GETDEVICES) { ArrayList<AudioDeviceAttributes> res; final Pair<AudioAttributes, Boolean> key = new Pair(attributes, forVolume); synchronized (mDevicesForAttrCache) { synchronized (sDeviceCacheLock) { res = mDevicesForAttrCache.get(key); if (res == null) { // result from AudioSystem guaranteed non-null, but could be invalid Loading Loading @@ -508,6 +519,13 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, */ public void dump(PrintWriter pw) { pw.println("\nAudioSystemAdapter:"); final DateTimeFormatter formatter = DateTimeFormatter .ofPattern("MM-dd HH:mm:ss:SSS") .withLocale(Locale.US) .withZone(ZoneId.systemDefault()); synchronized (sDeviceCacheLock) { pw.println(" last cache clear time: " + formatter.format( Instant.ofEpochMilli(mDevicesForAttributesCacheClearTimeMs))); pw.println(" mDevicesForAttrCache:"); if (mDevicesForAttrCache != null) { for (Map.Entry<Pair<AudioAttributes, Boolean>, ArrayList<AudioDeviceAttributes>> Loading @@ -528,6 +546,7 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, } } } } if (!ENABLE_GETDEVICES_STATS) { // only stats in the rest of this dump Loading Loading
services/core/java/com/android/server/audio/AudioSystemAdapter.java +41 −22 Original line number Diff line number Diff line Loading @@ -29,8 +29,12 @@ import android.util.Pair; import com.android.internal.annotations.GuardedBy; import java.io.PrintWriter; import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; Loading Loading @@ -60,8 +64,12 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, private String[] mMethodNames = {"getDevicesForAttributes"}; private static final boolean USE_CACHE_FOR_GETDEVICES = true; private static final Object sDeviceCacheLock = new Object(); @GuardedBy("sDeviceCacheLock") private ConcurrentHashMap<Pair<AudioAttributes, Boolean>, ArrayList<AudioDeviceAttributes>> mDevicesForAttrCache; @GuardedBy("sDeviceCacheLock") private long mDevicesForAttributesCacheClearTimeMs = System.currentTimeMillis(); private int[] mMethodCacheHit; private static final Object sRoutingListenerLock = new Object(); @GuardedBy("sRoutingListenerLock") Loading Loading @@ -147,10 +155,12 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, AudioSystem.setRoutingCallback(sSingletonDefaultAdapter); AudioSystem.setVolumeRangeInitRequestCallback(sSingletonDefaultAdapter); if (USE_CACHE_FOR_GETDEVICES) { synchronized (sDeviceCacheLock) { sSingletonDefaultAdapter.mDevicesForAttrCache = new ConcurrentHashMap<>(AudioSystem.getNumStreamTypes()); sSingletonDefaultAdapter.mMethodCacheHit = new int[NB_MEASUREMENTS]; } } if (ENABLE_GETDEVICES_STATS) { sSingletonDefaultAdapter.mMethodCallCounter = new int[NB_MEASUREMENTS]; sSingletonDefaultAdapter.mMethodTimeNs = new long[NB_MEASUREMENTS]; Loading @@ -163,8 +173,9 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, if (DEBUG_CACHE) { Log.d(TAG, "---- clearing cache ----------"); } synchronized (sDeviceCacheLock) { if (mDevicesForAttrCache != null) { synchronized (mDevicesForAttrCache) { mDevicesForAttributesCacheClearTimeMs = System.currentTimeMillis(); mDevicesForAttrCache.clear(); } } Loading Loading @@ -193,7 +204,7 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, if (USE_CACHE_FOR_GETDEVICES) { ArrayList<AudioDeviceAttributes> res; final Pair<AudioAttributes, Boolean> key = new Pair(attributes, forVolume); synchronized (mDevicesForAttrCache) { synchronized (sDeviceCacheLock) { res = mDevicesForAttrCache.get(key); if (res == null) { // result from AudioSystem guaranteed non-null, but could be invalid Loading Loading @@ -508,6 +519,13 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, */ public void dump(PrintWriter pw) { pw.println("\nAudioSystemAdapter:"); final DateTimeFormatter formatter = DateTimeFormatter .ofPattern("MM-dd HH:mm:ss:SSS") .withLocale(Locale.US) .withZone(ZoneId.systemDefault()); synchronized (sDeviceCacheLock) { pw.println(" last cache clear time: " + formatter.format( Instant.ofEpochMilli(mDevicesForAttributesCacheClearTimeMs))); pw.println(" mDevicesForAttrCache:"); if (mDevicesForAttrCache != null) { for (Map.Entry<Pair<AudioAttributes, Boolean>, ArrayList<AudioDeviceAttributes>> Loading @@ -528,6 +546,7 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, } } } } if (!ENABLE_GETDEVICES_STATS) { // only stats in the rest of this dump Loading