OSDN Git Service

Perfprofd: Refactor protobuf I/O
authorAndreas Gampe <agampe@google.com>
Thu, 22 Mar 2018 15:20:08 +0000 (08:20 -0700)
committerAndreas Gampe <agampe@google.com>
Sat, 24 Mar 2018 02:51:09 +0000 (19:51 -0700)
Move to its own compilation unit.

(cherry picked from commit d0aac2583c77d76f4b78a500d626b5a2a03c3e05)

Bug: 73175642
Test: mmma system/extras/perfprofd
Merged-In: Iad9ddfe5ea84df9fa1fe27a367ec19aa37162c00
Change-Id: Iad9ddfe5ea84df9fa1fe27a367ec19aa37162c00

perfprofd/Android.bp
perfprofd/binder_interface/perfprofd_binder.cc
perfprofd/perfprofd_io.cc [new file with mode: 0644]
perfprofd/perfprofd_io.h [new file with mode: 0644]
perfprofd/perfprofdcore.cc
perfprofd/perfprofdcore.h

index 0031c37..500826c 100644 (file)
@@ -71,6 +71,7 @@ cc_library_static {
         "configreader.cc",
         "cpuconfig.cc",
         "perfprofdcore.cc",
+        "perfprofd_io.cc",
         "symbolizer.cc"
     ],
 
index 0165ddd..64d216d 100644 (file)
@@ -51,6 +51,7 @@
 
 #include "config.h"
 #include "perfprofdcore.h"
