<commit_before>//
//  rbkit.c
//  gather_stats
//
//  Created by <NAME> on 26/04/14.
//  Copyright (c) 2014 Codemancers. All rights reserved.
//

#include "ruby/ruby.h"
#include "ruby/debug.h"
#include <stdio.h>
#include <assert.h>
#include "zmq.h"
#include "msgpack.h"

// Structure is used to store profiling data
struct gc_hooks {
  VALUE hooks[3];
  VALUE enabled;
  void (*funcs[3])(void *data, int event_index);
  void *args[3];
  void *data;
  st_table *object_table;
  st_table *str_table;
  VALUE newobj_trace;
  VALUE freeobj_trace;
  int keep_remains;
  msgpack_sbuffer *sbuf;
  msgpack_packer *msgpacker;
};

static const char *event_names[] = {
  "gc_start",
  "gc_end_m",
  "gc_end_s",
  "obj_created",
  "obj_destroyed"
};

static struct gc_hooks *logger;
static int tmp_keep_remains;
static void *zmq_publisher;
static void *zmq_context;

static void send_event(int event_index) {
  const char *event = event_names[event_index];
  msgpack_sbuffer_clear(logger->sbuf);
  msgpack_pack_raw(logger->msgpacker, strlen(event));
  msgpack_pack_raw_body(logger->msgpacker, event, strlen(event));
  zmq_send(zmq_publisher, logger->sbuf->data, logger->sbuf->size, 0);
}

static void trace_gc_invocation(void *data, int event_index) {
  if (event_index == 0) {
    send_event(1);
  } else if (event_index == 2) {
    send_event(2);
  }
}

static struct gc_hooks * get_trace_logger() {
  int i = 0;
  if (logger == 0) {
    logger = ALLOC_N(struct gc_hooks, 1);
    logger->enabled = Qfalse;
    logger->newobj_trace = 0;
    logger->freeobj_trace = 0;
    logger->keep_remains = tmp_keep_remains;
    logger->object_table = st_init_numtable();
    logger->str_table = st_init_strtable();

    for (i = 0; i < 3; i++) {
      logger->funcs[i] = trace_gc_invocation;
      logger->args[i] = (void *)event_names[i];
    }
    logger->sbuf = msgpack_sbuffer_new();
    logger->msgpacker = msgpack_packer_new(logger->sbuf, msgpack_sbuffer_write);
    logger->data = 0;
  }
  return logger;
}


static void
gc_start_i(VALUE tpval, void *data)
{
  struct gc_hooks *hooks = (struct gc_hooks *)data;
  (*hooks->funcs[0])(hooks->args[0], 0);
}

static void
gc_end_mark_i(VALUE tpval, void *data)
{
  struct gc_hooks *hooks = (struct gc_hooks *)data;
  (*hooks->funcs[1])(hooks->args[1], 1);
}

static void
gc_end_sweep_i(VALUE tpval, void *data)
{
  struct gc_hooks *hooks = (struct gc_hooks *)data;
  (*hooks->funcs[2])(hooks->args[2], 2);
}

static void
create_gc_hooks(void)
{
  int i;
  logger->hooks[0] =
    rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_GC_START,     gc_start_i,     logger);
  logger->hooks[1] =
    rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_GC_END_MARK,  gc_end_mark_i,  logger);
  logger->hooks[2] =
    rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_GC_END_SWEEP, gc_end_sweep_i, logger);
  /* mark for GC */
  for (i=0; i<3; i++) rb_gc_register_mark_object(logger->hooks[i]);
}

static void newobj_i(VALUE tpval, void *data) {
  rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval);
  VALUE klass = rb_tracearg_defined_class(tparg);
  const char *klass_name = rb_class2name(klass);
  send_event(3);
}

static void freeobj_i(VALUE tpval, void *data) {
  rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval);
  VALUE klass = rb_tracearg_defined_class(tparg);
  const char *klass_name = rb_class2name(klass);
  send_event(4);
}

