<commit_before>/*
* File:    perf_events.c
*
* Author:  <NAME>
*          <EMAIL>
*          - based upon perfmon.c written by -
*          <NAME>
*          <EMAIL>
* Mods:    <NAME>
*          <EMAIL>
* Mods:    <NAME>
*          <EMAIL>
* Mods:	   <NAME>
*	   <EMAIL> */


#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <syscall.h>
#include <sys/utsname.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/ioctl.h>

/* PAPI-specific includes */
#include "papi.h"
#include "papi_memory.h"
#include "papi_internal.h"
#include "papi_vector.h"
#include "extras.h"

/* libpfm4 includes */
#include "papi_libpfm4_events.h"
#include "perfmon/pfmlib.h"
#include PEINCLUDE

/* Linux-specific includes */
#include "mb.h"
#include "syscalls.h"
#include "linux-memory.h"
#include "linux-timer.h"
#include "linux-common.h"
#include "linux-context.h"

/* Various definitions */

/* This is arbitrary.  Typically you can add up to ~1000 before */
/* you run out of fds                                           */
#define PERF_EVENT_MAX_MPX_COUNTERS 64

typedef int pfm_register_t;
typedef int reg_alloc_t;            

typedef struct 
{
  unsigned char wakeup_mode;
} per_event_info_t;

typedef struct
{
  int num_events;
  int num_groups;
  unsigned int domain;
  unsigned int multiplexed;
  unsigned int overflow;
  unsigned int inherit;
  int cpu;
  pid_t tid;
  struct perf_event_attr events[PERF_EVENT_MAX_MPX_COUNTERS];
  per_event_info_t per_event_info[PERF_EVENT_MAX_MPX_COUNTERS];
  /* Buffer to gather counters */
  long long counts[PERF_EVENT_MAX_MPX_COUNTERS];
} pe_control_state_t;

typedef struct
{
  int group_leader;		  /* index of leader */
  int event_fd;
  uint64_t event_id;
  uint32_t nr_mmap_pages;	  /* number pages in the mmap buffer */
  void *mmap_buf;		  /* used to contain profiling data samples */
                                  /* as well as control */
  uint64_t tail;		  /* current read location in mmap buffer */
  uint64_t mask;		  /* mask used for wrapping the pages */
} evt_t;

typedef struct
{
  /* Array of event fds, event group leader is event_fd[0] */
  int cookie;
  int state;
  int num_evts;
  evt_t evt[PERF_EVENT_MAX_MPX_COUNTERS];
} pe_context_t;


#if defined(__mips__)
#warning "This platform has substandard kernel multiplexing support in perf_events: enabling BRAINDEAD_MULTIPLEXING"
#define BRAINDEAD_MULTIPLEXING 1
#endif

/* These sentinels tell papi_pe_set_overflow() how to set the
 * wakeup_events field in the event descriptor record.
 */
#define WAKEUP_COUNTER_OVERFLOW 0
#define WAKEUP_PROFILING -1

#define WAKEUP_MODE_COUNTER_OVERFLOW 0
#define WAKEUP_MODE_PROFILING 1

/* just an unlikely magic cookie */
#define CTX_INITIALIZED 0xdc1dc1

#define PERF_EVENTS_RUNNING 0x01

/* Static globals */

static int nmi_watchdog_active;

/* Advance declaration */
papi_vector_t _papi_pe_vector;

/* FIXME */
/* What the heck are these? -pjm */
/* event count, time_enabled, time_running, (count value, count id) * MAX_COUNTERS */
#define READ_BUFFER_SIZE (1 + 1 + 1 + 2 * PERF_EVENT_MAX_MPX_COUNTERS)
#define MAX_READ 8192


/******** Kernel Version Dependent Routines  **********************/

/* KERNEL_CHECKS_SCHEDUABILITY_UPON_OPEN is a work-around for kernel arch
 * implementations (e.g. x86) which don't do a static event scheduability
 * check in sys_perf_event_open.  
 * This was fixed for x86 in the 2.6.33 kernel
 *
 * Also! Kernels newer than 2.6.34 will fail in a similar way
 *       if the nmi_watchdog has stolen a performance counter<commit_msg>perf_events: MAX_READ was no longer being used, remove it<commit_after>/*
* File:    perf_events.c
*
* Author:  <NAME>
*          <EMAIL>
*          - based upon perfmon.c written by -
*          <NAME>
*          <EMAIL>
* Mods:    <NAME>
*          <EMAIL>
* Mods:    <NAME>
*          <EMAIL>
* Mods:	   <NAME>
*	   <EMAIL> */


