Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 ART_RUNTIME_PROFILER_H_ |
| 18 | #define ART_RUNTIME_PROFILER_H_ |
| 19 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 21 | #include <ostream> |
| 22 | #include <set> |
| 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 26 | #include "barrier.h" |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 27 | #include "base/macros.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 28 | #include "base/mutex.h" |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 29 | #include "globals.h" |
| 30 | #include "instrumentation.h" |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 31 | #include "profiler_options.h" |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 32 | #include "os.h" |
| 33 | #include "safe_map.h" |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 34 | #include "method_reference.h" |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | namespace mirror { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 39 | class Class; |
| 40 | } // namespace mirror |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 41 | class ArtMethod; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 42 | class Thread; |
| 43 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 44 | typedef std::pair<ArtMethod*, uint32_t> InstructionLocation; |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 45 | |
| 46 | // This class stores the sampled bounded stacks in a trie structure. A path of the trie represents |
| 47 | // a particular context with the method on top of the stack being a leaf or an internal node of the |
| 48 | // trie rather than the root. |
| 49 | class StackTrieNode { |
| 50 | public: |
| 51 | StackTrieNode(MethodReference method, uint32_t dex_pc, uint32_t method_size, |
| 52 | StackTrieNode* parent) : |
| 53 | parent_(parent), method_(method), dex_pc_(dex_pc), |
| 54 | count_(0), method_size_(method_size) { |
| 55 | } |
| 56 | StackTrieNode() : parent_(nullptr), method_(nullptr, 0), |
| 57 | dex_pc_(0), count_(0), method_size_(0) { |
| 58 | } |
| 59 | StackTrieNode* GetParent() { return parent_; } |
| 60 | MethodReference GetMethod() { return method_; } |
| 61 | uint32_t GetCount() { return count_; } |
| 62 | uint32_t GetDexPC() { return dex_pc_; } |
| 63 | uint32_t GetMethodSize() { return method_size_; } |
| 64 | void AppendChild(StackTrieNode* child) { children_.insert(child); } |
| 65 | StackTrieNode* FindChild(MethodReference method, uint32_t dex_pc); |
| 66 | void DeleteChildren(); |
| 67 | void IncreaseCount() { ++count_; } |
| 68 | |
| 69 | private: |
| 70 | // Comparator for stack trie node. |
| 71 | struct StackTrieNodeComparator { |
| 72 | bool operator()(StackTrieNode* node1, StackTrieNode* node2) const { |
| 73 | MethodReference mr1 = node1->GetMethod(); |
| 74 | MethodReference mr2 = node2->GetMethod(); |
| 75 | if (mr1.dex_file == mr2.dex_file) { |
| 76 | if (mr1.dex_method_index == mr2.dex_method_index) { |
| 77 | return node1->GetDexPC() < node2->GetDexPC(); |
| 78 | } else { |
| 79 | return mr1.dex_method_index < mr2.dex_method_index; |
| 80 | } |
| 81 | } else { |
| 82 | return mr1.dex_file < mr2.dex_file; |
| 83 | } |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | std::set<StackTrieNode*, StackTrieNodeComparator> children_; |
| 88 | StackTrieNode* parent_; |
| 89 | MethodReference method_; |
| 90 | uint32_t dex_pc_; |
| 91 | uint32_t count_; |
| 92 | uint32_t method_size_; |
| 93 | }; |
| 94 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 95 | // |
| 96 | // This class holds all the results for all runs of the profiler. It also |
| 97 | // counts the number of null methods (where we can't determine the method) and |
| 98 | // the number of methods in the boot path (where we have already compiled the method). |
| 99 | // |
| 100 | // This object is an internal profiler object and uses the same locking as the profiler |
| 101 | // itself. |
| 102 | class ProfileSampleResults { |
| 103 | public: |
| 104 | explicit ProfileSampleResults(Mutex& lock); |
| 105 | ~ProfileSampleResults(); |
| 106 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 107 | void Put(ArtMethod* method) REQUIRES(!lock_); |
| 108 | void PutStack(const std::vector<InstructionLocation>& stack_dump) REQUIRES(!lock_); |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 109 | uint32_t Write(std::ostream &os, ProfileDataType type); |
| 110 | void ReadPrevious(int fd, ProfileDataType type); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 111 | void Clear(); |
| 112 | uint32_t GetNumSamples() { return num_samples_; } |
| 113 | void NullMethod() { ++num_null_methods_; } |
| 114 | void BootMethod() { ++num_boot_methods_; } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 115 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 116 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 117 | uint32_t Hash(ArtMethod* method); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 118 | static constexpr int kHashSize = 17; |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 119 | Mutex& lock_; // Reference to the main profiler lock - we don't need two of them. |
| 120 | uint32_t num_samples_; // Total number of samples taken. |
| 121 | uint32_t num_null_methods_; // Number of samples where can don't know the method. |
| 122 | uint32_t num_boot_methods_; // Number of samples in the boot path. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 123 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 124 | typedef std::map<ArtMethod*, uint32_t> Map; // Map of method vs its count. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 125 | Map *table[kHashSize]; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 126 | |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 127 | typedef std::set<StackTrieNode*> TrieNodeSet; |
| 128 | // Map of method hit by profiler vs the set of stack trie nodes for this method. |
| 129 | typedef std::map<MethodReference, TrieNodeSet*, MethodReferenceComparator> MethodContextMap; |
| 130 | MethodContextMap *method_context_table; |
| 131 | StackTrieNode* stack_trie_root_; // Root of the trie that stores sampled stack information. |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 132 | |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 133 | // Map from <pc, context> to counts. |
| 134 | typedef std::map<std::pair<uint32_t, std::string>, uint32_t> PreviousContextMap; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 135 | struct PreviousValue { |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 136 | PreviousValue() : count_(0), method_size_(0), context_map_(nullptr) {} |
| 137 | PreviousValue(uint32_t count, uint32_t method_size, PreviousContextMap* context_map) |
| 138 | : count_(count), method_size_(method_size), context_map_(context_map) {} |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 139 | uint32_t count_; |
| 140 | uint32_t method_size_; |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 141 | PreviousContextMap* context_map_; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | typedef std::map<std::string, PreviousValue> PreviousProfile; |
| 145 | PreviousProfile previous_; |
| 146 | uint32_t previous_num_samples_; |
| 147 | uint32_t previous_num_null_methods_; // Number of samples where can don't know the method. |
| 148 | uint32_t previous_num_boot_methods_; // Number of samples in the boot path. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | // |
| 152 | // The BackgroundMethodSamplingProfiler runs in a thread. Most of the time it is sleeping but |
| 153 | // occasionally wakes up and counts the number of times a method is called. Each time |
| 154 | // it ticks, it looks at the current method and records it in the ProfileSampleResults |
| 155 | // table. |
| 156 | // |
| 157 | // The timing is controlled by a number of variables: |
| 158 | // 1. Period: the time between sampling runs. |
| 159 | // 2. Interval: the time between each sample in a run. |
| 160 | // 3. Duration: the duration of a run. |
| 161 | // |
| 162 | // So the profiler thread is sleeping for the 'period' time. It wakes up and runs for the |
| 163 | // 'duration'. The run consists of a series of samples, each of which is 'interval' microseconds |
| 164 | // apart. At the end of a run, it writes the results table to a file and goes back to sleep. |
| 165 | |
| 166 | class BackgroundMethodSamplingProfiler { |
| 167 | public: |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 168 | // Start a profile thread with the user-supplied arguments. |
| 169 | // Returns true if the profile was started or if it was already running. Returns false otherwise. |
| 170 | static bool Start(const std::string& output_filename, const ProfilerOptions& options) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 171 | REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_, |
| 172 | !Locks::profiler_lock_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 173 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 174 | // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock. |
| 175 | static void Stop() REQUIRES(!Locks::profiler_lock_, !wait_lock_, !Locks::profiler_lock_) |
| 176 | NO_THREAD_SAFETY_ANALYSIS; |
| 177 | // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock. |
| 178 | static void Shutdown() REQUIRES(!Locks::profiler_lock_) NO_THREAD_SAFETY_ANALYSIS; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 179 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 180 | void RecordMethod(ArtMethod *method) SHARED_REQUIRES(Locks::mutator_lock_); |
| 181 | void RecordStack(const std::vector<InstructionLocation>& stack) |
| 182 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 183 | bool ProcessMethod(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 184 | const ProfilerOptions& GetProfilerOptions() const { return options_; } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 185 | |
| 186 | Barrier& GetBarrier() { |
| 187 | return *profiler_barrier_; |
| 188 | } |
| 189 | |
| 190 | private: |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 191 | explicit BackgroundMethodSamplingProfiler( |
| 192 | const std::string& output_filename, const ProfilerOptions& options); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 193 | |
| 194 | // The sampling interval in microseconds is passed as an argument. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 195 | // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock. |
| 196 | static void* RunProfilerThread(void* arg) REQUIRES(!Locks::profiler_lock_) |
| 197 | NO_THREAD_SAFETY_ANALYSIS; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 198 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 199 | uint32_t WriteProfile() SHARED_REQUIRES(Locks::mutator_lock_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 200 | |
| 201 | void CleanProfile(); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 202 | uint32_t DumpProfile(std::ostream& os) SHARED_REQUIRES(Locks::mutator_lock_); |
| 203 | static bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 204 | |
| 205 | static BackgroundMethodSamplingProfiler* profiler_ GUARDED_BY(Locks::profiler_lock_); |
| 206 | |
| 207 | // We need to shut the sample thread down at exit. Setting this to true will do that. |
| 208 | static volatile bool shutting_down_ GUARDED_BY(Locks::profiler_lock_); |
| 209 | |
| 210 | // Sampling thread, non-zero when sampling. |
| 211 | static pthread_t profiler_pthread_; |
| 212 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 213 | // Some measure of the number of samples that are significant. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 214 | static constexpr uint32_t kSignificantSamples = 10; |
| 215 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 216 | // The name of the file where profile data will be written. |
| 217 | std::string output_filename_; |
| 218 | // The options used to start the profiler. |
| 219 | const ProfilerOptions& options_; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 220 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 221 | |
| 222 | // Profile condition support. |
| 223 | Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 224 | ConditionVariable period_condition_ GUARDED_BY(wait_lock_); |
| 225 | |
| 226 | ProfileSampleResults profile_table_; |
| 227 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 228 | std::unique_ptr<Barrier> profiler_barrier_; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 229 | |
| 230 | // Set of methods to be filtered out. This will probably be rare because |
| 231 | // most of the methods we want to be filtered reside in the boot path and |
| 232 | // are automatically filtered. |
| 233 | typedef std::set<std::string> FilteredMethods; |
| 234 | FilteredMethods filtered_methods_; |
| 235 | |
| 236 | DISALLOW_COPY_AND_ASSIGN(BackgroundMethodSamplingProfiler); |
| 237 | }; |
| 238 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 239 | // |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 240 | // Contains profile data generated from previous runs of the program and stored |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 241 | // in a file. It is used to determine whether to compile a particular method or not. |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 242 | class ProfileFile { |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 243 | public: |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 244 | class ProfileData { |
| 245 | public: |
Sebastien Hertz | aa50d3a | 2015-08-25 15:25:41 +0200 | [diff] [blame] | 246 | ProfileData() : count_(0), method_size_(0), used_percent_(0), top_k_used_percentage_(0) {} |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 247 | ProfileData(const std::string& method_name, uint32_t count, uint32_t method_size, |
| 248 | double used_percent, double top_k_used_percentage) : |
| 249 | method_name_(method_name), count_(count), method_size_(method_size), |
| 250 | used_percent_(used_percent), top_k_used_percentage_(top_k_used_percentage) { |
| 251 | // TODO: currently method_size_ is unused |
| 252 | UNUSED(method_size_); |
| 253 | } |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 254 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 255 | double GetUsedPercent() const { return used_percent_; } |
| 256 | uint32_t GetCount() const { return count_; } |
| 257 | double GetTopKUsedPercentage() const { return top_k_used_percentage_; } |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 258 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 259 | private: |
| 260 | std::string method_name_; // Method name. |
| 261 | uint32_t count_; // Number of times it has been called. |
| 262 | uint32_t method_size_; // Size of the method on dex instructions. |
| 263 | double used_percent_; // Percentage of how many times this method was called. |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 264 | double top_k_used_percentage_; // The percentage of the group that comprise K% of the total |
| 265 | // used methods this methods belongs to. |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 266 | }; |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 267 | |
| 268 | public: |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 269 | // Loads profile data from the given file. The new data are merged with any existing data. |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 270 | // Returns true if the file was loaded successfully and false otherwise. |
| 271 | bool LoadFile(const std::string& filename); |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 272 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 273 | // Computes the group that comprise top_k_percentage of the total used methods. |
| 274 | bool GetTopKSamples(std::set<std::string>& top_k_methods, double top_k_percentage); |
| 275 | |
| 276 | // If the given method has an entry in the profile table it updates the data |
| 277 | // and returns true. Otherwise returns false and leaves the data unchanged. |
| 278 | bool GetProfileData(ProfileData* data, const std::string& method_name); |
| 279 | |
| 280 | private: |
| 281 | // Profile data is stored in a map, indexed by the full method name. |
| 282 | typedef std::map<std::string, ProfileData> ProfileMap; |
| 283 | ProfileMap profile_map_; |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 284 | }; |
| 285 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 286 | } // namespace art |
| 287 | |
| 288 | #endif // ART_RUNTIME_PROFILER_H_ |