From 500c9be1f261a8a95ae7a25f4f8084f43207f313 Mon Sep 17 00:00:00 2001 From: Calin Juravle Date: Wed, 25 Nov 2015 15:59:14 +0000 Subject: [PATCH] Enable profiled guided compilation in dex2oat - add parsing of the profile info saved during JIT. - don't compile methods which are not part of the profile info. - delete old profile hooks. - add test for reading/writing profile. The test is disable in: * interpreter modes: the test needs JIT. * no-dex2oat/no-prebuild: we only save profiling info for the primary oat file. In these modes we don't create oat files and thus nothing is saved. Bug:26080105 Change-Id: Ifdc63dc9d4b537fc79e54c3edc3ae3a462bc30fb --- compiler/dex/mir_analysis.cc | 4 - compiler/dex/mir_graph.h | 5 - compiler/dex/quick/quick_compiler.cc | 8 - compiler/driver/compiler_driver.cc | 77 +++---- compiler/driver/compiler_driver.h | 19 +- runtime/jit/jit.cc | 2 +- runtime/jit/offline_profiling_info.cc | 240 ++++++++++++++++++++- runtime/jit/offline_profiling_info.h | 40 +++- runtime/runtime.cc | 2 - test/554-jit-profile-file/expected.txt | 7 + test/554-jit-profile-file/info.txt | 1 + test/554-jit-profile-file/offline_profile.cc | 59 +++++ test/554-jit-profile-file/run | 22 ++ .../src-multidex/OtherDex.java | 31 +++ test/554-jit-profile-file/src/Main.java | 143 ++++++++++++ test/Android.libarttest.mk | 3 +- test/Android.run-test.mk | 14 +- 17 files changed, 588 insertions(+), 89 deletions(-) create mode 100644 test/554-jit-profile-file/expected.txt create mode 100644 test/554-jit-profile-file/info.txt create mode 100644 test/554-jit-profile-file/offline_profile.cc create mode 100644 test/554-jit-profile-file/run create mode 100644 test/554-jit-profile-file/src-multidex/OtherDex.java create mode 100644 test/554-jit-profile-file/src/Main.java diff --git a/compiler/dex/mir_analysis.cc b/compiler/dex/mir_analysis.cc index 39f8ee8f7..18ce563fc 100644 --- a/compiler/dex/mir_analysis.cc +++ b/compiler/dex/mir_analysis.cc @@ -1430,8 +1430,4 @@ void MIRGraph::DoCacheMethodLoweringInfo() { method_lowering_infos_.data(), count); } -bool MIRGraph::SkipCompilationByName(const std::string& methodname) { - return cu_->compiler_driver->SkipCompilation(methodname); -} - } // namespace art diff --git a/compiler/dex/mir_graph.h b/compiler/dex/mir_graph.h index 2da8a98f0..3191fe9d5 100644 --- a/compiler/dex/mir_graph.h +++ b/compiler/dex/mir_graph.h @@ -564,11 +564,6 @@ class MIRGraph { bool SkipCompilation(std::string* skip_message); /* - * Should we skip the compilation of this method based on its name? - */ - bool SkipCompilationByName(const std::string& methodname); - - /* * Parse dex method and add MIR at current insert point. Returns id (which is * actually the index of the method in the m_units_ array). */ diff --git a/compiler/dex/quick/quick_compiler.cc b/compiler/dex/quick/quick_compiler.cc index 05dde9f64..3260a7a05 100644 --- a/compiler/dex/quick/quick_compiler.cc +++ b/compiler/dex/quick/quick_compiler.cc @@ -780,14 +780,6 @@ CompiledMethod* QuickCompiler::Compile(const DexFile::CodeItem* code_item, PassDriverMEOpts pass_driver(GetPreOptPassManager(), GetPostOptPassManager(), &cu); pass_driver.Launch(); - /* For non-leaf methods check if we should skip compilation when the profiler is enabled. */ - if (cu.compiler_driver->ProfilePresent() - && !cu.mir_graph->MethodIsLeaf() - && cu.mir_graph->SkipCompilationByName(PrettyMethod(method_idx, dex_file))) { - cu.EndTiming(); - return nullptr; - } - if (cu.enable_debug & (1 << kDebugDumpCheckStats)) { cu.mir_graph->DumpCheckStats(); } diff --git a/compiler/driver/compiler_driver.cc b/compiler/driver/compiler_driver.cc index a05105b84..fd9a1c567 100644 --- a/compiler/driver/compiler_driver.cc +++ b/compiler/driver/compiler_driver.cc @@ -80,6 +80,9 @@ static constexpr bool kTimeCompileMethod = !kIsDebugBuild; // given, too all compilations. static constexpr bool kRestrictCompilationFiltersToImage = true; +// Print additional info during profile guided compilation. +static constexpr bool kDebugProfileGuidedCompilation = false; + static double Percentage(size_t x, size_t y) { return 100.0 * (static_cast(x)) / (static_cast(x + y)); } @@ -344,8 +347,7 @@ CompilerDriver::CompilerDriver(const CompilerOptions* compiler_options, const std::string& dump_cfg_file_name, bool dump_cfg_append, CumulativeLogger* timer, int swap_fd, const std::string& profile_file) - : profile_present_(false), - compiler_options_(compiler_options), + : compiler_options_(compiler_options), verification_results_(verification_results), method_inliner_map_(method_inliner_map), compiler_(Compiler::Create(this, compiler_kind)), @@ -383,12 +385,8 @@ CompilerDriver::CompilerDriver(const CompilerOptions* compiler_options, // Read the profile file if one is provided. if (!profile_file.empty()) { - profile_present_ = profile_file_.LoadFile(profile_file); - if (profile_present_) { - LOG(INFO) << "Using profile data form file " << profile_file; - } else { - LOG(INFO) << "Failed to load profile file " << profile_file; - } + profile_compilation_info_.reset(new ProfileCompilationInfo(profile_file)); + LOG(INFO) << "Using profile data from file " << profile_file; } } @@ -569,7 +567,9 @@ static void CompileMethod(Thread* self, (verified_method->GetEncounteredVerificationFailures() & (verifier::VERIFY_ERROR_FORCE_INTERPRETER | verifier::VERIFY_ERROR_LOCKING)) == 0 && // Is eligable for compilation by methods-to-compile filter. - driver->IsMethodToCompile(method_ref); + driver->IsMethodToCompile(method_ref) && + driver->ShouldCompileBasedOnProfile(method_ref); + if (compile) { // NOTE: if compiler declines to compile this method, it will return null. compiled_method = driver->GetCompiler()->Compile(code_item, access_flags, invoke_type, @@ -766,6 +766,22 @@ bool CompilerDriver::IsMethodToCompile(const MethodReference& method_ref) const return methods_to_compile_->find(tmp.c_str()) != methods_to_compile_->end(); } +bool CompilerDriver::ShouldCompileBasedOnProfile(const MethodReference& method_ref) const { + if (profile_compilation_info_ == nullptr) { + // If we miss profile information it means that we don't do a profile guided compilation. + // Return true, and let the other filters decide if the method should be compiled. + return true; + } + bool result = profile_compilation_info_->ContainsMethod(method_ref); + + if (kDebugProfileGuidedCompilation) { + LOG(INFO) << "[ProfileGuidedCompilation] " + << (result ? "Compiled" : "Skipped") << " method:" + << PrettyMethod(method_ref.dex_method_index, *method_ref.dex_file, true); + } + return result; +} + class ResolveCatchBlockExceptionsClassVisitor : public ClassVisitor { public: ResolveCatchBlockExceptionsClassVisitor( @@ -2273,6 +2289,16 @@ void CompilerDriver::InitializeClasses(jobject class_loader, void CompilerDriver::Compile(jobject class_loader, const std::vector& dex_files, ThreadPool* thread_pool, TimingLogger* timings) { + if (profile_compilation_info_ != nullptr) { + if (!profile_compilation_info_->Load(dex_files)) { + LOG(WARNING) << "Failed to load offline profile info from " + << profile_compilation_info_->GetFilename() + << ". No methods will be compiled"; + } else if (kDebugProfileGuidedCompilation) { + LOG(INFO) << "[ProfileGuidedCompilation] " + << profile_compilation_info_->DumpInfo(); + } + } for (size_t i = 0; i != dex_files.size(); ++i) { const DexFile* dex_file = dex_files[i]; CHECK(dex_file != nullptr); @@ -2510,39 +2536,6 @@ bool CompilerDriver::RequiresConstructorBarrier(Thread* self, const DexFile* dex return freezing_constructor_classes_.count(ClassReference(dex_file, class_def_index)) != 0; } -bool CompilerDriver::SkipCompilation(const std::string& method_name) { - if (!profile_present_) { - return false; - } - // First find the method in the profile file. - ProfileFile::ProfileData data; - if (!profile_file_.GetProfileData(&data, method_name)) { - // Not in profile, no information can be determined. - if (kIsDebugBuild) { - VLOG(compiler) << "not compiling " << method_name << " because it's not in the profile"; - } - return true; - } - - // Methods that comprise top_k_threshold % of the total samples will be compiled. - // Compare against the start of the topK percentage bucket just in case the threshold - // falls inside a bucket. - bool compile = data.GetTopKUsedPercentage() - data.GetUsedPercent() - <= compiler_options_->GetTopKProfileThreshold(); - if (kIsDebugBuild) { - if (compile) { - LOG(INFO) << "compiling method " << method_name << " because its usage is part of top " - << data.GetTopKUsedPercentage() << "% with a percent of " << data.GetUsedPercent() << "%" - << " (topKThreshold=" << compiler_options_->GetTopKProfileThreshold() << ")"; - } else { - VLOG(compiler) << "not compiling method " << method_name - << " because it's not part of leading " << compiler_options_->GetTopKProfileThreshold() - << "% samples)"; - } - } - return !compile; -} - std::string CompilerDriver::GetMemoryUsageString(bool extended) const { std::ostringstream oss; Runtime* const runtime = Runtime::Current(); diff --git a/compiler/driver/compiler_driver.h b/compiler/driver/compiler_driver.h index 1347b37fa..a351f6d5d 100644 --- a/compiler/driver/compiler_driver.h +++ b/compiler/driver/compiler_driver.h @@ -31,11 +31,11 @@ #include "compiler.h" #include "dex_file.h" #include "driver/compiled_method_storage.h" +#include "jit/offline_profiling_info.h" #include "invoke_type.h" #include "method_reference.h" #include "mirror/class.h" // For mirror::Class::Status. #include "os.h" -#include "profiler.h" #include "runtime.h" #include "safe_map.h" #include "thread_pool.h" @@ -147,10 +147,6 @@ class CompilerDriver { return compiler_.get(); } - bool ProfilePresent() const { - return profile_present_; - } - // Are we compiling and creating an image file? bool IsBootImage() const { return boot_image_; @@ -445,6 +441,10 @@ class CompilerDriver { // Checks whether the provided method should be compiled, i.e., is in method_to_compile_. bool IsMethodToCompile(const MethodReference& method_ref) const; + // Checks whether profile guided compilation is enabled and if the method should be compiled + // according to the profile file. + bool ShouldCompileBasedOnProfile(const MethodReference& method_ref) const; + void RecordClassStatus(ClassReference ref, mirror::Class::Status status) REQUIRES(!compiled_classes_lock_); @@ -454,9 +454,6 @@ class CompilerDriver { uint16_t class_def_idx, const DexFile& dex_file) const; - // Should the compiler run on this method given profile information? - bool SkipCompilation(const std::string& method_name); - // Get memory usage during compilation. std::string GetMemoryUsageString(bool extended) const; @@ -595,9 +592,6 @@ class CompilerDriver { ThreadPool* thread_pool, TimingLogger* timings) REQUIRES(!Locks::mutator_lock_); - ProfileFile profile_file_; - bool profile_present_; - const CompilerOptions* const compiler_options_; VerificationResults* const verification_results_; DexFileToMethodInlinerMap* const method_inliner_map_; @@ -647,6 +641,9 @@ class CompilerDriver { // This option may be restricted to the boot image, depending on a flag in the implementation. std::unique_ptr> methods_to_compile_; + // Info for profile guided compilation. + std::unique_ptr profile_compilation_info_; + bool had_hard_verifier_failure_; size_t thread_count_; diff --git a/runtime/jit/jit.cc b/runtime/jit/jit.cc index 92aa86ee5..a653440b5 100644 --- a/runtime/jit/jit.cc +++ b/runtime/jit/jit.cc @@ -188,7 +188,7 @@ void Jit::SaveProfilingInfo(const std::string& filename) { uint64_t last_update_ns = code_cache_->GetLastUpdateTimeNs(); if (offline_profile_info_->NeedsSaving(last_update_ns)) { - VLOG(profiler) << "Iniate save profiling information to: " << filename; + VLOG(profiler) << "Initiate save profiling information to: " << filename; std::set methods; { ScopedObjectAccess soa(Thread::Current()); diff --git a/runtime/jit/offline_profiling_info.cc b/runtime/jit/offline_profiling_info.cc index 4450653a9..761587024 100644 --- a/runtime/jit/offline_profiling_info.cc +++ b/runtime/jit/offline_profiling_info.cc @@ -68,7 +68,6 @@ void OfflineProfilingInfo::SaveProfilingInfo(const std::string& filename, } } - void OfflineProfilingInfo::AddMethodInfo(ArtMethod* method, DexFileToMethodsMap* info) { DCHECK(method != nullptr); const DexFile* dex_file = method->GetDexFile(); @@ -80,11 +79,25 @@ void OfflineProfilingInfo::AddMethodInfo(ArtMethod* method, DexFileToMethodsMap* info_it->second.insert(method->GetDexMethodIndex()); } -static int OpenOrCreateFile(const std::string& filename) { - // TODO(calin) allow the shared uid of the app to access the file. - int fd = open(filename.c_str(), - O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, - S_IRUSR | S_IWUSR); +enum OpenMode { + READ, + READ_WRITE +}; + +static int OpenFile(const std::string& filename, OpenMode open_mode) { + int fd = -1; + switch (open_mode) { + case READ: + fd = open(filename.c_str(), O_RDONLY); + break; + case READ_WRITE: + // TODO(calin) allow the shared uid of the app to access the file. + fd = open(filename.c_str(), + O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, + S_IRUSR | S_IWUSR); + break; + } + if (fd < 0) { PLOG(WARNING) << "Failed to open profile file " << filename; return -1; @@ -96,7 +109,6 @@ static int OpenOrCreateFile(const std::string& filename) { PLOG(WARNING) << "Failed to lock profile file " << filename; return -1; } - return fd; } @@ -129,8 +141,8 @@ static void WriteToFile(int fd, const std::ostringstream& os) { } while (length > 0); } -static constexpr char kFieldSeparator = ','; -static constexpr char kLineSeparator = '\n'; +static constexpr const char kFieldSeparator = ','; +static constexpr const char kLineSeparator = '\n'; /** * Serialization format: @@ -142,7 +154,7 @@ static constexpr char kLineSeparator = '\n'; **/ bool OfflineProfilingInfo::Serialize(const std::string& filename, const DexFileToMethodsMap& info) const { - int fd = OpenOrCreateFile(filename); + int fd = OpenFile(filename, READ_WRITE); if (fd == -1) { return false; } @@ -168,4 +180,212 @@ bool OfflineProfilingInfo::Serialize(const std::string& filename, return CloseDescriptorForFile(fd, filename); } + +// TODO(calin): This a duplicate of Utils::Split fixing the case where the first character +// is the separator. Merge the fix into Utils::Split once verified that it doesn't break its users. +static void SplitString(const std::string& s, char separator, std::vector* result) { + const char* p = s.data(); + const char* end = p + s.size(); + // Check if the first character is the separator. + if (p != end && *p ==separator) { + result->push_back(""); + ++p; + } + // Process the rest of the characters. + while (p != end) { + if (*p == separator) { + ++p; + } else { + const char* start = p; + while (++p != end && *p != separator) { + // Skip to the next occurrence of the separator. + } + result->push_back(std::string(start, p - start)); + } + } +} + +bool ProfileCompilationInfo::ProcessLine(const std::string& line, + const std::vector& dex_files) { + std::vector parts; + SplitString(line, kFieldSeparator, &parts); + if (parts.size() < 3) { + LOG(WARNING) << "Invalid line: " << line; + return false; + } + + const std::string& multidex_suffix = parts[0]; + uint32_t checksum; + if (!ParseInt(parts[1].c_str(), &checksum)) { + return false; + } + + const DexFile* current_dex_file = nullptr; + for (auto dex_file : dex_files) { + if (DexFile::GetMultiDexSuffix(dex_file->GetLocation()) == multidex_suffix) { + if (checksum != dex_file->GetLocationChecksum()) { + LOG(WARNING) << "Checksum mismatch for " + << dex_file->GetLocation() << " when parsing " << filename_; + return false; + } + current_dex_file = dex_file; + break; + } + } + if (current_dex_file == nullptr) { + return true; + } + + for (size_t i = 2; i < parts.size(); i++) { + uint32_t method_idx; + if (!ParseInt(parts[i].c_str(), &method_idx)) { + LOG(WARNING) << "Cannot parse method_idx " << parts[i]; + return false; + } + uint16_t class_idx = current_dex_file->GetMethodId(method_idx).class_idx_; + auto info_it = info_.find(current_dex_file); + if (info_it == info_.end()) { + info_it = info_.Put(current_dex_file, ClassToMethodsMap()); + } + ClassToMethodsMap& class_map = info_it->second; + auto class_it = class_map.find(class_idx); + if (class_it == class_map.end()) { + class_it = class_map.Put(class_idx, std::set()); + } + class_it->second.insert(method_idx); + } + return true; +} + +// Parses the buffer (of length n) starting from start_from and identify new lines +// based on kLineSeparator marker. +// Returns the first position after kLineSeparator in the buffer (starting from start_from), +// or -1 if the marker doesn't appear. +// The processed characters are appended to the given line. +static int GetLineFromBuffer(char* buffer, int n, int start_from, std::string& line) { + if (start_from >= n) { + return -1; + } + int new_line_pos = -1; + for (int i = start_from; i < n; i++) { + if (buffer[i] == kLineSeparator) { + new_line_pos = i; + break; + } + } + int append_limit = new_line_pos == -1 ? n : new_line_pos; + line.append(buffer + start_from, append_limit - start_from); + // Jump over kLineSeparator and return the position of the next character. + return new_line_pos == -1 ? new_line_pos : new_line_pos + 1; +} + +bool ProfileCompilationInfo::Load(const std::vector& dex_files) { + if (dex_files.empty()) { + return true; + } + if (kIsDebugBuild) { + // In debug builds verify that the multidex suffixes are unique. + std::set suffixes; + for (auto dex_file : dex_files) { + std::string multidex_suffix = DexFile::GetMultiDexSuffix(dex_file->GetLocation()); + DCHECK(suffixes.find(multidex_suffix) == suffixes.end()) + << "DexFiles appear to belong to different apks." + << " There are multiple dex files with the same multidex suffix: " + << multidex_suffix; + suffixes.insert(multidex_suffix); + } + } + info_.clear(); + + int fd = OpenFile(filename_, READ); + if (fd == -1) { + return false; + } + + std::string current_line; + const int kBufferSize = 1024; + char buffer[kBufferSize]; + bool success = true; + + while (success) { + int n = read(fd, buffer, kBufferSize); + if (n < 0) { + PLOG(WARNING) << "Error when reading profile file " << filename_; + success = false; + break; + } else if (n == 0) { + break; + } + // Detect the new lines from the buffer. If we manage to complete a line, + // process it. Otherwise append to the current line. + int current_start_pos = 0; + while (current_start_pos < n) { + current_start_pos = GetLineFromBuffer(buffer, n, current_start_pos, current_line); + if (current_start_pos == -1) { + break; + } + if (!ProcessLine(current_line, dex_files)) { + success = false; + break; + } + // Reset the current line (we just processed it). + current_line.clear(); + } + } + if (!success) { + info_.clear(); + } + return CloseDescriptorForFile(fd, filename_) && success; +} + +bool ProfileCompilationInfo::ContainsMethod(const MethodReference& method_ref) const { + auto info_it = info_.find(method_ref.dex_file); + if (info_it != info_.end()) { + uint16_t class_idx = method_ref.dex_file->GetMethodId(method_ref.dex_method_index).class_idx_; + const ClassToMethodsMap& class_map = info_it->second; + auto class_it = class_map.find(class_idx); + if (class_it != class_map.end()) { + const std::set& methods = class_it->second; + return methods.find(method_ref.dex_method_index) != methods.end(); + } + return false; + } + return false; +} + +std::string ProfileCompilationInfo::DumpInfo(bool print_full_dex_location) const { + std::ostringstream os; + if (info_.empty()) { + return "ProfileInfo: empty"; + } + + os << "ProfileInfo:"; + + // Use an additional map to achieve a predefined order based on the dex locations. + SafeMap dex_locations_map; + for (auto info_it : info_) { + dex_locations_map.Put(info_it.first->GetLocation(), info_it.first); + } + + const std::string kFirstDexFileKeySubstitute = ":classes.dex"; + for (auto dex_file_it : dex_locations_map) { + os << "\n"; + const std::string& location = dex_file_it.first; + const DexFile* dex_file = dex_file_it.second; + if (print_full_dex_location) { + os << location; + } else { + // Replace the (empty) multidex suffix of the first key with a substitute for easier reading. + std::string multidex_suffix = DexFile::GetMultiDexSuffix(location); + os << (multidex_suffix.empty() ? kFirstDexFileKeySubstitute : multidex_suffix); + } + for (auto class_it : info_.find(dex_file)->second) { + for (auto method_it : class_it.second) { + os << "\n " << PrettyMethod(method_it, *dex_file, true); + } + } + } + return os.str(); +} + } // namespace art diff --git a/runtime/jit/offline_profiling_info.h b/runtime/jit/offline_profiling_info.h index e3117eb5e..90bda60ac 100644 --- a/runtime/jit/offline_profiling_info.h +++ b/runtime/jit/offline_profiling_info.h @@ -21,6 +21,7 @@ #include "atomic.h" #include "dex_file.h" +#include "method_reference.h" #include "safe_map.h" namespace art { @@ -50,10 +51,47 @@ class OfflineProfilingInfo { bool Serialize(const std::string& filename, const DexFileToMethodsMap& info) const; // TODO(calin): Verify if Atomic is really needed (are we sure to be called from a - // singe thread?) + // single thread?) Atomic last_update_time_ns_; }; +/** + * Profile information in a format suitable to be queried by the compiler and performing + * profile guided compilation. + */ +class ProfileCompilationInfo { + public: + // Constructs a ProfileCompilationInfo backed by the provided file. + explicit ProfileCompilationInfo(const std::string& filename) : filename_(filename) {} + + // Loads profile information corresponding to the provided dex files. + // The dex files' multidex suffixes must be unique. + // This resets the state of the profiling information + // (i.e. all previously loaded info are cleared). + bool Load(const std::vector& dex_files); + + // Returns true if the method reference is present in the profiling info. + bool ContainsMethod(const MethodReference& method_ref) const; + + const std::string& GetFilename() const { return filename_; } + + // Dumps all the loaded profile info into a string and returns it. + // This is intended for testing and debugging. + std::string DumpInfo(bool print_full_dex_location = true) const; + + private: + bool ProcessLine(const std::string& line, + const std::vector& dex_files); + + using ClassToMethodsMap = SafeMap>; + // Map identifying the location of the profiled methods. + // dex_file -> class_index -> [dex_method_index]+ + using DexFileToProfileInfoMap = SafeMap; + + const std::string filename_; + DexFileToProfileInfoMap info_; +}; + } // namespace art #endif // ART_RUNTIME_JIT_OFFLINE_PROFILING_INFO_H_ diff --git a/runtime/runtime.cc b/runtime/runtime.cc index dedc110b0..828c228a6 100644 --- a/runtime/runtime.cc +++ b/runtime/runtime.cc @@ -219,8 +219,6 @@ Runtime::~Runtime() { UnloadNativeBridge(); } - MaybeSaveJitProfilingInfo(); - if (dump_gc_performance_on_shutdown_) { // This can't be called from the Heap destructor below because it // could call RosAlloc::InspectAll() which needs the thread_list diff --git a/test/554-jit-profile-file/expected.txt b/test/554-jit-profile-file/expected.txt new file mode 100644 index 000000000..cde211e1c --- /dev/null +++ b/test/554-jit-profile-file/expected.txt @@ -0,0 +1,7 @@ +JNI_OnLoad called +ProfileInfo: +:classes.dex + java.lang.String Main.hotMethod() + void Main.main(java.lang.String[]) +:classes2.dex + java.lang.String OtherDex.hotMethod() diff --git a/test/554-jit-profile-file/info.txt b/test/554-jit-profile-file/info.txt new file mode 100644 index 000000000..b1bfe81eb --- /dev/null +++ b/test/554-jit-profile-file/info.txt @@ -0,0 +1 @@ +Check that saving and restoring profile files works correctly in a JIT environment. diff --git a/test/554-jit-profile-file/offline_profile.cc b/test/554-jit-profile-file/offline_profile.cc new file mode 100644 index 000000000..75e441fc8 --- /dev/null +++ b/test/554-jit-profile-file/offline_profile.cc @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dex_file.h" + +#include "jit/offline_profiling_info.h" +#include "jni.h" +#include "mirror/class-inl.h" +#include "oat_file_assistant.h" +#include "oat_file_manager.h" +#include "scoped_thread_state_change.h" +#include "thread.h" + +namespace art { +namespace { + +extern "C" JNIEXPORT jstring JNICALL Java_Main_getProfileInfoDump( + JNIEnv* env, jclass cls, jstring filename) { + std::string dex_location; + { + ScopedObjectAccess soa(Thread::Current()); + dex_location = soa.Decode(cls)->GetDexCache()->GetDexFile()->GetLocation(); + } + const OatFile* oat_file = Runtime::Current()->GetOatFileManager().GetPrimaryOatFile(); + std::vector> dex_files = + OatFileAssistant::LoadDexFiles(*oat_file, dex_location.c_str()); + const char* filename_chars = env->GetStringUTFChars(filename, nullptr); + + std::vector dex_files_raw; + for (size_t i = 0; i < dex_files.size(); i++) { + dex_files_raw.push_back(dex_files[i].get()); + } + + ProfileCompilationInfo info(filename_chars); + + std::string result = info.Load(dex_files_raw) + ? info.DumpInfo(/*print_full_dex_location*/false) + : "Could not load profile info"; + + env->ReleaseStringUTFChars(filename, filename_chars); + // Return the dump of the profile info. It will be compared against a golden value. + return env->NewStringUTF(result.c_str()); +} + +} // namespace +} // namespace art diff --git a/test/554-jit-profile-file/run b/test/554-jit-profile-file/run new file mode 100644 index 000000000..08dcb38ab --- /dev/null +++ b/test/554-jit-profile-file/run @@ -0,0 +1,22 @@ +#!/bin/bash +# +# Copyright 2015 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +exec ${RUN} \ + -Xcompiler-option --compiler-filter=interpret-only \ + --runtime-option -Xjitsaveprofilinginfo \ + --runtime-option -Xusejit:true \ + --runtime-option -Xjitthreshold:100 \ + "${@}" diff --git a/test/554-jit-profile-file/src-multidex/OtherDex.java b/test/554-jit-profile-file/src-multidex/OtherDex.java new file mode 100644 index 000000000..51644db5a --- /dev/null +++ b/test/554-jit-profile-file/src-multidex/OtherDex.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.HashMap; + +public class OtherDex { + public void coldMethod() { + hotMethod(); + } + + public String hotMethod() { + HashMap map = new HashMap(); + for (int i = 0; i < 10; i++) { + map.put("" + i, "" + i + 1); + } + return map.get("1"); + } +} diff --git a/test/554-jit-profile-file/src/Main.java b/test/554-jit-profile-file/src/Main.java new file mode 100644 index 000000000..b581a674e --- /dev/null +++ b/test/554-jit-profile-file/src/Main.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.HashMap; + +public class Main { + + public void coldMethod() { + hotMethod(); + } + + public String hotMethod() { + HashMap map = new HashMap(); + for (int i = 0; i < 10; i++) { + map.put("" + i, "" + i + 1); + } + return map.get("1"); + } + + private static final String PKG_NAME = "test.package"; + private static final String APP_DIR_PREFIX = "app_dir_"; + private static final String CODE_CACHE = "code_cache"; + private static final String PROFILE_FILE = PKG_NAME + ".prof"; + private static final String TEMP_FILE_NAME_PREFIX = "dummy"; + private static final String TEMP_FILE_NAME_SUFFIX = "-file"; + private static final int JIT_INVOCATION_COUNT = 101; + + /* needs to match Runtime:: kProfileBackground */ + private static final int PROFILE_BACKGROUND = 1; + + public static void main(String[] args) throws Exception { + System.loadLibrary(args[0]); + + File file = null; + File appDir = null; + File profileDir = null; + File profileFile = null; + try { + // We don't know where we have rights to create the code_cache. So create + // a dummy temporary file and get its parent directory. That will serve as + // the app directory. + file = createTempFile(); + appDir = new File(file.getParent(), APP_DIR_PREFIX + file.getName()); + appDir.mkdir(); + profileDir = new File(appDir, CODE_CACHE); + profileDir.mkdir(); + + // Registering the app info will set the profile file name. + VMRuntime.registerAppInfo(PKG_NAME, appDir.getPath()); + + // Make sure the hot methods are jitted. + Main m = new Main(); + OtherDex o = new OtherDex(); + for (int i = 0; i < JIT_INVOCATION_COUNT; i++) { + m.hotMethod(); + o.hotMethod(); + } + + // Updating the process state to BACKGROUND will trigger profile saving. + VMRuntime.updateProcessState(PROFILE_BACKGROUND); + + // Check that the profile file exists. + profileFile = new File(profileDir, PROFILE_FILE); + if (!profileFile.exists()) { + throw new RuntimeException("No profile file found"); + } + // Dump the profile file. + // We know what methods are hot and we compare with the golden `expected` output. + System.out.println(getProfileInfoDump(profileFile.getPath())); + } finally { + if (file != null) { + file.delete(); + } + if (profileFile != null) { + profileFile.delete(); + } + if (profileDir != null) { + profileDir.delete(); + } + if (appDir != null) { + appDir.delete(); + } + } + } + + private static class VMRuntime { + private static final Method registerAppInfoMethod; + private static final Method updateProcessStateMethod; + private static final Method getRuntimeMethod; + static { + try { + Class c = Class.forName("dalvik.system.VMRuntime"); + registerAppInfoMethod = c.getDeclaredMethod("registerAppInfo", + String.class, String.class, String.class); + updateProcessStateMethod = c.getDeclaredMethod("updateProcessState", Integer.TYPE); + getRuntimeMethod = c.getDeclaredMethod("getRuntime"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void registerAppInfo(String pkgName, String appDir) throws Exception { + registerAppInfoMethod.invoke(null, pkgName, appDir, null); + } + public static void updateProcessState(int state) throws Exception { + Object runtime = getRuntimeMethod.invoke(null); + updateProcessStateMethod.invoke(runtime, state); + } + } + + static native String getProfileInfoDump( + String filename); + + private static File createTempFile() throws Exception { + try { + return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); + } catch (IOException e) { + System.setProperty("java.io.tmpdir", "/data/local/tmp"); + try { + return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); + } catch (IOException e2) { + System.setProperty("java.io.tmpdir", "/sdcard"); + return File.createTempFile(TEMP_FILE_NAME_PREFIX, TEMP_FILE_NAME_SUFFIX); + } + } + } +} diff --git a/test/Android.libarttest.mk b/test/Android.libarttest.mk index f74a51648..f84dfe671 100644 --- a/test/Android.libarttest.mk +++ b/test/Android.libarttest.mk @@ -38,7 +38,8 @@ LIBARTTEST_COMMON_SRC_FILES := \ 461-get-reference-vreg/get_reference_vreg_jni.cc \ 466-get-live-vreg/get_live_vreg_jni.cc \ 497-inlining-and-class-loader/clear_dex_cache.cc \ - 543-env-long-ref/env_long_ref.cc + 543-env-long-ref/env_long_ref.cc \ + 554-jit-profile-file/offline_profile.cc ART_TARGET_LIBARTTEST_$(ART_PHONY_TEST_TARGET_SUFFIX) += $(ART_TARGET_TEST_OUT)/$(TARGET_ARCH)/libarttest.so ART_TARGET_LIBARTTEST_$(ART_PHONY_TEST_TARGET_SUFFIX) += $(ART_TARGET_TEST_OUT)/$(TARGET_ARCH)/libarttestd.so diff --git a/test/Android.run-test.mk b/test/Android.run-test.mk index 0925d3605..72b0dcb0e 100644 --- a/test/Android.run-test.mk +++ b/test/Android.run-test.mk @@ -276,7 +276,8 @@ TEST_ART_BROKEN_NO_PREBUILD_TESTS := TEST_ART_BROKEN_NO_RELOCATE_TESTS := \ 117-nopatchoat \ 118-noimage-dex2oat \ - 119-noimage-patchoat + 119-noimage-patchoat \ + 554-jit-profile-file ifneq (,$(filter no-relocate,$(RELOCATE_TYPES))) ART_TEST_KNOWN_BROKEN += $(call all-run-test-names,$(TARGET_TYPES),$(RUN_TYPES),$(PREBUILD_TYPES), \ @@ -298,6 +299,7 @@ TEST_ART_BROKEN_INTERPRETER_ACCESS_CHECK_TESTS := \ 412-new-array \ 471-uninitialized-locals \ 506-verify-aput \ + 554-jit-profile-file \ 800-smali ifneq (,$(filter interp-ac,$(COMPILER_TYPES))) @@ -356,13 +358,15 @@ ART_TEST_KNOWN_BROKEN += $(call all-run-test-names,$(TARGET_TYPES),ndebug,$(PREB # All these tests check that we have sane behavior if we don't have a patchoat or dex2oat. # Therefore we shouldn't run them in situations where we actually don't have these since they # explicitly test for them. These all also assume we have an image. +# 554-jit-profile-file is disabled because it needs a primary oat file to know what it should save. TEST_ART_BROKEN_FALLBACK_RUN_TESTS := \ 116-nodex2oat \ 117-nopatchoat \ 118-noimage-dex2oat \ 119-noimage-patchoat \ 137-cfi \ - 138-duplicate-classes-check2 + 138-duplicate-classes-check2 \ + 554-jit-profile-file # This test fails without an image. TEST_ART_BROKEN_NO_IMAGE_RUN_TESTS := \ @@ -413,7 +417,8 @@ endif # Known broken tests for the interpreter. # CFI unwinding expects managed frames. TEST_ART_BROKEN_INTERPRETER_RUN_TESTS := \ - 137-cfi + 137-cfi \ + 554-jit-profile-file ifneq (,$(filter interpreter,$(COMPILER_TYPES))) ART_TEST_KNOWN_BROKEN += $(call all-run-test-names,$(TARGET_TYPES),$(RUN_TYPES),$(PREBUILD_TYPES), \ @@ -520,7 +525,8 @@ TEST_ART_BROKEN_OPTIMIZING_HEAP_POISONING_RUN_TESTS := \ # Tests that should fail in the heap poisoning configuration with the interpreter. # 137: Cannot run this with the interpreter. TEST_ART_BROKEN_INTERPRETER_HEAP_POISONING_RUN_TESTS := \ - 137-cfi + 137-cfi \ + 554-jit-profile-file ifeq ($(ART_HEAP_POISONING),true) ifneq (,$(filter default,$(COMPILER_TYPES))) -- 2.11.0