summaryrefslogtreecommitdiffstats
path: root/runtime/profiler.h
blob: bcd7c29793c962f4c54a22933244bb3b1a61b168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ART_RUNTIME_PROFILER_H_
#define ART_RUNTIME_PROFILER_H_

#include <ostream>
#include <set>
#include <string>
#include <vector>

#include "barrier.h"
#include "base/macros.h"
#include "base/mutex.h"
#include "globals.h"
#include "instrumentation.h"
#include "os.h"
#include "safe_map.h"
#include "UniquePtrCompat.h"

namespace art {

namespace mirror {
  class ArtMethod;
  class Class;
}  // namespace mirror
class Thread;

//
// This class holds all the results for all runs of the profiler.  It also
// counts the number of null methods (where we can't determine the method) and
// the number of methods in the boot path (where we have already compiled the method).
//
// This object is an internal profiler object and uses the same locking as the profiler
// itself.
class ProfileSampleResults {
 public:
  explicit ProfileSampleResults(Mutex& lock);
  ~ProfileSampleResults();

  void Put(mirror::ArtMethod* method);
  uint32_t Write(std::ostream &os);
  void ReadPrevious(int fd);
  void Clear();
  uint32_t GetNumSamples() { return num_samples_; }
  void NullMethod() { ++num_null_methods_; }
  void BootMethod() { ++num_boot_methods_; }

 private:
  uint32_t Hash(mirror::ArtMethod* method);
  static constexpr int kHashSize = 17;
  Mutex& lock_;                   // Reference to the main profiler lock - we don't need two of them.
  uint32_t num_samples_;          // Total number of samples taken.
  uint32_t num_null_methods_;     // Number of samples where can don't know the method.
  uint32_t num_boot_methods_;     // Number of samples in the boot path.

  typedef std::map<mirror::ArtMethod*, uint32_t> Map;   // Map of method vs its count.
  Map *table[kHashSize];

  struct PreviousValue {
    PreviousValue() : count_(0), method_size_(0) {}
    PreviousValue(uint32_t count, uint32_t method_size) : count_(count), method_size_(method_size) {}
    uint32_t count_;
    uint32_t method_size_;
  };

  typedef std::map<std::string, PreviousValue> PreviousProfile;
  PreviousProfile previous_;
  uint32_t previous_num_samples_;
  uint32_t previous_num_null_methods_;     // Number of samples where can don't know the method.
  uint32_t previous_num_boot_methods_;     // Number of samples in the boot path.
};

//
// The BackgroundMethodSamplingProfiler runs in a thread.  Most of the time it is sleeping but
// occasionally wakes up and counts the number of times a method is called.  Each time
// it ticks, it looks at the current method and records it in the ProfileSampleResults
// table.
//
// The timing is controlled by a number of variables:
// 1.  Period: the time between sampling runs.
// 2.  Interval: the time between each sample in a run.
// 3.  Duration: the duration of a run.
//
// So the profiler thread is sleeping for the 'period' time.  It wakes up and runs for the
// 'duration'.  The run consists of a series of samples, each of which is 'interval' microseconds
// apart.  At the end of a run, it writes the results table to a file and goes back to sleep.

class BackgroundMethodSamplingProfiler {
 public:
  static void Start(int period, int duration, const std::string& profile_filename,
                    const std::string& procName, int interval_us,
                    double backoff_coefficient, bool startImmediately)
  LOCKS_EXCLUDED(Locks::mutator_lock_,
                 Locks::thread_list_lock_,
                 Locks::thread_suspend_count_lock_,
                 Locks::profiler_lock_);

  static void Stop() LOCKS_EXCLUDED(Locks::profiler_lock_, wait_lock_);
  static void Shutdown() LOCKS_EXCLUDED(Locks::profiler_lock_);

  void RecordMethod(mirror::ArtMethod *method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);

  Barrier& GetBarrier() {
    return *profiler_barrier_;
  }

 private:
  explicit BackgroundMethodSamplingProfiler(int period, int duration,
                                            const std::string& profile_filename,
                                            const std::string& process_name,
                                            double backoff_coefficient, int interval_us, bool startImmediately);

