Merge "storage: fix dumpstate avc denials"
diff --git a/powerstats/CpupmStateResidencyDataProvider.cpp b/powerstats/CpupmStateResidencyDataProvider.cpp
index c963f78..2adae54 100644
--- a/powerstats/CpupmStateResidencyDataProvider.cpp
+++ b/powerstats/CpupmStateResidencyDataProvider.cpp
@@ -34,8 +34,14 @@
 namespace stats {
 
 CpupmStateResidencyDataProvider::CpupmStateResidencyDataProvider(
-        const std::string &path, const Config &config)
-    : mPath(std::move(path)), mConfig(std::move(config)) {}
+        const std::string &path,
+        const Config &config,
+        const std::string &sleepPath,
+        const SleepConfig &sleepConfig)
+    : mPath(std::move(path)),
+      mConfig(std::move(config)),
+      mSleepPath(std::move(sleepPath)),
+      mSleepConfig(std::move(sleepConfig)) {}
 
 int32_t CpupmStateResidencyDataProvider::matchState(char const *line) {
     for (int32_t i = 0; i < mConfig.states.size(); i++) {
@@ -78,6 +84,12 @@
         return false;
     }
 
+    std::unique_ptr<FILE, decltype(&fclose)> sleepFp(fopen(mSleepPath.c_str(), "r"), fclose);
+    if (!sleepFp) {
+        PLOG(ERROR) << __func__ << ":Failed to open file " << mSleepPath;
+        return false;
+    }
+
     for (int32_t i = 0; i < mConfig.entities.size(); i++) {
         std::vector<StateResidency> stateResidencies(mConfig.states.size());
         for (int32_t j = 0; j < stateResidencies.size(); j++) {
@@ -90,9 +102,29 @@
     char *line = nullptr;
 
     int32_t temp, entityIndex, stateId = -1;
-    uint64_t duration, count;
+    uint64_t duration, count, sleepDurationMs = 0;
     auto it = residencies->end();
+    int32_t sleepIndex = 0;
 
+    // Parse state for sleep duration
+    while (getline(&line, &len, sleepFp.get()) != -1) {
+        std::string trimedLine = Trim(std::string(line));
+        if (StartsWith(trimedLine, mSleepConfig[sleepIndex])) {
+            if (sleepIndex < mSleepConfig.size() - 1) {
+                sleepIndex++;
+                continue;
+            } else {
+                std::vector<std::string> parts = Split(trimedLine, " ");
+                if (parts.size() == 2) {
+                    ParseUint(parts[1], &sleepDurationMs);
+                    sleepDurationMs /= NS_TO_MS;
+                }
+                break;
+            }
+        }
+    }
+
+    // Parse state for CPUPM entities
     while (getline(&line, &len, fp.get()) != -1) {
         temp = matchState(line);
         // Assign new id only when a new valid state is encountered.
@@ -109,7 +141,7 @@
         it = residencies->find(mConfig.entities[entityIndex].first);
         if (it != residencies->end()) {
             if (parseState(line, &duration, &count)) {
-                it->second[stateId].totalTimeInStateMs = duration / US_TO_MS;
+                it->second[stateId].totalTimeInStateMs = duration / US_TO_MS + sleepDurationMs;
                 it->second[stateId].totalStateEntryCount = count;
             } else {
                 LOG(ERROR) << "Failed to parse duration and count from [" << std::string(line)
diff --git a/powerstats/include/CpupmStateResidencyDataProvider.h b/powerstats/include/CpupmStateResidencyDataProvider.h
index c04e11e..be8ee46 100644
--- a/powerstats/include/CpupmStateResidencyDataProvider.h
+++ b/powerstats/include/CpupmStateResidencyDataProvider.h
@@ -34,10 +34,16 @@
         std::vector<std::pair<std::string, std::string>> states;
     };
 
+    typedef std::vector<std::string> SleepConfig;
+
     /*
      * path - path to cpupm sysfs node.
      */
-    CpupmStateResidencyDataProvider(const std::string &path, const Config &config);
+    CpupmStateResidencyDataProvider(
+        const std::string &path,
+        const Config &config,
+        const std::string &sleepPath,
+        const SleepConfig &sleepConfig);
     ~CpupmStateResidencyDataProvider() = default;
 
     /*
@@ -58,9 +64,13 @@
 
     // A constant to represent the number of microseconds in one millisecond.
     const uint64_t US_TO_MS = 1000;
+    // A constant to represent the number of nanoseconds in one millisecond.
+    const uint64_t NS_TO_MS = 1000000;
 
     const std::string mPath;
     const Config mConfig;
+    const std::string mSleepPath;
+    const SleepConfig mSleepConfig;
 };
 
 }  // namespace stats
diff --git a/soc/Android.bp b/soc/Android.bp
index 9600855..57b8577 100644
--- a/soc/Android.bp
+++ b/soc/Android.bp
@@ -2,6 +2,13 @@
     default_applicable_licenses: ["Android-Apache-2.0"],
 }
 
+sh_binary {
+    name: "dump_memory.sh",
+    src: "dump_memory.sh",
+    vendor: true,
+    sub_dir: "dump",
+}
+
 cc_binary {
     name: "dump_soc",
     srcs: ["dump_soc.cpp"],
diff --git a/soc/dump_memory.sh b/soc/dump_memory.sh
new file mode 100644
index 0000000..5f4bde7
--- /dev/null
+++ b/soc/dump_memory.sh
@@ -0,0 +1,22 @@
+#!/vendor/bin/sh
+echo "------ ION HEAPS ------"
+for d in $(ls -d /d/ion/*)
+do
+  if [ -f $d ]; then
+    echo --- $d
+    cat $d
+  else
+    for f in $(ls $d)
+    do
+      echo --- $d/$f
+      cat $d/$f
+    done
+  fi
+done
+
+echo "------ dmabuf info ------"
+cat "/d/dma_buf/bufinfo"
+
+echo "------ Page Pinner - longterm pin ------"
+cat "/sys/kernel/debug/page_pinner/buffer"
+
diff --git a/soc/sepolicy/dump_memory.te b/soc/sepolicy/dump_memory.te
new file mode 100644
index 0000000..47f9f07
--- /dev/null
+++ b/soc/sepolicy/dump_memory.te
@@ -0,0 +1,8 @@
+pixel_bugreport(dump_memory)
+allow dump_memory vendor_toolbox_exec:file execute_no_trans;
+userdebug_or_eng(`
+  allow dump_memory vendor_dmabuf_debugfs:file r_file_perms;
+  allow dump_memory vendor_page_pinner_debugfs:dir r_dir_perms;
+  allow dump_memory vendor_page_pinner_debugfs:file r_file_perms;
+')
+
diff --git a/soc/sepolicy/dumpstate.te b/soc/sepolicy/dumpstate.te
new file mode 100644
index 0000000..1d23bb4
--- /dev/null
+++ b/soc/sepolicy/dumpstate.te
@@ -0,0 +1,2 @@
+dontaudit dumpstate vendor_dmabuf_debugfs:file r_file_perms;
+
diff --git a/soc/sepolicy/file.te b/soc/sepolicy/file.te
new file mode 100644
index 0000000..553825a
--- /dev/null
+++ b/soc/sepolicy/file.te
@@ -0,0 +1,3 @@
+type vendor_dmabuf_debugfs, fs_type, debugfs_type;
+type vendor_page_pinner_debugfs, fs_type, debugfs_type;
+
diff --git a/soc/sepolicy/file_contexts b/soc/sepolicy/file_contexts
index 81b1e68..23a91e5 100644
--- a/soc/sepolicy/file_contexts
+++ b/soc/sepolicy/file_contexts
@@ -1 +1,3 @@
-/vendor/bin/dump/dump_soc      u:object_r:dump_soc_exec:s0
+/vendor/bin/dump/dump_soc           u:object_r:dump_soc_exec:s0
+/vendor/bin/dump/dump_memory\.sh    u:object_r:dump_memory_exec:s0
+
diff --git a/soc/sepolicy/genfs_contexts b/soc/sepolicy/genfs_contexts
index 07ae4b8..454ab6a 100644
--- a/soc/sepolicy/genfs_contexts
+++ b/soc/sepolicy/genfs_contexts
@@ -5,3 +5,6 @@
 genfscon sysfs /devices/system/chip-id/revision        u:object_r:sysfs_chip_id:s0
 genfscon sysfs /devices/system/chip-id/raw_str         u:object_r:sysfs_chip_id:s0
 
+genfscon debugfs /dma_buf/bufinfo                      u:object_r:vendor_dmabuf_debugfs:s0
+genfscon debugfs /page_pinner                          u:object_r:vendor_page_pinner_debugfs:s0
+
diff --git a/soc/soc.mk b/soc/soc.mk
index 75b134e..b9b6208 100644
--- a/soc/soc.mk
+++ b/soc/soc.mk
@@ -1,3 +1,5 @@
 BOARD_VENDOR_SEPOLICY_DIRS += device/google/gs-common/soc/sepolicy
 
 PRODUCT_PACKAGES += dump_soc
+PRODUCT_PACKAGES_DEBUG += dump_memory.sh
+