<commit_before>#include "types.h"
#include "stat.h"
#include "user.h"
#include "amd64.h"
#include "uspinlock.h"
#include "mtrace.h"
#include "pthread.h"

#include <sys/mman.h>

enum { readaccess = 1 };
enum { verbose = 0 };
enum { npg = 1 };

static pthread_barrier_t bar;
static pthread_barrier_t bar2;
static int niter = 100;

void*
thr(void *arg)
{
  u64 tid = (u64)arg;
  if (setaffinity(tid) < 0)
    fprintf(1, "setaffinity err\n");

  pthread_barrier_wait(&bar);

  if (tid == 0)
    mtenable_type(mtrace_record_ascope, "xv6-asharing");

  pthread_barrier_wait(&bar2);

  for (int i = 0; i < niter; i++) {
    if (verbose && ((i % 100) == 0))
      fprintf(1, "%d: %d ops\n", tid, i);

    volatile char *p = (char*) (0x100000000UL + tid * npg * 0x100000);
    if (mmap((void *) p, npg * 4096, PROT_READ|PROT_WRITE,
             MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED) {
      fprintf(1, "%d: map failed\n", tid);
      exit();
    }

    if (readaccess) {
      for (int j = 0; j < npg * 4096; j++)
	p[j] = '\0';
    }

    if (munmap((void *) p, npg * 4096) < 0) {
      fprintf(1, "%d: unmap failed\n", tid);
      exit();
    }
  }
  return 0;
}

int
main(int ac, char **av)
{
  if (ac < 2)
    die("usage: %s nthreads [nloop]", av[0]);

  int nthread = atoi(av[1]);
  if (ac > 2)
    niter = atoi(av[2]);

  pthread_barrier_init(&bar, 0, nthread);
  pthread_barrier_init(&bar2, 0, nthread);

  for(u64 i = 0; i < nthread; i++) {
    pthread_t tid;
    pthread_create(&tid, 0, thr, (void*) i);
    if (0) fprintf(1, "mapbench[%d]: child %d\n", getpid(), tid);
  }

  for(int i = 0; i < nthread; i++)
    wait();

  mtdisable("xv6-asharing");

  // fprintf(1, "mapbench[%d]: done\n", getpid());
  // halt();
  exit();
}
<commit_msg>mapbench hack: ensure niter is in bss to work around buggy vm<commit_after>#include "types.h"
#include "stat.h"
#include "user.h"
#include "amd64.h"
#include "uspinlock.h"
#include "mtrace.h"
#include "pthread.h"

#include <sys/mman.h>

enum { readaccess = 1 };
enum { verbose = 0 };
enum { npg = 1 };

static pthread_barrier_t bar;
static pthread_barrier_t bar2;
static int niter;

void*
thr(void *arg)
{
  u64 tid = (u64)arg;
  if (setaffinity(tid) < 0)
    fprintf(1, "setaffinity err\n");

  pthread_barrier_wait(&bar);

  if (tid == 0)
    mtenable_type(mtrace_record_ascope, "xv6-asharing");

  pthread_barrier_wait(&bar2);

  for (int i = 0; i < niter; i++) {
    if (verbose && ((i % 100) == 0))
      fprintf(1, "%d: %d ops\n", tid, i);

    volatile char *p = (char*) (0x100000000UL + tid * npg * 0x100000);
    if (mmap((void *) p, npg * 4096, PROT_READ|PROT_WRITE,
             MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED) {
      fprintf(1, "%d: map failed\n", tid);
      exit();
    }

    if (readaccess) {
      for (int j = 0; j < npg * 4096; j++)
	p[j] = '\0';
    }

    if (munmap((void *) p, npg * 4096) < 0) {
      fprintf(1, "%d: unmap failed\n", tid);
      exit();
    }
  }
  return 0;
}

int
main(int ac, char **av)
{
  if (ac < 2)
    die("usage: %s nthreads [nloop]", av[0]);

  int nthread = atoi(av[1]);
  niter = 100;
  if (ac > 2)
    niter = atoi(av[2]);

  pthread_barrier_init(&bar, 0, nthread);
  pthread_barrier_init(&bar2, 0, nthread);

  for(u64 i = 0; i < nthread; i++) {
    pthread_t tid;
    pthread_create(&tid, 0, thr, (void*) i);
    if (0) fprintf(1, "mapbench[%d]: child %d\n", getpid(), tid);
  }

  for(int i = 0; i < nthread; i++)
    wait();

  mtdisable("xv6-asharing");

  // fprintf(1, "mapbench[%d]: done\n", getpid());
  // halt();
  exit();
}

{'fraction_non_alphanumeric': 0.19390891310421907, 'fraction_numerical': 0.04358759430008382, 'mean_word_length': 3.0451977401129944, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5851338', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1521, 'n_words': 380}