wl_plugin_scenario_file.c from EmStar at Krugle
Show wl_plugin_scenario_file.c syntax highlighted
/*
* Copyright (c) 2004 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "wl_i.h"
struct scen_file {
g_event_t *timer;
wl_state_t *parent;
int start_full;
int halt_on_next;
char *file;
buf_t *input;
parser_state_t *lfs;
char *nodes;
};
static int sf_check_next(void *data, int interval, g_event_t *event)
{
struct scen_file *sf = (struct scen_file *)data;
int status;
if (sf->halt_on_next) {
elog(LOG_NOTICE, "Halt time arrived... Scenario complete!");
wl_force_shutdown();
}
/* do the next trigger */
if (sf->nodes) {
/* run thru the nodes */
parser_state_t *ps = misc_parse_init(sf->nodes, MISC_PARSE_COMMA_SCHEME);
while (misc_parse_next_kvp(ps) >= 0) {
/* for this key */
int pick = atoi(ps->key);
int j;
wl_node_t *n;
int count;
/* pick a node */
for (n=wl_nodes_top(&(sf->parent->nodes));
n && n->id != pick;
n=wl_nodes_next(n));
if (n) {
/* count existing entries */
count = wl_count_entries(n);
for (j=0; j<sf->parent->max_keys; j++) {
wl_admit_random_data(sf->parent, n, -1, -1);
/* only add all if started out blank */
if (count || !sf->start_full)
break;
}
wl_push_data(sf->parent, n);
elog(LOG_NOTICE, "Data to node %d", pick);
}
else {
elog(LOG_WARNING, "No node number %d found!", pick);
}
}
misc_parse_cleanup(ps);
}
next_line:
/* get the next line */
status = misc_parse_next_kvp(sf->lfs);
if (status < 0) {
elog(LOG_NOTICE, "Scenario complete!");
wl_force_shutdown();
}
{
char *next_line = sf->lfs->key;
int time = -1;
if (sf->nodes) {
free(sf->nodes);
sf->nodes = NULL;
}
/* skip comments */
if (next_line && (next_line[0] == '#')) {
elog(LOG_NOTICE, "*** %s", next_line);
goto next_line;
}
parser_state_t *line = misc_parse_init(next_line, MISC_PARSE_COLON_SCHEME);
while (misc_parse_next_kvp(line) >= 0) {
if (strcmp(line->key, "default_size") == 0) {
sf->parent->entry_size = atoi(line->value);
}
else if (strcmp(line->key, "time") == 0) {
time = atoi(line->value);
}
else if (strcmp(line->key, "nodes") == 0) {
sf->nodes = strdup(line->value);
}
else if (strcmp(line->key, "halt") == 0) {
sf->halt_on_next = 1;
}
else {
elog(LOG_WARNING, "unknown key: %s", line->key);
}
}
misc_parse_cleanup(line);
if (time < 0) {
elog(LOG_WARNING, "No time entry: '%s'", next_line);
goto next_line;
}
time += sf->parent->delay;
/* set timer for the next event */
{
int64_t uptime = misc_time_elapsed_sys() / 1000;
if (time > uptime)
time = time - uptime;
else
time = 0;
elog(LOG_NOTICE, "Setting timer, nodes %s in %f seconds..",
sf->nodes, time / 1000.0);
return TIMER_RENEW_MS(time);
}
}
}
static struct scen_file sf_state = {
};
void wl_plugin_scenario_file(wl_state_t *wl, char *load, int *argc, char **argv)
{
bufprintf(wl->usage_buf,
" scenario_file: generates data according to a schedule in a file\n"
" --file <file>: specifies the file to load\n"
);
if (load) {
sf_state.parent = wl;
if (strcmp(load, "scenario_file") == 0) {
sf_state.file = misc_parse_out_option(argc, argv, "file", 0);
elog(LOG_NOTICE, "Starting 'scenario_file' workload, file=%s", sf_state.file);
sf_state.input = buf_new();
if (file_to_buf(sf_state.input, sf_state.file) < 0) {
elog(LOG_ERR, "Unable to read scenario file: %m");
exit(1);
}
sf_state.lfs = misc_parse_init(sf_state.input->buf, MISC_PARSE_LF_SCHEME);
g_timer_add(wl->delay, sf_check_next, &sf_state, NULL, &(sf_state.timer));
}
}
}
See more files for this project here