<commit_before>// Copyright (c) 2021 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "BasicPagefaultTrack.h"

#include "GlCanvas.h"

namespace orbit_gl {

namespace {

static std::array<std::string, kBasicPagefaultTrackDimension> CreateSeriesName(
    const std::string& cgroup_name, const std::string& process_name) {
  return {absl::StrFormat("Process [%s]", process_name),
          absl::StrFormat("CGroup [%s]", cgroup_name), "System"};
}

}  // namespace

BasicPagefaultTrack::BasicPagefaultTrack(Track* parent, TimeGraph* time_graph,
                                         orbit_gl::Viewport* viewport, TimeGraphLayout* layout,
                                         const std::string& name, const std::string& cgroup_name,
                                         uint64_t memory_sampling_period_ms,
                                         const orbit_client_model::CaptureData* capture_data,
                                         uint32_t indentation_level)
    : LineGraphTrack<kBasicPagefaultTrackDimension>(
          parent, time_graph, viewport, layout, name,
          CreateSeriesName(cgroup_name, capture_data->process_name()), capture_data,
          indentation_level),
      AnnotationTrack(),
      cgroup_name_(cgroup_name),
      memory_sampling_period_ms_(memory_sampling_period_ms),
      parent_(parent) {
  draw_background_ = false;

  constexpr uint8_t kTrackValueDecimalDigits = 0;
  SetNumberOfDecimalDigits(kTrackValueDecimalDigits);
}

void BasicPagefaultTrack::AddValues(
    uint64_t timestamp_ns, const std::array<double, kBasicPagefaultTrackDimension>& values) {
  if (previous_time_and_values_.has_value()) {
    std::array<double, kBasicPagefaultTrackDimension> differences;
    std::transform(values.begin(), values.end(), previous_time_and_values_.value().second.begin(),
                   differences.begin(), std::minus<double>());
    series_.AddValues(previous_time_and_values_.value().first, differences);
  }

  previous_time_and_values_ = std::make_pair(timestamp_ns, values);
}

void BasicPagefaultTrack::AddValuesAndUpdateAnnotations(
    uint64_t timestamp_ns, const std::array<double, kBasicPagefaultTrackDimension>& values) {
  AddValues(timestamp_ns, values);

  double updated_max = GetGraphMaxValue();
  std::optional<std::pair<std::string, double>> value_upper_bound = GetValueUpperBound();
  if (!value_upper_bound.has_value() || value_upper_bound.value().second < updated_max) {
    SetValueUpperBound(
        absl::StrFormat("Maximum Rate: %.0f per %d MS", updated_max, memory_sampling_period_ms_),
        updated_max);
  }

  double updated_min = GetGraphMinValue();
  std::optional<std::pair<std::string, double>> value_lower_bound = GetValueLowerBound();
  if (!value_lower_bound.has_value() || value_lower_bound.value().second > updated_min) {
    SetValueLowerBound(
        absl::StrFormat("Minimum Rate: %.0f per %d MS", updated_min, memory_sampling_period_ms_),
        updated_min);
  }
}

void BasicPagefaultTrack::Draw(Batcher& batcher, TextRenderer& text_renderer,
                               uint64_t current_mouse_time_ns, PickingMode picking_mode,
                               float z_offset) {
  LineGraphTrack<kBasicPagefaultTrackDimension>::Draw(batcher, text_renderer, current_mouse_time_ns,
                                                      picking_mode, z_offset);

  if (picking_mode != PickingMode::kNone || IsCollapsed()) return;
  AnnotationTrack::DrawAnnotation(batcher, text_renderer, layout_,
                                  GlCanvas::kZValueTrackText + z_offset);
}

void BasicPagefaultTrack::DrawSingleSeriesEntry(<commit_msg>Fix the pagefault track value upper/lower bound labels<commit_after>// Copyright (c) 2021 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "BasicPagefaultTrack.h"

#include "GlCanvas.h"

namespace orbit_gl {

namespace {

static std::array<std::string, kBasicPagefaultTrackDimension> CreateSeriesName(
    const std::string& cgroup_name, const std::string& process_name) {
  return {absl::StrFormat("Process [%s]", process_name),
          absl::StrFormat("CGroup [%s]", cgroup_name), "System"};
}

}  // namespace

BasicPagefaultTrack::BasicPagefaultTrack(Track* parent, TimeGraph* time_graph,
                                         orbit_gl::Viewport* viewport, TimeGraphLayout* layout,
                                         const std::string& name, const std::string& cgroup_name,
                                         uint64_t memory_sampling_period_ms,
                                         const orbit_client_model::CaptureData* capture_data,
                                         uint32_t indentation_level)
    : LineGraphTrack<kBasicPagefaultTrackDimension>(
          parent, time_graph, viewport, layout, name,
          CreateSeriesName(cgroup_name, capture_data->process_name()), capture_data,
          indentation_level),
      AnnotationTrack(),
      cgroup_name_(cgroup_name),
      memory_sampling_period_ms_(memory_sampling_period_ms),
      parent_(parent) {
  draw_background_ = false;

  constexpr uint8_t kTrackValueDecimalDigits = 0;
  SetNumberOfDecimalDigits(kTrackValueDecimalDigits);
}

void BasicPagefaultTrack::AddValues(
    uint64_t timestamp_ns, const std::array<double, kBasicPagefaultTrackDimension>& values) {
  if (previous_time_and_values_.has_value()) {
    std::array<double, kBasicPagefaultTrackDimension> differences;
    std::transform(values.begin(), values.end(), previous_time_and_values_.value().second.begin(),
                   differences.begin(), std::minus<double>());
    series_.AddValues(previous_time_and_values_.value().first, differences);
  }

  previous_time_and_values_ = std::make_pair(timestamp_ns, values);
}

void BasicPagefaultTrack::AddValuesAndUpdateAnnotations(
    uint64_t timestamp_ns, const std::array<double, kBasicPagefaultTrackDimension>& values) {
  AddValues(timestamp_ns, values);

  double updated_max = GetGraphMaxValue();
  std::optional<std::pair<std::string, double>> value_upper_bound = GetValueUpperBound();
  if (!value_upper_bound.has_value() || value_upper_bound.value().second < updated_max) {
    SetValueUpperBound(
        absl::StrFormat("Maximum Rate: %.0f per %d ms", updated_max, memory_sampling_period_ms_),
        updated_max);
  }

  double updated_min = GetGraphMinValue();
  std::optional<std::pair<std::string, double>> value_lower_bound = GetValueLowerBound();
  if (!value_lower_bound.has_value() || value_lower_bound.value().second > updated_min) {
    SetValueLowerBound(
        absl::StrFormat("Minimum Rate: %.0f per %d ms", updated_min, memory_sampling_period_ms_),
        updated_min);
  }
}

void BasicPagefaultTrack::Draw(Batcher& batcher, TextRenderer& text_renderer,
                               uint64_t current_mouse_time_ns, PickingMode picking_mode,
                               float z_offset) {
  LineGraphTrack<kBasicPagefaultTrackDimension>::Draw(batcher, text_renderer, current_mouse_time_ns,
                                                      picking_mode, z_offset);

  if (picking_mode != PickingMode::kNone || IsCollapsed()) return;
  AnnotationTrack::DrawAnnotation(batcher, text_renderer, layout_,
                                  GlCanvas::kZValueTrackText + z_offset);
}

void BasicPagefaultTrack::DrawSingleSeriesEntry(
{'fraction_non_alphanumeric': 0.10057127673707984, 'fraction_numerical': 0.004782781984854523, 'mean_word_length': 3.5104853205512283, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3934764', 'n_tokens_mistral': 2152, 'n_tokens_neox': 2089, 'n_words': 406}