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; }
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; }
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); }
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; }