Home | History | Annotate | Download | only in dex
      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 
     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 
     50 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 
     59 void DexLayoutSections::Madvise(const DexFile* dex_file, MadviseState state) const {
     60 #ifdef _WIN32
     61   UNUSED(dex_file);
     62   UNUSED(state);
     63   PLOG(WARNING) << "madvise is unsupported on Windows.";
     64 #else
     65   // 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   }
     94 #endif
     95 }
     96 
     97 std::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) << "("
    101        << part.start_offset_ << "-" << part.end_offset_ << ") ";
    102   }
    103   return os;
    104 }
    105 
    106 std::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
    114