profile_saver.h revision e5de54cfab5f14ba0b8ff25d8d60901c7021943f
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 "jit_code_cache.h" 22#include "offline_profiling_info.h" 23#include "safe_map.h" 24 25namespace art { 26 27class ProfileSaver { 28 public: 29 // Starts the profile saver thread if not already started. 30 // If the saver is already running it adds (output_filename, code_paths) to its tracked locations. 31 static void Start(const std::string& output_filename, 32 jit::JitCodeCache* jit_code_cache, 33 const std::vector<std::string>& code_paths, 34 const std::string& foreign_dex_profile_path, 35 const std::string& app_data_dir) 36 REQUIRES(!Locks::profiler_lock_, !wait_lock_); 37 38 // Stops the profile saver thread. 39 // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock. 40 static void Stop(bool dump_info_) 41 REQUIRES(!Locks::profiler_lock_, !wait_lock_) 42 NO_THREAD_SAFETY_ANALYSIS; 43 44 // Returns true if the profile saver is started. 45 static bool IsStarted() REQUIRES(!Locks::profiler_lock_); 46 47 static void NotifyDexUse(const std::string& dex_location); 48 49 // If the profile saver is running, dumps statistics to the `os`. Otherwise it does nothing. 50 static void DumpInstanceInfo(std::ostream& os); 51 52 // Just for testing purpose. 53 static void ForceProcessProfiles(); 54 static bool HasSeenMethod(const std::string& profile, 55 const DexFile* dex_file, 56 uint16_t method_idx); 57 58 private: 59 ProfileSaver(const std::string& output_filename, 60 jit::JitCodeCache* jit_code_cache, 61 const std::vector<std::string>& code_paths, 62 const std::string& foreign_dex_profile_path, 63 const std::string& app_data_dir); 64 65 // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock. 66 static void* RunProfileSaverThread(void* arg) 67 REQUIRES(!Locks::profiler_lock_, !wait_lock_) 68 NO_THREAD_SAFETY_ANALYSIS; 69 70 // The run loop for the saver. 71 void Run() REQUIRES(!Locks::profiler_lock_, !wait_lock_); 72 // Processes the existing profiling info from the jit code cache and returns 73 // true if it needed to be saved to disk. 74 bool ProcessProfilingInfo() 75 REQUIRES(!Locks::profiler_lock_) 76 REQUIRES(!Locks::mutator_lock_); 77 78 // Returns true if the saver is shutting down (ProfileSaver::Stop() has been called). 79 bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_); 80 81 void AddTrackedLocations(const std::string& output_filename, 82 const std::string& app_data_dir, 83 const std::vector<std::string>& code_paths) 84 REQUIRES(Locks::profiler_lock_); 85 86 // Retrieves the cached profile compilation info for the given profile file. 87 // If no entry exists, a new empty one will be created, added to the cache and 88 // then returned. 89 ProfileCompilationInfo* GetCachedProfiledInfo(const std::string& filename); 90 // Fetches the current resolved classes from the ClassLinker and stores them 91 // in the profile_cache_ for later save. 92 void FetchAndCacheResolvedClasses(); 93 94 static bool MaybeRecordDexUseInternal( 95 const std::string& dex_location, 96 const std::set<std::string>& tracked_locations, 97 const std::string& foreign_dex_profile_path, 98 const std::set<std::string>& app_data_dirs); 99 100 void DumpInfo(std::ostream& os); 101 102 // The only instance of the saver. 103 static ProfileSaver* instance_ GUARDED_BY(Locks::profiler_lock_); 104 // Profile saver thread. 105 static pthread_t profiler_pthread_ GUARDED_BY(Locks::profiler_lock_); 106 107 jit::JitCodeCache* jit_code_cache_; 108 109 // Collection of code paths that the profiles tracks. 110 // It maps profile locations to code paths (dex base locations). 111 SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_ 112 GUARDED_BY(Locks::profiler_lock_); 113 // The directory were the we should store the code paths. 114 std::string foreign_dex_profile_path_; 115 116 // A list of application directories, used to infer if a loaded dex belongs 117 // to the application or not. Multiple application data directories are possible when 118 // different apps share the same runtime. 119 std::set<std::string> app_data_dirs_ GUARDED_BY(Locks::profiler_lock_); 120 121 bool shutting_down_ GUARDED_BY(Locks::profiler_lock_); 122 uint32_t last_save_number_of_methods_; 123 uint32_t last_save_number_of_classes_; 124 125 // A local cache for the profile information. Maps each tracked file to its 126 // profile information. The size of this cache is usually very small and tops 127 // to just a few hundreds entries in the ProfileCompilationInfo objects. 128 // It helps avoiding unnecessary writes to disk. 129 SafeMap<std::string, ProfileCompilationInfo> profile_cache_; 130 131 // Save period condition support. 132 Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; 133 ConditionVariable period_condition_ GUARDED_BY(wait_lock_); 134 135 uint64_t total_bytes_written_; 136 uint64_t total_number_of_writes_; 137 uint64_t total_number_of_code_cache_queries_; 138 uint64_t total_number_of_skipped_writes_; 139 uint64_t total_number_of_failed_writes_; 140 uint64_t total_ms_of_sleep_; 141 uint64_t total_ns_of_work_; 142 uint64_t total_number_of_foreign_dex_marks_; 143 // TODO(calin): replace with an actual size. 144 uint64_t max_number_of_profile_entries_cached_; 145 146 DISALLOW_COPY_AND_ASSIGN(ProfileSaver); 147}; 148 149} // namespace art 150 151#endif // ART_RUNTIME_JIT_PROFILE_SAVER_H_ 152