blob: b03b1700f4d3917662910fa56f7b0ae149d7adc6 [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"
Dave Allison0aded082013-11-07 13:15:11 -080032#include "UniquePtr.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
42
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.
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;
66 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.
70
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) {}
76 PreviousValue(uint32_t count, uint32_t method_size) : count_(count), method_size_(method_size) {}
77 uint32_t count_;
78 uint32_t method_size_;
79 };
80
81 typedef std::map<std::string, PreviousValue> PreviousProfile;
82 PreviousProfile previous_;
83 uint32_t previous_num_samples_;
84 uint32_t previous_num_null_methods_; // Number of samples where can don't know the method.
85 uint32_t previous_num_boot_methods_; // Number of samples in the boot path.
Dave Allison0aded082013-11-07 13:15:11 -080086};
87
88//
89// The BackgroundMethodSamplingProfiler runs in a thread. Most of the time it is sleeping but
90// occasionally wakes up and counts the number of times a method is called. Each time
91// it ticks, it looks at the current method and records it in the ProfileSampleResults
92// table.
93//
94// The timing is controlled by a number of variables:
95// 1. Period: the time between sampling runs.
96// 2. Interval: the time between each sample in a run.
97// 3. Duration: the duration of a run.
98//
99// So the profiler thread is sleeping for the 'period' time. It wakes up and runs for the
100// 'duration'. The run consists of a series of samples, each of which is 'interval' microseconds
101// apart. At the end of a run, it writes the results table to a file and goes back to sleep.
102
103class BackgroundMethodSamplingProfiler {
104 public:
Dave Allison39c3bfb2014-01-28 18:33:52 -0800105 static void Start(int period, int duration, const std::string& profile_filename,
106 const std::string& procName, int interval_us,
Dave Allison0aded082013-11-07 13:15:11 -0800107 double backoff_coefficient, bool startImmediately)
108 LOCKS_EXCLUDED(Locks::mutator_lock_,
109 Locks::thread_list_lock_,
110 Locks::thread_suspend_count_lock_,
111 Locks::profiler_lock_);
112
113 static void Stop() LOCKS_EXCLUDED(Locks::profiler_lock_, wait_lock_);
114 static void Shutdown() LOCKS_EXCLUDED(Locks::profiler_lock_);
115
116 void RecordMethod(mirror::ArtMethod *method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
117
118 Barrier& GetBarrier() {
119 return *profiler_barrier_;
120 }
121
122 private:
Dave Allison39c3bfb2014-01-28 18:33:52 -0800123 explicit BackgroundMethodSamplingProfiler(int period, int duration,
124 const std::string& profile_filename,
125 const std::string& process_name,
126 double backoff_coefficient, int interval_us, bool startImmediately);
Dave Allison0aded082013-11-07 13:15:11 -0800127
128 // The sampling interval in microseconds is passed as an argument.
129 static void* RunProfilerThread(void* arg) LOCKS_EXCLUDED(Locks::profiler_lock_);
130
131 uint32_t WriteProfile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
132
133 void CleanProfile();
134 uint32_t DumpProfile(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
135 static bool ShuttingDown(Thread* self) LOCKS_EXCLUDED(Locks::profiler_lock_);
136
137 static BackgroundMethodSamplingProfiler* profiler_ GUARDED_BY(Locks::profiler_lock_);
138
139 // We need to shut the sample thread down at exit. Setting this to true will do that.
140 static volatile bool shutting_down_ GUARDED_BY(Locks::profiler_lock_);
141
142 // Sampling thread, non-zero when sampling.
143 static pthread_t profiler_pthread_;
144
145 // Some measure of the number of samples that are significant
146 static constexpr uint32_t kSignificantSamples = 10;
147
148 // File to write profile data out to. Cannot be empty if we are profiling.
149 std::string profile_file_name_;
150
Dave Allison39c3bfb2014-01-28 18:33:52 -0800151 // Process name.
152 std::string process_name_;
153
Dave Allison0aded082013-11-07 13:15:11 -0800154 // Number of seconds between profile runs.
155 uint32_t period_s_;
156
157 // Most of the time we want to delay the profiler startup to prevent everything
158 // running at the same time (all processes). This is the default, but if we
159 // want to override this, set the 'start_immediately_' to true. This is done
160 // if the -Xprofile option is given on the command line.
161 bool start_immediately_;
162
163 uint32_t interval_us_;
164
165 // A backoff coefficent to adjust the profile period based on time.
166 double backoff_factor_;
167
168 // How much to increase the backoff by on each profile iteration.
169 double backoff_coefficient_;
170
171 // Duration of each profile run. The profile file will be written at the end
172 // of each run.
173 uint32_t duration_s_;
174
175 // Profile condition support.
176 Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
177 ConditionVariable period_condition_ GUARDED_BY(wait_lock_);
178
179 ProfileSampleResults profile_table_;
180
181 UniquePtr<Barrier> profiler_barrier_;
182
183 // Set of methods to be filtered out. This will probably be rare because
184 // most of the methods we want to be filtered reside in the boot path and
185 // are automatically filtered.
186 typedef std::set<std::string> FilteredMethods;
187 FilteredMethods filtered_methods_;
188
189 DISALLOW_COPY_AND_ASSIGN(BackgroundMethodSamplingProfiler);
190};
191
192} // namespace art
193
194#endif // ART_RUNTIME_PROFILER_H_