AudioFlinger: Preserve recent MMapThread history for dumpsys

Test: Aaudio tests, check audioflinger dumpsys
Bug: 116026119
Change-Id: Ib891686480b2579e4d3b2e419c83aec0a79ba812
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index e62156e..47dbabf 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -56,7 +56,6 @@
 #include <system/audio_effects/effect_ns.h>
 #include <system/audio_effects/effect_aec.h>
 
-#include <audio_utils/FdToString.h>
 #include <audio_utils/primitives.h>
 
 #include <json/json.h>
@@ -2300,15 +2299,7 @@
         if (playbackThread != NULL) {
             ALOGV("closeOutput() %d", output);
 
-            {
-                // Dump thread before deleting for history
-                audio_utils::FdToString fdToString;
-                const int fd = fdToString.fd();
-                if (fd >= 0) {
-                    playbackThread->dump(fd, {} /* args */);
-                    mThreadLog.logs(-1 /* time */, fdToString.getStringAndClose());
-                }
-            }
+            dumpToThreadLog_l(playbackThread);
 
             if (playbackThread->type() == ThreadBase::MIXER) {
                 for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
@@ -2341,6 +2332,7 @@
             if (mmapThread == 0) {
                 return BAD_VALUE;
             }
+            dumpToThreadLog_l(mmapThread);
             mMmapThreads.removeItem(output);
             ALOGD("closing mmapThread %p", mmapThread.get());
         }
@@ -2549,15 +2541,7 @@
         if (recordThread != 0) {
             ALOGV("closeInput() %d", input);
 
-            {
-                // Dump thread before deleting for history
-                audio_utils::FdToString fdToString;
-                const int fd = fdToString.fd();
-                if (fd >= 0) {
-                    recordThread->dump(fd, {} /* args */);
-                    mThreadLog.logs(-1 /* time */, fdToString.getStringAndClose());
-                }
-            }
+            dumpToThreadLog_l(recordThread);
 
             // If we still have effect chains, it means that a client still holds a handle
             // on at least one effect. We must either move the chain to an existing thread with the
@@ -2601,6 +2585,7 @@
             if (mmapThread == 0) {
                 return BAD_VALUE;
             }
+            dumpToThreadLog_l(mmapThread);
             mMmapThreads.removeItem(input);
         }
         const sp<AudioIoDescriptor> ioDesc = new AudioIoDescriptor();
@@ -2799,6 +2784,17 @@
     return;
 }
 
+// dumpToThreadLog_l() must be called with AudioFlinger::mLock held
+void AudioFlinger::dumpToThreadLog_l(const sp<ThreadBase> &thread)
+{
+    audio_utils::FdToString fdToString;
+    const int fd = fdToString.fd();
+    if (fd >= 0) {
+        thread->dump(fd, {} /* args */);
+        mThreadLog.logs(-1 /* time */, fdToString.getStringAndClose());
+    }
+}
+
 // checkThread_l() must be called with AudioFlinger::mLock held
 AudioFlinger::ThreadBase *AudioFlinger::checkThread_l(audio_io_handle_t ioHandle) const
 {