Merge "tp: move util related sources to util build file"
diff --git a/include/perfetto/ext/base/watchdog_posix.h b/include/perfetto/ext/base/watchdog_posix.h
index 40e3289..9a6e1dd 100644
--- a/include/perfetto/ext/base/watchdog_posix.h
+++ b/include/perfetto/ext/base/watchdog_posix.h
@@ -27,6 +27,14 @@
 namespace perfetto {
 namespace base {
 
+struct ProcStat {
+  unsigned long int utime = 0l;
+  unsigned long int stime = 0l;
+  long int rss_pages = -1l;
+};
+
+bool ReadProcStat(int fd, ProcStat* out);
+
 // Ensures that the calling program does not exceed certain hard limits on
 // resource usage e.g. time, memory and CPU. If exceeded, the program is
 // crashed.
diff --git a/src/base/watchdog_posix.cc b/src/base/watchdog_posix.cc
index 25782d9..6b30406 100644
--- a/src/base/watchdog_posix.cc
+++ b/src/base/watchdog_posix.cc
@@ -53,6 +53,22 @@
 
 }  //  namespace
 
+bool ReadProcStat(int fd, ProcStat* out) {
+  char c[512];
+  if (PERFETTO_EINTR(read(fd, c, sizeof(c))) < 0) {
+    PERFETTO_ELOG("Failed to read stat file to enforce resource limits.");
+    return false;
+  }
+  c[sizeof(c) - 1] = '\0';
+
+  PERFETTO_CHECK(
+      sscanf(c,
+             "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu"
+             "%lu %*d %*d %*d %*d %*d %*d %*u %*u %ld",
+             &out->utime, &out->stime, &out->rss_pages) == 3);
+  return true;
+}
+
 Watchdog::Watchdog(uint32_t polling_interval_ms)
     : polling_interval_ms_(polling_interval_ms) {}
 
@@ -134,24 +150,14 @@
 
     lseek(stat_fd.get(), 0, SEEK_SET);
 
-    char c[512];
-    if (read(stat_fd.get(), c, sizeof(c)) < 0) {
-      PERFETTO_ELOG("Failed to read stat file to enforce resource limits.");
+    ProcStat stat;
+    if (!ReadProcStat(stat_fd.get(), &stat)) {
       return;
     }
-    c[sizeof(c) - 1] = '\0';
 
-    unsigned long int utime = 0l;
-    unsigned long int stime = 0l;
-    long int rss_pages = -1l;
-    PERFETTO_CHECK(
-        sscanf(c,
-               "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu"
-               "%lu %*d %*d %*d %*d %*d %*d %*u %*u %ld",
-               &utime, &stime, &rss_pages) == 3);
-
-    uint64_t cpu_time = utime + stime;
-    uint64_t rss_bytes = static_cast<uint64_t>(rss_pages) * base::kPageSize;
+    uint64_t cpu_time = stat.utime + stat.stime;
+    uint64_t rss_bytes =
+        static_cast<uint64_t>(stat.rss_pages) * base::kPageSize;
 
     CheckMemory(rss_bytes);
     CheckCpu(cpu_time);