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" |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
| 37 | namespace mirror { |
| 38 | class ArtMethod; |
| 39 | class Class; |
| 40 | } // namespace mirror |
| 41 | class Thread; |
| 42 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 43 | // |
| 44 | // This class holds all the results for all runs of the profiler. It also |
| 45 | // counts the number of null methods (where we can't determine the method) and |
| 46 | // the number of methods in the boot path (where we have already compiled the method). |
| 47 | // |
| 48 | // This object is an internal profiler object and uses the same locking as the profiler |
| 49 | // itself. |
| 50 | class ProfileSampleResults { |
| 51 | public: |
| 52 | explicit ProfileSampleResults(Mutex& lock); |
| 53 | ~ProfileSampleResults(); |
| 54 | |
| 55 | void Put(mirror::ArtMethod* method); |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame^] | 56 | void PutDexPC(mirror::ArtMethod* method, uint32_t pc); |
| 57 | uint32_t Write(std::ostream &os, ProfileDataType type); |
| 58 | void ReadPrevious(int fd, ProfileDataType type); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 59 | void Clear(); |
| 60 | uint32_t GetNumSamples() { return num_samples_; } |
| 61 | void NullMethod() { ++num_null_methods_; } |
| 62 | void BootMethod() { ++num_boot_methods_; } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 63 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 64 | private: |
| 65 | uint32_t Hash(mirror::ArtMethod* method); |
| 66 | static constexpr int kHashSize = 17; |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 67 | Mutex& lock_; // Reference to the main profiler lock - we don't need two of them. |
| 68 | uint32_t num_samples_; // Total number of samples taken. |
| 69 | uint32_t num_null_methods_; // Number of samples where can don't know the method. |
| 70 | uint32_t num_boot_methods_; // Number of samples in the boot path. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 71 | |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame^] | 72 | typedef std::map<mirror::ArtMethod*, uint32_t> Map; // Map of method vs its count. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 73 | Map *table[kHashSize]; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 74 | |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame^] | 75 | typedef std::map<uint32_t, uint32_t> DexPCCountMap; // Map of dex pc vs its count |
| 76 | // Map of method vs dex pc counts in the method. |
| 77 | typedef std::map<mirror::ArtMethod*, DexPCCountMap*> MethodDexPCMap; |
| 78 | MethodDexPCMap *dex_table[kHashSize]; |
| 79 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 80 | struct PreviousValue { |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame^] | 81 | PreviousValue() : count_(0), method_size_(0), dex_pc_map_(nullptr) {} |
| 82 | PreviousValue(uint32_t count, uint32_t method_size, DexPCCountMap* dex_pc_map) |
| 83 | : count_(count), method_size_(method_size), dex_pc_map_(dex_pc_map) {} |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 84 | uint32_t count_; |
| 85 | uint32_t method_size_; |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame^] | 86 | DexPCCountMap* dex_pc_map_; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | typedef std::map<std::string, PreviousValue> PreviousProfile; |
| 90 | PreviousProfile previous_; |
| 91 | uint32_t previous_num_samples_; |
| 92 | uint32_t previous_num_null_methods_; // Number of samples where can don't know the method. |
| 93 | 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] | 94 | }; |
| 95 | |
| 96 | // |
| 97 | // The BackgroundMethodSamplingProfiler runs in a thread. Most of the time it is sleeping but |
| 98 | // occasionally wakes up and counts the number of times a method is called. Each time |
| 99 | // it ticks, it looks at the current method and records it in the ProfileSampleResults |
| 100 | // table. |
| 101 | // |
| 102 | // The timing is controlled by a number of variables: |
| 103 | // 1. Period: the time between sampling runs. |
| 104 | // 2. Interval: the time between each sample in a run. |
| 105 | // 3. Duration: the duration of a run. |
| 106 | // |
| 107 | // So the profiler thread is sleeping for the 'period' time. It wakes up and runs for the |
| 108 | // 'duration'. The run consists of a series of samples, each of which is 'interval' microseconds |
| 109 | // apart. At the end of a run, it writes the results table to a file and goes back to sleep. |
| 110 | |
| 111 | class BackgroundMethodSamplingProfiler { |
| 112 | public: |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 113 | // Start a profile thread with the user-supplied arguments. |
| 114 | // Returns true if the profile was started or if it was already running. Returns false otherwise. |
| 115 | static bool Start(const std::string& output_filename, const ProfilerOptions& options) |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 116 | LOCKS_EXCLUDED(Locks::mutator_lock_, |
| 117 | Locks::thread_list_lock_, |
| 118 | Locks::thread_suspend_count_lock_, |
| 119 | Locks::profiler_lock_); |
| 120 | |
| 121 | static void Stop() LOCKS_EXCLUDED(Locks::profiler_lock_, wait_lock_); |
| 122 | static void Shutdown() LOCKS_EXCLUDED(Locks::profiler_lock_); |
| 123 | |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame^] | 124 | void RecordMethod(mirror::ArtMethod *method, uint32_t pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 125 | |
| 126 | Barrier& GetBarrier() { |
| 127 | return *profiler_barrier_; |
| 128 | } |
| 129 | |
| 130 | private: |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 131 | explicit BackgroundMethodSamplingProfiler( |
| 132 | const std::string& output_filename, const ProfilerOptions& options); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 133 | |
| 134 | // The sampling interval in microseconds is passed as an argument. |
| 135 | static void* RunProfilerThread(void* arg) LOCKS_EXCLUDED(Locks::profiler_lock_); |
| 136 | |
| 137 | uint32_t WriteProfile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 138 | |
| 139 | void CleanProfile(); |
| 140 | uint32_t DumpProfile(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 141 | static bool ShuttingDown(Thread* self) LOCKS_EXCLUDED(Locks::profiler_lock_); |
| 142 | |
| 143 | static BackgroundMethodSamplingProfiler* profiler_ GUARDED_BY(Locks::profiler_lock_); |
| 144 | |
| 145 | // We need to shut the sample thread down at exit. Setting this to true will do that. |
| 146 | static volatile bool shutting_down_ GUARDED_BY(Locks::profiler_lock_); |
| 147 | |
| 148 | // Sampling thread, non-zero when sampling. |
| 149 | static pthread_t profiler_pthread_; |
| 150 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 151 | // Some measure of the number of samples that are significant. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 152 | static constexpr uint32_t kSignificantSamples = 10; |
| 153 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 154 | // The name of the file where profile data will be written. |
| 155 | std::string output_filename_; |
| 156 | // The options used to start the profiler. |
| 157 | const ProfilerOptions& options_; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 158 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 159 | |
| 160 | // Profile condition support. |
| 161 | Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 162 | ConditionVariable period_condition_ GUARDED_BY(wait_lock_); |
| 163 | |
| 164 | ProfileSampleResults profile_table_; |
| 165 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 166 | std::unique_ptr<Barrier> profiler_barrier_; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 167 | |
| 168 | // Set of methods to be filtered out. This will probably be rare because |
| 169 | // most of the methods we want to be filtered reside in the boot path and |
| 170 | // are automatically filtered. |
| 171 | typedef std::set<std::string> FilteredMethods; |
| 172 | FilteredMethods filtered_methods_; |
| 173 | |
| 174 | DISALLOW_COPY_AND_ASSIGN(BackgroundMethodSamplingProfiler); |
| 175 | }; |
| 176 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 177 | // |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 178 | // Contains profile data generated from previous runs of the program and stored |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 179 | // 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] | 180 | class ProfileFile { |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 181 | public: |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 182 | class ProfileData { |
| 183 | public: |
| 184 | ProfileData() : count_(0), method_size_(0), used_percent_(0) {} |
| 185 | ProfileData(const std::string& method_name, uint32_t count, uint32_t method_size, |
| 186 | double used_percent, double top_k_used_percentage) : |
| 187 | method_name_(method_name), count_(count), method_size_(method_size), |
| 188 | used_percent_(used_percent), top_k_used_percentage_(top_k_used_percentage) { |
| 189 | // TODO: currently method_size_ is unused |
| 190 | UNUSED(method_size_); |
| 191 | } |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 192 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 193 | double GetUsedPercent() const { return used_percent_; } |
| 194 | uint32_t GetCount() const { return count_; } |
| 195 | double GetTopKUsedPercentage() const { return top_k_used_percentage_; } |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 196 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 197 | private: |
| 198 | std::string method_name_; // Method name. |
| 199 | uint32_t count_; // Number of times it has been called. |
| 200 | uint32_t method_size_; // Size of the method on dex instructions. |
| 201 | double used_percent_; // Percentage of how many times this method was called. |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 202 | double top_k_used_percentage_; // The percentage of the group that comprise K% of the total |
| 203 | // used methods this methods belongs to. |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 204 | }; |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 205 | |
| 206 | public: |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 207 | // 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] | 208 | // Returns true if the file was loaded successfully and false otherwise. |
| 209 | bool LoadFile(const std::string& filename); |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 210 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 211 | // Computes the group that comprise top_k_percentage of the total used methods. |
| 212 | bool GetTopKSamples(std::set<std::string>& top_k_methods, double top_k_percentage); |
| 213 | |
| 214 | // If the given method has an entry in the profile table it updates the data |
| 215 | // and returns true. Otherwise returns false and leaves the data unchanged. |
| 216 | bool GetProfileData(ProfileData* data, const std::string& method_name); |
| 217 | |
| 218 | private: |
| 219 | // Profile data is stored in a map, indexed by the full method name. |
| 220 | typedef std::map<std::string, ProfileData> ProfileMap; |
| 221 | ProfileMap profile_map_; |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 222 | }; |
| 223 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 224 | } // namespace art |
| 225 | |
| 226 | #endif // ART_RUNTIME_PROFILER_H_ |