static VALUE start_stat_server(int argc, VALUE *argv, VALUE self) {
  int default_port = 5555;
  VALUE port;

  rb_scan_args(argc, argv, "01", &port);
  if (!NIL_P(port)) {
    default_port = FIX2INT(port);
    if (default_port < 1024 || default_port > 65000)<commit_msg>Pushing class name printing code<commit_after>//
//  rbkit.c
//  gather_stats
//
//  Created by <NAME> on 26/04/14.
//  Copyright (c) 2014 Codemancers. All rights reserved.
//

#include "ruby/ruby.h"
#include "ruby/debug.h"
#include <stdio.h>
#include <assert.h>
#include "zmq.h"
#include "msgpack.h"

// Structure is used to store profiling data
struct gc_hooks {
  VALUE hooks[3];
  VALUE enabled;
  void (*funcs[3])(void *data, int event_index);
  void *args[3];
  void *data;
  st_table *object_table;
  st_table *str_table;
  VALUE newobj_trace;
  VALUE freeobj_trace;
  int keep_remains;
  msgpack_sbuffer *sbuf;
  msgpack_packer *msgpacker;
};

static const char *event_names[] = {
  "gc_start",
  "gc_end_m",
  "gc_end_s",
  "obj_created",
  "obj_destroyed"
};

static struct gc_hooks *logger;
static int tmp_keep_remains;
static void *zmq_publisher;
static void *zmq_context;

static void send_event(int event_index) {
  const char *event = event_names[event_index];
  msgpack_sbuffer_clear(logger->sbuf);
  msgpack_pack_raw(logger->msgpacker, strlen(event));
  msgpack_pack_raw_body(logger->msgpacker, event, strlen(event));
  zmq_send(zmq_publisher, logger->sbuf->data, logger->sbuf->size, 0);
}

static void trace_gc_invocation(void *data, int event_index) {
  if (event_index == 0) {
    send_event(1);
  } else if (event_index == 2) {
    send_event(2);
  }
}

static struct gc_hooks * get_trace_logger() {
  int i = 0;
  if (logger == 0) {
    logger = ALLOC_N(struct gc_hooks, 1);
    logger->enabled = Qfalse;
    logger->newobj_trace = 0;
    logger->freeobj_trace = 0;
    logger->keep_remains = tmp_keep_remains;
    logger->object_table = st_init_numtable();
    logger->str_table = st_init_strtable();

    for (i = 0; i < 3; i++) {
      logger->funcs[i] = trace_gc_invocation;
      logger->args[i] = (void *)event_names[i];
    }
    logger->sbuf = msgpack_sbuffer_new();
    logger->msgpacker = msgpack_packer_new(logger->sbuf, msgpack_sbuffer_write);
    logger->data = 0;
  }
  return logger;
}


static void
gc_start_i(VALUE tpval, void *data)
{
  struct gc_hooks *hooks = (struct gc_hooks *)data;
  (*hooks->funcs[0])(hooks->args[0], 0);
}

static void
gc_end_mark_i(VALUE tpval, void *data)
{
  struct gc_hooks *hooks = (struct gc_hooks *)data;
  (*hooks->funcs[1])(hooks->args[1], 1);
}

static void
gc_end_sweep_i(VALUE tpval, void *data)
{
  struct gc_hooks *hooks = (struct gc_hooks *)data;
  (*hooks->funcs[2])(hooks->args[2], 2);
}

static void
create_gc_hooks(void)
{
  int i;
  logger->hooks[0] =
    rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_GC_START,     gc_start_i,     logger);
  logger->hooks[1] =
    rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_GC_END_MARK,  gc_end_mark_i,  logger);
  logger->hooks[2] =
    rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_GC_END_SWEEP, gc_end_sweep_i, logger);
  /* mark for GC */
  for (i=0; i<3; i++) rb_gc_register_mark_object(logger->hooks[i]);
}

static const char *tracer_object_klass_name(VALUE klass) {
  const char *klass_name;

  switch (BUILTIN_TYPE(klass)) {
  case T_CLASS:
  case T_MODULE:
    klass_name = rb_class2name(klass);
    break;
  default:
    klass_name = "unknown";
  }
  return klass_name;
}

static void newobj_i(VALUE tpval, void *data) {
  rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval);
  VALUE klass = rb_tracearg_defined_class(tparg);
  const char *klass_name = tracer_object_klass_name(klass);
  puts(klass_name);
  send_event(3);
}

static void freeobj_i(VALUE tpval, void *data) {
  rb_trace_arg_t *tparg = rb_tracearg_from_tracepoint(tpval);
  VALUE klass = rb_tracearg_defined_class(tparg);
  const char *klass_name = tracer_object_klass_name(klass);
  puts(klass_name);
  send_event(4);
}

static VALUE start_stat_server(int argc, VALUE *argv, VALUE self) {
  int default_port = 5555;
  VALUE port;

  rb_scan_args(argc, argv, "01", &port);
  if (!NIL_P(port)) {
    default_port = FIX2INT(port);
    if (default_port < 1024 || default_port > 65000)
{'fraction_non_alphanumeric': 0.13517786561264822, 'fraction_numerical': 0.016469038208168644, 'mean_word_length': 4.48879248011569, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 69, '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': '4181864', 'n_tokens_mistral': 3080, 'n_tokens_neox': 3079, 'n_words': 615}