1 /* 2 * Copyright (C) 2017 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 17 #include "dex_file_layout.h" 18 19 #include <sys/mman.h> 20 21 #include "dex_file.h" 22 23 namespace art { 24 25 int DexLayoutSection::MadviseLargestPageAlignedRegion(const uint8_t* begin, 26 const uint8_t* end, 27 int advice) { 28 DCHECK_LE(begin, end); 29 begin = AlignUp(begin, kPageSize); 30 end = AlignDown(end, kPageSize); 31 if (begin < end) { 32 // TODO: remove the direct dependency on madvise here. 33 int result = madvise(const_cast<uint8_t*>(begin), end - begin, advice); 34 if (result != 0) { 35 PLOG(WARNING) << "madvise failed " << result; 36 } 37 return result; 38 } 39 return 0; 40 } 41 42 void DexLayoutSection::Subsection::Madvise(const DexFile* dex_file, int advice) const { 43 DCHECK(dex_file != nullptr); 44 DCHECK_LT(start_offset_, dex_file->Size()); 45 DCHECK_LE(end_offset_, dex_file->Size()); 46 MadviseLargestPageAlignedRegion(dex_file->Begin() + start_offset_, 47 dex_file->Begin() + end_offset_, 48 advice); 49 } 50 51 void DexLayoutSections::Madvise(const DexFile* dex_file, MadviseState state) const { 52 // The dex file is already defaulted to random access everywhere. 53 for (const DexLayoutSection& section : sections_) { 54 switch (state) { 55 case MadviseState::kMadviseStateAtLoad: { 56 section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)].Madvise( 57 dex_file, 58 MADV_WILLNEED); 59 section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)].Madvise( 60 dex_file, 61 MADV_WILLNEED); 62 break; 63 } 64 case MadviseState::kMadviseStateFinishedLaunch: { 65 section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)].Madvise( 66 dex_file, 67 MADV_DONTNEED); 68 break; 69 } 70 case MadviseState::kMadviseStateFinishedTrim: { 71 section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)].Madvise( 72 dex_file, 73 MADV_DONTNEED); 74 section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUsedOnce)].Madvise( 75 dex_file, 76 MADV_DONTNEED); 77 break; 78 } 79 } 80 } 81 } 82 83 std::ostream& operator<<(std::ostream& os, const DexLayoutSection& section) { 84 for (size_t i = 0; i < static_cast<size_t>(LayoutType::kLayoutTypeCount); ++i) { 85 const DexLayoutSection::Subsection& part = section.parts_[i]; 86 os << static_cast<LayoutType>(i) << "(" 87 << part.start_offset_ << "-" << part.end_offset_ << ") "; 88 } 89 return os; 90 } 91 92 std::ostream& operator<<(std::ostream& os, const DexLayoutSections& sections) { 93 for (size_t i = 0; i < static_cast<size_t>(DexLayoutSections::SectionType::kSectionCount); ++i) { 94 os << static_cast<DexLayoutSections::SectionType>(i) << ":" << sections.sections_[i] << "\n"; 95 } 96 return os; 97 } 98 99 } // namespace art 100