blob: e1ae44d31d370bdd8dc942bfd41d2f4c26fb9f11 [file] [log] [blame]
Martin Stjernholmc15e7e42020-12-02 22:50:53 +00001/*
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#ifndef ART_LIBDEXFILE_DEX_DEX_FILE_LAYOUT_H_
18#define ART_LIBDEXFILE_DEX_DEX_FILE_LAYOUT_H_
19
20#include <algorithm>
21#include <cstdint>
22#include <iosfwd>
23
24#include <android-base/logging.h>
25
26namespace art {
27
28class DexFile;
29
30enum class LayoutType : uint8_t {
31 // Layout of things that are hot (commonly accessed), these should be pinned or madvised will
32 // need.
33 kLayoutTypeHot,
34 // Layout of things that are randomly used. These should be advised to random access.
35 // Without layout, this is the default mode when loading a dex file.
36 kLayoutTypeSometimesUsed,
37 // Layout of things that are only used during startup, these can be madvised after launch.
38 kLayoutTypeStartupOnly,
39 // Layout of things that are needed probably only once (class initializers). These can be
40 // madvised during trim events.
41 kLayoutTypeUsedOnce,
42 // Layout of things that are thought to be unused. These things should be advised to random
43 // access.
44 kLayoutTypeUnused,
45 // Unused value, just the number of elements in the enum.
46 kLayoutTypeCount,
47};
48std::ostream& operator<<(std::ostream& os, LayoutType collector_type);
49
50// Return the "best" layout option if the same item has multiple different layouts.
51static inline LayoutType MergeLayoutType(LayoutType a, LayoutType b) {
52 return std::min(a, b);
53}
54
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000055// A dex layout section such as code items or strings. Each section is composed of subsections
56// that are laid out adjacently to each other such as (hot, unused, startup, etc...).
57class DexLayoutSection {
58 public:
59 // A subsection is a a continuous range of dex file that is all part of the same layout hint.
60 class Subsection {
61 public:
62 // Use uint32_t to handle 32/64 bit cross compilation.
63 uint32_t start_offset_ = 0u;
64 uint32_t end_offset_ = 0u;
65
66 bool Contains(uint32_t offset) const {
67 return start_offset_ <= offset && offset < end_offset_;
68 }
69
70 bool Size() const {
71 DCHECK_LE(start_offset_, end_offset_);
72 return end_offset_ - start_offset_;
73 }
74
75 void CombineSection(uint32_t start_offset, uint32_t end_offset) {
76 DCHECK_LE(start_offset, end_offset);
77 if (start_offset_ == end_offset_) {
78 start_offset_ = start_offset;
79 end_offset_ = end_offset;
80 } else {
81 start_offset_ = std::min(start_offset_, start_offset);
82 end_offset_ = std::max(end_offset_, end_offset);
83 }
84 }
85
86 void Madvise(const DexFile* dex_file, int advice) const;
87 };
88
89 // Madvise the largest page-aligned region contained in [begin, end).
90 static int MadviseLargestPageAlignedRegion(const uint8_t* begin, const uint8_t* end, int advice);
91
92 Subsection parts_[static_cast<size_t>(LayoutType::kLayoutTypeCount)];
93};
94
95// A set of dex layout sections, currently there is only one section for code and one for strings.
96class DexLayoutSections {
97 public:
98 enum class SectionType : uint8_t {
99 kSectionTypeCode,
100 kSectionTypeStrings,
101 kSectionCount,
102 };
103
android-t13d2c5b22022-10-12 13:43:18 +0800104 // Advise load access about the dex file based on layout. The caller is expected to have already
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000105 // madvised to MADV_RANDOM.
android-t13d2c5b22022-10-12 13:43:18 +0800106 void MadviseAtLoad(const DexFile* dex_file) const;
Martin Stjernholmc15e7e42020-12-02 22:50:53 +0000107
108 DexLayoutSection sections_[static_cast<size_t>(SectionType::kSectionCount)];
109};
110
111std::ostream& operator<<(std::ostream& os, DexLayoutSections::SectionType collector_type);
112std::ostream& operator<<(std::ostream& os, const DexLayoutSection& section);
113std::ostream& operator<<(std::ostream& os, const DexLayoutSections& sections);
114
115} // namespace art
116
117#endif // ART_LIBDEXFILE_DEX_DEX_FILE_LAYOUT_H_