blob: 53d0eea93215ad7421b1efb20968abc6f53a8aa1 [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"
Andreas Gampea5b09a62016-11-17 15:21:22 -080026#include "dex_file_types.h"
Calin Juravle226501b2015-12-11 14:41:31 +000027#include "method_reference.h"
Calin Juravle31f2c152015-10-23 17:56:15 +010028#include "safe_map.h"
29
30namespace art {
31
Calin Juravle998c2162015-12-21 15:39:33 +020032// TODO: rename file.
Calin Juravle31f2c152015-10-23 17:56:15 +010033/**
Calin Juravle998c2162015-12-21 15:39:33 +020034 * Profile information in a format suitable to be queried by the compiler and
35 * performing profile guided compilation.
36 * It is a serialize-friendly format based on information collected by the
37 * interpreter (ProfileInfo).
Calin Juravle31f2c152015-10-23 17:56:15 +010038 * Currently it stores only the hot compiled methods.
39 */
Calin Juravle226501b2015-12-11 14:41:31 +000040class ProfileCompilationInfo {
41 public:
Calin Juravle64142952016-03-21 14:37:55 +000042 static const uint8_t kProfileMagic[];
43 static const uint8_t kProfileVersion[];
44
Calin Juravle67265462016-03-18 16:23:40 +000045 // Add the given methods and classes to the current profile object.
Calin Juravle99629622016-04-19 16:33:46 +010046 bool AddMethodsAndClasses(const std::vector<MethodReference>& methods,
Calin Juravle67265462016-03-18 16:23:40 +000047 const std::set<DexCacheResolvedClasses>& resolved_classes);
Calin Juravle877fd962016-01-05 14:29:29 +000048 // Loads profile information from the given file descriptor.
Calin Juravle2e2db782016-02-23 12:00:03 +000049 bool Load(int fd);
Calin Juravle67265462016-03-18 16:23:40 +000050 // Merge the data from another ProfileCompilationInfo into the current object.
51 bool MergeWith(const ProfileCompilationInfo& info);
Calin Juravle877fd962016-01-05 14:29:29 +000052 // Saves the profile data to the given file descriptor.
Calin Juravle2e2db782016-02-23 12:00:03 +000053 bool Save(int fd);
Calin Juravle67265462016-03-18 16:23:40 +000054 // Loads and merges profile information from the given file into the current
55 // object and tries to save it back to disk.
Calin Juravle5d1bd0a2016-03-24 20:33:22 +000056 // If `force` is true then the save will go through even if the given file
57 // has bad data or its version does not match. In this cases the profile content
58 // is ignored.
59 bool MergeAndSave(const std::string& filename, uint64_t* bytes_written, bool force);
Calin Juravle67265462016-03-18 16:23:40 +000060
Calin Juravle998c2162015-12-21 15:39:33 +020061 // Returns the number of methods that were profiled.
62 uint32_t GetNumberOfMethods() const;
Calin Juravle67265462016-03-18 16:23:40 +000063 // Returns the number of resolved classes that were profiled.
64 uint32_t GetNumberOfResolvedClasses() const;
Calin Juravle226501b2015-12-11 14:41:31 +000065
66 // Returns true if the method reference is present in the profiling info.
67 bool ContainsMethod(const MethodReference& method_ref) const;
68
Jeff Hao54b58552016-11-16 15:15:04 -080069 // Returns true if the class's type is present in the profiling info.
Andreas Gampea5b09a62016-11-17 15:21:22 -080070 bool ContainsClass(const DexFile& dex_file, dex::TypeIndex type_idx) const;
Mathieu Chartiera8077802016-03-16 19:08:31 -070071
Calin Juravle226501b2015-12-11 14:41:31 +000072 // Dumps all the loaded profile info into a string and returns it.
Calin Juravle998c2162015-12-21 15:39:33 +020073 // If dex_files is not null then the method indices will be resolved to their
74 // names.
Calin Juravle226501b2015-12-11 14:41:31 +000075 // This is intended for testing and debugging.
Calin Juravle998c2162015-12-21 15:39:33 +020076 std::string DumpInfo(const std::vector<const DexFile*>* dex_files,
77 bool print_full_dex_location = true) const;
Calin Juravle226501b2015-12-11 14:41:31 +000078
Calin Juravle2e2db782016-02-23 12:00:03 +000079 bool Equals(const ProfileCompilationInfo& other);
Calin Juravle67265462016-03-18 16:23:40 +000080
Calin Juravle31708b72016-02-05 19:44:05 +000081 static std::string GetProfileDexFileKey(const std::string& dex_location);
Calin Juravle877fd962016-01-05 14:29:29 +000082
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080083 // Returns the class descriptors for all of the classes in the profiles' class sets.
84 // Note the dex location is actually the profile key, the caller needs to call back in to the
85 // profile info stuff to generate a map back to the dex location.
86 std::set<DexCacheResolvedClasses> GetResolvedClasses() const;
Calin Juravle226501b2015-12-11 14:41:31 +000087
Calin Juravle67265462016-03-18 16:23:40 +000088 // Clears the resolved classes from the current object.
89 void ClearResolvedClasses();
90
Calin Juravle7bcdb532016-06-07 16:14:47 +010091 static bool GenerateTestProfile(int fd,
92 uint16_t number_of_dex_files,
93 uint16_t method_ratio,
94 uint16_t class_ratio);
95
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080096 private:
Calin Juravle64142952016-03-21 14:37:55 +000097 enum ProfileLoadSatus {
98 kProfileLoadIOError,
99 kProfileLoadVersionMismatch,
100 kProfileLoadBadData,
101 kProfileLoadSuccess
102 };
103
Calin Juravle998c2162015-12-21 15:39:33 +0200104 struct DexFileData {
105 explicit DexFileData(uint32_t location_checksum) : checksum(location_checksum) {}
106 uint32_t checksum;
107 std::set<uint16_t> method_set;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800108 std::set<dex::TypeIndex> class_set;
Calin Juravle877fd962016-01-05 14:29:29 +0000109
110 bool operator==(const DexFileData& other) const {
111 return checksum == other.checksum && method_set == other.method_set;
112 }
Calin Juravle998c2162015-12-21 15:39:33 +0200113 };
Calin Juravle226501b2015-12-11 14:41:31 +0000114
Calin Juravle998c2162015-12-21 15:39:33 +0200115 using DexFileToProfileInfoMap = SafeMap<const std::string, DexFileData>;
116
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800117 DexFileData* GetOrAddDexFileData(const std::string& dex_location, uint32_t checksum);
118 bool AddMethodIndex(const std::string& dex_location, uint32_t checksum, uint16_t method_idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800119 bool AddClassIndex(const std::string& dex_location, uint32_t checksum, dex::TypeIndex type_idx);
Calin Juravle99629622016-04-19 16:33:46 +0100120 bool AddResolvedClasses(const DexCacheResolvedClasses& classes);
Calin Juravle64142952016-03-21 14:37:55 +0000121
122 // Parsing functionality.
123
124 struct ProfileLineHeader {
125 std::string dex_location;
126 uint16_t method_set_size;
127 uint16_t class_set_size;
128 uint32_t checksum;
129 };
130
131 // A helper structure to make sure we don't read past our buffers in the loops.
132 struct SafeBuffer {
133 public:
134 explicit SafeBuffer(size_t size) : storage_(new uint8_t[size]) {
135 ptr_current_ = storage_.get();
136 ptr_end_ = ptr_current_ + size;
137 }
138
139 // Reads the content of the descriptor at the current position.
140 ProfileLoadSatus FillFromFd(int fd,
141 const std::string& source,
142 /*out*/std::string* error);
143
144 // Reads an uint value (high bits to low bits) and advances the current pointer
145 // with the number of bits read.
146 template <typename T> T ReadUintAndAdvance();
147
148 // Compares the given data with the content current pointer. If the contents are
149 // equal it advances the current pointer by data_size.
150 bool CompareAndAdvance(const uint8_t* data, size_t data_size);
151
152 // Get the underlying raw buffer.
153 uint8_t* Get() { return storage_.get(); }
154
155 private:
Andreas Gampe7b8a2652016-11-11 17:11:25 -0800156 std::unique_ptr<uint8_t[]> storage_;
Calin Juravle64142952016-03-21 14:37:55 +0000157 uint8_t* ptr_current_;
158 uint8_t* ptr_end_;
159 };
160
161 ProfileLoadSatus LoadInternal(int fd, std::string* error);
162
163 ProfileLoadSatus ReadProfileHeader(int fd,
164 /*out*/uint16_t* number_of_lines,
165 /*out*/std::string* error);
166
167 ProfileLoadSatus ReadProfileLineHeader(int fd,
168 /*out*/ProfileLineHeader* line_header,
169 /*out*/std::string* error);
170 ProfileLoadSatus ReadProfileLine(int fd,
171 const ProfileLineHeader& line_header,
172 /*out*/std::string* error);
173
174 bool ProcessLine(SafeBuffer& line_buffer,
175 uint16_t method_set_size,
176 uint16_t class_set_size,
177 uint32_t checksum,
178 const std::string& dex_location);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800179
Calin Juravle877fd962016-01-05 14:29:29 +0000180 friend class ProfileCompilationInfoTest;
181 friend class CompilerDriverProfileTest;
182 friend class ProfileAssistantTest;
Jeff Hao41fba6a2016-11-28 11:53:33 -0800183 friend class Dex2oatLayoutTest;
Calin Juravle877fd962016-01-05 14:29:29 +0000184
Calin Juravle226501b2015-12-11 14:41:31 +0000185 DexFileToProfileInfoMap info_;
186};
187
Calin Juravle31f2c152015-10-23 17:56:15 +0100188} // namespace art
189
190#endif // ART_RUNTIME_JIT_OFFLINE_PROFILING_INFO_H_