Merge "Fixing overflow bug"
diff --git a/cmds/dumpstate/Android.bp b/cmds/dumpstate/Android.bp
index 16b55a5..8d383f5 100644
--- a/cmds/dumpstate/Android.bp
+++ b/cmds/dumpstate/Android.bp
@@ -86,6 +86,7 @@
"libdumpstateaidl",
"libdumpstateutil",
"libdumputils",
+ "libhardware_legacy",
"libhidlbase",
"libhidltransport",
"liblog",
@@ -94,7 +95,6 @@
srcs: [
"DumpstateSectionReporter.cpp",
"DumpstateService.cpp",
- "utils.cpp",
],
static_libs: [
"libincidentcompanion",
diff --git a/cmds/dumpstate/DumpstateInternal.cpp b/cmds/dumpstate/DumpstateInternal.cpp
index 33e35f7..bbc724c 100644
--- a/cmds/dumpstate/DumpstateInternal.cpp
+++ b/cmds/dumpstate/DumpstateInternal.cpp
@@ -68,7 +68,8 @@
}
static const std::vector<std::string> group_names{
- "log", "sdcard_r", "sdcard_rw", "mount", "inet", "net_bw_stats", "readproc", "bluetooth"};
+ "log", "sdcard_r", "sdcard_rw", "mount", "inet", "net_bw_stats",
+ "readproc", "bluetooth", "wakelock"};
std::vector<gid_t> groups(group_names.size(), 0);
for (size_t i = 0; i < group_names.size(); ++i) {
grp = getgrnam(group_names[i].c_str());
@@ -116,6 +117,11 @@
capdata[cap_syslog_index].effective |= cap_syslog_mask;
}
+ const uint32_t cap_block_suspend_mask = CAP_TO_MASK(CAP_BLOCK_SUSPEND);
+ const uint32_t cap_block_suspend_index = CAP_TO_INDEX(CAP_BLOCK_SUSPEND);
+ capdata[cap_block_suspend_index].permitted |= cap_block_suspend_mask;
+ capdata[cap_block_suspend_index].effective |= cap_block_suspend_mask;
+
if (capset(&capheader, &capdata[0]) != 0) {
MYLOGE("capset({%#x, %#x}) failed: %s\n", capdata[0].effective,
capdata[1].effective, strerror(errno));
diff --git a/cmds/dumpstate/dumpstate.cpp b/cmds/dumpstate/dumpstate.cpp
index cc74570..1416629 100644
--- a/cmds/dumpstate/dumpstate.cpp
+++ b/cmds/dumpstate/dumpstate.cpp
@@ -22,6 +22,8 @@
#include <inttypes.h>
#include <libgen.h>
#include <limits.h>
+#include <math.h>
+#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -32,6 +34,13 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/wait.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <string.h>
+#include <sys/capability.h>
+#include <sys/inotify.h>
+#include <sys/klog.h>
+#include <time.h>
#include <unistd.h>
#include <chrono>
@@ -58,10 +67,13 @@
#include <binder/IServiceManager.h>
#include <cutils/native_handle.h>
#include <cutils/properties.h>
+#include <cutils/sockets.h>
#include <debuggerd/client.h>
#include <dumpsys.h>
#include <dumputils/dump_utils.h>
+#include <hardware_legacy/power.h>
#include <hidl/ServiceManagement.h>
+#include <log/log.h>
#include <openssl/sha.h>
#include <private/android_filesystem_config.h>
#include <private/android_logger.h>
@@ -96,6 +108,26 @@
using android::os::dumpstate::DumpstateSectionReporter;
using android::os::dumpstate::PropertiesHelper;
+// Keep in sync with
+// frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
+static const int TRACE_DUMP_TIMEOUT_MS = 10000; // 10 seconds
+
+/* Most simple commands have 10 as timeout, so 5 is a good estimate */
+static const int32_t WEIGHT_FILE = 5;
+
+// TODO: temporary variables and functions used during C++ refactoring
+static Dumpstate& ds = Dumpstate::GetInstance();
+static int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
+ const CommandOptions& options = CommandOptions::DEFAULT) {
+ return ds.RunCommand(title, full_command, options);
+}
+
+// Reasonable value for max stats.
+static const int STATS_MAX_N_RUNS = 1000;
+static const long STATS_MAX_AVERAGE = 100000;
+
+CommandOptions Dumpstate::DEFAULT_DUMPSYS = CommandOptions::WithTimeout(30).Build();
+
typedef Dumpstate::ConsentCallback::ConsentResult UserConsentResult;
/* read before root is shed */
@@ -132,7 +164,6 @@
static const std::string ANR_FILE_PREFIX = "anr_";
// TODO: temporary variables and functions used during C++ refactoring
-static Dumpstate& ds = Dumpstate::GetInstance();
#define RETURN_IF_USER_DENIED_CONSENT() \
if (ds.IsUserConsentDenied()) { \
@@ -147,6 +178,8 @@
func_ptr(__VA_ARGS__); \
RETURN_IF_USER_DENIED_CONSENT();
+static const char* WAKE_LOCK_NAME = "dumpstate_wakelock";
+
namespace android {
namespace os {
namespace {
@@ -234,10 +267,6 @@
} // namespace os
} // namespace android
-static int RunCommand(const std::string& title, const std::vector<std::string>& fullCommand,
- const CommandOptions& options = CommandOptions::DEFAULT) {
- return ds.RunCommand(title, fullCommand, options);
-}
static void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsysArgs,
const CommandOptions& options = Dumpstate::DEFAULT_DUMPSYS,
long dumpsysTimeoutMs = 0) {
@@ -2442,6 +2471,13 @@
MYLOGI("begin\n");
+ if (acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME) < 0) {
+ MYLOGE("Failed to acquire wake lock: %s\n", strerror(errno));
+ } else {
+ // Wake lock will be released automatically on process death
+ MYLOGD("Wake lock acquired.\n");
+ }
+
register_sig_handler();
// TODO(b/111441001): maybe skip if already started?
@@ -2768,3 +2804,947 @@
exit(2);
}
}
+
+// TODO(111441001): Default DumpOptions to sensible values.
+Dumpstate::Dumpstate(const std::string& version)
+ : pid_(getpid()),
+ options_(new Dumpstate::DumpOptions()),
+ version_(version),
+ now_(time(nullptr)) {
+}
+
+Dumpstate& Dumpstate::GetInstance() {
+ static Dumpstate singleton_(android::base::GetProperty("dumpstate.version", VERSION_CURRENT));
+ return singleton_;
+}
+
+DurationReporter::DurationReporter(const std::string& title, bool logcat_only)
+ : title_(title), logcat_only_(logcat_only) {
+ if (!title_.empty()) {
+ started_ = Nanotime();
+ }
+}
+
+DurationReporter::~DurationReporter() {
+ if (!title_.empty()) {
+ float elapsed = (float)(Nanotime() - started_) / NANOS_PER_SEC;
+ if (elapsed < .5f) {
+ return;
+ }
+ MYLOGD("Duration of '%s': %.2fs\n", title_.c_str(), elapsed);
+ if (logcat_only_) {
+ return;
+ }
+ // Use "Yoda grammar" to make it easier to grep|sort sections.
+ printf("------ %.3fs was the duration of '%s' ------\n", elapsed, title_.c_str());
+ }
+}
+
+const int32_t Progress::kDefaultMax = 5000;
+
+Progress::Progress(const std::string& path) : Progress(Progress::kDefaultMax, 1.1, path) {
+}
+
+Progress::Progress(int32_t initial_max, int32_t progress, float growth_factor)
+ : Progress(initial_max, growth_factor, "") {
+ progress_ = progress;
+}
+
+Progress::Progress(int32_t initial_max, float growth_factor, const std::string& path)
+ : initial_max_(initial_max),
+ progress_(0),
+ max_(initial_max),
+ growth_factor_(growth_factor),
+ n_runs_(0),
+ average_max_(0),
+ path_(path) {
+ if (!path_.empty()) {
+ Load();
+ }
+}
+
+void Progress::Load() {
+ MYLOGD("Loading stats from %s\n", path_.c_str());
+ std::string content;
+ if (!android::base::ReadFileToString(path_, &content)) {
+ MYLOGI("Could not read stats from %s; using max of %d\n", path_.c_str(), max_);
+ return;
+ }
+ if (content.empty()) {
+ MYLOGE("No stats (empty file) on %s; using max of %d\n", path_.c_str(), max_);
+ return;
+ }
+ std::vector<std::string> lines = android::base::Split(content, "\n");
+
+ if (lines.size() < 1) {
+ MYLOGE("Invalid stats on file %s: not enough lines (%d). Using max of %d\n", path_.c_str(),
+ (int)lines.size(), max_);
+ return;
+ }
+ char* ptr;
+ n_runs_ = strtol(lines[0].c_str(), &ptr, 10);
+ average_max_ = strtol(ptr, nullptr, 10);
+ if (n_runs_ <= 0 || average_max_ <= 0 || n_runs_ > STATS_MAX_N_RUNS ||
+ average_max_ > STATS_MAX_AVERAGE) {
+ MYLOGE("Invalid stats line on file %s: %s\n", path_.c_str(), lines[0].c_str());
+ initial_max_ = Progress::kDefaultMax;
+ } else {
+ initial_max_ = average_max_;
+ }
+ max_ = initial_max_;
+
+ MYLOGI("Average max progress: %d in %d runs; estimated max: %d\n", average_max_, n_runs_, max_);
+}
+
+void Progress::Save() {
+ int32_t total = n_runs_ * average_max_ + progress_;
+ int32_t runs = n_runs_ + 1;
+ int32_t average = floor(((float)total) / runs);
+ MYLOGI("Saving stats (total=%d, runs=%d, average=%d) on %s\n", total, runs, average,
+ path_.c_str());
+ if (path_.empty()) {
+ return;
+ }
+
+ std::string content = android::base::StringPrintf("%d %d\n", runs, average);
+ if (!android::base::WriteStringToFile(content, path_)) {
+ MYLOGE("Could not save stats on %s\n", path_.c_str());
+ }
+}
+
+int32_t Progress::Get() const {
+ return progress_;
+}
+
+bool Progress::Inc(int32_t delta_sec) {
+ bool changed = false;
+ if (delta_sec >= 0) {
+ progress_ += delta_sec;
+ if (progress_ > max_) {
+ int32_t old_max = max_;
+ max_ = floor((float)progress_ * growth_factor_);
+ MYLOGD("Adjusting max progress from %d to %d\n", old_max, max_);
+ changed = true;
+ }
+ }
+ return changed;
+}
+
+int32_t Progress::GetMax() const {
+ return max_;
+}
+
+int32_t Progress::GetInitialMax() const {
+ return initial_max_;
+}
+
+void Progress::Dump(int fd, const std::string& prefix) const {
+ const char* pr = prefix.c_str();
+ dprintf(fd, "%sprogress: %d\n", pr, progress_);
+ dprintf(fd, "%smax: %d\n", pr, max_);
+ dprintf(fd, "%sinitial_max: %d\n", pr, initial_max_);
+ dprintf(fd, "%sgrowth_factor: %0.2f\n", pr, growth_factor_);
+ dprintf(fd, "%spath: %s\n", pr, path_.c_str());
+ dprintf(fd, "%sn_runs: %d\n", pr, n_runs_);
+ dprintf(fd, "%saverage_max: %d\n", pr, average_max_);
+}
+
+bool Dumpstate::IsZipping() const {
+ return zip_writer_ != nullptr;
+}
+
+std::string Dumpstate::GetPath(const std::string& suffix) const {
+ return GetPath(bugreport_internal_dir_, suffix);
+}
+
+std::string Dumpstate::GetPath(const std::string& directory, const std::string& suffix) const {
+ return android::base::StringPrintf("%s/%s-%s%s", directory.c_str(), base_name_.c_str(),
+ name_.c_str(), suffix.c_str());
+}
+
+void Dumpstate::SetProgress(std::unique_ptr<Progress> progress) {
+ progress_ = std::move(progress);
+}
+
+void for_each_userid(void (*func)(int), const char *header) {
+ std::string title = header == nullptr ? "for_each_userid" : android::base::StringPrintf(
+ "for_each_userid(%s)", header);
+ DurationReporter duration_reporter(title);
+ if (PropertiesHelper::IsDryRun()) return;
+
+ DIR *d;
+ struct dirent *de;
+
+ if (header) printf("\n------ %s ------\n", header);
+ func(0);
+
+ if (!(d = opendir("/data/system/users"))) {
+ printf("Failed to open /data/system/users (%s)\n", strerror(errno));
+ return;
+ }
+
+ while ((de = readdir(d))) {
+ int userid;
+ if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) {
+ continue;
+ }
+ func(userid);
+ }
+
+ closedir(d);
+}
+
+static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
+ DIR *d;
+ struct dirent *de;
+
+ if (!(d = opendir("/proc"))) {
+ printf("Failed to open /proc (%s)\n", strerror(errno));
+ return;
+ }
+
+ if (header) printf("\n------ %s ------\n", header);
+ while ((de = readdir(d))) {
+ if (ds.IsUserConsentDenied()) {
+ MYLOGE(
+ "Returning early because user denied consent to share bugreport with calling app.");
+ closedir(d);
+ return;
+ }
+ int pid;
+ int fd;
+ char cmdpath[255];
+ char cmdline[255];
+
+ if (!(pid = atoi(de->d_name))) {
+ continue;
+ }
+
+ memset(cmdline, 0, sizeof(cmdline));
+
+ snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid);
+ if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) {
+ TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2));
+ close(fd);
+ if (cmdline[0]) {
+ helper(pid, cmdline, arg);
+ continue;
+ }
+ }
+
+ // if no cmdline, a kernel thread has comm
+ snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid);
+ if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) {
+ TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4));
+ close(fd);
+ if (cmdline[1]) {
+ cmdline[0] = '[';
+ size_t len = strcspn(cmdline, "\f\b\r\n");
+ cmdline[len] = ']';
+ cmdline[len+1] = '\0';
+ }
+ }
+ if (!cmdline[0]) {
+ strcpy(cmdline, "N/A");
+ }
+ helper(pid, cmdline, arg);
+ }
+
+ closedir(d);
+}
+
+static void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
+ for_each_pid_func *func = (for_each_pid_func*) arg;
+ func(pid, cmdline);
+}
+
+void for_each_pid(for_each_pid_func func, const char *header) {
+ std::string title = header == nullptr ? "for_each_pid"
+ : android::base::StringPrintf("for_each_pid(%s)", header);
+ DurationReporter duration_reporter(title);
+ if (PropertiesHelper::IsDryRun()) return;
+
+ __for_each_pid(for_each_pid_helper, header, (void *) func);
+}
+
+static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
+ DIR *d;
+ struct dirent *de;
+ char taskpath[255];
+ for_each_tid_func *func = (for_each_tid_func *) arg;
+
+ snprintf(taskpath, sizeof(taskpath), "/proc/%d/task", pid);
+
+ if (!(d = opendir(taskpath))) {
+ printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
+ return;
+ }
+
+ func(pid, pid, cmdline);
+
+ while ((de = readdir(d))) {
+ if (ds.IsUserConsentDenied()) {
+ MYLOGE(
+ "Returning early because user denied consent to share bugreport with calling app.");
+ closedir(d);
+ return;
+ }
+ int tid;
+ int fd;
+ char commpath[255];
+ char comm[255];
+
+ if (!(tid = atoi(de->d_name))) {
+ continue;
+ }
+
+ if (tid == pid)
+ continue;
+
+ snprintf(commpath, sizeof(commpath), "/proc/%d/comm", tid);
+ memset(comm, 0, sizeof(comm));
+ if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) {
+ strcpy(comm, "N/A");
+ } else {
+ char *c;
+ TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2));
+ close(fd);
+
+ c = strrchr(comm, '\n');
+ if (c) {
+ *c = '\0';
+ }
+ }
+ func(pid, tid, comm);
+ }
+
+ closedir(d);
+}
+
+void for_each_tid(for_each_tid_func func, const char *header) {
+ std::string title = header == nullptr ? "for_each_tid"
+ : android::base::StringPrintf("for_each_tid(%s)", header);
+ DurationReporter duration_reporter(title);
+
+ if (PropertiesHelper::IsDryRun()) return;
+
+ __for_each_pid(for_each_tid_helper, header, (void *) func);
+}
+
+void show_wchan(int pid, int tid, const char *name) {
+ if (PropertiesHelper::IsDryRun()) return;
+
+ char path[255];
+ char buffer[255];
+ int fd, ret, save_errno;
+ char name_buffer[255];
+
+ memset(buffer, 0, sizeof(buffer));
+
+ snprintf(path, sizeof(path), "/proc/%d/wchan", tid);
+ if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
+ printf("Failed to open '%s' (%s)\n", path, strerror(errno));
+ return;
+ }
+
+ ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
+ save_errno = errno;
+ close(fd);
+
+ if (ret < 0) {
+ printf("Failed to read '%s' (%s)\n", path, strerror(save_errno));
+ return;
+ }
+
+ snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
+ pid == tid ? 0 : 3, "", name);
+
+ printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
+
+ return;
+}
+
+// print time in centiseconds
+static void snprcent(char *buffer, size_t len, size_t spc,
+ unsigned long long time) {
+ static long hz; // cache discovered hz
+
+ if (hz <= 0) {
+ hz = sysconf(_SC_CLK_TCK);
+ if (hz <= 0) {
+ hz = 1000;
+ }
+ }
+
+ // convert to centiseconds
+ time = (time * 100 + (hz / 2)) / hz;
+
+ char str[16];
+
+ snprintf(str, sizeof(str), " %llu.%02u",
+ time / 100, (unsigned)(time % 100));
+ size_t offset = strlen(buffer);
+ snprintf(buffer + offset, (len > offset) ? len - offset : 0,
+ "%*s", (spc > offset) ? (int)(spc - offset) : 0, str);
+}
+
+// print permille as a percent
+static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) {
+ char str[16];
+
+ snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10);
+ size_t offset = strlen(buffer);
+ snprintf(buffer + offset, (len > offset) ? len - offset : 0,
+ "%*s", (spc > offset) ? (int)(spc - offset) : 0, str);
+}
+
+void show_showtime(int pid, const char *name) {
+ if (PropertiesHelper::IsDryRun()) return;
+
+ char path[255];
+ char buffer[1023];
+ int fd, ret, save_errno;
+
+ memset(buffer, 0, sizeof(buffer));
+
+ snprintf(path, sizeof(path), "/proc/%d/stat", pid);
+ if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
+ printf("Failed to open '%s' (%s)\n", path, strerror(errno));
+ return;
+ }
+
+ ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
+ save_errno = errno;
+ close(fd);
+
+ if (ret < 0) {
+ printf("Failed to read '%s' (%s)\n", path, strerror(save_errno));
+ return;
+ }
+
+ // field 14 is utime
+ // field 15 is stime
+ // field 42 is iotime
+ unsigned long long utime = 0, stime = 0, iotime = 0;
+ if (sscanf(buffer,
+ "%*u %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d "
+ "%*d %*d %llu %llu %*d %*d %*d %*d %*d %*d "
+ "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
+ "%*d %*d %*d %*d %*d %*d %*d %*d %*d %llu ",
+ &utime, &stime, &iotime) != 3) {
+ return;
+ }
+
+ unsigned long long total = utime + stime;
+ if (!total) {
+ return;
+ }
+
+ unsigned permille = (iotime * 1000 + (total / 2)) / total;
+ if (permille > 1000) {
+ permille = 1000;
+ }
+
+ // try to beautify and stabilize columns at <80 characters
+ snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name);
+ if ((name[0] != '[') || utime) {
+ snprcent(buffer, sizeof(buffer), 57, utime);
+ }
+ snprcent(buffer, sizeof(buffer), 65, stime);
+ if ((name[0] != '[') || iotime) {
+ snprcent(buffer, sizeof(buffer), 73, iotime);
+ }
+ if (iotime) {
+ snprdec(buffer, sizeof(buffer), 79, permille);
+ }
+ puts(buffer); // adds a trailing newline
+
+ return;
+}
+
+void do_dmesg() {
+ const char *title = "KERNEL LOG (dmesg)";
+ DurationReporter duration_reporter(title);
+ printf("------ %s ------\n", title);
+
+ if (PropertiesHelper::IsDryRun()) return;
+
+ /* Get size of kernel buffer */
+ int size = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
+ if (size <= 0) {
+ printf("Unexpected klogctl return value: %d\n\n", size);
+ return;
+ }
+ char *buf = (char *) malloc(size + 1);
+ if (buf == nullptr) {
+ printf("memory allocation failed\n\n");
+ return;
+ }
+ int retval = klogctl(KLOG_READ_ALL, buf, size);
+ if (retval < 0) {
+ printf("klogctl failure\n\n");
+ free(buf);
+ return;
+ }
+ buf[retval] = '\0';
+ printf("%s\n\n", buf);
+ free(buf);
+ return;
+}
+
+void do_showmap(int pid, const char *name) {
+ char title[255];
+ char arg[255];
+
+ snprintf(title, sizeof(title), "SHOW MAP %d (%s)", pid, name);
+ snprintf(arg, sizeof(arg), "%d", pid);
+ RunCommand(title, {"showmap", "-q", arg}, CommandOptions::AS_ROOT);
+}
+
+int Dumpstate::DumpFile(const std::string& title, const std::string& path) {
+ DurationReporter duration_reporter(title);
+
+ int status = DumpFileToFd(STDOUT_FILENO, title, path);
+
+ UpdateProgress(WEIGHT_FILE);
+
+ return status;
+}
+
+int read_file_as_long(const char *path, long int *output) {
+ int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
+ if (fd < 0) {
+ int err = errno;
+ MYLOGE("Error opening file descriptor for %s: %s\n", path, strerror(err));
+ return -1;
+ }
+ char buffer[50];
+ ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
+ if (bytes_read == -1) {
+ MYLOGE("Error reading file %s: %s\n", path, strerror(errno));
+ return -2;
+ }
+ if (bytes_read == 0) {
+ MYLOGE("File %s is empty\n", path);
+ return -3;
+ }
+ *output = atoi(buffer);
+ return 0;
+}
+
+/* calls skip to gate calling dump_from_fd recursively
+ * in the specified directory. dump_from_fd defaults to
+ * dump_file_from_fd above when set to NULL. skip defaults
+ * to false when set to NULL. dump_from_fd will always be
+ * called with title NULL.
+ */
+int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
+ int (*dump_from_fd)(const char* title, const char* path, int fd)) {
+ DurationReporter duration_reporter(title);
+ DIR *dirp;
+ struct dirent *d;
+ char *newpath = nullptr;
+ const char *slash = "/";
+ int retval = 0;
+
+ if (!title.empty()) {
+ printf("------ %s (%s) ------\n", title.c_str(), dir);
+ }
+ if (PropertiesHelper::IsDryRun()) return 0;
+
+ if (dir[strlen(dir) - 1] == '/') {
+ ++slash;
+ }
+ dirp = opendir(dir);
+ if (dirp == nullptr) {
+ retval = -errno;
+ MYLOGE("%s: %s\n", dir, strerror(errno));
+ return retval;
+ }
+
+ if (!dump_from_fd) {
+ dump_from_fd = dump_file_from_fd;
+ }
+ for (; ((d = readdir(dirp))); free(newpath), newpath = nullptr) {
+ if ((d->d_name[0] == '.')
+ && (((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
+ || (d->d_name[1] == '\0'))) {
+ continue;
+ }
+ asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name,
+ (d->d_type == DT_DIR) ? "/" : "");
+ if (!newpath) {
+ retval = -errno;
+ continue;
+ }
+ if (skip && (*skip)(newpath)) {
+ continue;
+ }
+ if (d->d_type == DT_DIR) {
+ int ret = dump_files("", newpath, skip, dump_from_fd);
+ if (ret < 0) {
+ retval = ret;
+ }
+ continue;
+ }
+ android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
+ if (fd.get() < 0) {
+ retval = -1;
+ printf("*** %s: %s\n", newpath, strerror(errno));
+ continue;
+ }
+ (*dump_from_fd)(nullptr, newpath, fd.get());
+ }
+ closedir(dirp);
+ if (!title.empty()) {
+ printf("\n");
+ }
+ return retval;
+}
+
+/* fd must have been opened with the flag O_NONBLOCK. With this flag set,
+ * it's possible to avoid issues where opening the file itself can get
+ * stuck.
+ */
+int dump_file_from_fd(const char *title, const char *path, int fd) {
+ if (PropertiesHelper::IsDryRun()) return 0;
+
+ int flags = fcntl(fd, F_GETFL);
+ if (flags == -1) {
+ printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno));
+ return -1;
+ } else if (!(flags & O_NONBLOCK)) {
+ printf("*** %s: fd must have O_NONBLOCK set.\n", path);
+ return -1;
+ }
+ return DumpFileFromFdToFd(title, path, fd, STDOUT_FILENO, PropertiesHelper::IsDryRun());
+}
+
+int Dumpstate::RunCommand(const std::string& title, const std::vector<std::string>& full_command,
+ const CommandOptions& options) {
+ DurationReporter duration_reporter(title);
+
+ int status = RunCommandToFd(STDOUT_FILENO, title, full_command, options);
+
+ /* TODO: for now we're simplifying the progress calculation by using the
+ * timeout as the weight. It's a good approximation for most cases, except when calling dumpsys,
+ * where its weight should be much higher proportionally to its timeout.
+ * Ideally, it should use a options.EstimatedDuration() instead...*/
+ UpdateProgress(options.Timeout());
+
+ return status;
+}
+
+void Dumpstate::RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
+ const CommandOptions& options, long dumpsysTimeoutMs) {
+ long timeout_ms = dumpsysTimeoutMs > 0 ? dumpsysTimeoutMs : options.TimeoutInMs();
+ std::vector<std::string> dumpsys = {"/system/bin/dumpsys", "-T", std::to_string(timeout_ms)};
+ dumpsys.insert(dumpsys.end(), dumpsys_args.begin(), dumpsys_args.end());
+ RunCommand(title, dumpsys, options);
+}
+
+int open_socket(const char *service) {
+ int s = android_get_control_socket(service);
+ if (s < 0) {
+ MYLOGE("android_get_control_socket(%s): %s\n", service, strerror(errno));
+ return -1;
+ }
+ fcntl(s, F_SETFD, FD_CLOEXEC);
+
+ // Set backlog to 0 to make sure that queue size will be minimum.
+ // In Linux, because the minimum queue will be 1, connect() will be blocked
+ // if the other clients already called connect() and the connection request was not accepted.
+ if (listen(s, 0) < 0) {
+ MYLOGE("listen(control socket): %s\n", strerror(errno));
+ return -1;
+ }
+
+ struct sockaddr addr;
+ socklen_t alen = sizeof(addr);
+ int fd = accept(s, &addr, &alen);
+
+ // Close socket just after accept(), to make sure that connect() by client will get error
+ // when the socket is used by the other services.
+ // There is still a race condition possibility between accept and close, but there is no way
+ // to close-on-accept atomically.
+ // See detail; b/123306389#comment25
+ close(s);
+
+ if (fd < 0) {
+ MYLOGE("accept(control socket): %s\n", strerror(errno));
+ return -1;
+ }
+
+ return fd;
+}
+
+/* redirect output to a service control socket */
+bool redirect_to_socket(FILE* redirect, const char* service) {
+ int fd = open_socket(service);
+ if (fd == -1) {
+ return false;
+ }
+ fflush(redirect);
+ // TODO: handle dup2 failure
+ TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
+ close(fd);
+ return true;
+}
+
+// TODO: should call is_valid_output_file and/or be merged into it.
+void create_parent_dirs(const char *path) {
+ char *chp = const_cast<char *> (path);
+
+ /* skip initial slash */
+ if (chp[0] == '/')
+ chp++;
+
+ /* create leading directories, if necessary */
+ struct stat dir_stat;
+ while (chp && chp[0]) {
+ chp = strchr(chp, '/');
+ if (chp) {
+ *chp = 0;
+ if (stat(path, &dir_stat) == -1 || !S_ISDIR(dir_stat.st_mode)) {
+ MYLOGI("Creating directory %s\n", path);
+ if (mkdir(path, 0770)) { /* drwxrwx--- */
+ MYLOGE("Unable to create directory %s: %s\n", path, strerror(errno));
+ } else if (chown(path, AID_SHELL, AID_SHELL)) {
+ MYLOGE("Unable to change ownership of dir %s: %s\n", path, strerror(errno));
+ }
+ }
+ *chp++ = '/';
+ }
+ }
+}
+
+bool _redirect_to_file(FILE* redirect, char* path, int truncate_flag) {
+ create_parent_dirs(path);
+
+ int fd = TEMP_FAILURE_RETRY(open(path,
+ O_WRONLY | O_CREAT | truncate_flag | O_CLOEXEC | O_NOFOLLOW,
+ S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
+ if (fd < 0) {
+ MYLOGE("%s: %s\n", path, strerror(errno));
+ return false;
+ }
+
+ TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
+ close(fd);
+ return true;
+}
+
+bool redirect_to_file(FILE* redirect, char* path) {
+ return _redirect_to_file(redirect, path, O_TRUNC);
+}
+
+bool redirect_to_existing_file(FILE* redirect, char* path) {
+ return _redirect_to_file(redirect, path, O_APPEND);
+}
+
+void dump_route_tables() {
+ DurationReporter duration_reporter("DUMP ROUTE TABLES");
+ if (PropertiesHelper::IsDryRun()) return;
+ const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
+ ds.DumpFile("RT_TABLES", RT_TABLES_PATH);
+ FILE* fp = fopen(RT_TABLES_PATH, "re");
+ if (!fp) {
+ printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
+ return;
+ }
+ char table[16];
+ // Each line has an integer (the table number), a space, and a string (the table name). We only
+ // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
+ // Add a fixed max limit so this doesn't go awry.
+ for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
+ RunCommand("ROUTE TABLE IPv4", {"ip", "-4", "route", "show", "table", table});
+ RunCommand("ROUTE TABLE IPv6", {"ip", "-6", "route", "show", "table", table});
+ }
+ fclose(fp);
+}
+
+// TODO: make this function thread safe if sections are generated in parallel.
+void Dumpstate::UpdateProgress(int32_t delta_sec) {
+ if (progress_ == nullptr) {
+ MYLOGE("UpdateProgress: progress_ not set\n");
+ return;
+ }
+
+ // Always update progess so stats can be tuned...
+ bool max_changed = progress_->Inc(delta_sec);
+
+ // ...but only notifiy listeners when necessary.
+ if (!options_->do_progress_updates) return;
+
+ int progress = progress_->Get();
+ int max = progress_->GetMax();
+
+ // adjusts max on the fly
+ if (max_changed && listener_ != nullptr) {
+ listener_->onMaxProgressUpdated(max);
+ }
+
+ int32_t last_update_delta = progress - last_updated_progress_;
+ if (last_updated_progress_ > 0 && last_update_delta < update_progress_threshold_) {
+ return;
+ }
+ last_updated_progress_ = progress;
+
+ if (control_socket_fd_ >= 0) {
+ dprintf(control_socket_fd_, "PROGRESS:%d/%d\n", progress, max);
+ fsync(control_socket_fd_);
+ }
+
+ int percent = 100 * progress / max;
+ if (listener_ != nullptr) {
+ if (percent % 5 == 0) {
+ // We don't want to spam logcat, so only log multiples of 5.
+ MYLOGD("Setting progress (%s): %d/%d (%d%%)\n", listener_name_.c_str(), progress, max,
+ percent);
+ } else {
+ // stderr is ignored on normal invocations, but useful when calling
+ // /system/bin/dumpstate directly for debuggging.
+ fprintf(stderr, "Setting progress (%s): %d/%d (%d%%)\n", listener_name_.c_str(),
+ progress, max, percent);
+ }
+ // TODO(b/111441001): Remove in favor of onProgress
+ listener_->onProgressUpdated(progress);
+
+ listener_->onProgress(percent);
+ }
+}
+
+void Dumpstate::TakeScreenshot(const std::string& path) {
+ const std::string& real_path = path.empty() ? screenshot_path_ : path;
+ int status =
+ RunCommand("", {"/system/bin/screencap", "-p", real_path},
+ CommandOptions::WithTimeout(10).Always().DropRoot().RedirectStderr().Build());
+ if (status == 0) {
+ MYLOGD("Screenshot saved on %s\n", real_path.c_str());
+ } else {
+ MYLOGE("Failed to take screenshot on %s\n", real_path.c_str());
+ }
+}
+
+bool is_dir(const char* pathname) {
+ struct stat info;
+ if (stat(pathname, &info) == -1) {
+ return false;
+ }
+ return S_ISDIR(info.st_mode);
+}
+
+time_t get_mtime(int fd, time_t default_mtime) {
+ struct stat info;
+ if (fstat(fd, &info) == -1) {
+ return default_mtime;
+ }
+ return info.st_mtime;
+}
+
+void dump_emmc_ecsd(const char *ext_csd_path) {
+ // List of interesting offsets
+ struct hex {
+ char str[2];
+ };
+ static const size_t EXT_CSD_REV = 192 * sizeof(hex);
+ static const size_t EXT_PRE_EOL_INFO = 267 * sizeof(hex);
+ static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268 * sizeof(hex);
+ static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269 * sizeof(hex);
+
+ std::string buffer;
+ if (!android::base::ReadFileToString(ext_csd_path, &buffer)) {
+ return;
+ }
+
+ printf("------ %s Extended CSD ------\n", ext_csd_path);
+
+ if (buffer.length() < (EXT_CSD_REV + sizeof(hex))) {
+ printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length());
+ return;
+ }
+
+ int ext_csd_rev = 0;
+ std::string sub = buffer.substr(EXT_CSD_REV, sizeof(hex));
+ if (sscanf(sub.c_str(), "%2x", &ext_csd_rev) != 1) {
+ printf("*** %s: EXT_CSD_REV parse error \"%s\"\n\n", ext_csd_path, sub.c_str());
+ return;
+ }
+
+ static const char *ver_str[] = {
+ "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0"
+ };
+ printf("rev 1.%d (MMC %s)\n", ext_csd_rev,
+ (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ? ver_str[ext_csd_rev]
+ : "Unknown");
+ if (ext_csd_rev < 7) {
+ printf("\n");
+ return;
+ }
+
+ if (buffer.length() < (EXT_PRE_EOL_INFO + sizeof(hex))) {
+ printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length());
+ return;
+ }
+
+ int ext_pre_eol_info = 0;
+ sub = buffer.substr(EXT_PRE_EOL_INFO, sizeof(hex));
+ if (sscanf(sub.c_str(), "%2x", &ext_pre_eol_info) != 1) {
+ printf("*** %s: PRE_EOL_INFO parse error \"%s\"\n\n", ext_csd_path, sub.c_str());
+ return;
+ }
+
+ static const char *eol_str[] = {
+ "Undefined",
+ "Normal",
+ "Warning (consumed 80% of reserve)",
+ "Urgent (consumed 90% of reserve)"
+ };
+ printf(
+ "PRE_EOL_INFO %d (MMC %s)\n", ext_pre_eol_info,
+ eol_str[(ext_pre_eol_info < (int)(sizeof(eol_str) / sizeof(eol_str[0]))) ? ext_pre_eol_info
+ : 0]);
+
+ for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A;
+ lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B;
+ lifetime += sizeof(hex)) {
+ int ext_device_life_time_est;
+ static const char *est_str[] = {
+ "Undefined",
+ "0-10% of device lifetime used",
+ "10-20% of device lifetime used",
+ "20-30% of device lifetime used",
+ "30-40% of device lifetime used",
+ "40-50% of device lifetime used",
+ "50-60% of device lifetime used",
+ "60-70% of device lifetime used",
+ "70-80% of device lifetime used",
+ "80-90% of device lifetime used",
+ "90-100% of device lifetime used",
+ "Exceeded the maximum estimated device lifetime",
+ };
+
+ if (buffer.length() < (lifetime + sizeof(hex))) {
+ printf("*** %s: truncated content %zu\n", ext_csd_path, buffer.length());
+ break;
+ }
+
+ ext_device_life_time_est = 0;
+ sub = buffer.substr(lifetime, sizeof(hex));
+ if (sscanf(sub.c_str(), "%2x", &ext_device_life_time_est) != 1) {
+ printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%s\"\n", ext_csd_path,
+ (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / sizeof(hex)) + 'A',
+ sub.c_str());
+ continue;
+ }
+ printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n",
+ (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / sizeof(hex)) + 'A',
+ ext_device_life_time_est,
+ est_str[(ext_device_life_time_est < (int)(sizeof(est_str) / sizeof(est_str[0])))
+ ? ext_device_life_time_est
+ : 0]);
+ }
+
+ printf("\n");
+}
+
diff --git a/cmds/dumpstate/utils.cpp b/cmds/dumpstate/utils.cpp
deleted file mode 100644
index e08c806..0000000
--- a/cmds/dumpstate/utils.cpp
+++ /dev/null
@@ -1,1024 +0,0 @@
-/*
- * Copyright (C) 2008 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.
- */
-
-#define LOG_TAG "dumpstate"
-
-#include "dumpstate.h"
-
-#include <dirent.h>
-#include <fcntl.h>
-#include <libgen.h>
-#include <math.h>
-#include <poll.h>
-#include <signal.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/capability.h>
-#include <sys/inotify.h>
-#include <sys/klog.h>
-#include <sys/prctl.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/wait.h>
-#include <time.h>
-#include <unistd.h>
-
-#include <memory>
-#include <set>
-#include <string>
-#include <vector>
-
-#include <android-base/file.h>
-#include <android-base/properties.h>
-#include <android-base/stringprintf.h>
-#include <android-base/strings.h>
-#include <android-base/unique_fd.h>
-#include <cutils/properties.h>
-#include <cutils/sockets.h>
-#include <log/log.h>
-#include <private/android_filesystem_config.h>
-
-#include "DumpstateInternal.h"
-
-// TODO: remove once moved to namespace
-using android::os::dumpstate::CommandOptions;
-using android::os::dumpstate::DumpFileToFd;
-using android::os::dumpstate::PropertiesHelper;
-
-// Keep in sync with
-// frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
-static const int TRACE_DUMP_TIMEOUT_MS = 10000; // 10 seconds
-
-/* Most simple commands have 10 as timeout, so 5 is a good estimate */
-static const int32_t WEIGHT_FILE = 5;
-
-// TODO: temporary variables and functions used during C++ refactoring
-static Dumpstate& ds = Dumpstate::GetInstance();
-static int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
- const CommandOptions& options = CommandOptions::DEFAULT) {
- return ds.RunCommand(title, full_command, options);
-}
-
-// Reasonable value for max stats.
-static const int STATS_MAX_N_RUNS = 1000;
-static const long STATS_MAX_AVERAGE = 100000;
-
-CommandOptions Dumpstate::DEFAULT_DUMPSYS = CommandOptions::WithTimeout(30).Build();
-
-// TODO(111441001): Default DumpOptions to sensible values.
-Dumpstate::Dumpstate(const std::string& version)
- : pid_(getpid()),
- options_(new Dumpstate::DumpOptions()),
- version_(version),
- now_(time(nullptr)) {
-}
-
-Dumpstate& Dumpstate::GetInstance() {
- static Dumpstate singleton_(android::base::GetProperty("dumpstate.version", VERSION_CURRENT));
- return singleton_;
-}
-
-DurationReporter::DurationReporter(const std::string& title, bool logcat_only)
- : title_(title), logcat_only_(logcat_only) {
- if (!title_.empty()) {
- started_ = Nanotime();
- }
-}
-
-DurationReporter::~DurationReporter() {
- if (!title_.empty()) {
- float elapsed = (float)(Nanotime() - started_) / NANOS_PER_SEC;
- if (elapsed < .5f) {
- return;
- }
- MYLOGD("Duration of '%s': %.2fs\n", title_.c_str(), elapsed);
- if (logcat_only_) {
- return;
- }
- // Use "Yoda grammar" to make it easier to grep|sort sections.
- printf("------ %.3fs was the duration of '%s' ------\n", elapsed, title_.c_str());
- }
-}
-
-const int32_t Progress::kDefaultMax = 5000;
-
-Progress::Progress(const std::string& path) : Progress(Progress::kDefaultMax, 1.1, path) {
-}
-
-Progress::Progress(int32_t initial_max, int32_t progress, float growth_factor)
- : Progress(initial_max, growth_factor, "") {
- progress_ = progress;
-}
-
-Progress::Progress(int32_t initial_max, float growth_factor, const std::string& path)
- : initial_max_(initial_max),
- progress_(0),
- max_(initial_max),
- growth_factor_(growth_factor),
- n_runs_(0),
- average_max_(0),
- path_(path) {
- if (!path_.empty()) {
- Load();
- }
-}
-
-void Progress::Load() {
- MYLOGD("Loading stats from %s\n", path_.c_str());
- std::string content;
- if (!android::base::ReadFileToString(path_, &content)) {
- MYLOGI("Could not read stats from %s; using max of %d\n", path_.c_str(), max_);
- return;
- }
- if (content.empty()) {
- MYLOGE("No stats (empty file) on %s; using max of %d\n", path_.c_str(), max_);
- return;
- }
- std::vector<std::string> lines = android::base::Split(content, "\n");
-
- if (lines.size() < 1) {
- MYLOGE("Invalid stats on file %s: not enough lines (%d). Using max of %d\n", path_.c_str(),
- (int)lines.size(), max_);
- return;
- }
- char* ptr;
- n_runs_ = strtol(lines[0].c_str(), &ptr, 10);
- average_max_ = strtol(ptr, nullptr, 10);
- if (n_runs_ <= 0 || average_max_ <= 0 || n_runs_ > STATS_MAX_N_RUNS ||
- average_max_ > STATS_MAX_AVERAGE) {
- MYLOGE("Invalid stats line on file %s: %s\n", path_.c_str(), lines[0].c_str());
- initial_max_ = Progress::kDefaultMax;
- } else {
- initial_max_ = average_max_;
- }
- max_ = initial_max_;
-
- MYLOGI("Average max progress: %d in %d runs; estimated max: %d\n", average_max_, n_runs_, max_);
-}
-
-void Progress::Save() {
- int32_t total = n_runs_ * average_max_ + progress_;
- int32_t runs = n_runs_ + 1;
- int32_t average = floor(((float)total) / runs);
- MYLOGI("Saving stats (total=%d, runs=%d, average=%d) on %s\n", total, runs, average,
- path_.c_str());
- if (path_.empty()) {
- return;
- }
-
- std::string content = android::base::StringPrintf("%d %d\n", runs, average);
- if (!android::base::WriteStringToFile(content, path_)) {
- MYLOGE("Could not save stats on %s\n", path_.c_str());
- }
-}
-
-int32_t Progress::Get() const {
- return progress_;
-}
-
-bool Progress::Inc(int32_t delta_sec) {
- bool changed = false;
- if (delta_sec >= 0) {
- progress_ += delta_sec;
- if (progress_ > max_) {
- int32_t old_max = max_;
- max_ = floor((float)progress_ * growth_factor_);
- MYLOGD("Adjusting max progress from %d to %d\n", old_max, max_);
- changed = true;
- }
- }
- return changed;
-}
-
-int32_t Progress::GetMax() const {
- return max_;
-}
-
-int32_t Progress::GetInitialMax() const {
- return initial_max_;
-}
-
-void Progress::Dump(int fd, const std::string& prefix) const {
- const char* pr = prefix.c_str();
- dprintf(fd, "%sprogress: %d\n", pr, progress_);
- dprintf(fd, "%smax: %d\n", pr, max_);
- dprintf(fd, "%sinitial_max: %d\n", pr, initial_max_);
- dprintf(fd, "%sgrowth_factor: %0.2f\n", pr, growth_factor_);
- dprintf(fd, "%spath: %s\n", pr, path_.c_str());
- dprintf(fd, "%sn_runs: %d\n", pr, n_runs_);
- dprintf(fd, "%saverage_max: %d\n", pr, average_max_);
-}
-
-bool Dumpstate::IsZipping() const {
- return zip_writer_ != nullptr;
-}
-
-std::string Dumpstate::GetPath(const std::string& suffix) const {
- return GetPath(bugreport_internal_dir_, suffix);
-}
-
-std::string Dumpstate::GetPath(const std::string& directory, const std::string& suffix) const {
- return android::base::StringPrintf("%s/%s-%s%s", directory.c_str(), base_name_.c_str(),
- name_.c_str(), suffix.c_str());
-}
-
-void Dumpstate::SetProgress(std::unique_ptr<Progress> progress) {
- progress_ = std::move(progress);
-}
-
-void for_each_userid(void (*func)(int), const char *header) {
- std::string title = header == nullptr ? "for_each_userid" : android::base::StringPrintf(
- "for_each_userid(%s)", header);
- DurationReporter duration_reporter(title);
- if (PropertiesHelper::IsDryRun()) return;
-
- DIR *d;
- struct dirent *de;
-
- if (header) printf("\n------ %s ------\n", header);
- func(0);
-
- if (!(d = opendir("/data/system/users"))) {
- printf("Failed to open /data/system/users (%s)\n", strerror(errno));
- return;
- }
-
- while ((de = readdir(d))) {
- int userid;
- if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) {
- continue;
- }
- func(userid);
- }
-
- closedir(d);
-}
-
-static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
- DIR *d;
- struct dirent *de;
-
- if (!(d = opendir("/proc"))) {
- printf("Failed to open /proc (%s)\n", strerror(errno));
- return;
- }
-
- if (header) printf("\n------ %s ------\n", header);
- while ((de = readdir(d))) {
- if (ds.IsUserConsentDenied()) {
- MYLOGE(
- "Returning early because user denied consent to share bugreport with calling app.");
- closedir(d);
- return;
- }
- int pid;
- int fd;
- char cmdpath[255];
- char cmdline[255];
-
- if (!(pid = atoi(de->d_name))) {
- continue;
- }
-
- memset(cmdline, 0, sizeof(cmdline));
-
- snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid);
- if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) {
- TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2));
- close(fd);
- if (cmdline[0]) {
- helper(pid, cmdline, arg);
- continue;
- }
- }
-
- // if no cmdline, a kernel thread has comm
- snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid);
- if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) {
- TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4));
- close(fd);
- if (cmdline[1]) {
- cmdline[0] = '[';
- size_t len = strcspn(cmdline, "\f\b\r\n");
- cmdline[len] = ']';
- cmdline[len+1] = '\0';
- }
- }
- if (!cmdline[0]) {
- strcpy(cmdline, "N/A");
- }
- helper(pid, cmdline, arg);
- }
-
- closedir(d);
-}
-
-static void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
- for_each_pid_func *func = (for_each_pid_func*) arg;
- func(pid, cmdline);
-}
-
-void for_each_pid(for_each_pid_func func, const char *header) {
- std::string title = header == nullptr ? "for_each_pid"
- : android::base::StringPrintf("for_each_pid(%s)", header);
- DurationReporter duration_reporter(title);
- if (PropertiesHelper::IsDryRun()) return;
-
- __for_each_pid(for_each_pid_helper, header, (void *) func);
-}
-
-static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
- DIR *d;
- struct dirent *de;
- char taskpath[255];
- for_each_tid_func *func = (for_each_tid_func *) arg;
-
- snprintf(taskpath, sizeof(taskpath), "/proc/%d/task", pid);
-
- if (!(d = opendir(taskpath))) {
- printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
- return;
- }
-
- func(pid, pid, cmdline);
-
- while ((de = readdir(d))) {
- if (ds.IsUserConsentDenied()) {
- MYLOGE(
- "Returning early because user denied consent to share bugreport with calling app.");
- closedir(d);
- return;
- }
- int tid;
- int fd;
- char commpath[255];
- char comm[255];
-
- if (!(tid = atoi(de->d_name))) {
- continue;
- }
-
- if (tid == pid)
- continue;
-
- snprintf(commpath, sizeof(commpath), "/proc/%d/comm", tid);
- memset(comm, 0, sizeof(comm));
- if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) {
- strcpy(comm, "N/A");
- } else {
- char *c;
- TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2));
- close(fd);
-
- c = strrchr(comm, '\n');
- if (c) {
- *c = '\0';
- }
- }
- func(pid, tid, comm);
- }
-
- closedir(d);
-}
-
-void for_each_tid(for_each_tid_func func, const char *header) {
- std::string title = header == nullptr ? "for_each_tid"
- : android::base::StringPrintf("for_each_tid(%s)", header);
- DurationReporter duration_reporter(title);
-
- if (PropertiesHelper::IsDryRun()) return;
-
- __for_each_pid(for_each_tid_helper, header, (void *) func);
-}
-
-void show_wchan(int pid, int tid, const char *name) {
- if (PropertiesHelper::IsDryRun()) return;
-
- char path[255];
- char buffer[255];
- int fd, ret, save_errno;
- char name_buffer[255];
-
- memset(buffer, 0, sizeof(buffer));
-
- snprintf(path, sizeof(path), "/proc/%d/wchan", tid);
- if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
- printf("Failed to open '%s' (%s)\n", path, strerror(errno));
- return;
- }
-
- ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
- save_errno = errno;
- close(fd);
-
- if (ret < 0) {
- printf("Failed to read '%s' (%s)\n", path, strerror(save_errno));
- return;
- }
-
- snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
- pid == tid ? 0 : 3, "", name);
-
- printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
-
- return;
-}
-
-// print time in centiseconds
-static void snprcent(char *buffer, size_t len, size_t spc,
- unsigned long long time) {
- static long hz; // cache discovered hz
-
- if (hz <= 0) {
- hz = sysconf(_SC_CLK_TCK);
- if (hz <= 0) {
- hz = 1000;
- }
- }
-
- // convert to centiseconds
- time = (time * 100 + (hz / 2)) / hz;
-
- char str[16];
-
- snprintf(str, sizeof(str), " %llu.%02u",
- time / 100, (unsigned)(time % 100));
- size_t offset = strlen(buffer);
- snprintf(buffer + offset, (len > offset) ? len - offset : 0,
- "%*s", (spc > offset) ? (int)(spc - offset) : 0, str);
-}
-
-// print permille as a percent
-static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) {
- char str[16];
-
- snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10);
- size_t offset = strlen(buffer);
- snprintf(buffer + offset, (len > offset) ? len - offset : 0,
- "%*s", (spc > offset) ? (int)(spc - offset) : 0, str);
-}
-
-void show_showtime(int pid, const char *name) {
- if (PropertiesHelper::IsDryRun()) return;
-
- char path[255];
- char buffer[1023];
- int fd, ret, save_errno;
-
- memset(buffer, 0, sizeof(buffer));
-
- snprintf(path, sizeof(path), "/proc/%d/stat", pid);
- if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
- printf("Failed to open '%s' (%s)\n", path, strerror(errno));
- return;
- }
-
- ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
- save_errno = errno;
- close(fd);
-
- if (ret < 0) {
- printf("Failed to read '%s' (%s)\n", path, strerror(save_errno));
- return;
- }
-
- // field 14 is utime
- // field 15 is stime
- // field 42 is iotime
- unsigned long long utime = 0, stime = 0, iotime = 0;
- if (sscanf(buffer,
- "%*u %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d "
- "%*d %*d %llu %llu %*d %*d %*d %*d %*d %*d "
- "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
- "%*d %*d %*d %*d %*d %*d %*d %*d %*d %llu ",
- &utime, &stime, &iotime) != 3) {
- return;
- }
-
- unsigned long long total = utime + stime;
- if (!total) {
- return;
- }
-
- unsigned permille = (iotime * 1000 + (total / 2)) / total;
- if (permille > 1000) {
- permille = 1000;
- }
-
- // try to beautify and stabilize columns at <80 characters
- snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name);
- if ((name[0] != '[') || utime) {
- snprcent(buffer, sizeof(buffer), 57, utime);
- }
- snprcent(buffer, sizeof(buffer), 65, stime);
- if ((name[0] != '[') || iotime) {
- snprcent(buffer, sizeof(buffer), 73, iotime);
- }
- if (iotime) {
- snprdec(buffer, sizeof(buffer), 79, permille);
- }
- puts(buffer); // adds a trailing newline
-
- return;
-}
-
-void do_dmesg() {
- const char *title = "KERNEL LOG (dmesg)";
- DurationReporter duration_reporter(title);
- printf("------ %s ------\n", title);
-
- if (PropertiesHelper::IsDryRun()) return;
-
- /* Get size of kernel buffer */
- int size = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
- if (size <= 0) {
- printf("Unexpected klogctl return value: %d\n\n", size);
- return;
- }
- char *buf = (char *) malloc(size + 1);
- if (buf == nullptr) {
- printf("memory allocation failed\n\n");
- return;
- }
- int retval = klogctl(KLOG_READ_ALL, buf, size);
- if (retval < 0) {
- printf("klogctl failure\n\n");
- free(buf);
- return;
- }
- buf[retval] = '\0';
- printf("%s\n\n", buf);
- free(buf);
- return;
-}
-
-void do_showmap(int pid, const char *name) {
- char title[255];
- char arg[255];
-
- snprintf(title, sizeof(title), "SHOW MAP %d (%s)", pid, name);
- snprintf(arg, sizeof(arg), "%d", pid);
- RunCommand(title, {"showmap", "-q", arg}, CommandOptions::AS_ROOT);
-}
-
-int Dumpstate::DumpFile(const std::string& title, const std::string& path) {
- DurationReporter duration_reporter(title);
-
- int status = DumpFileToFd(STDOUT_FILENO, title, path);
-
- UpdateProgress(WEIGHT_FILE);
-
- return status;
-}
-
-int read_file_as_long(const char *path, long int *output) {
- int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
- if (fd < 0) {
- int err = errno;
- MYLOGE("Error opening file descriptor for %s: %s\n", path, strerror(err));
- return -1;
- }
- char buffer[50];
- ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
- if (bytes_read == -1) {
- MYLOGE("Error reading file %s: %s\n", path, strerror(errno));
- return -2;
- }
- if (bytes_read == 0) {
- MYLOGE("File %s is empty\n", path);
- return -3;
- }
- *output = atoi(buffer);
- return 0;
-}
-
-/* calls skip to gate calling dump_from_fd recursively
- * in the specified directory. dump_from_fd defaults to
- * dump_file_from_fd above when set to NULL. skip defaults
- * to false when set to NULL. dump_from_fd will always be
- * called with title NULL.
- */
-int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
- int (*dump_from_fd)(const char* title, const char* path, int fd)) {
- DurationReporter duration_reporter(title);
- DIR *dirp;
- struct dirent *d;
- char *newpath = nullptr;
- const char *slash = "/";
- int retval = 0;
-
- if (!title.empty()) {
- printf("------ %s (%s) ------\n", title.c_str(), dir);
- }
- if (PropertiesHelper::IsDryRun()) return 0;
-
- if (dir[strlen(dir) - 1] == '/') {
- ++slash;
- }
- dirp = opendir(dir);
- if (dirp == nullptr) {
- retval = -errno;
- MYLOGE("%s: %s\n", dir, strerror(errno));
- return retval;
- }
-
- if (!dump_from_fd) {
- dump_from_fd = dump_file_from_fd;
- }
- for (; ((d = readdir(dirp))); free(newpath), newpath = nullptr) {
- if ((d->d_name[0] == '.')
- && (((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
- || (d->d_name[1] == '\0'))) {
- continue;
- }
- asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name,
- (d->d_type == DT_DIR) ? "/" : "");
- if (!newpath) {
- retval = -errno;
- continue;
- }
- if (skip && (*skip)(newpath)) {
- continue;
- }
- if (d->d_type == DT_DIR) {
- int ret = dump_files("", newpath, skip, dump_from_fd);
- if (ret < 0) {
- retval = ret;
- }
- continue;
- }
- android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)));
- if (fd.get() < 0) {
- retval = -1;
- printf("*** %s: %s\n", newpath, strerror(errno));
- continue;
- }
- (*dump_from_fd)(nullptr, newpath, fd.get());
- }
- closedir(dirp);
- if (!title.empty()) {
- printf("\n");
- }
- return retval;
-}
-
-/* fd must have been opened with the flag O_NONBLOCK. With this flag set,
- * it's possible to avoid issues where opening the file itself can get
- * stuck.
- */
-int dump_file_from_fd(const char *title, const char *path, int fd) {
- if (PropertiesHelper::IsDryRun()) return 0;
-
- int flags = fcntl(fd, F_GETFL);
- if (flags == -1) {
- printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno));
- return -1;
- } else if (!(flags & O_NONBLOCK)) {
- printf("*** %s: fd must have O_NONBLOCK set.\n", path);
- return -1;
- }
- return DumpFileFromFdToFd(title, path, fd, STDOUT_FILENO, PropertiesHelper::IsDryRun());
-}
-
-int Dumpstate::RunCommand(const std::string& title, const std::vector<std::string>& full_command,
- const CommandOptions& options) {
- DurationReporter duration_reporter(title);
-
- int status = RunCommandToFd(STDOUT_FILENO, title, full_command, options);
-
- /* TODO: for now we're simplifying the progress calculation by using the
- * timeout as the weight. It's a good approximation for most cases, except when calling dumpsys,
- * where its weight should be much higher proportionally to its timeout.
- * Ideally, it should use a options.EstimatedDuration() instead...*/
- UpdateProgress(options.Timeout());
-
- return status;
-}
-
-void Dumpstate::RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
- const CommandOptions& options, long dumpsysTimeoutMs) {
- long timeout_ms = dumpsysTimeoutMs > 0 ? dumpsysTimeoutMs : options.TimeoutInMs();
- std::vector<std::string> dumpsys = {"/system/bin/dumpsys", "-T", std::to_string(timeout_ms)};
- dumpsys.insert(dumpsys.end(), dumpsys_args.begin(), dumpsys_args.end());
- RunCommand(title, dumpsys, options);
-}
-
-int open_socket(const char *service) {
- int s = android_get_control_socket(service);
- if (s < 0) {
- MYLOGE("android_get_control_socket(%s): %s\n", service, strerror(errno));
- return -1;
- }
- fcntl(s, F_SETFD, FD_CLOEXEC);
-
- // Set backlog to 0 to make sure that queue size will be minimum.
- // In Linux, because the minimum queue will be 1, connect() will be blocked
- // if the other clients already called connect() and the connection request was not accepted.
- if (listen(s, 0) < 0) {
- MYLOGE("listen(control socket): %s\n", strerror(errno));
- return -1;
- }
-
- struct sockaddr addr;
- socklen_t alen = sizeof(addr);
- int fd = accept(s, &addr, &alen);
-
- // Close socket just after accept(), to make sure that connect() by client will get error
- // when the socket is used by the other services.
- // There is still a race condition possibility between accept and close, but there is no way
- // to close-on-accept atomically.
- // See detail; b/123306389#comment25
- close(s);
-
- if (fd < 0) {
- MYLOGE("accept(control socket): %s\n", strerror(errno));
- return -1;
- }
-
- return fd;
-}
-
-/* redirect output to a service control socket */
-bool redirect_to_socket(FILE* redirect, const char* service) {
- int fd = open_socket(service);
- if (fd == -1) {
- return false;
- }
- fflush(redirect);
- // TODO: handle dup2 failure
- TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
- close(fd);
- return true;
-}
-
-// TODO: should call is_valid_output_file and/or be merged into it.
-void create_parent_dirs(const char *path) {
- char *chp = const_cast<char *> (path);
-
- /* skip initial slash */
- if (chp[0] == '/')
- chp++;
-
- /* create leading directories, if necessary */
- struct stat dir_stat;
- while (chp && chp[0]) {
- chp = strchr(chp, '/');
- if (chp) {
- *chp = 0;
- if (stat(path, &dir_stat) == -1 || !S_ISDIR(dir_stat.st_mode)) {
- MYLOGI("Creating directory %s\n", path);
- if (mkdir(path, 0770)) { /* drwxrwx--- */
- MYLOGE("Unable to create directory %s: %s\n", path, strerror(errno));
- } else if (chown(path, AID_SHELL, AID_SHELL)) {
- MYLOGE("Unable to change ownership of dir %s: %s\n", path, strerror(errno));
- }
- }
- *chp++ = '/';
- }
- }
-}
-
-bool _redirect_to_file(FILE* redirect, char* path, int truncate_flag) {
- create_parent_dirs(path);
-
- int fd = TEMP_FAILURE_RETRY(open(path,
- O_WRONLY | O_CREAT | truncate_flag | O_CLOEXEC | O_NOFOLLOW,
- S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
- if (fd < 0) {
- MYLOGE("%s: %s\n", path, strerror(errno));
- return false;
- }
-
- TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
- close(fd);
- return true;
-}
-
-bool redirect_to_file(FILE* redirect, char* path) {
- return _redirect_to_file(redirect, path, O_TRUNC);
-}
-
-bool redirect_to_existing_file(FILE* redirect, char* path) {
- return _redirect_to_file(redirect, path, O_APPEND);
-}
-
-void dump_route_tables() {
- DurationReporter duration_reporter("DUMP ROUTE TABLES");
- if (PropertiesHelper::IsDryRun()) return;
- const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
- ds.DumpFile("RT_TABLES", RT_TABLES_PATH);
- FILE* fp = fopen(RT_TABLES_PATH, "re");
- if (!fp) {
- printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
- return;
- }
- char table[16];
- // Each line has an integer (the table number), a space, and a string (the table name). We only
- // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
- // Add a fixed max limit so this doesn't go awry.
- for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
- RunCommand("ROUTE TABLE IPv4", {"ip", "-4", "route", "show", "table", table});
- RunCommand("ROUTE TABLE IPv6", {"ip", "-6", "route", "show", "table", table});
- }
- fclose(fp);
-}
-
-// TODO: make this function thread safe if sections are generated in parallel.
-void Dumpstate::UpdateProgress(int32_t delta_sec) {
- if (progress_ == nullptr) {
- MYLOGE("UpdateProgress: progress_ not set\n");
- return;
- }
-
- // Always update progess so stats can be tuned...
- bool max_changed = progress_->Inc(delta_sec);
-
- // ...but only notifiy listeners when necessary.
- if (!options_->do_progress_updates) return;
-
- int progress = progress_->Get();
- int max = progress_->GetMax();
-
- // adjusts max on the fly
- if (max_changed && listener_ != nullptr) {
- listener_->onMaxProgressUpdated(max);
- }
-
- int32_t last_update_delta = progress - last_updated_progress_;
- if (last_updated_progress_ > 0 && last_update_delta < update_progress_threshold_) {
- return;
- }
- last_updated_progress_ = progress;
-
- if (control_socket_fd_ >= 0) {
- dprintf(control_socket_fd_, "PROGRESS:%d/%d\n", progress, max);
- fsync(control_socket_fd_);
- }
-
- int percent = 100 * progress / max;
- if (listener_ != nullptr) {
- if (percent % 5 == 0) {
- // We don't want to spam logcat, so only log multiples of 5.
- MYLOGD("Setting progress (%s): %d/%d (%d%%)\n", listener_name_.c_str(), progress, max,
- percent);
- } else {
- // stderr is ignored on normal invocations, but useful when calling
- // /system/bin/dumpstate directly for debuggging.
- fprintf(stderr, "Setting progress (%s): %d/%d (%d%%)\n", listener_name_.c_str(),
- progress, max, percent);
- }
- // TODO(b/111441001): Remove in favor of onProgress
- listener_->onProgressUpdated(progress);
-
- listener_->onProgress(percent);
- }
-}
-
-void Dumpstate::TakeScreenshot(const std::string& path) {
- const std::string& real_path = path.empty() ? screenshot_path_ : path;
- int status =
- RunCommand("", {"/system/bin/screencap", "-p", real_path},
- CommandOptions::WithTimeout(10).Always().DropRoot().RedirectStderr().Build());
- if (status == 0) {
- MYLOGD("Screenshot saved on %s\n", real_path.c_str());
- } else {
- MYLOGE("Failed to take screenshot on %s\n", real_path.c_str());
- }
-}
-
-bool is_dir(const char* pathname) {
- struct stat info;
- if (stat(pathname, &info) == -1) {
- return false;
- }
- return S_ISDIR(info.st_mode);
-}
-
-time_t get_mtime(int fd, time_t default_mtime) {
- struct stat info;
- if (fstat(fd, &info) == -1) {
- return default_mtime;
- }
- return info.st_mtime;
-}
-
-void dump_emmc_ecsd(const char *ext_csd_path) {
- // List of interesting offsets
- struct hex {
- char str[2];
- };
- static const size_t EXT_CSD_REV = 192 * sizeof(hex);
- static const size_t EXT_PRE_EOL_INFO = 267 * sizeof(hex);
- static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268 * sizeof(hex);
- static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269 * sizeof(hex);
-
- std::string buffer;
- if (!android::base::ReadFileToString(ext_csd_path, &buffer)) {
- return;
- }
-
- printf("------ %s Extended CSD ------\n", ext_csd_path);
-
- if (buffer.length() < (EXT_CSD_REV + sizeof(hex))) {
- printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length());
- return;
- }
-
- int ext_csd_rev = 0;
- std::string sub = buffer.substr(EXT_CSD_REV, sizeof(hex));
- if (sscanf(sub.c_str(), "%2x", &ext_csd_rev) != 1) {
- printf("*** %s: EXT_CSD_REV parse error \"%s\"\n\n", ext_csd_path, sub.c_str());
- return;
- }
-
- static const char *ver_str[] = {
- "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0"
- };
- printf("rev 1.%d (MMC %s)\n", ext_csd_rev,
- (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ? ver_str[ext_csd_rev]
- : "Unknown");
- if (ext_csd_rev < 7) {
- printf("\n");
- return;
- }
-
- if (buffer.length() < (EXT_PRE_EOL_INFO + sizeof(hex))) {
- printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length());
- return;
- }
-
- int ext_pre_eol_info = 0;
- sub = buffer.substr(EXT_PRE_EOL_INFO, sizeof(hex));
- if (sscanf(sub.c_str(), "%2x", &ext_pre_eol_info) != 1) {
- printf("*** %s: PRE_EOL_INFO parse error \"%s\"\n\n", ext_csd_path, sub.c_str());
- return;
- }
-
- static const char *eol_str[] = {
- "Undefined",
- "Normal",
- "Warning (consumed 80% of reserve)",
- "Urgent (consumed 90% of reserve)"
- };
- printf(
- "PRE_EOL_INFO %d (MMC %s)\n", ext_pre_eol_info,
- eol_str[(ext_pre_eol_info < (int)(sizeof(eol_str) / sizeof(eol_str[0]))) ? ext_pre_eol_info
- : 0]);
-
- for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A;
- lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B;
- lifetime += sizeof(hex)) {
- int ext_device_life_time_est;
- static const char *est_str[] = {
- "Undefined",
- "0-10% of device lifetime used",
- "10-20% of device lifetime used",
- "20-30% of device lifetime used",
- "30-40% of device lifetime used",
- "40-50% of device lifetime used",
- "50-60% of device lifetime used",
- "60-70% of device lifetime used",
- "70-80% of device lifetime used",
- "80-90% of device lifetime used",
- "90-100% of device lifetime used",
- "Exceeded the maximum estimated device lifetime",
- };
-
- if (buffer.length() < (lifetime + sizeof(hex))) {
- printf("*** %s: truncated content %zu\n", ext_csd_path, buffer.length());
- break;
- }
-
- ext_device_life_time_est = 0;
- sub = buffer.substr(lifetime, sizeof(hex));
- if (sscanf(sub.c_str(), "%2x", &ext_device_life_time_est) != 1) {
- printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%s\"\n", ext_csd_path,
- (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / sizeof(hex)) + 'A',
- sub.c_str());
- continue;
- }
- printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n",
- (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / sizeof(hex)) + 'A',
- ext_device_life_time_est,
- est_str[(ext_device_life_time_est < (int)(sizeof(est_str) / sizeof(est_str[0])))
- ? ext_device_life_time_est
- : 0]);
- }
-
- printf("\n");
-}
diff --git a/cmds/service/Android.bp b/cmds/service/Android.bp
index 9513ec1..a5b1ac5 100644
--- a/cmds/service/Android.bp
+++ b/cmds/service/Android.bp
@@ -4,6 +4,7 @@
srcs: ["service.cpp"],
shared_libs: [
+ "libcutils",
"libutils",
"libbinder",
],
@@ -22,6 +23,7 @@
srcs: ["service.cpp"],
shared_libs: [
+ "libcutils",
"libutils",
"libbinder",
],
diff --git a/cmds/service/service.cpp b/cmds/service/service.cpp
index 543357c..18b6b58 100644
--- a/cmds/service/service.cpp
+++ b/cmds/service/service.cpp
@@ -18,13 +18,18 @@
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/TextOutput.h>
+#include <cutils/ashmem.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+#include <sys/mman.h>
#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
using namespace android;
@@ -187,6 +192,57 @@
} else if (strcmp(argv[optind], "null") == 0) {
optind++;
data.writeStrongBinder(nullptr);
+ } else if (strcmp(argv[optind], "fd") == 0) {
+ optind++;
+ if (optind >= argc) {
+ aerr << "service: no path supplied for 'fd'" << endl;
+ wantsUsage = true;
+ result = 10;
+ break;
+ }
+ const char *path = argv[optind++];
+ int fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ aerr << "service: could not open '" << path << "'" << endl;
+ wantsUsage = true;
+ result = 10;
+ break;
+ }
+ data.writeFileDescriptor(fd, true /* take ownership */);
+ } else if (strcmp(argv[optind], "afd") == 0) {
+ optind++;
+ if (optind >= argc) {
+ aerr << "service: no path supplied for 'afd'" << endl;
+ wantsUsage = true;
+ result = 10;
+ break;
+ }
+ const char *path = argv[optind++];
+ int fd = open(path, O_RDONLY);
+ struct stat statbuf;
+ if (fd < 0 || fstat(fd, &statbuf) != 0) {
+ aerr << "service: could not open or stat '" << path << "'" << endl;
+ wantsUsage = true;
+ result = 10;
+ break;
+ }
+ int afd = ashmem_create_region("test", statbuf.st_size);
+ void* ptr = mmap(NULL, statbuf.st_size,
+ PROT_READ | PROT_WRITE, MAP_SHARED, afd, 0);
+ read(fd, ptr, statbuf.st_size);
+ close(fd);
+ data.writeFileDescriptor(afd, true /* take ownership */);
+ } else if (strcmp(argv[optind], "nfd") == 0) {
+ optind++;
+ if (optind >= argc) {
+ aerr << "service: no file descriptor supplied for 'nfd'" << endl;
+ wantsUsage = true;
+ result = 10;
+ break;
+ }
+ data.writeFileDescriptor(
+ atoi(argv[optind++]), true /* take ownership */);
+
} else if (strcmp(argv[optind], "intent") == 0) {
char* action = nullptr;
@@ -300,13 +356,19 @@
aout << "Usage: service [-h|-?]\n"
" service list\n"
" service check SERVICE\n"
- " service call SERVICE CODE [i32 N | i64 N | f N | d N | s16 STR ] ...\n"
+ " service call SERVICE CODE [i32 N | i64 N | f N | d N | s16 STR | null"
+ " | fd f | nfd n | afd f ] ...\n"
"Options:\n"
" i32: Write the 32-bit integer N into the send parcel.\n"
" i64: Write the 64-bit integer N into the send parcel.\n"
" f: Write the 32-bit single-precision number N into the send parcel.\n"
" d: Write the 64-bit double-precision number N into the send parcel.\n"
- " s16: Write the UTF-16 string STR into the send parcel.\n";
+ " s16: Write the UTF-16 string STR into the send parcel.\n"
+ " null: Write a null binder into the send parcel.\n"
+ " fd: Write a file descriptor for the file f to the send parcel.\n"
+ " nfd: Write file descriptor n to the send parcel.\n"
+ " afd: Write an ashmem file descriptor for a region containing the data from"
+ " file f to the send parcel.\n";
// " intent: Write and Intent int the send parcel. ARGS can be\n"
// " action=STR data=STR type=STR launchFlags=INT component=STR categories=STR[,STR,...]\n";
return result;
diff --git a/cmds/servicemanager/Access.cpp b/cmds/servicemanager/Access.cpp
index f4005c4..d936dbe 100644
--- a/cmds/servicemanager/Access.cpp
+++ b/cmds/servicemanager/Access.cpp
@@ -69,7 +69,7 @@
return 0;
}
- snprintf(buf, len, "service=%s pid=%d uid=%d", ad->name.c_str(), ad->debugPid, ad->uid);
+ snprintf(buf, len, "pid=%d uid=%d", ad->debugPid, ad->uid);
return 0;
}
@@ -91,7 +91,7 @@
freecon(mThisProcessContext);
}
-Access::CallingContext Access::getCallingContext(const std::string& name) {
+Access::CallingContext Access::getCallingContext() {
IPCThreadState* ipc = IPCThreadState::self();
const char* callingSid = ipc->getCallingSid();
@@ -101,21 +101,18 @@
.debugPid = callingPid,
.uid = ipc->getCallingUid(),
.sid = callingSid ? std::string(callingSid) : getPidcon(callingPid),
- .name = name,
};
}
-bool Access::canFind(const CallingContext& ctx) {
- return actionAllowedFromLookup(ctx, "find");
+bool Access::canFind(const CallingContext& ctx,const std::string& name) {
+ return actionAllowedFromLookup(ctx, name, "find");
}
-bool Access::canAdd(const CallingContext& ctx) {
- return actionAllowedFromLookup(ctx, "add");
+bool Access::canAdd(const CallingContext& ctx, const std::string& name) {
+ return actionAllowedFromLookup(ctx, name, "add");
}
bool Access::canList(const CallingContext& ctx) {
- CHECK(ctx.name == "");
-
return actionAllowed(ctx, mThisProcessContext, "list");
}
@@ -125,10 +122,10 @@
return 0 == selinux_check_access(sctx.sid.c_str(), tctx, tclass, perm, reinterpret_cast<void*>(const_cast<CallingContext*>((&sctx))));
}
-bool Access::actionAllowedFromLookup(const CallingContext& sctx, const char *perm) {
+bool Access::actionAllowedFromLookup(const CallingContext& sctx, const std::string& name, const char *perm) {
char *tctx = nullptr;
- if (selabel_lookup(getSehandle(), &tctx, sctx.name.c_str(), 0) != 0) {
- LOG(ERROR) << "SELinux: No match for " << sctx.name << " in service_contexts.\n";
+ if (selabel_lookup(getSehandle(), &tctx, name.c_str(), 0) != 0) {
+ LOG(ERROR) << "SELinux: No match for " << name << " in service_contexts.\n";
return false;
}
diff --git a/cmds/servicemanager/Access.h b/cmds/servicemanager/Access.h
index b2c78cc..05a60d3 100644
--- a/cmds/servicemanager/Access.h
+++ b/cmds/servicemanager/Access.h
@@ -36,22 +36,18 @@
pid_t debugPid;
uid_t uid;
std::string sid;
-
- // name of the service
- //
- // empty if call is unrelated to service (e.g. list)
- std::string name;
};
- virtual CallingContext getCallingContext(const std::string& name);
+ virtual CallingContext getCallingContext();
- virtual bool canFind(const CallingContext& ctx);
- virtual bool canAdd(const CallingContext& ctx);
+ virtual bool canFind(const CallingContext& ctx, const std::string& name);
+ virtual bool canAdd(const CallingContext& ctx, const std::string& name);
virtual bool canList(const CallingContext& ctx);
private:
bool actionAllowed(const CallingContext& sctx, const char* tctx, const char* perm);
- bool actionAllowedFromLookup(const CallingContext& sctx, const char *perm);
+ bool actionAllowedFromLookup(const CallingContext& sctx, const std::string& name,
+ const char *perm);
char* mThisProcessContext = nullptr;
};
diff --git a/cmds/servicemanager/ServiceManager.cpp b/cmds/servicemanager/ServiceManager.cpp
index 6cfcf40..c2c71e0 100644
--- a/cmds/servicemanager/ServiceManager.cpp
+++ b/cmds/servicemanager/ServiceManager.cpp
@@ -32,7 +32,7 @@
}
Status ServiceManager::checkService(const std::string& name, sp<IBinder>* outBinder) {
- auto ctx = mAccess->getCallingContext(name);
+ auto ctx = mAccess->getCallingContext();
auto it = mNameToService.find(name);
if (it == mNameToService.end()) {
@@ -53,7 +53,7 @@
}
// TODO(b/136023468): move this check to be first
- if (!mAccess->canFind(ctx)) {
+ if (!mAccess->canFind(ctx, name)) {
// returns ok and null for legacy reasons
*outBinder = nullptr;
return Status::ok();
@@ -79,14 +79,14 @@
}
Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
- auto ctx = mAccess->getCallingContext(name);
+ auto ctx = mAccess->getCallingContext();
// apps cannot add services
if (multiuser_get_app_id(ctx.uid) >= AID_APP) {
return Status::fromExceptionCode(Status::EX_SECURITY);
}
- if (!mAccess->canAdd(ctx)) {
+ if (!mAccess->canAdd(ctx, name)) {
return Status::fromExceptionCode(Status::EX_SECURITY);
}
@@ -121,7 +121,7 @@
}
Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
- if (!mAccess->canList(mAccess->getCallingContext(""))) {
+ if (!mAccess->canList(mAccess->getCallingContext())) {
return Status::fromExceptionCode(Status::EX_SECURITY);
}
diff --git a/cmds/servicemanager/test_sm.cpp b/cmds/servicemanager/test_sm.cpp
index 25d2aa8..91485e4 100644
--- a/cmds/servicemanager/test_sm.cpp
+++ b/cmds/servicemanager/test_sm.cpp
@@ -40,18 +40,18 @@
class MockAccess : public Access {
public:
- MOCK_METHOD1(getCallingContext, CallingContext(const std::string& name));
- MOCK_METHOD1(canAdd, bool(const CallingContext&));
- MOCK_METHOD1(canFind, bool(const CallingContext&));
+ MOCK_METHOD0(getCallingContext, CallingContext());
+ MOCK_METHOD2(canAdd, bool(const CallingContext&, const std::string& name));
+ MOCK_METHOD2(canFind, bool(const CallingContext&, const std::string& name));
MOCK_METHOD1(canList, bool(const CallingContext&));
};
static sp<ServiceManager> getPermissiveServiceManager() {
std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
- ON_CALL(*access, getCallingContext(_)).WillByDefault(Return(Access::CallingContext{}));
- ON_CALL(*access, canAdd(_)).WillByDefault(Return(true));
- ON_CALL(*access, canFind(_)).WillByDefault(Return(true));
+ ON_CALL(*access, getCallingContext()).WillByDefault(Return(Access::CallingContext{}));
+ ON_CALL(*access, canAdd(_, _)).WillByDefault(Return(true));
+ ON_CALL(*access, canFind(_, _)).WillByDefault(Return(true));
ON_CALL(*access, canList(_)).WillByDefault(Return(true));
sp<ServiceManager> sm = new ServiceManager(std::move(access));
@@ -97,11 +97,11 @@
TEST(AddService, AddDisallowedFromApp) {
for (uid_t uid : { AID_APP_START, AID_APP_START + 1, AID_APP_END }) {
std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
- EXPECT_CALL(*access, getCallingContext(_)).WillOnce(Return(Access::CallingContext{
+ EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{
.debugPid = 1337,
.uid = uid,
}));
- EXPECT_CALL(*access, canAdd(_)).Times(0);
+ EXPECT_CALL(*access, canAdd(_, _)).Times(0);
sp<ServiceManager> sm = new ServiceManager(std::move(access));
EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
@@ -121,8 +121,8 @@
TEST(AddService, NoPermissions) {
std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
- EXPECT_CALL(*access, getCallingContext(_)).WillOnce(Return(Access::CallingContext{}));
- EXPECT_CALL(*access, canAdd(_)).WillOnce(Return(false));
+ EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
+ EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(false));
sp<ServiceManager> sm = new ServiceManager(std::move(access));
@@ -151,9 +151,9 @@
TEST(GetService, NoPermissionsForGettingService) {
std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
- EXPECT_CALL(*access, getCallingContext(_)).WillRepeatedly(Return(Access::CallingContext{}));
- EXPECT_CALL(*access, canAdd(_)).WillOnce(Return(true));
- EXPECT_CALL(*access, canFind(_)).WillOnce(Return(false));
+ EXPECT_CALL(*access, getCallingContext()).WillRepeatedly(Return(Access::CallingContext{}));
+ EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
+ EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(false));
sp<ServiceManager> sm = new ServiceManager(std::move(access));
@@ -169,15 +169,15 @@
TEST(GetService, AllowedFromIsolated) {
std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
- EXPECT_CALL(*access, getCallingContext(_))
+ EXPECT_CALL(*access, getCallingContext())
// something adds it
.WillOnce(Return(Access::CallingContext{}))
// next call is from isolated app
.WillOnce(Return(Access::CallingContext{
.uid = AID_ISOLATED_START,
}));
- EXPECT_CALL(*access, canAdd(_)).WillOnce(Return(true));
- EXPECT_CALL(*access, canFind(_)).WillOnce(Return(true));
+ EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
+ EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
sp<ServiceManager> sm = new ServiceManager(std::move(access));
@@ -192,17 +192,17 @@
TEST(GetService, NotAllowedFromIsolated) {
std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
- EXPECT_CALL(*access, getCallingContext(_))
+ EXPECT_CALL(*access, getCallingContext())
// something adds it
.WillOnce(Return(Access::CallingContext{}))
// next call is from isolated app
.WillOnce(Return(Access::CallingContext{
.uid = AID_ISOLATED_START,
}));
- EXPECT_CALL(*access, canAdd(_)).WillOnce(Return(true));
+ EXPECT_CALL(*access, canAdd(_, _)).WillOnce(Return(true));
// TODO(b/136023468): when security check is first, this should be called first
- // EXPECT_CALL(*access, canFind(_)).WillOnce(Return(true));
+ // EXPECT_CALL(*access, canFind(_, _)).WillOnce(Return(true));
sp<ServiceManager> sm = new ServiceManager(std::move(access));
@@ -218,7 +218,7 @@
TEST(ListServices, NoPermissions) {
std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
- EXPECT_CALL(*access, getCallingContext(_)).WillOnce(Return(Access::CallingContext{}));
+ EXPECT_CALL(*access, getCallingContext()).WillOnce(Return(Access::CallingContext{}));
EXPECT_CALL(*access, canList(_)).WillOnce(Return(false));
sp<ServiceManager> sm = new ServiceManager(std::move(access));
diff --git a/cmds/surfacereplayer/proto/src/trace.proto b/cmds/surfacereplayer/proto/src/trace.proto
index c70bc3e..a738527 100644
--- a/cmds/surfacereplayer/proto/src/trace.proto
+++ b/cmds/surfacereplayer/proto/src/trace.proto
@@ -46,6 +46,10 @@
SecureFlagChange secure_flag = 14;
DeferredTransactionChange deferred_transaction = 15;
CornerRadiusChange corner_radius = 16;
+ ReparentChange reparent = 17;
+ RelativeParentChange relative_parent = 18;
+ DetachChildrenChange detach_children = 19;
+ ReparentChildrenChange reparent_children = 20;
}
}
@@ -177,3 +181,20 @@
required int32 id = 1;
required int32 mode = 2;
}
+
+message ReparentChange {
+ required int32 parent_id = 1;
+}
+
+message ReparentChildrenChange {
+ required int32 parent_id = 1;
+}
+
+message RelativeParentChange {
+ required int32 relative_parent_id = 1;
+ required int32 z = 2;
+}
+
+message DetachChildrenChange {
+ required bool detach_children = 1;
+}
diff --git a/cmds/surfacereplayer/replayer/Replayer.cpp b/cmds/surfacereplayer/replayer/Replayer.cpp
index 34886a9..a4a9b6a 100644
--- a/cmds/surfacereplayer/replayer/Replayer.cpp
+++ b/cmds/surfacereplayer/replayer/Replayer.cpp
@@ -412,6 +412,18 @@
setDeferredTransaction(transaction, change.id(),
change.deferred_transaction());
break;
+ case SurfaceChange::SurfaceChangeCase::kReparent:
+ setReparentChange(transaction, change.id(), change.reparent());
+ break;
+ case SurfaceChange::SurfaceChangeCase::kReparentChildren:
+ setReparentChildrenChange(transaction, change.id(), change.reparent_children());
+ break;
+ case SurfaceChange::SurfaceChangeCase::kRelativeParent:
+ setRelativeParentChange(transaction, change.id(), change.relative_parent());
+ break;
+ case SurfaceChange::SurfaceChangeCase::kDetachChildren:
+ setDetachChildrenChange(transaction, change.id(), change.detach_children());
+ break;
default:
status = 1;
break;
@@ -680,3 +692,35 @@
mComposerClient = new SurfaceComposerClient;
return mComposerClient->initCheck();
}
+
+void Replayer::setReparentChange(SurfaceComposerClient::Transaction& t,
+ layer_id id, const ReparentChange& c) {
+ sp<IBinder> newParentHandle = nullptr;
+ if (mLayers.count(c.parent_id()) != 0 && mLayers[c.parent_id()] != nullptr) {
+ newParentHandle = mLayers[c.parent_id()]->getHandle();
+ }
+ t.reparent(mLayers[id], newParentHandle);
+}
+
+void Replayer::setRelativeParentChange(SurfaceComposerClient::Transaction& t,
+ layer_id id, const RelativeParentChange& c) {
+ if (mLayers.count(c.relative_parent_id()) == 0 || mLayers[c.relative_parent_id()] == nullptr) {
+ ALOGE("Layer %d not found in set relative parent transaction", c.relative_parent_id());
+ return;
+ }
+ t.setRelativeLayer(mLayers[id], mLayers[c.relative_parent_id()]->getHandle(), c.z());
+}
+
+void Replayer::setDetachChildrenChange(SurfaceComposerClient::Transaction& t,
+ layer_id id, const DetachChildrenChange& c) {
+ t.detachChildren(mLayers[id]);
+}
+
+void Replayer::setReparentChildrenChange(SurfaceComposerClient::Transaction& t,
+ layer_id id, const ReparentChildrenChange& c) {
+ if (mLayers.count(c.parent_id()) == 0 || mLayers[c.parent_id()] == nullptr) {
+ ALOGE("Layer %d not found in reparent children transaction", c.parent_id());
+ return;
+ }
+ t.reparentChildren(mLayers[id], mLayers[c.parent_id()]->getHandle());
+}
diff --git a/cmds/surfacereplayer/replayer/Replayer.h b/cmds/surfacereplayer/replayer/Replayer.h
index ad807ee..d7709cc 100644
--- a/cmds/surfacereplayer/replayer/Replayer.h
+++ b/cmds/surfacereplayer/replayer/Replayer.h
@@ -108,6 +108,14 @@
layer_id id, const SecureFlagChange& sfc);
void setDeferredTransaction(SurfaceComposerClient::Transaction& t,
layer_id id, const DeferredTransactionChange& dtc);
+ void setReparentChange(SurfaceComposerClient::Transaction& t,
+ layer_id id, const ReparentChange& c);
+ void setRelativeParentChange(SurfaceComposerClient::Transaction& t,
+ layer_id id, const RelativeParentChange& c);
+ void setDetachChildrenChange(SurfaceComposerClient::Transaction& t,
+ layer_id id, const DetachChildrenChange& c);
+ void setReparentChildrenChange(SurfaceComposerClient::Transaction& t,
+ layer_id id, const ReparentChildrenChange& c);
void setDisplaySurface(SurfaceComposerClient::Transaction& t,
display_id id, const DispSurfaceChange& dsc);
diff --git a/libs/binder/ndk/Android.bp b/libs/binder/ndk/Android.bp
index 21bef2e..6da3086 100644
--- a/libs/binder/ndk/Android.bp
+++ b/libs/binder/ndk/Android.bp
@@ -16,7 +16,6 @@
cc_library {
name: "libbinder_ndk",
- vendor_available: true,
export_include_dirs: [
"include_ndk",
@@ -74,3 +73,12 @@
symbol_file: "libbinder_ndk.map.txt",
first_version: "29",
}
+
+llndk_library {
+ name: "libbinder_ndk",
+ symbol_file: "libbinder_ndk.map.txt",
+ export_include_dirs: [
+ "include_ndk",
+ "include_apex",
+ ],
+}
diff --git a/libs/binder/ndk/libbinder_ndk.map.txt b/libs/binder/ndk/libbinder_ndk.map.txt
index 7e65817..4f685d1 100644
--- a/libs/binder/ndk/libbinder_ndk.map.txt
+++ b/libs/binder/ndk/libbinder_ndk.map.txt
@@ -89,12 +89,12 @@
AStatus_getStatus;
AStatus_isOk;
AStatus_newOk;
- ABinderProcess_joinThreadPool; # apex
- ABinderProcess_setThreadPoolMaxThreadCount; # apex
- ABinderProcess_startThreadPool; # apex
- AServiceManager_addService; # apex
- AServiceManager_checkService; # apex
- AServiceManager_getService; # apex
+ ABinderProcess_joinThreadPool; # apex vndk
+ ABinderProcess_setThreadPoolMaxThreadCount; # apex vndk
+ ABinderProcess_startThreadPool; # apex vndk
+ AServiceManager_addService; # apex vndk
+ AServiceManager_checkService; # apex vndk
+ AServiceManager_getService; # apex vndk
local:
*;
};
diff --git a/libs/gui/Android.bp b/libs/gui/Android.bp
index b2a7557..beb13ad 100644
--- a/libs/gui/Android.bp
+++ b/libs/gui/Android.bp
@@ -38,6 +38,7 @@
"BufferItemConsumer.cpp",
"ConsumerBase.cpp",
"CpuConsumer.cpp",
+ "DebugEGLImageTracker.cpp",
"DisplayEventReceiver.cpp",
"GLConsumer.cpp",
"GuiConfig.cpp",
diff --git a/libs/gui/DebugEGLImageTracker.cpp b/libs/gui/DebugEGLImageTracker.cpp
new file mode 100644
index 0000000..ab6f364
--- /dev/null
+++ b/libs/gui/DebugEGLImageTracker.cpp
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2019 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 <android-base/stringprintf.h>
+#include <cutils/properties.h>
+#include <gui/DebugEGLImageTracker.h>
+
+#include <cinttypes>
+#include <unordered_map>
+
+using android::base::StringAppendF;
+
+std::mutex DebugEGLImageTracker::mInstanceLock;
+std::atomic<DebugEGLImageTracker *> DebugEGLImageTracker::mInstance;
+
+class DebugEGLImageTrackerNoOp : public DebugEGLImageTracker {
+public:
+ DebugEGLImageTrackerNoOp() = default;
+ ~DebugEGLImageTrackerNoOp() override = default;
+ void create(const char * /*from*/) override {}
+ void destroy(const char * /*from*/) override {}
+
+ void dump(std::string & /*result*/) override {}
+};
+
+class DebugEGLImageTrackerImpl : public DebugEGLImageTracker {
+public:
+ DebugEGLImageTrackerImpl() = default;
+ ~DebugEGLImageTrackerImpl() override = default;
+ void create(const char * /*from*/) override;
+ void destroy(const char * /*from*/) override;
+
+ void dump(std::string & /*result*/) override;
+
+private:
+ std::mutex mLock;
+ std::unordered_map<std::string, int64_t> mCreateTracker;
+ std::unordered_map<std::string, int64_t> mDestroyTracker;
+
+ int64_t mTotalCreated = 0;
+ int64_t mTotalDestroyed = 0;
+};
+
+DebugEGLImageTracker *DebugEGLImageTracker::getInstance() {
+ std::lock_guard lock(mInstanceLock);
+ if (mInstance == nullptr) {
+ char value[PROPERTY_VALUE_MAX];
+ property_get("debug.sf.enable_egl_image_tracker", value, "0");
+ const bool enabled = static_cast<bool>(atoi(value));
+
+ if (enabled) {
+ mInstance = new DebugEGLImageTrackerImpl();
+ } else {
+ mInstance = new DebugEGLImageTrackerNoOp();
+ }
+ }
+
+ return mInstance;
+}
+
+void DebugEGLImageTrackerImpl::create(const char *from) {
+ std::lock_guard lock(mLock);
+ mCreateTracker[from]++;
+ mTotalCreated++;
+}
+
+void DebugEGLImageTrackerImpl::destroy(const char *from) {
+ std::lock_guard lock(mLock);
+ mDestroyTracker[from]++;
+ mTotalDestroyed++;
+}
+
+void DebugEGLImageTrackerImpl::dump(std::string &result) {
+ std::lock_guard lock(mLock);
+ StringAppendF(&result, "Live EGL Image objects: %" PRIi64 "\n",
+ mTotalCreated - mTotalDestroyed);
+ StringAppendF(&result, "Total EGL Image created: %" PRIi64 "\n", mTotalCreated);
+ for (const auto &[from, count] : mCreateTracker) {
+ StringAppendF(&result, "\t%s: %" PRIi64 "\n", from.c_str(), count);
+ }
+ StringAppendF(&result, "Total EGL Image destroyed: %" PRIi64 "\n", mTotalDestroyed);
+ for (const auto &[from, count] : mDestroyTracker) {
+ StringAppendF(&result, "\t%s: %" PRIi64 "\n", from.c_str(), count);
+ }
+}
diff --git a/libs/gui/GLConsumer.cpp b/libs/gui/GLConsumer.cpp
index 8d66154..8199c98 100644
--- a/libs/gui/GLConsumer.cpp
+++ b/libs/gui/GLConsumer.cpp
@@ -34,6 +34,7 @@
#include <math/mat4.h>
#include <gui/BufferItem.h>
+#include <gui/DebugEGLImageTracker.h>
#include <gui/GLConsumer.h>
#include <gui/ISurfaceComposer.h>
#include <gui/SurfaceComposerClient.h>
@@ -944,6 +945,7 @@
if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
ALOGE("~EglImage: eglDestroyImageKHR failed");
}
+ DEBUG_EGL_IMAGE_TRACKER_DESTROY();
eglTerminate(mEglDisplay);
}
}
@@ -957,6 +959,7 @@
if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
ALOGE("createIfNeeded: eglDestroyImageKHR failed");
}
+ DEBUG_EGL_IMAGE_TRACKER_DESTROY();
eglTerminate(mEglDisplay);
mEglImage = EGL_NO_IMAGE_KHR;
mEglDisplay = EGL_NO_DISPLAY;
@@ -1006,7 +1009,10 @@
EGLint error = eglGetError();
ALOGE("error creating EGLImage: %#x", error);
eglTerminate(dpy);
+ } else {
+ DEBUG_EGL_IMAGE_TRACKER_CREATE();
}
+
return image;
}
diff --git a/libs/gui/include/gui/DebugEGLImageTracker.h b/libs/gui/include/gui/DebugEGLImageTracker.h
new file mode 100644
index 0000000..5d369c9
--- /dev/null
+++ b/libs/gui/include/gui/DebugEGLImageTracker.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2019 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.
+ */
+
+#pragma once
+
+#include <atomic>
+#include <mutex>
+#include <string>
+
+class DebugEGLImageTracker {
+public:
+ static DebugEGLImageTracker *getInstance();
+
+ virtual void create(const char *from) = 0;
+ virtual void destroy(const char *from) = 0;
+
+ virtual void dump(std::string &result) = 0;
+
+protected:
+ DebugEGLImageTracker() = default;
+ virtual ~DebugEGLImageTracker() = default;
+ DebugEGLImageTracker(const DebugEGLImageTracker &) = delete;
+
+ static std::mutex mInstanceLock;
+ static std::atomic<DebugEGLImageTracker *> mInstance;
+};
+
+#define DEBUG_EGL_IMAGE_TRACKER_CREATE() \
+ (DebugEGLImageTracker::getInstance()->create(__PRETTY_FUNCTION__))
+#define DEBUG_EGL_IMAGE_TRACKER_DESTROY() \
+ (DebugEGLImageTracker::getInstance()->destroy(__PRETTY_FUNCTION__))
\ No newline at end of file
diff --git a/libs/renderengine/gl/GLESRenderEngine.cpp b/libs/renderengine/gl/GLESRenderEngine.cpp
index a2f12d0..8bfd3dd 100644
--- a/libs/renderengine/gl/GLESRenderEngine.cpp
+++ b/libs/renderengine/gl/GLESRenderEngine.cpp
@@ -31,6 +31,7 @@
#include <android-base/stringprintf.h>
#include <cutils/compiler.h>
#include <cutils/properties.h>
+#include <gui/DebugEGLImageTracker.h>
#include <renderengine/Mesh.h>
#include <renderengine/Texture.h>
#include <renderengine/private/Description.h>
@@ -433,6 +434,7 @@
EGLImageKHR expired = mFramebufferImageCache.front().second;
mFramebufferImageCache.pop_front();
eglDestroyImageKHR(mEGLDisplay, expired);
+ DEBUG_EGL_IMAGE_TRACKER_DESTROY();
}
mImageCache.clear();
eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
@@ -841,6 +843,7 @@
bool useFramebufferCache) {
sp<GraphicBuffer> graphicBuffer = GraphicBuffer::from(nativeBuffer);
if (useFramebufferCache) {
+ std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex);
for (const auto& image : mFramebufferImageCache) {
if (image.first == graphicBuffer->getId()) {
return image.second;
@@ -856,14 +859,20 @@
nativeBuffer, attributes);
if (useFramebufferCache) {
if (image != EGL_NO_IMAGE_KHR) {
+ std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex);
if (mFramebufferImageCache.size() >= mFramebufferImageCacheSize) {
EGLImageKHR expired = mFramebufferImageCache.front().second;
mFramebufferImageCache.pop_front();
eglDestroyImageKHR(mEGLDisplay, expired);
+ DEBUG_EGL_IMAGE_TRACKER_DESTROY();
}
mFramebufferImageCache.push_back({graphicBuffer->getId(), image});
}
}
+
+ if (image != EGL_NO_IMAGE_KHR) {
+ DEBUG_EGL_IMAGE_TRACKER_CREATE();
+ }
return image;
}
@@ -1266,6 +1275,23 @@
StringAppendF(&result, "RenderEngine last dataspace conversion: (%s) to (%s)\n",
dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
+ {
+ std::lock_guard<std::mutex> lock(mRenderingMutex);
+ StringAppendF(&result, "RenderEngine image cache size: %zu\n", mImageCache.size());
+ StringAppendF(&result, "Dumping buffer ids...\n");
+ for (const auto& [id, unused] : mImageCache) {
+ StringAppendF(&result, "0x%" PRIx64 "\n", id);
+ }
+ }
+ {
+ std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex);
+ StringAppendF(&result, "RenderEngine framebuffer image cache size: %zu\n",
+ mFramebufferImageCache.size());
+ StringAppendF(&result, "Dumping buffer ids...\n");
+ for (const auto& [id, unused] : mFramebufferImageCache) {
+ StringAppendF(&result, "0x%" PRIx64 "\n", id);
+ }
+ }
}
GLESRenderEngine::GlesVersion GLESRenderEngine::parseGlesVersion(const char* str) {
@@ -1392,7 +1418,7 @@
}
bool GLESRenderEngine::isFramebufferImageCachedForTesting(uint64_t bufferId) {
- std::lock_guard<std::mutex> lock(mRenderingMutex);
+ std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex);
return std::any_of(mFramebufferImageCache.cbegin(), mFramebufferImageCache.cend(),
[=](std::pair<uint64_t, EGLImageKHR> image) {
return image.first == bufferId;
diff --git a/libs/renderengine/gl/GLESRenderEngine.h b/libs/renderengine/gl/GLESRenderEngine.h
index 54fc987..c8b45d2 100644
--- a/libs/renderengine/gl/GLESRenderEngine.h
+++ b/libs/renderengine/gl/GLESRenderEngine.h
@@ -78,17 +78,20 @@
EGLDisplay getEGLDisplay() const { return mEGLDisplay; }
// Creates an output image for rendering to
EGLImageKHR createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer, bool isProtected,
- bool useFramebufferCache);
+ bool useFramebufferCache)
+ EXCLUDES(mFramebufferImageCacheMutex);
// Test-only methods
// Returns true iff mImageCache contains an image keyed by bufferId
bool isImageCachedForTesting(uint64_t bufferId) EXCLUDES(mRenderingMutex);
// Returns true iff mFramebufferImageCache contains an image keyed by bufferId
- bool isFramebufferImageCachedForTesting(uint64_t bufferId) EXCLUDES(mRenderingMutex);
+ bool isFramebufferImageCachedForTesting(uint64_t bufferId)
+ EXCLUDES(mFramebufferImageCacheMutex);
protected:
Framebuffer* getFramebufferForDrawing() override;
- void dump(std::string& result) override;
+ void dump(std::string& result) override EXCLUDES(mRenderingMutex)
+ EXCLUDES(mFramebufferImageCacheMutex);
size_t getMaxTextureSize() const override;
size_t getMaxViewportDims() const override;
@@ -190,7 +193,11 @@
uint32_t mFramebufferImageCacheSize = 0;
// Cache of output images, keyed by corresponding GraphicBuffer ID.
- std::deque<std::pair<uint64_t, EGLImageKHR>> mFramebufferImageCache;
+ std::deque<std::pair<uint64_t, EGLImageKHR>> mFramebufferImageCache
+ GUARDED_BY(mFramebufferImageCacheMutex);
+ // The only reason why we have this mutex is so that we don't segfault when
+ // dumping info.
+ std::mutex mFramebufferImageCacheMutex;
// Current dataspace of layer being rendered
ui::Dataspace mDataSpace = ui::Dataspace::UNKNOWN;
diff --git a/libs/renderengine/gl/GLFramebuffer.cpp b/libs/renderengine/gl/GLFramebuffer.cpp
index dacf8d3..5fbb5ba 100644
--- a/libs/renderengine/gl/GLFramebuffer.cpp
+++ b/libs/renderengine/gl/GLFramebuffer.cpp
@@ -22,6 +22,7 @@
#include <GLES/glext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
+#include <gui/DebugEGLImageTracker.h>
#include <nativebase/nativebase.h>
#include <utils/Trace.h>
#include "GLESRenderEngine.h"
@@ -47,6 +48,7 @@
if (mEGLImage != EGL_NO_IMAGE_KHR) {
if (!usingFramebufferCache) {
eglDestroyImageKHR(mEGLDisplay, mEGLImage);
+ DEBUG_EGL_IMAGE_TRACKER_DESTROY();
}
mEGLImage = EGL_NO_IMAGE_KHR;
mBufferWidth = 0;
diff --git a/libs/renderengine/gl/GLImage.cpp b/libs/renderengine/gl/GLImage.cpp
index 77e648e..8497721 100644
--- a/libs/renderengine/gl/GLImage.cpp
+++ b/libs/renderengine/gl/GLImage.cpp
@@ -20,6 +20,7 @@
#include <vector>
+#include <gui/DebugEGLImageTracker.h>
#include <log/log.h>
#include <utils/Trace.h>
#include "GLESRenderEngine.h"
@@ -58,6 +59,7 @@
if (!eglDestroyImageKHR(mEGLDisplay, mEGLImage)) {
ALOGE("failed to destroy image: %#x", eglGetError());
}
+ DEBUG_EGL_IMAGE_TRACKER_DESTROY();
mEGLImage = EGL_NO_IMAGE_KHR;
}
@@ -69,6 +71,7 @@
ALOGE("failed to create EGLImage: %#x", eglGetError());
return false;
}
+ DEBUG_EGL_IMAGE_TRACKER_CREATE();
mProtected = isProtected;
}
diff --git a/services/surfaceflinger/Scheduler/DispSync.cpp b/services/surfaceflinger/Scheduler/DispSync.cpp
index 83fd42b..0c94052 100644
--- a/services/surfaceflinger/Scheduler/DispSync.cpp
+++ b/services/surfaceflinger/Scheduler/DispSync.cpp
@@ -668,7 +668,13 @@
nsecs_t durationSum = 0;
nsecs_t minDuration = INT64_MAX;
nsecs_t maxDuration = 0;
- for (size_t i = 1; i < mNumResyncSamples; i++) {
+ // We skip the first 2 samples because the first vsync duration on some
+ // devices may be much more inaccurate than on other devices, e.g. due
+ // to delays in ramping up from a power collapse. By doing so this
+ // actually increases the accuracy of the DispSync model even though
+ // we're effectively relying on fewer sample points.
+ static constexpr size_t numSamplesSkipped = 2;
+ for (size_t i = numSamplesSkipped; i < mNumResyncSamples; i++) {
size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
size_t prev = (idx + MAX_RESYNC_SAMPLES - 1) % MAX_RESYNC_SAMPLES;
nsecs_t duration = mResyncSamples[idx] - mResyncSamples[prev];
@@ -679,15 +685,14 @@
// Exclude the min and max from the average
durationSum -= minDuration + maxDuration;
- mPeriod = durationSum / (mNumResyncSamples - 3);
+ mPeriod = durationSum / (mNumResyncSamples - numSamplesSkipped - 2);
ALOGV("[%s] mPeriod = %" PRId64, mName, ns2us(mPeriod));
double sampleAvgX = 0;
double sampleAvgY = 0;
double scale = 2.0 * M_PI / double(mPeriod);
- // Intentionally skip the first sample
- for (size_t i = 1; i < mNumResyncSamples; i++) {
+ for (size_t i = numSamplesSkipped; i < mNumResyncSamples; i++) {
size_t idx = (mFirstResyncSample + i) % MAX_RESYNC_SAMPLES;
nsecs_t sample = mResyncSamples[idx] - mReferenceTime;
double samplePhase = double(sample % mPeriod) * scale;
@@ -695,8 +700,8 @@
sampleAvgY += sin(samplePhase);
}
- sampleAvgX /= double(mNumResyncSamples - 1);
- sampleAvgY /= double(mNumResyncSamples - 1);
+ sampleAvgX /= double(mNumResyncSamples - numSamplesSkipped);
+ sampleAvgY /= double(mNumResyncSamples - numSamplesSkipped);
mPhase = nsecs_t(atan2(sampleAvgY, sampleAvgX) / scale);
diff --git a/services/surfaceflinger/Scheduler/LayerInfo.h b/services/surfaceflinger/Scheduler/LayerInfo.h
index 66df9dc..a733781 100644
--- a/services/surfaceflinger/Scheduler/LayerInfo.h
+++ b/services/surfaceflinger/Scheduler/LayerInfo.h
@@ -129,8 +129,8 @@
private:
std::deque<nsecs_t> mElements;
- static constexpr size_t HISTORY_SIZE = 10;
- static constexpr std::chrono::nanoseconds HISTORY_TIME = 500ms;
+ static constexpr size_t HISTORY_SIZE = 90;
+ static constexpr std::chrono::nanoseconds HISTORY_TIME = 1s;
};
public:
diff --git a/services/surfaceflinger/SurfaceFlinger.cpp b/services/surfaceflinger/SurfaceFlinger.cpp
index 5b82556..3f32f35 100644
--- a/services/surfaceflinger/SurfaceFlinger.cpp
+++ b/services/surfaceflinger/SurfaceFlinger.cpp
@@ -48,6 +48,8 @@
#include <compositionengine/impl/OutputLayerCompositionState.h>
#include <dvr/vr_flinger.h>
#include <gui/BufferQueue.h>
+#include <gui/DebugEGLImageTracker.h>
+
#include <gui/GuiConfig.h>
#include <gui/IDisplayEventConnection.h>
#include <gui/IProducerListener.h>
@@ -5019,6 +5021,8 @@
getRenderEngine().dump(result);
+ DebugEGLImageTracker::getInstance()->dump(result);
+
if (const auto display = getDefaultDisplayDeviceLocked()) {
display->getCompositionDisplay()->getState().undefinedRegion.dump(result,
"undefinedRegion");
diff --git a/services/surfaceflinger/SurfaceInterceptor.cpp b/services/surfaceflinger/SurfaceInterceptor.cpp
index 7bfe033..a02d14c 100644
--- a/services/surfaceflinger/SurfaceInterceptor.cpp
+++ b/services/surfaceflinger/SurfaceInterceptor.cpp
@@ -116,7 +116,14 @@
layer->mCurrentState.frameNumber_legacy);
}
addOverrideScalingModeLocked(transaction, layerId, layer->getEffectiveScalingMode());
- addFlagsLocked(transaction, layerId, layer->mCurrentState.flags);
+ addFlagsLocked(transaction, layerId, layer->mCurrentState.flags,
+ layer_state_t::eLayerHidden | layer_state_t::eLayerOpaque |
+ layer_state_t::eLayerSecure);
+ addReparentLocked(transaction, layerId, getLayerIdFromWeakRef(layer->mCurrentParent));
+ addDetachChildrenLocked(transaction, layerId, layer->isLayerDetached());
+ addRelativeParentLocked(transaction, layerId,
+ getLayerIdFromWeakRef(layer->mCurrentState.zOrderRelativeOf),
+ layer->mCurrentState.z);
}
void SurfaceInterceptor::addInitialDisplayStateLocked(Increment* increment,
@@ -150,7 +157,7 @@
return NO_ERROR;
}
-const sp<const Layer> SurfaceInterceptor::getLayer(const wp<const IBinder>& weakHandle) {
+const sp<const Layer> SurfaceInterceptor::getLayer(const wp<const IBinder>& weakHandle) const {
const sp<const IBinder>& handle(weakHandle.promote());
const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
const sp<const Layer> layer(layerHandle->owner.promote());
@@ -158,14 +165,31 @@
return layer;
}
-const std::string SurfaceInterceptor::getLayerName(const sp<const Layer>& layer) {
+const std::string SurfaceInterceptor::getLayerName(const sp<const Layer>& layer) const {
return layer->getName().string();
}
-int32_t SurfaceInterceptor::getLayerId(const sp<const Layer>& layer) {
+int32_t SurfaceInterceptor::getLayerId(const sp<const Layer>& layer) const {
return layer->sequence;
}
+int32_t SurfaceInterceptor::getLayerIdFromWeakRef(const wp<const Layer>& layer) const {
+ if (layer == nullptr) {
+ return -1;
+ }
+ auto strongLayer = layer.promote();
+ return strongLayer == nullptr ? -1 : getLayerId(strongLayer);
+}
+
+int32_t SurfaceInterceptor::getLayerIdFromHandle(const sp<const IBinder>& handle) const {
+ if (handle == nullptr) {
+ return -1;
+ }
+ const auto layerHandle(static_cast<const Layer::Handle*>(handle.get()));
+ const sp<const Layer> layer(layerHandle->owner.promote());
+ return layer == nullptr ? -1 : getLayerId(layer);
+}
+
Increment* SurfaceInterceptor::createTraceIncrementLocked() {
Increment* increment(mTrace.add_increment());
increment->set_time_stamp(systemTime());
@@ -252,24 +276,23 @@
}
}
-void SurfaceInterceptor::addFlagsLocked(Transaction* transaction, int32_t layerId,
- uint8_t flags)
-{
+void SurfaceInterceptor::addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags,
+ uint8_t mask) {
// There can be multiple flags changed
- if (flags & layer_state_t::eLayerHidden) {
+ if (mask & layer_state_t::eLayerHidden) {
SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
HiddenFlagChange* flagChange(change->mutable_hidden_flag());
- flagChange->set_hidden_flag(true);
+ flagChange->set_hidden_flag(flags & layer_state_t::eLayerHidden);
}
- if (flags & layer_state_t::eLayerOpaque) {
+ if (mask & layer_state_t::eLayerOpaque) {
SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
OpaqueFlagChange* flagChange(change->mutable_opaque_flag());
- flagChange->set_opaque_flag(true);
+ flagChange->set_opaque_flag(flags & layer_state_t::eLayerOpaque);
}
- if (flags & layer_state_t::eLayerSecure) {
+ if (mask & layer_state_t::eLayerSecure) {
SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
SecureFlagChange* flagChange(change->mutable_secure_flag());
- flagChange->set_secure_flag(true);
+ flagChange->set_secure_flag(flags & layer_state_t::eLayerSecure);
}
}
@@ -320,6 +343,35 @@
overrideChange->set_override_scaling_mode(overrideScalingMode);
}
+void SurfaceInterceptor::addReparentLocked(Transaction* transaction, int32_t layerId,
+ int32_t parentId) {
+ SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
+ ReparentChange* overrideChange(change->mutable_reparent());
+ overrideChange->set_parent_id(parentId);
+}
+
+void SurfaceInterceptor::addReparentChildrenLocked(Transaction* transaction, int32_t layerId,
+ int32_t parentId) {
+ SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
+ ReparentChildrenChange* overrideChange(change->mutable_reparent_children());
+ overrideChange->set_parent_id(parentId);
+}
+
+void SurfaceInterceptor::addDetachChildrenLocked(Transaction* transaction, int32_t layerId,
+ bool detached) {
+ SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
+ DetachChildrenChange* overrideChange(change->mutable_detach_children());
+ overrideChange->set_detach_children(detached);
+}
+
+void SurfaceInterceptor::addRelativeParentLocked(Transaction* transaction, int32_t layerId,
+ int32_t parentId, int z) {
+ SurfaceChange* change(createSurfaceChangeLocked(transaction, layerId));
+ RelativeParentChange* overrideChange(change->mutable_relative_parent());
+ overrideChange->set_relative_parent_id(parentId);
+ overrideChange->set_z(z);
+}
+
void SurfaceInterceptor::addSurfaceChangesLocked(Transaction* transaction,
const layer_state_t& state)
{
@@ -351,7 +403,7 @@
addTransparentRegionLocked(transaction, layerId, state.transparentRegion);
}
if (state.what & layer_state_t::eFlagsChanged) {
- addFlagsLocked(transaction, layerId, state.flags);
+ addFlagsLocked(transaction, layerId, state.flags, state.mask);
}
if (state.what & layer_state_t::eLayerStackChanged) {
addLayerStackLocked(transaction, layerId, state.layerStack);
@@ -380,6 +432,19 @@
if (state.what & layer_state_t::eOverrideScalingModeChanged) {
addOverrideScalingModeLocked(transaction, layerId, state.overrideScalingMode);
}
+ if (state.what & layer_state_t::eReparent) {
+ addReparentLocked(transaction, layerId, getLayerIdFromHandle(state.parentHandleForChild));
+ }
+ if (state.what & layer_state_t::eReparentChildren) {
+ addReparentChildrenLocked(transaction, layerId, getLayerIdFromHandle(state.reparentHandle));
+ }
+ if (state.what & layer_state_t::eDetachChildren) {
+ addDetachChildrenLocked(transaction, layerId, true);
+ }
+ if (state.what & layer_state_t::eRelativeLayerChanged) {
+ addRelativeParentLocked(transaction, layerId,
+ getLayerIdFromHandle(state.relativeLayerHandle), state.z);
+ }
}
void SurfaceInterceptor::addDisplayChangesLocked(Transaction* transaction,
diff --git a/services/surfaceflinger/SurfaceInterceptor.h b/services/surfaceflinger/SurfaceInterceptor.h
index 563a44c..7f86c14 100644
--- a/services/surfaceflinger/SurfaceInterceptor.h
+++ b/services/surfaceflinger/SurfaceInterceptor.h
@@ -116,9 +116,11 @@
void addInitialDisplayStateLocked(Increment* increment, const DisplayDeviceState& display);
status_t writeProtoFileLocked();
- const sp<const Layer> getLayer(const wp<const IBinder>& weakHandle);
- const std::string getLayerName(const sp<const Layer>& layer);
- int32_t getLayerId(const sp<const Layer>& layer);
+ const sp<const Layer> getLayer(const wp<const IBinder>& weakHandle) const;
+ const std::string getLayerName(const sp<const Layer>& layer) const;
+ int32_t getLayerId(const sp<const Layer>& layer) const;
+ int32_t getLayerIdFromWeakRef(const wp<const Layer>& layer) const;
+ int32_t getLayerIdFromHandle(const sp<const IBinder>& weakHandle) const;
Increment* createTraceIncrementLocked();
void addSurfaceCreationLocked(Increment* increment, const sp<const Layer>& layer);
@@ -141,7 +143,7 @@
const layer_state_t::matrix22_t& matrix);
void addTransparentRegionLocked(Transaction* transaction, int32_t layerId,
const Region& transRegion);
- void addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags);
+ void addFlagsLocked(Transaction* transaction, int32_t layerId, uint8_t flags, uint8_t mask);
void addLayerStackLocked(Transaction* transaction, int32_t layerId, uint32_t layerStack);
void addCropLocked(Transaction* transaction, int32_t layerId, const Rect& rect);
void addCornerRadiusLocked(Transaction* transaction, int32_t layerId, float cornerRadius);
@@ -153,6 +155,11 @@
void addTransactionLocked(Increment* increment, const Vector<ComposerState>& stateUpdates,
const DefaultKeyedVector< wp<IBinder>, DisplayDeviceState>& displays,
const Vector<DisplayState>& changedDisplays, uint32_t transactionFlags);
+ void addReparentLocked(Transaction* transaction, int32_t layerId, int32_t parentId);
+ void addReparentChildrenLocked(Transaction* transaction, int32_t layerId, int32_t parentId);
+ void addDetachChildrenLocked(Transaction* transaction, int32_t layerId, bool detached);
+ void addRelativeParentLocked(Transaction* transaction, int32_t layerId, int32_t parentId,
+ int z);
// Add display transactions to the trace
DisplayChange* createDisplayChangeLocked(Transaction* transaction, int32_t sequenceId);
diff --git a/services/surfaceflinger/tests/SurfaceInterceptor_test.cpp b/services/surfaceflinger/tests/SurfaceInterceptor_test.cpp
index 5cc946a..26c6da9 100644
--- a/services/surfaceflinger/tests/SurfaceInterceptor_test.cpp
+++ b/services/surfaceflinger/tests/SurfaceInterceptor_test.cpp
@@ -43,14 +43,17 @@
constexpr uint32_t SIZE_UPDATE = 134;
constexpr uint32_t STACK_UPDATE = 1;
constexpr uint64_t DEFERRED_UPDATE = 0;
+constexpr int32_t RELATIVE_Z = 42;
constexpr float ALPHA_UPDATE = 0.29f;
constexpr float CORNER_RADIUS_UPDATE = 0.2f;
constexpr float POSITION_UPDATE = 121;
const Rect CROP_UPDATE(16, 16, 32, 32);
const String8 DISPLAY_NAME("SurfaceInterceptor Display Test");
-constexpr auto TEST_SURFACE_NAME = "BG Interceptor Test Surface";
-constexpr auto UNIQUE_TEST_SURFACE_NAME = "BG Interceptor Test Surface#0";
+constexpr auto TEST_BG_SURFACE_NAME = "BG Interceptor Test Surface";
+constexpr auto TEST_FG_SURFACE_NAME = "FG Interceptor Test Surface";
+constexpr auto UNIQUE_TEST_BG_SURFACE_NAME = "BG Interceptor Test Surface#0";
+constexpr auto UNIQUE_TEST_FG_SURFACE_NAME = "FG Interceptor Test Surface#0";
constexpr auto LAYER_NAME = "Layer Create and Delete Test";
constexpr auto UNIQUE_LAYER_NAME = "Layer Create and Delete Test#0";
@@ -136,12 +139,15 @@
void TearDown() override {
mComposerClient->dispose();
mBGSurfaceControl.clear();
+ mFGSurfaceControl.clear();
mComposerClient.clear();
}
sp<SurfaceComposerClient> mComposerClient;
sp<SurfaceControl> mBGSurfaceControl;
+ sp<SurfaceControl> mFGSurfaceControl;
int32_t mBGLayerId;
+ int32_t mFGLayerId;
public:
using TestTransactionAction = void (SurfaceInterceptorTest::*)(Transaction&);
@@ -177,6 +183,10 @@
bool opaqueFlagUpdateFound(const SurfaceChange& change, bool foundOpaqueFlag);
bool secureFlagUpdateFound(const SurfaceChange& change, bool foundSecureFlag);
bool deferredTransactionUpdateFound(const SurfaceChange& change, bool foundDeferred);
+ bool reparentUpdateFound(const SurfaceChange& change, bool found);
+ bool relativeParentUpdateFound(const SurfaceChange& change, bool found);
+ bool detachChildrenUpdateFound(const SurfaceChange& change, bool found);
+ bool reparentChildrenUpdateFound(const SurfaceChange& change, bool found);
bool surfaceUpdateFound(const Trace& trace, SurfaceChange::SurfaceChangeCase changeCase);
// Find all of the updates in the single trace
@@ -209,6 +219,10 @@
void opaqueFlagUpdate(Transaction&);
void secureFlagUpdate(Transaction&);
void deferredTransactionUpdate(Transaction&);
+ void reparentUpdate(Transaction&);
+ void relativeParentUpdate(Transaction&);
+ void detachChildrenUpdate(Transaction&);
+ void reparentChildrenUpdate(Transaction&);
void surfaceCreation(Transaction&);
void displayCreation(Transaction&);
void displayDeletion(Transaction&);
@@ -250,21 +264,30 @@
ssize_t displayHeight = info.h;
// Background surface
- mBGSurfaceControl = mComposerClient->createSurface(
- String8(TEST_SURFACE_NAME), displayWidth, displayHeight,
- PIXEL_FORMAT_RGBA_8888, 0);
+ mBGSurfaceControl = mComposerClient->createSurface(String8(TEST_BG_SURFACE_NAME), displayWidth,
+ displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
ASSERT_TRUE(mBGSurfaceControl != nullptr);
ASSERT_TRUE(mBGSurfaceControl->isValid());
+ // Foreground surface
+ mFGSurfaceControl = mComposerClient->createSurface(String8(TEST_FG_SURFACE_NAME), displayWidth,
+ displayHeight, PIXEL_FORMAT_RGBA_8888, 0);
+ ASSERT_TRUE(mFGSurfaceControl != nullptr);
+ ASSERT_TRUE(mFGSurfaceControl->isValid());
+
Transaction t;
t.setDisplayLayerStack(display, 0);
- ASSERT_EQ(NO_ERROR, t.setLayer(mBGSurfaceControl, INT_MAX-3)
- .show(mBGSurfaceControl)
- .apply());
+ ASSERT_EQ(NO_ERROR,
+ t.setLayer(mBGSurfaceControl, INT_MAX - 3)
+ .show(mBGSurfaceControl)
+ .setLayer(mFGSurfaceControl, INT_MAX - 3)
+ .show(mFGSurfaceControl)
+ .apply());
}
void SurfaceInterceptorTest::preProcessTrace(const Trace& trace) {
- mBGLayerId = getSurfaceId(trace, UNIQUE_TEST_SURFACE_NAME);
+ mBGLayerId = getSurfaceId(trace, UNIQUE_TEST_BG_SURFACE_NAME);
+ mFGLayerId = getSurfaceId(trace, UNIQUE_TEST_FG_SURFACE_NAME);
}
void SurfaceInterceptorTest::captureTest(TestTransactionAction action,
@@ -364,6 +387,22 @@
DEFERRED_UPDATE);
}
+void SurfaceInterceptorTest::reparentUpdate(Transaction& t) {
+ t.reparent(mBGSurfaceControl, mFGSurfaceControl->getHandle());
+}
+
+void SurfaceInterceptorTest::relativeParentUpdate(Transaction& t) {
+ t.setRelativeLayer(mBGSurfaceControl, mFGSurfaceControl->getHandle(), RELATIVE_Z);
+}
+
+void SurfaceInterceptorTest::detachChildrenUpdate(Transaction& t) {
+ t.detachChildren(mBGSurfaceControl);
+}
+
+void SurfaceInterceptorTest::reparentChildrenUpdate(Transaction& t) {
+ t.reparentChildren(mBGSurfaceControl, mFGSurfaceControl->getHandle());
+}
+
void SurfaceInterceptorTest::displayCreation(Transaction&) {
sp<IBinder> testDisplay = SurfaceComposerClient::createDisplay(DISPLAY_NAME, true);
SurfaceComposerClient::destroyDisplay(testDisplay);
@@ -389,6 +428,10 @@
runInTransaction(&SurfaceInterceptorTest::opaqueFlagUpdate);
runInTransaction(&SurfaceInterceptorTest::secureFlagUpdate);
runInTransaction(&SurfaceInterceptorTest::deferredTransactionUpdate);
+ runInTransaction(&SurfaceInterceptorTest::reparentUpdate);
+ runInTransaction(&SurfaceInterceptorTest::reparentChildrenUpdate);
+ runInTransaction(&SurfaceInterceptorTest::detachChildrenUpdate);
+ runInTransaction(&SurfaceInterceptorTest::relativeParentUpdate);
}
void SurfaceInterceptorTest::surfaceCreation(Transaction&) {
@@ -569,6 +612,46 @@
return foundDeferred;
}
+bool SurfaceInterceptorTest::reparentUpdateFound(const SurfaceChange& change, bool found) {
+ bool hasId(change.reparent().parent_id() == mFGLayerId);
+ if (hasId && !found) {
+ found = true;
+ } else if (hasId && found) {
+ []() { FAIL(); }();
+ }
+ return found;
+}
+
+bool SurfaceInterceptorTest::relativeParentUpdateFound(const SurfaceChange& change, bool found) {
+ bool hasId(change.relative_parent().relative_parent_id() == mFGLayerId);
+ if (hasId && !found) {
+ found = true;
+ } else if (hasId && found) {
+ []() { FAIL(); }();
+ }
+ return found;
+}
+
+bool SurfaceInterceptorTest::detachChildrenUpdateFound(const SurfaceChange& change, bool found) {
+ bool detachChildren(change.detach_children().detach_children());
+ if (detachChildren && !found) {
+ found = true;
+ } else if (detachChildren && found) {
+ []() { FAIL(); }();
+ }
+ return found;
+}
+
+bool SurfaceInterceptorTest::reparentChildrenUpdateFound(const SurfaceChange& change, bool found) {
+ bool hasId(change.reparent_children().parent_id() == mFGLayerId);
+ if (hasId && !found) {
+ found = true;
+ } else if (hasId && found) {
+ []() { FAIL(); }();
+ }
+ return found;
+}
+
bool SurfaceInterceptorTest::surfaceUpdateFound(const Trace& trace,
SurfaceChange::SurfaceChangeCase changeCase) {
bool foundUpdate = false;
@@ -620,6 +703,18 @@
case SurfaceChange::SurfaceChangeCase::kDeferredTransaction:
foundUpdate = deferredTransactionUpdateFound(change, foundUpdate);
break;
+ case SurfaceChange::SurfaceChangeCase::kReparent:
+ foundUpdate = reparentUpdateFound(change, foundUpdate);
+ break;
+ case SurfaceChange::SurfaceChangeCase::kReparentChildren:
+ foundUpdate = reparentChildrenUpdateFound(change, foundUpdate);
+ break;
+ case SurfaceChange::SurfaceChangeCase::kRelativeParent:
+ foundUpdate = relativeParentUpdateFound(change, foundUpdate);
+ break;
+ case SurfaceChange::SurfaceChangeCase::kDetachChildren:
+ foundUpdate = detachChildrenUpdateFound(change, foundUpdate);
+ break;
case SurfaceChange::SurfaceChangeCase::SURFACECHANGE_NOT_SET:
break;
}
@@ -644,6 +739,10 @@
ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kOpaqueFlag));
ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kSecureFlag));
ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kDeferredTransaction));
+ ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kReparent));
+ ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kReparentChildren));
+ ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kRelativeParent));
+ ASSERT_TRUE(surfaceUpdateFound(trace, SurfaceChange::SurfaceChangeCase::kDetachChildren));
}
bool SurfaceInterceptorTest::surfaceCreationFound(const Increment& increment, bool foundSurface) {
@@ -798,6 +897,26 @@
SurfaceChange::SurfaceChangeCase::kDeferredTransaction);
}
+TEST_F(SurfaceInterceptorTest, InterceptReparentUpdateWorks) {
+ captureTest(&SurfaceInterceptorTest::reparentUpdate,
+ SurfaceChange::SurfaceChangeCase::kReparent);
+}
+
+TEST_F(SurfaceInterceptorTest, InterceptReparentChildrenUpdateWorks) {
+ captureTest(&SurfaceInterceptorTest::reparentChildrenUpdate,
+ SurfaceChange::SurfaceChangeCase::kReparentChildren);
+}
+
+TEST_F(SurfaceInterceptorTest, InterceptRelativeParentUpdateWorks) {
+ captureTest(&SurfaceInterceptorTest::relativeParentUpdate,
+ SurfaceChange::SurfaceChangeCase::kRelativeParent);
+}
+
+TEST_F(SurfaceInterceptorTest, InterceptDetachChildrenUpdateWorks) {
+ captureTest(&SurfaceInterceptorTest::detachChildrenUpdate,
+ SurfaceChange::SurfaceChangeCase::kDetachChildren);
+}
+
TEST_F(SurfaceInterceptorTest, InterceptAllUpdatesWorks) {
captureTest(&SurfaceInterceptorTest::runAllUpdates,
&SurfaceInterceptorTest::assertAllUpdatesFound);
diff --git a/services/surfaceflinger/tests/unittests/LayerHistoryTest.cpp b/services/surfaceflinger/tests/unittests/LayerHistoryTest.cpp
index 7ec9066..8e7440c 100644
--- a/services/surfaceflinger/tests/unittests/LayerHistoryTest.cpp
+++ b/services/surfaceflinger/tests/unittests/LayerHistoryTest.cpp
@@ -27,6 +27,15 @@
static constexpr float MIN_REFRESH_RATE = 30.f;
static constexpr float MAX_REFRESH_RATE = 90.f;
+ static constexpr auto RELEVANT_FRAME_THRESHOLD = 90u;
+ static constexpr uint64_t THIRTY_FPS_INTERVAL = 33'333'333;
+
+ void forceRelevancy(const std::unique_ptr<LayerHistory::LayerHandle>& testLayer) {
+ mLayerHistory->setVisibility(testLayer, true);
+ for (auto i = 0u; i < RELEVANT_FRAME_THRESHOLD; i++) {
+ mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
+ }
+ };
};
LayerHistoryTest::LayerHistoryTest() {
@@ -39,24 +48,18 @@
std::unique_ptr<LayerHistory::LayerHandle> testLayer =
mLayerHistory->createLayer("TestLayer", MIN_REFRESH_RATE, MAX_REFRESH_RATE);
mLayerHistory->setVisibility(testLayer, true);
+ for (auto i = 0u; i < RELEVANT_FRAME_THRESHOLD; i++) {
+ EXPECT_FLOAT_EQ(0.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
+ mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
+ }
- mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
- EXPECT_FLOAT_EQ(0.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
-
- mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
- mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
- mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
- // This is still 0, because the layer is not considered recently active if it
- // has been present in less than 10 frames.
- EXPECT_FLOAT_EQ(0.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
- mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
- mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
- mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
- mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
- mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
- mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
- // This should be MAX_REFRESH_RATE as we have more than 10 samples
- EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
+ // Add a few more. This time we should get MAX refresh rate as the layer
+ // becomes relevant
+ static constexpr auto A_FEW = 10;
+ for (auto i = 0u; i < A_FEW; i++) {
+ EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
+ mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
+ }
}
TEST_F(LayerHistoryTest, oneHDRLayer) {
@@ -79,8 +82,9 @@
mLayerHistory->setVisibility(test30FpsLayer, true);
nsecs_t startTime = systemTime();
- for (int i = 0; i < 31; i++) {
- mLayerHistory->insert(test30FpsLayer, startTime + (i * 33333333), false /*isHDR*/);
+ for (int i = 0; i < RELEVANT_FRAME_THRESHOLD; i++) {
+ mLayerHistory->insert(test30FpsLayer, startTime + (i * THIRTY_FPS_INTERVAL),
+ false /*isHDR*/);
}
EXPECT_FLOAT_EQ(30.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
@@ -98,19 +102,21 @@
mLayerHistory->setVisibility(testLayer2, true);
nsecs_t startTime = systemTime();
- for (int i = 0; i < 10; i++) {
+ for (int i = 0; i < RELEVANT_FRAME_THRESHOLD; i++) {
mLayerHistory->insert(testLayer, 0, false /*isHDR*/);
}
EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
startTime = systemTime();
- for (int i = 0; i < 10; i++) {
- mLayerHistory->insert(test30FpsLayer, startTime + (i * 33333333), false /*isHDR*/);
+ for (int i = 0; i < RELEVANT_FRAME_THRESHOLD; i++) {
+ mLayerHistory->insert(test30FpsLayer, startTime + (i * THIRTY_FPS_INTERVAL),
+ false /*isHDR*/);
}
EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
- for (int i = 10; i < 30; i++) {
- mLayerHistory->insert(test30FpsLayer, startTime + (i * 33333333), false /*isHDR*/);
+ for (int i = 10; i < RELEVANT_FRAME_THRESHOLD; i++) {
+ mLayerHistory->insert(test30FpsLayer, startTime + (i * THIRTY_FPS_INTERVAL),
+ false /*isHDR*/);
}
EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
@@ -122,8 +128,10 @@
EXPECT_FLOAT_EQ(MAX_REFRESH_RATE, mLayerHistory->getDesiredRefreshRateAndHDR().first);
// After 1200 ms frames become obsolete.
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
- // Insert the 31st frame.
- mLayerHistory->insert(test30FpsLayer, startTime + (30 * 33333333), false /*isHDR*/);
+
+ mLayerHistory->insert(test30FpsLayer,
+ startTime + (RELEVANT_FRAME_THRESHOLD * THIRTY_FPS_INTERVAL),
+ false /*isHDR*/);
EXPECT_FLOAT_EQ(30.f, mLayerHistory->getDesiredRefreshRateAndHDR().first);
}
diff --git a/vulkan/libvulkan/swapchain.cpp b/vulkan/libvulkan/swapchain.cpp
index e5ac2de..d60eaa7 100644
--- a/vulkan/libvulkan/swapchain.cpp
+++ b/vulkan/libvulkan/swapchain.cpp
@@ -370,9 +370,6 @@
nullptr, //&first_composition_start_time,
nullptr, //&last_composition_start_time,
nullptr, //&composition_finish_time,
- // TODO(ianelliott): Maybe ask if this one is
- // supported, at startup time (since it may not be
- // supported):
&actual_present_time,
nullptr, //&dequeue_ready_time,
nullptr /*&reads_done_time*/);
@@ -399,7 +396,6 @@
return num_ready;
}
-// TODO(ianelliott): DEAL WITH RETURN VALUE (e.g. VK_INCOMPLETE)!!!
void copy_ready_timings(Swapchain& swapchain,
uint32_t* count,
VkPastPresentationTimingGOOGLE* timings) {
@@ -1773,6 +1769,10 @@
ATRACE_CALL();
Swapchain& swapchain = *SwapchainFromHandle(swapchain_handle);
+ if (swapchain.surface.swapchain_handle != swapchain_handle) {
+ return VK_ERROR_OUT_OF_DATE_KHR;
+ }
+
ANativeWindow* window = swapchain.surface.window.get();
VkResult result = VK_SUCCESS;
@@ -1783,8 +1783,15 @@
}
if (timings) {
- // TODO(ianelliott): plumb return value (e.g. VK_INCOMPLETE)
+ // Get the latest ready timing count before copying, since the copied
+ // timing info will be erased in copy_ready_timings function.
+ uint32_t n = get_num_ready_timings(swapchain);
copy_ready_timings(swapchain, count, timings);
+ // Check the *count here against the recorded ready timing count, since
+ // *count can be overwritten per spec describes.
+ if (*count < n) {
+ result = VK_INCOMPLETE;
+ }
} else {
*count = get_num_ready_timings(swapchain);
}