<commit_before>/* libunwind - a platform-independent unwind library
   Copyright (C) 2010 by <NAME> <<EMAIL>>

This file is part of libunwind.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#include "unwind_i.h"
#include "ucontext_i.h"
#include <signal.h>

#pragma weak pthread_once
#pragma weak pthread_key_create
#pragma weak pthread_getspecific
#pragma weak pthread_setspecific

/* Total maxium hash table size is 16M addresses. HASH_TOP_BITS must
   be a power of four, as the hash expands by four each time. */
#define HASH_LOW_BITS 14
#define HASH_TOP_BITS 10

/* There's not enough space to store RIP's location in a signal
   frame, but we can calculate it relative to RBP's (or RSP's)
   position in mcontext structure.  Note we don't want to use
   the UC_MCONTEXT_GREGS_* directly since we rely on DWARF info. */
#define dRIP (UC_MCONTEXT_GREGS_RIP - UC_MCONTEXT_GREGS_RBP)

typedef struct
{
  unw_tdep_frame_t *frames[1u << HASH_TOP_BITS];
  size_t log_frame_vecs;
  size_t used;
} unw_trace_cache_t;

static const unw_tdep_frame_t empty_frame = { 0, UNW_X86_64_FRAME_OTHER, -1, -1, 0, -1, -1 };
static pthread_mutex_t trace_init_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_once_t trace_cache_once = PTHREAD_ONCE_INIT;
static pthread_key_t trace_cache_key;
static struct mempool trace_cache_pool;
static struct mempool trace_frame_pool;

/* Initialise memory pools for frame tracing. */
static void
trace_pools_init (void)
{
  mempool_init (&trace_cache_pool, sizeof (unw_trace_cache_t), 0);
  mempool_init (&trace_frame_pool, (1u << HASH_LOW_BITS) * sizeof (unw_tdep_frame_t), 0);
}

/* Free memory for a thread's trace cache. */
static void
trace_cache_free (void *arg)
{
  unw_trace_cache_t *cache = arg;
  size_t i;

  for (i = 0; i < (1u << cache->log_frame_vecs); ++i)
    mempool_free (&trace_frame_pool, cache->frames[i]);

  mempool_free (&trace_cache_pool, cache);
  Debug(5, "freed cache %p\n", cache);
}

/* Initialise frame tracing for threaded use. */
static void
trace_cache_init_once (void)
{
  pthread_key_create (&trace_cache_key, &trace_cache_free);
  trace_pools_init ();
}

static unw_tdep_frame_t *
trace_cache_buckets (void)
{
  unw_tdep_frame_t *frames = mempool_alloc(&trace_frame_pool);
  size_t i;

  if (likely (frames != 0))
    for (i = 0; i < (1u << HASH_LOW_BITS); ++i)
      frames[i] = empty_frame;

  return frames;
}

/* Allocate and initialise hash table for frame cache lookups.
   Returns the cache initialised with (1u << HASH_LOW_BITS) hash
   buckets, or NULL if there was a memory allocation problem. */
static unw_trace_cache_t *
trace_cache_create (void)
{
  unw_trace_cache_t *cache;

  if (! (cache = mempool_alloc(&trace_cache_pool)))
  {
    Debug(5, "failed to allocate cache\n");
    return 0;
  }

  memset(cache, 0, sizeof (*cache));
  if (! (cache->frames[0] = trace_cache_buckets()))
  {
    Debug(5, "failed to allocate buckets\n");
    mempool_free(&trace_cache_pool, cache);
    return 0;
  }

  cache->log_frame_vecs = 0;
  cache->used = 0;
  Debug(5, "allocated cache %p\n", cache);
  return cache;
}

/* Expand the hash table in the frame cache if possible. This always
   quadruples the hash size, and clears all previous frame entries. */
