The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 17 | #include "bootchart.h" |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <dirent.h> |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
Elliott Hughes | f3cf438 | 2015-02-03 17:12:07 -0800 | [diff] [blame] | 23 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 25 | #include <sys/utsname.h> |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 26 | #include <time.h> |
| 27 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 29 | #include <chrono> |
| 30 | #include <condition_variable> |
Elliott Hughes | 81399e1 | 2015-03-10 08:39:45 -0700 | [diff] [blame] | 31 | #include <memory> |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 32 | #include <mutex> |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 33 | #include <thread> |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 34 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 35 | #include <android-base/file.h> |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 36 | #include <android-base/logging.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 37 | #include <android-base/properties.h> |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 39 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 40 | using android::base::StringPrintf; |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 41 | using namespace std::chrono_literals; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 43 | static std::thread* g_bootcharting_thread; |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 44 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 45 | static std::mutex g_bootcharting_finished_mutex; |
| 46 | static std::condition_variable g_bootcharting_finished_cv; |
| 47 | static bool g_bootcharting_finished; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 49 | static long long get_uptime_jiffies() { |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 50 | std::string uptime; |
| 51 | if (!android::base::ReadFileToString("/proc/uptime", &uptime)) return 0; |
| 52 | return 100LL * strtod(uptime.c_str(), NULL); |
| 53 | } |
| 54 | |
| 55 | static std::unique_ptr<FILE, decltype(&fclose)> fopen_unique(const char* filename, |
| 56 | const char* mode) { |
| 57 | std::unique_ptr<FILE, decltype(&fclose)> result(fopen(filename, mode), fclose); |
| 58 | if (!result) PLOG(ERROR) << "bootchart: failed to open " << filename; |
| 59 | return result; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 62 | static void log_header() { |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 63 | char date[32]; |
| 64 | time_t now_t = time(NULL); |
| 65 | struct tm now = *localtime(&now_t); |
| 66 | strftime(date, sizeof(date), "%F %T", &now); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 68 | utsname uts; |
| 69 | if (uname(&uts) == -1) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 70 | |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 71 | std::string fingerprint = android::base::GetProperty("ro.build.fingerprint", ""); |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 72 | if (fingerprint.empty()) return; |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 73 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 74 | std::string kernel_cmdline; |
| 75 | android::base::ReadFileToString("/proc/cmdline", &kernel_cmdline); |
Elliott Hughes | 841b263 | 2015-02-12 14:28:54 -0800 | [diff] [blame] | 76 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 77 | auto fp = fopen_unique("/data/bootchart/header", "we"); |
| 78 | if (!fp) return; |
| 79 | fprintf(&*fp, "version = Android init 0.8\n"); |
| 80 | fprintf(&*fp, "title = Boot chart for Android (%s)\n", date); |
| 81 | fprintf(&*fp, "system.uname = %s %s %s %s\n", uts.sysname, uts.release, uts.version, uts.machine); |
| 82 | fprintf(&*fp, "system.release = %s\n", fingerprint.c_str()); |
| 83 | // TODO: use /proc/cpuinfo "model name" line for x86, "Processor" line for arm. |
| 84 | fprintf(&*fp, "system.cpu = %s\n", uts.machine); |
| 85 | fprintf(&*fp, "system.kernel.options = %s\n", kernel_cmdline.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 88 | static void log_uptime(FILE* log) { |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 89 | fprintf(log, "%lld\n", get_uptime_jiffies()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 92 | static void log_file(FILE* log, const char* procfile) { |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 93 | log_uptime(log); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 94 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 95 | std::string content; |
| 96 | if (android::base::ReadFileToString(procfile, &content)) { |
| 97 | fprintf(log, "%s\n", content.c_str()); |
| 98 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 101 | static void log_processes(FILE* log) { |
| 102 | log_uptime(log); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 104 | std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir("/proc"), closedir); |
| 105 | struct dirent* entry; |
| 106 | while ((entry = readdir(dir.get())) != NULL) { |
| 107 | // Only match numeric values. |
| 108 | int pid = atoi(entry->d_name); |
| 109 | if (pid == 0) continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 110 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 111 | // /proc/<pid>/stat only has truncated task names, so get the full |
| 112 | // name from /proc/<pid>/cmdline. |
| 113 | std::string cmdline; |
| 114 | android::base::ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &cmdline); |
| 115 | const char* full_name = cmdline.c_str(); // So we stop at the first NUL. |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 116 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 117 | // Read process stat line. |
| 118 | std::string stat; |
| 119 | if (android::base::ReadFileToString(StringPrintf("/proc/%d/stat", pid), &stat)) { |
| 120 | if (!cmdline.empty()) { |
| 121 | // Substitute the process name with its real name. |
| 122 | size_t open = stat.find('('); |
| 123 | size_t close = stat.find_last_of(')'); |
| 124 | if (open != std::string::npos && close != std::string::npos) { |
| 125 | stat.replace(open + 1, close - open - 1, full_name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | } |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 127 | } |
| 128 | fputs(stat.c_str(), log); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | fputc('\n', log); |
| 133 | } |
| 134 | |
| 135 | static void bootchart_thread_main() { |
| 136 | LOG(INFO) << "Bootcharting started"; |
| 137 | |
| 138 | // Open log files. |
| 139 | auto stat_log = fopen_unique("/data/bootchart/proc_stat.log", "we"); |
| 140 | if (!stat_log) return; |
| 141 | auto proc_log = fopen_unique("/data/bootchart/proc_ps.log", "we"); |
| 142 | if (!proc_log) return; |
| 143 | auto disk_log = fopen_unique("/data/bootchart/proc_diskstats.log", "we"); |
| 144 | if (!disk_log) return; |
| 145 | |
| 146 | log_header(); |
| 147 | |
| 148 | while (true) { |
| 149 | { |
| 150 | std::unique_lock<std::mutex> lock(g_bootcharting_finished_mutex); |
| 151 | g_bootcharting_finished_cv.wait_for(lock, 200ms); |
| 152 | if (g_bootcharting_finished) break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 | } |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 154 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 155 | log_file(&*stat_log, "/proc/stat"); |
| 156 | log_file(&*disk_log, "/proc/diskstats"); |
| 157 | log_processes(&*proc_log); |
| 158 | } |
| 159 | |
| 160 | LOG(INFO) << "Bootcharting finished"; |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static int do_bootchart_start() { |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 164 | // We don't care about the content, but we do care that /data/bootchart/enabled actually exists. |
| 165 | std::string start; |
| 166 | if (!android::base::ReadFileToString("/data/bootchart/enabled", &start)) { |
| 167 | LOG(VERBOSE) << "Not bootcharting"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 168 | return 0; |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 169 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 170 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 171 | g_bootcharting_thread = new std::thread(bootchart_thread_main); |
| 172 | return 0; |
Yongqin Liu | a197ff1 | 2014-12-05 13:45:02 +0800 | [diff] [blame] | 173 | } |
| 174 | |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 175 | static int do_bootchart_stop() { |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 176 | if (!g_bootcharting_thread) return 0; |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 177 | |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 178 | // Tell the worker thread it's time to quit. |
| 179 | { |
| 180 | std::lock_guard<std::mutex> lock(g_bootcharting_finished_mutex); |
| 181 | g_bootcharting_finished = true; |
| 182 | g_bootcharting_finished_cv.notify_one(); |
| 183 | } |
| 184 | |
| 185 | g_bootcharting_thread->join(); |
| 186 | delete g_bootcharting_thread; |
| 187 | g_bootcharting_thread = nullptr; |
| 188 | return 0; |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | int do_bootchart(const std::vector<std::string>& args) { |
Elliott Hughes | c249794 | 2016-12-16 14:45:17 -0800 | [diff] [blame] | 192 | if (args[1] == "start") return do_bootchart_start(); |
| 193 | return do_bootchart_stop(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | } |