blob: e7eab95f3da251061147d80f48628e80a37f3abe [file] [log] [blame]
Calin Juravle4d77b6a2015-12-01 18:38:09 +00001/*
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"
Calin Juravleb4eddd22016-01-13 15:52:33 -080023#include "safe_map.h"
Calin Juravle4d77b6a2015-12-01 18:38:09 +000024
25namespace art {
26
27class ProfileSaver {
28 public:
Calin Juravleb4eddd22016-01-13 15:52:33 -080029 // 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.
Calin Juravle4d77b6a2015-12-01 18:38:09 +000031 static void Start(const std::string& output_filename,
32 jit::JitCodeCache* jit_code_cache,
Calin Juravlec90bc922016-02-24 10:13:09 +000033 const std::vector<std::string>& code_paths,
34 const std::string& foreign_dex_profile_path,
35 const std::string& app_data_dir)
Calin Juravle4d77b6a2015-12-01 18:38:09 +000036 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()
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
Calin Juravlec90bc922016-02-24 10:13:09 +000047 static void NotifyDexUse(const std::string& dex_location);
48
Calin Juravle4d77b6a2015-12-01 18:38:09 +000049 private:
50 ProfileSaver(const std::string& output_filename,
51 jit::JitCodeCache* jit_code_cache,
Calin Juravlec90bc922016-02-24 10:13:09 +000052 const std::vector<std::string>& code_paths,
53 const std::string& foreign_dex_profile_path,
54 const std::string& app_data_dir);
Calin Juravle4d77b6a2015-12-01 18:38:09 +000055
56 // NO_THREAD_SAFETY_ANALYSIS for static function calling into member function with excludes lock.
57 static void* RunProfileSaverThread(void* arg)
58 REQUIRES(!Locks::profiler_lock_, !wait_lock_)
59 NO_THREAD_SAFETY_ANALYSIS;
60
61 // The run loop for the saver.
62 void Run() REQUIRES(!Locks::profiler_lock_, !wait_lock_);
63 // Processes the existing profiling info from the jit code cache and returns
64 // true if it needed to be saved to disk.
65 bool ProcessProfilingInfo();
66 // Returns true if the saver is shutting down (ProfileSaver::Stop() has been called).
67 bool ShuttingDown(Thread* self) REQUIRES(!Locks::profiler_lock_);
68
Calin Juravleb4eddd22016-01-13 15:52:33 -080069 void AddTrackedLocations(const std::string& output_filename,
70 const std::vector<std::string>& code_paths)
71 REQUIRES(Locks::profiler_lock_);
72
Calin Juravlec90bc922016-02-24 10:13:09 +000073 static void MaybeRecordDexUseInternal(
74 const std::string& dex_location,
75 const std::set<std::string>& tracked_locations,
76 const std::string& foreign_dex_profile_path,
77 const std::string& app_data_dir);
78
Calin Juravle4d77b6a2015-12-01 18:38:09 +000079 // The only instance of the saver.
80 static ProfileSaver* instance_ GUARDED_BY(Locks::profiler_lock_);
81 // Profile saver thread.
82 static pthread_t profiler_pthread_ GUARDED_BY(Locks::profiler_lock_);
83
Calin Juravle4d77b6a2015-12-01 18:38:09 +000084 jit::JitCodeCache* jit_code_cache_;
Calin Juravleb4eddd22016-01-13 15:52:33 -080085 SafeMap<std::string, std::set<std::string>> tracked_dex_base_locations_
86 GUARDED_BY(Locks::profiler_lock_);
Calin Juravlec90bc922016-02-24 10:13:09 +000087 std::string foreign_dex_profile_path_;
88 std::string app_data_dir_;
Calin Juravle4d77b6a2015-12-01 18:38:09 +000089 uint64_t code_cache_last_update_time_ns_;
90 bool shutting_down_ GUARDED_BY(Locks::profiler_lock_);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080091 bool first_profile_ = true;
Calin Juravle4d77b6a2015-12-01 18:38:09 +000092
93 // Save period condition support.
94 Mutex wait_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
95 ConditionVariable period_condition_ GUARDED_BY(wait_lock_);
96
97 DISALLOW_COPY_AND_ASSIGN(ProfileSaver);
98};
99
100} // namespace art
101
102#endif // ART_RUNTIME_JIT_PROFILE_SAVER_H_