示例#1
0
int64_t category_first_allocation_max_throughput(struct histogram *h, int64_t top_resource) {
	/* Automatically labeling for resource is not activated. */
	if(top_resource < 0) {
		return -1;
	}

	int64_t n = histogram_size(h);

	if(n < 1)
		return -1;

	double *keys = histogram_buckets(h);
	double tau_mean;
	double *counts_cdp  = malloc(n*sizeof(double));
	double *times_accum = malloc(n*sizeof(double));

	category_first_allocation_accum_times(h, keys, &tau_mean, counts_cdp, times_accum);

	int64_t a_1 = top_resource;
	int64_t a_m = top_resource;

	double Ta_1 = 0;

	int i;
	for(i = 0; i < n; i++) {
		int64_t a  = keys[i];

		if(a < 1) {
			continue;
		}

		if(a > top_resource) {
			a_1 = top_resource;
			break;
		}

		double Pbef = counts_cdp[i];
		double Paft = 1 - Pbef;

		double numerator   = (Pbef*a_m)/a + Paft;
		double denominator = tau_mean + times_accum[i];

		double  Ta = numerator/denominator;

		if(Ta > Ta_1) {
			Ta_1 = Ta;
			a_1 = a;
		}
	}

	if(a_1 > top_resource) {
		a_1 = top_resource;
	}

	free(counts_cdp);
	free(times_accum);
	free(keys);

	return a_1;
}
示例#2
0
int main(int argc, char **argv) {

	// use bucket size of .5
	struct histogram *h = histogram_create(.5);

	// bucket [3.0, 3.5)
	histogram_insert(h, 3.00);
	histogram_insert(h, 3.14);

	// bucket [21.5, 22.0)
	histogram_insert(h, 21.99);

	// bucket [22.0, 22.5)
	histogram_insert(h, 22.00);
	histogram_insert(h, 22.20);
	histogram_insert(h, 22.49);

	// bucket [22.5, 23.0)
	histogram_insert(h, 22.50);
	histogram_insert(h, 22.99);

	// bucket [-22.0, -21.5)
	histogram_insert(h, -21.51);
	histogram_insert(h, -22.00);

	// bucket [-21.5, -21.0)
	histogram_insert(h, -21.49);
	histogram_insert(h, -21.20);
	histogram_insert(h, -21.01);

	double *buckets = histogram_buckets(h);
	double b        = histogram_bucket_size(h);

	int expected_counts[] = {2, 3, 2, 1, 3, 2};

	int i;
	for(i = 0; i < histogram_size(h); i++) {

		double start = buckets[i];
		int count    = histogram_count(h, start);

		if(expected_counts[i] != count) {
			fprintf(stderr, "Expected a count of %d, got %d.", expected_counts[i], count);
			return -1;
		}

		fprintf(stdout, "[%6.2lf, %6.2f) count: %d\n", start, start + b, histogram_count(h, start));
	}

	fprintf(stdout, "max: %6.2lf\n",    histogram_max_value(h));
	fprintf(stdout, "min: %6.2lf\n",    histogram_min_value(h));
	fprintf(stdout, "mode: %6.2lf\n",   histogram_mode(h));
	fprintf(stdout, "mode count: %d\n", histogram_count(h, histogram_mode(h)));

	free(buckets);
	histogram_delete(h);

	return 0;
}
示例#3
0
double *histogram_buckets(struct histogram *h) {

	int n = histogram_size(h);

	if(n < 1) {
		return NULL;
	}

	double *values = calloc(histogram_size(h), sizeof(double));

	int i = 0;
	uint64_t key;
	struct box_count *box;

	itable_firstkey(h->buckets);
	while(itable_nextkey(h->buckets, &key, (void **) &box)) {
		values[i] = end_of(h, key);
		i++;
	}

	qsort(values, n, sizeof(double), cmp_double);

	return values;
}
示例#4
0
static void category_clear_histogram(struct histogram *h) {
	double *buckets = histogram_buckets(h);

	int i;
	for(i = 0; i < histogram_size(h); i++) {
		double start   = buckets[i];
		void *time_accum = histogram_get_data(h, start);

		if(time_accum) {
			free(time_accum);
		}
	}

	histogram_clear(h);
}
示例#5
0
void category_first_allocation_accum_times(struct histogram *h, double *keys, double *tau_mean, double *counts_cdp, double *times_accum) {

	int n = histogram_size(h);

	double *times_mean = malloc(n*sizeof(double));
	double *counts     = malloc(n*sizeof(double));

	// accumulate counts and compute mean times per bucket...
	int i;
	for(i = 0; i < n; i++) {
		int count          = histogram_count(h, keys[i]);
		double *time_value = (double *) histogram_get_data(h, keys[i]);
		counts[i]          = count;
		times_mean[i]      = (*time_value)/count;
	}
	//... and compute the probability distribution and cumulative
	for(i = 0; i < n; i++) {
		counts_cdp[i]  = (i > 0 ? counts_cdp[i-1] : 0) + counts[i];
	}
	for(i = 0; i < n; i++) {
		counts[i]     /= counts_cdp[n-1];
		counts_cdp[i] /= counts_cdp[n-1];
	}

	// compute proportion of mean time for buckets larger than i, for each i.
	for(i = n-1; i >= 0; i--) {
		// base case
		if(i == n-1) {
			times_accum[i] = 0;
		} else {
			times_accum[i] = times_accum[i+1] + (times_mean[i+1] * counts[i+1]);
		}
	}

	// set overall mean time
	*tau_mean = times_accum[0] + (times_mean[0] * counts[0]);

	free(counts);
	free(times_mean);
}
示例#6
0
int64_t category_first_allocation_min_waste(struct histogram *h, int assume_independence, int64_t top_resource) {
	/* Automatically labeling for resource is not activated. */
	if(top_resource < 0) {
		return -1;
	}

	int64_t n = histogram_size(h);

	if(n < 1)
		return -1;

	double *keys = histogram_buckets(h);
	double tau_mean;
	double *counts_cdp  = malloc(n*sizeof(double));
	double *times_accum = malloc(n*sizeof(double));

	category_first_allocation_accum_times(h, keys, &tau_mean, counts_cdp, times_accum);

	int64_t a_1 = top_resource;
	int64_t a_m = top_resource;

	double Ea_1 = DBL_MAX;

	int i;
	for(i = 0; i < n; i++) {
		int64_t a  = keys[i];
		double  Ea;

		if(a < 1) {
			continue;
		}

		if(a > top_resource) {
			a_1 = top_resource;
			break;
		}

		double Pa = 1 - counts_cdp[i];

		if(assume_independence) {
			Ea = a + a_m*Pa;
			Ea *= tau_mean;
		} else {
			Ea = a*tau_mean + a_m*times_accum[i];
		}

		if(Ea < Ea_1) {
			Ea_1 = Ea;
			a_1 = a;
		}
	}

	if(a_1 > top_resource) {
		a_1 = top_resource;
	}

	free(counts_cdp);
	free(times_accum);
	free(keys);

	return a_1;
}