Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2008 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 "reference_table.h"
     18 
     19 #include "base/mutex.h"
     20 #include "indirect_reference_table.h"
     21 #include "mirror/array.h"
     22 #include "mirror/array-inl.h"
     23 #include "mirror/class.h"
     24 #include "mirror/class-inl.h"
     25 #include "mirror/object-inl.h"
     26 #include "mirror/string-inl.h"
     27 #include "thread.h"
     28 #include "utils.h"
     29 
     30 namespace art {
     31 
     32 ReferenceTable::ReferenceTable(const char* name, size_t initial_size, size_t max_size)
     33     : name_(name), max_size_(max_size) {
     34   CHECK_LE(initial_size, max_size);
     35   entries_.reserve(initial_size);
     36 }
     37 
     38 ReferenceTable::~ReferenceTable() {
     39 }
     40 
     41 void ReferenceTable::Add(mirror::Object* obj) {
     42   DCHECK(obj != NULL);
     43   VerifyObject(obj);
     44   if (entries_.size() >= max_size_) {
     45     LOG(FATAL) << "ReferenceTable '" << name_ << "' "
     46                << "overflowed (" << max_size_ << " entries)";
     47   }
     48   entries_.push_back(GcRoot<mirror::Object>(obj));
     49 }
     50 
     51 void ReferenceTable::Remove(mirror::Object* obj) {
     52   // We iterate backwards on the assumption that references are LIFO.
     53   for (int i = entries_.size() - 1; i >= 0; --i) {
     54     mirror::Object* entry = entries_[i].Read();
     55     if (entry == obj) {
     56       entries_.erase(entries_.begin() + i);
     57       return;
     58     }
     59   }
     60 }
     61 
     62 // If "obj" is an array, return the number of elements in the array.
     63 // Otherwise, return zero.
     64 static size_t GetElementCount(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     65   if (obj == NULL || obj == kClearedJniWeakGlobal || !obj->IsArrayInstance()) {
     66     return 0;
     67   }
     68   return obj->AsArray()->GetLength();
     69 }
     70 
     71 struct ObjectComparator {
     72   bool operator()(GcRoot<mirror::Object> root1, GcRoot<mirror::Object> root2)
     73     // TODO: enable analysis when analysis can work with the STL.
     74       NO_THREAD_SAFETY_ANALYSIS {
     75     Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
     76     mirror::Object* obj1 = root1.Read<kWithoutReadBarrier>();
     77     mirror::Object* obj2 = root2.Read<kWithoutReadBarrier>();
     78     // Ensure null references and cleared jweaks appear at the end.
     79     if (obj1 == NULL) {
     80       return true;
     81     } else if (obj2 == NULL) {
     82       return false;
     83     }
     84     if (obj1 == kClearedJniWeakGlobal) {
     85       return true;
     86     } else if (obj2 == kClearedJniWeakGlobal) {
     87       return false;
     88     }
     89 
     90     // Sort by class...
     91     if (obj1->GetClass() != obj2->GetClass()) {
     92       return obj1->GetClass()->IdentityHashCode() < obj2->GetClass()->IdentityHashCode();
     93     } else {
     94       // ...then by size...
     95       size_t count1 = obj1->SizeOf();
     96       size_t count2 = obj2->SizeOf();
     97       if (count1 != count2) {
     98         return count1 < count2;
     99       } else {
    100         // ...and finally by identity hash code.
    101         return obj1->IdentityHashCode() < obj2->IdentityHashCode();
    102       }
    103     }
    104   }
    105 };
    106 
    107 // Log an object with some additional info.
    108 //
    109 // Pass in the number of elements in the array (or 0 if this is not an
    110 // array object), and the number of additional objects that are identical
    111 // or equivalent to the original.
    112 static void DumpSummaryLine(std::ostream& os, mirror::Object* obj, size_t element_count,
    113                             int identical, int equiv)
    114     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
    115   if (obj == NULL) {
    116     os << "    NULL reference (count=" << equiv << ")\n";
    117     return;
    118   }
    119   if (obj == kClearedJniWeakGlobal) {
    120     os << "    cleared jweak (count=" << equiv << ")\n";
    121     return;
    122   }
    123 
    124   std::string className(PrettyTypeOf(obj));
    125   if (obj->IsClass()) {
    126     // We're summarizing multiple instances, so using the exemplar
    127     // Class' type parameter here would be misleading.
    128     className = "java.lang.Class";
    129   }
    130   if (element_count != 0) {
    131     StringAppendF(&className, " (%zd elements)", element_count);
    132   }
    133 
    134   size_t total = identical + equiv + 1;
    135   std::string msg(StringPrintf("%5zd of %s", total, className.c_str()));
    136   if (identical + equiv != 0) {
    137     StringAppendF(&msg, " (%d unique instances)", equiv + 1);
    138   }
    139   os << "    " << msg << "\n";
    140 }
    141 
    142 size_t ReferenceTable::Size() const {
    143   return entries_.size();
    144 }
    145 
    146 void ReferenceTable::Dump(std::ostream& os) {
    147   os << name_ << " reference table dump:\n";
    148   Dump(os, entries_);
    149 }
    150 
    151 void ReferenceTable::Dump(std::ostream& os, Table& entries) {
    152   if (entries.empty()) {
    153     os << "  (empty)\n";
    154     return;
    155   }
    156 
    157   // Dump the most recent N entries.
    158   const size_t kLast = 10;
    159   size_t count = entries.size();
    160   int first = count - kLast;
    161   if (first < 0) {
    162     first = 0;
    163   }
    164   os << "  Last " << (count - first) << " entries (of " << count << "):\n";
    165   for (int idx = count - 1; idx >= first; --idx) {
    166     mirror::Object* ref = entries[idx].Read();
    167     if (ref == NULL) {
    168       continue;
    169     }
    170     if (ref == kClearedJniWeakGlobal) {
    171       os << StringPrintf("    %5d: cleared jweak\n", idx);
    172       continue;
    173     }
    174     if (ref->GetClass() == NULL) {
    175       // should only be possible right after a plain dvmMalloc().
    176       size_t size = ref->SizeOf();
    177       os << StringPrintf("    %5d: %p (raw) (%zd bytes)\n", idx, ref, size);
    178       continue;
    179     }
    180 
    181     std::string className(PrettyTypeOf(ref));
    182 
    183     std::string extras;
    184     size_t element_count = GetElementCount(ref);
    185     if (element_count != 0) {
    186       StringAppendF(&extras, " (%zd elements)", element_count);
    187     } else if (ref->GetClass()->IsStringClass()) {
    188       mirror::String* s = const_cast<mirror::Object*>(ref)->AsString();
    189       std::string utf8(s->ToModifiedUtf8());
    190       if (s->GetLength() <= 16) {
    191         StringAppendF(&extras, " \"%s\"", utf8.c_str());
    192       } else {
    193         StringAppendF(&extras, " \"%.16s... (%d chars)", utf8.c_str(), s->GetLength());
    194       }
    195     }
    196     os << StringPrintf("    %5d: ", idx) << ref << " " << className << extras << "\n";
    197   }
    198 
    199   // Make a copy of the table and sort it.
    200   Table sorted_entries;
    201   for (size_t i = 0; i < entries.size(); ++i) {
    202     mirror::Object* entry = entries[i].Read();
    203     sorted_entries.push_back(GcRoot<mirror::Object>(entry));
    204   }
    205   std::sort(sorted_entries.begin(), sorted_entries.end(), ObjectComparator());
    206 
    207   // Remove any uninteresting stuff from the list. The sort moved them all to the end.
    208   while (!sorted_entries.empty() && sorted_entries.back().IsNull()) {
    209     sorted_entries.pop_back();
    210   }
    211   while (!sorted_entries.empty() &&
    212          sorted_entries.back().Read<kWithoutReadBarrier>() == kClearedJniWeakGlobal) {
    213     sorted_entries.pop_back();
    214   }
    215   if (sorted_entries.empty()) {
    216     return;
    217   }
    218 
    219   // Dump a summary of the whole table.
    220   os << "  Summary:\n";
    221   size_t equiv = 0;
    222   size_t identical = 0;
    223   for (size_t idx = 1; idx < count; idx++) {
    224     mirror::Object* prev = sorted_entries[idx-1].Read<kWithoutReadBarrier>();
    225     mirror::Object* current = sorted_entries[idx].Read<kWithoutReadBarrier>();
    226     size_t element_count = GetElementCount(prev);
    227     if (current == prev) {
    228       // Same reference, added more than once.
    229       identical++;
    230     } else if (current->GetClass() == prev->GetClass() && GetElementCount(current) == element_count) {
    231       // Same class / element count, different object.
    232       equiv++;
    233     } else {
    234       // Different class.
    235       DumpSummaryLine(os, prev, element_count, identical, equiv);
    236       equiv = identical = 0;
    237     }
    238   }
    239   // Handle the last entry.
    240   DumpSummaryLine(os, sorted_entries.back().Read<kWithoutReadBarrier>(),
    241                   GetElementCount(sorted_entries.back().Read<kWithoutReadBarrier>()),
    242                   identical, equiv);
    243 }
    244 
    245 void ReferenceTable::VisitRoots(RootCallback* visitor, void* arg, const RootInfo& root_info) {
    246   for (GcRoot<mirror::Object>& root : entries_) {
    247     root.VisitRoot(visitor, arg, root_info);
    248   }
    249 }
    250 
    251 }  // namespace art
    252