SampleDisplayer.h revision 9970a2344333f2c19d9126cfec4f833f50ff2f22
1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef SIMPLE_PERF_SAMPLE_DISPLAYER_H_ 18#define SIMPLE_PERF_SAMPLE_DISPLAYER_H_ 19 20#include <inttypes.h> 21 22#include <functional> 23#include <string> 24 25#include <android-base/logging.h> 26#include <android-base/stringprintf.h> 27 28// The display functions below are used to show items in a sample. 29 30template <typename EntryT, typename InfoT> 31std::string DisplayAccumulatedOverhead(const EntryT* sample, 32 const InfoT* info) { 33 uint64_t period = sample->period + sample->accumulated_period; 34 uint64_t total_period = info->total_period; 35 double percentage = (total_period != 0) ? 100.0 * period / total_period : 0.0; 36 return android::base::StringPrintf("%.2f%%", percentage); 37} 38 39template <typename EntryT, typename InfoT> 40std::string DisplaySelfOverhead(const EntryT* sample, const InfoT* info) { 41 uint64_t period = sample->period; 42 uint64_t total_period = info->total_period; 43 double percentage = (total_period != 0) ? 100.0 * period / total_period : 0.0; 44 return android::base::StringPrintf("%.2f%%", percentage); 45} 46 47#define BUILD_DISPLAY_UINT64_FUNCTION(function_name, display_part) \ 48 template <typename EntryT> \ 49 std::string function_name(const EntryT* sample) { \ 50 return android::base::StringPrintf("%" PRIu64, sample->display_part); \ 51 } 52 53#define BUILD_DISPLAY_HEX64_FUNCTION(function_name, display_part) \ 54 template <typename EntryT> \ 55 std::string function_name(const EntryT* sample) { \ 56 return android::base::StringPrintf("0x%" PRIx64, sample->display_part); \ 57 } 58 59BUILD_DISPLAY_UINT64_FUNCTION(DisplaySampleCount, sample_count); 60 61template <typename EntryT> 62std::string DisplayPid(const EntryT* sample) { 63 return android::base::StringPrintf("%d", sample->thread->pid); 64} 65 66template <typename EntryT> 67std::string DisplayTid(const EntryT* sample) { 68 return android::base::StringPrintf("%d", sample->thread->tid); 69} 70 71template <typename EntryT> 72std::string DisplayComm(const EntryT* sample) { 73 return sample->thread_comm; 74} 75 76template <typename EntryT> 77std::string DisplayDso(const EntryT* sample) { 78 return sample->map->dso->Path(); 79} 80 81template <typename EntryT> 82std::string DisplaySymbol(const EntryT* sample) { 83 return sample->symbol->DemangledName(); 84} 85 86template <typename EntryT> 87std::string DisplayDsoFrom(const EntryT* sample) { 88 return sample->branch_from.map->dso->Path(); 89} 90 91template <typename EntryT> 92std::string DisplaySymbolFrom(const EntryT* sample) { 93 return sample->branch_from.symbol->DemangledName(); 94} 95 96template <typename SampleT, typename CallChainNodeT> 97class CallgraphDisplayer { 98 public: 99 virtual ~CallgraphDisplayer() {} 100 101 void operator()(FILE* fp, const SampleT* sample) { 102 std::string prefix = " "; 103 fprintf(fp, "%s|\n", prefix.c_str()); 104 fprintf(fp, "%s-- %s\n", prefix.c_str(), PrintSampleName(sample).c_str()); 105 prefix.append(3, ' '); 106 for (size_t i = 0; i < sample->callchain.children.size(); ++i) { 107 DisplayCallGraphEntry(fp, 1, prefix, sample->callchain.children[i], 108 sample->callchain.children_period, 109 (i + 1 == sample->callchain.children.size())); 110 } 111 } 112 113 void DisplayCallGraphEntry(FILE* fp, size_t depth, std::string prefix, 114 const std::unique_ptr<CallChainNodeT>& node, 115 uint64_t parent_period, bool last) { 116 if (depth > 20) { 117 LOG(WARNING) << "truncated callgraph at depth " << depth; 118 return; 119 } 120 prefix += "|"; 121 fprintf(fp, "%s\n", prefix.c_str()); 122 if (last) { 123 prefix.back() = ' '; 124 } 125 std::string percentage_s = "-- "; 126 if (node->period + node->children_period != parent_period) { 127 double percentage = 128 100.0 * (node->period + node->children_period) / parent_period; 129 percentage_s = android::base::StringPrintf("--%.2f%%-- ", percentage); 130 } 131 fprintf(fp, "%s%s%s\n", prefix.c_str(), percentage_s.c_str(), 132 PrintSampleName(node->chain[0]).c_str()); 133 prefix.append(percentage_s.size(), ' '); 134 for (size_t i = 1; i < node->chain.size(); ++i) { 135 fprintf(fp, "%s%s\n", prefix.c_str(), 136 PrintSampleName(node->chain[i]).c_str()); 137 } 138 for (size_t i = 0; i < node->children.size(); ++i) { 139 DisplayCallGraphEntry(fp, depth + 1, prefix, node->children[i], 140 node->children_period, 141 (i + 1 == node->children.size())); 142 } 143 } 144 145 protected: 146 virtual std::string PrintSampleName(const SampleT* sample) { 147 return sample->symbol->DemangledName(); 148 } 149}; 150 151// SampleDisplayer is a class using a collections of display functions to show a 152// sample. 153 154template <typename EntryT, typename InfoT> 155class SampleDisplayer { 156 public: 157 typedef std::string (*display_sample_func_t)(const EntryT*); 158 typedef std::string (*display_sample_with_info_func_t)(const EntryT*, 159 const InfoT*); 160 using exclusive_display_sample_func_t = 161 std::function<void(FILE*, const EntryT*)>; 162 163 private: 164 struct Item { 165 std::string name; 166 size_t width; 167 display_sample_func_t func; 168 display_sample_with_info_func_t func_with_info; 169 }; 170 171 public: 172 void SetInfo(const InfoT* info) { info_ = info; } 173 174 void AddDisplayFunction(const std::string& name, display_sample_func_t func) { 175 Item item; 176 item.name = name; 177 item.width = name.size(); 178 item.func = func; 179 item.func_with_info = nullptr; 180 display_v_.push_back(item); 181 } 182 183 void AddDisplayFunction(const std::string& name, 184 display_sample_with_info_func_t func_with_info) { 185 Item item; 186 item.name = name; 187 item.width = name.size(); 188 item.func = nullptr; 189 item.func_with_info = func_with_info; 190 display_v_.push_back(item); 191 } 192 193 void AddExclusiveDisplayFunction(exclusive_display_sample_func_t func) { 194 exclusive_display_v_.push_back(func); 195 } 196 197 void AdjustWidth(const EntryT* sample) { 198 for (auto& item : display_v_) { 199 std::string data = (item.func != nullptr) 200 ? item.func(sample) 201 : item.func_with_info(sample, info_); 202 item.width = std::max(item.width, data.size()); 203 } 204 } 205 206 void PrintNames(FILE* fp) { 207 for (size_t i = 0; i < display_v_.size(); ++i) { 208 auto& item = display_v_[i]; 209 if (i != display_v_.size() - 1) { 210 fprintf(fp, "%-*s ", static_cast<int>(item.width), item.name.c_str()); 211 } else { 212 fprintf(fp, "%s\n", item.name.c_str()); 213 } 214 } 215 } 216 217 void PrintSample(FILE* fp, const EntryT* sample) { 218 for (size_t i = 0; i < display_v_.size(); ++i) { 219 auto& item = display_v_[i]; 220 std::string data = (item.func != nullptr) 221 ? item.func(sample) 222 : item.func_with_info(sample, info_); 223 if (i != display_v_.size() - 1) { 224 fprintf(fp, "%-*s ", static_cast<int>(item.width), data.c_str()); 225 } else { 226 fprintf(fp, "%s\n", data.c_str()); 227 } 228 } 229 for (auto& func : exclusive_display_v_) { 230 func(fp, sample); 231 } 232 } 233 234 private: 235 const InfoT* info_; 236 std::vector<Item> display_v_; 237 std::vector<exclusive_display_sample_func_t> exclusive_display_v_; 238}; 239 240#endif // SIMPLE_PERF_SAMPLE_DISPLAYER_H_ 241