blob: 929025a25385a325cc9478170ad4d5a61b2515cc [file] [log] [blame]
Mathieu Chartier120aa282017-08-05 16:03:03 -07001/*
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 Chartier120aa282017-08-05 16:03:03 -070019
David Brazdildcfa89b2018-10-31 11:04:10 +000020#include "base/bit_utils.h"
David Sehr10db8fe2018-07-18 11:01:20 -070021#include "base/mman.h"
David Sehrc431b9d2018-03-02 12:01:51 -080022#include "dex_file.h"
Mathieu Chartier120aa282017-08-05 16:03:03 -070023
24namespace art {
25
David Sehrc431b9d2018-03-02 12:01:51 -080026int DexLayoutSection::MadviseLargestPageAlignedRegion(const uint8_t* begin,
27 const uint8_t* end,
28 int advice) {
David Sehr10db8fe2018-07-18 11:01:20 -070029#ifdef _WIN32
30 UNUSED(begin);
31 UNUSED(end);
32 UNUSED(advice);
33 PLOG(WARNING) << "madvise is unsupported on Windows.";
34#else
David Sehrc431b9d2018-03-02 12:01:51 -080035 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 Sehr10db8fe2018-07-18 11:01:20 -070046#endif
David Sehrc431b9d2018-03-02 12:01:51 -080047 return 0;
48}
49
Mathieu Chartier120aa282017-08-05 16:03:03 -070050void DexLayoutSection::Subsection::Madvise(const DexFile* dex_file, int advice) const {
51 DCHECK(dex_file != nullptr);
Mathieu Chartier3e0c5172017-11-12 12:58:40 -080052 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 Chartier120aa282017-08-05 16:03:03 -070056 advice);
57}
58
59void DexLayoutSections::Madvise(const DexFile* dex_file, MadviseState state) const {
David Sehr10db8fe2018-07-18 11:01:20 -070060#ifdef _WIN32
61 UNUSED(dex_file);
62 UNUSED(state);
63 PLOG(WARNING) << "madvise is unsupported on Windows.";
64#else
Mathieu Chartier120aa282017-08-05 16:03:03 -070065 // 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 Sehr10db8fe2018-07-18 11:01:20 -070094#endif
Mathieu Chartier120aa282017-08-05 16:03:03 -070095}
96
97std::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 Chartier3e0c5172017-11-12 12:58:40 -0800101 << part.start_offset_ << "-" << part.end_offset_ << ") ";
Mathieu Chartier120aa282017-08-05 16:03:03 -0700102 }
103 return os;
104}
105
106std::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