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 #ifndef ART_RUNTIME_DEX_FILE_LAYOUT_H_ 18 #define ART_RUNTIME_DEX_FILE_LAYOUT_H_ 19 20 #include <cstdint> 21 #include <iosfwd> 22 23 namespace art { 24 25 class DexFile; 26 27 enum class LayoutType : uint8_t { 28 // Layout of things that are randomly used. These should be advised to random access. 29 // Without layout, this is the default mode when loading a dex file. 30 kLayoutTypeSometimesUsed, 31 // Layout of things that are only used during startup, these can be madvised after launch. 32 kLayoutTypeStartupOnly, 33 // Layout of things that are hot (commonly accessed), these should be pinned or madvised will 34 // need. 35 kLayoutTypeHot, 36 // Layout of things that are needed probably only once (class initializers). These can be 37 // madvised during trim events. 38 kLayoutTypeUsedOnce, 39 // Layout of things that are thought to be unused. These things should be advised to random 40 // access. 41 kLayoutTypeUnused, 42 // Unused value, just the number of elements in the enum. 43 kLayoutTypeCount, 44 }; 45 std::ostream& operator<<(std::ostream& os, const LayoutType& collector_type); 46 47 enum class MadviseState : uint8_t { 48 // Madvise based on a file that was just loaded. 49 kMadviseStateAtLoad, 50 // Madvise based after launch is finished. 51 kMadviseStateFinishedLaunch, 52 // Trim by madvising code that is unlikely to be too important in the future. 53 kMadviseStateFinishedTrim, 54 }; 55 std::ostream& operator<<(std::ostream& os, const MadviseState& collector_type); 56 57 // A dex layout section such as code items or strings. Each section is composed of subsections 58 // that are layed out ajacently to each other such as (hot, unused, startup, etc...). 59 class DexLayoutSection { 60 public: 61 // A subsection is a a continuous range of dex file that is all part of the same layout hint. 62 class Subsection { 63 public: 64 // Use uint32_t to handle 32/64 bit cross compilation. 65 uint32_t offset_ = 0u; 66 uint32_t size_ = 0u; 67 68 void Madvise(const DexFile* dex_file, int advice) const; 69 }; 70 71 Subsection parts_[static_cast<size_t>(LayoutType::kLayoutTypeCount)]; 72 }; 73 74 // A set of dex layout sections, currently there is only one section for code and one for strings. 75 class DexLayoutSections { 76 public: 77 enum class SectionType : uint8_t { 78 kSectionTypeCode, 79 kSectionTypeStrings, 80 kSectionCount, 81 }; 82 83 // Advise access about the dex file based on layout. The caller is expected to have already 84 // madvised to MADV_RANDOM. 85 void Madvise(const DexFile* dex_file, MadviseState state) const; 86 87 DexLayoutSection sections_[static_cast<size_t>(SectionType::kSectionCount)]; 88 }; 89 90 std::ostream& operator<<(std::ostream& os, const DexLayoutSections::SectionType& collector_type); 91 std::ostream& operator<<(std::ostream& os, const DexLayoutSection& section); 92 std::ostream& operator<<(std::ostream& os, const DexLayoutSections& sections); 93 94 } // namespace art 95 96 #endif // ART_RUNTIME_DEX_FILE_LAYOUT_H_ 97