blob: e3af47cf5069db399dbec3247eb7239269d06c7d [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
25#include "base/macros.h"
26#include "globals.h"
27#include "instrumentation.h"
28#include "os.h"
29#include "safe_map.h"
30#include "base/mutex.h"
31#include "locks.h"
32#include "UniquePtr.h"
33#include "barrier.h"
34
35namespace art {
36
37namespace mirror {
38 class ArtMethod;
39 class Class;
40} // namespace mirror
41class Thread;
42
43
44//
45// This class holds all the results for all runs of the profiler. It also
46// counts the number of null methods (where we can't determine the method) and
47// the number of methods in the boot path (where we have already compiled the method).
48//
49// This object is an internal profiler object and uses the same locking as the profiler
50// itself.
51class ProfileSampleResults {
52 public:
53 explicit ProfileSampleResults(Mutex& lock);
54 ~ProfileSampleResults();
55
56 void Put(mirror::ArtMethod* method);
57 uint32_t Write(std::ostream &os);
58 void Clear();
59 uint32_t GetNumSamples() { return num_samples_; }
60 void NullMethod() { ++num_null_methods_; }
61 void BootMethod() { ++num_boot_methods_; }
62 private:
63 uint32_t Hash(mirror::ArtMethod* method);
64 static constexpr int kHashSize = 17;
65 Mutex& lock_; // Reference to the main profiler lock - we don't need two of them.
66 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];
72};
73
74//
75// The BackgroundMethodSamplingProfiler runs in a thread. Most of the time it is sleeping but
76// occasionally wakes up and counts the number of times a method is called. Each time
77// it ticks, it looks at the current method and records it in the ProfileSampleResults
78// table.
79//
80// The timing is controlled by a number of variables:
81// 1. Period: the time between sampling runs.
82// 2. Interval: the time between each sample in a run.
83// 3. Duration: the duration of a run.
84//
85// So the profiler thread is sleeping for the 'period' time. It wakes up and runs for the
86// 'duration'. The run consists of a series of samples, each of which is 'interval' microseconds
87// apart. At the end of a run, it writes the results table to a file and goes back to sleep.
88
89class BackgroundMethodSamplingProfiler {
90 public:
91 static void Start(int period, int duration, std::string profile_filename, int interval_us,
92 double backoff_coefficient, bool startImmediately)
93 LOCKS_EXCLUDED(Locks::mutator_lock_,
94 Locks::thread_list_lock_,
95 Locks::thread_suspend_count_lock_,
96 Locks::profiler_lock_);
97
98 static void Stop() LOCKS_EXCLUDED(Locks::profiler_lock_, wait_lock_);
99 static void Shutdown() LOCKS_EXCLUDED(Locks::profiler_lock_);
100
101 void RecordMethod(mirror::ArtMethod *method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102
103 Barrier& GetBarrier() {
104 return *profiler_barrier_;
105 }
106
107 private:
108 explicit BackgroundMethodSamplingProfiler(int period, int duration, std::string profile_filename,
109 double backoff_coefficient, int interval_us, bool startImmediately);
110
111 // The sampling interval in microseconds is passed as an argument.
112 static void* RunProfilerThread(void* arg) LOCKS_EXCLUDED(Locks::profiler_lock_);
113
114 uint32_t WriteProfile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
115
116 void CleanProfile();
117 uint32_t DumpProfile(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
118 static bool ShuttingDown(Thread* self) LOCKS_EXCLUDED(Locks::profiler_lock_);
119
120 static BackgroundMethodSamplingProfiler* profiler_ GUARDED_BY(Locks::profiler_lock_);
121
122 // We need to shut the sample thread down at exit. Setting this to true will do that.
123 static volatile bool shutting_down_ GUARDED_BY(Locks::profiler_lock_);
124
125 // Sampling thread, non-zero when sampling.
126 static pthread_t profiler_pthread_;
127
128 // Some measure of the number of samples that are significant
129 static constexpr uint32_t kSignificantSamples = 10;
130
131 // File to write profile data out to. Cannot be empty if we are profiling.
132 std::string profile_file_name_;
133
134 // Number of seconds between profile runs.
135 uint32_t period_s_;
136
137 // Most of the time we want to delay the profiler startup to prevent everything
138 // running at the same time (all processes). This is the default, but if we
139 // want to override this, set the 'start_immediately_' to true. This is done
140 // if the -Xprofile option is given on the command line.
141 bool start_immediately_;
142
143 uint32_t interval_us_;
144
145 // A backoff coefficent to adjust the profile period based on time.
146 double backoff_factor_;
147
148 // How much to increase the backoff by on each profile iteration.
149 double backoff_coefficient_;
150
151 // Duration of each profile run. The profile file will be written at the end
152 // of each run.
153 uint32_t duration_s_;
154
155 // Profile condition support.
156 Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
157 ConditionVariable period_condition_ GUARDED_BY(wait_lock_);
158
159 ProfileSampleResults profile_table_;
160
161 UniquePtr<Barrier> profiler_barrier_;
162
163 // Set of methods to be filtered out. This will probably be rare because
164 // most of the methods we want to be filtered reside in the boot path and
165 // are automatically filtered.
166 typedef std::set<std::string> FilteredMethods;
167 FilteredMethods filtered_methods_;
168
169 DISALLOW_COPY_AND_ASSIGN(BackgroundMethodSamplingProfiler);
170};
171
172} // namespace art
173
174#endif // ART_RUNTIME_PROFILER_H_