<commit_before>#include "Python.h"

/*
 * This is the C source code for various bits of the Vision Egg library.
 *
 * Copyright (c) 2001 <NAME>.  Distributed under the terms of the
 * GNU General Public License (GPL).
 *
 * $Revision$
 * $Date$
 * Author = <NAME> <<EMAIL>>
 *
 */
 
/* For _visionegg_getTime */
#include <sys/time.h>
#include <time.h>

/* For _visionegg_precise_sleep */
#include <unistd.h>

/* For _visionegg_setRealtime */
#include <sys/mman.h>
#include <sched.h>
#include <errno.h>

#define XXX_LPT_DOUT // Very non-portable i386 linux hack

#ifdef XXX_LPT_DOUT
#include <stdio.h> // for ioperm
#include <asm/io.h> // for outb
#define LPT 0x378
#endif

#define TRY(E)     if(! (E)) return NULL

static PyObject *_visionegg_getTime(PyObject * self, PyObject * args)
{
  double result;

#define USE_GETTIMEOFDAY
#ifdef USE_GETTIMEOFDAY
  static struct timeval tv;
  double sec;
  double usec;
  gettimeofday(&tv,NULL);
  sec = tv.tv_sec;
  usec = tv.tv_usec;
  result = sec + (usec*1.0e-6);
#endif
#ifdef USE_GETTIMEOFDAY2
  /* This seems broken. */
  //  static struct timeval mytv;
  //  gettimeofday(&mytv,NULL);
  //  return(mytv.tv_sec+mytv.tv_usec/1000000.0);
  static struct timespec myts;
  gettimeofday(&myts,NULL);
  result = (double)myts.tv_sec + (double)((double)myts.tv_nsec/(double)1.0e9);
#endif
#ifdef USE_RDTSC
  /* Never really tried to get this working.  It's not very portable! */
  // This bit is shamelessly stolen and modified from
  // latency-graph.c, (c) <NAME>, http://www.linuxdj.com/latency-graph
  extern double cpu_hz; // set magically from /proc/cpuinfo?
  unsigned long long int rdtsc;

  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (rdtsc));

  result = rdtsc / cpu_hz;
#endif
  return Py_BuildValue("d",result);
}

static PyObject *_visionegg_preciseSleep(PyObject * self, PyObject * args)
{
  double arg1;
  unsigned long usec;

  TRY(PyArg_ParseTuple(args,"d", &arg1));
  usec = (unsigned long)(arg1*1.0e6); // Convert seconds to microseconds
  usleep(usec);
  Py_INCREF(Py_None);
  return Py_None;
}

static PyObject *_visionegg_setRealtime(PyObject * self, PyObject * args)
{
  int policy;
  struct sched_param params;

  /* First, tell the scheduler that we want maximum priority! */
  //  policy = SCHED_RR;
  policy = SCHED_FIFO;
  params.sched_priority = sched_get_priority_max(policy);

  if (sched_setscheduler(0,policy,&params) == -1) { // pid 0 means this process
    switch(errno) {
    case EPERM:
      PyErr_SetString(PyExc_RuntimeError,"sched_setscheduler returned EPERM (Are you superuser?)");
      return NULL;
      break;
    case ESRCH:
      PyErr_SetString(PyExc_RuntimeError,"sched_setscheduler returned ESRCH");
      return NULL;
      break;
    case EINVAL:
      PyErr_SetString(PyExc_RuntimeError,"sched_setscheduler returned EINVAL");
      return NULL;
      break;
    default:
      PyErr_SetString(PyExc_RuntimeError,"sched_setscheduler returned unknown error");
      return NULL;
      break;
    }
  }
  
#ifdef MCL_CURRENT
#ifdef MCL_FUTURE
  /* Second, prevent the memory from paging. */
  if (mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
    switch(errno) {
    case EPERM:
      PyErr_SetString(PyExc_RuntimeError,"mlockall returned EPERM (Are you superuser?)");
      return NULL;
      break;
    case ESRCH:
      PyErr_SetString(PyExc_RuntimeError,"mlockall returned ESRCH");
      return NULL;
      break;
    case EINVAL:
      PyErr_SetString(PyExc_RuntimeError,"mlockall returned EINVAL");
      return NULL;
      break;
    default:
      PyErr_SetString(PyExc_RuntimeError,"mlockall returned unknown error");
      return NULL;
      break;
    }
  }
#endif /* closes ifdef MCL_FUTURE */
#endif /* closes ifdef MCL_CURRENT */

  Py_INCREF(Py_None);
  return Py_None;  /* It worked OK. */
}

#ifdef XXX_LPT_DOUT
static int lpt_works; // define a couple of global variables
static int lpt_state;
#endif

