<commit_before>#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <pthread.h>
#include <iostream>
#include <iomanip>
#include "tern/runtime/scheduler.h"
#include "tern/runtime/loghooks.h"
#include "tern/runtime/record-log.h"

#ifdef _DEBUG_LOGGER
#  define dprintf(fmt...) fprintf(stderr, fmt)
#else
#  define dprintf(fmt...)
#endif

using namespace std;

namespace tern {

__thread Logger* Logger::the = NULL;
Logger::func_map Logger::funcs;

void TxtLogger::logSync(unsigned insid, unsigned short sync,
                        unsigned turn, bool after, ...) {
  assert(sync >= syncfunc::first_sync && sync < syncfunc::num_syncs
    && "trying to log unknown synchronization operation!");

  if(!syncfunc::isSync(sync))
  {
    if (sync == syncfunc::tern_thread_begin
      || sync == syncfunc::tern_thread_end)    //  for tests, i need to know the thread_mapping
    {
      va_list args;
      va_start(args, after);
      ouf << syncfunc::getName(sync)
          << " 0x" << hex << insid << dec
          << ' ' << turn
          << ' ' << tid
          << hex << " 0x" << va_arg(args, uint64_t) << dec;
      va_end(args);
      ouf << "\n";
      ouf.flush();
    }
    return;
  }
  // YJF: why ignore prog_begin/end and thread_begin/end?
  const char *suffix = "";
  if(NumRecordsForSync(sync) == 2)
    suffix = (after?"_second":"_first");

  ouf << syncfunc::getName(sync) << suffix
      << " 0x" << hex << setfill('0') << setw(8) << insid << dec
      << ' ' << turn
      << ' ' << tid;

  va_list args;
  va_start(args, after);

  switch(sync) {
    // log one sync var (common case)
  case syncfunc::pthread_mutex_lock:
  case syncfunc::pthread_mutex_unlock:
  case syncfunc::pthread_barrier_wait:
  case syncfunc::pthread_barrier_destroy:
  case syncfunc::pthread_cond_signal:
  case syncfunc::pthread_cond_broadcast:
  case syncfunc::sem_wait:
  case syncfunc::sem_post:
  case syncfunc::pthread_join:
    ouf << hex << " 0x" << va_arg(args, uint64_t) << dec;
    break;

    // log two sync vars for cond_*wait
  case syncfunc::pthread_mutex_timedlock:
  case syncfunc::pthread_cond_wait:
  case syncfunc::pthread_barrier_init:
  case syncfunc::pthread_create:
  case syncfunc::pthread_mutex_trylock:
  case syncfunc::sem_trywait:
  case syncfunc::sem_timedwait:
    {
      //  notice "<<" operator is expanded from right to left.
      uint64_t a = va_arg(args, uint64_t);
      uint64_t b = va_arg(args, uint64_t);

    ouf << hex
        << " 0x" << a
        << " 0x" << b
        << dec;
    }
    break;
    // log three sync vars
  case syncfunc::pthread_cond_timedwait:
    {
      //  notice "<<" operator is explained from right to left.
      uint64_t a = va_arg(args, uint64_t);
      uint64_t b = va_arg(args, uint64_t);
      uint64_t c = va_arg(args, uint64_t);

    ouf << hex
        << " 0x" << a
        << " 0x" << b
        << " 0x" << c
        << dec;
    }
    break;

  default:
    assert(0 && "sync not handled");
  }
  va_end(args);
  ouf << "\n";
  ouf.flush();
}

TxtLogger::TxtLogger(int thid) {
  char logFile[64];
  getLogFilename(logFile, sizeof(logFile), thid, ".txt");

  tid = thid;
  ouf.open(logFile, ios::out|ios::trunc);
  assert(ouf && "can't open log file for write!");
}

TxtLogger::~TxtLogger()
{
  ouf.close();
}

void BinLogger::logInsid(unsigned insid) {
  checkAndGrowLogSize();

  InsidRec *rec = (InsidRec*)(buf+off);
  rec->setInsid(insid);
  rec->type = InsidRecTy;
<commit_msg>print out the measured time cost of each system call<commit_after>#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <iostream>
#include <iomanip>
#include "tern/runtime/scheduler.h"
#include "tern/runtime/loghooks.h"
#include "tern/runtime/record-log.h"

#ifdef _DEBUG_LOGGER
#  define dprintf(fmt...) fprintf(stderr, fmt)
#else
#  define dprintf(fmt...)
#endif

using namespace std;

namespace tern {

__thread Logger* Logger::the = NULL;
Logger::func_map Logger::funcs;

void TxtLogger::logSync(unsigned insid, unsigned short sync,
                        unsigned turn, bool after, ...) {
  assert(sync >= syncfunc::first_sync && sync < syncfunc::num_syncs
    && "trying to log unknown synchronization operation!");

  if(!syncfunc::isSync(sync))
  {
    if (sync == syncfunc::tern_thread_begin
      || sync == syncfunc::tern_thread_end)    //  for tests, i need to know the thread_mapping
    {
      va_list args;
      va_start(args, after);
      ouf << syncfunc::getName(sync)
          << " 0x" << hex << insid << dec
          << ' ' << turn
          << ' ' << tid
          << hex << " 0x" << va_arg(args, uint64_t) << dec;
      /// output the measured time interval
      timespec measured_time = va_arg(args, timespec);
      ouf << " " << dec << measured_time.tv_sec << ":"
          << setfill('0') << setw(9) << measured_time.tv_nsec;
 
      va_end(args);
      ouf << "\n";
      ouf.flush();
    }
    return;
  }
  // YJF: why ignore prog_begin/end and thread_begin/end?
  const char *suffix = "";
  if(NumRecordsForSync(sync) == 2)
    suffix = (after?"_second":"_first");

  ouf << syncfunc::getName(sync) << suffix
      << " 0x" << hex << setfill('0') << setw(8) << insid << dec
      << ' ' << turn
      << ' ' << tid;

  va_list args;
  va_start(args, after);

  switch(sync) {
    // log one sync var (common case)
  case syncfunc::pthread_mutex_lock:
  case syncfunc::pthread_mutex_unlock:
  case syncfunc::pthread_barrier_wait:
  case syncfunc::pthread_barrier_destroy:
  case syncfunc::pthread_cond_signal:
  case syncfunc::pthread_cond_broadcast:
  case syncfunc::sem_wait:
  case syncfunc::sem_post:
  case syncfunc::pthread_join:
    ouf << hex << " 0x" << va_arg(args, uint64_t) << dec;
    break;

    // log two sync vars for cond_*wait
  case syncfunc::pthread_mutex_timedlock:
  case syncfunc::pthread_cond_wait:
  case syncfunc::pthread_barrier_init:
  case syncfunc::pthread_create:
  case syncfunc::pthread_mutex_trylock:
  case syncfunc::sem_trywait:
  case syncfunc::sem_timedwait:
    {
      //  notice "<<" operator is expanded from right to left.
      uint64_t a = va_arg(args, uint64_t);
      uint64_t b = va_arg(args, uint64_t);

    ouf << hex
        << " 0x" << a
        << " 0x" << b
        << dec;
    }
    break;
    // log three sync vars
  case syncfunc::pthread_cond_timedwait:
    {
      //  notice "<<" operator is explained from right to left.
      uint64_t a = va_arg(args, uint64_t);
      uint64_t b = va_arg(args, uint64_t);
      uint64_t c = va_arg(args, uint64_t);

    ouf << hex
        << " 0x" << a
        << " 0x" << b
        << " 0x" << c
        << dec;
    }
    break;

  default:
    assert(0 && "sync not handled");
  }
  /// output the measured time interval
  timespec measured_time = va_arg(args, timespec);
  ouf << " " << dec << measured_time.tv_sec << ":"
          << setfill('0') << setw(9) << measured_time.tv_nsec;

  va_end(args);
  ouf << "\n";
  ouf.flush();
}

TxtLogger::TxtLogger(int thid) {
  char logFile[64];
  getLogFilename(logFile, sizeof(logFile), thid, ".txt");

  tid = thid;
  ouf.open(logFile, ios::out|ios::trunc);
  assert(ouf && "can't open log file for write!");
}

TxtLogger::~TxtLogger()
{
  ouf.close();
}

void BinLogger::logInsid(unsigned insid) {
  checkAndGrowLogSize();

  InsidRec *rec = (InsidRec*)(buf+off);
  rec->setInsid(insid);
  rec->type = InsidRecTy;

{'fraction_non_alphanumeric': 0.15487867836861124, 'fraction_numerical': 0.010583376355188436, 'mean_word_length': 3.1130573248407645, 'pattern_counts': {'":': 4, '<': 250, '<?xml version=': 0, '>': 38, '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': '5186274', 'n_tokens_mistral': 2887, 'n_tokens_neox': 2803, 'n_words': 623}