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
 {
diff --git a/services/audioflinger/AudioFlinger.h b/services/audioflinger/AudioFlinger.h
index 53a7a8f..37d26b2 100644
--- a/services/audioflinger/AudioFlinger.h
+++ b/services/audioflinger/AudioFlinger.h
@@ -66,6 +66,7 @@
 #include <media/VolumeShaper.h>
 
 #include <audio_utils/clock.h>
+#include <audio_utils/FdToString.h>
 #include <audio_utils/SimpleLog.h>
 #include <audio_utils/TimestampVerifier.h>
 
@@ -426,7 +427,10 @@
     void dumpClients(int fd, const Vector<String16>& args);
     void dumpInternals(int fd, const Vector<String16>& args);
 
-    SimpleLog mThreadLog{10}; // 10 Thread history limit
+    SimpleLog mThreadLog{16}; // 16 Thread history limit
+
+    class ThreadBase;
+    void dumpToThreadLog_l(const sp<ThreadBase> &thread);
 
     // --- Client ---
     class Client : public RefBase {
diff --git a/services/audioflinger/Threads.h b/services/audioflinger/Threads.h
index b6e8abf..bb37a0a 100644
--- a/services/audioflinger/Threads.h
+++ b/services/audioflinger/Threads.h
@@ -397,6 +397,8 @@
 
                 bool                isMsdDevice() const { return mIsMsdDevice; }
 
+    virtual     void                dump(int fd, const Vector<String16>& args) = 0;
+
     mutable     Mutex                   mLock;
 
 protected:
@@ -665,7 +667,7 @@
                    audio_io_handle_t id, audio_devices_t device, type_t type, bool systemReady);
     virtual             ~PlaybackThread();
 
-                void        dump(int fd, const Vector<String16>& args);
+                void        dump(int fd, const Vector<String16>& args) override;
                 // returns a string of audio performance related data in JSON format.
     virtual     Json::Value getJsonDump() const;
 
@@ -1480,7 +1482,7 @@
             // return true if the caller should then do it's part of the stopping process
             bool        stop(RecordTrack* recordTrack);
 
-            void        dump(int fd, const Vector<String16>& args);
+            void        dump(int fd, const Vector<String16>& args) override;
             AudioStreamIn* clearInput();
             virtual sp<StreamHalInterface> stream() const;
 
@@ -1689,7 +1691,7 @@
                 // Sets the UID records silence
     virtual     void        setRecordSilenced(uid_t uid __unused, bool silenced __unused) {}
 
-                void        dump(int fd, const Vector<String16>& args);
+                void        dump(int fd, const Vector<String16>& args) override;
     virtual     void        dumpInternals(int fd, const Vector<String16>& args);
                 void        dumpTracks(int fd, const Vector<String16>& args);