Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 19 | |
David Brazdil | dcfa89b | 2018-10-31 11:04:10 +0000 | [diff] [blame] | 20 | #include "base/bit_utils.h" |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 21 | #include "base/mman.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 22 | #include "dex_file.h" |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 26 | int DexLayoutSection::MadviseLargestPageAlignedRegion(const uint8_t* begin, |
| 27 | const uint8_t* end, |
| 28 | int advice) { |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 29 | #ifdef _WIN32 |
| 30 | UNUSED(begin); |
| 31 | UNUSED(end); |
| 32 | UNUSED(advice); |
| 33 | PLOG(WARNING) << "madvise is unsupported on Windows."; |
| 34 | #else |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 35 | DCHECK_LE(begin, end); |
| 36 | begin = AlignUp(begin, kPageSize); |
| 37 | end = AlignDown(end, kPageSize); |
| 38 | if (begin < end) { |
| 39 | // TODO: remove the direct dependency on madvise here. |
| 40 | int result = madvise(const_cast<uint8_t*>(begin), end - begin, advice); |
| 41 | if (result != 0) { |
| 42 | PLOG(WARNING) << "madvise failed " << result; |
| 43 | } |
| 44 | return result; |
| 45 | } |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 46 | #endif |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 50 | void DexLayoutSection::Subsection::Madvise(const DexFile* dex_file, int advice) const { |
| 51 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 52 | DCHECK_LT(start_offset_, dex_file->Size()); |
| 53 | DCHECK_LE(end_offset_, dex_file->Size()); |
| 54 | MadviseLargestPageAlignedRegion(dex_file->Begin() + start_offset_, |
| 55 | dex_file->Begin() + end_offset_, |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 56 | advice); |
| 57 | } |
| 58 | |
| 59 | void DexLayoutSections::Madvise(const DexFile* dex_file, MadviseState state) const { |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 60 | #ifdef _WIN32 |
| 61 | UNUSED(dex_file); |
| 62 | UNUSED(state); |
| 63 | PLOG(WARNING) << "madvise is unsupported on Windows."; |
| 64 | #else |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 65 | // The dex file is already defaulted to random access everywhere. |
| 66 | for (const DexLayoutSection& section : sections_) { |
| 67 | switch (state) { |
| 68 | case MadviseState::kMadviseStateAtLoad: { |
| 69 | section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)].Madvise( |
| 70 | dex_file, |
| 71 | MADV_WILLNEED); |
| 72 | section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)].Madvise( |
| 73 | dex_file, |
| 74 | MADV_WILLNEED); |
| 75 | break; |
| 76 | } |
| 77 | case MadviseState::kMadviseStateFinishedLaunch: { |
| 78 | section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)].Madvise( |
| 79 | dex_file, |
| 80 | MADV_DONTNEED); |
| 81 | break; |
| 82 | } |
| 83 | case MadviseState::kMadviseStateFinishedTrim: { |
| 84 | section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)].Madvise( |
| 85 | dex_file, |
| 86 | MADV_DONTNEED); |
| 87 | section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUsedOnce)].Madvise( |
| 88 | dex_file, |
| 89 | MADV_DONTNEED); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 94 | #endif |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | std::ostream& operator<<(std::ostream& os, const DexLayoutSection& section) { |
| 98 | for (size_t i = 0; i < static_cast<size_t>(LayoutType::kLayoutTypeCount); ++i) { |
| 99 | const DexLayoutSection::Subsection& part = section.parts_[i]; |
| 100 | os << static_cast<LayoutType>(i) << "(" |
Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 101 | << part.start_offset_ << "-" << part.end_offset_ << ") "; |
Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 102 | } |
| 103 | return os; |
| 104 | } |
| 105 | |
| 106 | std::ostream& operator<<(std::ostream& os, const DexLayoutSections& sections) { |
| 107 | for (size_t i = 0; i < static_cast<size_t>(DexLayoutSections::SectionType::kSectionCount); ++i) { |
| 108 | os << static_cast<DexLayoutSections::SectionType>(i) << ":" << sections.sections_[i] << "\n"; |
| 109 | } |
| 110 | return os; |
| 111 | } |
| 112 | |
| 113 | } // namespace art |