diff options
| author | 2023-01-30 12:57:19 +0000 | |
|---|---|---|
| committer | 2023-01-30 12:57:19 +0000 | |
| commit | c6a55488cc3e100cfb60fd520a851aa9dd363de7 (patch) | |
| tree | 21c119b56f462f54c01b75e654f770b3a4e15b1e | |
| parent | 12788aac85d1aadde99071f79207123ec00bd2be (diff) | |
| parent | 58c310edbec4ec1905fa1cf399aa2df073e34742 (diff) | |
Merge "AudioService: log routing cache clear time, cache sync"
| -rw-r--r-- | services/core/java/com/android/server/audio/AudioSystemAdapter.java | 62 |
1 files changed, 40 insertions, 22 deletions
diff --git a/services/core/java/com/android/server/audio/AudioSystemAdapter.java b/services/core/java/com/android/server/audio/AudioSystemAdapter.java index 7839ada9d5b2..7af7ed5fff65 100644 --- a/services/core/java/com/android/server/audio/AudioSystemAdapter.java +++ b/services/core/java/com/android/server/audio/AudioSystemAdapter.java @@ -37,8 +37,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; @@ -68,11 +72,14 @@ 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(); private ConcurrentHashMap<Pair<AudioAttributes, Boolean>, ArrayList<AudioDeviceAttributes>> mLastDevicesForAttr = new ConcurrentHashMap<>(); + @GuardedBy("sDeviceCacheLock") private ConcurrentHashMap<Pair<AudioAttributes, Boolean>, ArrayList<AudioDeviceAttributes>> mDevicesForAttrCache; - private final Object mDeviceCacheLock = new Object(); + @GuardedBy("sDeviceCacheLock") + private long mDevicesForAttributesCacheClearTimeMs = System.currentTimeMillis(); private int[] mMethodCacheHit; /** * Map that stores all attributes + forVolume pairs that are registered for @@ -249,9 +256,11 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, AudioSystem.setRoutingCallback(sSingletonDefaultAdapter); AudioSystem.setVolumeRangeInitRequestCallback(sSingletonDefaultAdapter); if (USE_CACHE_FOR_GETDEVICES) { - sSingletonDefaultAdapter.mDevicesForAttrCache = - new ConcurrentHashMap<>(AudioSystem.getNumStreamTypes()); - sSingletonDefaultAdapter.mMethodCacheHit = new int[NB_MEASUREMENTS]; + synchronized (sDeviceCacheLock) { + sSingletonDefaultAdapter.mDevicesForAttrCache = + new ConcurrentHashMap<>(AudioSystem.getNumStreamTypes()); + sSingletonDefaultAdapter.mMethodCacheHit = new int[NB_MEASUREMENTS]; + } } if (ENABLE_GETDEVICES_STATS) { sSingletonDefaultAdapter.mMethodCallCounter = new int[NB_MEASUREMENTS]; @@ -265,8 +274,9 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, if (DEBUG_CACHE) { Log.d(TAG, "---- clearing cache ----------"); } - synchronized (mDeviceCacheLock) { + synchronized (sDeviceCacheLock) { if (mDevicesForAttrCache != null) { + mDevicesForAttributesCacheClearTimeMs = System.currentTimeMillis(); // Save latest cache to determine routing updates mLastDevicesForAttr.putAll(mDevicesForAttrCache); @@ -298,7 +308,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 (mDeviceCacheLock) { + synchronized (sDeviceCacheLock) { res = mDevicesForAttrCache.get(key); if (res == null) { res = AudioSystem.getDevicesForAttributes(attributes, forVolume); @@ -656,23 +666,31 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback, */ public void dump(PrintWriter pw) { pw.println("\nAudioSystemAdapter:"); - pw.println(" mDevicesForAttrCache:"); - if (mDevicesForAttrCache != null) { - for (Map.Entry<Pair<AudioAttributes, Boolean>, ArrayList<AudioDeviceAttributes>> - entry : mDevicesForAttrCache.entrySet()) { - final AudioAttributes attributes = entry.getKey().first; - try { - final int stream = attributes.getVolumeControlStream(); - pw.println("\t" + attributes + " forVolume: " + entry.getKey().second - + " stream: " - + AudioSystem.STREAM_NAMES[stream] + "(" + stream + ")"); - for (AudioDeviceAttributes devAttr : entry.getValue()) { - pw.println("\t\t" + devAttr); + 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>> + entry : mDevicesForAttrCache.entrySet()) { + final AudioAttributes attributes = entry.getKey().first; + try { + final int stream = attributes.getVolumeControlStream(); + pw.println("\t" + attributes + " forVolume: " + entry.getKey().second + + " stream: " + + AudioSystem.STREAM_NAMES[stream] + "(" + stream + ")"); + for (AudioDeviceAttributes devAttr : entry.getValue()) { + pw.println("\t\t" + devAttr); + } + } catch (IllegalArgumentException e) { + // dump could fail if attributes do not map to a stream. + pw.println("\t dump failed for attributes: " + attributes); + Log.e(TAG, "dump failed", e); } - } catch (IllegalArgumentException e) { - // dump could fail if attributes do not map to a stream. - pw.println("\t dump failed for attributes: " + attributes); - Log.e(TAG, "dump failed", e); } } } |