+#include "perfprofd_io.h"
 
 namespace android {
 namespace perfprofd {
@@ -233,8 +234,7 @@ bool PerfProfdNativeService::BinderHandler(
   std::string data_file_path(config->destination_directory);
   data_file_path += "/perf.data";
   std::string path = android::base::StringPrintf("%s.encoded.%d", data_file_path.c_str(), seq_);
-  PROFILE_RESULT result = SerializeProtobuf(encodedProfile, path.c_str());
-  if (result != PROFILE_RESULT::OK_PROFILE_COLLECTION) {
+  if (!SerializeProtobuf(encodedProfile, path.c_str())) {
     return false;
   }
 
diff --git a/perfprofd/perfprofd_io.cc b/perfprofd/perfprofd_io.cc
new file mode 100644 (file)
index 0000000..202ee2e
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+**
+** Copyright 2015, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#include "perfprofd_io.h"
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <memory>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/unique_fd.h>
+
+#include "perfprofd_record.pb.h"
+
+namespace android {
+namespace perfprofd {
+
+bool SerializeProtobuf(PerfprofdRecord* encodedProfile,
+                       const char* encoded_file_path) {
+  //
+  // Serialize protobuf to array
+  //
+  size_t size = encodedProfile->ByteSize();
+  std::unique_ptr<uint8_t[]> data(new uint8_t[size]);
+  encodedProfile->SerializeWithCachedSizesToArray(data.get());
+
+  //
+  // Open file and write encoded data to it
+  //
+  unlink(encoded_file_path);  // Attempt to unlink for a clean slate.
+  constexpr int kFlags = O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC;
+  android::base::unique_fd fd(open(encoded_file_path, kFlags, 0664));
+  if (fd.get() == -1) {
+    PLOG(WARNING) << "Could not open " << encoded_file_path << " for serialization";
+    return false;
+  }
+  if (!android::base::WriteFully(fd.get(), data.get(), size)) {
+    PLOG(WARNING) << "Could not write to " << encoded_file_path;
+    return false;
+  }
+
+  return true;
+}
+
+}  // namespace perfprofd
+}  // namespace android
diff --git a/perfprofd/perfprofd_io.h b/perfprofd/perfprofd_io.h
new file mode 100644 (file)
index 0000000..7387956
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+**
+** Copyright 2018, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+#ifndef SYSTEM_EXTRAS_PERFPROFD_PERFPROFD_IO_H_
+#define SYSTEM_EXTRAS_PERFPROFD_PERFPROFD_IO_H_
+
+#include "perfprofd_record-fwd.h"
+
+namespace android {
+namespace perfprofd {
+
+bool SerializeProtobuf(android::perfprofd::PerfprofdRecord* encodedProfile,
+                       const char* encoded_file_path);
+
+}  // namespace perfprofd
+}  // namespace android
+
+#endif
index 3d38a39..bb9dc99 100644 (file)
@@ -52,6 +52,7 @@
 #include "cpuconfig.h"
 #include "perf_data_converter.h"
 #include "perfprofdcore.h"
+#include "perfprofd_io.h"
 #include "symbolizer.h"
 
 //
@@ -477,33 +478,6 @@ static void annotate_encoded_perf_profile(android::perfprofd::PerfprofdRecord* p
   }
 }
 
-PROFILE_RESULT SerializeProtobuf(android::perfprofd::PerfprofdRecord* encodedProfile,
-                                 const char* encoded_file_path) {
-  //
-  // Serialize protobuf to array
-  //
-  size_t size = encodedProfile->ByteSize();
-  std::unique_ptr<uint8_t[]> data(new uint8_t[size]);
-  encodedProfile->SerializeWithCachedSizesToArray(data.get());
-
-  //
-  // Open file and write encoded data to it
-  //
-  unlink(encoded_file_path);  // Attempt to unlink for a clean slate.
-  constexpr int kFlags = O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC;
-  android::base::unique_fd fd(open(encoded_file_path, kFlags, 0664));
-  if (fd.get() == -1) {
-    PLOG(WARNING) << "Could not open " << encoded_file_path << " for serialization";
-    return ERR_OPEN_ENCODED_FILE_FAILED;
-  }
-  if (!android::base::WriteFully(fd.get(), data.get(), size)) {
-    PLOG(WARNING) << "Could not write to " << encoded_file_path;
-    return ERR_WRITE_ENCODED_FILE_FAILED;
-  }
-
-  return OK_PROFILE_COLLECTION;
-}
-
 static ProtoUniquePtr encode_to_proto(const std::string &data_file_path,
                                       const Config& config,
                                       unsigned cpu_utilization,
@@ -542,7 +516,9 @@ PROFILE_RESULT encode_to_proto(const std::string &data_file_path,
     return ERR_PERF_ENCODE_FAILED;
   }
 
-  return SerializeProtobuf(encodedProfile.get(), encoded_file_path);
+  return android::perfprofd::SerializeProtobuf(encodedProfile.get(), encoded_file_path)
+      ? OK_PROFILE_COLLECTION
+      : ERR_WRITE_ENCODED_FILE_FAILED;
 }
 
 //
@@ -1033,8 +1009,7 @@ int perfprofd_main(int argc, char** argv, Config* config)
     data_file_path += "/";
     data_file_path += PERF_OUTPUT;
     std::string path = android::base::StringPrintf("%s.encoded.%d", data_file_path.c_str(), seq);
-    PROFILE_RESULT result = SerializeProtobuf(proto, path.c_str());
-    if (result != PROFILE_RESULT::OK_PROFILE_COLLECTION) {
+    if (!android::perfprofd::SerializeProtobuf(proto, path.c_str())) {
       return false;
     }
 
index 1fcbd4c..73a9c56 100644 (file)
@@ -82,9 +82,6 @@ PROFILE_RESULT encode_to_proto(const std::string &data_file_path,
                                unsigned cpu_utilization,
                                perfprofd::Symbolizer* symbolizer);
 
-PROFILE_RESULT SerializeProtobuf(android::perfprofd::PerfprofdRecord* encodedProfile,
-                                 const char* encoded_file_path);
-
 using HandlerFn = std::function<bool(android::perfprofd::PerfprofdRecord* proto,
                                      Config* config)>;