blob: bd29f711c07bc225a35919abba7161a05c89fedd [file] [log] [blame]
Dave Allison0aded082013-11-07 13:15:11 -08001/*
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 Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Dave Allison0aded082013-11-07 13:15:11 -080021#include <ostream>
22#include <set>
23#include <string>
24#include <vector>
25
Ian Rogers719d1a32014-03-06 12:13:39 -080026#include "barrier.h"
Dave Allison0aded082013-11-07 13:15:11 -080027#include "base/macros.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080028#include "base/mutex.h"
Dave Allison0aded082013-11-07 13:15:11 -080029#include "globals.h"
30#include "instrumentation.h"
Calin Juravlec1b643c2014-05-30 23:44:11 +010031#include "profiler_options.h"
Dave Allison0aded082013-11-07 13:15:11 -080032#include "os.h"
33#include "safe_map.h"
Wei Jin445220d2014-06-20 15:56:53 -070034#include "method_reference.h"
Dave Allison0aded082013-11-07 13:15:11 -080035
36namespace art {
37
38namespace mirror {
Dave Allison0aded082013-11-07 13:15:11 -080039 class Class;
40} // namespace mirror
Mathieu Chartiere401d142015-04-22 13:56:20 -070041class ArtMethod;
Dave Allison0aded082013-11-07 13:15:11 -080042class Thread;
43
Mathieu Chartiere401d142015-04-22 13:56:20 -070044typedef std::pair<ArtMethod*, uint32_t> InstructionLocation;
Wei Jin445220d2014-06-20 15:56:53 -070045
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.
49class 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 Allison0aded082013-11-07 13:15:11 -080095//
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.
102class ProfileSampleResults {
103 public:
104 explicit ProfileSampleResults(Mutex& lock);
105 ~ProfileSampleResults();
106
Mathieu Chartier90443472015-07-16 20:32:27 -0700107 void Put(ArtMethod* method) REQUIRES(!lock_);
108 void PutStack(const std::vector<InstructionLocation>& stack_dump) REQUIRES(!lock_);
Wei Jina93b0bb2014-06-09 16:19:15 -0700109 uint32_t Write(std::ostream &os, ProfileDataType type);
110 void ReadPrevious(int fd, ProfileDataType type);
Dave Allison0aded082013-11-07 13:15:11 -0800111 void Clear();
112 uint32_t GetNumSamples() { return num_samples_; }
113 void NullMethod() { ++num_null_methods_; }
114 void BootMethod() { ++num_boot_methods_; }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800115
Dave Allison0aded082013-11-07 13:15:11 -0800116 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117 uint32_t Hash(ArtMethod* method);
Dave Allison0aded082013-11-07 13:15:11 -0800118 static constexpr int kHashSize = 17;
Calin Juravlec1b643c2014-05-30 23:44:11 +0100119 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 Allison0aded082013-11-07 13:15:11 -0800123
Mathieu Chartiere401d142015-04-22 13:56:20 -0700124 typedef std::map<ArtMethod*, uint32_t> Map; // Map of method vs its count.
Dave Allison0aded082013-11-07 13:15:11 -0800125 Map *table[kHashSize];
Dave Allison39c3bfb2014-01-28 18:33:52 -0800126
Wei Jin445220d2014-06-20 15:56:53 -0700127 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 Jina93b0bb2014-06-09 16:19:15 -0700132
Wei Jin445220d2014-06-20 15:56:53 -0700133 // Map from <pc, context> to counts.
134 typedef std::map<std::pair<uint32_t, std::string>, uint32_t> PreviousContextMap;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800135 struct PreviousValue {
Wei Jin445220d2014-06-20 15:56:53 -0700136 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 Allison39c3bfb2014-01-28 18:33:52 -0800139 uint32_t count_;
140 uint32_t method_size_;
Wei Jin445220d2014-06-20 15:56:53 -0700141 PreviousContextMap* context_map_;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800142 };
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 Allison0aded082013-11-07 13:15:11 -0800149};
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
166class BackgroundMethodSamplingProfiler {
167 public:
Calin Juravlec1b643c2014-05-30 23:44:11 +0100168 // 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 Chartier90443472015-07-16 20:32:27 -0700171 REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_,
172 !Locks::profiler_lock_);
Dave Allison0aded082013-11-07 13:15:11 -0800173
Mathieu Chartier90443472015-07-16 20:32:27 -0700174 // 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 Allison0aded082013-11-07 13:15:11 -0800179
Mathieu Chartier90443472015-07-16 20:32:27 -0700180 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 Jin445220d2014-06-20 15:56:53 -0700184 const ProfilerOptions& GetProfilerOptions() const { return options_; }
Dave Allison0aded082013-11-07 13:15:11 -0800185
186 Barrier& GetBarrier() {
187 return *profiler_barrier_;
188 }
189
190 private:
Calin Juravlec1b643c2014-05-30 23:44:11 +0100191 explicit BackgroundMethodSamplingProfiler(
192 const std::string& output_filename, const ProfilerOptions& options);
Dave Allison0aded082013-11-07 13:15:11 -0800193
194 // The sampling interval in microseconds is passed as an argument.
Mathieu Chartier90443472015-07-16 20:32:27 -0700195 // 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 Allison0aded082013-11-07 13:15:11 -0800198
Mathieu Chartier90443472015-07-16 20:32:27 -0700199 uint32_t WriteProfile() SHARED_REQUIRES(Locks::mutator_lock_);
Dave Allison0aded082013-11-07 13:15:11 -0800200
201 void CleanProfile();
Mathieu Chartier90443472015-07-16 20:32:27 -0700202 uint32_t DumpProfile(std::ostream& os) SHARED_REQUIRES(Locks::mutator_lock_);
203 static bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_);
Dave Allison0aded082013-11-07 13:15:11 -0800204
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 Juravlec1b643c2014-05-30 23:44:11 +0100213 // Some measure of the number of samples that are significant.
Dave Allison0aded082013-11-07 13:15:11 -0800214 static constexpr uint32_t kSignificantSamples = 10;
215
Calin Juravlec1b643c2014-05-30 23:44:11 +0100216 // 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 Allison0aded082013-11-07 13:15:11 -0800220
Dave Allison0aded082013-11-07 13:15:11 -0800221
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 Rogers700a4022014-05-19 16:49:03 -0700228 std::unique_ptr<Barrier> profiler_barrier_;
Dave Allison0aded082013-11-07 13:15:11 -0800229
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 Juravlec1b643c2014-05-30 23:44:11 +0100239//
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100240// Contains profile data generated from previous runs of the program and stored
Calin Juravle9dae5b42014-04-07 16:36:21 +0300241// in a file. It is used to determine whether to compile a particular method or not.
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100242class ProfileFile {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300243 public:
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100244 class ProfileData {
245 public:
Sebastien Hertzaa50d3a2015-08-25 15:25:41 +0200246 ProfileData() : count_(0), method_size_(0), used_percent_(0), top_k_used_percentage_(0) {}
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100247 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 Juravle9dae5b42014-04-07 16:36:21 +0300254
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100255 double GetUsedPercent() const { return used_percent_; }
256 uint32_t GetCount() const { return count_; }
257 double GetTopKUsedPercentage() const { return top_k_used_percentage_; }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300258
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100259 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 Juravlec1b643c2014-05-30 23:44:11 +0100264 double top_k_used_percentage_; // The percentage of the group that comprise K% of the total
265 // used methods this methods belongs to.
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100266 };
Calin Juravle9dae5b42014-04-07 16:36:21 +0300267
268 public:
Calin Juravlec1b643c2014-05-30 23:44:11 +0100269 // Loads profile data from the given file. The new data are merged with any existing data.
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100270 // Returns true if the file was loaded successfully and false otherwise.
271 bool LoadFile(const std::string& filename);
Calin Juravle9dae5b42014-04-07 16:36:21 +0300272
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100273 // 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 Juravle9dae5b42014-04-07 16:36:21 +0300284};
285
Dave Allison0aded082013-11-07 13:15:11 -0800286} // namespace art
287
288#endif // ART_RUNTIME_PROFILER_H_