blob: 8588c3ade8550df9957c1ac80992de74c6d8d1b1 [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"
Calin Juravle177b4292014-06-03 16:30:39 +010038#include "ScopedFd.h"
Ian Rogersc9818482012-01-11 08:52:51 -080039#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070040#include "ScopedUtfChars.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
Dave Allison39c3bfb2014-01-28 18:33:52 -080044#ifdef HAVE_ANDROID_OS
45#include "cutils/properties.h"
46#endif
47
Brian Carlstromf91c8c32011-09-21 17:30:34 -070048namespace art {
49
Brian Carlstromf91c8c32011-09-21 17:30:34 -070050// A smart pointer that provides read-only access to a Java string's UTF chars.
51// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
52// passed a null jstring. The correct idiom is:
53//
54// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070055// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070056// return NULL;
57// }
58// // ... use name.c_str()
59//
60// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
61class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080062 public:
63 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
64 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
65 }
66
67 ~NullableScopedUtfChars() {
68 if (mUtfChars) {
69 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070070 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080071 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070072
Elliott Hughesba8eee12012-01-24 20:25:24 -080073 const char* c_str() const {
74 return mUtfChars;
75 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070076
Elliott Hughesba8eee12012-01-24 20:25:24 -080077 size_t size() const {
78 return strlen(mUtfChars);
79 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070080
Elliott Hughesba8eee12012-01-24 20:25:24 -080081 // Element access.
82 const char& operator[](size_t n) const {
83 return mUtfChars[n];
84 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070085
Elliott Hughesba8eee12012-01-24 20:25:24 -080086 private:
87 JNIEnv* mEnv;
88 jstring mString;
89 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070090
Elliott Hughesba8eee12012-01-24 20:25:24 -080091 // Disallow copy and assignment.
92 NullableScopedUtfChars(const NullableScopedUtfChars&);
93 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070094};
95
Elliott Hughes2d983902014-02-04 16:17:13 -080096static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070097 ScopedUtfChars sourceName(env, javaSourceName);
98 if (sourceName.c_str() == NULL) {
99 return 0;
100 }
101 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700102 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700103 return 0;
104 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700105
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700106 uint32_t dex_location_checksum;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800107 uint32_t* dex_location_checksum_pointer = &dex_location_checksum;
Andreas Gampe329d1882014-04-08 10:32:19 -0700108 std::vector<std::string> error_msgs;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700109 std::string error_msg;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800110 if (!DexFile::GetChecksum(sourceName.c_str(), dex_location_checksum_pointer, &error_msg)) {
111 dex_location_checksum_pointer = NULL;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700112 }
113
114 ClassLinker* linker = Runtime::Current()->GetClassLinker();
115 const DexFile* dex_file;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700116 if (outputName.c_str() == nullptr) {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800117 // FindOrCreateOatFileForDexLocation can tolerate a missing dex_location_checksum
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700118 dex_file = linker->FindDexFileInOatFileFromDexLocation(sourceName.c_str(),
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700119 dex_location_checksum_pointer,
120 kRuntimeISA,
121 &error_msgs);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700122 } else {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800123 // FindOrCreateOatFileForDexLocation requires the dex_location_checksum
124 if (dex_location_checksum_pointer == NULL) {
125 ScopedObjectAccess soa(env);
126 DCHECK(!error_msg.empty());
127 ThrowIOException("%s", error_msg.c_str());
128 return 0;
129 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700130 dex_file = linker->FindOrCreateOatFileForDexLocation(sourceName.c_str(), dex_location_checksum,
Andreas Gampe329d1882014-04-08 10:32:19 -0700131 outputName.c_str(), &error_msgs);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700132 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700133 if (dex_file == nullptr) {
Vladimir Marko60836d52014-01-16 15:53:38 +0000134 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700135 CHECK(!error_msgs.empty());
136 // The most important message is at the end. So set up nesting by going forward, which will
137 // wrap the existing exception as a cause for the following one.
138 auto it = error_msgs.begin();
139 auto itEnd = error_msgs.end();
140 for ( ; it != itEnd; ++it) {
141 ThrowWrappedIOException("%s", it->c_str());
142 }
143
jeffhaoc393a4f2011-10-19 13:46:09 -0700144 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700145 }
Elliott Hughes2d983902014-02-04 16:17:13 -0800146 return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_file));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700147}
148
Elliott Hughes2d983902014-02-04 16:17:13 -0800149static const DexFile* toDexFile(jlong dex_file_address, JNIEnv* env) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700150 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Ian Rogers1eb512d2013-10-18 15:42:20 -0700151 if (UNLIKELY(dex_file == nullptr)) {
152 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800153 ThrowNullPointerException(NULL, "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700154 }
155 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700156}
157
Elliott Hughes2d983902014-02-04 16:17:13 -0800158static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 const DexFile* dex_file;
Ian Rogers1eb512d2013-10-18 15:42:20 -0700160 dex_file = toDexFile(cookie, env);
161 if (dex_file == nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700162 return;
163 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700164 ScopedObjectAccess soa(env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700165 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
166 return;
167 }
168 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700169}
170
Elliott Hughes0512f022012-03-15 22:10:52 -0700171static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
Elliott Hughes2d983902014-02-04 16:17:13 -0800172 jlong cookie) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700173 const DexFile* dex_file = toDexFile(cookie, env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700174 if (dex_file == NULL) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700175 VLOG(class_linker) << "Failed to find dex_file";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700176 return NULL;
177 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700178 ScopedUtfChars class_name(env, javaName);
179 if (class_name.c_str() == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700180 VLOG(class_linker) << "Failed to find class_name";
Brian Carlstromdf143242011-10-10 18:05:34 -0700181 return NULL;
182 }
Elliott Hughes95572412011-12-13 18:14:20 -0800183 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Ian Rogersee39a102013-09-19 02:56:49 -0700184 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700185 if (dex_class_def == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700186 VLOG(class_linker) << "Failed to find dex_class_def";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700187 return NULL;
188 }
Ian Rogers1eb512d2013-10-18 15:42:20 -0700189 ScopedObjectAccess soa(env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700190 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
191 class_linker->RegisterDexFile(*dex_file);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700192 StackHandleScope<1> hs(soa.Self());
193 Handle<mirror::ClassLoader> class_loader(
194 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700195 mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file,
196 *dex_class_def);
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700197 VLOG(class_linker) << "DexFile_defineClassNative returning " << result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700198 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700199}
200
Elliott Hughes2d983902014-02-04 16:17:13 -0800201static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700202 jobjectArray result = nullptr;
203 const DexFile* dex_file = toDexFile(cookie, env);
Ian Rogers46889ea2014-05-19 22:31:22 -0700204 if (dex_file != nullptr) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700205 result = env->NewObjectArray(dex_file->NumClassDefs(), WellKnownClasses::java_lang_String,
206 nullptr);
207 if (result != nullptr) {
208 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
209 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
Brian Carlstromcf790bb2014-05-28 11:09:10 -0700210 std::string descriptor(DescriptorToDot(dex_file->GetClassDescriptor(class_def)));
211 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
Ian Rogersdd157d72014-05-15 14:47:50 -0700212 if (jdescriptor.get() == nullptr) {
213 return nullptr;
214 }
215 env->SetObjectArrayElement(result, i, jdescriptor.get());
216 }
217 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700218 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700219 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700220}
221
Dave Allison39c3bfb2014-01-28 18:33:52 -0800222// Copy a profile file
223static void CopyProfileFile(const char* oldfile, const char* newfile) {
Calin Juravle177b4292014-06-03 16:30:39 +0100224 ScopedFd fd(open(oldfile, O_RDONLY));
225 if (fd.get() == -1) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800226 // If we can't open the file show the uid:gid of the this process to allow
227 // diagnosis of the problem.
228 LOG(ERROR) << "Failed to open profile file " << oldfile<< ". My uid:gid is "
229 << getuid() << ":" << getgid();
230 return;
231 }
232
233 // Create the copy with rw------- (only accessible by system)
Calin Juravle177b4292014-06-03 16:30:39 +0100234 ScopedFd fd2(open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600));
235 if (fd2.get() == -1) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800236 // If we can't open the file show the uid:gid of the this process to allow
237 // diagnosis of the problem.
238 LOG(ERROR) << "Failed to create/write prev profile file " << newfile << ". My uid:gid is "
239 << getuid() << ":" << getgid();
240 return;
241 }
242 char buf[4096];
243 while (true) {
Calin Juravle177b4292014-06-03 16:30:39 +0100244 int n = read(fd.get(), buf, sizeof(buf));
Dave Allison39c3bfb2014-01-28 18:33:52 -0800245 if (n <= 0) {
246 break;
247 }
Calin Juravle177b4292014-06-03 16:30:39 +0100248 write(fd2.get(), buf, n);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800249 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800250}
251
Calin Juravle9dae5b42014-04-07 16:36:21 +0300252static double GetDoubleProperty(const char* property, double minValue, double maxValue, double defaultValue) {
253#ifndef HAVE_ANDROID_OS
254 return defaultValue;
255#else
256 char buf[PROP_VALUE_MAX];
257 char* endptr;
258
259 property_get(property, buf, "");
260 double value = strtod(buf, &endptr);
261
262 if (value == 0 && endptr == buf) {
263 value = defaultValue;
264 } else if (value < minValue || value > maxValue) {
265 value = defaultValue;
266 }
267 return value;
268#endif
269}
270
Narayan Kamath11d9f062014-04-23 20:24:57 +0100271static jboolean IsDexOptNeededInternal(JNIEnv* env, const char* filename,
272 const char* pkgname, const char* instruction_set, const jboolean defer) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700273 const bool kVerboseLogging = false; // Spammy logging.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700274 const bool kReasonLogging = true; // Logging of reason for returning JNI_TRUE.
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800275
Narayan Kamath11d9f062014-04-23 20:24:57 +0100276 if ((filename == nullptr) || !OS::FileExists(filename)) {
277 LOG(ERROR) << "DexFile_isDexOptNeeded file '" << filename << "' does not exist";
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700278 ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100279 const char* message = (filename == nullptr) ? "<empty file name>" : filename;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700280 env->ThrowNew(fnfe.get(), message);
281 return JNI_FALSE;
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700282 }
283
284 // Always treat elements of the bootclasspath as up-to-date. The
285 // fact that code is running at all means that this should be true.
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700286 Runtime* runtime = Runtime::Current();
287 ClassLinker* class_linker = runtime->GetClassLinker();
Narayan Kamath11d9f062014-04-23 20:24:57 +0100288 // TODO: We're assuming that the 64 and 32 bit runtimes have identical
289 // class paths. isDexOptNeeded will not necessarily be called on a runtime
290 // that has the same instruction set as the file being dexopted.
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700291 const std::vector<const DexFile*>& boot_class_path = class_linker->GetBootClassPath();
292 for (size_t i = 0; i < boot_class_path.size(); i++) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100293 if (boot_class_path[i]->GetLocation() == filename) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700294 if (kVerboseLogging) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100295 LOG(INFO) << "DexFile_isDexOptNeeded ignoring boot class path file: " << filename;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800296 }
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -0700297 return JNI_FALSE;
298 }
299 }
300
Brian Carlstrome1ff1992014-05-18 22:37:51 -0700301 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
302
303 // Check if we have an odex file next to the dex file.
304 std::string odex_filename(DexFilenameToOdexFilename(filename, kRuntimeISA));
305 std::string error_msg;
306 std::unique_ptr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL, false,
307 &error_msg));
308 if (oat_file.get() == nullptr) {
309 if (kVerboseLogging) {
310 LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << filename
311 << "': " << error_msg;
312 }
313 error_msg.clear();
314 } else {
315 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename, NULL,
316 kReasonLogging);
317 if (oat_dex_file != nullptr) {
318 uint32_t location_checksum;
319 // If its not possible to read the classes.dex assume up-to-date as we won't be able to
320 // compile it anyway.
321 if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
322 if (kVerboseLogging) {
323 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: "
324 << filename << ": " << error_msg;
325 }
326 return JNI_FALSE;
327 }
328 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum,
329 target_instruction_set,
330 &error_msg)) {
331 if (kVerboseLogging) {
332 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename
333 << " has an up-to-date checksum compared to " << filename;
334 }
335 return JNI_FALSE;
336 } else {
337 if (kVerboseLogging) {
338 LOG(INFO) << "DexFile_isDexOptNeeded found precompiled file " << odex_filename
339 << " with an out-of-date checksum compared to " << filename
340 << ": " << error_msg;
341 }
342 error_msg.clear();
343 }
344 }
345 }
346
Dave Allison39c3bfb2014-01-28 18:33:52 -0800347 // Check the profile file. We need to rerun dex2oat if the profile has changed significantly
348 // since the last time, or it's new.
349 // If the 'defer' argument is true then this will be retried later. In this case we
350 // need to make sure that the profile file copy is not made so that we will get the
351 // same result second time.
Narayan Kamath11d9f062014-04-23 20:24:57 +0100352 if (pkgname != nullptr) {
353 const std::string profile_file = GetDalvikCacheOrDie("profiles", false /* create_if_absent */)
354 + std::string("/") + pkgname;
355 const std::string profile_cache_dir = GetDalvikCacheOrDie("profile-cache",
356 false /* create_if_absent */);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800357
358 // Make the profile cache if it doesn't exist.
359 mkdir(profile_cache_dir.c_str(), 0700);
360
361 // The previous profile file (a copy of the profile the last time this was run) is
362 // in the dalvik-cache directory because this is owned by system. The profiles
363 // directory is owned by install so system cannot write files in there.
Narayan Kamath11d9f062014-04-23 20:24:57 +0100364 std::string prev_profile_file = profile_cache_dir + std::string("/") + pkgname;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800365
366 struct stat profstat, prevstat;
367 int e1 = stat(profile_file.c_str(), &profstat);
368 int e2 = stat(prev_profile_file.c_str(), &prevstat);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800369 if (e1 < 0) {
370 // No profile file, need to run dex2oat
Brian Carlstrom09881a82014-04-18 17:44:01 -0700371 if (kReasonLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800372 LOG(INFO) << "DexFile_isDexOptNeeded profile file " << profile_file << " doesn't exist";
373 }
374 return JNI_TRUE;
375 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300376
Dave Allison39c3bfb2014-01-28 18:33:52 -0800377 if (e2 == 0) {
378 // There is a previous profile file. Check if the profile has changed significantly.
Calin Juravle9dae5b42014-04-07 16:36:21 +0300379 // A change in profile is considered significant if X% (change_thr property) of the top K%
380 // (compile_thr property) samples has changed.
Dave Allison39c3bfb2014-01-28 18:33:52 -0800381
Calin Juravle9dae5b42014-04-07 16:36:21 +0300382 double topKThreshold = GetDoubleProperty("dalvik.vm.profiler.dex2oat.compile_thr", 10.0, 90.0, 90.0);
383 double changeThreshold = GetDoubleProperty("dalvik.vm.profiler.dex2oat.change_thr", 1.0, 90.0, 10.0);
384 double changePercent = 0.0;
385 std::set<std::string> newTopK, oldTopK;
386 bool newOk = ProfileHelper::LoadTopKSamples(newTopK, profile_file, topKThreshold);
387 bool oldOk = ProfileHelper::LoadTopKSamples(oldTopK, prev_profile_file, topKThreshold);
388 if (!newOk || !oldOk) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700389 if (kVerboseLogging) {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300390 LOG(INFO) << "DexFile_isDexOptNeeded Ignoring invalid profiles: "
391 << (newOk ? "" : profile_file) << " " << (oldOk ? "" : prev_profile_file);
392 }
393 } else if (newTopK.empty()) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700394 if (kVerboseLogging) {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300395 LOG(INFO) << "DexFile_isDexOptNeeded empty profile: " << profile_file;
396 }
397 // If the new topK is empty we shouldn't optimize so we leave the changePercent at 0.0.
398 } else {
399 std::set<std::string> diff;
400 std::set_difference(newTopK.begin(), newTopK.end(), oldTopK.begin(), oldTopK.end(),
401 std::inserter(diff, diff.end()));
402 // TODO: consider using the usedPercentage instead of the plain diff count.
403 changePercent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(newTopK.size());
Brian Carlstrom09881a82014-04-18 17:44:01 -0700404 if (kVerboseLogging) {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300405 std::set<std::string>::iterator end = diff.end();
406 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
407 LOG(INFO) << "DexFile_isDexOptNeeded new in topK: " << *it;
408 }
409 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800410 }
411
Calin Juravle9dae5b42014-04-07 16:36:21 +0300412 if (changePercent > changeThreshold) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700413 if (kReasonLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800414 LOG(INFO) << "DexFile_isDexOptNeeded size of new profile file " << profile_file <<
Calin Juravle9dae5b42014-04-07 16:36:21 +0300415 " is significantly different from old profile file " << prev_profile_file << " (top "
416 << topKThreshold << "% samples changed in proportion of " << changePercent << "%)";
Dave Allison39c3bfb2014-01-28 18:33:52 -0800417 }
418 if (!defer) {
419 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
420 }
421 return JNI_TRUE;
422 }
423 } else {
424 // Previous profile does not exist. Make a copy of the current one.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700425 if (kVerboseLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800426 LOG(INFO) << "DexFile_isDexOptNeeded previous profile doesn't exist: " << prev_profile_file;
427 }
428 if (!defer) {
429 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
430 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800431 }
432 }
433
Brian Carlstroma004aa92012-02-08 18:05:09 -0800434 // Check if we have an oat file in the cache
Narayan Kamath11d9f062014-04-23 20:24:57 +0100435 const std::string cache_dir(GetDalvikCacheOrDie(instruction_set));
436 const std::string cache_location(
437 GetDalvikCacheFilenameOrDie(filename, cache_dir.c_str()));
438 oat_file.reset(OatFile::Open(cache_location, filename, NULL, false, &error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700439 if (oat_file.get() == nullptr) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700440 if (kReasonLogging) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700441 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Narayan Kamath11d9f062014-04-23 20:24:57 +0100442 << " does not exist for " << filename << ": " << error_msg;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700443 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800444 return JNI_TRUE;
445 }
446
Brian Carlstroma004aa92012-02-08 18:05:09 -0800447 uint32_t location_checksum;
Narayan Kamath11d9f062014-04-23 20:24:57 +0100448 if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700449 if (kReasonLogging) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100450 LOG(ERROR) << "DexFile_isDexOptNeeded failed to compute checksum of " << filename
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700451 << " (error " << error_msg << ")";
452 }
Brian Carlstroma004aa92012-02-08 18:05:09 -0800453 return JNI_TRUE;
454 }
455
Narayan Kamath11d9f062014-04-23 20:24:57 +0100456 if (!ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum,
Narayan Kamath52f84882014-05-02 10:10:39 +0100457 target_instruction_set, &error_msg)) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700458 if (kReasonLogging) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700459 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Narayan Kamath11d9f062014-04-23 20:24:57 +0100460 << " has out-of-date checksum compared to " << filename
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700461 << " (error " << error_msg << ")";
462 }
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800463 return JNI_TRUE;
464 }
465
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700466 if (kVerboseLogging) {
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800467 LOG(INFO) << "DexFile_isDexOptNeeded cache file " << cache_location
Narayan Kamath11d9f062014-04-23 20:24:57 +0100468 << " is up-to-date for " << filename;
Brian Carlstrombf2cb162012-02-27 17:49:19 -0800469 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700470 CHECK(error_msg.empty()) << error_msg;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700471 return JNI_FALSE;
472}
473
Narayan Kamath11d9f062014-04-23 20:24:57 +0100474static jboolean DexFile_isDexOptNeededInternal(JNIEnv* env, jclass, jstring javaFilename,
475 jstring javaPkgname, jstring javaInstructionSet, jboolean defer) {
476 ScopedUtfChars filename(env, javaFilename);
477 NullableScopedUtfChars pkgname(env, javaPkgname);
478 ScopedUtfChars instruction_set(env, javaInstructionSet);
479
480 return IsDexOptNeededInternal(env, filename.c_str(), pkgname.c_str(),
481 instruction_set.c_str(), defer);
482}
483
Dave Allison39c3bfb2014-01-28 18:33:52 -0800484// public API, NULL pkgname
Narayan Kamath11d9f062014-04-23 20:24:57 +0100485static jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) {
486 const char* instruction_set = GetInstructionSetString(kRuntimeISA);
487 ScopedUtfChars filename(env, javaFilename);
488 return IsDexOptNeededInternal(env, filename.c_str(), nullptr /* pkgname */,
489 instruction_set, false /* defer */);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800490}
491
492
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700493static JNINativeMethod gMethods[] = {
Elliott Hughes2d983902014-02-04 16:17:13 -0800494 NATIVE_METHOD(DexFile, closeDexFile, "(J)V"),
495 NATIVE_METHOD(DexFile, defineClassNative, "(Ljava/lang/String;Ljava/lang/ClassLoader;J)Ljava/lang/Class;"),
496 NATIVE_METHOD(DexFile, getClassNameList, "(J)[Ljava/lang/String;"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700497 NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"),
Narayan Kamath11d9f062014-04-23 20:24:57 +0100498 NATIVE_METHOD(DexFile, isDexOptNeededInternal, "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)Z"),
Elliott Hughes2d983902014-02-04 16:17:13 -0800499 NATIVE_METHOD(DexFile, openDexFileNative, "(Ljava/lang/String;Ljava/lang/String;I)J"),
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700500};
501
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700502void register_dalvik_system_DexFile(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700503 REGISTER_NATIVE_METHODS("dalvik/system/DexFile");
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700504}
505
506} // namespace art