• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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_JIT_PROFILE_SAVER_H_
18 #define ART_RUNTIME_JIT_PROFILE_SAVER_H_
19 
20 #include "base/mutex.h"
21 #include "base/safe_map.h"
22 #include "dex/method_reference.h"
23 #include "jit_code_cache.h"
24 #include "profile/profile_compilation_info.h"
25 #include "profile_saver_options.h"
26 
27 namespace art {
28 
29 class ProfileSaver {
30  public:
31   // Starts the profile saver thread if not already started.
32   // If the saver is already running it adds (output_filename, code_paths) to its tracked locations.
33   //
34   // The `ref_profile_filename` denotes the path to the reference profile which
35   // might be queried to determine if an initial save should be done earlier.
36   // It can be empty indicating there is no reference profile.
37   static void Start(const ProfileSaverOptions& options,
38                     const std::string& output_filename,
39                     jit::JitCodeCache* jit_code_cache,
40                     const std::vector<std::string>& code_paths,
41                     const std::string& ref_profile_filename)
42       REQUIRES(!Locks::profiler_lock_, !instance_->wait_lock_);
43 
44   // Stops the profile saver thread.
45   static void Stop(bool dump_info_) REQUIRES(!Locks::profiler_lock_, !instance_->wait_lock_);
46 
47   // Returns true if the profile saver is started.
48   static bool IsStarted() REQUIRES(!Locks::profiler_lock_);
49 
50   // If the profile saver is running, dumps statistics to the `os`. Otherwise it does nothing.
51   static void DumpInstanceInfo(std::ostream& os);
52 
53   static void NotifyJitActivity() REQUIRES(!Locks::profiler_lock_, !instance_->wait_lock_);
54 
55   // For testing or manual purposes (SIGUSR1).
56   static void ForceProcessProfiles() REQUIRES(!Locks::profiler_lock_, !Locks::mutator_lock_);
57 
58   // Notify that startup has completed.
59   static void NotifyStartupCompleted() REQUIRES(!Locks::profiler_lock_, !instance_->wait_lock_);
60 
61  private:
62   // Helper classes for collecting classes and methods.
63   class GetClassesAndMethodsHelper;
64   class ScopedDefaultPriority;
65 
66   ProfileSaver(const ProfileSaverOptions& options, jit::JitCodeCache* jit_code_cache);
67   ~ProfileSaver();
68 
69   static void* RunProfileSaverThread(void* arg)
70       REQUIRES(!Locks::profiler_lock_, !instance_->wait_lock_);
71 
72   // The run loop for the saver.
73   void Run()
74       REQUIRES(Locks::profiler_lock_, !wait_lock_)
75       RELEASE(Locks::profiler_lock_);
76 
77   // Processes the existing profiling info from the jit code cache and returns
78   // true if it needed to be saved to disk.
79   // If number_of_new_methods is not null, after the call it will contain the number of new methods
80   // written to disk.
81   // If force_save is true, the saver will ignore any constraints which limit IO (e.g. will write
82   // the profile to disk even if it's just one new method).
83   bool ProcessProfilingInfo(
84         bool force_save,
85         bool skip_class_and_method_fetching,
86         /*out*/uint16_t* number_of_new_methods)
87       REQUIRES(!Locks::profiler_lock_)
88       REQUIRES(!Locks::mutator_lock_);
89 
90   void NotifyJitActivityInternal() REQUIRES(!wait_lock_);
91   void WakeUpSaver() REQUIRES(wait_lock_);
92 
93   // Returns true if the saver is shutting down (ProfileSaver::Stop() has been called).
94   bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_);
95 
96   void AddTrackedLocations(const std::string& output_filename,
97                            const std::vector<std::string>& code_paths,
98                            const std::string& ref_profile_filename)
99       REQUIRES(Locks::profiler_lock_);
100 
101   // Fetches the current resolved classes and methods from the ClassLinker and stores them in the
102   // profile_cache_ for later save.
103   void FetchAndCacheResolvedClassesAndMethods(bool startup) REQUIRES(!Locks::profiler_lock_);
104 
105   void DumpInfo(std::ostream& os);
106 
107   // Resolve the realpath of the locations stored in tracked_dex_base_locations_to_be_resolved_
108   // and put the result in tracked_dex_base_locations_.
109   void ResolveTrackedLocations() REQUIRES(!Locks::profiler_lock_);
110 
111   // Get the profile metadata that should be associated with the profile session during the current
112   // profile saver session.
113   ProfileCompilationInfo::ProfileSampleAnnotation GetProfileSampleAnnotation();
114 
115   // Get extra global flags if necessary (e.g. the running architecture), otherwise 0.
116   static uint32_t GetExtraMethodHotnessFlags(const ProfileSaverOptions& options);
117 
118   // Extends the given set of flags with global flags if necessary (e.g. the running architecture).
119   ProfileCompilationInfo::MethodHotness::Flag AnnotateSampleFlags(uint32_t flags);
120 
121   // The only instance of the saver.
122   static ProfileSaver* instance_ GUARDED_BY(Locks::profiler_lock_);
123   // Profile saver thread.
124   static pthread_t profiler_pthread_ GUARDED_BY(Locks::profiler_lock_);
125 
126   jit::JitCodeCache* jit_code_cache_;
127 
128   // Collection of code paths that the profiler tracks.
129   // It maps profile locations to code paths (dex base locations).
130   SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_
131       GUARDED_BY(Locks::profiler_lock_);
132 
133   // Collection of code paths that the profiler tracks but may note have been resolved
134   // to their realpath. The resolution is done async to minimize the time it takes for
135   // someone to register a path.
136   SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_to_be_resolved_
137       GUARDED_BY(Locks::profiler_lock_);
138 
139   // Collection of output profiles that the profile tracks.
140   // It maps output profile locations to reference profiles, and is used
141   // to determine if any profile is non-empty at the start of the ProfileSaver.
142   // This influences the time of the first ever save.
143   SafeMap<std::string, std::string> tracked_profiles_
144       GUARDED_BY(Locks::profiler_lock_);
145 
146   bool shutting_down_ GUARDED_BY(Locks::profiler_lock_);
147   uint64_t last_time_ns_saver_woke_up_ GUARDED_BY(wait_lock_);
148   uint32_t jit_activity_notifications_;
149 
150   // A local cache for the profile information. Maps each tracked file to its
151   // profile information. This is used to cache the startup classes so that
152   // we don't hammer the disk to save them right away.
153   // The size of this cache is usually very small and tops
154   // to just a few hundreds entries in the ProfileCompilationInfo objects.
155   SafeMap<std::string, ProfileCompilationInfo*> profile_cache_ GUARDED_BY(Locks::profiler_lock_);
156 
157   // Whether or not this is the first ever profile save.
158   // Note this is an approximation and is not 100% precise. It relies on checking
159   // whether or not the profiles are empty which is not a precise indication
160   // of being the first save (they could have been cleared in the meantime).
161   bool IsFirstSave() REQUIRES(!Locks::profiler_lock_);
162 
163   // Save period condition support.
164   Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
165   ConditionVariable period_condition_ GUARDED_BY(wait_lock_);
166 
167   uint64_t total_bytes_written_;
168   uint64_t total_number_of_writes_;
169   uint64_t total_number_of_code_cache_queries_;
170   uint64_t total_number_of_skipped_writes_;
171   uint64_t total_number_of_failed_writes_;
172   uint64_t total_ms_of_sleep_;
173   uint64_t total_ns_of_work_;
174   // TODO(calin): replace with an actual size.
175   uint64_t total_number_of_hot_spikes_;
176   uint64_t total_number_of_wake_ups_;
177 
178   const ProfileSaverOptions options_;
179 
180   friend class ProfileSaverTest;
181   friend class ProfileSaverForBootTest;
182 
183   DISALLOW_COPY_AND_ASSIGN(ProfileSaver);
184 };
185 
186 }  // namespace art
187 
188 #endif  // ART_RUNTIME_JIT_PROFILE_SAVER_H_
189