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