static PyObject *_visionegg_toggleDOut(PyObject * self, PyObject * args)
{
#ifdef XXX_LPT_DOUT<commit_msg>Made compile under Mac OS X by chopping setscheduler and LPT hack.<commit_after>#include "Python.h"

/*
 * This is the C source code for various bits of the Vision Egg library.
 *
 * Copyright (c) 2001 <NAME>.  Distributed under the terms of the
 * GNU General Public License (GPL).
 *
 * $Revision$
 * $Date$
 * Author = <NAME> <<EMAIL>>
 *
 */
 
/* For _visionegg_getTime */
#include <sys/time.h>
#include <time.h>

/* For _visionegg_precise_sleep */
#include <unistd.h>

/* For _visionegg_setRealtime */
#ifndef __APPLE_CC__
#include <sys/mman.h>
#include <sched.h>
#include <errno.h>
#endif

#ifdef XXX_LPT_DOUT // This turns on a very non-portable i386 linux hack
#include <stdio.h> // for ioperm
#include <asm/io.h> // for outb
#define LPT 0x378
#endif

#define TRY(E)     if(! (E)) return NULL

static PyObject *_visionegg_getTime(PyObject * self, PyObject * args)
{
  double result;

#define USE_GETTIMEOFDAY
#ifdef USE_GETTIMEOFDAY
  static struct timeval tv;
  double sec;
  double usec;
  gettimeofday(&tv,NULL);
  sec = tv.tv_sec;
  usec = tv.tv_usec;
  result = sec + (usec*1.0e-6);
#endif
#ifdef USE_GETTIMEOFDAY2
  /* This seems broken. */
  //  static struct timeval mytv;
  //  gettimeofday(&mytv,NULL);
  //  return(mytv.tv_sec+mytv.tv_usec/1000000.0);
  static struct timespec myts;
  gettimeofday(&myts,NULL);
  result = (double)myts.tv_sec + (double)((double)myts.tv_nsec/(double)1.0e9);
#endif
#ifdef USE_RDTSC
  /* Never really tried to get this working.  It's not very portable! */
  // This bit is shamelessly stolen and modified from
  // latency-graph.c, (c) <NAME>, http://www.linuxdj.com/latency-graph
  extern double cpu_hz; // set magically from /proc/cpuinfo?
  unsigned long long int rdtsc;

  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (rdtsc));

  result = rdtsc / cpu_hz;
#endif
  return Py_BuildValue("d",result);
}

static PyObject *_visionegg_preciseSleep(PyObject * self, PyObject * args)
{
  double arg1;
  unsigned long usec;

  TRY(PyArg_ParseTuple(args,"d", &arg1));
  usec = (unsigned long)(arg1*1.0e6); // Convert seconds to microseconds
  usleep(usec);
  Py_INCREF(Py_None);
  return Py_None;
}

static PyObject *_visionegg_setRealtime(PyObject * self, PyObject * args)
{
#ifndef __APPLE_CC__ // Mac OS X doesn't support setscheduler() commands
  int policy;
  struct sched_param params;

  /* First, tell the scheduler that we want maximum priority! */
  //  policy = SCHED_RR;
  policy = SCHED_FIFO;
  params.sched_priority = sched_get_priority_max(policy);

  if (sched_setscheduler(0,policy,&params) == -1) { // pid 0 means this process
    switch(errno) {
    case EPERM:
      PyErr_SetString(PyExc_RuntimeError,"sched_setscheduler returned EPERM (Are you superuser?)");
      return NULL;
      break;
    case ESRCH:
      PyErr_SetString(PyExc_RuntimeError,"sched_setscheduler returned ESRCH");
      return NULL;
      break;
    case EINVAL:
      PyErr_SetString(PyExc_RuntimeError,"sched_setscheduler returned EINVAL");
      return NULL;
      break;
    default:
      PyErr_SetString(PyExc_RuntimeError,"sched_setscheduler returned unknown error");
      return NULL;
      break;
    }
  }
  
#ifdef MCL_CURRENT
#ifdef MCL_FUTURE
  /* Second, prevent the memory from paging. */
  if (mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
    switch(errno) {
    case EPERM:
      PyErr_SetString(PyExc_RuntimeError,"mlockall returned EPERM (Are you superuser?)");
      return NULL;
      break;
    case ESRCH:
      PyErr_SetString(PyExc_RuntimeError,"mlockall returned ESRCH");
      return NULL;
      break;
    case EINVAL:
      PyErr_SetString(PyExc_RuntimeError,"mlockall returned EINVAL");
      return NULL;
      break;
    default:
      PyErr_SetString(PyExc_RuntimeError,"mlockall returned unknown error");
      return NULL;
      break;
    }
  }
#endif /* closes ifdef MCL_FUTURE */
#endif /* closes ifdef MCL_CURRENT */

#endif /* closes ifndef __APPLE_CC__ */

  Py_INCREF(Py_None);
  return Py_None;  /* It worked OK. */
}

#ifdef XXX_LPT_DOUT
static int lpt_works; // define a couple of global variables
static int lpt_state;
#endif

static PyObject *_visionegg_toggleDOut(PyObject * self, PyObject * args)
{
#ifdef XXX_LPT_DOUT
{'fraction_non_alphanumeric': 0.11403188511622246, 'fraction_numerical': 0.009979311184130462, 'mean_word_length': 3.6746302616609783, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '717326', 'n_tokens_mistral': 2978, 'n_tokens_neox': 2908, 'n_words': 748}