blob: 981ea0e9bd01c59d63361e9483a69d41205b82f7 [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 Juravle52214102014-06-04 12:01:50 +010020#include <sys/sendfile.h>
21#include <sys/stat.h>
Calin Juravle9dae5b42014-04-07 16:36:21 +030022#include <unistd.h>
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070023
Elliott Hughes07ed66b2012-12-12 18:34:25 -080024#include "base/logging.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070025#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080026#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/space/image_space.h"
29#include "gc/space/space-inl.h"
Brian Carlstrom81f3ca12012-03-17 00:27:35 -070030#include "image.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070031#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/class_loader.h"
Ian Rogers05f30572013-02-20 12:13:11 -080033#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/string.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080035#include "oat.h"
Brian Carlstrom1d9f52b2011-10-13 10:50:45 -070036#include "os.h"
Calin Juravle9dae5b42014-04-07 16:36:21 +030037#include "profiler.h"
Brian Carlstromaded5f72011-10-07 17:15:04 -070038#include "runtime.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070039#include "scoped_thread_state_change.h"
Calin Juravle177b4292014-06-03 16:30:39 +010040#include "ScopedFd.h"
Ian Rogersc9818482012-01-11 08:52:51 -080041#include "ScopedLocalRef.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070042#include "ScopedUtfChars.h"
Ian Rogersdd157d72014-05-15 14:47:50 -070043#include "well_known_classes.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070044#include "zip_archive.h"
Brian Carlstromf91c8c32011-09-21 17:30:34 -070045
Dave Allison39c3bfb2014-01-28 18:33:52 -080046#ifdef HAVE_ANDROID_OS
47#include "cutils/properties.h"
48#endif
49
Brian Carlstromf91c8c32011-09-21 17:30:34 -070050namespace art {
51
Brian Carlstromf91c8c32011-09-21 17:30:34 -070052// A smart pointer that provides read-only access to a Java string's UTF chars.
53// Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if
54// passed a null jstring. The correct idiom is:
55//
56// NullableScopedUtfChars name(env, javaName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -070057// if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070058// return NULL;
59// }
60// // ... use name.c_str()
61//
62// TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option.
63class NullableScopedUtfChars {
Elliott Hughesba8eee12012-01-24 20:25:24 -080064 public:
65 NullableScopedUtfChars(JNIEnv* env, jstring s) : mEnv(env), mString(s) {
66 mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL;
67 }
68
69 ~NullableScopedUtfChars() {
70 if (mUtfChars) {
71 mEnv->ReleaseStringUTFChars(mString, mUtfChars);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070072 }
Elliott Hughesba8eee12012-01-24 20:25:24 -080073 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070074
Elliott Hughesba8eee12012-01-24 20:25:24 -080075 const char* c_str() const {
76 return mUtfChars;
77 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070078
Elliott Hughesba8eee12012-01-24 20:25:24 -080079 size_t size() const {
80 return strlen(mUtfChars);
81 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070082
Elliott Hughesba8eee12012-01-24 20:25:24 -080083 // Element access.
84 const char& operator[](size_t n) const {
85 return mUtfChars[n];
86 }
Brian Carlstromf91c8c32011-09-21 17:30:34 -070087
Elliott Hughesba8eee12012-01-24 20:25:24 -080088 private:
89 JNIEnv* mEnv;
90 jstring mString;
91 const char* mUtfChars;
Brian Carlstromf91c8c32011-09-21 17:30:34 -070092
Elliott Hughesba8eee12012-01-24 20:25:24 -080093 // Disallow copy and assignment.
94 NullableScopedUtfChars(const NullableScopedUtfChars&);
95 void operator=(const NullableScopedUtfChars&);
Brian Carlstromf91c8c32011-09-21 17:30:34 -070096};
97
Elliott Hughes2d983902014-02-04 16:17:13 -080098static jlong DexFile_openDexFileNative(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName, jint) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -070099 ScopedUtfChars sourceName(env, javaSourceName);
100 if (sourceName.c_str() == NULL) {
101 return 0;
102 }
103 NullableScopedUtfChars outputName(env, javaOutputName);
Brian Carlstromc252c3e2011-10-16 23:21:02 -0700104 if (env->ExceptionCheck()) {
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700105 return 0;
106 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700107
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700108 uint32_t dex_location_checksum;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800109 uint32_t* dex_location_checksum_pointer = &dex_location_checksum;
Andreas Gampe329d1882014-04-08 10:32:19 -0700110 std::vector<std::string> error_msgs;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700111 std::string error_msg;
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800112 if (!DexFile::GetChecksum(sourceName.c_str(), dex_location_checksum_pointer, &error_msg)) {
113 dex_location_checksum_pointer = NULL;
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700114 }
115
116 ClassLinker* linker = Runtime::Current()->GetClassLinker();
117 const DexFile* dex_file;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700118 if (outputName.c_str() == nullptr) {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800119 // FindOrCreateOatFileForDexLocation can tolerate a missing dex_location_checksum
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700120 dex_file = linker->FindDexFileInOatFileFromDexLocation(sourceName.c_str(),
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700121 dex_location_checksum_pointer,
122 kRuntimeISA,
123 &error_msgs);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700124 } else {
Brian Carlstrom08cbf662013-12-10 16:52:57 -0800125 // FindOrCreateOatFileForDexLocation requires the dex_location_checksum
126 if (dex_location_checksum_pointer == NULL) {
127 ScopedObjectAccess soa(env);
128 DCHECK(!error_msg.empty());
129 ThrowIOException("%s", error_msg.c_str());
130 return 0;
131 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700132 dex_file = linker->FindOrCreateOatFileForDexLocation(sourceName.c_str(), dex_location_checksum,
Andreas Gampe329d1882014-04-08 10:32:19 -0700133 outputName.c_str(), &error_msgs);
Brian Carlstrom756ee4e2013-10-03 15:46:12 -0700134 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700135 if (dex_file == nullptr) {
Vladimir Marko60836d52014-01-16 15:53:38 +0000136 ScopedObjectAccess soa(env);
Andreas Gampe329d1882014-04-08 10:32:19 -0700137 CHECK(!error_msgs.empty());
138 // The most important message is at the end. So set up nesting by going forward, which will
139 // wrap the existing exception as a cause for the following one.
140 auto it = error_msgs.begin();
141 auto itEnd = error_msgs.end();
142 for ( ; it != itEnd; ++it) {
143 ThrowWrappedIOException("%s", it->c_str());
144 }
145
jeffhaoc393a4f2011-10-19 13:46:09 -0700146 return 0;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700147 }
Elliott Hughes2d983902014-02-04 16:17:13 -0800148 return static_cast<jlong>(reinterpret_cast<uintptr_t>(dex_file));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700149}
150
Elliott Hughes2d983902014-02-04 16:17:13 -0800151static const DexFile* toDexFile(jlong dex_file_address, JNIEnv* env) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700152 const DexFile* dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>(dex_file_address));
Ian Rogers1eb512d2013-10-18 15:42:20 -0700153 if (UNLIKELY(dex_file == nullptr)) {
154 ScopedObjectAccess soa(env);
Ian Rogers62d6c772013-02-27 08:32:07 -0800155 ThrowNullPointerException(NULL, "dex_file == null");
Brian Carlstromaded5f72011-10-07 17:15:04 -0700156 }
157 return dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700158}
159
Elliott Hughes2d983902014-02-04 16:17:13 -0800160static void DexFile_closeDexFile(JNIEnv* env, jclass, jlong cookie) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700161 const DexFile* dex_file;
Ian Rogers1eb512d2013-10-18 15:42:20 -0700162 dex_file = toDexFile(cookie, env);
163 if (dex_file == nullptr) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700164 return;
165 }
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700166 ScopedObjectAccess soa(env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700167 if (Runtime::Current()->GetClassLinker()->IsDexFileRegistered(*dex_file)) {
168 return;
169 }
170 delete dex_file;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700171}
172
Elliott Hughes0512f022012-03-15 22:10:52 -0700173static jclass DexFile_defineClassNative(JNIEnv* env, jclass, jstring javaName, jobject javaLoader,
Elliott Hughes2d983902014-02-04 16:17:13 -0800174 jlong cookie) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700175 const DexFile* dex_file = toDexFile(cookie, env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700176 if (dex_file == NULL) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700177 VLOG(class_linker) << "Failed to find dex_file";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700178 return NULL;
179 }
Brian Carlstromdf143242011-10-10 18:05:34 -0700180 ScopedUtfChars class_name(env, javaName);
181 if (class_name.c_str() == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700182 VLOG(class_linker) << "Failed to find class_name";
Brian Carlstromdf143242011-10-10 18:05:34 -0700183 return NULL;
184 }
Elliott Hughes95572412011-12-13 18:14:20 -0800185 const std::string descriptor(DotToDescriptor(class_name.c_str()));
Ian Rogersee39a102013-09-19 02:56:49 -0700186 const DexFile::ClassDef* dex_class_def = dex_file->FindClassDef(descriptor.c_str());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700187 if (dex_class_def == NULL) {
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700188 VLOG(class_linker) << "Failed to find dex_class_def";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700189 return NULL;
190 }
Ian Rogers1eb512d2013-10-18 15:42:20 -0700191 ScopedObjectAccess soa(env);
Brian Carlstromaded5f72011-10-07 17:15:04 -0700192 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
193 class_linker->RegisterDexFile(*dex_file);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700194 StackHandleScope<1> hs(soa.Self());
195 Handle<mirror::ClassLoader> class_loader(
196 hs.NewHandle(soa.Decode<mirror::ClassLoader*>(javaLoader)));
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700197 mirror::Class* result = class_linker->DefineClass(descriptor.c_str(), class_loader, *dex_file,
198 *dex_class_def);
Brian Carlstrom2e450bf2013-09-06 15:39:46 -0700199 VLOG(class_linker) << "DexFile_defineClassNative returning " << result;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 return soa.AddLocalReference<jclass>(result);
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700201}
202
Elliott Hughes2d983902014-02-04 16:17:13 -0800203static jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jlong cookie) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700204 jobjectArray result = nullptr;
205 const DexFile* dex_file = toDexFile(cookie, env);
Ian Rogers46889ea2014-05-19 22:31:22 -0700206 if (dex_file != nullptr) {
Ian Rogersdd157d72014-05-15 14:47:50 -0700207 result = env->NewObjectArray(dex_file->NumClassDefs(), WellKnownClasses::java_lang_String,
208 nullptr);
209 if (result != nullptr) {
210 for (size_t i = 0; i < dex_file->NumClassDefs(); ++i) {
211 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
Brian Carlstromcf790bb2014-05-28 11:09:10 -0700212 std::string descriptor(DescriptorToDot(dex_file->GetClassDescriptor(class_def)));
213 ScopedLocalRef<jstring> jdescriptor(env, env->NewStringUTF(descriptor.c_str()));
Ian Rogersdd157d72014-05-15 14:47:50 -0700214 if (jdescriptor.get() == nullptr) {
215 return nullptr;
216 }
217 env->SetObjectArrayElement(result, i, jdescriptor.get());
218 }
219 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700220 }
Ian Rogersdd157d72014-05-15 14:47:50 -0700221 return result;
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700222}
223
Dave Allison39c3bfb2014-01-28 18:33:52 -0800224static void CopyProfileFile(const char* oldfile, const char* newfile) {
Calin Juravle52214102014-06-04 12:01:50 +0100225 ScopedFd src(open(oldfile, O_RDONLY));
226 if (src.get() == -1) {
227 PLOG(ERROR) << "Failed to open profile file " << oldfile
228 << ". My uid:gid is " << getuid() << ":" << getgid();
229 return;
230 }
231
232 struct stat stat_src;
233 if (fstat(src.get(), &stat_src) == -1) {
234 PLOG(ERROR) << "Failed to get stats for profile file " << oldfile
235 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800236 return;
237 }
238
239 // Create the copy with rw------- (only accessible by system)
Calin Juravle52214102014-06-04 12:01:50 +0100240 ScopedFd dst(open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600));
241 if (dst.get() == -1) {
242 PLOG(ERROR) << "Failed to create/write prev profile file " << newfile
243 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800244 return;
245 }
Calin Juravle52214102014-06-04 12:01:50 +0100246
247 if (sendfile(dst.get(), src.get(), nullptr, stat_src.st_size) == -1) {
248 PLOG(ERROR) << "Failed to copy profile file " << oldfile << " to " << newfile
249 << ". My uid:gid is " << getuid() << ":" << getgid();
Dave Allison39c3bfb2014-01-28 18:33:52 -0800250 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800251}
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
Brian Carlstrome1ff1992014-05-18 22:37:51 -0700302 const InstructionSet target_instruction_set = GetInstructionSetFromString(instruction_set);
303
304 // Check if we have an odex file next to the dex file.
305 std::string odex_filename(DexFilenameToOdexFilename(filename, kRuntimeISA));
306 std::string error_msg;
307 std::unique_ptr<const OatFile> oat_file(OatFile::Open(odex_filename, odex_filename, NULL, false,
308 &error_msg));
309 if (oat_file.get() == nullptr) {
310 if (kVerboseLogging) {
311 LOG(INFO) << "DexFile_isDexOptNeeded failed to open oat file '" << filename
312 << "': " << error_msg;
313 }
314 error_msg.clear();
315 } else {
316 const art::OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(filename, NULL,
317 kReasonLogging);
318 if (oat_dex_file != nullptr) {
319 uint32_t location_checksum;
320 // If its not possible to read the classes.dex assume up-to-date as we won't be able to
321 // compile it anyway.
322 if (!DexFile::GetChecksum(filename, &location_checksum, &error_msg)) {
323 if (kVerboseLogging) {
324 LOG(INFO) << "DexFile_isDexOptNeeded ignoring precompiled stripped file: "
325 << filename << ": " << error_msg;
326 }
327 return JNI_FALSE;
328 }
329 if (ClassLinker::VerifyOatFileChecksums(oat_file.get(), filename, location_checksum,
330 target_instruction_set,
331 &error_msg)) {
332 if (kVerboseLogging) {
333 LOG(INFO) << "DexFile_isDexOptNeeded precompiled file " << odex_filename
334 << " has an up-to-date checksum compared to " << filename;
335 }
336 return JNI_FALSE;
337 } else {
338 if (kVerboseLogging) {
339 LOG(INFO) << "DexFile_isDexOptNeeded found precompiled file " << odex_filename
340 << " with an out-of-date checksum compared to " << filename
341 << ": " << error_msg;
342 }
343 error_msg.clear();
344 }
345 }
346 }
347
Dave Allison39c3bfb2014-01-28 18:33:52 -0800348 // Check the profile file. We need to rerun dex2oat if the profile has changed significantly
349 // since the last time, or it's new.
350 // If the 'defer' argument is true then this will be retried later. In this case we
351 // need to make sure that the profile file copy is not made so that we will get the
352 // same result second time.
Narayan Kamath11d9f062014-04-23 20:24:57 +0100353 if (pkgname != nullptr) {
354 const std::string profile_file = GetDalvikCacheOrDie("profiles", false /* create_if_absent */)
355 + std::string("/") + pkgname;
356 const std::string profile_cache_dir = GetDalvikCacheOrDie("profile-cache",
357 false /* create_if_absent */);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800358
359 // Make the profile cache if it doesn't exist.
360 mkdir(profile_cache_dir.c_str(), 0700);
361
362 // The previous profile file (a copy of the profile the last time this was run) is
363 // in the dalvik-cache directory because this is owned by system. The profiles
364 // directory is owned by install so system cannot write files in there.
Narayan Kamath11d9f062014-04-23 20:24:57 +0100365 std::string prev_profile_file = profile_cache_dir + std::string("/") + pkgname;
Dave Allison39c3bfb2014-01-28 18:33:52 -0800366
367 struct stat profstat, prevstat;
368 int e1 = stat(profile_file.c_str(), &profstat);
369 int e2 = stat(prev_profile_file.c_str(), &prevstat);
Dave Allison39c3bfb2014-01-28 18:33:52 -0800370 if (e1 < 0) {
371 // No profile file, need to run dex2oat
Brian Carlstrom09881a82014-04-18 17:44:01 -0700372 if (kReasonLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800373 LOG(INFO) << "DexFile_isDexOptNeeded profile file " << profile_file << " doesn't exist";
374 }
375 return JNI_TRUE;
376 }
Calin Juravle9dae5b42014-04-07 16:36:21 +0300377
Dave Allison39c3bfb2014-01-28 18:33:52 -0800378 if (e2 == 0) {
379 // There is a previous profile file. Check if the profile has changed significantly.
Calin Juravle9dae5b42014-04-07 16:36:21 +0300380 // A change in profile is considered significant if X% (change_thr property) of the top K%
381 // (compile_thr property) samples has changed.
Dave Allison39c3bfb2014-01-28 18:33:52 -0800382
Calin Juravle9dae5b42014-04-07 16:36:21 +0300383 double topKThreshold = GetDoubleProperty("dalvik.vm.profiler.dex2oat.compile_thr", 10.0, 90.0, 90.0);
384 double changeThreshold = GetDoubleProperty("dalvik.vm.profiler.dex2oat.change_thr", 1.0, 90.0, 10.0);
385 double changePercent = 0.0;
386 std::set<std::string> newTopK, oldTopK;
387 bool newOk = ProfileHelper::LoadTopKSamples(newTopK, profile_file, topKThreshold);
388 bool oldOk = ProfileHelper::LoadTopKSamples(oldTopK, prev_profile_file, topKThreshold);
389 if (!newOk || !oldOk) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700390 if (kVerboseLogging) {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300391 LOG(INFO) << "DexFile_isDexOptNeeded Ignoring invalid profiles: "
392 << (newOk ? "" : profile_file) << " " << (oldOk ? "" : prev_profile_file);
393 }
394 } else if (newTopK.empty()) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700395 if (kVerboseLogging) {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300396 LOG(INFO) << "DexFile_isDexOptNeeded empty profile: " << profile_file;
397 }
398 // If the new topK is empty we shouldn't optimize so we leave the changePercent at 0.0.
399 } else {
400 std::set<std::string> diff;
401 std::set_difference(newTopK.begin(), newTopK.end(), oldTopK.begin(), oldTopK.end(),
402 std::inserter(diff, diff.end()));
403 // TODO: consider using the usedPercentage instead of the plain diff count.
404 changePercent = 100.0 * static_cast<double>(diff.size()) / static_cast<double>(newTopK.size());
Brian Carlstrom09881a82014-04-18 17:44:01 -0700405 if (kVerboseLogging) {
Calin Juravle9dae5b42014-04-07 16:36:21 +0300406 std::set<std::string>::iterator end = diff.end();
407 for (std::set<std::string>::iterator it = diff.begin(); it != end; it++) {
408 LOG(INFO) << "DexFile_isDexOptNeeded new in topK: " << *it;
409 }
410 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800411 }
412
Calin Juravle9dae5b42014-04-07 16:36:21 +0300413 if (changePercent > changeThreshold) {
Brian Carlstrom09881a82014-04-18 17:44:01 -0700414 if (kReasonLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800415 LOG(INFO) << "DexFile_isDexOptNeeded size of new profile file " << profile_file <<
Calin Juravle9dae5b42014-04-07 16:36:21 +0300416 " is significantly different from old profile file " << prev_profile_file << " (top "
417 << topKThreshold << "% samples changed in proportion of " << changePercent << "%)";
Dave Allison39c3bfb2014-01-28 18:33:52 -0800418 }
419 if (!defer) {
420 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
421 }
422 return JNI_TRUE;
423 }
424 } else {
425 // Previous profile does not exist. Make a copy of the current one.
Brian Carlstrom09881a82014-04-18 17:44:01 -0700426 if (kVerboseLogging) {
Dave Allison39c3bfb2014-01-28 18:33:52 -0800427 LOG(INFO) << "DexFile_isDexOptNeeded previous profile doesn't exist: " << prev_profile_file;
428 }
429 if (!defer) {
430 CopyProfileFile(profile_file.c_str(), prev_profile_file.c_str());
431 }
Dave Allison39c3bfb2014-01-28 18:33:52 -0800432 }
433 }
434
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