Esempio n. 1
0
void *cvar_alloc_handle(const char *cvar_parameters,
		void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr))
{
	cvar_token_t *list_head;;
	cvar_token_t *t;
	handle_t handle;
	handle_t *state = NULL;
	int ret = 0;

	cvar_trace("entry");

	/* Tokenize parameters supplied by filebench. */
	list_head = NULL;
	ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER,
			DEFAULT_KEY_VALUE_DELIMITER, &list_head);
	if (ret)
		goto out;

	/* Get the value of mean and sigma. */
	t = find_token(list_head, RN_MEAN);
	if (t && t->value) {
		t->used = 1;
		handle.mean = atof(t->value);
	} else
		handle.mean = RN_MEAN_DEFAULT;

	t = find_token(list_head, RN_SIGMA);
	if (t && t->value) {
		t->used = 1;
		handle.sigma = atof(t->value);
	} else
		handle.sigma = RN_SIGMA_DEFAULT;

	cvar_trace("mean = %lf, sigma = %lf", handle.mean, handle.sigma);

	t = unused_tokens(list_head);
	if (t) {
		cvar_log_error("Unsupported parameter %s", t->key);
		goto out;
	}

	/* Seed the state. */
	mts_goodseed(&handle.state);

	/* All set. Now allocate space for the handle in the shared segment and
	 * copy the state over. */
	state = (handle_t *) cvar_malloc(sizeof(handle_t));
	if (!state) {
		cvar_log_error("Out of memory");
		goto out;
	}

	*state = handle;

out:
	free_tokens(list_head);

	cvar_trace("exit");
	return state;
}
Esempio n. 2
0
int cvar_next_value(void *cvar_handle, double *value)
{
	handle_t *h = (handle_t *) cvar_handle;

	if (!h) {
		cvar_log_error("NULL cvar_handle");
		return -1;
	}

	if (!value) {
		cvar_log_error("NULL value");
		return -1;
	}

	*value = rds_triangular(&h->state, h->lower, h->upper, h->mode);

	return 0;
}
Esempio n. 3
0
int tokenize(const char *parameters, const char parameter_delimiter,
		const char key_value_delimiter, cvar_token_t **list_head)
{
	char *param;
	char *param_start, *param_end;
	char *key_start, *key_end;
	cvar_token_t *lhead, *prev, *curr;
	int more_params;
	int no_value;
	int ret = 0;

	if (!parameters)
		goto out;

	ret = -1;

	lhead = prev = NULL;

	param = strdup(parameters);
	if (!param) {
		cvar_log_error("Out of memory");
		goto cleanup;
	}

	param_start = param;
	more_params = 1;

	while (more_params) {
		param_end = strchr(param_start, parameter_delimiter);
		if (param_end)
			*param_end = '\0';
		else {
			param_end = param_start + strlen(param_start);
			more_params = 0;
		}

		if (param_start != param_end) {
			key_start = param_start;
			key_end = strchr(param_start, key_value_delimiter);
			if (key_end) {
				*key_end = '\0';
				no_value = 0;
			} else {
				key_end = param_end;
				no_value = 1;
			}

			if (key_start == key_end) {
				cvar_log_error("Empty key at position %lu in parameter string "
						"\"%s\"", ((unsigned long)(key_start - param))/sizeof(char) + 1,
						parameters);
				goto cleanup;
			}


			curr = (cvar_token_t *) malloc(sizeof(cvar_token_t));
			if (!curr) {
				cvar_log_error("Out of memory");
				goto cleanup;
			}

			memset(curr, 0x00, sizeof(cvar_token_t));

			curr->key = strdup(key_start);
			if (!curr->key) {
				cvar_log_error("Out of memory");
				goto cleanup;
			}

			if (!no_value) {
				curr->value = strdup(key_end+1);
				if (!curr->value) {
					cvar_log_error("Out of memory");
					goto cleanup;
				}
			}

			if (!prev)
				lhead = prev = curr;
			else {
				prev->next = curr;
				prev = curr;
			}
		}

		if (more_params)
			param_start = param_end + 1;
	}

	*list_head = lhead;
	ret = 0;

out:
	return ret;

cleanup:
	free_tokens(lhead);
	lhead = NULL;
	goto out;
}
Esempio n. 4
0
void *cvar_alloc_handle(const char *cvar_parameters,
		void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr))
{
	cvar_token_t *list_head;;
	cvar_token_t *t;
	handle_t handle;
	handle_t *state = NULL;
	int ret = 0;

	cvar_trace("entry");

	/* Tokenize parameters supplied by filebench. */
	list_head = NULL;
	ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER,
			DEFAULT_KEY_VALUE_DELIMITER, &list_head);
	if (ret)
		goto out;

	/* Get the value of lower, upper and mode. */
	t = find_token(list_head, RT_LOWER);
	if (t && t->value) {
		t->used = 1;
		handle.lower = atof(t->value);
	} else
		handle.lower = RT_LOWER_DEFAULT;

	t = find_token(list_head, RT_UPPER);
	if (t && t->value) {
		t->used = 1;
		handle.upper = atof(t->value);
	} else
		handle.upper = RT_UPPER_DEFAULT;

	t = find_token(list_head, RT_MODE);
	if (t && t->value) {
		t->used = 1;
		handle.mode = atof(t->value);
	} else
		handle.mode = RT_MODE_DEFAULT;

	cvar_trace("lower = %lf, upper = %lf, mode = %lf", handle.lower,
			handle.upper, handle.mode);

	/* Validate parameters. */
	if (handle.upper < handle.lower) {
		cvar_log_error("Invalid parameter values: lower = %lf and upper = %lf. "
				"upper must be greater than lower", handle.lower, handle.upper);
		goto out;
	}

	if ((handle.mode > handle.upper) || (handle.mode < handle.lower)) {
		cvar_log_error("Invalid parameter values: lower = %lf, mode = %lf and "
				"upper = %lf. mode must be between lower and upper",
				handle.lower, handle.mode, handle.upper);
		goto out;
	}

	/* Check if there are unused tokens. */
	t = unused_tokens(list_head);
	if (t) {
		cvar_log_error("Unsupported parameter %s", t->key);
		goto out;
	}

	/* Seed the state. */
	mts_goodseed(&handle.state);

	/* All set. Now allocate space for the handle in the shared segment and
	 * copy the state over. */
	state = (handle_t *) cvar_malloc(sizeof(handle_t));
	if (!state) {
		cvar_log_error("Out of memory");
		goto out;
	}

	*state = handle;

