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   static void Start(const ProfileSaverOptions& options,
34                     const std::string& output_filename,
35                     jit::JitCodeCache* jit_code_cache,
36                     const std::vector<std::string>& code_paths)
37       REQUIRES(!Locks::profiler_lock_, !instance_->wait_lock_);
38 
39   // Stops the profile saver thread.
40   static void Stop(bool dump_info_) REQUIRES(!Locks::profiler_lock_, !instance_->wait_lock_);
41 
42   // Returns true if the profile saver is started.
43   static bool IsStarted() REQUIRES(!Locks::profiler_lock_);
44 
45   // If the profile saver is running, dumps statistics to the `os`. Otherwise it does nothing.
46   static void DumpInstanceInfo(std::ostream& os);
47 
48   static void NotifyJitActivity() REQUIRES(!Locks::profiler_lock_, !instance_->wait_lock_);
49 
50   // For testing or manual purposes (SIGUSR1).
51   static void ForceProcessProfiles() REQUIRES(!Locks::profiler_lock_, !Locks::mutator_lock_);
52 
53   // Just for testing purposes.
54   static bool HasSeenMethod(const std::string& profile, bool hot, MethodReference ref)
55       REQUIRES(!Locks::profiler_lock_);
56 
57   // Notify that startup has completed.
58   static void NotifyStartupCompleted() REQUIRES(!Locks::profiler_lock_, !instance_->wait_lock_);
59 
60  private:
61   ProfileSaver(const ProfileSaverOptions& options,
62                const std::string& output_filename,
63                jit::JitCodeCache* jit_code_cache,
64                const std::vector<std::string>& code_paths);
65   ~ProfileSaver();
66 
67   static void* RunProfileSaverThread(void* arg)
68       REQUIRES(!Locks::profiler_lock_, !instance_->wait_lock_);
69 
70   // The run loop for the saver.
71   void Run()
72       REQUIRES(Locks::profiler_lock_, !wait_lock_)
73       RELEASE(Locks::profiler_lock_);
74 
75   // Processes the existing profiling info from the jit code cache and returns
76   // true if it needed to be saved to disk.
77   // If number_of_new_methods is not null, after the call it will contain the number of new methods
78   // written to disk.
79   // If force_save is true, the saver will ignore any constraints which limit IO (e.g. will write
80   // the profile to disk even if it's just one new method).
81   bool ProcessProfilingInfo(bool force_save, /*out*/uint16_t* number_of_new_methods)
82       REQUIRES(!Locks::profiler_lock_)
83       REQUIRES(!Locks::mutator_lock_);
84 
85   void NotifyJitActivityInternal() REQUIRES(!wait_lock_);
86   void WakeUpSaver() REQUIRES(wait_lock_);
87 
88   // Returns true if the saver is shutting down (ProfileSaver::Stop() has been called).
89   bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_);
90 
91   void AddTrackedLocations(const std::string& output_filename,
92                            const std::vector<std::string>& code_paths)
93       REQUIRES(Locks::profiler_lock_);
94 
95   // Fetches the current resolved classes and methods from the ClassLinker and stores them in the
96   // profile_cache_ for later save.
97   void FetchAndCacheResolvedClassesAndMethods(bool startup) REQUIRES(!Locks::profiler_lock_);
98 
99   void DumpInfo(std::ostream& os);
100 
101   // Resolve the realpath of the locations stored in tracked_dex_base_locations_to_be_resolved_
102   // and put the result in tracked_dex_base_locations_.
103   void ResolveTrackedLocations() REQUIRES(!Locks::profiler_lock_);
104 
105   // Get the profile metadata that should be associated with the profile session during the current
106   // profile saver session.
107   ProfileCompilationInfo::ProfileSampleAnnotation GetProfileSampleAnnotation();
108 
109   // Extends the given set of flags with global flags if necessary (e.g. the running architecture).
110   ProfileCompilationInfo::MethodHotness::Flag AnnotateSampleFlags(uint32_t flags);
111 
112   // The only instance of the saver.
113   static ProfileSaver* instance_ GUARDED_BY(Locks::profiler_lock_);
114   // Profile saver thread.
115   static pthread_t profiler_pthread_ GUARDED_BY(Locks::profiler_lock_);
116 
117   jit::JitCodeCache* jit_code_cache_;
118 
119   // Collection of code paths that the profiler tracks.
120   // It maps profile locations to code paths (dex base locations).
121   SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_
122       GUARDED_BY(Locks::profiler_lock_);
123 
124   // Collection of code paths that the profiler tracks but may note have been resolved
125   // to their realpath. The resolution is done async to minimize the time it takes for
126   // someone to register a path.
127   SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_to_be_resolved_
128       GUARDED_BY(Locks::profiler_lock_);
129 
130   bool shutting_down_ GUARDED_BY(Locks::profiler_lock_);
131   uint64_t last_time_ns_saver_woke_up_ GUARDED_BY(wait_lock_);
132   uint32_t jit_activity_notifications_;
133 
134   // A local cache for the profile information. Maps each tracked file to its
135   // profile information. This is used to cache the startup classes so that
136   // we don't hammer the disk to save them right away.
137   // The size of this cache is usually very small and tops
138   // to just a few hundreds entries in the ProfileCompilationInfo objects.
139   SafeMap<std::string, ProfileCompilationInfo*> profile_cache_;
140 
141   // Save period condition support.
142   Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
143   ConditionVariable period_condition_ GUARDED_BY(wait_lock_);
144 
145   uint64_t total_bytes_written_;
146   uint64_t total_number_of_writes_;
147   uint64_t total_number_of_code_cache_queries_;
148   uint64_t total_number_of_skipped_writes_;
149   uint64_t total_number_of_failed_writes_;
150   uint64_t total_ms_of_sleep_;
151   uint64_t total_ns_of_work_;
152   // TODO(calin): replace with an actual size.
153   uint64_t total_number_of_hot_spikes_;
154   uint64_t total_number_of_wake_ups_;
155 
156   const ProfileSaverOptions options_;
157 
158   friend class ProfileSaverTest;
159   friend class ProfileSaverForBootTest;
160 
161   DISALLOW_COPY_AND_ASSIGN(ProfileSaver);
162 };
163 
164 }  // namespace art
165 
166 #endif  // ART_RUNTIME_JIT_PROFILE_SAVER_H_
167