blob: 396dd23fe3bedd28bbade8266abe4f57e8cc3c34 [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"
Dave Allison0aded082013-11-07 13:15:11 -080034
35namespace art {
36
37namespace mirror {
38 class ArtMethod;
39 class Class;
40} // namespace mirror
41class Thread;
42
Dave Allison0aded082013-11-07 13:15:11 -080043//
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.
50class ProfileSampleResults {
51 public:
52 explicit ProfileSampleResults(Mutex& lock);
53 ~ProfileSampleResults();
54
55 void Put(mirror::ArtMethod* method);
Wei Jina93b0bb2014-06-09 16:19:15 -070056 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 Allison0aded082013-11-07 13:15:11 -080059 void Clear();
60 uint32_t GetNumSamples() { return num_samples_; }
61 void NullMethod() { ++num_null_methods_; }
62 void BootMethod() { ++num_boot_methods_; }
Dave Allison39c3bfb2014-01-28 18:33:52 -080063
Dave Allison0aded082013-11-07 13:15:11 -080064 private:
65 uint32_t Hash(mirror::ArtMethod* method);
66 static constexpr int kHashSize = 17;
Calin Juravlec1b643c2014-05-30 23:44:11 +010067 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 Allison0aded082013-11-07 13:15:11 -080071
Wei Jina93b0bb2014-06-09 16:19:15 -070072 typedef std::map<mirror::ArtMethod*, uint32_t> Map; // Map of method vs its count.
Dave Allison0aded082013-11-07 13:15:11 -080073 Map *table[kHashSize];
Dave Allison39c3bfb2014-01-28 18:33:52 -080074
Wei Jina93b0bb2014-06-09 16:19:15 -070075 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 Allison39c3bfb2014-01-28 18:33:52 -080080 struct PreviousValue {
Wei Jina93b0bb2014-06-09 16:19:15 -070081 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 Allison39c3bfb2014-01-28 18:33:52 -080084 uint32_t count_;
85 uint32_t method_size_;
Wei Jina93b0bb2014-06-09 16:19:15 -070086 DexPCCountMap* dex_pc_map_;
Dave Allison39c3bfb2014-01-28 18:33:52 -080087 };
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 Allison0aded082013-11-07 13:15:11 -080094};
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
111class BackgroundMethodSamplingProfiler {
112 public:
Calin Juravlec1b643c2014-05-30 23:44:11 +0100113 // 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 Allison0aded082013-11-07 13:15:11 -0800116 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 Jina93b0bb2014-06-09 16:19:15 -0700124 void RecordMethod(mirror::ArtMethod *method, uint32_t pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Dave Allison0aded082013-11-07 13:15:11 -0800125
126 Barrier& GetBarrier() {
127 return *profiler_barrier_;
128 }
129
130 private:
Calin Juravlec1b643c2014-05-30 23:44:11 +0100131 explicit BackgroundMethodSamplingProfiler(
132 const std::string& output_filename, const ProfilerOptions& options);
Dave Allison0aded082013-11-07 13:15:11 -0800133
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 Juravlec1b643c2014-05-30 23:44:11 +0100151 // Some measure of the number of samples that are significant.
Dave Allison0aded082013-11-07 13:15:11 -0800152 static constexpr uint32_t kSignificantSamples = 10;
153
Calin Juravlec1b643c2014-05-30 23:44:11 +0100154 // 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 Allison0aded082013-11-07 13:15:11 -0800158
Dave Allison0aded082013-11-07 13:15:11 -0800159
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 Rogers700a4022014-05-19 16:49:03 -0700166 std::unique_ptr<Barrier> profiler_barrier_;
Dave Allison0aded082013-11-07 13:15:11 -0800167
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 Juravlec1b643c2014-05-30 23:44:11 +0100177//
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100178// Contains profile data generated from previous runs of the program and stored
Calin Juravle9dae5b42014-04-07 16:36:21 +0300179// in a file. It is used to determine whether to compile a particular method or not.
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100180class ProfileFile {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300181 public:
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100182 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 Juravle9dae5b42014-04-07 16:36:21 +0300192
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100193 double GetUsedPercent() const { return used_percent_; }
194 uint32_t GetCount() const { return count_; }
195 double GetTopKUsedPercentage() const { return top_k_used_percentage_; }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300196
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100197 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 Juravlec1b643c2014-05-30 23:44:11 +0100202 double top_k_used_percentage_; // The percentage of the group that comprise K% of the total
203 // used methods this methods belongs to.
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100204 };
Calin Juravle9dae5b42014-04-07 16:36:21 +0300205
206 public:
Calin Juravlec1b643c2014-05-30 23:44:11 +0100207 // Loads profile data from the given file. The new data are merged with any existing data.
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100208 // Returns true if the file was loaded successfully and false otherwise.
209 bool LoadFile(const std::string& filename);
Calin Juravle9dae5b42014-04-07 16:36:21 +0300210
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100211 // 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 Juravle9dae5b42014-04-07 16:36:21 +0300222};
223
Dave Allison0aded082013-11-07 13:15:11 -0800224} // namespace art
225
226#endif // ART_RUNTIME_PROFILER_H_