getaddrinfo_host.c [plain text]
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include "../libmicro.h"
#if DEBUG
# define debug(fmt, args...) (void) fprintf(stderr, fmt "\n" , ##args)
#else
# define debug(fmt, args...)
#endif
extern int gL1CacheEnabled;
typedef struct {
} tsd_t;
#define HOSTNAME_LEN 125
static int host_min=-1, host_range=0;
static char *hostname_format=NULL;
static char *hostname_list=NULL;
int
benchmark_init()
{
debug("benchmark_init");
(void) sprintf(lm_optstr, "l:h:r:");
lm_tsdsize = sizeof (tsd_t);
lm_defB = 100;
(void) sprintf(lm_usage,
"\n ------- getaddrinfo_host specific options (default: *)\n"
" [-h \"hostname format\"]. ie. \"server-%%d.perf\"\n"
" [-r min-max]\n"
"\n" );
return (0);
}
int
parse_range(int *min, int *offset, char *buf)
{
char *value, *tmp_ptr = strdup(buf);
int range=0;
debug("parse_range");
value = strsep(&tmp_ptr, "-");
*min = atoi(value);
debug("min = %d", *min);
if (tmp_ptr) {
value = strsep(&tmp_ptr, "-");
range = atoi(value);
if (range < *min) {
printf("max id should be larger than min id\n");
return -1;
}
*offset = range - *min + 1; debug("range = %d", *offset);
}
else {
printf("argument should be in the form of min-max\n");
return -1;
}
return 0;
}
int
benchmark_optswitch(int opt, char *optarg)
{
debug("benchmark_optswitch");
switch (opt) {
case 'h': hostname_format = strdup(optarg);
debug ("hostname format: %s", hostname_format);
break;
case 'l':
gL1CacheEnabled = atoi(optarg);
break;
case 'r': return parse_range( &host_min, &host_range, optarg);
break;
default:
return -1;
}
return 0;
}
int
benchmark_initrun()
{
int i;
debug("\nbenchmark_initrun");
if (host_min == -1) {
printf("-r min-max needs to be specified\n");
exit (1);
}
if (!hostname_format) {
printf("-h hostname_format needs to be specified\n");
exit (1);
}
hostname_list = malloc ( host_range * HOSTNAME_LEN );
if (!hostname_list) {
debug("malloc error");
exit (1);
}
for (i = 0; i < host_range; i++) {
sprintf( &hostname_list[i*HOSTNAME_LEN], hostname_format, i+host_min);
}
return (0);
}
int
benchmark(void *tsd, result_t *res)
{
int i, index, err;
struct addrinfo *addi;
res->re_errors = 0;
debug("in to benchmark - optB = %i", lm_optB);
srandom(getpid());
for (i = 0; i < lm_optB; i++) {
index = HOSTNAME_LEN * (random() % host_range);
err = getaddrinfo( &hostname_list[index], NULL, NULL, &addi);
if (err) {
debug("%s: error: %s", &hostname_list[index], gai_strerror(err));
res->re_errors++;
}
else {
debug("host %s done", &hostname_list[index]);
}
freeaddrinfo (addi);
}
res->re_count = i;
return (0);
}
int
benchmark_finirun(void *tsd)
{
debug("benchmark_finirun ");
free(hostname_list);
return (0);
}
char *
benchmark_result()
{
static char result = '\0';
debug("benchmark_result");
return (&result);
}