blob: c4055b386cc7c9810088c28b214b80fbd4f5572a [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 Juravle64142952016-03-21 14:37:55 +000044 static const uint8_t kProfileMagic[];
45 static const uint8_t kProfileVersion[];
46
Calin Juravle67265462016-03-18 16:23:40 +000047 // Add the given methods and classes to the current profile object.
48 bool AddMethodsAndClasses(const std::vector<ArtMethod*>& methods,
49 const std::set<DexCacheResolvedClasses>& resolved_classes);
Calin Juravle877fd962016-01-05 14:29:29 +000050 // Loads profile information from the given file descriptor.
Calin Juravle2e2db782016-02-23 12:00:03 +000051 bool Load(int fd);
Calin Juravle67265462016-03-18 16:23:40 +000052 // Merge the data from another ProfileCompilationInfo into the current object.
53 bool MergeWith(const ProfileCompilationInfo& info);
Calin Juravle877fd962016-01-05 14:29:29 +000054 // Saves the profile data to the given file descriptor.
Calin Juravle2e2db782016-02-23 12:00:03 +000055 bool Save(int fd);
Calin Juravle67265462016-03-18 16:23:40 +000056 // Loads and merges profile information from the given file into the current
57 // object and tries to save it back to disk.
Calin Juravle5d1bd0a2016-03-24 20:33:22 +000058 // If `force` is true then the save will go through even if the given file
59 // has bad data or its version does not match. In this cases the profile content
60 // is ignored.
61 bool MergeAndSave(const std::string& filename, uint64_t* bytes_written, bool force);
Calin Juravle67265462016-03-18 16:23:40 +000062
Calin Juravle998c2162015-12-21 15:39:33 +020063 // Returns the number of methods that were profiled.
64 uint32_t GetNumberOfMethods() const;
Calin Juravle67265462016-03-18 16:23:40 +000065 // Returns the number of resolved classes that were profiled.
66 uint32_t GetNumberOfResolvedClasses() const;
Calin Juravle226501b2015-12-11 14:41:31 +000067
68 // Returns true if the method reference is present in the profiling info.
69 bool ContainsMethod(const MethodReference& method_ref) const;
70
Mathieu Chartiera8077802016-03-16 19:08:31 -070071 // Returns true if the class is present in the profiling info.
72 bool ContainsClass(const DexFile& dex_file, uint16_t class_def_idx) const;
73
Calin Juravle226501b2015-12-11 14:41:31 +000074 // Dumps all the loaded profile info into a string and returns it.
Calin Juravle998c2162015-12-21 15:39:33 +020075 // If dex_files is not null then the method indices will be resolved to their
76 // names.
Calin Juravle226501b2015-12-11 14:41:31 +000077 // This is intended for testing and debugging.
Calin Juravle998c2162015-12-21 15:39:33 +020078 std::string DumpInfo(const std::vector<const DexFile*>* dex_files,
79 bool print_full_dex_location = true) const;
Calin Juravle226501b2015-12-11 14:41:31 +000080
Calin Juravle2e2db782016-02-23 12:00:03 +000081 bool Equals(const ProfileCompilationInfo& other);
Calin Juravle67265462016-03-18 16:23:40 +000082
Calin Juravle31708b72016-02-05 19:44:05 +000083 static std::string GetProfileDexFileKey(const std::string& dex_location);
Calin Juravle877fd962016-01-05 14:29:29 +000084
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080085 // Returns the class descriptors for all of the classes in the profiles' class sets.
86 // Note the dex location is actually the profile key, the caller needs to call back in to the
87 // profile info stuff to generate a map back to the dex location.
88 std::set<DexCacheResolvedClasses> GetResolvedClasses() const;
Calin Juravle226501b2015-12-11 14:41:31 +000089
Calin Juravle67265462016-03-18 16:23:40 +000090 // Clears the resolved classes from the current object.
91 void ClearResolvedClasses();
92
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080093 private:
Calin Juravle64142952016-03-21 14:37:55 +000094 enum ProfileLoadSatus {
95 kProfileLoadIOError,
96 kProfileLoadVersionMismatch,
97 kProfileLoadBadData,
98 kProfileLoadSuccess
99 };
100
Calin Juravle998c2162015-12-21 15:39:33 +0200101 struct DexFileData {
102 explicit DexFileData(uint32_t location_checksum) : checksum(location_checksum) {}
103 uint32_t checksum;
104 std::set<uint16_t> method_set;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800105 std::set<uint16_t> class_set;
Calin Juravle877fd962016-01-05 14:29:29 +0000106
107 bool operator==(const DexFileData& other) const {
108 return checksum == other.checksum && method_set == other.method_set;
109 }
Calin Juravle998c2162015-12-21 15:39:33 +0200110 };
Calin Juravle226501b2015-12-11 14:41:31 +0000111
Calin Juravle998c2162015-12-21 15:39:33 +0200112 using DexFileToProfileInfoMap = SafeMap<const std::string, DexFileData>;
113
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800114 DexFileData* GetOrAddDexFileData(const std::string& dex_location, uint32_t checksum);
115 bool AddMethodIndex(const std::string& dex_location, uint32_t checksum, uint16_t method_idx);
116 bool AddClassIndex(const std::string& dex_location, uint32_t checksum, uint16_t class_idx);
117 bool AddResolvedClasses(const DexCacheResolvedClasses& classes)
118 SHARED_REQUIRES(Locks::mutator_lock_);
Calin Juravle64142952016-03-21 14:37:55 +0000119
120 // Parsing functionality.
121
122 struct ProfileLineHeader {
123 std::string dex_location;
124 uint16_t method_set_size;
125 uint16_t class_set_size;
126 uint32_t checksum;
127 };
128
129 // A helper structure to make sure we don't read past our buffers in the loops.
130 struct SafeBuffer {
131 public:
132 explicit SafeBuffer(size_t size) : storage_(new uint8_t[size]) {
133 ptr_current_ = storage_.get();
134 ptr_end_ = ptr_current_ + size;
135 }
136
137 // Reads the content of the descriptor at the current position.
138 ProfileLoadSatus FillFromFd(int fd,
139 const std::string& source,
140 /*out*/std::string* error);
141
142 // Reads an uint value (high bits to low bits) and advances the current pointer
143 // with the number of bits read.
144 template <typename T> T ReadUintAndAdvance();
145
146 // Compares the given data with the content current pointer. If the contents are
147 // equal it advances the current pointer by data_size.
148 bool CompareAndAdvance(const uint8_t* data, size_t data_size);
149
150 // Get the underlying raw buffer.
151 uint8_t* Get() { return storage_.get(); }
152
153 private:
154 std::unique_ptr<uint8_t> storage_;
155 uint8_t* ptr_current_;
156 uint8_t* ptr_end_;
157 };
158
159 ProfileLoadSatus LoadInternal(int fd, std::string* error);
160
161 ProfileLoadSatus ReadProfileHeader(int fd,
162 /*out*/uint16_t* number_of_lines,
163 /*out*/std::string* error);
164
165 ProfileLoadSatus ReadProfileLineHeader(int fd,
166 /*out*/ProfileLineHeader* line_header,
167 /*out*/std::string* error);
168 ProfileLoadSatus ReadProfileLine(int fd,
169 const ProfileLineHeader& line_header,
170 /*out*/std::string* error);
171
172 bool ProcessLine(SafeBuffer& line_buffer,
173 uint16_t method_set_size,
174 uint16_t class_set_size,
175 uint32_t checksum,
176 const std::string& dex_location);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800177
Calin Juravle877fd962016-01-05 14:29:29 +0000178 friend class ProfileCompilationInfoTest;
179 friend class CompilerDriverProfileTest;
180 friend class ProfileAssistantTest;
181
Calin Juravle226501b2015-12-11 14:41:31 +0000182 DexFileToProfileInfoMap info_;
183};
184
Calin Juravle31f2c152015-10-23 17:56:15 +0100185} // namespace art
186
187#endif // ART_RUNTIME_JIT_OFFLINE_PROFILING_INFO_H_