static int
trace_cache_expand (unw_trace_cache_t *cache)
{
  size_t i, j, new_size, old_size;
  if (cache->log_frame_vecs == HASH_TOP_BITS)
  {
    Debug(5, "cache already at maximum size, cannot expand\n");
    return -UNW_ENOMEM;
  }

  old_size = (1u << cache->log_frame_vecs);
  new_size = cache->log_frame_vecs + 2;
  for (i = old_size; i < (1u << new_size); ++i)
    if (unlikely (! (cache->frames[i] = trace_cache_buckets())))
    {
      Debug(5, "failed to expand cache to 2^%lu hash bucket sets\n", new_size);
      for (j = old_size; j < i; ++j)
	mempool_free(&trace_frame_pool, cache->frames[j]);
      return -UNW_ENOMEM;
    }

  for (i = 0; i < old_size; ++i)
    for (j = 0; j < (1u << HASH_LOW_BITS); ++j)
      cache->frames[i][j] = empty_frame;

  Debug(5, "expanded cache from 2^%lu to 2^%lu hash bucket sets\n",
	cache->log_frame_vecs, new_size);
  cache->log_frame_vecs = new_size;
  return 0;
}

/* Get the frame cache for the current thread. Create it if there is none. */
static unw_trace_cache_t *
trace_cache_get (void)
{
  unw_trace_cache_t *cache;
  if (pthread_once)
  {
    pthread_once(&trace_cache_once, &trace_cache_init_once);
    if (! (cache = pthread_getspecific(trace_cache_key)))
    {
      cache = trace_cache_create();
      pthread_setspecific(trace_cache_key, cache);
    }
    Debug(5, "using cache %p\n", cache);
    return cache;
  }
  else
  {
    intrmask_t saved_mask;
    static unw_trace_cache_t *global_cache = 0;<commit_msg>Reset 'used' to zero after expanding frame cache hash table.<commit_after>/* libunwind - a platform-independent unwind library
   Copyright (C) 2010 by <NAME> <<EMAIL>>

This file is part of libunwind.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#include "unwind_i.h"
#include "ucontext_i.h"
#include <signal.h>

#pragma weak pthread_once
#pragma weak pthread_key_create
#pragma weak pthread_getspecific
#pragma weak pthread_setspecific

/* Total maxium hash table size is 16M addresses. HASH_TOP_BITS must
   be a power of four, as the hash expands by four each time. */
#define HASH_LOW_BITS 14
#define HASH_TOP_BITS 10

/* There's not enough space to store RIP's location in a signal
   frame, but we can calculate it relative to RBP's (or RSP's)
   position in mcontext structure.  Note we don't want to use
   the UC_MCONTEXT_GREGS_* directly since we rely on DWARF info. */
#define dRIP (UC_MCONTEXT_GREGS_RIP - UC_MCONTEXT_GREGS_RBP)

typedef struct
{
  unw_tdep_frame_t *frames[1u << HASH_TOP_BITS];
  size_t log_frame_vecs;
  size_t used;
} unw_trace_cache_t;

static const unw_tdep_frame_t empty_frame = { 0, UNW_X86_64_FRAME_OTHER, -1, -1, 0, -1, -1 };
static pthread_mutex_t trace_init_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_once_t trace_cache_once = PTHREAD_ONCE_INIT;
static pthread_key_t trace_cache_key;
static struct mempool trace_cache_pool;
static struct mempool trace_frame_pool;

/* Initialise memory pools for frame tracing. */
static void
trace_pools_init (void)
{
  mempool_init (&trace_cache_pool, sizeof (unw_trace_cache_t), 0);
  mempool_init (&trace_frame_pool, (1u << HASH_LOW_BITS) * sizeof (unw_tdep_frame_t), 0);
}

/* Free memory for a thread's trace cache. */
static void
trace_cache_free (void *arg)
{
  unw_trace_cache_t *cache = arg;
  size_t i;

  for (i = 0; i < (1u << cache->log_frame_vecs); ++i)
    mempool_free (&trace_frame_pool, cache->frames[i]);

  mempool_free (&trace_cache_pool, cache);
  Debug(5, "freed cache %p\n", cache);
}

/* Initialise frame tracing for threaded use. */
static void
trace_cache_init_once (void)
{
  pthread_key_create (&trace_cache_key, &trace_cache_free);
  trace_pools_init ();
}

static unw_tdep_frame_t *
trace_cache_buckets (void)
{
  unw_tdep_frame_t *frames = mempool_alloc(&trace_frame_pool);
  size_t i;

  if (likely (frames != 0))
    for (i = 0; i < (1u << HASH_LOW_BITS); ++i)
      frames[i] = empty_frame;

  return frames;
}

/* Allocate and initialise hash table for frame cache lookups.
   Returns the cache initialised with (1u << HASH_LOW_BITS) hash
   buckets, or NULL if there was a memory allocation problem. */
static unw_trace_cache_t *
trace_cache_create (void)
{
  unw_trace_cache_t *cache;

  if (! (cache = mempool_alloc(&trace_cache_pool)))
  {
    Debug(5, "failed to allocate cache\n");
    return 0;
  }

  memset(cache, 0, sizeof (*cache));
  if (! (cache->frames[0] = trace_cache_buckets()))
  {
    Debug(5, "failed to allocate buckets\n");
    mempool_free(&trace_cache_pool, cache);
    return 0;
  }

  cache->log_frame_vecs = 0;
  cache->used = 0;
  Debug(5, "allocated cache %p\n", cache);
  return cache;
}

/* Expand the hash table in the frame cache if possible. This always
   quadruples the hash size, and clears all previous frame entries. */
static int
trace_cache_expand (unw_trace_cache_t *cache)
{
  size_t i, j, new_size, old_size;
  if (cache->log_frame_vecs == HASH_TOP_BITS)
  {
    Debug(5, "cache already at maximum size, cannot expand\n");
    return -UNW_ENOMEM;
  }

  old_size = (1u << cache->log_frame_vecs);
  new_size = cache->log_frame_vecs + 2;
  for (i = old_size; i < (1u << new_size); ++i)
    if (unlikely (! (cache->frames[i] = trace_cache_buckets())))
    {
      Debug(5, "failed to expand cache to 2^%lu hash bucket sets\n", new_size);
      for (j = old_size; j < i; ++j)
	mempool_free(&trace_frame_pool, cache->frames[j]);
      return -UNW_ENOMEM;
    }

  for (i = 0; i < old_size; ++i)
    for (j = 0; j < (1u << HASH_LOW_BITS); ++j)
      cache->frames[i][j] = empty_frame;

  Debug(5, "expanded cache from 2^%lu to 2^%lu hash bucket sets\n",
	cache->log_frame_vecs, new_size);
  cache->log_frame_vecs = new_size;
  cache->used = 0;
  return 0;
}

/* Get the frame cache for the current thread. Create it if there is none. */
static unw_trace_cache_t *
trace_cache_get (void)
{
  unw_trace_cache_t *cache;
  if (pthread_once)
  {
    pthread_once(&trace_cache_once, &trace_cache_init_once);
    if (! (cache = pthread_getspecific(trace_cache_key)))
    {
      cache = trace_cache_create();
      pthread_setspecific(trace_cache_key, cache);
    }
    Debug(5, "using cache %p\n", cache);
    return cache;
  }
  else
  {
    intrmask_t saved_mask;
    static unw_trace_cache_t *global_cache = 0;
{'fraction_non_alphanumeric': 0.09845965342201995, 'fraction_numerical': 0.009714685804305968, 'mean_word_length': 4.278060046189377, 'pattern_counts': {'":': 0, '<': 55, '<?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': '6570475', 'n_tokens_mistral': 4029, 'n_tokens_neox': 3894, 'n_words': 1284}