10aded089f565008ba5908e395e5914ca4f91f2deDave Allison/*
20aded089f565008ba5908e395e5914ca4f91f2deDave Allison * Copyright (C) 2011 The Android Open Source Project
30aded089f565008ba5908e395e5914ca4f91f2deDave Allison *
40aded089f565008ba5908e395e5914ca4f91f2deDave Allison * Licensed under the Apache License, Version 2.0 (the "License");
50aded089f565008ba5908e395e5914ca4f91f2deDave Allison * you may not use this file except in compliance with the License.
60aded089f565008ba5908e395e5914ca4f91f2deDave Allison * You may obtain a copy of the License at
70aded089f565008ba5908e395e5914ca4f91f2deDave Allison *
80aded089f565008ba5908e395e5914ca4f91f2deDave Allison *      http://www.apache.org/licenses/LICENSE-2.0
90aded089f565008ba5908e395e5914ca4f91f2deDave Allison *
100aded089f565008ba5908e395e5914ca4f91f2deDave Allison * Unless required by applicable law or agreed to in writing, software
110aded089f565008ba5908e395e5914ca4f91f2deDave Allison * distributed under the License is distributed on an "AS IS" BASIS,
120aded089f565008ba5908e395e5914ca4f91f2deDave Allison * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130aded089f565008ba5908e395e5914ca4f91f2deDave Allison * See the License for the specific language governing permissions and
140aded089f565008ba5908e395e5914ca4f91f2deDave Allison * limitations under the License.
150aded089f565008ba5908e395e5914ca4f91f2deDave Allison */
160aded089f565008ba5908e395e5914ca4f91f2deDave Allison
170aded089f565008ba5908e395e5914ca4f91f2deDave Allison#ifndef ART_RUNTIME_PROFILER_H_
180aded089f565008ba5908e395e5914ca4f91f2deDave Allison#define ART_RUNTIME_PROFILER_H_
190aded089f565008ba5908e395e5914ca4f91f2deDave Allison
20700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers#include <memory>
210aded089f565008ba5908e395e5914ca4f91f2deDave Allison#include <ostream>
220aded089f565008ba5908e395e5914ca4f91f2deDave Allison#include <set>
230aded089f565008ba5908e395e5914ca4f91f2deDave Allison#include <string>
240aded089f565008ba5908e395e5914ca4f91f2deDave Allison#include <vector>
250aded089f565008ba5908e395e5914ca4f91f2deDave Allison
26719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers#include "barrier.h"
270aded089f565008ba5908e395e5914ca4f91f2deDave Allison#include "base/macros.h"
28719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers#include "base/mutex.h"
290aded089f565008ba5908e395e5914ca4f91f2deDave Allison#include "globals.h"
300aded089f565008ba5908e395e5914ca4f91f2deDave Allison#include "instrumentation.h"
31c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle#include "profiler_options.h"
320aded089f565008ba5908e395e5914ca4f91f2deDave Allison#include "os.h"
330aded089f565008ba5908e395e5914ca4f91f2deDave Allison#include "safe_map.h"
34445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin#include "method_reference.h"
350aded089f565008ba5908e395e5914ca4f91f2deDave Allison
360aded089f565008ba5908e395e5914ca4f91f2deDave Allisonnamespace art {
370aded089f565008ba5908e395e5914ca4f91f2deDave Allison
380aded089f565008ba5908e395e5914ca4f91f2deDave Allisonnamespace mirror {
390aded089f565008ba5908e395e5914ca4f91f2deDave Allison  class Class;
400aded089f565008ba5908e395e5914ca4f91f2deDave Allison}  // namespace mirror
41e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierclass ArtMethod;
420aded089f565008ba5908e395e5914ca4f91f2deDave Allisonclass Thread;
430aded089f565008ba5908e395e5914ca4f91f2deDave Allison
44e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartiertypedef std::pair<ArtMethod*, uint32_t> InstructionLocation;
45445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin
46445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin// This class stores the sampled bounded stacks in a trie structure. A path of the trie represents
47445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin// a particular context with the method on top of the stack being a leaf or an internal node of the
48445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin// trie rather than the root.
49445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jinclass StackTrieNode {
50445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin public:
51445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  StackTrieNode(MethodReference method, uint32_t dex_pc, uint32_t method_size,
52445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin      StackTrieNode* parent) :
53445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin      parent_(parent), method_(method), dex_pc_(dex_pc),
54445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin      count_(0), method_size_(method_size) {
55445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  }
56445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  StackTrieNode() : parent_(nullptr), method_(nullptr, 0),
57445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin      dex_pc_(0), count_(0), method_size_(0) {
58445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  }
59445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  StackTrieNode* GetParent() { return parent_; }
60445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  MethodReference GetMethod() { return method_; }
61445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  uint32_t GetCount() { return count_; }
62445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  uint32_t GetDexPC() { return dex_pc_; }
63445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  uint32_t GetMethodSize() { return method_size_; }
64445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  void AppendChild(StackTrieNode* child) { children_.insert(child); }
65445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  StackTrieNode* FindChild(MethodReference method, uint32_t dex_pc);
66445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  void DeleteChildren();
67445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  void IncreaseCount() { ++count_; }
68445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin
69445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin private:
70445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  // Comparator for stack trie node.
71445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  struct StackTrieNodeComparator {
72445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin    bool operator()(StackTrieNode* node1, StackTrieNode* node2) const {
73445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin      MethodReference mr1 = node1->GetMethod();
74445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin      MethodReference mr2 = node2->GetMethod();
75445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin      if (mr1.dex_file == mr2.dex_file) {
76445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin        if (mr1.dex_method_index == mr2.dex_method_index) {
77445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin          return node1->GetDexPC() < node2->GetDexPC();
78445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin        } else {
79445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin          return mr1.dex_method_index < mr2.dex_method_index;
80445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin        }
81445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin      } else {
82445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin        return mr1.dex_file < mr2.dex_file;
83445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin      }
84445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin    }
85445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  };
86445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin
87445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  std::set<StackTrieNode*, StackTrieNodeComparator> children_;
88445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  StackTrieNode* parent_;
89445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  MethodReference method_;
90445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  uint32_t dex_pc_;
91445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  uint32_t count_;
92445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  uint32_t method_size_;
93445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin};
94445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin
950aded089f565008ba5908e395e5914ca4f91f2deDave Allison//
960aded089f565008ba5908e395e5914ca4f91f2deDave Allison// This class holds all the results for all runs of the profiler.  It also
970aded089f565008ba5908e395e5914ca4f91f2deDave Allison// counts the number of null methods (where we can't determine the method) and
980aded089f565008ba5908e395e5914ca4f91f2deDave Allison// the number of methods in the boot path (where we have already compiled the method).
990aded089f565008ba5908e395e5914ca4f91f2deDave Allison//
1000aded089f565008ba5908e395e5914ca4f91f2deDave Allison// This object is an internal profiler object and uses the same locking as the profiler
1010aded089f565008ba5908e395e5914ca4f91f2deDave Allison// itself.
1020aded089f565008ba5908e395e5914ca4f91f2deDave Allisonclass ProfileSampleResults {
1030aded089f565008ba5908e395e5914ca4f91f2deDave Allison public:
1040aded089f565008ba5908e395e5914ca4f91f2deDave Allison  explicit ProfileSampleResults(Mutex& lock);
1050aded089f565008ba5908e395e5914ca4f91f2deDave Allison  ~ProfileSampleResults();
1060aded089f565008ba5908e395e5914ca4f91f2deDave Allison
10790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void Put(ArtMethod* method) REQUIRES(!lock_);
10890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void PutStack(const std::vector<InstructionLocation>& stack_dump) REQUIRES(!lock_);
109a93b0bb9abfbd19fc40ff8ced8818aec1a3464b9Wei Jin  uint32_t Write(std::ostream &os, ProfileDataType type);
110a93b0bb9abfbd19fc40ff8ced8818aec1a3464b9Wei Jin  void ReadPrevious(int fd, ProfileDataType type);
1110aded089f565008ba5908e395e5914ca4f91f2deDave Allison  void Clear();
1120aded089f565008ba5908e395e5914ca4f91f2deDave Allison  uint32_t GetNumSamples() { return num_samples_; }
1130aded089f565008ba5908e395e5914ca4f91f2deDave Allison  void NullMethod() { ++num_null_methods_; }
1140aded089f565008ba5908e395e5914ca4f91f2deDave Allison  void BootMethod() { ++num_boot_methods_; }
11539c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison
1160aded089f565008ba5908e395e5914ca4f91f2deDave Allison private:
117e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uint32_t Hash(ArtMethod* method);
1180aded089f565008ba5908e395e5914ca4f91f2deDave Allison  static constexpr int kHashSize = 17;
119c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  Mutex& lock_;                  // Reference to the main profiler lock - we don't need two of them.
120c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  uint32_t num_samples_;         // Total number of samples taken.
121c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  uint32_t num_null_methods_;    // Number of samples where can don't know the method.
122c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  uint32_t num_boot_methods_;    // Number of samples in the boot path.
1230aded089f565008ba5908e395e5914ca4f91f2deDave Allison
124e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  typedef std::map<ArtMethod*, uint32_t> Map;  // Map of method vs its count.
1250aded089f565008ba5908e395e5914ca4f91f2deDave Allison  Map *table[kHashSize];
12639c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison
127445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  typedef std::set<StackTrieNode*> TrieNodeSet;
128445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  // Map of method hit by profiler vs the set of stack trie nodes for this method.
129445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  typedef std::map<MethodReference, TrieNodeSet*, MethodReferenceComparator> MethodContextMap;
130445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  MethodContextMap *method_context_table;
131445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  StackTrieNode* stack_trie_root_;  // Root of the trie that stores sampled stack information.
132a93b0bb9abfbd19fc40ff8ced8818aec1a3464b9Wei Jin
133445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  // Map from <pc, context> to counts.
134445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  typedef std::map<std::pair<uint32_t, std::string>, uint32_t> PreviousContextMap;
13539c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  struct PreviousValue {
136445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin    PreviousValue() : count_(0), method_size_(0), context_map_(nullptr) {}
137445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin    PreviousValue(uint32_t count, uint32_t method_size, PreviousContextMap* context_map)
138445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin      : count_(count), method_size_(method_size), context_map_(context_map) {}
13939c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison    uint32_t count_;
14039c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison    uint32_t method_size_;
141445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin    PreviousContextMap* context_map_;
14239c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  };
14339c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison
14439c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  typedef std::map<std::string, PreviousValue> PreviousProfile;
14539c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  PreviousProfile previous_;
14639c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  uint32_t previous_num_samples_;
14739c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  uint32_t previous_num_null_methods_;     // Number of samples where can don't know the method.
14839c3bfbd03d85c63cfbe69f17ce5800ccc7d6c13Dave Allison  uint32_t previous_num_boot_methods_;     // Number of samples in the boot path.
1490aded089f565008ba5908e395e5914ca4f91f2deDave Allison};
1500aded089f565008ba5908e395e5914ca4f91f2deDave Allison
1510aded089f565008ba5908e395e5914ca4f91f2deDave Allison//
1520aded089f565008ba5908e395e5914ca4f91f2deDave Allison// The BackgroundMethodSamplingProfiler runs in a thread.  Most of the time it is sleeping but
1530aded089f565008ba5908e395e5914ca4f91f2deDave Allison// occasionally wakes up and counts the number of times a method is called.  Each time
1540aded089f565008ba5908e395e5914ca4f91f2deDave Allison// it ticks, it looks at the current method and records it in the ProfileSampleResults
1550aded089f565008ba5908e395e5914ca4f91f2deDave Allison// table.
1560aded089f565008ba5908e395e5914ca4f91f2deDave Allison//
1570aded089f565008ba5908e395e5914ca4f91f2deDave Allison// The timing is controlled by a number of variables:
1580aded089f565008ba5908e395e5914ca4f91f2deDave Allison// 1.  Period: the time between sampling runs.
1590aded089f565008ba5908e395e5914ca4f91f2deDave Allison// 2.  Interval: the time between each sample in a run.
1600aded089f565008ba5908e395e5914ca4f91f2deDave Allison// 3.  Duration: the duration of a run.
1610aded089f565008ba5908e395e5914ca4f91f2deDave Allison//
1620aded089f565008ba5908e395e5914ca4f91f2deDave Allison// So the profiler thread is sleeping for the 'period' time.  It wakes up and runs for the
1630aded089f565008ba5908e395e5914ca4f91f2deDave Allison// 'duration'.  The run consists of a series of samples, each of which is 'interval' microseconds
1640aded089f565008ba5908e395e5914ca4f91f2deDave Allison// apart.  At the end of a run, it writes the results table to a file and goes back to sleep.
1650aded089f565008ba5908e395e5914ca4f91f2deDave Allison
1660aded089f565008ba5908e395e5914ca4f91f2deDave Allisonclass BackgroundMethodSamplingProfiler {
1670aded089f565008ba5908e395e5914ca4f91f2deDave Allison public:
168c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  // Start a profile thread with the user-supplied arguments.
169c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  // Returns true if the profile was started or if it was already running. Returns false otherwise.
170c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  static bool Start(const std::string& output_filename, const ProfilerOptions& options)
17190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      REQUIRES(!Locks::mutator_lock_, !Locks::thread_list_lock_, !Locks::thread_suspend_count_lock_,
17290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier               !Locks::profiler_lock_);
17390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier
17490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock.
17590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  static void Stop() REQUIRES(!Locks::profiler_lock_, !wait_lock_, !Locks::profiler_lock_)
17690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      NO_THREAD_SAFETY_ANALYSIS;
17790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock.
17890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  static void Shutdown() REQUIRES(!Locks::profiler_lock_) NO_THREAD_SAFETY_ANALYSIS;
17990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier
18090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void RecordMethod(ArtMethod *method) SHARED_REQUIRES(Locks::mutator_lock_);
18190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  void RecordStack(const std::vector<InstructionLocation>& stack)
18290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_);
18390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool ProcessMethod(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_);
184445220d4fe5bd9f0616d6da2322a7b6e9466595eWei Jin  const ProfilerOptions& GetProfilerOptions() const { return options_; }
1850aded089f565008ba5908e395e5914ca4f91f2deDave Allison
1860aded089f565008ba5908e395e5914ca4f91f2deDave Allison  Barrier& GetBarrier() {
1870aded089f565008ba5908e395e5914ca4f91f2deDave Allison    return *profiler_barrier_;
1880aded089f565008ba5908e395e5914ca4f91f2deDave Allison  }
1890aded089f565008ba5908e395e5914ca4f91f2deDave Allison
1900aded089f565008ba5908e395e5914ca4f91f2deDave Allison private:
191c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  explicit BackgroundMethodSamplingProfiler(
192c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle    const std::string& output_filename, const ProfilerOptions& options);
1930aded089f565008ba5908e395e5914ca4f91f2deDave Allison
1940aded089f565008ba5908e395e5914ca4f91f2deDave Allison  // The sampling interval in microseconds is passed as an argument.
19590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock.
19690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  static void* RunProfilerThread(void* arg) REQUIRES(!Locks::profiler_lock_)
19790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      NO_THREAD_SAFETY_ANALYSIS;
1980aded089f565008ba5908e395e5914ca4f91f2deDave Allison
19990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  uint32_t WriteProfile() SHARED_REQUIRES(Locks::mutator_lock_);
2000aded089f565008ba5908e395e5914ca4f91f2deDave Allison
2010aded089f565008ba5908e395e5914ca4f91f2deDave Allison  void CleanProfile();
20290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  uint32_t DumpProfile(std::ostream& os) SHARED_REQUIRES(Locks::mutator_lock_);
20390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  static bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_);
2040aded089f565008ba5908e395e5914ca4f91f2deDave Allison
2050aded089f565008ba5908e395e5914ca4f91f2deDave Allison  static BackgroundMethodSamplingProfiler* profiler_ GUARDED_BY(Locks::profiler_lock_);
2060aded089f565008ba5908e395e5914ca4f91f2deDave Allison
2070aded089f565008ba5908e395e5914ca4f91f2deDave Allison  // We need to shut the sample thread down at exit.  Setting this to true will do that.
2080aded089f565008ba5908e395e5914ca4f91f2deDave Allison  static volatile bool shutting_down_ GUARDED_BY(Locks::profiler_lock_);
2090aded089f565008ba5908e395e5914ca4f91f2deDave Allison
2100aded089f565008ba5908e395e5914ca4f91f2deDave Allison  // Sampling thread, non-zero when sampling.
2110aded089f565008ba5908e395e5914ca4f91f2deDave Allison  static pthread_t profiler_pthread_;
2120aded089f565008ba5908e395e5914ca4f91f2deDave Allison
213c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  // Some measure of the number of samples that are significant.
2140aded089f565008ba5908e395e5914ca4f91f2deDave Allison  static constexpr uint32_t kSignificantSamples = 10;
2150aded089f565008ba5908e395e5914ca4f91f2deDave Allison
216c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  // The name of the file where profile data will be written.
217c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  std::string output_filename_;
218c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  // The options used to start the profiler.
219c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  const ProfilerOptions& options_;
2200aded089f565008ba5908e395e5914ca4f91f2deDave Allison
2210aded089f565008ba5908e395e5914ca4f91f2deDave Allison
2220aded089f565008ba5908e395e5914ca4f91f2deDave Allison  // Profile condition support.
2230aded089f565008ba5908e395e5914ca4f91f2deDave Allison  Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
2240aded089f565008ba5908e395e5914ca4f91f2deDave Allison  ConditionVariable period_condition_ GUARDED_BY(wait_lock_);
2250aded089f565008ba5908e395e5914ca4f91f2deDave Allison
2260aded089f565008ba5908e395e5914ca4f91f2deDave Allison  ProfileSampleResults profile_table_;
2270aded089f565008ba5908e395e5914ca4f91f2deDave Allison
228700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Barrier> profiler_barrier_;
2290aded089f565008ba5908e395e5914ca4f91f2deDave Allison
2300aded089f565008ba5908e395e5914ca4f91f2deDave Allison  // Set of methods to be filtered out.  This will probably be rare because
2310aded089f565008ba5908e395e5914ca4f91f2deDave Allison  // most of the methods we want to be filtered reside in the boot path and
2320aded089f565008ba5908e395e5914ca4f91f2deDave Allison  // are automatically filtered.
2330aded089f565008ba5908e395e5914ca4f91f2deDave Allison  typedef std::set<std::string> FilteredMethods;
2340aded089f565008ba5908e395e5914ca4f91f2deDave Allison  FilteredMethods filtered_methods_;
2350aded089f565008ba5908e395e5914ca4f91f2deDave Allison
2360aded089f565008ba5908e395e5914ca4f91f2deDave Allison  DISALLOW_COPY_AND_ASSIGN(BackgroundMethodSamplingProfiler);
2370aded089f565008ba5908e395e5914ca4f91f2deDave Allison};
2380aded089f565008ba5908e395e5914ca4f91f2deDave Allison
239c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle//
240bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle// Contains profile data generated from previous runs of the program and stored
2419dae5b4b952824da45e9fd9c12cfcde9858f0974Calin Juravle// in a file.  It is used to determine whether to compile a particular method or not.
242bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravleclass ProfileFile {
2439dae5b4b952824da45e9fd9c12cfcde9858f0974Calin Juravle public:
244bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  class ProfileData {
245bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle   public:
246aa50d3a01a4137cd89c152be5487fed23683d0ffSebastien Hertz    ProfileData() : count_(0), method_size_(0), used_percent_(0), top_k_used_percentage_(0) {}
247bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle    ProfileData(const std::string& method_name, uint32_t count, uint32_t method_size,
248bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle      double used_percent, double top_k_used_percentage) :
249bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle      method_name_(method_name), count_(count), method_size_(method_size),
250bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle      used_percent_(used_percent), top_k_used_percentage_(top_k_used_percentage) {
251bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle      // TODO: currently method_size_ is unused
252bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle      UNUSED(method_size_);
253bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle    }
254bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle
255bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle    double GetUsedPercent() const { return used_percent_; }
256bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle    uint32_t GetCount() const { return count_; }
257bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle    double GetTopKUsedPercentage() const { return top_k_used_percentage_; }
258bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle
259bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle   private:
260bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle    std::string method_name_;       // Method name.
261bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle    uint32_t count_;                // Number of times it has been called.
262bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle    uint32_t method_size_;          // Size of the method on dex instructions.
263bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle    double used_percent_;           // Percentage of how many times this method was called.
264c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle    double top_k_used_percentage_;  // The percentage of the group that comprise K% of the total
265c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle                                    // used methods this methods belongs to.
266bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  };
2679dae5b4b952824da45e9fd9c12cfcde9858f0974Calin Juravle
268bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle public:
269c1b643cc6ac45dbd0eabdcd7425c7e86006c27d6Calin Juravle  // Loads profile data from the given file. The new data are merged with any existing data.
270bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  // Returns true if the file was loaded successfully and false otherwise.
271bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  bool LoadFile(const std::string& filename);
2729dae5b4b952824da45e9fd9c12cfcde9858f0974Calin Juravle
273bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  // Computes the group that comprise top_k_percentage of the total used methods.
274bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  bool GetTopKSamples(std::set<std::string>& top_k_methods, double top_k_percentage);
2759dae5b4b952824da45e9fd9c12cfcde9858f0974Calin Juravle
276bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  // If the given method has an entry in the profile table it updates the data
277bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  // and returns true. Otherwise returns false and leaves the data unchanged.
278bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  bool GetProfileData(ProfileData* data, const std::string& method_name);
2799dae5b4b952824da45e9fd9c12cfcde9858f0974Calin Juravle
2809dae5b4b952824da45e9fd9c12cfcde9858f0974Calin Juravle private:
281bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  // Profile data is stored in a map, indexed by the full method name.
282bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  typedef std::map<std::string, ProfileData> ProfileMap;
283bb0b53f58f11c628f077603b56077dfed1a18f11Calin Juravle  ProfileMap profile_map_;
2849dae5b4b952824da45e9fd9c12cfcde9858f0974Calin Juravle};
2859dae5b4b952824da45e9fd9c12cfcde9858f0974Calin Juravle
2860aded089f565008ba5908e395e5914ca4f91f2deDave Allison}  // namespace art
2870aded089f565008ba5908e395e5914ca4f91f2deDave Allison
2880aded089f565008ba5908e395e5914ca4f91f2deDave Allison#endif  // ART_RUNTIME_PROFILER_H_
289