blob: bcd7c29793c962f4c54a22933244bb3b1a61b168 [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
20#include <ostream>
21#include <set>
22#include <string>
23#include <vector>
24
Ian Rogers719d1a32014-03-06 12:13:39 -080025#include "barrier.h"
Dave Allison0aded082013-11-07 13:15:11 -080026#include "base/macros.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080027#include "base/mutex.h"
Dave Allison0aded082013-11-07 13:15:11 -080028#include "globals.h"
29#include "instrumentation.h"
30#include "os.h"
31#include "safe_map.h"
Ian Rogers507dfdd2014-05-15 16:42:40 -070032#include "UniquePtrCompat.h"
Dave Allison0aded082013-11-07 13:15:11 -080033
34namespace art {
35
36namespace mirror {
37 class ArtMethod;
38 class Class;
39} // namespace mirror
40class Thread;
41
Dave Allison0aded082013-11-07 13:15:11 -080042//
43// This class holds all the results for all runs of the profiler. It also
44// counts the number of null methods (where we can't determine the method) and
45// the number of methods in the boot path (where we have already compiled the method).
46//
47// This object is an internal profiler object and uses the same locking as the profiler
48// itself.
49class ProfileSampleResults {
50 public:
51 explicit ProfileSampleResults(Mutex& lock);
52 ~ProfileSampleResults();
53
54 void Put(mirror::ArtMethod* method);
55 uint32_t Write(std::ostream &os);
Dave Allison39c3bfb2014-01-28 18:33:52 -080056 void ReadPrevious(int fd);
Dave Allison0aded082013-11-07 13:15:11 -080057 void Clear();
58 uint32_t GetNumSamples() { return num_samples_; }
59 void NullMethod() { ++num_null_methods_; }
60 void BootMethod() { ++num_boot_methods_; }
Dave Allison39c3bfb2014-01-28 18:33:52 -080061
Dave Allison0aded082013-11-07 13:15:11 -080062 private:
63 uint32_t Hash(mirror::ArtMethod* method);
64 static constexpr int kHashSize = 17;
Calin Juravle9dae5b42014-04-07 16:36:21 +030065 Mutex& lock_; // Reference to the main profiler lock - we don't need two of them.
Dave Allison0aded082013-11-07 13:15:11 -080066 uint32_t num_samples_; // Total number of samples taken.
67 uint32_t num_null_methods_; // Number of samples where can don't know the method.
68 uint32_t num_boot_methods_; // Number of samples in the boot path.
69
70 typedef std::map<mirror::ArtMethod*, uint32_t> Map; // Map of method vs its count.
71 Map *table[kHashSize];
Dave Allison39c3bfb2014-01-28 18:33:52 -080072
73 struct PreviousValue {
74 PreviousValue() : count_(0), method_size_(0) {}
75 PreviousValue(uint32_t count, uint32_t method_size) : count_(count), method_size_(method_size) {}
76 uint32_t count_;
77 uint32_t method_size_;
78 };
79
80 typedef std::map<std::string, PreviousValue> PreviousProfile;
81 PreviousProfile previous_;
82 uint32_t previous_num_samples_;
83 uint32_t previous_num_null_methods_; // Number of samples where can don't know the method.
84 uint32_t previous_num_boot_methods_; // Number of samples in the boot path.
Dave Allison0aded082013-11-07 13:15:11 -080085};
86
87//
88// The BackgroundMethodSamplingProfiler runs in a thread. Most of the time it is sleeping but
89// occasionally wakes up and counts the number of times a method is called. Each time
90// it ticks, it looks at the current method and records it in the ProfileSampleResults
91// table.
92//
93// The timing is controlled by a number of variables:
94// 1. Period: the time between sampling runs.
95// 2. Interval: the time between each sample in a run.
96// 3. Duration: the duration of a run.
97//
98// So the profiler thread is sleeping for the 'period' time. It wakes up and runs for the
99// 'duration'. The run consists of a series of samples, each of which is 'interval' microseconds
100// apart. At the end of a run, it writes the results table to a file and goes back to sleep.
101
102class BackgroundMethodSamplingProfiler {
103 public:
Dave Allison39c3bfb2014-01-28 18:33:52 -0800104 static void Start(int period, int duration, const std::string& profile_filename,
105 const std::string& procName, int interval_us,
Dave Allison0aded082013-11-07 13:15:11 -0800106 double backoff_coefficient, bool startImmediately)
107 LOCKS_EXCLUDED(Locks::mutator_lock_,
108 Locks::thread_list_lock_,
109 Locks::thread_suspend_count_lock_,
110 Locks::profiler_lock_);
111
112 static void Stop() LOCKS_EXCLUDED(Locks::profiler_lock_, wait_lock_);
113 static void Shutdown() LOCKS_EXCLUDED(Locks::profiler_lock_);
114
115 void RecordMethod(mirror::ArtMethod *method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116
117 Barrier& GetBarrier() {
118 return *profiler_barrier_;
119 }
120
121 private:
Dave Allison39c3bfb2014-01-28 18:33:52 -0800122 explicit BackgroundMethodSamplingProfiler(int period, int duration,
123 const std::string& profile_filename,
124 const std::string& process_name,
125 double backoff_coefficient, int interval_us, bool startImmediately);
Dave Allison0aded082013-11-07 13:15:11 -0800126
127 // The sampling interval in microseconds is passed as an argument.
128 static void* RunProfilerThread(void* arg) LOCKS_EXCLUDED(Locks::profiler_lock_);
129
130 uint32_t WriteProfile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
131
132 void CleanProfile();
133 uint32_t DumpProfile(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
134 static bool ShuttingDown(Thread* self) LOCKS_EXCLUDED(Locks::profiler_lock_);
135
136 static BackgroundMethodSamplingProfiler* profiler_ GUARDED_BY(Locks::profiler_lock_);
137
138 // We need to shut the sample thread down at exit. Setting this to true will do that.
139 static volatile bool shutting_down_ GUARDED_BY(Locks::profiler_lock_);
140
141 // Sampling thread, non-zero when sampling.
142 static pthread_t profiler_pthread_;
143
144 // Some measure of the number of samples that are significant
145 static constexpr uint32_t kSignificantSamples = 10;
146
147 // File to write profile data out to. Cannot be empty if we are profiling.
148 std::string profile_file_name_;
149
Dave Allison39c3bfb2014-01-28 18:33:52 -0800150 // Process name.
151 std::string process_name_;
152
Dave Allison0aded082013-11-07 13:15:11 -0800153 // Number of seconds between profile runs.
154 uint32_t period_s_;
155
156 // Most of the time we want to delay the profiler startup to prevent everything
157 // running at the same time (all processes). This is the default, but if we
158 // want to override this, set the 'start_immediately_' to true. This is done
159 // if the -Xprofile option is given on the command line.
160 bool start_immediately_;
161
162 uint32_t interval_us_;
163
164 // A backoff coefficent to adjust the profile period based on time.
165 double backoff_factor_;
166
167 // How much to increase the backoff by on each profile iteration.
168 double backoff_coefficient_;
169
170 // Duration of each profile run. The profile file will be written at the end
171 // of each run.
172 uint32_t duration_s_;
173
174 // Profile condition support.
175 Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
176 ConditionVariable period_condition_ GUARDED_BY(wait_lock_);
177
178 ProfileSampleResults profile_table_;
179
180 UniquePtr<Barrier> profiler_barrier_;
181
182 // Set of methods to be filtered out. This will probably be rare because
183 // most of the methods we want to be filtered reside in the boot path and
184 // are automatically filtered.
185 typedef std::set<std::string> FilteredMethods;
186 FilteredMethods filtered_methods_;
187
188 DISALLOW_COPY_AND_ASSIGN(BackgroundMethodSamplingProfiler);
189};
190
Calin Juravle9dae5b42014-04-07 16:36:21 +0300191// TODO: incorporate in ProfileSampleResults
192
193// Profile data. This is generated from previous runs of the program and stored
194// in a file. It is used to determine whether to compile a particular method or not.
195class ProfileData {
196 public:
197 ProfileData() : count_(0), method_size_(0), usedPercent_(0) {}
198 ProfileData(const std::string& method_name, uint32_t count, uint32_t method_size,
199 double usedPercent, double topKUsedPercentage) :
200 method_name_(method_name), count_(count), method_size_(method_size),
201 usedPercent_(usedPercent), topKUsedPercentage_(topKUsedPercentage) {
202 // TODO: currently method_size_ and count_ are unused.
203 UNUSED(method_size_);
204 UNUSED(count_);
205 }
206
207 bool IsAbove(double v) const { return usedPercent_ >= v; }
208 double GetUsedPercent() const { return usedPercent_; }
209 uint32_t GetCount() const { return count_; }
210 double GetTopKUsedPercentage() const { return topKUsedPercentage_; }
211
212 private:
213 std::string method_name_; // Method name.
214 uint32_t count_; // Number of times it has been called.
215 uint32_t method_size_; // Size of the method on dex instructions.
216 double usedPercent_; // Percentage of how many times this method was called.
217 double topKUsedPercentage_; // The percentage of the group that comprise K% of the total used
218 // methods this methods belongs to.
219};
220
221// Profile data is stored in a map, indexed by the full method name.
222typedef std::map<std::string, ProfileData> ProfileMap;
223
224class ProfileHelper {
225 private:
226 ProfileHelper();
227
228 public:
229 // Read the profile data from the given file. Calculates the percentage for each method.
230 // Returns false if there was no profile file or it was malformed.
231 static bool LoadProfileMap(ProfileMap& profileMap, const std::string& fileName);
232
233 // Read the profile data from the given file and computes the group that comprise
234 // topKPercentage of the total used methods.
235 static bool LoadTopKSamples(std::set<std::string>& topKMethods, const std::string& fileName,
236 double topKPercentage);
237};
238
Dave Allison0aded082013-11-07 13:15:11 -0800239} // namespace art
240
241#endif // ART_RUNTIME_PROFILER_H_