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
20 #include "base/bit_utils.h"
21 #include "base/mman.h"
22 #include "dex_file.h"
23
24 namespace art {
25
MadviseLargestPageAlignedRegion(const uint8_t * begin,const uint8_t * end,int advice)26 int DexLayoutSection::MadviseLargestPageAlignedRegion(const uint8_t* begin,
27 const uint8_t* end,
28 int advice) {
29 #ifdef _WIN32
30 UNUSED(begin);
31 UNUSED(end);
32 UNUSED(advice);
33 PLOG(WARNING) << "madvise is unsupported on Windows.";
34 #else
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 }
46 #endif
47 return 0;
48 }
49
Madvise(const DexFile * dex_file,int advice) const50 void DexLayoutSection::Subsection::Madvise(const DexFile* dex_file, int advice) const {
51 DCHECK(dex_file != nullptr);
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_,
56 advice);
57 }
58
MadviseAtLoad(const DexFile * dex_file) const59 void DexLayoutSections::MadviseAtLoad(const DexFile* dex_file) const {
60 #ifdef _WIN32
61 UNUSED(dex_file);
62 PLOG(WARNING) << "madvise is unsupported on Windows.";
63 #else
64 // The dex file is already defaulted to random access everywhere.
65 for (const DexLayoutSection& section : sections_) {
66 section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)].Madvise(
67 dex_file,
68 MADV_WILLNEED);
69 section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)].Madvise(
70 dex_file,
71 MADV_WILLNEED);
72 }
73 #endif
74 }
75
operator <<(std::ostream & os,const DexLayoutSection & section)76 std::ostream& operator<<(std::ostream& os, const DexLayoutSection& section) {
77 for (size_t i = 0; i < static_cast<size_t>(LayoutType::kLayoutTypeCount); ++i) {
78 const DexLayoutSection::Subsection& part = section.parts_[i];
79 os << static_cast<LayoutType>(i) << "("
80 << part.start_offset_ << "-" << part.end_offset_ << ") ";
81 }
82 return os;
83 }
84
operator <<(std::ostream & os,const DexLayoutSections & sections)85 std::ostream& operator<<(std::ostream& os, const DexLayoutSections& sections) {
86 for (size_t i = 0; i < static_cast<size_t>(DexLayoutSections::SectionType::kSectionCount); ++i) {
87 os << static_cast<DexLayoutSections::SectionType>(i) << ":" << sections.sections_[i] << "\n";
88 }
89 return os;
90 }
91
92 } // namespace art
93