Home | History | Annotate | Download | only in runtime
      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 #include <sys/mman.h>
     20 
     21 #include "dex_file.h"
     22 #include "utils.h"
     23 
     24 namespace art {
     25 
     26 void DexLayoutSection::Subsection::Madvise(const DexFile* dex_file, int advice) const {
     27   DCHECK(dex_file != nullptr);
     28   DCHECK_LE(size_, dex_file->Size());
     29   DCHECK_LE(offset_ + size_, dex_file->Size());
     30   MadviseLargestPageAlignedRegion(dex_file->Begin() + offset_,
     31                                   dex_file->Begin() + offset_ + size_,
     32                                   advice);
     33 }
     34 
     35 void DexLayoutSections::Madvise(const DexFile* dex_file, MadviseState state) const {
     36   // The dex file is already defaulted to random access everywhere.
     37   for (const DexLayoutSection& section : sections_) {
     38     switch (state) {
     39       case MadviseState::kMadviseStateAtLoad: {
     40         section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)].Madvise(
     41             dex_file,
     42             MADV_WILLNEED);
     43         section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)].Madvise(
     44             dex_file,
     45             MADV_WILLNEED);
     46         break;
     47       }
     48       case MadviseState::kMadviseStateFinishedLaunch: {
     49         section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)].Madvise(
     50             dex_file,
     51             MADV_DONTNEED);
     52         break;
     53       }
     54       case MadviseState::kMadviseStateFinishedTrim: {
     55         section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)].Madvise(
     56             dex_file,
     57             MADV_DONTNEED);
     58         section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUsedOnce)].Madvise(
     59             dex_file,
     60             MADV_DONTNEED);
     61         break;
     62       }
     63     }
     64   }
     65 }
     66 
     67 std::ostream& operator<<(std::ostream& os, const DexLayoutSection& section) {
     68   for (size_t i = 0; i < static_cast<size_t>(LayoutType::kLayoutTypeCount); ++i) {
     69     const DexLayoutSection::Subsection& part = section.parts_[i];
     70     os << static_cast<LayoutType>(i) << "("
     71        << part.offset_ << "-" << part.offset_ + part.size_ << ") ";
     72   }
     73   return os;
     74 }
     75 
     76 std::ostream& operator<<(std::ostream& os, const DexLayoutSections& sections) {
     77   for (size_t i = 0; i < static_cast<size_t>(DexLayoutSections::SectionType::kSectionCount); ++i) {
     78     os << static_cast<DexLayoutSections::SectionType>(i) << ":" << sections.sections_[i] << "\n";
     79   }
     80   return os;
     81 }
     82 
     83 }  // namespace art
     84