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