11cb0ef41Sopenharmony_ci// Copyright 2014 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/compiler/pipeline-statistics.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include <memory>
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#include "src/codegen/optimized-compilation-info.h"
101cb0ef41Sopenharmony_ci#include "src/compiler/zone-stats.h"
111cb0ef41Sopenharmony_ci#include "src/objects/shared-function-info.h"
121cb0ef41Sopenharmony_ci#include "src/objects/string.h"
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cinamespace v8 {
151cb0ef41Sopenharmony_cinamespace internal {
161cb0ef41Sopenharmony_cinamespace compiler {
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciconstexpr char PipelineStatistics::kTraceCategory[];
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_civoid PipelineStatistics::CommonStats::Begin(
211cb0ef41Sopenharmony_ci    PipelineStatistics* pipeline_stats) {
221cb0ef41Sopenharmony_ci  DCHECK(!scope_);
231cb0ef41Sopenharmony_ci  scope_.reset(new ZoneStats::StatsScope(pipeline_stats->zone_stats_));
241cb0ef41Sopenharmony_ci  timer_.Start();
251cb0ef41Sopenharmony_ci  outer_zone_initial_size_ = pipeline_stats->OuterZoneSize();
261cb0ef41Sopenharmony_ci  allocated_bytes_at_start_ =
271cb0ef41Sopenharmony_ci      outer_zone_initial_size_ -
281cb0ef41Sopenharmony_ci      pipeline_stats->total_stats_.outer_zone_initial_size_ +
291cb0ef41Sopenharmony_ci      pipeline_stats->zone_stats_->GetCurrentAllocatedBytes();
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_civoid PipelineStatistics::CommonStats::End(
341cb0ef41Sopenharmony_ci    PipelineStatistics* pipeline_stats,
351cb0ef41Sopenharmony_ci    CompilationStatistics::BasicStats* diff) {
361cb0ef41Sopenharmony_ci  DCHECK(scope_);
371cb0ef41Sopenharmony_ci  diff->function_name_ = pipeline_stats->function_name_;
381cb0ef41Sopenharmony_ci  diff->delta_ = timer_.Elapsed();
391cb0ef41Sopenharmony_ci  size_t outer_zone_diff =
401cb0ef41Sopenharmony_ci      pipeline_stats->OuterZoneSize() - outer_zone_initial_size_;
411cb0ef41Sopenharmony_ci  diff->max_allocated_bytes_ = outer_zone_diff + scope_->GetMaxAllocatedBytes();
421cb0ef41Sopenharmony_ci  diff->absolute_max_allocated_bytes_ =
431cb0ef41Sopenharmony_ci      diff->max_allocated_bytes_ + allocated_bytes_at_start_;
441cb0ef41Sopenharmony_ci  diff->total_allocated_bytes_ =
451cb0ef41Sopenharmony_ci      outer_zone_diff + scope_->GetTotalAllocatedBytes();
461cb0ef41Sopenharmony_ci  scope_.reset();
471cb0ef41Sopenharmony_ci  timer_.Stop();
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciPipelineStatistics::PipelineStatistics(OptimizedCompilationInfo* info,
511cb0ef41Sopenharmony_ci                                       CompilationStatistics* compilation_stats,
521cb0ef41Sopenharmony_ci                                       ZoneStats* zone_stats)
531cb0ef41Sopenharmony_ci    : outer_zone_(info->zone()),
541cb0ef41Sopenharmony_ci      zone_stats_(zone_stats),
551cb0ef41Sopenharmony_ci      compilation_stats_(compilation_stats),
561cb0ef41Sopenharmony_ci      code_kind_(info->code_kind()),
571cb0ef41Sopenharmony_ci      phase_kind_name_(nullptr),
581cb0ef41Sopenharmony_ci      phase_name_(nullptr) {
591cb0ef41Sopenharmony_ci  if (info->has_shared_info()) {
601cb0ef41Sopenharmony_ci    function_name_.assign(info->shared_info()->DebugNameCStr().get());
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci  total_stats_.Begin(this);
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciPipelineStatistics::~PipelineStatistics() {
661cb0ef41Sopenharmony_ci  if (InPhaseKind()) EndPhaseKind();
671cb0ef41Sopenharmony_ci  CompilationStatistics::BasicStats diff;
681cb0ef41Sopenharmony_ci  total_stats_.End(this, &diff);
691cb0ef41Sopenharmony_ci  compilation_stats_->RecordTotalStats(diff);
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_civoid PipelineStatistics::BeginPhaseKind(const char* phase_kind_name) {
741cb0ef41Sopenharmony_ci  DCHECK(!InPhase());
751cb0ef41Sopenharmony_ci  if (InPhaseKind()) EndPhaseKind();
761cb0ef41Sopenharmony_ci  TRACE_EVENT_BEGIN1(kTraceCategory, phase_kind_name, "kind",
771cb0ef41Sopenharmony_ci                     CodeKindToString(code_kind_));
781cb0ef41Sopenharmony_ci  phase_kind_name_ = phase_kind_name;
791cb0ef41Sopenharmony_ci  phase_kind_stats_.Begin(this);
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_civoid PipelineStatistics::EndPhaseKind() {
831cb0ef41Sopenharmony_ci  DCHECK(!InPhase());
841cb0ef41Sopenharmony_ci  CompilationStatistics::BasicStats diff;
851cb0ef41Sopenharmony_ci  phase_kind_stats_.End(this, &diff);
861cb0ef41Sopenharmony_ci  compilation_stats_->RecordPhaseKindStats(phase_kind_name_, diff);
871cb0ef41Sopenharmony_ci  TRACE_EVENT_END2(kTraceCategory, phase_kind_name_, "kind",
881cb0ef41Sopenharmony_ci                   CodeKindToString(code_kind_), "stats",
891cb0ef41Sopenharmony_ci                   TRACE_STR_COPY(diff.AsJSON().c_str()));
901cb0ef41Sopenharmony_ci}
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_civoid PipelineStatistics::BeginPhase(const char* phase_name) {
931cb0ef41Sopenharmony_ci  TRACE_EVENT_BEGIN1(kTraceCategory, phase_name, "kind",
941cb0ef41Sopenharmony_ci                     CodeKindToString(code_kind_));
951cb0ef41Sopenharmony_ci  DCHECK(InPhaseKind());
961cb0ef41Sopenharmony_ci  phase_name_ = phase_name;
971cb0ef41Sopenharmony_ci  phase_stats_.Begin(this);
981cb0ef41Sopenharmony_ci}
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_civoid PipelineStatistics::EndPhase() {
1011cb0ef41Sopenharmony_ci  DCHECK(InPhaseKind());
1021cb0ef41Sopenharmony_ci  CompilationStatistics::BasicStats diff;
1031cb0ef41Sopenharmony_ci  phase_stats_.End(this, &diff);
1041cb0ef41Sopenharmony_ci  compilation_stats_->RecordPhaseStats(phase_kind_name_, phase_name_, diff);
1051cb0ef41Sopenharmony_ci  TRACE_EVENT_END2(kTraceCategory, phase_name_, "kind",
1061cb0ef41Sopenharmony_ci                   CodeKindToString(code_kind_), "stats",
1071cb0ef41Sopenharmony_ci                   TRACE_STR_COPY(diff.AsJSON().c_str()));
1081cb0ef41Sopenharmony_ci}
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci}  // namespace compiler
1111cb0ef41Sopenharmony_ci}  // namespace internal
1121cb0ef41Sopenharmony_ci}  // namespace v8
113