blob: 492f9f881867af454255f525aca8e652abe91605 [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 Juravle877fd962016-01-05 14:29:29 +000047 // Saves profile information about the given methods in the given file.
48 // Note that the saving proceeds only if the file can be locked for exclusive access.
49 // If not (the locking is not blocking), the function does not save and returns false.
Calin Juravle998c2162015-12-21 15:39:33 +020050 static bool SaveProfilingInfo(const std::string& filename,
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080051 const std::vector<ArtMethod*>& methods,
Calin Juravleb8e69992016-03-09 15:37:48 +000052 const std::set<DexCacheResolvedClasses>& resolved_classes,
53 uint64_t* bytes_written = nullptr);
Calin Juravle226501b2015-12-11 14:41:31 +000054
Calin Juravle67265462016-03-18 16:23:40 +000055 // Add the given methods and classes to the current profile object.
56 bool AddMethodsAndClasses(const std::vector<ArtMethod*>& methods,
57 const std::set<DexCacheResolvedClasses>& resolved_classes);
Calin Juravle877fd962016-01-05 14:29:29 +000058 // Loads profile information from the given file descriptor.
Calin Juravle2e2db782016-02-23 12:00:03 +000059 bool Load(int fd);
Calin Juravle67265462016-03-18 16:23:40 +000060 // Merge the data from another ProfileCompilationInfo into the current object.
61 bool MergeWith(const ProfileCompilationInfo& info);
Calin Juravle877fd962016-01-05 14:29:29 +000062 // Saves the profile data to the given file descriptor.
Calin Juravle2e2db782016-02-23 12:00:03 +000063 bool Save(int fd);
Calin Juravle67265462016-03-18 16:23:40 +000064 // Loads and merges profile information from the given file into the current
65 // object and tries to save it back to disk.
66 bool MergeAndSave(const std::string& filename, uint64_t* bytes_written);
67
Calin Juravle998c2162015-12-21 15:39:33 +020068 // Returns the number of methods that were profiled.
69 uint32_t GetNumberOfMethods() const;
Calin Juravle67265462016-03-18 16:23:40 +000070 // Returns the number of resolved classes that were profiled.
71 uint32_t GetNumberOfResolvedClasses() const;
Calin Juravle226501b2015-12-11 14:41:31 +000072
73 // Returns true if the method reference is present in the profiling info.
74 bool ContainsMethod(const MethodReference& method_ref) const;
75
Mathieu Chartiera8077802016-03-16 19:08:31 -070076 // Returns true if the class is present in the profiling info.
77 bool ContainsClass(const DexFile& dex_file, uint16_t class_def_idx) const;
78
Calin Juravle226501b2015-12-11 14:41:31 +000079 // Dumps all the loaded profile info into a string and returns it.
Calin Juravle998c2162015-12-21 15:39:33 +020080 // If dex_files is not null then the method indices will be resolved to their
81 // names.
Calin Juravle226501b2015-12-11 14:41:31 +000082 // This is intended for testing and debugging.
Calin Juravle998c2162015-12-21 15:39:33 +020083 std::string DumpInfo(const std::vector<const DexFile*>* dex_files,
84 bool print_full_dex_location = true) const;
Calin Juravle226501b2015-12-11 14:41:31 +000085
Calin Juravle2e2db782016-02-23 12:00:03 +000086 bool Equals(const ProfileCompilationInfo& other);
Calin Juravle67265462016-03-18 16:23:40 +000087
Calin Juravle31708b72016-02-05 19:44:05 +000088 static std::string GetProfileDexFileKey(const std::string& dex_location);
Calin Juravle877fd962016-01-05 14:29:29 +000089
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080090 // Returns the class descriptors for all of the classes in the profiles' class sets.
91 // Note the dex location is actually the profile key, the caller needs to call back in to the
92 // profile info stuff to generate a map back to the dex location.
93 std::set<DexCacheResolvedClasses> GetResolvedClasses() const;
Calin Juravle226501b2015-12-11 14:41:31 +000094
Calin Juravle67265462016-03-18 16:23:40 +000095 // Clears the resolved classes from the current object.
96 void ClearResolvedClasses();
97
Mathieu Chartierc5dd3192015-12-09 16:38:30 -080098 private:
Calin Juravle64142952016-03-21 14:37:55 +000099 enum ProfileLoadSatus {
100 kProfileLoadIOError,
101 kProfileLoadVersionMismatch,
102 kProfileLoadBadData,
103 kProfileLoadSuccess
104 };
105
Calin Juravle998c2162015-12-21 15:39:33 +0200106 struct DexFileData {
107 explicit DexFileData(uint32_t location_checksum) : checksum(location_checksum) {}
108 uint32_t checksum;
109 std::set<uint16_t> method_set;
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800110 std::set<uint16_t> class_set;
Calin Juravle877fd962016-01-05 14:29:29 +0000111
112 bool operator==(const DexFileData& other) const {
113 return checksum == other.checksum && method_set == other.method_set;
114 }
Calin Juravle998c2162015-12-21 15:39:33 +0200115 };
Calin Juravle226501b2015-12-11 14:41:31 +0000116
Calin Juravle998c2162015-12-21 15:39:33 +0200117 using DexFileToProfileInfoMap = SafeMap<const std::string, DexFileData>;
118
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800119 DexFileData* GetOrAddDexFileData(const std::string& dex_location, uint32_t checksum);
120 bool AddMethodIndex(const std::string& dex_location, uint32_t checksum, uint16_t method_idx);
121 bool AddClassIndex(const std::string& dex_location, uint32_t checksum, uint16_t class_idx);
122 bool AddResolvedClasses(const DexCacheResolvedClasses& classes)
123 SHARED_REQUIRES(Locks::mutator_lock_);
Calin Juravle64142952016-03-21 14:37:55 +0000124
125 // Parsing functionality.
126
127 struct ProfileLineHeader {
128 std::string dex_location;
129 uint16_t method_set_size;
130 uint16_t class_set_size;
131 uint32_t checksum;
132 };
133
134 // A helper structure to make sure we don't read past our buffers in the loops.
135 struct SafeBuffer {
136 public:
137 explicit SafeBuffer(size_t size) : storage_(new uint8_t[size]) {
138 ptr_current_ = storage_.get();
139 ptr_end_ = ptr_current_ + size;
140 }
141
142 // Reads the content of the descriptor at the current position.
143 ProfileLoadSatus FillFromFd(int fd,
144 const std::string& source,
145 /*out*/std::string* error);
146
147 // Reads an uint value (high bits to low bits) and advances the current pointer
148 // with the number of bits read.
149 template <typename T> T ReadUintAndAdvance();
150
151 // Compares the given data with the content current pointer. If the contents are
152 // equal it advances the current pointer by data_size.
153 bool CompareAndAdvance(const uint8_t* data, size_t data_size);
154
155 // Get the underlying raw buffer.
156 uint8_t* Get() { return storage_.get(); }
157
158 private:
159 std::unique_ptr<uint8_t> storage_;
160 uint8_t* ptr_current_;
161 uint8_t* ptr_end_;
162 };
163
164 ProfileLoadSatus LoadInternal(int fd, std::string* error);
165
166 ProfileLoadSatus ReadProfileHeader(int fd,
167 /*out*/uint16_t* number_of_lines,
168 /*out*/std::string* error);
169
170 ProfileLoadSatus ReadProfileLineHeader(int fd,
171 /*out*/ProfileLineHeader* line_header,
172 /*out*/std::string* error);
173 ProfileLoadSatus ReadProfileLine(int fd,
174 const ProfileLineHeader& line_header,
175 /*out*/std::string* error);
176
177 bool ProcessLine(SafeBuffer& line_buffer,
178 uint16_t method_set_size,
179 uint16_t class_set_size,
180 uint32_t checksum,
181 const std::string& dex_location);
Mathieu Chartierc5dd3192015-12-09 16:38:30 -0800182
Calin Juravle877fd962016-01-05 14:29:29 +0000183 friend class ProfileCompilationInfoTest;
184 friend class CompilerDriverProfileTest;
185 friend class ProfileAssistantTest;
186
Calin Juravle226501b2015-12-11 14:41:31 +0000187 DexFileToProfileInfoMap info_;
188};
189
Calin Juravle31f2c152015-10-23 17:56:15 +0100190} // namespace art
191
192#endif // ART_RUNTIME_JIT_OFFLINE_PROFILING_INFO_H_