#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <syscall.h>
#include <sys/utsname.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/ioctl.h>

/* PAPI-specific includes */
#include "papi.h"
#include "papi_memory.h"
#include "papi_internal.h"
#include "papi_vector.h"
#include "extras.h"

/* libpfm4 includes */
#include "papi_libpfm4_events.h"
#include "perfmon/pfmlib.h"
#include PEINCLUDE

/* Linux-specific includes */
#include "mb.h"
#include "syscalls.h"
#include "linux-memory.h"
#include "linux-timer.h"
#include "linux-common.h"
#include "linux-context.h"

/* Various definitions */

/* This is arbitrary.  Typically you can add up to ~1000 before */
/* you run out of fds                                           */
#define PERF_EVENT_MAX_MPX_COUNTERS 64

typedef int pfm_register_t;
typedef int reg_alloc_t;            

typedef struct 
{
  unsigned char wakeup_mode;
} per_event_info_t;

typedef struct
{
  int num_events;
  int num_groups;
  unsigned int domain;
  unsigned int multiplexed;
  unsigned int overflow;
  unsigned int inherit;
  int cpu;
  pid_t tid;
  struct perf_event_attr events[PERF_EVENT_MAX_MPX_COUNTERS];
  per_event_info_t per_event_info[PERF_EVENT_MAX_MPX_COUNTERS];
  /* Buffer to gather counters */
  long long counts[PERF_EVENT_MAX_MPX_COUNTERS];
} pe_control_state_t;

typedef struct
{
  int group_leader;		  /* index of leader */
  int event_fd;
  uint64_t event_id;
  uint32_t nr_mmap_pages;	  /* number pages in the mmap buffer */
  void *mmap_buf;		  /* used to contain profiling data samples */
                                  /* as well as control */
  uint64_t tail;		  /* current read location in mmap buffer */
  uint64_t mask;		  /* mask used for wrapping the pages */
} evt_t;

typedef struct
{
  /* Array of event fds, event group leader is event_fd[0] */
  int cookie;
  int state;
  int num_evts;
  evt_t evt[PERF_EVENT_MAX_MPX_COUNTERS];
} pe_context_t;


#if defined(__mips__)
#warning "This platform has substandard kernel multiplexing support in perf_events: enabling BRAINDEAD_MULTIPLEXING"
#define BRAINDEAD_MULTIPLEXING 1
#endif

/* These sentinels tell papi_pe_set_overflow() how to set the
 * wakeup_events field in the event descriptor record.
 */
#define WAKEUP_COUNTER_OVERFLOW 0
#define WAKEUP_PROFILING -1

#define WAKEUP_MODE_COUNTER_OVERFLOW 0
#define WAKEUP_MODE_PROFILING 1

/* just an unlikely magic cookie */
#define CTX_INITIALIZED 0xdc1dc1

#define PERF_EVENTS_RUNNING 0x01

/* Static globals */

static int nmi_watchdog_active;

/* Advance declaration */
papi_vector_t _papi_pe_vector;

/* FIXME */
/* What the heck are these? -pjm */
/* event count, time_enabled, time_running, (count value, count id) * MAX_COUNTERS */
#define READ_BUFFER_SIZE (1 + 1 + 1 + 2 * PERF_EVENT_MAX_MPX_COUNTERS)


/******** Kernel Version Dependent Routines  **********************/

/* KERNEL_CHECKS_SCHEDUABILITY_UPON_OPEN is a work-around for kernel arch
 * implementations (e.g. x86) which don't do a static event scheduability
 * check in sys_perf_event_open.  
 * This was fixed for x86 in the 2.6.33 kernel
 *
 * Also! Kernels newer than 2.6.34 will fail in a similar way
 *       if the nmi_watchdog has stolen a performance counter
{'fraction_non_alphanumeric': 0.10549173437937798, 'fraction_numerical': 0.01288876435976464, 'mean_word_length': 3.4730576441102756, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2310701', 'n_tokens_mistral': 2680, 'n_tokens_neox': 2554, 'n_words': 710}