Exemplo n.º 1
0
/* 
 * _hostrange_output
 *
 * Output metric data in hostrange format.  The algorithm involves
 * using the metric_value as a hash key.  Each hash item will then
 * store the hosts with the same metric_value/key.
 */
static void
_hostrange_output(List l)
{
#if CEREBRO_DEBUG
  const char *func = __FUNCTION__;
#endif /* CEREBRO_DEBUG */
  struct node_metric_data *data = NULL;
  ListIterator litr = NULL;
  unsigned int count;
  hash_t h;

  assert(l);

  count = List_count(l);

#if CEREBRO_DEBUG
  if (!count)
    err_exit("%s: invalid count", func);
#endif /* CEREBRO_DEBUG */

  h = Hash_create(count, 
                  (hash_key_f)hash_key_string,
                  (hash_cmp_f)strcmp,
                  (hash_del_f)_hostrange_data_destroy);

  litr = List_iterator_create(l);

  while ((data = list_next(litr)))
    {
      char buf[CEREBRO_STAT_BUFLEN];
      struct hostrange_data *hd;

      _metric_value_str(data->metric_value_type,
                        data->metric_value_len,
                        data->metric_value, 
                        buf, 
                        CEREBRO_STAT_BUFLEN);

      if (!(hd = Hash_find(h, buf)))
        {
          hd = Malloc(sizeof(struct hostrange_data));
          hd->hl = Hostlist_create(NULL);
          hd->key = Strdup(buf);

          Hash_insert(h, hd->key, hd);
        }

      Hostlist_push(hd->hl, data->nodename);
    }

  Hash_for_each(h, _hostrange_output_data, NULL);

  /* No need to destroy list iterator, caller will destroy List */
  Hash_destroy(h);
}
Exemplo n.º 2
0
/*
 * _setup_event_node_timeout_data
 *
 * Setup event timeout calculation data structures. 
 *
 * Return 0 on success, -1 on error
 */
static int
_setup_event_node_timeout_data(void)
{
  struct timeval tv;
  int num;

  assert(conf.event_server);
  assert(listener_data);

  event_node_timeout_data = List_create(NULL);
  event_node_timeout_data_index = Hash_create(EVENT_NODE_TIMEOUT_SIZE_DEFAULT, 
                                              (hash_key_f)hash_key_string,
                                              (hash_cmp_f)strcmp, 
                                              (hash_del_f)NULL);
  event_node_timeout_data_index_size = EVENT_NODE_TIMEOUT_SIZE_DEFAULT;
  
  Gettimeofday(&tv, NULL);

  num = Hash_for_each(listener_data,
                      _event_node_timeout_data_callback,
                      &(tv.tv_sec));
  if (num != listener_data_numnodes)
    {
      fprintf(stderr, "* invalid create count: num=%d numnodes=%d\n",
              num, listener_data_numnodes);
      goto cleanup;
    }

  return 0;

 cleanup:
  if (event_node_timeout_data)
    {
      List_destroy(event_node_timeout_data);
      event_node_timeout_data = NULL;
    }
  if (event_node_timeout_data_index)
    {
      Hash_destroy(event_node_timeout_data_index);
      event_node_timeout_data_index =  NULL;
    }
  return -1;
}
Exemplo n.º 3
0
void
cerebrod_rehash(hash_t *old_hash,
                int *hash_size,
                int hash_size_increment,
                int hash_num,
                pthread_mutex_t *hash_mutex)
{
    hash_t new_hash;

    assert(old_hash && hash_size && hash_size_increment && hash_num);

#if CEREBRO_DEBUG
    /* Should be called with lock already set */
    if (hash_mutex)
    {
        int rv = Pthread_mutex_trylock(hash_mutex);
        if (rv != EBUSY)
            CEREBROD_EXIT(("mutex not locked: rv=%d", rv));
    }
#endif /* CEREBRO_DEBUG */

    *hash_size += hash_size_increment;

    new_hash = Hash_create(*hash_size,
                           (hash_key_f)hash_key_string,
                           (hash_cmp_f)strcmp,
                           (hash_del_f)_Free);

    if (Hash_for_each(*old_hash, _hash_reinsert, &new_hash) != hash_num)
        CEREBROD_EXIT(("invalid reinsert: hash_num=%d", hash_num));

    if (Hash_remove_if(*old_hash, _hash_removeall, NULL) != hash_num)
        CEREBROD_EXIT(("invalid removeall: hash_num=%d", hash_num));

    Hash_destroy(*old_hash);

    *old_hash = new_hash;
}