blob: fb07f8c569482def5835c99001995f8760f74151 [file] [log] [blame]
Calin Juravle31f2c152015-10-23 17:56:15 +01001/*
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_OFFLINE_PROFILING_INFO_H_
18#define ART_RUNTIME_JIT_OFFLINE_PROFILING_INFO_H_
19
20#include <set>
Calin Juravle4d77b6a2015-12-01 18:38:09 +000021#include <vector>
Calin Juravle31f2c152015-10-23 17:56:15 +010022
23#include "atomic.h"
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080024#include "dex_cache_resolved_classes.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010025#include "dex_file.h"
Calin Juravle226501b2015-12-11 14:41:31 +000026#include "method_reference.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010027#include "safe_map.h"
28
29namespace art {
30
31class ArtMethod;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080032class DexCacheProfileData;
Calin Juravle31f2c152015-10-23 17:56:15 +010033
Calin Juravle998c2162015-12-21 15:39:33 +020034// TODO: rename file.
Calin Juravle31f2c152015-10-23 17:56:15 +010035/**
Calin Juravle998c2162015-12-21 15:39:33 +020036 * Profile information in a format suitable to be queried by the compiler and
37 * performing profile guided compilation.
38 * It is a serialize-friendly format based on information collected by the
39 * interpreter (ProfileInfo).
Calin Juravle31f2c152015-10-23 17:56:15 +010040 * Currently it stores only the hot compiled methods.
41 */
Calin Juravle226501b2015-12-11 14:41:31 +000042class ProfileCompilationInfo {
43 public:
Calin Juravle877fd962016-01-05 14:29:29 +000044 // Saves profile information about the given methods in the given file.
45 // Note that the saving proceeds only if the file can be locked for exclusive access.
46 // If not (the locking is not blocking), the function does not save and returns false.
Calin Juravle998c2162015-12-21 15:39:33 +020047 static bool SaveProfilingInfo(const std::string& filename,
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080048 const std::vector<ArtMethod*>& methods,
Calin Juravleb8e69992016-03-09 15:37:48 +000049 const std::set<DexCacheResolvedClasses>& resolved_classes,
50 uint64_t* bytes_written = nullptr);
Calin Juravle226501b2015-12-11 14:41:31 +000051
Calin Juravle877fd962016-01-05 14:29:29 +000052 // Loads profile information from the given file descriptor.
Calin Juravle2e2db782016-02-23 12:00:03 +000053 bool Load(int fd);
Calin Juravle998c2162015-12-21 15:39:33 +020054 // Loads the data from another ProfileCompilationInfo object.
55 bool Load(const ProfileCompilationInfo& info);
Calin Juravle877fd962016-01-05 14:29:29 +000056 // Saves the profile data to the given file descriptor.
Calin Juravle2e2db782016-02-23 12:00:03 +000057 bool Save(int fd);
Calin Juravle998c2162015-12-21 15:39:33 +020058 // Returns the number of methods that were profiled.
59 uint32_t GetNumberOfMethods() const;
Calin Juravle226501b2015-12-11 14:41:31 +000060
61 // Returns true if the method reference is present in the profiling info.
62 bool ContainsMethod(const MethodReference& method_ref) const;
63
Mathieu Chartiera8077802016-03-16 19:08:31 -070064 // Returns true if the class is present in the profiling info.
65 bool ContainsClass(const DexFile& dex_file, uint16_t class_def_idx) const;
66
Calin Juravle226501b2015-12-11 14:41:31 +000067 // Dumps all the loaded profile info into a string and returns it.
Calin Juravle998c2162015-12-21 15:39:33 +020068 // If dex_files is not null then the method indices will be resolved to their
69 // names.
Calin Juravle226501b2015-12-11 14:41:31 +000070 // This is intended for testing and debugging.
Calin Juravle998c2162015-12-21 15:39:33 +020071 std::string DumpInfo(const std::vector<const DexFile*>* dex_files,
72 bool print_full_dex_location = true) const;
Calin Juravle226501b2015-12-11 14:41:31 +000073
Calin Juravle877fd962016-01-05 14:29:29 +000074 // For testing purposes.
Calin Juravle2e2db782016-02-23 12:00:03 +000075 bool Equals(const ProfileCompilationInfo& other);
Calin Juravle31708b72016-02-05 19:44:05 +000076 static std::string GetProfileDexFileKey(const std::string& dex_location);
Calin Juravle877fd962016-01-05 14:29:29 +000077
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080078 // Returns the class descriptors for all of the classes in the profiles' class sets.
79 // Note the dex location is actually the profile key, the caller needs to call back in to the
80 // profile info stuff to generate a map back to the dex location.
81 std::set<DexCacheResolvedClasses> GetResolvedClasses() const;
Calin Juravle226501b2015-12-11 14:41:31 +000082
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080083 private:
Calin Juravle998c2162015-12-21 15:39:33 +020084 struct DexFileData {
85 explicit DexFileData(uint32_t location_checksum) : checksum(location_checksum) {}
86 uint32_t checksum;
87 std::set<uint16_t> method_set;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080088 std::set<uint16_t> class_set;
Calin Juravle877fd962016-01-05 14:29:29 +000089
90 bool operator==(const DexFileData& other) const {
91 return checksum == other.checksum && method_set == other.method_set;
92 }
Calin Juravle998c2162015-12-21 15:39:33 +020093 };
Calin Juravle226501b2015-12-11 14:41:31 +000094
Calin Juravle998c2162015-12-21 15:39:33 +020095 using DexFileToProfileInfoMap = SafeMap<const std::string, DexFileData>;
96
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080097 DexFileData* GetOrAddDexFileData(const std::string& dex_location, uint32_t checksum);
98 bool AddMethodIndex(const std::string& dex_location, uint32_t checksum, uint16_t method_idx);
99 bool AddClassIndex(const std::string& dex_location, uint32_t checksum, uint16_t class_idx);
100 bool AddResolvedClasses(const DexCacheResolvedClasses& classes)
101 SHARED_REQUIRES(Locks::mutator_lock_);
102 bool ProcessLine(const std::string& line);
103
Calin Juravle877fd962016-01-05 14:29:29 +0000104 friend class ProfileCompilationInfoTest;
105 friend class CompilerDriverProfileTest;
106 friend class ProfileAssistantTest;
107
Calin Juravle226501b2015-12-11 14:41:31 +0000108 DexFileToProfileInfoMap info_;
109};
110
Calin Juravle31f2c152015-10-23 17:56:15 +0100111} // namespace art
112
113#endif // ART_RUNTIME_JIT_OFFLINE_PROFILING_INFO_H_