out:
	free_tokens(list_head);

	cvar_trace("exit");
	return state;
}
Esempio n. 5
0
void *cvar_alloc_handle(const char *cvar_parameters,
		void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr))
{
	cvar_token_t *list_head;;
	cvar_token_t *t;
	handle_t handle;
	handle_t *state = NULL;
	int ret = 0;

	cvar_trace("entry");

	/* Tokenize parameters supplied by filebench. */
	list_head = NULL;
	ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER,
			DEFAULT_KEY_VALUE_DELIMITER, &list_head);
	if (ret)
		goto out;

	/* Get the value of shape and rate. */
	t = find_token(list_head, RER_SHAPE);
	if (t && t->value) {
		t->used = 1;
		handle.shape = atoi(t->value);
	} else
		handle.shape = RER_SHAPE_DEFAULT;

	t = find_token(list_head, RER_RATE);
	if (t && t->value) {
		t->used = 1;
		handle.rate = atof(t->value);
	} else
		handle.rate = RER_RATE_DEFAULT;

	cvar_trace("shape = %d, rate = %lf", handle.shape, handle.rate);

	/* Validate parameters. */
	if (handle.shape < 0) {
		cvar_log_error("Invalid parameter value: shape = %d. shape is a "
				"non-zero positive integer", handle.shape);
		goto out;
	}

	if (handle.rate < 0) {
		cvar_log_error("Invalid parameter value: rate = %lf. rate is a "
				"non-zero positive rational number", handle.rate);
		goto out;
	}

	t = unused_tokens(list_head);
	if (t) {
		cvar_log_error("Unsupported parameter %s", t->key);
		goto out;
	}

	/* Seed the state. */
	mts_goodseed(&handle.state);

	/* All set. Now allocate space for the handle in the shared segment and
	 * copy the state over. */
	state = (handle_t *) cvar_malloc(sizeof(handle_t));
	if (!state) {
		cvar_log_error("Out of memory");
		goto out;
	}

	*state = handle;

out:
	free_tokens(list_head);

	cvar_trace("exit");
	return state;
}