Primiano Tucci | e4d4491 | 2018-01-10 12:14:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Log.h" |
| 18 | |
| 19 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" // Alert |
| 20 | |
| 21 | #include <android-base/unique_fd.h> |
| 22 | #include <errno.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/wait.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <string> |
| 28 | |
| 29 | namespace { |
| 30 | const char kDropboxTag[] = "perfetto"; |
| 31 | } |
| 32 | |
| 33 | namespace android { |
| 34 | namespace os { |
| 35 | namespace statsd { |
| 36 | |
| 37 | bool CollectPerfettoTraceAndUploadToDropbox(const PerfettoDetails& config) { |
| 38 | ALOGD("Starting trace collection through perfetto"); |
| 39 | |
| 40 | if (!config.has_trace_config()) { |
| 41 | ALOGE("The perfetto trace config is empty, aborting"); |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | android::base::unique_fd readPipe; |
| 46 | android::base::unique_fd writePipe; |
| 47 | if (!android::base::Pipe(&readPipe, &writePipe)) { |
| 48 | ALOGE("pipe() failed while calling the Perfetto client: %s", strerror(errno)); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | pid_t pid = fork(); |
| 53 | if (pid < 0) { |
| 54 | ALOGE("fork() failed while calling the Perfetto client: %s", strerror(errno)); |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | if (pid == 0) { |
| 59 | // Child process. |
| 60 | |
| 61 | // No malloc calls or library calls after this point. Remember that even |
| 62 | // ALOGx (aka android_printLog()) can use dynamic memory for vsprintf(). |
| 63 | |
| 64 | writePipe.reset(); // Close the write end (owned by the main process). |
| 65 | |
| 66 | // Replace stdin with |readPipe| so the main process can write into it. |
| 67 | if (dup2(readPipe.get(), STDIN_FILENO) < 0) _exit(1); |
| 68 | execl("/system/bin/perfetto", "perfetto", "--background", "--config", "-", "--dropbox", |
| 69 | kDropboxTag, nullptr); |
| 70 | |
| 71 | // execl() doesn't return in case of success, if we get here something failed. |
| 72 | _exit(1); |
| 73 | } |
| 74 | |
| 75 | // Main process. |
| 76 | |
| 77 | readPipe.reset(); // Close the read end (owned by the child process). |
| 78 | |
| 79 | // Using fopen() because fwrite() has the right logic to chunking write() |
| 80 | // over a pipe (see __sfvwrite()). |
| 81 | FILE* writePipeStream = fdopen(writePipe.get(), "wb"); |
| 82 | if (!writePipeStream) { |
| 83 | ALOGE("fdopen() failed while calling the Perfetto client: %s", strerror(errno)); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | std::string cfgProto = config.trace_config().SerializeAsString(); |
| 88 | size_t bytesWritten = fwrite(cfgProto.data(), 1, cfgProto.size(), writePipeStream); |
| 89 | fclose(writePipeStream); |
| 90 | if (bytesWritten != cfgProto.size() || cfgProto.size() == 0) { |
| 91 | ALOGE("fwrite() failed (ret: %zd) while calling the Perfetto client: %s", bytesWritten, |
| 92 | strerror(errno)); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | // This does NOT wait for the full duration of the trace. It just waits until the process |
| 97 | // has read the config from stdin and detached. |
| 98 | int childStatus = 0; |
| 99 | waitpid(pid, &childStatus, 0); |
| 100 | if (!WIFEXITED(childStatus) || WEXITSTATUS(childStatus) != 0) { |
| 101 | ALOGE("Child process failed (0x%x) while calling the Perfetto client", childStatus); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | ALOGD("CollectPerfettoTraceAndUploadToDropbox() succeeded"); |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | } // namespace statsd |
| 110 | } // namespace os |
| 111 | } // namespace android |