  // The sampling interval in microseconds is passed as an argument.
  static void* RunProfilerThread(void* arg) LOCKS_EXCLUDED(Locks::profiler_lock_);

  uint32_t WriteProfile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);

  void CleanProfile();
  uint32_t DumpProfile(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
  static bool ShuttingDown(Thread* self) LOCKS_EXCLUDED(Locks::profiler_lock_);

  static BackgroundMethodSamplingProfiler* profiler_ GUARDED_BY(Locks::profiler_lock_);

  // We need to shut the sample thread down at exit.  Setting this to true will do that.
  static volatile bool shutting_down_ GUARDED_BY(Locks::profiler_lock_);

  // Sampling thread, non-zero when sampling.
  static pthread_t profiler_pthread_;

  // Some measure of the number of samples that are significant
  static constexpr uint32_t kSignificantSamples = 10;

  // File to write profile data out to.  Cannot be empty if we are profiling.
  std::string profile_file_name_;

  // Process name.
  std::string process_name_;

  // Number of seconds between profile runs.
  uint32_t period_s_;

  // Most of the time we want to delay the profiler startup to prevent everything
  // running at the same time (all processes).  This is the default, but if we
  // want to override this, set the 'start_immediately_' to true.  This is done
  // if the -Xprofile option is given on the command line.
  bool start_immediately_;

  uint32_t interval_us_;

  // A backoff coefficent to adjust the profile period based on time.
  double backoff_factor_;

  // How much to increase the backoff by on each profile iteration.
  double backoff_coefficient_;

  // Duration of each profile run.  The profile file will be written at the end
  // of each run.
  uint32_t duration_s_;

  // Profile condition support.
  Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
  ConditionVariable period_condition_ GUARDED_BY(wait_lock_);

  ProfileSampleResults profile_table_;

  UniquePtr<Barrier> profiler_barrier_;

  // Set of methods to be filtered out.  This will probably be rare because
  // most of the methods we want to be filtered reside in the boot path and
  // are automatically filtered.
  typedef std::set<std::string> FilteredMethods;
  FilteredMethods filtered_methods_;

  DISALLOW_COPY_AND_ASSIGN(BackgroundMethodSamplingProfiler);
};

// TODO: incorporate in ProfileSampleResults

// Profile data.  This is generated from previous runs of the program and stored
// in a file.  It is used to determine whether to compile a particular method or not.
class ProfileData {
 public:
  ProfileData() : count_(0), method_size_(0), usedPercent_(0) {}
  ProfileData(const std::string& method_name, uint32_t count, uint32_t method_size,
    double usedPercent, double topKUsedPercentage) :
    method_name_(method_name), count_(count), method_size_(method_size),
    usedPercent_(usedPercent), topKUsedPercentage_(topKUsedPercentage) {
    // TODO: currently method_size_ and count_ are unused.
    UNUSED(method_size_);
    UNUSED(count_);
  }

  bool IsAbove(double v) const { return usedPercent_ >= v; }
  double GetUsedPercent() const { return usedPercent_; }
  uint32_t GetCount() const { return count_; }
  double GetTopKUsedPercentage() const { return topKUsedPercentage_; }

 private:
  std::string method_name_;    // Method name.
  uint32_t count_;             // Number of times it has been called.
  uint32_t method_size_;       // Size of the method on dex instructions.
  double usedPercent_;         // Percentage of how many times this method was called.
  double topKUsedPercentage_;  // The percentage of the group that comprise K% of the total used
                               // methods this methods belongs to.
};

// Profile data is stored in a map, indexed by the full method name.
typedef std::map<std::string, ProfileData> ProfileMap;

class ProfileHelper {
 private:
  ProfileHelper();

 public:
  // Read the profile data from the given file.  Calculates the percentage for each method.
  // Returns false if there was no profile file or it was malformed.
  static bool LoadProfileMap(ProfileMap& profileMap, const std::string& fileName);

  // Read the profile data from the given file and computes the group that comprise
  // topKPercentage of the total used methods.
  static bool LoadTopKSamples(std::set<std::string>& topKMethods, const std::string& fileName,
                              double topKPercentage);
};

}  // namespace art

#endif  // ART_RUNTIME_PROFILER_H_