aboutsummaryrefslogtreecommitdiffstats
path: root/bin/common.c
blob: 2aa9a7cfcbbac1b73302018cd88b2cbf70b3163f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include "common.h"
#include "litmus.h"
#include <litmus/color.h>

#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>

void bail_out(const char* msg)
{
	perror(msg);
	exit(-1 * errno);
}

void request_resources(int task_colors, int avg_ways)
{
        const gsl_rng       *way_rng = gsl_rng_alloc(gsl_rng_taus);
	struct control_page *page    = get_ctrl_page();
	int color, ways, rem = task_colors;
	float random;
	char taken[NUM_COLORS];

	memset(page->requests, 0, NUM_COLORS*sizeof(*page->requests));

	gsl_rng_set(way_rng, rand());
	memset(taken, 0, NUM_COLORS);

	srand(getpid());
	gsl_rng_set(way_rng, getpid());

	while (rem) {
		/* select a random unchosen color */
		do {
			color = rand() % NUM_COLORS;
		} while (taken[color]);

		if (rand() % NUM_COLORS <= task_colors) {
			taken[color] = 1;
			rem --;

			random = gsl_ran_exponential(way_rng, avg_ways + 1);
			ways = 1 + (int)random;
			ways = (ways < 1) ? 1 : ways;
			ways = (ways > NUM_WAYS) ? NUM_WAYS : ways;

			page->requests[color] = ways;
		}
	}
}