<commit_before>package dym;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import som.compiler.MixinDefinition;
import som.interpreter.nodes.dispatch.Dispatchable;
import som.vmobjects.SClass;
import som.vmobjects.SInvokable;
import som.vmobjects.SSymbol;

import com.oracle.truffle.api.source.SourceSection;

import dym.profiles.AllocationProfile;
import dym.profiles.ArrayCreationProfile;
import dym.profiles.InvocationProfile;
import dym.profiles.MethodCallsiteProbe;
import dym.profiles.ReadValueProfile;
import dym.profiles.StructuralProbe;


public final class MetricsCsvWriter {

  private final Map<String, Map<SourceSection, ? extends JsonSerializable>> data;
  private final String metricsFolder;
  private final StructuralProbe structuralProbe; // TODO: not sure, we should probably not depend on the probe here

  private MetricsCsvWriter(
      final Map<String, Map<SourceSection, ? extends JsonSerializable>> data,
      final String metricsFolder, final StructuralProbe probe) {
    this.data          = data;
    this.metricsFolder = metricsFolder;
    this.structuralProbe = probe;
  }

  public static void fileOut(
      final Map<String, Map<SourceSection, ? extends JsonSerializable>> data,
      final String metricsFolder,
      final StructuralProbe structuralProbe) { // TODO: remove direct StructuralProbe passing hack
    new MetricsCsvWriter(data, metricsFolder, structuralProbe).createCsvFiles();
  }

  private void createCsvFiles() {
    new File(metricsFolder).mkdirs();

    methodActivations();
    methodCallsites();
    newObjectCount();
    newArrayCount();
    fieldReads();
    localReads();
    usedClassesAndMethods();
  }

  private void methodActivations() {
    @SuppressWarnings("unchecked")
    Map<SourceSection, InvocationProfile> profiles = (Map<SourceSection, InvocationProfile>) data.get("methodInvocationProfile");

    try (PrintWriter file = new PrintWriter(metricsFolder + File.separator + "method-activations.csv")) {
      file.println("Source Identifier\tActivation Count");
      for (InvocationProfile p : profiles.values()) {
        file.print(p.getSourceSection().getIdentifier()); //TODO: probably need something more precise
        file.print("\t");
        file.println(p.getValue());
      }
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }
  }

  private void methodCallsites() {
    @SuppressWarnings("unchecked")
    Map<SourceSection, MethodCallsiteProbe> profiles = (Map<SourceSection, MethodCallsiteProbe>) data.get("methodCallsite");

    try (PrintWriter file = new PrintWriter(metricsFolder + File.separator + "method-callsites.csv")) {
      file.println("Source Section\tCall Count");
      for (MethodCallsiteProbe p : profiles.values()) {
        String abbrv = getSourceSectionAbbrv(p.getSourceSection());
        file.print(abbrv);
        file.print("\t");
        file.println(p.getValue());
      }
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }
  }

  private void newObjectCount() {
    @SuppressWarnings("unchecked")
    Map<SourceSection, AllocationProfile> profiles = (Map<SourceSection, AllocationProfile>) data.get("newObjectCount");

    try (PrintWriter file = new PrintWriter(metricsFolder + File.separator + "new-objects.csv")) {
      file.println("Source Section\tNew Objects\tNumber of Fields\tClass");

      for (AllocationProfile p : profiles.values()) {
        String abbrv = getSourceSectionAbbrv(p.getSourceSection());
        file.print(abbrv);
        file.print("\t");
        file.print(p.getValue());
        file.print("\t");
        file.print(p.getNumberOfObjectFields());
        file.print("\t");
        file.println(p.getSOMClass().getName().getString());
      }<commit_msg>Replace MethodCallsiteProbe with CallsiteProfile<commit_after>package dym;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;

import som.compiler.MixinDefinition;
import som.interpreter.nodes.dispatch.Dispatchable;
import som.vmobjects.SClass;
import som.vmobjects.SInvokable;
import som.vmobjects.SSymbol;

import com.oracle.truffle.api.source.SourceSection;

import dym.profiles.AllocationProfile;
import dym.profiles.ArrayCreationProfile;
import dym.profiles.InvocationProfile;
import dym.profiles.CallsiteProfile;
import dym.profiles.ReadValueProfile;
import dym.profiles.StructuralProbe;


public final class MetricsCsvWriter {

  private final Map<String, Map<SourceSection, ? extends JsonSerializable>> data;
  private final String metricsFolder;
  private final StructuralProbe structuralProbe; // TODO: not sure, we should probably not depend on the probe here

  private MetricsCsvWriter(
      final Map<String, Map<SourceSection, ? extends JsonSerializable>> data,
      final String metricsFolder, final StructuralProbe probe) {
    this.data          = data;
    this.metricsFolder = metricsFolder;
    this.structuralProbe = probe;
  }

  public static void fileOut(
      final Map<String, Map<SourceSection, ? extends JsonSerializable>> data,
      final String metricsFolder,
      final StructuralProbe structuralProbe) { // TODO: remove direct StructuralProbe passing hack
    new MetricsCsvWriter(data, metricsFolder, structuralProbe).createCsvFiles();
  }

  private void createCsvFiles() {
    new File(metricsFolder).mkdirs();

    methodActivations();
    methodCallsites();
    newObjectCount();
    newArrayCount();
    fieldReads();
    localReads();
    usedClassesAndMethods();
  }

  private void methodActivations() {
    @SuppressWarnings("unchecked")
    Map<SourceSection, InvocationProfile> profiles = (Map<SourceSection, InvocationProfile>) data.get("methodInvocationProfile");

    try (PrintWriter file = new PrintWriter(metricsFolder + File.separator + "method-activations.csv")) {
      file.println("Source Identifier\tActivation Count");
      for (InvocationProfile p : profiles.values()) {
        file.print(p.getSourceSection().getIdentifier()); //TODO: probably need something more precise
        file.print("\t");
        file.println(p.getValue());
      }
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }
  }

  private void methodCallsites() {
    @SuppressWarnings("unchecked")
    Map<SourceSection, CallsiteProfile> profiles = (Map<SourceSection, CallsiteProfile>) data.get("methodCallsite");


    try (PrintWriter file = new PrintWriter(metricsFolder + File.separator + "method-callsites.csv")) {
      file.println("Source Section\tCall Count");
      for (CallsiteProfile p : profiles.values()) {
        String abbrv = getSourceSectionAbbrv(p.getSourceSection());
        file.print(abbrv);
        file.print("\t");
        file.println(p.getValue());
      }
    } catch (FileNotFoundException e) {
      throw new RuntimeException(e);
    }
  }

  private void newObjectCount() {
    @SuppressWarnings("unchecked")
    Map<SourceSection, AllocationProfile> profiles = (Map<SourceSection, AllocationProfile>) data.get("newObjectCount");

    try (PrintWriter file = new PrintWriter(metricsFolder + File.separator + "new-objects.csv")) {
      file.println("Source Section\tNew Objects\tNumber of Fields\tClass");

      for (AllocationProfile p : profiles.values()) {
        String abbrv = getSourceSectionAbbrv(p.getSourceSection());
        file.print(abbrv);
        file.print("\t");
        file.print(p.getValue());
        file.print("\t");
        file.print(p.getNumberOfObjectFields());
        file.print("\t");
        file.println(p.getSOMClass().getName().getString());
      }
{'fraction_non_alphanumeric': 0.11293634496919917, 'fraction_numerical': 0.0, 'mean_word_length': 4.0768729641693815, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3077091', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2176, 'n_words': 479}