Exemple #1
0
Fichier : util.c Projet : xiph/flac
double
benchmark_function (void (*testfunc) (void), unsigned count)
{
	uint64_t start, end;
	unsigned k;

	start = mach_absolute_time();

	for (k = 0 ; k < count ; k++)
		testfunc();

	end = mach_absolute_time();

	return counter_diff (&start, &end) / count ;
} /* benchmark_function */
Exemple #2
0
Fichier : util.c Projet : xiph/flac
double
benchmark_function (void (*testfunc) (void), unsigned count)
{
	LARGE_INTEGER start, end;
	unsigned k;

	QueryPerformanceCounter (&start) ;

	for (k = 0 ; k < count ; k++)
		testfunc();

	QueryPerformanceCounter (&end) ;

	return counter_diff (&start, &end) / count ;
} /* benchmark_function */
Exemple #3
0
int uc_update (const data_set_t *ds, const value_list_t *vl)
{
    char name[6 * DATA_MAX_NAME_LEN];
    cache_entry_t *ce = NULL;
    int status;
    size_t i;

    if (FORMAT_VL (name, sizeof (name), vl) != 0)
    {
        ERROR ("uc_update: FORMAT_VL failed.");
        return (-1);
    }

    pthread_mutex_lock (&cache_lock);

    status = c_avl_get (cache_tree, name, (void *) &ce);
    if (status != 0) /* entry does not yet exist */
    {
        status = uc_insert (ds, vl, name);
        pthread_mutex_unlock (&cache_lock);
        return (status);
    }

    assert (ce != NULL);
    assert (ce->values_num == ds->ds_num);

    if (ce->last_time >= vl->time)
    {
        pthread_mutex_unlock (&cache_lock);
        NOTICE ("uc_update: Value too old: name = %s; value time = %.3f; "
                "last cache update = %.3f;",
                name,
                CDTIME_T_TO_DOUBLE (vl->time),
                CDTIME_T_TO_DOUBLE (ce->last_time));
        return (-1);
    }

    for (i = 0; i < ds->ds_num; i++)
    {
        switch (ds->ds[i].type)
        {
        case DS_TYPE_COUNTER:
        {
            counter_t diff = counter_diff (ce->values_raw[i].counter, vl->values[i].counter);
            ce->values_gauge[i] = ((double) diff)
                                  / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
            ce->values_raw[i].counter = vl->values[i].counter;
        }
        break;

        case DS_TYPE_GAUGE:
            ce->values_raw[i].gauge = vl->values[i].gauge;
            ce->values_gauge[i] = vl->values[i].gauge;
            break;

        case DS_TYPE_DERIVE:
        {
            derive_t diff = vl->values[i].derive - ce->values_raw[i].derive;

            ce->values_gauge[i] = ((double) diff)
                                  / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
            ce->values_raw[i].derive = vl->values[i].derive;
        }
        break;

        case DS_TYPE_ABSOLUTE:
            ce->values_gauge[i] = ((double) vl->values[i].absolute)
                                  / (CDTIME_T_TO_DOUBLE (vl->time - ce->last_time));
            ce->values_raw[i].absolute = vl->values[i].absolute;
            break;

        default:
            /* This shouldn't happen. */
            pthread_mutex_unlock (&cache_lock);
            ERROR ("uc_update: Don't know how to handle data source type %i.",
                   ds->ds[i].type);
            return (-1);
        } /* switch (ds->ds[i].type) */

        DEBUG ("uc_update: %s: ds[%zu] = %lf", name, i, ce->values_gauge[i]);
    } /* for (i) */

    /* Update the history if it exists. */
    if (ce->history != NULL)
    {
        assert (ce->history_index < ce->history_length);
        for (i = 0; i < ce->values_num; i++)
        {
            size_t hist_idx = (ce->values_num * ce->history_index) + i;
            ce->history[hist_idx] = ce->values_gauge[i];
        }

        assert (ce->history_length > 0);
        ce->history_index = (ce->history_index + 1) % ce->history_length;
    }

    /* Prune invalid gauge data */
    uc_check_range (ds, ce);

    ce->last_time = vl->time;
    ce->last_update = cdtime ();
    ce->interval = vl->interval;

    pthread_mutex_unlock (&cache_lock);

    return (0);
} /* int uc_update */
Exemple #4
0
static int ts_invoke_counter(const data_set_t *ds, value_list_t *vl, /* {{{ */
                             ts_data_t *data, int dsrc_index) {
  uint64_t curr_counter;
  int status;
  int failure;

  /* Required meta data */
  uint64_t prev_counter;
  char key_prev_counter[128];
  uint64_t int_counter;
  char key_int_counter[128];
  double int_fraction;
  char key_int_fraction[128];

  curr_counter = (uint64_t)vl->values[dsrc_index].counter;

  snprintf(key_prev_counter, sizeof(key_prev_counter),
           "target_scale[%p,%i]:prev_counter", (void *)data, dsrc_index);
  snprintf(key_int_counter, sizeof(key_int_counter),
           "target_scale[%p,%i]:int_counter", (void *)data, dsrc_index);
  snprintf(key_int_fraction, sizeof(key_int_fraction),
           "target_scale[%p,%i]:int_fraction", (void *)data, dsrc_index);

  prev_counter = curr_counter;
  int_counter = 0;
  int_fraction = 0.0;

  /* Query the meta data */
  failure = 0;

  status = uc_meta_data_get_unsigned_int(vl, key_prev_counter, &prev_counter);
  if (status != 0)
    failure++;

  status = uc_meta_data_get_unsigned_int(vl, key_int_counter, &int_counter);
  if (status != 0)
    failure++;

  status = uc_meta_data_get_double(vl, key_int_fraction, &int_fraction);
  if (status != 0)
    failure++;

  if (failure == 0) {
    uint64_t diff;
    double rate;

    diff = (uint64_t)counter_diff(prev_counter, curr_counter);
    rate = ((double)diff) / CDTIME_T_TO_DOUBLE(vl->interval);

    /* Modify the rate. */
    if (!isnan(data->factor))
      rate *= data->factor;
    if (!isnan(data->offset))
      rate += data->offset;

    /* Calculate the internal counter. */
    int_fraction += (rate * CDTIME_T_TO_DOUBLE(vl->interval));
    diff = (uint64_t)int_fraction;
    int_fraction -= ((double)diff);
    int_counter += diff;

    assert(int_fraction >= 0.0);
    assert(int_fraction < 1.0);

    DEBUG("Target `scale': ts_invoke_counter: %" PRIu64 " -> %g -> %" PRIu64
          "(+%g)",
          curr_counter, rate, int_counter, int_fraction);
  } else /* (failure != 0) */
  {
    int_counter = 0;
    int_fraction = 0.0;
  }

  vl->values[dsrc_index].counter = (counter_t)int_counter;

  /* Update to the new counter value */
  uc_meta_data_add_unsigned_int(vl, key_prev_counter, curr_counter);
  uc_meta_data_add_unsigned_int(vl, key_int_counter, int_counter);
  uc_meta_data_add_double(vl, key_int_fraction, int_fraction);

  return 0;
} /* }}} int ts_invoke_counter */