blob: 0b18dbb90be0f25aba182d835049fac9ad44d28d [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);
56 uint32_t Write(std::ostream &os);
Dave Allison39c3bfb2014-01-28 18:33:52 -080057 void ReadPrevious(int fd);
Dave Allison0aded082013-11-07 13:15:11 -080058 void Clear();
59 uint32_t GetNumSamples() { return num_samples_; }
60 void NullMethod() { ++num_null_methods_; }
61 void BootMethod() { ++num_boot_methods_; }
Dave Allison39c3bfb2014-01-28 18:33:52 -080062
Dave Allison0aded082013-11-07 13:15:11 -080063 private:
64 uint32_t Hash(mirror::ArtMethod* method);
65 static constexpr int kHashSize = 17;
Calin Juravlec1b643c2014-05-30 23:44:11 +010066 Mutex& lock_; // Reference to the main profiler lock - we don't need two of them.
67 uint32_t num_samples_; // Total number of samples taken.
68 uint32_t num_null_methods_; // Number of samples where can don't know the method.
69 uint32_t num_boot_methods_; // Number of samples in the boot path.
Dave Allison0aded082013-11-07 13:15:11 -080070
71 typedef std::map<mirror::ArtMethod*, uint32_t> Map; // Map of method vs its count.
72 Map *table[kHashSize];
Dave Allison39c3bfb2014-01-28 18:33:52 -080073
74 struct PreviousValue {
75 PreviousValue() : count_(0), method_size_(0) {}
Calin Juravlec1b643c2014-05-30 23:44:11 +010076 PreviousValue(uint32_t count, uint32_t method_size)
77 : count_(count), method_size_(method_size) {}
Dave Allison39c3bfb2014-01-28 18:33:52 -080078 uint32_t count_;
79 uint32_t method_size_;
80 };
81
82 typedef std::map<std::string, PreviousValue> PreviousProfile;
83 PreviousProfile previous_;
84 uint32_t previous_num_samples_;
85 uint32_t previous_num_null_methods_; // Number of samples where can don't know the method.
86 uint32_t previous_num_boot_methods_; // Number of samples in the boot path.
Dave Allison0aded082013-11-07 13:15:11 -080087};
88
89//
90// The BackgroundMethodSamplingProfiler runs in a thread. Most of the time it is sleeping but
91// occasionally wakes up and counts the number of times a method is called. Each time
92// it ticks, it looks at the current method and records it in the ProfileSampleResults
93// table.
94//
95// The timing is controlled by a number of variables:
96// 1. Period: the time between sampling runs.
97// 2. Interval: the time between each sample in a run.
98// 3. Duration: the duration of a run.
99//
100// So the profiler thread is sleeping for the 'period' time. It wakes up and runs for the
101// 'duration'. The run consists of a series of samples, each of which is 'interval' microseconds
102// apart. At the end of a run, it writes the results table to a file and goes back to sleep.
103
104class BackgroundMethodSamplingProfiler {
105 public:
Calin Juravlec1b643c2014-05-30 23:44:11 +0100106 // Start a profile thread with the user-supplied arguments.
107 // Returns true if the profile was started or if it was already running. Returns false otherwise.
108 static bool Start(const std::string& output_filename, const ProfilerOptions& options)
Dave Allison0aded082013-11-07 13:15:11 -0800109 LOCKS_EXCLUDED(Locks::mutator_lock_,
110 Locks::thread_list_lock_,
111 Locks::thread_suspend_count_lock_,
112 Locks::profiler_lock_);
113
114 static void Stop() LOCKS_EXCLUDED(Locks::profiler_lock_, wait_lock_);
115 static void Shutdown() LOCKS_EXCLUDED(Locks::profiler_lock_);
116
117 void RecordMethod(mirror::ArtMethod *method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
118
119 Barrier& GetBarrier() {
120 return *profiler_barrier_;
121 }
122
123 private:
Calin Juravlec1b643c2014-05-30 23:44:11 +0100124 explicit BackgroundMethodSamplingProfiler(
125 const std::string& output_filename, const ProfilerOptions& options);
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
Calin Juravlec1b643c2014-05-30 23:44:11 +0100144 // Some measure of the number of samples that are significant.
Dave Allison0aded082013-11-07 13:15:11 -0800145 static constexpr uint32_t kSignificantSamples = 10;
146
Calin Juravlec1b643c2014-05-30 23:44:11 +0100147 // The name of the file where profile data will be written.
148 std::string output_filename_;
149 // The options used to start the profiler.
150 const ProfilerOptions& options_;
Dave Allison0aded082013-11-07 13:15:11 -0800151
Dave Allison0aded082013-11-07 13:15:11 -0800152
153 // Profile condition support.
154 Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
155 ConditionVariable period_condition_ GUARDED_BY(wait_lock_);
156
157 ProfileSampleResults profile_table_;
158
Ian Rogers700a4022014-05-19 16:49:03 -0700159 std::unique_ptr<Barrier> profiler_barrier_;
Dave Allison0aded082013-11-07 13:15:11 -0800160
161 // Set of methods to be filtered out. This will probably be rare because
162 // most of the methods we want to be filtered reside in the boot path and
163 // are automatically filtered.
164 typedef std::set<std::string> FilteredMethods;
165 FilteredMethods filtered_methods_;
166
167 DISALLOW_COPY_AND_ASSIGN(BackgroundMethodSamplingProfiler);
168};
169
Calin Juravlec1b643c2014-05-30 23:44:11 +0100170//
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100171// Contains profile data generated from previous runs of the program and stored
Calin Juravle9dae5b42014-04-07 16:36:21 +0300172// in a file. It is used to determine whether to compile a particular method or not.
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100173class ProfileFile {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300174 public:
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100175 class ProfileData {
176 public:
177 ProfileData() : count_(0), method_size_(0), used_percent_(0) {}
178 ProfileData(const std::string& method_name, uint32_t count, uint32_t method_size,
179 double used_percent, double top_k_used_percentage) :
180 method_name_(method_name), count_(count), method_size_(method_size),
181 used_percent_(used_percent), top_k_used_percentage_(top_k_used_percentage) {
182 // TODO: currently method_size_ is unused
183 UNUSED(method_size_);
184 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300185
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100186 double GetUsedPercent() const { return used_percent_; }
187 uint32_t GetCount() const { return count_; }
188 double GetTopKUsedPercentage() const { return top_k_used_percentage_; }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300189
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100190 private:
191 std::string method_name_; // Method name.
192 uint32_t count_; // Number of times it has been called.
193 uint32_t method_size_; // Size of the method on dex instructions.
194 double used_percent_; // Percentage of how many times this method was called.
Calin Juravlec1b643c2014-05-30 23:44:11 +0100195 double top_k_used_percentage_; // The percentage of the group that comprise K% of the total
196 // used methods this methods belongs to.
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100197 };
Calin Juravle9dae5b42014-04-07 16:36:21 +0300198
199 public:
Calin Juravlec1b643c2014-05-30 23:44:11 +0100200 // Loads profile data from the given file. The new data are merged with any existing data.
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100201 // Returns true if the file was loaded successfully and false otherwise.
202 bool LoadFile(const std::string& filename);
Calin Juravle9dae5b42014-04-07 16:36:21 +0300203
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100204 // Computes the group that comprise top_k_percentage of the total used methods.
205 bool GetTopKSamples(std::set<std::string>& top_k_methods, double top_k_percentage);
206
207 // If the given method has an entry in the profile table it updates the data
208 // and returns true. Otherwise returns false and leaves the data unchanged.
209 bool GetProfileData(ProfileData* data, const std::string& method_name);
210
211 private:
212 // Profile data is stored in a map, indexed by the full method name.
213 typedef std::map<std::string, ProfileData> ProfileMap;
214 ProfileMap profile_map_;
Calin Juravle9dae5b42014-04-07 16:36:21 +0300215};
216
Dave Allison0aded082013-11-07 13:15:11 -0800217} // namespace art
218
219#endif // ART_RUNTIME_PROFILER_H_