#include <android-base/test_utils.h>
#include <android-base/thread_annotations.h>
#include <gtest/gtest.h>
+#include <zlib.h>
#include "config.h"
#include "configreader.h"
+#include "map_utils.h"
#include "perfprofdcore.h"
+#include "perfprofd_cmdline.h"
#include "quipper_helper.h"
#include "symbolizer.h"
using namespace android::perfprofd::quipper;
+static_assert(android::base::kEnableDChecks, "Expected DCHECKs to be enabled");
+
//
// Set to argv[0] on startup
//
}
private:
- void TestLogFunction(LogId log_id ATTRIBUTE_UNUSED,
+ void TestLogFunction(LogId log_id,
LogSeverity severity,
const char* tag,
- const char* file ATTRIBUTE_UNUSED,
- unsigned int line ATTRIBUTE_UNUSED,
+ const char* file,
+ unsigned int line,
const char* message) {
std::unique_lock<std::mutex> ul(lock_);
constexpr char log_characters[] = "VDIWEFF";
char severity_char = log_characters[severity];
test_log_messages_.push_back(android::base::StringPrintf("%c: %s", severity_char, message));
+
+ if (severity >= LogSeverity::FATAL_WITHOUT_ABORT) {
+ android::base::StderrLogger(log_id, severity, tag, file, line, message);
+ }
}
private:
}
static void readEncodedProfile(const std::string& dest_dir,
- const char *testpoint,
+ bool compressed,
android::perfprofd::PerfprofdRecord& encodedProfile)
{
struct stat statb;
ASSERT_EQ(1, items_read);
fclose(ifp);
+ // uncompress
+ if (compressed && !encoded.empty()) {
+ z_stream stream;
+ stream.zalloc = Z_NULL;
+ stream.zfree = Z_NULL;
+ stream.opaque = Z_NULL;
+
+ {
+ constexpr int kWindowBits = 15;
+ constexpr int kGzipEncoding = 16;
+ int init_result = inflateInit2(&stream, kWindowBits | kGzipEncoding);
+ if (init_result != Z_OK) {
+ LOG(ERROR) << "Could not initialize libz stream " << init_result;
+ return;
+ }
+ }
+
+ std::string buf;
+ buf.reserve(2 * encoded.size());
+ stream.avail_in = encoded.size();
+ stream.next_in = reinterpret_cast<Bytef*>(const_cast<char*>(encoded.data()));
+
+ int result;
+ do {
+ uint8_t chunk[1024];
+ stream.next_out = static_cast<Bytef*>(chunk);
+ stream.avail_out = arraysize(chunk);
+
+ result = inflate(&stream, 0);
+ const size_t amount = arraysize(chunk) - stream.avail_out;
+ if (amount > 0) {
+ if (buf.capacity() - buf.size() < amount) {
+ buf.reserve(buf.capacity() + 64u * 1024u);
+ CHECK_LE(amount, buf.capacity() - buf.size());
+ }
+ size_t index = buf.size();
+ buf.resize(buf.size() + amount);
+ memcpy(reinterpret_cast<uint8_t*>(const_cast<char*>(buf.data())) + index, chunk, amount);
+ }
+ } while (result == Z_OK);
+ inflateEnd(&stream);
+ if (result != Z_STREAM_END) {
+ LOG(ERROR) << "Finished with not-Z_STREAM_END " << result;
+ return;
+ }
+ encoded = buf;
+ }
+
// decode
encodedProfile.ParseFromString(encoded);
}
}
-TEST_F(PerfProfdTest, BasicRunWithCannedPerf)
+struct BasicRunWithCannedPerf : PerfProfdTest {
+ void VerifyBasicCannedProfile(const android::perfprofd::PerfprofdRecord& encodedProfile) {
+ ASSERT_TRUE(encodedProfile.has_perf_data()) << test_logger.JoinTestLog(" ");
+ const quipper::PerfDataProto& perf_data = encodedProfile.perf_data();
+
+ // Expect 21108 events.
+ EXPECT_EQ(21108, perf_data.events_size()) << CreateStats(perf_data);
+
+ EXPECT_EQ(48, CountMmapEvents(perf_data)) << CreateStats(perf_data);
+ EXPECT_EQ(19986, CountSampleEvents(perf_data)) << CreateStats(perf_data);
+ EXPECT_EQ(1033, CountCommEvents(perf_data)) << CreateStats(perf_data);
+ EXPECT_EQ(15, CountForkEvents(perf_data)) << CreateStats(perf_data);
+ EXPECT_EQ(26, CountExitEvents(perf_data)) << CreateStats(perf_data);
+
+ if (HasNonfatalFailure()) {
+ FAIL();
+ }
+
+ {
+ MmapEventIterator mmap(perf_data);
+ constexpr std::pair<const char*, uint64_t> kMmapEvents[] = {
+ std::make_pair("[kernel.kallsyms]_text", 0),
+ std::make_pair("/system/lib/libc.so", 3067412480u),
+ std::make_pair("/system/vendor/lib/libdsutils.so", 3069911040u),
+ std::make_pair("/system/lib/libc.so", 3067191296u),
+ std::make_pair("/system/lib/libc++.so", 3069210624u),
+ std::make_pair("/data/dalvik-cache/arm/system@framework@boot.oat", 1900048384u),
+ std::make_pair("/system/lib/libjavacore.so", 2957135872u),
+ std::make_pair("/system/vendor/lib/libqmi_encdec.so", 3006644224u),
+ std::make_pair("/data/dalvik-cache/arm/system@framework@wifi-service.jar@classes.dex",
+ 3010351104u),
+ std::make_pair("/system/lib/libart.so", 3024150528u),
+ std::make_pair("/system/lib/libz.so", 3056410624u),
+ std::make_pair("/system/lib/libicui18n.so", 3057610752u),
+ };
+ for (auto& pair : kMmapEvents) {
+ EXPECT_STREQ(pair.first, mmap->mmap_event().filename().c_str());
+ EXPECT_EQ(pair.second, mmap->mmap_event().start()) << pair.first;
+ ++mmap;
+ }
+ }
+
+ {
+ CommEventIterator comm(perf_data);
+ constexpr const char* kCommEvents[] = {
+ "init", "kthreadd", "ksoftirqd/0", "kworker/u:0H", "migration/0", "khelper",
+ "netns", "modem_notifier", "smd_channel_clo", "smsm_cb_wq", "rpm-smd", "kworker/u:1H",
+ };
+ for (auto str : kCommEvents) {
+ EXPECT_STREQ(str, comm->comm_event().comm().c_str());
+ ++comm;
+ }
+ }
+
+ {
+ SampleEventIterator samples(perf_data);
+ constexpr const char* kSampleEvents[] = {
+ "pid=0 tid=0 ip=3222720196",
+ "pid=0 tid=0 ip=3222910876",
+ "pid=0 tid=0 ip=3222910876",
+ "pid=0 tid=0 ip=3222910876",
+ "pid=0 tid=0 ip=3222910876",
+ "pid=0 tid=0 ip=3222910876",
+ "pid=0 tid=0 ip=3222910876",
+ "pid=3 tid=3 ip=3231975108",
+ "pid=5926 tid=5926 ip=3231964952",
+ "pid=5926 tid=5926 ip=3225342428",
+ "pid=5926 tid=5926 ip=3223841448",
+ "pid=5926 tid=5926 ip=3069807920",
+ };
+ for (auto str : kSampleEvents) {
+ EXPECT_STREQ(str, FormatSampleEvent(samples->sample_event()).c_str());
+ ++samples;
+ }
+
+ // Skip some samples.
+ for (size_t i = 0; i != 5000; ++i) {
+ ++samples;
+ }
+ constexpr const char* kSampleEvents2[] = {
+ "pid=5938 tid=5938 ip=3069630992",
+ "pid=5938 tid=5938 ip=3069626616",
+ "pid=5938 tid=5938 ip=3069626636",
+ "pid=5938 tid=5938 ip=3069637212",
+ "pid=5938 tid=5938 ip=3069637208",
+ "pid=5938 tid=5938 ip=3069637252",
+ "pid=5938 tid=5938 ip=3069346040",
+ "pid=5938 tid=5938 ip=3069637128",
+ "pid=5938 tid=5938 ip=3069626616",
+ };
+ for (auto str : kSampleEvents2) {
+ EXPECT_STREQ(str, FormatSampleEvent(samples->sample_event()).c_str());
+ ++samples;
+ }
+
+ // Skip some samples.
+ for (size_t i = 0; i != 5000; ++i) {
+ ++samples;
+ }
+ constexpr const char* kSampleEvents3[] = {
+ "pid=5938 tid=5938 ip=3069912036",
+ "pid=5938 tid=5938 ip=3069637260",
+ "pid=5938 tid=5938 ip=3069631024",
+ "pid=5938 tid=5938 ip=3069346064",
+ "pid=5938 tid=5938 ip=3069637356",
+ "pid=5938 tid=5938 ip=3069637144",
+ "pid=5938 tid=5938 ip=3069912036",
+ "pid=5938 tid=5938 ip=3069912036",
+ "pid=5938 tid=5938 ip=3069631244",
+ };
+ for (auto str : kSampleEvents3) {
+ EXPECT_STREQ(str, FormatSampleEvent(samples->sample_event()).c_str());
+ ++samples;
+ }
+ }
+ }
+};
+
+TEST_F(BasicRunWithCannedPerf, Basic)
{
//
// Verify the portion of the daemon that reads and encodes
config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
config_reader.overrideUnsignedEntry("collect_charging_state", 0);
config_reader.overrideUnsignedEntry("collect_camera_active", 0);
+
+ // Disable compression.
+ config_reader.overrideUnsignedEntry("compress", 0);
+
PerfProfdRunner::LoggingConfig config;
config_reader.FillConfig(&config);
// Read and decode the resulting perf.data.encoded file
android::perfprofd::PerfprofdRecord encodedProfile;
- readEncodedProfile(dest_dir,
- "BasicRunWithCannedPerf",
- encodedProfile);
+ readEncodedProfile(dest_dir, false, encodedProfile);
- ASSERT_TRUE(encodedProfile.has_perf_data());
- const quipper::PerfDataProto& perf_data = encodedProfile.perf_data();
-
- // Expect 21108 events.
- EXPECT_EQ(21108, perf_data.events_size()) << CreateStats(perf_data);
+ VerifyBasicCannedProfile(encodedProfile);
+}
- EXPECT_EQ(48, CountMmapEvents(perf_data)) << CreateStats(perf_data);
- EXPECT_EQ(19986, CountSampleEvents(perf_data)) << CreateStats(perf_data);
- EXPECT_EQ(1033, CountCommEvents(perf_data)) << CreateStats(perf_data);
- EXPECT_EQ(15, CountForkEvents(perf_data)) << CreateStats(perf_data);
- EXPECT_EQ(26, CountExitEvents(perf_data)) << CreateStats(perf_data);
+TEST_F(BasicRunWithCannedPerf, Compressed)
+{
+ //
+ // Verify the portion of the daemon that reads and encodes
+ // perf.data files. Here we run the encoder on a canned perf.data
+ // file and verify that the resulting protobuf contains what
+ // we think it should contain.
+ //
+ std::string input_perf_data(test_dir);
+ input_perf_data += "/canned.perf.data";
- if (HasNonfatalFailure()) {
- FAIL();
- }
+ // Set up config to avoid these annotations (they are tested elsewhere)
+ ConfigReader config_reader;
+ config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
+ config_reader.overrideUnsignedEntry("collect_charging_state", 0);
+ config_reader.overrideUnsignedEntry("collect_camera_active", 0);
- {
- MmapEventIterator mmap(perf_data);
- constexpr std::pair<const char*, uint64_t> kMmapEvents[] = {
- std::make_pair("[kernel.kallsyms]_text", 0),
- std::make_pair("/system/lib/libc.so", 3067412480u),
- std::make_pair("/system/vendor/lib/libdsutils.so", 3069911040u),
- std::make_pair("/system/lib/libc.so", 3067191296u),
- std::make_pair("/system/lib/libc++.so", 3069210624u),
- std::make_pair("/data/dalvik-cache/arm/system@framework@boot.oat", 1900048384u),
- std::make_pair("/system/lib/libjavacore.so", 2957135872u),
- std::make_pair("/system/vendor/lib/libqmi_encdec.so", 3006644224u),
- std::make_pair("/data/dalvik-cache/arm/system@framework@wifi-service.jar@classes.dex",
- 3010351104u),
- std::make_pair("/system/lib/libart.so", 3024150528u),
- std::make_pair("/system/lib/libz.so", 3056410624u),
- std::make_pair("/system/lib/libicui18n.so", 3057610752u),
- };
- for (auto& pair : kMmapEvents) {
- EXPECT_STREQ(pair.first, mmap->mmap_event().filename().c_str());
- EXPECT_EQ(pair.second, mmap->mmap_event().start()) << pair.first;
- ++mmap;
- }
- }
+ // Enable compression.
+ config_reader.overrideUnsignedEntry("compress", 1);
- {
- CommEventIterator comm(perf_data);
- constexpr const char* kCommEvents[] = {
- "init", "kthreadd", "ksoftirqd/0", "kworker/u:0H", "migration/0", "khelper",
- "netns", "modem_notifier", "smd_channel_clo", "smsm_cb_wq", "rpm-smd", "kworker/u:1H",
- };
- for (auto str : kCommEvents) {
- EXPECT_STREQ(str, comm->comm_event().comm().c_str());
- ++comm;
- }
- }
+ PerfProfdRunner::LoggingConfig config;
+ config_reader.FillConfig(&config);
- {
- SampleEventIterator samples(perf_data);
- constexpr const char* kSampleEvents[] = {
- "pid=0 tid=0 ip=3222720196",
- "pid=0 tid=0 ip=3222910876",
- "pid=0 tid=0 ip=3222910876",
- "pid=0 tid=0 ip=3222910876",
- "pid=0 tid=0 ip=3222910876",
- "pid=0 tid=0 ip=3222910876",
- "pid=0 tid=0 ip=3222910876",
- "pid=3 tid=3 ip=3231975108",
- "pid=5926 tid=5926 ip=3231964952",
- "pid=5926 tid=5926 ip=3225342428",
- "pid=5926 tid=5926 ip=3223841448",
- "pid=5926 tid=5926 ip=3069807920",
- };
- for (auto str : kSampleEvents) {
- EXPECT_STREQ(str, FormatSampleEvent(samples->sample_event()).c_str());
- ++samples;
- }
+ // Kick off encoder and check return code
+ PROFILE_RESULT result =
+ encode_to_proto(input_perf_data, encoded_file_path(dest_dir, 0).c_str(), config, 0, nullptr);
+ ASSERT_EQ(OK_PROFILE_COLLECTION, result) << test_logger.JoinTestLog(" ");
- // Skip some samples.
- for (size_t i = 0; i != 5000; ++i) {
- ++samples;
- }
- constexpr const char* kSampleEvents2[] = {
- "pid=5938 tid=5938 ip=3069630992",
- "pid=5938 tid=5938 ip=3069626616",
- "pid=5938 tid=5938 ip=3069626636",
- "pid=5938 tid=5938 ip=3069637212",
- "pid=5938 tid=5938 ip=3069637208",
- "pid=5938 tid=5938 ip=3069637252",
- "pid=5938 tid=5938 ip=3069346040",
- "pid=5938 tid=5938 ip=3069637128",
- "pid=5938 tid=5938 ip=3069626616",
- };
- for (auto str : kSampleEvents2) {
- EXPECT_STREQ(str, FormatSampleEvent(samples->sample_event()).c_str());
- ++samples;
- }
+ // Read and decode the resulting perf.data.encoded file
+ android::perfprofd::PerfprofdRecord encodedProfile;
+ readEncodedProfile(dest_dir, true, encodedProfile);
- // Skip some samples.
- for (size_t i = 0; i != 5000; ++i) {
- ++samples;
- }
- constexpr const char* kSampleEvents3[] = {
- "pid=5938 tid=5938 ip=3069912036",
- "pid=5938 tid=5938 ip=3069637260",
- "pid=5938 tid=5938 ip=3069631024",
- "pid=5938 tid=5938 ip=3069346064",
- "pid=5938 tid=5938 ip=3069637356",
- "pid=5938 tid=5938 ip=3069637144",
- "pid=5938 tid=5938 ip=3069912036",
- "pid=5938 tid=5938 ip=3069912036",
- "pid=5938 tid=5938 ip=3069631244",
- };
- for (auto str : kSampleEvents3) {
- EXPECT_STREQ(str, FormatSampleEvent(samples->sample_event()).c_str());
- ++samples;
- }
- }
+ VerifyBasicCannedProfile(encodedProfile);
}
-TEST_F(PerfProfdTest, DISABLED_BasicRunWithCannedPerfWithSymbolizer)
+TEST_F(BasicRunWithCannedPerf, WithSymbolizer)
{
//
// Verify the portion of the daemon that reads and encodes
config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
config_reader.overrideUnsignedEntry("collect_charging_state", 0);
config_reader.overrideUnsignedEntry("collect_camera_active", 0);
+
+ // Disable compression.
+ config_reader.overrideUnsignedEntry("compress", 0);
+
PerfProfdRunner::LoggingConfig config;
config_reader.FillConfig(&config);
std::string Decode(const std::string& dso, uint64_t address) override {
return dso + "@" + std::to_string(address);
}
+ bool GetMinExecutableVAddr(const std::string& dso, uint64_t* addr) override {
+ *addr = 4096;
+ return true;
+ }
};
TestSymbolizer test_symbolizer;
PROFILE_RESULT result =
// Read and decode the resulting perf.data.encoded file
android::perfprofd::PerfprofdRecord encodedProfile;
- readEncodedProfile(dest_dir,
- "BasicRunWithCannedPerf",
- encodedProfile);
+ readEncodedProfile(dest_dir, false, encodedProfile);
- ASSERT_TRUE(encodedProfile.has_perf_data());
- const quipper::PerfDataProto& perf_data = encodedProfile.perf_data();
+ VerifyBasicCannedProfile(encodedProfile);
- // Expect 21108 events.
- EXPECT_EQ(21108, perf_data.events_size()) << CreateStats(perf_data);
+ auto find_symbol = [&](const std::string& filename)
+ -> const android::perfprofd::PerfprofdRecord_SymbolInfo* {
+ for (auto& symbol_info : encodedProfile.symbol_info()) {
+ if (symbol_info.filename() == filename) {
+ return &symbol_info;
+ }
+ }
+ return nullptr;
+ };
+ auto all_filenames = [&]() {
+ std::ostringstream oss;
+ for (auto& symbol_info : encodedProfile.symbol_info()) {
+ oss << " " << symbol_info.filename();
+ }
+ return oss.str();
+ };
- // TODO: Re-add symbolization.
+ EXPECT_TRUE(find_symbol("/data/app/com.google.android.apps.plus-1/lib/arm/libcronet.so")
+ != nullptr) << all_filenames() << test_logger.JoinTestLog("\n");
+ EXPECT_TRUE(find_symbol("/data/dalvik-cache/arm/system@framework@wifi-service.jar@classes.dex")
+ != nullptr) << all_filenames();
+ EXPECT_TRUE(find_symbol("/data/dalvik-cache/arm/data@app@com.google.android.gms-2@base.apk@"
+ "classes.dex")
+ != nullptr) << all_filenames();
+ EXPECT_TRUE(find_symbol("/data/dalvik-cache/arm/system@framework@boot.oat") != nullptr)
+ << all_filenames();
}
TEST_F(PerfProfdTest, CallchainRunWithCannedPerf)
config_reader.overrideUnsignedEntry("collect_cpu_utilization", 0);
config_reader.overrideUnsignedEntry("collect_charging_state", 0);
config_reader.overrideUnsignedEntry("collect_camera_active", 0);
+
+ // Disable compression.
+ config_reader.overrideUnsignedEntry("compress", 0);
+
PerfProfdRunner::LoggingConfig config;
config_reader.FillConfig(&config);
// Read and decode the resulting perf.data.encoded file
android::perfprofd::PerfprofdRecord encodedProfile;
- readEncodedProfile(dest_dir,
- "BasicRunWithCannedPerf",
- encodedProfile);
+ readEncodedProfile(dest_dir, false, encodedProfile);
ASSERT_TRUE(encodedProfile.has_perf_data());
// Avoid the symbolizer for spurious messages.
runner.addToConfig("use_elf_symbolizer=0");
+ // Disable compression.
+ runner.addToConfig("compress=0");
+
// Create semaphore file
runner.create_semaphore_file();
// Read and decode the resulting perf.data.encoded file
android::perfprofd::PerfprofdRecord encodedProfile;
- readEncodedProfile(dest_dir, "BasicRunWithLivePerf", encodedProfile);
+ readEncodedProfile(dest_dir, false, encodedProfile);
// Examine what we get back. Since it's a live profile, we can't
// really do much in terms of verifying the contents.
runner.addToConfig("sample_duration=2");
// Avoid the symbolizer for spurious messages.
runner.addToConfig("use_elf_symbolizer=0");
+
+ // Disable compression.
+ runner.addToConfig("compress=0");
+
runner.write_processed_file(1, 2);
// Create semaphore file
// Read and decode the resulting perf.data.encoded file
android::perfprofd::PerfprofdRecord encodedProfile;
- readEncodedProfile(dest_dir, "BasicRunWithLivePerf", encodedProfile);
+ readEncodedProfile(dest_dir, false, encodedProfile);
// Examine what we get back. Since it's a live profile, we can't
// really do much in terms of verifying the contents.
// Avoid the symbolizer for spurious messages.
runner.addToConfig("use_elf_symbolizer=0");
+ // Disable compression.
+ runner.addToConfig("compress=0");
+
// Create semaphore file
runner.create_semaphore_file();
// Read and decode the resulting perf.data.encoded file
android::perfprofd::PerfprofdRecord encodedProfile;
- readEncodedProfile(dest_dir, "CallChainRunWithLivePerf", encodedProfile);
+ readEncodedProfile(dest_dir, false, encodedProfile);
// Examine what we get back. Since it's a live profile, we can't
// really do much in terms of verifying the contents.
#endif
+class RangeMapTest : public testing::Test {
+};
+
+TEST_F(RangeMapTest, TestRangeMap) {
+ using namespace android::perfprofd;
+
+ RangeMap<std::string, uint64_t> map;
+ auto print = [&]() {
+ std::ostringstream oss;
+ for (auto& aggr_sym : map) {
+ oss << aggr_sym.first << "#" << aggr_sym.second.symbol;
+ oss << "[";
+ for (auto& x : aggr_sym.second.offsets) {
+ oss << x << ",";
+ }
+ oss << "]";
+ }
+ return oss.str();
+ };
+
+ EXPECT_STREQ("", print().c_str());
+
+ map.Insert("a", 10);
+ EXPECT_STREQ("10#a[10,]", print().c_str());
+ map.Insert("a", 100);
+ EXPECT_STREQ("10#a[10,100,]", print().c_str());
+ map.Insert("a", 1);
+ EXPECT_STREQ("1#a[1,10,100,]", print().c_str());
+ map.Insert("a", 1);
+ EXPECT_STREQ("1#a[1,10,100,]", print().c_str());
+ map.Insert("a", 2);
+ EXPECT_STREQ("1#a[1,2,10,100,]", print().c_str());
+
+ map.Insert("b", 200);
+ EXPECT_STREQ("1#a[1,2,10,100,]200#b[200,]", print().c_str());
+ map.Insert("b", 199);
+ EXPECT_STREQ("1#a[1,2,10,100,]199#b[199,200,]", print().c_str());
+
+ map.Insert("c", 50);
+ EXPECT_STREQ("1#a[1,2,10,]50#c[50,]100#a[100,]199#b[199,200,]", print().c_str());
+}
+
int main(int argc, char **argv) {
// Always log to cerr, so that device failures are visible.
android::base::SetLogger(android::base::StderrLogger);