type-stats.cc 2.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef V8_ENABLE_PRECISE_ZONE_STATS

#if defined(__clang__) || defined(__GLIBCXX__)
#include <cxxabi.h>
#endif  // __GLIBCXX__
#include <cinttypes>
#include <cstdio>

13
#include "src/base/platform/wrappers.h"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include "src/utils/utils.h"
#include "src/zone/type-stats.h"

namespace v8 {
namespace internal {

void TypeStats::MergeWith(const TypeStats& other) {
  for (auto const& item : other.map_) {
    Add(item.first, item.second);
  }
}

class Demangler {
 public:
  Demangler() = default;
  ~Demangler() {
30
    if (buffer_) base::Free(buffer_);
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    USE(buffer_len_);  // In case demangling is not supported.
  }

  const char* demangle(std::type_index type_id) {
#if defined(__clang__) || defined(__GLIBCXX__)
    int status = -1;
    char* result =
        abi::__cxa_demangle(type_id.name(), buffer_, &buffer_len_, &status);
    if (status == 0) {
      // Upon success, the buffer_ may be reallocated.
      buffer_ = result;
      return buffer_;
    }
#endif
    return type_id.name();
  }

 private:
  char* buffer_ = nullptr;
  size_t buffer_len_ = 0;
};

void TypeStats::Dump() const {
  Demangler d;
  PrintF("===== TypeStats =====\n");
  PrintF("-------------+--------------+------------+--------+--------------\n");
  PrintF("       alloc |      dealloc |      count | sizeof | name\n");
  PrintF("-------------+--------------+------------+--------+--------------\n");
  uint64_t total_allocation_count = 0;
  uint64_t total_allocated_bytes = 0;
  uint64_t total_deallocated_bytes = 0;
  for (auto const& item : map_) {
    const StatsEntry& entry = item.second;
    total_allocation_count += entry.allocation_count;
    total_allocated_bytes += entry.allocated_bytes;
66
    total_deallocated_bytes += entry.deallocated_bytes;
67 68 69 70 71 72 73 74 75 76 77 78 79 80
    PrintF("%12zu | %12zu | %10zu | %6zu | %s\n", entry.allocated_bytes,
           entry.deallocated_bytes, entry.allocation_count, entry.instance_size,
           d.demangle(item.first));
  }
  PrintF("%12" PRIu64 " | %12" PRIu64 " | %10" PRIu64
         " | ===== TOTAL STATS =====\n",
         total_allocated_bytes, total_deallocated_bytes,
         total_allocation_count);
}

}  // namespace internal
}  // namespace v8

#endif  // V8_ENABLE_PRECISE_ZONE_STATS