blob: 716356e9a0032dca88b704001a6799ac603334ca [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"
Ian Rogersdd157d72014-05-15 14:47:50 -070040#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070041#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070042
Dave Allison39c3bfb2014-01-28 18:33:52 -080043#ifdef HAVE_ANDROID_OS
44#include "cutils/properties.h"
45#endif
46
Brian Carlstromf91c8c32011-09-21 17:30:34 -070047namespace art {
48
Brian Carlstromf91c8c32011-09-21 17:30:34 -070049// A smart pointer that provides read-only access to a Java string's UTF chars.
50// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
51// passed a null jstring. The correct idiom is:
52//
53// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070054// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070055// return NULL;
56// }
57// // ... use name.c_str()
58//
59// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
60class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080061 public:
62 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
63 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
64 }
65
66 ~NullableScopedUtfChars() {
67 if (mUtfChars) {
68 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070069 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080070 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070071
Elliott Hughesba8eee12012-01-24 20:25:24 -080072 const char* c_str() const {
73 return mUtfChars;
74 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070075
Elliott Hughesba8eee12012-01-24 20:25:24 -080076 size_t size() const {
77 return strlen(mUtfChars);
78 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070079
Elliott Hughesba8eee12012-01-24 20:25:24 -080080 // Element access.
81 const char& operator[](size_t n) const {
82 return mUtfChars[n];
83 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070084
Elliott Hughesba8eee12012-01-24 20:25:24 -080085 private:
86 JNIEnv* mEnv;
87 jstring mString;
88 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070089
Elliott Hughesba8eee12012-01-24 20:25:24 -080090 // Disallow copy and assignment.
91 NullableScopedUtfChars(const NullableScopedUtfChars&);
92 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070093};
94
Elliott Hughes2d983902014-02-04 16:17:13 -080095static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070096 ScopedUtfChars sourceName(env, javaSourceName);
97 if (sourceName.c_str() == NULL) {
98 return 0;
99 }
100 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700101 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700102 return 0;
103 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700104
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700105 uint32_t dex_location_checksum;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800106 uint32_t* dex_location_checksum_pointer = &dex_location_checksum;
Andreas Gampe329d1882014-04-08 10:32:19 -0700107 std::vector<std::string> error_msgs;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700108 std::string error_msg;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800109 if (!DexFile::GetChecksum(sourceName.c_str(), dex_location_checksum_pointer, &error_msg)) {
110 dex_location_checksum_pointer = NULL;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700111 }
112
113 ClassLinker* linker = Runtime::Current()->GetClassLinker();
114 const DexFile* dex_file;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700115 if (outputName.c_str() == nullptr) {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800116 // FindOrCreateOatFileForDexLocation can tolerate a missing dex_location_checksum
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700117 dex_file = linker->FindDexFileInOatFileFromDexLocation(sourceName.c_str(),
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700118 dex_location_checksum_pointer,
119 kRuntimeISA,
120 &error_msgs);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700121 } else {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800122 // FindOrCreateOatFileForDexLocation requires the dex_location_checksum
123 if (dex_location_checksum_pointer == NULL) {
124 ScopedObjectAccess soa(env);
125 DCHECK(!error_msg.empty());
126 ThrowIOException("%s", error_msg.c_str());
127 return 0;
128 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700129 dex_file = linker->FindOrCreateOatFileForDexLocation(sourceName.c_str(), dex_location_checksum,
Andreas Gampe329d1882014-04-08 10:32:19 -0700130 outputName.c_str(), &error_msgs);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700131 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700132 if (dex_file == nullptr) {
Vladimir Marko60836d52014-01-16 15:53:38 +0000133 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700134 CHECK(!error_msgs.empty());
135 // The most important message is at the end. So set up nesting by going forward, which will
136 // wrap the existing exception as a cause for the following one.
137 auto it = error_msgs.begin();
138 auto itEnd = error_msgs.end();
139 for ( ; it != itEnd; ++it) {
140 ThrowWrappedIOException("%s", it->c_str());
141 }
142
jeffhaoc393a4f2011-10-19 13:46:09 -0700143 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700144 }
Elliott Hughes2d983902014-02-04 16:17:13 -0800145 return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_file));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700146}
147
Elliott Hughes2d983902014-02-04 16:17:13 -0800148static const DexFile* toDexFile(jlong dex_file_address, JNIEnv* env) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700149 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Ian Rogers1eb512d2013-10-18 15:42:20 -0700150 if (UNLIKELY(dex_file == nullptr)) {
151 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800152 ThrowNullPointerException(NULL, "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700153 }
154 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700155}
156
Elliott Hughes2d983902014-02-04 16:17:13 -0800157static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 const DexFile* dex_file;
Ian Rogers1eb512d2013-10-18 15:42:20 -0700159 dex_file = toDexFile(cookie, env);
160 if (dex_file == nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700161 return;
162 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700163 ScopedObjectAccess soa(env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700164 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
165 return;
166 }
167 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700168}
169
Elliott Hughes0512f022012-03-15 22:10:52 -0700170static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
Elliott Hughes2d983902014-02-04 16:17:13 -0800171 jlong cookie) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700172 const DexFile* dex_file = toDexFile(cookie, env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700173 if (dex_file == NULL) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700174 VLOG(class_linker) << "Failed to find dex_file";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700175 return NULL;
176 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700177 ScopedUtfChars class_name(env, javaName);
178 if (class_name.c_str() == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700179 VLOG(class_linker) << "Failed to find class_name";
Brian Carlstromdf143242011-10-10 18:05:34 -0700180 return NULL;
181 }
Elliott Hughes95572412011-12-13 18:14:20 -0800182 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Ian Rogersee39a102013-09-19 02:56:49 -0700183 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700184 if (dex_class_def == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700185 VLOG(class_linker) << "Failed to find dex_class_def";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700186 return NULL;
187 }
Ian Rogers1eb512d2013-10-18 15:42:20 -0700188 ScopedObjectAccess soa(env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700189 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
190 class_linker->RegisterDexFile(*dex_file);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700191 StackHandleScope<1> hs(soa.Self());
192 Handle<mirror::ClassLoader> class_loader(
193 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700194 mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file,
195 *dex_class_def);
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700196 VLOG(class_linker) << "DexFile_defineClassNative returning " << result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700197 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700198}
199
Elliott Hughes2d983902014-02-04 16:17:13 -0800200static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700201 jobjectArray result = nullptr;
202 const DexFile* dex_file = toDexFile(cookie, env);
Ian Rogers46889ea2014-05-19 22:31:22 -0700203 if (dex_file != nullptr) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700204 result = env->NewObjectArray(dex_file->NumClassDefs(), WellKnownClasses::java_lang_String,
205 nullptr);
206 if (result != nullptr) {
207 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
208 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
209 const char* descriptor = dex_file->GetClassDescriptor(class_def);
210 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor));
211 if (jdescriptor.get() == nullptr) {
212 return nullptr;
213 }
214 env->SetObjectArrayElement(result, i, jdescriptor.get());
215 }
216 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700217 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700218 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700219}
220
Dave Allison39c3bfb2014-01-28 18:33:52 -0800221// Copy a profile file
222static void CopyProfileFile(const char* oldfile, const char* newfile) {
223 int fd = open(oldfile, O_RDONLY);
224 if (fd < 0) {
225 // If we can't open the file show the uid:gid of the this process to allow
226 // diagnosis of the problem.
227 LOG(ERROR) << "Failed to open profile file " << oldfile<< ". My uid:gid is "
228 << getuid() << ":" << getgid();
229 return;
230 }
231
232 // Create the copy with rw------- (only accessible by system)
233 int fd2 = open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
234 if (fd2 < 0) {
235 // If we can't open the file show the uid:gid of the this process to allow
236 // diagnosis of the problem.
237 LOG(ERROR) << "Failed to create/write prev profile file " << newfile << ". My uid:gid is "
238 << getuid() << ":" << getgid();
239 return;
240 }
241 char buf[4096];
242 while (true) {
243 int n = read(fd, buf, sizeof(buf));
244 if (n <= 0) {
245 break;
246 }
247 write(fd2, buf, n);
248 }
249 close(fd);
250 close(fd2);
251}
252
Calin Juravle9dae5b42014-04-07 16:36:21 +0300253static double GetDoubleProperty(const char* property, double minValue, double maxValue, double defaultValue) {
254#ifndef HAVE_ANDROID_OS
255 return defaultValue;
256#else
257 char buf[PROP_VALUE_MAX];
258 char* endptr;
259
260 property_get(property, buf, "");
261 double value = strtod(buf, &endptr);
262
263 if (value == 0 && endptr == buf) {
264 value = defaultValue;
265 } else if (value < minValue || value > maxValue) {
266 value = defaultValue;
267 }
268 return value;
269#endif
270}
271
Narayan Kamath11d9f062014-04-23 20:24:57 +0100272static jboolean IsDexOptNeededInternal(JNIEnv* env, const char* filename,
273 const char* pkgname, const char* instruction_set, const jboolean defer) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700274 const bool kVerboseLogging = false; // Spammy logging.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700275 const bool kReasonLogging = true; // Logging of reason for returning JNI_TRUE.
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800276
Narayan Kamath11d9f062014-04-23 20:24:57 +0100277 if ((filename == nullptr) || !OS::FileExists(filename)) {
278 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700279 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100280 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700281 env->ThrowNew(fnfe.get(), message);
282 return JNI_FALSE;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700283 }
284
285 // Always treat elements of the bootclasspath as up-to-date. The
286 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700287 Runtime* runtime = Runtime::Current();
288 ClassLinker* class_linker = runtime->GetClassLinker();
Narayan Kamath11d9f062014-04-23 20:24:57 +0100289 // TODO: We're assuming that the 64 and 32 bit runtimes have identical
290 // class paths. isDexOptNeeded will not necessarily be called on a runtime
291 // that has the same instruction set as the file being dexopted.
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700292 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
293 for (size_t i = 0; i < boot_class_path.size(); i++) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100294 if (boot_class_path[i]->GetLocation() == filename) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700295 if (kVerboseLogging) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100296 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800297 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700298 return JNI_FALSE;
299 }
300 }
301
Dave Allison39c3bfb2014-01-28 18:33:52 -0800302 // Check the profile file. We need to rerun dex2oat if the profile has changed significantly
303 // since the last time, or it's new.
304 // If the 'defer' argument is true then this will be retried later. In this case we
305 // need to make sure that the profile file copy is not made so that we will get the
306 // same result second time.
Narayan Kamath11d9f062014-04-23 20:24:57 +0100307 if (pkgname != nullptr) {
308 const std::string profile_file = GetDalvikCacheOrDie("profiles", false /* create_if_absent */)
309 + std::string("/") + pkgname;
310 const std::string profile_cache_dir = GetDalvikCacheOrDie("profile-cache",
311 false /* create_if_absent */);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800312
313 // Make the profile cache if it doesn't exist.
314 mkdir(profile_cache_dir.c_str(), 0700);
315
316 // The previous profile file (a copy of the profile the last time this was run) is
317 // in the dalvik-cache directory because this is owned by system. The profiles
318 // directory is owned by install so system cannot write files in there.
Narayan Kamath11d9f062014-04-23 20:24:57 +0100319 std::string prev_profile_file = profile_cache_dir + std::string("/") + pkgname;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800320
321 struct stat profstat, prevstat;
322 int e1 = stat(profile_file.c_str(), &profstat);
323 int e2 = stat(prev_profile_file.c_str(), &prevstat);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800324 if (e1 < 0) {
325 // No profile file, need to run dex2oat
Brian Carlstrom09881a82014-04-18 17:44:01 -0700326 if (kReasonLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800327 LOG(INFO) << "DexFile_isDexOptNeeded profile file " << profile_file << " doesn't exist";
328 }
329 return JNI_TRUE;
330 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300331
Dave Allison39c3bfb2014-01-28 18:33:52 -0800332 if (e2 == 0) {
333 // There is a previous profile file. Check if the profile has changed significantly.
Calin Juravle9dae5b42014-04-07 16:36:21 +0300334 // A change in profile is considered significant if X% (change_thr property) of the top K%
335 // (compile_thr property) samples has changed.
Dave Allison39c3bfb2014-01-28 18:33:52 -0800336
Calin Juravle9dae5b42014-04-07 16:36:21 +0300337 double topKThreshold = GetDoubleProperty("dalvik.vm.profiler.dex2oat.compile_thr", 10.0, 90.0, 90.0);
338 double changeThreshold = GetDoubleProperty("dalvik.vm.profiler.dex2oat.change_thr", 1.0, 90.0, 10.0);
339 double changePercent = 0.0;
340 std::set<std::string> newTopK, oldTopK;
341 bool newOk = ProfileHelper::LoadTopKSamples(newTopK, profile_file, topKThreshold);
342 bool oldOk = ProfileHelper::LoadTopKSamples(oldTopK, prev_profile_file, topKThreshold);
343 if (!newOk || !oldOk) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700344 if (kVerboseLogging) {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300345 LOG(INFO) << "DexFile_isDexOptNeeded Ignoring invalid profiles: "
346 << (newOk ? "" : profile_file) << " " << (oldOk ? "" : prev_profile_file);
347 }
348 } else if (newTopK.empty()) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700349 if (kVerboseLogging) {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300350 LOG(INFO) << "DexFile_isDexOptNeeded empty profile: " << profile_file;
351 }
352 // If the new topK is empty we shouldn't optimize so we leave the changePercent at 0.0.
353 } else {
354 std::set<std::string> diff;
355 std::set_difference(newTopK.begin(), newTopK.end(), oldTopK.begin(), oldTopK.end(),
356 std::inserter(diff, diff.end()));
357 // TODO: consider using the usedPercentage instead of the plain diff count.
358 changePercent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(newTopK.size());
Brian Carlstrom09881a82014-04-18 17:44:01 -0700359 if (kVerboseLogging) {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300360 std::set<std::string>::iterator end = diff.end();
361 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
362 LOG(INFO) << "DexFile_isDexOptNeeded new in topK: " << *it;
363 }
364 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800365 }
366
Calin Juravle9dae5b42014-04-07 16:36:21 +0300367 if (changePercent > changeThreshold) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700368 if (kReasonLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800369 LOG(INFO) << "DexFile_isDexOptNeeded size of new profile file " << profile_file <<
Calin Juravle9dae5b42014-04-07 16:36:21 +0300370 " is significantly different from old profile file " << prev_profile_file << " (top "
371 << topKThreshold << "% samples changed in proportion of " << changePercent << "%)";
Dave Allison39c3bfb2014-01-28 18:33:52 -0800372 }
373 if (!defer) {
374 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
375 }
376 return JNI_TRUE;
377 }
378 } else {
379 // Previous profile does not exist. Make a copy of the current one.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700380 if (kVerboseLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800381 LOG(INFO) << "DexFile_isDexOptNeeded previous profile doesn't exist: " << prev_profile_file;
382 }
383 if (!defer) {
384 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
385 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800386 }
387 }
388
Narayan Kamath52f84882014-05-02 10:10:39 +0100389 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
390
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700391 // Check if we have an odex file next to the dex file.
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700392 std::string odex_filename(DexFilenameToOdexFilename(filename, kRuntimeISA));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700393 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700394 std::unique_ptr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL, false,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700395 &error_msg));
396 if (oat_file.get() == nullptr) {
397 if (kVerboseLogging) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100398 LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << filename
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700399 << "': " << error_msg;
400 }
401 error_msg.clear();
402 } else {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100403 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename, NULL,
Brian Carlstrom09881a82014-04-18 17:44:01 -0700404 kReasonLogging);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700405 if (oat_dex_file != nullptr) {
Ian Rogers33e95662013-05-20 20:29:14 -0700406 uint32_t location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700407 // If its not possible to read the classes.dex assume up-to-date as we won't be able to
408 // compile it anyway.
Narayan Kamath11d9f062014-04-23 20:24:57 +0100409 if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700410 if (kVerboseLogging) {
Ian Rogers33e95662013-05-20 20:29:14 -0700411 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: "
Narayan Kamath11d9f062014-04-23 20:24:57 +0100412 << filename << ": " << error_msg;
Ian Rogers33e95662013-05-20 20:29:14 -0700413 }
414 return JNI_FALSE;
415 }
Narayan Kamath11d9f062014-04-23 20:24:57 +0100416 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum,
Narayan Kamath52f84882014-05-02 10:10:39 +0100417 target_instruction_set,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700418 &error_msg)) {
419 if (kVerboseLogging) {
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700420 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename
Narayan Kamath11d9f062014-04-23 20:24:57 +0100421 << " has an up-to-date checksum compared to " << filename;
Ian Rogers33e95662013-05-20 20:29:14 -0700422 }
423 return JNI_FALSE;
Brian Carlstrom0d3bbff2013-10-28 15:21:32 -0700424 } else {
425 if (kVerboseLogging) {
426 LOG(INFO) << "DexFile_isDexOptNeeded found precompiled file " << odex_filename
Narayan Kamath11d9f062014-04-23 20:24:57 +0100427 << " with an out-of-date checksum compared to " << filename
Brian Carlstrom0d3bbff2013-10-28 15:21:32 -0700428 << ": " << error_msg;
429 }
430 error_msg.clear();
Ian Rogers33e95662013-05-20 20:29:14 -0700431 }
Brian Carlstromafe25512012-06-27 17:02:58 -0700432 }
Brian Carlstromfad71432011-10-16 20:25:10 -0700433 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800434
Brian Carlstroma004aa92012-02-08 18:05:09 -0800435 // Check if we have an oat file in the cache
Narayan Kamath11d9f062014-04-23 20:24:57 +0100436 const std::string cache_dir(GetDalvikCacheOrDie(instruction_set));
437 const std::string cache_location(
438 GetDalvikCacheFilenameOrDie(filename, cache_dir.c_str()));
439 oat_file.reset(OatFile::Open(cache_location, filename, NULL, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700440 if (oat_file.get() == nullptr) {
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 << " does not exist for " << filename << ": " << error_msg;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700444 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800445 return JNI_TRUE;
446 }
447
Brian Carlstroma004aa92012-02-08 18:05:09 -0800448 uint32_t location_checksum;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100449 if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700450 if (kReasonLogging) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100451 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700452 << " (error " << error_msg << ")";
453 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800454 return JNI_TRUE;
455 }
456
Narayan Kamath11d9f062014-04-23 20:24:57 +0100457 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum,
Narayan Kamath52f84882014-05-02 10:10:39 +0100458 target_instruction_set, &error_msg)) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700459 if (kReasonLogging) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700460 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Narayan Kamath11d9f062014-04-23 20:24:57 +0100461 << " has out-of-date checksum compared to " << filename
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700462 << " (error " << error_msg << ")";
463 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800464 return JNI_TRUE;
465 }
466
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700467 if (kVerboseLogging) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800468 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Narayan Kamath11d9f062014-04-23 20:24:57 +0100469 << " is up-to-date for " << filename;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800470 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700471 CHECK(error_msg.empty()) << error_msg;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700472 return JNI_FALSE;
473}
474
Narayan Kamath11d9f062014-04-23 20:24:57 +0100475static jboolean DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename,
476 jstring javaPkgname, jstring javaInstructionSet, jboolean defer) {
477 ScopedUtfChars filename(env, javaFilename);
478 NullableScopedUtfChars pkgname(env, javaPkgname);
479 ScopedUtfChars instruction_set(env, javaInstructionSet);
480
481 return IsDexOptNeededInternal(env, filename.c_str(), pkgname.c_str(),
482 instruction_set.c_str(), defer);
483}
484
Dave Allison39c3bfb2014-01-28 18:33:52 -0800485// public API, NULL pkgname
Narayan Kamath11d9f062014-04-23 20:24:57 +0100486static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
487 const char* instruction_set = GetInstructionSetString(kRuntimeISA);
488 ScopedUtfChars filename(env, javaFilename);
489 return IsDexOptNeededInternal(env, filename.c_str(), nullptr /* pkgname */,
490 instruction_set, false /* defer */);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800491}
492
493
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700494static JNINativeMethod gMethods[] = {
Elliott Hughes2d983902014-02-04 16:17:13 -0800495 NATIVE_METHOD(DexFile, closeDexFile, "(J)V"),
496 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;J)Ljava/lang/Class;"),
497 NATIVE_METHOD(DexFile, getClassNameList, "(J)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700498 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Narayan Kamath11d9f062014-04-23 20:24:57 +0100499 NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)Z"),
Elliott Hughes2d983902014-02-04 16:17:13 -0800500 NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)J"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700501};
502
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700503void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700504 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700505}
506
507} // namespace art