blob: e3fbd5cde1bbffd3356ff27c3a253a20f4ae09dd [file] [log] [blame]
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001/*
2 * Copyright (C) 2008 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
Calin Juravle9dae5b42014-04-07 16:36:21 +030017#include <algorithm>
Calin Juravle9dae5b42014-04-07 16:36:21 +030018#include <set>
Ian Rogersdd157d72014-05-15 14:47:50 -070019#include <fcntl.h>
Calin Juravle9dae5b42014-04-07 16:36:21 +030020#include <unistd.h>
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070021
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070023#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080024#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/space/image_space.h"
27#include "gc/space/space-inl.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070028#include "image.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070029#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080031#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/string.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080033#include "oat.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070034#include "os.h"
Calin Juravle9dae5b42014-04-07 16:36:21 +030035#include "profiler.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070036#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037#include "scoped_thread_state_change.h"
Ian Rogersc9818482012-01-11 08:52:51 -080038#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070039#include "ScopedUtfChars.h"
Calin Juravlebb0b53f2014-05-23 17:33:29 +010040#include "utils.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070041#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070042#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070043
44namespace art {
45
Brian Carlstromf91c8c32011-09-21 17:30:34 -070046// A smart pointer that provides read-only access to a Java string's UTF chars.
47// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
48// passed a null jstring. The correct idiom is:
49//
50// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070051// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070052// return NULL;
53// }
54// // ... use name.c_str()
55//
56// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
57class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080058 public:
59 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
60 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
61 }
62
63 ~NullableScopedUtfChars() {
64 if (mUtfChars) {
65 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070066 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080067 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070068
Elliott Hughesba8eee12012-01-24 20:25:24 -080069 const char* c_str() const {
70 return mUtfChars;
71 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070072
Elliott Hughesba8eee12012-01-24 20:25:24 -080073 size_t size() const {
74 return strlen(mUtfChars);
75 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070076
Elliott Hughesba8eee12012-01-24 20:25:24 -080077 // Element access.
78 const char& operator[](size_t n) const {
79 return mUtfChars[n];
80 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070081
Elliott Hughesba8eee12012-01-24 20:25:24 -080082 private:
83 JNIEnv* mEnv;
84 jstring mString;
85 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070086
Elliott Hughesba8eee12012-01-24 20:25:24 -080087 // Disallow copy and assignment.
88 NullableScopedUtfChars(const NullableScopedUtfChars&);
89 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070090};
91
Elliott Hughes2d983902014-02-04 16:17:13 -080092static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070093 ScopedUtfChars sourceName(env, javaSourceName);
94 if (sourceName.c_str() == NULL) {
95 return 0;
96 }
97 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070098 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070099 return 0;
100 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700101
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700102 uint32_t dex_location_checksum;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800103 uint32_t* dex_location_checksum_pointer = &dex_location_checksum;
Andreas Gampe329d1882014-04-08 10:32:19 -0700104 std::vector<std::string> error_msgs;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700105 std::string error_msg;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800106 if (!DexFile::GetChecksum(sourceName.c_str(), dex_location_checksum_pointer, &error_msg)) {
107 dex_location_checksum_pointer = NULL;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700108 }
109
110 ClassLinker* linker = Runtime::Current()->GetClassLinker();
111 const DexFile* dex_file;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700112 if (outputName.c_str() == nullptr) {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800113 // FindOrCreateOatFileForDexLocation can tolerate a missing dex_location_checksum
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700114 dex_file = linker->FindDexFileInOatFileFromDexLocation(sourceName.c_str(),
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700115 dex_location_checksum_pointer,
116 kRuntimeISA,
117 &error_msgs);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700118 } else {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800119 // FindOrCreateOatFileForDexLocation requires the dex_location_checksum
120 if (dex_location_checksum_pointer == NULL) {
121 ScopedObjectAccess soa(env);
122 DCHECK(!error_msg.empty());
123 ThrowIOException("%s", error_msg.c_str());
124 return 0;
125 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700126 dex_file = linker->FindOrCreateOatFileForDexLocation(sourceName.c_str(), dex_location_checksum,
Andreas Gampe329d1882014-04-08 10:32:19 -0700127 outputName.c_str(), &error_msgs);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700128 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700129 if (dex_file == nullptr) {
Vladimir Marko60836d52014-01-16 15:53:38 +0000130 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700131 CHECK(!error_msgs.empty());
132 // The most important message is at the end. So set up nesting by going forward, which will
133 // wrap the existing exception as a cause for the following one.
134 auto it = error_msgs.begin();
135 auto itEnd = error_msgs.end();
136 for ( ; it != itEnd; ++it) {
137 ThrowWrappedIOException("%s", it->c_str());
138 }
139
jeffhaoc393a4f2011-10-19 13:46:09 -0700140 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700141 }
Elliott Hughes2d983902014-02-04 16:17:13 -0800142 return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_file));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700143}
144
Elliott Hughes2d983902014-02-04 16:17:13 -0800145static const DexFile* toDexFile(jlong dex_file_address, JNIEnv* env) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700146 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Ian Rogers1eb512d2013-10-18 15:42:20 -0700147 if (UNLIKELY(dex_file == nullptr)) {
148 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800149 ThrowNullPointerException(NULL, "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700150 }
151 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700152}
153
Elliott Hughes2d983902014-02-04 16:17:13 -0800154static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 const DexFile* dex_file;
Ian Rogers1eb512d2013-10-18 15:42:20 -0700156 dex_file = toDexFile(cookie, env);
157 if (dex_file == nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700158 return;
159 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700160 ScopedObjectAccess soa(env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700161 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
162 return;
163 }
164 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700165}
166
Elliott Hughes0512f022012-03-15 22:10:52 -0700167static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
Elliott Hughes2d983902014-02-04 16:17:13 -0800168 jlong cookie) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700169 const DexFile* dex_file = toDexFile(cookie, env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700170 if (dex_file == NULL) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700171 VLOG(class_linker) << "Failed to find dex_file";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700172 return NULL;
173 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700174 ScopedUtfChars class_name(env, javaName);
175 if (class_name.c_str() == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700176 VLOG(class_linker) << "Failed to find class_name";
Brian Carlstromdf143242011-10-10 18:05:34 -0700177 return NULL;
178 }
Elliott Hughes95572412011-12-13 18:14:20 -0800179 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Ian Rogersee39a102013-09-19 02:56:49 -0700180 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700181 if (dex_class_def == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700182 VLOG(class_linker) << "Failed to find dex_class_def";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700183 return NULL;
184 }
Ian Rogers1eb512d2013-10-18 15:42:20 -0700185 ScopedObjectAccess soa(env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700186 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
187 class_linker->RegisterDexFile(*dex_file);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700188 StackHandleScope<1> hs(soa.Self());
189 Handle<mirror::ClassLoader> class_loader(
190 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700191 mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file,
192 *dex_class_def);
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700193 VLOG(class_linker) << "DexFile_defineClassNative returning " << result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700194 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700195}
196
Elliott Hughes2d983902014-02-04 16:17:13 -0800197static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700198 jobjectArray result = nullptr;
199 const DexFile* dex_file = toDexFile(cookie, env);
Ian Rogers46889ea2014-05-19 22:31:22 -0700200 if (dex_file != nullptr) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700201 result = env->NewObjectArray(dex_file->NumClassDefs(), WellKnownClasses::java_lang_String,
202 nullptr);
203 if (result != nullptr) {
204 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
205 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
Brian Carlstromcf790bb2014-05-28 11:09:10 -0700206 std::string descriptor(DescriptorToDot(dex_file->GetClassDescriptor(class_def)));
207 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
Ian Rogersdd157d72014-05-15 14:47:50 -0700208 if (jdescriptor.get() == nullptr) {
209 return nullptr;
210 }
211 env->SetObjectArrayElement(result, i, jdescriptor.get());
212 }
213 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700214 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700215 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700216}
217
Dave Allison39c3bfb2014-01-28 18:33:52 -0800218// Copy a profile file
219static void CopyProfileFile(const char* oldfile, const char* newfile) {
220 int fd = open(oldfile, O_RDONLY);
221 if (fd < 0) {
222 // If we can't open the file show the uid:gid of the this process to allow
223 // diagnosis of the problem.
224 LOG(ERROR) << "Failed to open profile file " << oldfile<< ". My uid:gid is "
225 << getuid() << ":" << getgid();
226 return;
227 }
228
229 // Create the copy with rw------- (only accessible by system)
230 int fd2 = open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
231 if (fd2 < 0) {
232 // If we can't open the file show the uid:gid of the this process to allow
233 // diagnosis of the problem.
234 LOG(ERROR) << "Failed to create/write prev profile file " << newfile << ". My uid:gid is "
235 << getuid() << ":" << getgid();
236 return;
237 }
238 char buf[4096];
239 while (true) {
240 int n = read(fd, buf, sizeof(buf));
241 if (n <= 0) {
242 break;
243 }
244 write(fd2, buf, n);
245 }
246 close(fd);
247 close(fd2);
248}
249
Narayan Kamath11d9f062014-04-23 20:24:57 +0100250static jboolean IsDexOptNeededInternal(JNIEnv* env, const char* filename,
251 const char* pkgname, const char* instruction_set, const jboolean defer) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700252 const bool kVerboseLogging = false; // Spammy logging.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700253 const bool kReasonLogging = true; // Logging of reason for returning JNI_TRUE.
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800254
Narayan Kamath11d9f062014-04-23 20:24:57 +0100255 if ((filename == nullptr) || !OS::FileExists(filename)) {
256 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700257 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100258 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700259 env->ThrowNew(fnfe.get(), message);
260 return JNI_FALSE;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700261 }
262
263 // Always treat elements of the bootclasspath as up-to-date. The
264 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700265 Runtime* runtime = Runtime::Current();
266 ClassLinker* class_linker = runtime->GetClassLinker();
Narayan Kamath11d9f062014-04-23 20:24:57 +0100267 // TODO: We're assuming that the 64 and 32 bit runtimes have identical
268 // class paths. isDexOptNeeded will not necessarily be called on a runtime
269 // that has the same instruction set as the file being dexopted.
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700270 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
271 for (size_t i = 0; i < boot_class_path.size(); i++) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100272 if (boot_class_path[i]->GetLocation() == filename) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700273 if (kVerboseLogging) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100274 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800275 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700276 return JNI_FALSE;
277 }
278 }
279
Brian Carlstrome1ff1992014-05-18 22:37:51 -0700280 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
281
282 // Check if we have an odex file next to the dex file.
283 std::string odex_filename(DexFilenameToOdexFilename(filename, kRuntimeISA));
284 std::string error_msg;
285 std::unique_ptr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL, false,
286 &error_msg));
287 if (oat_file.get() == nullptr) {
288 if (kVerboseLogging) {
289 LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << filename
290 << "': " << error_msg;
291 }
292 error_msg.clear();
293 } else {
294 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename, NULL,
295 kReasonLogging);
296 if (oat_dex_file != nullptr) {
297 uint32_t location_checksum;
298 // If its not possible to read the classes.dex assume up-to-date as we won't be able to
299 // compile it anyway.
300 if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
301 if (kVerboseLogging) {
302 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: "
303 << filename << ": " << error_msg;
304 }
305 return JNI_FALSE;
306 }
307 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum,
308 target_instruction_set,
309 &error_msg)) {
310 if (kVerboseLogging) {
311 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename
312 << " has an up-to-date checksum compared to " << filename;
313 }
314 return JNI_FALSE;
315 } else {
316 if (kVerboseLogging) {
317 LOG(INFO) << "DexFile_isDexOptNeeded found precompiled file " << odex_filename
318 << " with an out-of-date checksum compared to " << filename
319 << ": " << error_msg;
320 }
321 error_msg.clear();
322 }
323 }
324 }
325
Dave Allison39c3bfb2014-01-28 18:33:52 -0800326 // Check the profile file. We need to rerun dex2oat if the profile has changed significantly
327 // since the last time, or it's new.
328 // If the 'defer' argument is true then this will be retried later. In this case we
329 // need to make sure that the profile file copy is not made so that we will get the
330 // same result second time.
Calin Juravlec1b643c2014-05-30 23:44:11 +0100331 if (Runtime::Current()->GetProfilerOptions().IsEnabled() && (pkgname != nullptr)) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100332 const std::string profile_file = GetDalvikCacheOrDie("profiles", false /* create_if_absent */)
333 + std::string("/") + pkgname;
334 const std::string profile_cache_dir = GetDalvikCacheOrDie("profile-cache",
335 false /* create_if_absent */);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800336
337 // Make the profile cache if it doesn't exist.
338 mkdir(profile_cache_dir.c_str(), 0700);
339
340 // The previous profile file (a copy of the profile the last time this was run) is
341 // in the dalvik-cache directory because this is owned by system. The profiles
342 // directory is owned by install so system cannot write files in there.
Narayan Kamath11d9f062014-04-23 20:24:57 +0100343 std::string prev_profile_file = profile_cache_dir + std::string("/") + pkgname;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800344
345 struct stat profstat, prevstat;
346 int e1 = stat(profile_file.c_str(), &profstat);
347 int e2 = stat(prev_profile_file.c_str(), &prevstat);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800348 if (e1 < 0) {
349 // No profile file, need to run dex2oat
Brian Carlstrom09881a82014-04-18 17:44:01 -0700350 if (kReasonLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800351 LOG(INFO) << "DexFile_isDexOptNeeded profile file " << profile_file << " doesn't exist";
352 }
353 return JNI_TRUE;
354 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300355
Dave Allison39c3bfb2014-01-28 18:33:52 -0800356 if (e2 == 0) {
357 // There is a previous profile file. Check if the profile has changed significantly.
Calin Juravle9dae5b42014-04-07 16:36:21 +0300358 // A change in profile is considered significant if X% (change_thr property) of the top K%
359 // (compile_thr property) samples has changed.
Calin Juravlec1b643c2014-05-30 23:44:11 +0100360 double top_k_threshold = Runtime::Current()->GetProfilerOptions().GetTopKThreshold();
361 double change_threshold = Runtime::Current()->GetProfilerOptions().GetTopKChangeThreshold();
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100362 double change_percent = 0.0;
363 ProfileFile new_profile, old_profile;
364 bool new_ok = new_profile.LoadFile(profile_file);
365 bool old_ok = old_profile.LoadFile(prev_profile_file);
366 if (!new_ok || !old_ok) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700367 if (kVerboseLogging) {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300368 LOG(INFO) << "DexFile_isDexOptNeeded Ignoring invalid profiles: "
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100369 << (new_ok ? "" : profile_file) << " " << (old_ok ? "" : prev_profile_file);
Calin Juravle9dae5b42014-04-07 16:36:21 +0300370 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300371 } else {
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100372 std::set<std::string> new_top_k, old_top_k;
373 new_profile.GetTopKSamples(new_top_k, top_k_threshold);
374 old_profile.GetTopKSamples(old_top_k, top_k_threshold);
375 if (new_top_k.empty()) {
376 if (kVerboseLogging) {
377 LOG(INFO) << "DexFile_isDexOptNeeded empty profile: " << profile_file;
378 }
379 // If the new topK is empty we shouldn't optimize so we leave the change_percent at 0.0.
380 } else {
381 std::set<std::string> diff;
382 std::set_difference(new_top_k.begin(), new_top_k.end(), old_top_k.begin(), old_top_k.end(),
383 std::inserter(diff, diff.end()));
384 // TODO: consider using the usedPercentage instead of the plain diff count.
385 change_percent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(new_top_k.size());
386 if (kVerboseLogging) {
387 std::set<std::string>::iterator end = diff.end();
388 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
389 LOG(INFO) << "DexFile_isDexOptNeeded new in topK: " << *it;
390 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300391 }
392 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800393 }
394
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100395 if (change_percent > change_threshold) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700396 if (kReasonLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800397 LOG(INFO) << "DexFile_isDexOptNeeded size of new profile file " << profile_file <<
Calin Juravle9dae5b42014-04-07 16:36:21 +0300398 " is significantly different from old profile file " << prev_profile_file << " (top "
Calin Juravlebb0b53f2014-05-23 17:33:29 +0100399 << top_k_threshold << "% samples changed in proportion of " << change_percent << "%)";
Dave Allison39c3bfb2014-01-28 18:33:52 -0800400 }
401 if (!defer) {
402 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
403 }
404 return JNI_TRUE;
405 }
406 } else {
407 // Previous profile does not exist. Make a copy of the current one.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700408 if (kVerboseLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800409 LOG(INFO) << "DexFile_isDexOptNeeded previous profile doesn't exist: " << prev_profile_file;
410 }
411 if (!defer) {
412 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
413 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800414 }
415 }
416
Brian Carlstroma004aa92012-02-08 18:05:09 -0800417 // Check if we have an oat file in the cache
Narayan Kamath11d9f062014-04-23 20:24:57 +0100418 const std::string cache_dir(GetDalvikCacheOrDie(instruction_set));
419 const std::string cache_location(
420 GetDalvikCacheFilenameOrDie(filename, cache_dir.c_str()));
421 oat_file.reset(OatFile::Open(cache_location, filename, NULL, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700422 if (oat_file.get() == nullptr) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700423 if (kReasonLogging) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700424 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Narayan Kamath11d9f062014-04-23 20:24:57 +0100425 << " does not exist for " << filename << ": " << error_msg;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700426 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800427 return JNI_TRUE;
428 }
429
Brian Carlstroma004aa92012-02-08 18:05:09 -0800430 uint32_t location_checksum;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100431 if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700432 if (kReasonLogging) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100433 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700434 << " (error " << error_msg << ")";
435 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800436 return JNI_TRUE;
437 }
438
Narayan Kamath11d9f062014-04-23 20:24:57 +0100439 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum,
Narayan Kamath52f84882014-05-02 10:10:39 +0100440 target_instruction_set, &error_msg)) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700441 if (kReasonLogging) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700442 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Narayan Kamath11d9f062014-04-23 20:24:57 +0100443 << " has out-of-date checksum compared to " << filename
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700444 << " (error " << error_msg << ")";
445 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800446 return JNI_TRUE;
447 }
448
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700449 if (kVerboseLogging) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800450 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Narayan Kamath11d9f062014-04-23 20:24:57 +0100451 << " is up-to-date for " << filename;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800452 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700453 CHECK(error_msg.empty()) << error_msg;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700454 return JNI_FALSE;
455}
456
Narayan Kamath11d9f062014-04-23 20:24:57 +0100457static jboolean DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename,
458 jstring javaPkgname, jstring javaInstructionSet, jboolean defer) {
459 ScopedUtfChars filename(env, javaFilename);
460 NullableScopedUtfChars pkgname(env, javaPkgname);
461 ScopedUtfChars instruction_set(env, javaInstructionSet);
462
463 return IsDexOptNeededInternal(env, filename.c_str(), pkgname.c_str(),
464 instruction_set.c_str(), defer);
465}
466
Dave Allison39c3bfb2014-01-28 18:33:52 -0800467// public API, NULL pkgname
Narayan Kamath11d9f062014-04-23 20:24:57 +0100468static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
469 const char* instruction_set = GetInstructionSetString(kRuntimeISA);
470 ScopedUtfChars filename(env, javaFilename);
471 return IsDexOptNeededInternal(env, filename.c_str(), nullptr /* pkgname */,
472 instruction_set, false /* defer */);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800473}
474
475
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700476static JNINativeMethod gMethods[] = {
Elliott Hughes2d983902014-02-04 16:17:13 -0800477 NATIVE_METHOD(DexFile, closeDexFile, "(J)V"),
478 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;J)Ljava/lang/Class;"),
479 NATIVE_METHOD(DexFile, getClassNameList, "(J)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700480 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Narayan Kamath11d9f062014-04-23 20:24:57 +0100481 NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)Z"),
Elliott Hughes2d983902014-02-04 16:17:13 -0800482 NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)J"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700483};
484
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700485void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700486 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700487}
488
489} // namespace art