Home | History | Annotate | Download | only in src
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "src/basic-block-profiler.h"
      6 
      7 #include <sstream>
      8 
      9 namespace v8 {
     10 namespace internal {
     11 
     12 BasicBlockProfiler::Data::Data(size_t n_blocks)
     13     : n_blocks_(n_blocks), block_ids_(n_blocks_), counts_(n_blocks_, 0) {}
     14 
     15 
     16 BasicBlockProfiler::Data::~Data() {}
     17 
     18 
     19 static void InsertIntoString(std::ostringstream* os, std::string* string) {
     20   string->insert(0, os->str());
     21 }
     22 
     23 
     24 void BasicBlockProfiler::Data::SetCode(std::ostringstream* os) {
     25   InsertIntoString(os, &code_);
     26 }
     27 
     28 
     29 void BasicBlockProfiler::Data::SetFunctionName(std::ostringstream* os) {
     30   InsertIntoString(os, &function_name_);
     31 }
     32 
     33 
     34 void BasicBlockProfiler::Data::SetSchedule(std::ostringstream* os) {
     35   InsertIntoString(os, &schedule_);
     36 }
     37 
     38 
     39 void BasicBlockProfiler::Data::SetBlockId(size_t offset, size_t block_id) {
     40   DCHECK(offset < n_blocks_);
     41   block_ids_[offset] = block_id;
     42 }
     43 
     44 
     45 uint32_t* BasicBlockProfiler::Data::GetCounterAddress(size_t offset) {
     46   DCHECK(offset < n_blocks_);
     47   return &counts_[offset];
     48 }
     49 
     50 
     51 void BasicBlockProfiler::Data::ResetCounts() {
     52   for (size_t i = 0; i < n_blocks_; ++i) {
     53     counts_[i] = 0;
     54   }
     55 }
     56 
     57 
     58 BasicBlockProfiler::BasicBlockProfiler() {}
     59 
     60 
     61 BasicBlockProfiler::Data* BasicBlockProfiler::NewData(size_t n_blocks) {
     62   Data* data = new Data(n_blocks);
     63   data_list_.push_back(data);
     64   return data;
     65 }
     66 
     67 
     68 BasicBlockProfiler::~BasicBlockProfiler() {
     69   for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
     70     delete (*i);
     71   }
     72 }
     73 
     74 
     75 void BasicBlockProfiler::ResetCounts() {
     76   for (DataList::iterator i = data_list_.begin(); i != data_list_.end(); ++i) {
     77     (*i)->ResetCounts();
     78   }
     79 }
     80 
     81 
     82 std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler& p) {
     83   os << "---- Start Profiling Data ----" << std::endl;
     84   typedef BasicBlockProfiler::DataList::const_iterator iterator;
     85   for (iterator i = p.data_list_.begin(); i != p.data_list_.end(); ++i) {
     86     os << **i;
     87   }
     88   os << "---- End Profiling Data ----" << std::endl;
     89   return os;
     90 }
     91 
     92 
     93 std::ostream& operator<<(std::ostream& os, const BasicBlockProfiler::Data& d) {
     94   const char* name = "unknown function";
     95   if (!d.function_name_.empty()) {
     96     name = d.function_name_.c_str();
     97   }
     98   if (!d.schedule_.empty()) {
     99     os << "schedule for " << name << std::endl;
    100     os << d.schedule_.c_str() << std::endl;
    101   }
    102   os << "block counts for " << name << ":" << std::endl;
    103   for (size_t i = 0; i < d.n_blocks_; ++i) {
    104     os << "block " << d.block_ids_[i] << " : " << d.counts_[i] << std::endl;
    105   }
    106   os << std::endl;
    107   if (!d.code_.empty()) {
    108     os << d.code_.c_str() << std::endl;
    109   }
    110   return os;
    111 }
    112 
    113 }  // namespace internal
    114 }  // namespace v8
    115