Added process name to PID log

Bug: 35357686
Test: included in CL
Change-Id: Ie4d3cb66a2c766ce87ebabcc9c0d3aa988702e09
diff --git a/include/media/nbaio/NBLog.h b/include/media/nbaio/NBLog.h
index ff10b8c..191f0cb 100644
--- a/include/media/nbaio/NBLog.h
+++ b/include/media/nbaio/NBLog.h
@@ -81,7 +81,7 @@
 
     static void    appendInt(String8 *body, const void *data);
     static void    appendFloat(String8 *body, const void *data);
-    static void    appendPID(String8 *body, const void *data);
+    static void    appendPID(String8 *body, const void *data, size_t length);
     static int     handleFormat(const char *fmt, size_t length, const uint8_t *data,
                                 String8 *timestamp, String8 *body);
     static void    appendTimestamp(String8 *body, const void *data);
@@ -177,6 +177,11 @@
     audio_utils_fifo_writer * const mFifoWriter;    // used to write to FIFO,
                                                     // non-NULL unless dummy constructor used
     bool            mEnabled;   // whether to actually log
+
+    // cached pid and process name to use in %p format specifier
+    // total tag length is mPidTagSize and process name is not zero terminated
+    char   *mPidTag;
+    size_t  mPidTagSize;
 };
 
 // ---------------------------------------------------------------------------
diff --git a/media/libnbaio/NBLog.cpp b/media/libnbaio/NBLog.cpp
index 7c2523d..f1c3f10 100644
--- a/media/libnbaio/NBLog.cpp
+++ b/media/libnbaio/NBLog.cpp
@@ -21,6 +21,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
+#include <sys/prctl.h>
 #include <time.h>
 #include <new>
 #include <audio_utils/roundup.h>
@@ -74,7 +75,7 @@
 // ---------------------------------------------------------------------------
 
 NBLog::Writer::Writer()
-    : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false)
+    : mShared(NULL), mFifo(NULL), mFifoWriter(NULL), mEnabled(false), mPidTag(NULL), mPidTagSize(0)
 {
 }
 
@@ -86,6 +87,18 @@
       mFifoWriter(mFifo != NULL ? new audio_utils_fifo_writer(*mFifo) : NULL),
       mEnabled(mFifoWriter != NULL)
 {
+    // caching pid and process name
+    pid_t id = ::getpid();
+    char procName[16];
+    int status = prctl(PR_GET_NAME, procName);
+    if (status) {  // error getting process name
+        procName[0] = '\0';
+    }
+    size_t length = strlen(procName);
+    mPidTagSize = length + sizeof(pid_t);
+    mPidTag = new char[mPidTagSize];
+    memcpy(mPidTag, &id, sizeof(pid_t));
+    memcpy(mPidTag + sizeof(pid_t), procName, length);
 }
 
 NBLog::Writer::Writer(const sp<IMemory>& iMemory, size_t size)
@@ -98,6 +111,7 @@
 {
     delete mFifoWriter;
     delete mFifo;
+    delete[] mPidTag;
 }
 
 void NBLog::Writer::log(const char *string)
@@ -181,15 +195,7 @@
     if (!mEnabled) {
         return;
     }
-    pid_t id = ::getpid();
-    // TODO: append process name to pid
-    // const char* path = sprintf("/proc/%d/status", id);
-    // FILE* f = fopen(path);
-    // size_t length = 30
-    // char buffer[length];
-    // getline(&buffer, &length, f);
-    // char* pidTag = sprintf("")
-    log(EVENT_PID, &id, sizeof(pid_t));
+    log(EVENT_PID, mPidTag, mPidTagSize);
 }
 
 void NBLog::Writer::logStart(const char *fmt)
@@ -271,10 +277,12 @@
             --p;
             break;
 
+        case '%':
+            break;
+
         default:
             ALOGW("NBLog Writer parsed invalid format specifier: %c", *p);
             break;
-        // the '%' case is handled using the formatted string in the reader
         }
     }
     Writer::logEnd();
@@ -583,7 +591,7 @@
             appendFloat(&body, data);
             break;
         case EVENT_PID:
-            appendPID(&body, data);
+            appendPID(&body, data, length);
             break;
         case EVENT_START_FMT:
             advance += handleFormat((const char*) &copy[i + 2], length,
@@ -642,9 +650,10 @@
     body->appendFormat("<%f>", f);
 }
 
-void NBLog::appendPID(String8 *body, const void* data) {
+void NBLog::appendPID(String8 *body, const void* data, size_t length) {
     pid_t id = *((pid_t*) data);
-    body->appendFormat("<PID: %d>", id);
+    char * name = &((char*) data)[sizeof(pid_t)];
+    body->appendFormat("<PID: %d, name: %.*s>", id, (int) (length - sizeof(pid_t)), name);
 }
 
 int NBLog::handleFormat(const char *fmt, size_t fmt_length, const uint8_t *data,
@@ -701,7 +710,6 @@
         case 'd': // integer
             ALOGW_IF(event != EVENT_INTEGER, "NBLog Reader incompatible event for integer specifier: %d", event);
             appendInt(body, datum);
-
             break;
 
         case 'f': // float
@@ -711,7 +719,7 @@
 
         case 'p': // pid
             ALOGW_IF(event != EVENT_PID, "NBLog Reader incompatible event for pid specifier: %d", event);
-            appendPID(body, datum);
+            appendPID(body, datum, length);
             break;
 
         default:
diff --git a/services/audioflinger/Threads.cpp b/services/audioflinger/Threads.cpp
index a16d1cd..ec1ebf5 100644
--- a/services/audioflinger/Threads.cpp
+++ b/services/audioflinger/Threads.cpp
@@ -3041,7 +3041,7 @@
             }
 #if 0
             // logFormat example
-            if (!(z % 100)) {
+            if (z % 100 == 0) {
                 timespec ts;
                 clock_gettime(CLOCK_MONOTONIC, &ts);
                 LOGF("This is an integer %d, this is a float %f, this is my "