Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #include "oat_file_assistant.h" |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #ifdef __linux__ |
| 21 | #include <sys/sendfile.h> |
| 22 | #else |
| 23 | #include <sys/socket.h> |
| 24 | #endif |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <set> |
| 30 | |
| 31 | #include "base/logging.h" |
| 32 | #include "base/stringprintf.h" |
| 33 | #include "class_linker.h" |
| 34 | #include "gc/heap.h" |
| 35 | #include "gc/space/image_space.h" |
| 36 | #include "image.h" |
| 37 | #include "oat.h" |
| 38 | #include "os.h" |
| 39 | #include "profiler.h" |
| 40 | #include "runtime.h" |
| 41 | #include "ScopedFd.h" |
| 42 | #include "utils.h" |
| 43 | |
| 44 | namespace art { |
| 45 | |
| 46 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 47 | const InstructionSet isa, |
| 48 | bool load_executable) |
| 49 | : OatFileAssistant(dex_location, nullptr, isa, load_executable, nullptr) { } |
| 50 | |
| 51 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 52 | const char* oat_location, |
| 53 | const InstructionSet isa, |
| 54 | bool load_executable) |
| 55 | : OatFileAssistant(dex_location, oat_location, isa, load_executable, nullptr) { } |
| 56 | |
| 57 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 58 | const InstructionSet isa, |
| 59 | bool load_executable, |
| 60 | const char* package_name) |
| 61 | : OatFileAssistant(dex_location, nullptr, isa, load_executable, package_name) { } |
| 62 | |
| 63 | OatFileAssistant::OatFileAssistant(const char* dex_location, |
| 64 | const char* oat_location, |
| 65 | const InstructionSet isa, |
| 66 | bool load_executable, |
| 67 | const char* package_name) |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 68 | : isa_(isa), package_name_(package_name), load_executable_(load_executable) { |
| 69 | CHECK(dex_location != nullptr) << "OatFileAssistant: null dex location"; |
| 70 | dex_location_.assign(dex_location); |
| 71 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 72 | if (load_executable_ && isa != kRuntimeISA) { |
| 73 | LOG(WARNING) << "OatFileAssistant: Load executable specified, " |
| 74 | << "but isa is not kRuntimeISA. Will not attempt to load executable."; |
| 75 | load_executable_ = false; |
| 76 | } |
| 77 | |
| 78 | // If the user gave a target oat location, save that as the cached oat |
| 79 | // location now so we won't try to construct the default location later. |
| 80 | if (oat_location != nullptr) { |
| 81 | cached_oat_file_name_ = std::string(oat_location); |
| 82 | cached_oat_file_name_attempted_ = true; |
| 83 | cached_oat_file_name_found_ = true; |
| 84 | } |
| 85 | |
| 86 | // If there is no package name given, we will not be able to find any |
| 87 | // profiles associated with this dex location. Preemptively mark that to |
| 88 | // be the case, rather than trying to find and load the profiles later. |
| 89 | // Similarly, if profiling is disabled. |
| 90 | if (package_name == nullptr |
| 91 | || !Runtime::Current()->GetProfilerOptions().IsEnabled()) { |
| 92 | profile_load_attempted_ = true; |
| 93 | profile_load_succeeded_ = false; |
| 94 | old_profile_load_attempted_ = true; |
| 95 | old_profile_load_succeeded_ = false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | OatFileAssistant::~OatFileAssistant() { |
| 100 | // Clean up the lock file. |
Richard Uhler | 581f4e9 | 2015-05-07 10:19:35 -0700 | [diff] [blame] | 101 | if (flock_.HasFile()) { |
| 102 | TEMP_FAILURE_RETRY(unlink(flock_.GetFile()->GetPath().c_str())); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | bool OatFileAssistant::IsInBootClassPath() { |
| 107 | // Note: We check the current boot class path, regardless of the ISA |
| 108 | // specified by the user. This is okay, because the boot class path should |
| 109 | // be the same for all ISAs. |
| 110 | // TODO: Can we verify the boot class path is the same for all ISAs? |
| 111 | Runtime* runtime = Runtime::Current(); |
| 112 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 113 | const auto& boot_class_path = class_linker->GetBootClassPath(); |
| 114 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 115 | if (boot_class_path[i]->GetLocation() == dex_location_) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 116 | VLOG(oat) << "Dex location " << dex_location_ << " is in boot class path"; |
| 117 | return true; |
| 118 | } |
| 119 | } |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | bool OatFileAssistant::Lock(std::string* error_msg) { |
| 124 | CHECK(error_msg != nullptr); |
Richard Uhler | 581f4e9 | 2015-05-07 10:19:35 -0700 | [diff] [blame] | 125 | CHECK(!flock_.HasFile()) << "OatFileAssistant::Lock already acquired"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 126 | |
| 127 | if (OatFileName() == nullptr) { |
| 128 | *error_msg = "Failed to determine lock file"; |
| 129 | return false; |
| 130 | } |
| 131 | std::string lock_file_name = *OatFileName() + ".flock"; |
| 132 | |
Richard Uhler | 581f4e9 | 2015-05-07 10:19:35 -0700 | [diff] [blame] | 133 | if (!flock_.Init(lock_file_name.c_str(), error_msg)) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 134 | TEMP_FAILURE_RETRY(unlink(lock_file_name.c_str())); |
| 135 | return false; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 140 | OatFileAssistant::DexOptNeeded OatFileAssistant::GetDexOptNeeded() { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 141 | // TODO: If the profiling code is ever restored, it's worth considering |
| 142 | // whether we should check to see if the profile is out of date here. |
| 143 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 144 | if (OatFileIsUpToDate() || OdexFileIsUpToDate()) { |
| 145 | return kNoDexOptNeeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 146 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 147 | |
| 148 | if (OdexFileNeedsRelocation()) { |
| 149 | return kPatchOatNeeded; |
| 150 | } |
| 151 | |
| 152 | if (OatFileNeedsRelocation()) { |
| 153 | return kSelfPatchOatNeeded; |
| 154 | } |
| 155 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 156 | return HasOriginalDexFiles() ? kDex2OatNeeded : kNoDexOptNeeded; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | bool OatFileAssistant::MakeUpToDate(std::string* error_msg) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 160 | switch (GetDexOptNeeded()) { |
| 161 | case kNoDexOptNeeded: return true; |
| 162 | case kDex2OatNeeded: return GenerateOatFile(error_msg); |
| 163 | case kPatchOatNeeded: return RelocateOatFile(OdexFileName(), error_msg); |
| 164 | case kSelfPatchOatNeeded: return RelocateOatFile(OatFileName(), error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 165 | } |
| 166 | UNREACHABLE(); |
| 167 | } |
| 168 | |
| 169 | std::unique_ptr<OatFile> OatFileAssistant::GetBestOatFile() { |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 170 | // The best oat files are, in descending order of bestness: |
| 171 | // 1. Properly relocated files. These may be opened executable. |
| 172 | // 2. Not out-of-date files that are already opened non-executable. |
| 173 | // 3. Not out-of-date files that we must reopen non-executable. |
| 174 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 175 | if (OatFileIsUpToDate()) { |
| 176 | oat_file_released_ = true; |
| 177 | return std::move(cached_oat_file_); |
| 178 | } |
| 179 | |
| 180 | if (OdexFileIsUpToDate()) { |
| 181 | oat_file_released_ = true; |
| 182 | return std::move(cached_odex_file_); |
| 183 | } |
| 184 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 185 | VLOG(oat) << "Oat File Assistant: No relocated oat file found," |
| 186 | << " attempting to fall back to interpreting oat file instead."; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 187 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 188 | if (!OatFileIsOutOfDate() && !OatFileIsExecutable()) { |
| 189 | oat_file_released_ = true; |
| 190 | return std::move(cached_oat_file_); |
| 191 | } |
| 192 | |
| 193 | if (!OdexFileIsOutOfDate() && !OdexFileIsExecutable()) { |
| 194 | oat_file_released_ = true; |
| 195 | return std::move(cached_odex_file_); |
| 196 | } |
| 197 | |
| 198 | if (!OatFileIsOutOfDate()) { |
| 199 | load_executable_ = false; |
| 200 | ClearOatFileCache(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 201 | if (!OatFileIsOutOfDate()) { |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 202 | CHECK(!OatFileIsExecutable()); |
| 203 | oat_file_released_ = true; |
| 204 | return std::move(cached_oat_file_); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 205 | } |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 206 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 207 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 208 | if (!OdexFileIsOutOfDate()) { |
| 209 | load_executable_ = false; |
| 210 | ClearOdexFileCache(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 211 | if (!OdexFileIsOutOfDate()) { |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 212 | CHECK(!OdexFileIsExecutable()); |
| 213 | oat_file_released_ = true; |
| 214 | return std::move(cached_odex_file_); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | return std::unique_ptr<OatFile>(); |
| 219 | } |
| 220 | |
| 221 | std::vector<std::unique_ptr<const DexFile>> OatFileAssistant::LoadDexFiles( |
| 222 | const OatFile& oat_file, const char* dex_location) { |
| 223 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 224 | |
| 225 | // Load the primary dex file. |
| 226 | std::string error_msg; |
| 227 | const OatFile::OatDexFile* oat_dex_file = oat_file.GetOatDexFile( |
| 228 | dex_location, nullptr, false); |
| 229 | if (oat_dex_file == nullptr) { |
| 230 | LOG(WARNING) << "Attempt to load out-of-date oat file " |
| 231 | << oat_file.GetLocation() << " for dex location " << dex_location; |
| 232 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 233 | } |
| 234 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 235 | std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 236 | if (dex_file.get() == nullptr) { |
| 237 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
| 238 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 239 | } |
| 240 | dex_files.push_back(std::move(dex_file)); |
| 241 | |
| 242 | // Load secondary multidex files |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 243 | for (size_t i = 1; ; i++) { |
| 244 | std::string secondary_dex_location = DexFile::GetMultiDexLocation(i, dex_location); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 245 | oat_dex_file = oat_file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 246 | if (oat_dex_file == nullptr) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 247 | // There are no more secondary dex files to load. |
| 248 | break; |
| 249 | } |
| 250 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 251 | dex_file = oat_dex_file->OpenDexFile(&error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 252 | if (dex_file.get() == nullptr) { |
| 253 | LOG(WARNING) << "Failed to open dex file from oat dex file: " << error_msg; |
| 254 | return std::vector<std::unique_ptr<const DexFile>>(); |
| 255 | } |
| 256 | dex_files.push_back(std::move(dex_file)); |
| 257 | } |
| 258 | return dex_files; |
| 259 | } |
| 260 | |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 261 | bool OatFileAssistant::HasOriginalDexFiles() { |
| 262 | // Ensure GetRequiredDexChecksum has been run so that |
| 263 | // has_original_dex_files_ is initialized. We don't care about the result of |
| 264 | // GetRequiredDexChecksum. |
| 265 | GetRequiredDexChecksum(); |
| 266 | return has_original_dex_files_; |
| 267 | } |
| 268 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 269 | const std::string* OatFileAssistant::OdexFileName() { |
| 270 | if (!cached_odex_file_name_attempted_) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 271 | cached_odex_file_name_attempted_ = true; |
| 272 | |
| 273 | std::string error_msg; |
| 274 | cached_odex_file_name_found_ = DexFilenameToOdexFilename( |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 275 | dex_location_, isa_, &cached_odex_file_name_, &error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 276 | if (!cached_odex_file_name_found_) { |
| 277 | // If we can't figure out the odex file, we treat it as if the odex |
| 278 | // file was inaccessible. |
| 279 | LOG(WARNING) << "Failed to determine odex file name: " << error_msg; |
| 280 | } |
| 281 | } |
| 282 | return cached_odex_file_name_found_ ? &cached_odex_file_name_ : nullptr; |
| 283 | } |
| 284 | |
| 285 | bool OatFileAssistant::OdexFileExists() { |
| 286 | return GetOdexFile() != nullptr; |
| 287 | } |
| 288 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 289 | OatFileAssistant::OatStatus OatFileAssistant::OdexFileStatus() { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 290 | if (OdexFileIsOutOfDate()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 291 | return kOatOutOfDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 292 | } |
| 293 | if (OdexFileIsUpToDate()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 294 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 295 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 296 | return kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | bool OatFileAssistant::OdexFileIsOutOfDate() { |
| 300 | if (!odex_file_is_out_of_date_attempted_) { |
| 301 | odex_file_is_out_of_date_attempted_ = true; |
| 302 | const OatFile* odex_file = GetOdexFile(); |
| 303 | if (odex_file == nullptr) { |
| 304 | cached_odex_file_is_out_of_date_ = true; |
| 305 | } else { |
| 306 | cached_odex_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*odex_file); |
| 307 | } |
| 308 | } |
| 309 | return cached_odex_file_is_out_of_date_; |
| 310 | } |
| 311 | |
| 312 | bool OatFileAssistant::OdexFileNeedsRelocation() { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 313 | return OdexFileStatus() == kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | bool OatFileAssistant::OdexFileIsUpToDate() { |
| 317 | if (!odex_file_is_up_to_date_attempted_) { |
| 318 | odex_file_is_up_to_date_attempted_ = true; |
| 319 | const OatFile* odex_file = GetOdexFile(); |
| 320 | if (odex_file == nullptr) { |
| 321 | cached_odex_file_is_up_to_date_ = false; |
| 322 | } else { |
| 323 | cached_odex_file_is_up_to_date_ = GivenOatFileIsUpToDate(*odex_file); |
| 324 | } |
| 325 | } |
| 326 | return cached_odex_file_is_up_to_date_; |
| 327 | } |
| 328 | |
| 329 | const std::string* OatFileAssistant::OatFileName() { |
| 330 | if (!cached_oat_file_name_attempted_) { |
| 331 | cached_oat_file_name_attempted_ = true; |
| 332 | |
| 333 | // Compute the oat file name from the dex location. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 334 | // TODO: The oat file assistant should be the definitive place for |
| 335 | // determining the oat file name from the dex location, not |
| 336 | // GetDalvikCacheFilename. |
| 337 | std::string cache_dir = StringPrintf("%s%s", |
| 338 | DalvikCacheDirectory().c_str(), GetInstructionSetString(isa_)); |
| 339 | std::string error_msg; |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 340 | cached_oat_file_name_found_ = GetDalvikCacheFilename(dex_location_.c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 341 | cache_dir.c_str(), &cached_oat_file_name_, &error_msg); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 342 | if (!cached_oat_file_name_found_) { |
| 343 | // If we can't determine the oat file name, we treat the oat file as |
| 344 | // inaccessible. |
| 345 | LOG(WARNING) << "Failed to determine oat file name for dex location " |
| 346 | << dex_location_ << ": " << error_msg; |
| 347 | } |
| 348 | } |
| 349 | return cached_oat_file_name_found_ ? &cached_oat_file_name_ : nullptr; |
| 350 | } |
| 351 | |
| 352 | bool OatFileAssistant::OatFileExists() { |
| 353 | return GetOatFile() != nullptr; |
| 354 | } |
| 355 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 356 | OatFileAssistant::OatStatus OatFileAssistant::OatFileStatus() { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 357 | if (OatFileIsOutOfDate()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 358 | return kOatOutOfDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 359 | } |
| 360 | if (OatFileIsUpToDate()) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 361 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 362 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 363 | return kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | bool OatFileAssistant::OatFileIsOutOfDate() { |
| 367 | if (!oat_file_is_out_of_date_attempted_) { |
| 368 | oat_file_is_out_of_date_attempted_ = true; |
| 369 | const OatFile* oat_file = GetOatFile(); |
| 370 | if (oat_file == nullptr) { |
| 371 | cached_oat_file_is_out_of_date_ = true; |
| 372 | } else { |
| 373 | cached_oat_file_is_out_of_date_ = GivenOatFileIsOutOfDate(*oat_file); |
| 374 | } |
| 375 | } |
| 376 | return cached_oat_file_is_out_of_date_; |
| 377 | } |
| 378 | |
| 379 | bool OatFileAssistant::OatFileNeedsRelocation() { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 380 | return OatFileStatus() == kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | bool OatFileAssistant::OatFileIsUpToDate() { |
| 384 | if (!oat_file_is_up_to_date_attempted_) { |
| 385 | oat_file_is_up_to_date_attempted_ = true; |
| 386 | const OatFile* oat_file = GetOatFile(); |
| 387 | if (oat_file == nullptr) { |
| 388 | cached_oat_file_is_up_to_date_ = false; |
| 389 | } else { |
| 390 | cached_oat_file_is_up_to_date_ = GivenOatFileIsUpToDate(*oat_file); |
| 391 | } |
| 392 | } |
| 393 | return cached_oat_file_is_up_to_date_; |
| 394 | } |
| 395 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 396 | OatFileAssistant::OatStatus OatFileAssistant::GivenOatFileStatus(const OatFile& file) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 397 | // TODO: This could cause GivenOatFileIsOutOfDate to be called twice, which |
| 398 | // is more work than we need to do. If performance becomes a concern, and |
| 399 | // this method is actually called, this should be fixed. |
| 400 | if (GivenOatFileIsOutOfDate(file)) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 401 | return kOatOutOfDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 402 | } |
| 403 | if (GivenOatFileIsUpToDate(file)) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 404 | return kOatUpToDate; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 405 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 406 | return kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | bool OatFileAssistant::GivenOatFileIsOutOfDate(const OatFile& file) { |
| 410 | // Verify the dex checksum. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 411 | // Note: GetOatDexFile will return null if the dex checksum doesn't match |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 412 | // what we provide, which verifies the primary dex checksum for us. |
| 413 | const uint32_t* dex_checksum_pointer = GetRequiredDexChecksum(); |
| 414 | const OatFile::OatDexFile* oat_dex_file = file.GetOatDexFile( |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 415 | dex_location_.c_str(), dex_checksum_pointer, false); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 416 | if (oat_dex_file == nullptr) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 417 | return true; |
| 418 | } |
| 419 | |
| 420 | // Verify the dex checksums for any secondary multidex files |
Andreas Gampe | 90e3404 | 2015-04-27 20:01:52 -0700 | [diff] [blame] | 421 | for (size_t i = 1; ; i++) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 422 | std::string secondary_dex_location |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 423 | = DexFile::GetMultiDexLocation(i, dex_location_.c_str()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 424 | const OatFile::OatDexFile* secondary_oat_dex_file |
| 425 | = file.GetOatDexFile(secondary_dex_location.c_str(), nullptr, false); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 426 | if (secondary_oat_dex_file == nullptr) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 427 | // There are no more secondary dex files to check. |
| 428 | break; |
| 429 | } |
| 430 | |
| 431 | std::string error_msg; |
| 432 | uint32_t expected_secondary_checksum = 0; |
| 433 | if (DexFile::GetChecksum(secondary_dex_location.c_str(), |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 434 | &expected_secondary_checksum, &error_msg)) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 435 | uint32_t actual_secondary_checksum |
| 436 | = secondary_oat_dex_file->GetDexFileLocationChecksum(); |
| 437 | if (expected_secondary_checksum != actual_secondary_checksum) { |
| 438 | VLOG(oat) << "Dex checksum does not match for secondary dex: " |
| 439 | << secondary_dex_location |
| 440 | << ". Expected: " << expected_secondary_checksum |
| 441 | << ", Actual: " << actual_secondary_checksum; |
Richard Uhler | 67ff7d1 | 2015-05-14 13:21:13 -0700 | [diff] [blame] | 442 | return true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 443 | } |
| 444 | } else { |
| 445 | // If we can't get the checksum for the secondary location, we assume |
| 446 | // the dex checksum is up to date for this and all other secondary dex |
| 447 | // files. |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | // Verify the image checksum |
| 453 | const ImageInfo* image_info = GetImageInfo(); |
| 454 | if (image_info == nullptr) { |
| 455 | VLOG(oat) << "No image for oat image checksum to match against."; |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | if (file.GetOatHeader().GetImageFileLocationOatChecksum() != image_info->oat_checksum) { |
| 460 | VLOG(oat) << "Oat image checksum does not match image checksum."; |
| 461 | return true; |
| 462 | } |
| 463 | |
| 464 | // The checksums are all good; the dex file is not out of date. |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | bool OatFileAssistant::GivenOatFileNeedsRelocation(const OatFile& file) { |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 469 | return GivenOatFileStatus(file) == kOatNeedsRelocation; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | bool OatFileAssistant::GivenOatFileIsUpToDate(const OatFile& file) { |
| 473 | if (GivenOatFileIsOutOfDate(file)) { |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | if (file.IsPic()) { |
| 478 | return true; |
| 479 | } |
| 480 | |
| 481 | const ImageInfo* image_info = GetImageInfo(); |
| 482 | if (image_info == nullptr) { |
Richard Uhler | f7f798c | 2015-05-11 09:36:57 -0700 | [diff] [blame] | 483 | VLOG(oat) << "No image to check oat relocation against."; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 484 | return false; |
| 485 | } |
| 486 | |
| 487 | // Verify the oat_data_begin recorded for the image in the oat file matches |
| 488 | // the actual oat_data_begin for boot.oat in the image. |
| 489 | const OatHeader& oat_header = file.GetOatHeader(); |
| 490 | uintptr_t oat_data_begin = oat_header.GetImageFileLocationOatDataBegin(); |
| 491 | if (oat_data_begin != image_info->oat_data_begin) { |
| 492 | VLOG(oat) << file.GetLocation() << |
| 493 | ": Oat file image oat_data_begin (" << oat_data_begin << ")" |
| 494 | << " does not match actual image oat_data_begin (" |
| 495 | << image_info->oat_data_begin << ")"; |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | // Verify the oat_patch_delta recorded for the image in the oat file matches |
| 500 | // the actual oat_patch_delta for the image. |
| 501 | int32_t oat_patch_delta = oat_header.GetImagePatchDelta(); |
| 502 | if (oat_patch_delta != image_info->patch_delta) { |
| 503 | VLOG(oat) << file.GetLocation() << |
| 504 | ": Oat file image patch delta (" << oat_patch_delta << ")" |
| 505 | << " does not match actual image patch delta (" |
| 506 | << image_info->patch_delta << ")"; |
| 507 | return false; |
| 508 | } |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | bool OatFileAssistant::ProfileExists() { |
| 513 | return GetProfile() != nullptr; |
| 514 | } |
| 515 | |
| 516 | bool OatFileAssistant::OldProfileExists() { |
| 517 | return GetOldProfile() != nullptr; |
| 518 | } |
| 519 | |
| 520 | // TODO: The IsProfileChangeSignificant implementation was copied from likely |
| 521 | // bit-rotted code. |
| 522 | bool OatFileAssistant::IsProfileChangeSignificant() { |
| 523 | ProfileFile* profile = GetProfile(); |
| 524 | if (profile == nullptr) { |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | ProfileFile* old_profile = GetOldProfile(); |
| 529 | if (old_profile == nullptr) { |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | // TODO: The following code to compare two profile files should live with |
| 534 | // the rest of the profiler code, not the oat file assistant code. |
| 535 | |
| 536 | // A change in profile is considered significant if X% (change_thr property) |
| 537 | // of the top K% (compile_thr property) samples has changed. |
| 538 | const ProfilerOptions& options = Runtime::Current()->GetProfilerOptions(); |
| 539 | const double top_k_threshold = options.GetTopKThreshold(); |
| 540 | const double change_threshold = options.GetTopKChangeThreshold(); |
| 541 | std::set<std::string> top_k, old_top_k; |
| 542 | profile->GetTopKSamples(top_k, top_k_threshold); |
| 543 | old_profile->GetTopKSamples(old_top_k, top_k_threshold); |
| 544 | std::set<std::string> diff; |
| 545 | std::set_difference(top_k.begin(), top_k.end(), old_top_k.begin(), |
| 546 | old_top_k.end(), std::inserter(diff, diff.end())); |
| 547 | |
| 548 | // TODO: consider using the usedPercentage instead of the plain diff count. |
| 549 | double change_percent = 100.0 * static_cast<double>(diff.size()) |
| 550 | / static_cast<double>(top_k.size()); |
| 551 | std::set<std::string>::iterator end = diff.end(); |
| 552 | for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) { |
| 553 | VLOG(oat) << "Profile new in topK: " << *it; |
| 554 | } |
| 555 | |
| 556 | if (change_percent > change_threshold) { |
| 557 | VLOG(oat) << "Oat File Assistant: Profile for " << dex_location_ |
| 558 | << "has changed significantly: (top " |
| 559 | << top_k_threshold << "% samples changed in proportion of " |
| 560 | << change_percent << "%)"; |
| 561 | return true; |
| 562 | } |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | // TODO: The CopyProfileFile implementation was copied from likely bit-rotted |
| 567 | // code. |
| 568 | void OatFileAssistant::CopyProfileFile() { |
| 569 | if (!ProfileExists()) { |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | std::string profile_name = ProfileFileName(); |
| 574 | std::string old_profile_name = OldProfileFileName(); |
| 575 | |
| 576 | ScopedFd src(open(old_profile_name.c_str(), O_RDONLY)); |
| 577 | if (src.get() == -1) { |
| 578 | PLOG(WARNING) << "Failed to open profile file " << old_profile_name |
| 579 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | struct stat stat_src; |
| 584 | if (fstat(src.get(), &stat_src) == -1) { |
| 585 | PLOG(WARNING) << "Failed to get stats for profile file " << old_profile_name |
| 586 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | // Create the copy with rw------- (only accessible by system) |
| 591 | ScopedFd dst(open(profile_name.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0600)); |
| 592 | if (dst.get() == -1) { |
| 593 | PLOG(WARNING) << "Failed to create/write prev profile file " << profile_name |
| 594 | << ". My uid:gid is " << getuid() << ":" << getgid(); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | #ifdef __linux__ |
| 599 | if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) { |
| 600 | #else |
| 601 | off_t len; |
| 602 | if (sendfile(dst.get(), src.get(), 0, &len, nullptr, 0) == -1) { |
| 603 | #endif |
| 604 | PLOG(WARNING) << "Failed to copy profile file " << old_profile_name |
| 605 | << " to " << profile_name << ". My uid:gid is " << getuid() |
| 606 | << ":" << getgid(); |
| 607 | } |
| 608 | } |
| 609 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 610 | bool OatFileAssistant::RelocateOatFile(const std::string* input_file, |
| 611 | std::string* error_msg) { |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 612 | CHECK(error_msg != nullptr); |
| 613 | |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 614 | if (input_file == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 615 | *error_msg = "Patching of oat file for dex location " + dex_location_ |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 616 | + " not attempted because the input file name could not be determined."; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 617 | return false; |
| 618 | } |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 619 | const std::string& input_file_name = *input_file; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 620 | |
| 621 | if (OatFileName() == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 622 | *error_msg = "Patching of oat file for dex location " + dex_location_ |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 623 | + " not attempted because the oat file name could not be determined."; |
| 624 | return false; |
| 625 | } |
| 626 | const std::string& oat_file_name = *OatFileName(); |
| 627 | |
| 628 | const ImageInfo* image_info = GetImageInfo(); |
| 629 | Runtime* runtime = Runtime::Current(); |
| 630 | if (image_info == nullptr) { |
| 631 | *error_msg = "Patching of oat file " + oat_file_name |
| 632 | + " not attempted because no image location was found."; |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | if (!runtime->IsDex2OatEnabled()) { |
| 637 | *error_msg = "Patching of oat file " + oat_file_name |
| 638 | + " not attempted because dex2oat is disabled"; |
| 639 | return false; |
| 640 | } |
| 641 | |
| 642 | std::vector<std::string> argv; |
| 643 | argv.push_back(runtime->GetPatchoatExecutable()); |
| 644 | argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(isa_))); |
Richard Uhler | 95abd04 | 2015-03-24 09:51:28 -0700 | [diff] [blame] | 645 | argv.push_back("--input-oat-file=" + input_file_name); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 646 | argv.push_back("--output-oat-file=" + oat_file_name); |
| 647 | argv.push_back("--patched-image-location=" + image_info->location); |
| 648 | |
| 649 | std::string command_line(Join(argv, ' ')); |
| 650 | if (!Exec(argv, error_msg)) { |
| 651 | // Manually delete the file. This ensures there is no garbage left over if |
| 652 | // the process unexpectedly died. |
| 653 | TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str())); |
| 654 | return false; |
| 655 | } |
| 656 | |
| 657 | // Mark that the oat file has changed and we should try to reload. |
| 658 | ClearOatFileCache(); |
| 659 | return true; |
| 660 | } |
| 661 | |
| 662 | bool OatFileAssistant::GenerateOatFile(std::string* error_msg) { |
| 663 | CHECK(error_msg != nullptr); |
| 664 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 665 | Runtime* runtime = Runtime::Current(); |
| 666 | if (!runtime->IsDex2OatEnabled()) { |
| 667 | *error_msg = "Generation of oat file for dex location " + dex_location_ |
| 668 | + " not attempted because dex2oat is disabled."; |
| 669 | return false; |
| 670 | } |
| 671 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 672 | if (OatFileName() == nullptr) { |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 673 | *error_msg = "Generation of oat file for dex location " + dex_location_ |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 674 | + " not attempted because the oat file name could not be determined."; |
| 675 | return false; |
| 676 | } |
| 677 | const std::string& oat_file_name = *OatFileName(); |
| 678 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 679 | // dex2oat ignores missing dex files and doesn't report an error. |
| 680 | // Check explicitly here so we can detect the error properly. |
| 681 | // TODO: Why does dex2oat behave that way? |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 682 | if (!OS::FileExists(dex_location_.c_str())) { |
| 683 | *error_msg = "Dex location " + dex_location_ + " does not exists."; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 684 | return false; |
| 685 | } |
| 686 | |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 687 | std::unique_ptr<File> oat_file; |
| 688 | oat_file.reset(OS::CreateEmptyFile(oat_file_name.c_str())); |
| 689 | if (oat_file.get() == nullptr) { |
| 690 | *error_msg = "Generation of oat file " + oat_file_name |
| 691 | + " not attempted because the oat file could not be created."; |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | if (fchmod(oat_file->Fd(), 0644) != 0) { |
| 696 | *error_msg = "Generation of oat file " + oat_file_name |
| 697 | + " not attempted because the oat file could not be made world readable."; |
| 698 | oat_file->Erase(); |
| 699 | return false; |
| 700 | } |
| 701 | |
| 702 | std::vector<std::string> args; |
| 703 | args.push_back("--dex-file=" + dex_location_); |
| 704 | args.push_back("--oat-fd=" + std::to_string(oat_file->Fd())); |
| 705 | args.push_back("--oat-location=" + oat_file_name); |
| 706 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 707 | if (!Dex2Oat(args, error_msg)) { |
| 708 | // Manually delete the file. This ensures there is no garbage left over if |
| 709 | // the process unexpectedly died. |
Richard Uhler | 8327cf7 | 2015-10-13 16:34:59 -0700 | [diff] [blame] | 710 | oat_file->Erase(); |
| 711 | TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str())); |
| 712 | return false; |
| 713 | } |
| 714 | |
| 715 | if (oat_file->FlushCloseOrErase() != 0) { |
| 716 | *error_msg = "Unable to close oat file " + oat_file_name; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 717 | TEMP_FAILURE_RETRY(unlink(oat_file_name.c_str())); |
| 718 | return false; |
| 719 | } |
| 720 | |
| 721 | // Mark that the oat file has changed and we should try to reload. |
| 722 | ClearOatFileCache(); |
| 723 | return true; |
| 724 | } |
| 725 | |
| 726 | bool OatFileAssistant::Dex2Oat(const std::vector<std::string>& args, |
| 727 | std::string* error_msg) { |
| 728 | Runtime* runtime = Runtime::Current(); |
| 729 | std::string image_location = ImageLocation(); |
| 730 | if (image_location.empty()) { |
| 731 | *error_msg = "No image location found for Dex2Oat."; |
| 732 | return false; |
| 733 | } |
| 734 | |
| 735 | std::vector<std::string> argv; |
| 736 | argv.push_back(runtime->GetCompilerExecutable()); |
| 737 | argv.push_back("--runtime-arg"); |
| 738 | argv.push_back("-classpath"); |
| 739 | argv.push_back("--runtime-arg"); |
| 740 | argv.push_back(runtime->GetClassPathString()); |
Nicolas Geoffray | 7a4d015 | 2015-07-10 17:29:39 +0100 | [diff] [blame] | 741 | if (runtime->IsDebuggable()) { |
Sebastien Hertz | 0de1133 | 2015-05-13 12:14:05 +0200 | [diff] [blame] | 742 | argv.push_back("--debuggable"); |
| 743 | } |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 744 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 745 | |
| 746 | if (!runtime->IsVerificationEnabled()) { |
| 747 | argv.push_back("--compiler-filter=verify-none"); |
| 748 | } |
| 749 | |
| 750 | if (runtime->MustRelocateIfPossible()) { |
| 751 | argv.push_back("--runtime-arg"); |
| 752 | argv.push_back("-Xrelocate"); |
| 753 | } else { |
| 754 | argv.push_back("--runtime-arg"); |
| 755 | argv.push_back("-Xnorelocate"); |
| 756 | } |
| 757 | |
| 758 | if (!kIsTargetBuild) { |
| 759 | argv.push_back("--host"); |
| 760 | } |
| 761 | |
| 762 | argv.push_back("--boot-image=" + image_location); |
| 763 | |
| 764 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 765 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 766 | |
| 767 | argv.insert(argv.end(), args.begin(), args.end()); |
| 768 | |
| 769 | std::string command_line(Join(argv, ' ')); |
| 770 | return Exec(argv, error_msg); |
| 771 | } |
| 772 | |
| 773 | bool OatFileAssistant::DexFilenameToOdexFilename(const std::string& location, |
| 774 | InstructionSet isa, std::string* odex_filename, std::string* error_msg) { |
| 775 | CHECK(odex_filename != nullptr); |
| 776 | CHECK(error_msg != nullptr); |
| 777 | |
| 778 | // The odex file name is formed by replacing the dex_location extension with |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 779 | // .odex and inserting an oat/<isa> directory. For example: |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 780 | // location = /foo/bar/baz.jar |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 781 | // odex_location = /foo/bar/oat/<isa>/baz.odex |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 782 | |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 783 | // Find the directory portion of the dex location and add the oat/<isa> |
| 784 | // directory. |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 785 | size_t pos = location.rfind('/'); |
| 786 | if (pos == std::string::npos) { |
| 787 | *error_msg = "Dex location " + location + " has no directory."; |
| 788 | return false; |
| 789 | } |
| 790 | std::string dir = location.substr(0, pos+1); |
Richard Uhler | 6343411 | 2015-03-16 14:32:16 -0700 | [diff] [blame] | 791 | dir += "oat/" + std::string(GetInstructionSetString(isa)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 792 | |
| 793 | // Find the file portion of the dex location. |
| 794 | std::string file; |
| 795 | if (pos == std::string::npos) { |
| 796 | file = location; |
| 797 | } else { |
| 798 | file = location.substr(pos+1); |
| 799 | } |
| 800 | |
| 801 | // Get the base part of the file without the extension. |
| 802 | pos = file.rfind('.'); |
| 803 | if (pos == std::string::npos) { |
| 804 | *error_msg = "Dex location " + location + " has no extension."; |
| 805 | return false; |
| 806 | } |
| 807 | std::string base = file.substr(0, pos); |
| 808 | |
| 809 | *odex_filename = dir + "/" + base + ".odex"; |
| 810 | return true; |
| 811 | } |
| 812 | |
| 813 | std::string OatFileAssistant::DalvikCacheDirectory() { |
| 814 | // Note: We don't cache this, because it will only be called once by |
| 815 | // OatFileName, and we don't care about the performance of the profiling |
| 816 | // code, which isn't used in practice. |
| 817 | |
| 818 | // TODO: The work done in GetDalvikCache is overkill for what we need. |
| 819 | // Ideally a new API for getting the DalvikCacheDirectory the way we want |
| 820 | // (without existence testing, creation, or death) is provided with the rest |
| 821 | // of the GetDalvikCache family of functions. Until such an API is in place, |
| 822 | // we use GetDalvikCache to avoid duplicating the logic for determining the |
| 823 | // dalvik cache directory. |
| 824 | std::string result; |
| 825 | bool have_android_data; |
| 826 | bool dalvik_cache_exists; |
| 827 | bool is_global_cache; |
| 828 | GetDalvikCache("", false, &result, &have_android_data, &dalvik_cache_exists, &is_global_cache); |
| 829 | return result; |
| 830 | } |
| 831 | |
| 832 | std::string OatFileAssistant::ProfileFileName() { |
| 833 | if (package_name_ != nullptr) { |
| 834 | return DalvikCacheDirectory() + std::string("profiles/") + package_name_; |
| 835 | } |
| 836 | return ""; |
| 837 | } |
| 838 | |
| 839 | std::string OatFileAssistant::OldProfileFileName() { |
| 840 | std::string profile_name = ProfileFileName(); |
| 841 | if (profile_name.empty()) { |
| 842 | return ""; |
| 843 | } |
| 844 | return profile_name + "@old"; |
| 845 | } |
| 846 | |
| 847 | std::string OatFileAssistant::ImageLocation() { |
| 848 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame^] | 849 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetBootImageSpace(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 850 | if (image_space == nullptr) { |
| 851 | return ""; |
| 852 | } |
| 853 | return image_space->GetImageLocation(); |
| 854 | } |
| 855 | |
| 856 | const uint32_t* OatFileAssistant::GetRequiredDexChecksum() { |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 857 | if (!required_dex_checksum_attempted_) { |
| 858 | required_dex_checksum_attempted_ = true; |
| 859 | required_dex_checksum_found_ = false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 860 | std::string error_msg; |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 861 | if (DexFile::GetChecksum(dex_location_.c_str(), &cached_required_dex_checksum_, &error_msg)) { |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 862 | required_dex_checksum_found_ = true; |
| 863 | has_original_dex_files_ = true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 864 | } else { |
| 865 | // This can happen if the original dex file has been stripped from the |
| 866 | // apk. |
| 867 | VLOG(oat) << "OatFileAssistant: " << error_msg; |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 868 | has_original_dex_files_ = false; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 869 | |
| 870 | // Get the checksum from the odex if we can. |
| 871 | const OatFile* odex_file = GetOdexFile(); |
| 872 | if (odex_file != nullptr) { |
| 873 | const OatFile::OatDexFile* odex_dex_file = odex_file->GetOatDexFile( |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 874 | dex_location_.c_str(), nullptr, false); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 875 | if (odex_dex_file != nullptr) { |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 876 | cached_required_dex_checksum_ = odex_dex_file->GetDexFileLocationChecksum(); |
| 877 | required_dex_checksum_found_ = true; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | } |
| 881 | } |
Richard Uhler | 9b994ea | 2015-06-24 08:44:19 -0700 | [diff] [blame] | 882 | return required_dex_checksum_found_ ? &cached_required_dex_checksum_ : nullptr; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | const OatFile* OatFileAssistant::GetOdexFile() { |
| 886 | CHECK(!oat_file_released_) << "OdexFile called after oat file released."; |
| 887 | if (!odex_file_load_attempted_) { |
| 888 | odex_file_load_attempted_ = true; |
| 889 | if (OdexFileName() != nullptr) { |
| 890 | const std::string& odex_file_name = *OdexFileName(); |
| 891 | std::string error_msg; |
| 892 | cached_odex_file_.reset(OatFile::Open(odex_file_name.c_str(), |
| 893 | odex_file_name.c_str(), nullptr, nullptr, load_executable_, |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 894 | dex_location_.c_str(), &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 895 | if (cached_odex_file_.get() == nullptr) { |
| 896 | VLOG(oat) << "OatFileAssistant test for existing pre-compiled oat file " |
| 897 | << odex_file_name << ": " << error_msg; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | return cached_odex_file_.get(); |
| 902 | } |
| 903 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 904 | bool OatFileAssistant::OdexFileIsExecutable() { |
| 905 | const OatFile* odex_file = GetOdexFile(); |
| 906 | return (odex_file != nullptr && odex_file->IsExecutable()); |
| 907 | } |
| 908 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 909 | void OatFileAssistant::ClearOdexFileCache() { |
| 910 | odex_file_load_attempted_ = false; |
| 911 | cached_odex_file_.reset(); |
| 912 | odex_file_is_out_of_date_attempted_ = false; |
| 913 | odex_file_is_up_to_date_attempted_ = false; |
| 914 | } |
| 915 | |
| 916 | const OatFile* OatFileAssistant::GetOatFile() { |
| 917 | CHECK(!oat_file_released_) << "OatFile called after oat file released."; |
| 918 | if (!oat_file_load_attempted_) { |
| 919 | oat_file_load_attempted_ = true; |
| 920 | if (OatFileName() != nullptr) { |
| 921 | const std::string& oat_file_name = *OatFileName(); |
| 922 | std::string error_msg; |
| 923 | cached_oat_file_.reset(OatFile::Open(oat_file_name.c_str(), |
Richard Uhler | e5fed03 | 2015-03-18 08:21:11 -0700 | [diff] [blame] | 924 | oat_file_name.c_str(), nullptr, nullptr, load_executable_, |
Richard Uhler | 740eec9 | 2015-10-15 15:12:23 -0700 | [diff] [blame] | 925 | dex_location_.c_str(), &error_msg)); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 926 | if (cached_oat_file_.get() == nullptr) { |
| 927 | VLOG(oat) << "OatFileAssistant test for existing oat file " |
| 928 | << oat_file_name << ": " << error_msg; |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | return cached_oat_file_.get(); |
| 933 | } |
| 934 | |
Richard Uhler | 5f946da | 2015-07-17 12:28:32 -0700 | [diff] [blame] | 935 | bool OatFileAssistant::OatFileIsExecutable() { |
| 936 | const OatFile* oat_file = GetOatFile(); |
| 937 | return (oat_file != nullptr && oat_file->IsExecutable()); |
| 938 | } |
| 939 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 940 | void OatFileAssistant::ClearOatFileCache() { |
| 941 | oat_file_load_attempted_ = false; |
| 942 | cached_oat_file_.reset(); |
| 943 | oat_file_is_out_of_date_attempted_ = false; |
| 944 | oat_file_is_up_to_date_attempted_ = false; |
| 945 | } |
| 946 | |
| 947 | const OatFileAssistant::ImageInfo* OatFileAssistant::GetImageInfo() { |
| 948 | if (!image_info_load_attempted_) { |
| 949 | image_info_load_attempted_ = true; |
| 950 | |
| 951 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame^] | 952 | const gc::space::ImageSpace* image_space = runtime->GetHeap()->GetBootImageSpace(); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 953 | if (image_space != nullptr) { |
| 954 | cached_image_info_.location = image_space->GetImageLocation(); |
| 955 | |
| 956 | if (isa_ == kRuntimeISA) { |
| 957 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 958 | cached_image_info_.oat_checksum = image_header.GetOatChecksum(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame^] | 959 | cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>( |
| 960 | image_header.GetOatDataBegin()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 961 | cached_image_info_.patch_delta = image_header.GetPatchDelta(); |
| 962 | } else { |
| 963 | std::unique_ptr<ImageHeader> image_header( |
| 964 | gc::space::ImageSpace::ReadImageHeaderOrDie( |
| 965 | cached_image_info_.location.c_str(), isa_)); |
| 966 | cached_image_info_.oat_checksum = image_header->GetOatChecksum(); |
Mathieu Chartier | 073b16c | 2015-11-10 14:13:23 -0800 | [diff] [blame^] | 967 | cached_image_info_.oat_data_begin = reinterpret_cast<uintptr_t>( |
| 968 | image_header->GetOatDataBegin()); |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 969 | cached_image_info_.patch_delta = image_header->GetPatchDelta(); |
| 970 | } |
| 971 | } |
| 972 | image_info_load_succeeded_ = (image_space != nullptr); |
| 973 | } |
| 974 | return image_info_load_succeeded_ ? &cached_image_info_ : nullptr; |
| 975 | } |
| 976 | |
| 977 | ProfileFile* OatFileAssistant::GetProfile() { |
| 978 | if (!profile_load_attempted_) { |
| 979 | CHECK(package_name_ != nullptr) |
| 980 | << "pakage_name_ is nullptr: " |
| 981 | << "profile_load_attempted_ should have been true"; |
| 982 | profile_load_attempted_ = true; |
| 983 | std::string profile_name = ProfileFileName(); |
| 984 | if (!profile_name.empty()) { |
| 985 | profile_load_succeeded_ = cached_profile_.LoadFile(profile_name); |
| 986 | } |
| 987 | } |
| 988 | return profile_load_succeeded_ ? &cached_profile_ : nullptr; |
| 989 | } |
| 990 | |
| 991 | ProfileFile* OatFileAssistant::GetOldProfile() { |
| 992 | if (!old_profile_load_attempted_) { |
| 993 | CHECK(package_name_ != nullptr) |
| 994 | << "pakage_name_ is nullptr: " |
| 995 | << "old_profile_load_attempted_ should have been true"; |
| 996 | old_profile_load_attempted_ = true; |
| 997 | std::string old_profile_name = OldProfileFileName(); |
| 998 | if (!old_profile_name.empty()) { |
| 999 | old_profile_load_succeeded_ = cached_old_profile_.LoadFile(old_profile_name); |
| 1000 | } |
| 1001 | } |
| 1002 | return old_profile_load_succeeded_ ? &cached_old_profile_ : nullptr; |
| 1003 | } |
| 1004 | |
| 1005 | } // namespace art |
| 1006 | |