<commit_before>/* This file is part of UProf.
 *
 * Copyright © 2006 OpenedHand
 * Copyright © 2008, 2009, 2010 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301  USA
 */

#define USE_RDTSC

#if !defined(__i386__) && !defined(__x86_64__)
#undef USE_RDTSC
#endif
//#undef USE_RDTSC

#include <uprof.h>

#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <glib.h>
#include <errno.h>
#include <string.h>
#include <glib/gprintf.h>
#ifndef USE_RDTSC
#include <time.h>
#endif
#include <unistd.h>

/* #define UPROF_DEBUG     1 */

#ifdef UPROF_DEBUG
#define DBG_PRINTF(fmt, args...)              \
  do                                          \
    {                                         \
      printf ("[%s] " fmt, __FILE__, ##args); \
    }                                         \
  while (0)
#else
#define DBG_PRINTF(fmt, args...) do { } while (0)
#endif
#define REPORT_COLUMN0_WIDTH 40

#define UPROF_OBJECT_STATE(X) ((UProfObjectState *)X)

typedef void (*UProfContextCallback) (UProfContext *context,
                                      gpointer user_data);

typedef struct _UProfPrintReportRecord
{
  int    height;
  GList *entries;
  void  *data; /* timer / counter */
} UProfPrintReportRecord;

#if 0
typedef enum
{
  UPROF_ALIGNMENT_LEFT,
  UPROF_ALIGNMENT_RIGHT,
  UPROF_ALIGNMENT_CENTER
} UProfAlignment;
#endif

typedef struct _UProfPrintReportEntry
{
  int            width;
  int            height;
#if 0
  UProfAlignment alignment;
#endif
  gboolean       is_percentage;
  float          percentage;
  char         **lines;
} UProfPrintReportEntry;

typedef struct _UProfTimerAttribute
{
  char *name;
  UProfReportTimersAttributeCallback callback;
  void *user_data;
} UProfTimerAttribute;

typedef struct _UProfCounterAttribute
{
  char *name;
  UProfReportCountersAttributeCallback callback;
  void *user_data;
} UProfCounterAttribute;

struct _UProfContext
{
  guint  ref;

  char	*name;

  GList *links;

  GList	*counters;
  GList	*timers;

  int disabled;

  gboolean resolved;
  GList *root_timers;

  GList *report_messages;
};

typedef struct _UProfObjectLocation
{
  char  *filename;
  long   line;
  char  *function;
} UProfObjectLocation;

struct _UProfReport
{
  int ref;
  GList *contexts;
  GList *context_callbacks;

  GList *timer_attributes;
  GList *counter_attributes;

  int max_timer_name_size;
};

typedef struct _RDTSCVal
{
  union {
      struct {
          uint32_t low;
          uint32_t hi;
      } split;
      guint64 full_value;
  } u;
} RDTSCVal;

static guint64 system_counter_hz;
static GList *_uprof_all_contexts;
#ifndef USE_RDTSC
static clockid_t clockid;
#endif

UProfContext *mainloop_context = NULL;

void
uprof_init_real (void)
{
#ifndef USE_RDTSC
  int ret;
  struct timespec ts;

  ret = clock_getcpuclockid(0, &clockid);
  if (ret == ENOENT)
    {
      g_warning ("Using the CPU clock will be unreliable on this system if "
                 "you don't assure processor affinity");
    }
  else if (ret != 0)
    {
      const char *str = strerror (errno);
      g_warning ("Failed to get CPU clock ID: %s", str);
    }

  if (clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts) == -1)
    {
      const char *str = strerror (errno);
      g_warning ("Failed to query CLOCK_PROCESS_CPUTIME_ID clock: %s", str);
    }
#endif

  mainloop_context = uprof_context_new ("Mainloop context");<commit_msg>Make repeat calls to uprof_init a NOP<commit_after>/* This file is part of UProf.
 *
 * Copyright © 2006 OpenedHand
 * Copyright © 2008, 2009, 2010 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301  USA
 */

#define USE_RDTSC

#if !defined(__i386__) && !defined(__x86_64__)
#undef USE_RDTSC
#endif
//#undef USE_RDTSC

#include <uprof.h>

#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <glib.h>
#include <errno.h>
#include <string.h>
#include <glib/gprintf.h>
#ifndef USE_RDTSC
#include <time.h>
#endif
#include <unistd.h>

/* #define UPROF_DEBUG     1 */

#ifdef UPROF_DEBUG
#define DBG_PRINTF(fmt, args...)              \
  do                                          \
    {                                         \
      printf ("[%s] " fmt, __FILE__, ##args); \
    }                                         \
  while (0)
#else
#define DBG_PRINTF(fmt, args...) do { } while (0)
#endif
#define REPORT_COLUMN0_WIDTH 40

#define UPROF_OBJECT_STATE(X) ((UProfObjectState *)X)

typedef void (*UProfContextCallback) (UProfContext *context,
                                      gpointer user_data);

typedef struct _UProfPrintReportRecord
{
  int    height;
  GList *entries;
  void  *data; /* timer / counter */
} UProfPrintReportRecord;

#if 0
typedef enum
{
  UPROF_ALIGNMENT_LEFT,
  UPROF_ALIGNMENT_RIGHT,
  UPROF_ALIGNMENT_CENTER
} UProfAlignment;
#endif

typedef struct _UProfPrintReportEntry
{
  int            width;
  int            height;
#if 0
  UProfAlignment alignment;
#endif
  gboolean       is_percentage;
  float          percentage;
  char         **lines;
} UProfPrintReportEntry;

typedef struct _UProfTimerAttribute
{
  char *name;
  UProfReportTimersAttributeCallback callback;
  void *user_data;
} UProfTimerAttribute;

typedef struct _UProfCounterAttribute
{
  char *name;
  UProfReportCountersAttributeCallback callback;
  void *user_data;
} UProfCounterAttribute;

struct _UProfContext
{
  guint  ref;

  char	*name;

  GList *links;

  GList	*counters;
  GList	*timers;

  int disabled;

  gboolean resolved;
  GList *root_timers;

  GList *report_messages;
};

typedef struct _UProfObjectLocation
{
  char  *filename;
  long   line;
  char  *function;
} UProfObjectLocation;

struct _UProfReport
{
  int ref;
  GList *contexts;
  GList *context_callbacks;

  GList *timer_attributes;
  GList *counter_attributes;

  int max_timer_name_size;
};

typedef struct _RDTSCVal
{
  union {
      struct {
          uint32_t low;
          uint32_t hi;
      } split;
      guint64 full_value;
  } u;
} RDTSCVal;

static guint64 system_counter_hz;
static GList *_uprof_all_contexts;
#ifndef USE_RDTSC
static clockid_t clockid;
#endif

UProfContext *mainloop_context = NULL;

void
uprof_init_real (void)
{
  static gboolean initialized = FALSE;

  if (initialized)
    return;

#ifndef USE_RDTSC
  int ret;
  struct timespec ts;

  ret = clock_getcpuclockid(0, &clockid);
  if (ret == ENOENT)
    {
      g_warning ("Using the CPU clock will be unreliable on this system if "
                 "you don't assure processor affinity");
    }
  else if (ret != 0)
    {
      const char *str = strerror (errno);
      g_warning ("Failed to get CPU clock ID: %s", str);
    }

  if (clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts) == -1)
    {
      const char *str = strerror (errno);
      g_warning ("Failed to query CLOCK_PROCESS_CPUTIME_ID clock: %s", str);
    }
#endif

  mainloop_context = uprof_context_new ("Mainloop context");
  initialized = TRUE;
{'fraction_non_alphanumeric': 0.08803584403003148, 'fraction_numerical': 0.01332041656575442, 'mean_word_length': 2.9783236994219653, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2302518', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2675, 'n_words': 836}