Flowgrind
Advanced TCP traffic generator
fg_math.h File Reference

Routines for statistics and advanced traffic generation. More...

#include "config.h"
#include "daemon.h"

Go to the source code of this file.

Functions

int dist_bernoulli (struct flow *flow, const double p)
 
double dist_chisq (struct flow *flow, const double nu)
 
double dist_exponential (struct flow *flow, const double mu)
 
double dist_lognormal (struct flow *flow, const double zeta, const double sigma)
 
double dist_normal (struct flow *flow, const double mu, const double sigma_square)
 
double dist_pareto (struct flow *flow, const double k, const double x_min)
 
double dist_uniform (struct flow *flow, const double minval, const double maxval)
 
double dist_weibull (struct flow *flow, const double alpha, const double beta)
 
void free_math_functions (struct flow *flow)
 
void init_math_functions (struct flow *flow, unsigned long seed)
 

Detailed Description

Routines for statistics and advanced traffic generation.

Definition in file fg_math.h.

Function Documentation

◆ dist_bernoulli()

int dist_bernoulli ( struct flow flow,
const double  p 
)

Definition at line 179 of file fg_math.c.

180 {
181 #ifdef HAVE_LIBGSL
182  gsl_rng * r = flow->r;
183  return gsl_ran_bernoulli (r, p);
184 #else /* HAVE_LIBGSL */
186  return rn_uniform_zero_to_one() <= p;
187 #endif /* HAVE_LIBGSL */
188 }

◆ dist_chisq()

double dist_chisq ( struct flow flow,
const double  nu 
)

Definition at line 219 of file fg_math.c.

220 {
221 #ifdef HAVE_LIBGSL
222  gsl_rng * r = flow->r;
223  return gsl_ran_chisq(r, nu);
224 #else /* HAVE_LIBGSL */
225  /* not implemented */
227  UNUSED_ARGUMENT(nu);
228  return 0;
229 #endif /* HAVE_LIBGSL */
230 }

◆ dist_exponential()

double dist_exponential ( struct flow flow,
const double  mu 
)

Definition at line 123 of file fg_math.c.

124 {
125 #ifdef HAVE_LIBGSL
126  gsl_rng * r = flow->r;
127  return gsl_ran_exponential(r, mu);
128 #else /* HAVE_LIBGSL */
130  return -log(rn_uniform())+mu;
131 #endif /* HAVE_LIBGSL */
132 }

◆ dist_lognormal()

double dist_lognormal ( struct flow flow,
const double  zeta,
const double  sigma 
)

Definition at line 163 of file fg_math.c.

165 {
166 #ifdef HAVE_LIBGSL
167  gsl_rng * r = flow->r;
168  return gsl_ran_lognormal (r, zeta, sigma);
169 #else /* HAVE_LIBGSL */
170  /* not implemented */
172  UNUSED_ARGUMENT(zeta);
173  UNUSED_ARGUMENT(sigma);
174  return 0;
175 #endif /* HAVE_LIBGSL */
176 }

◆ dist_normal()

double dist_normal ( struct flow flow,
const double  mu,
const double  sigma_square 
)

Definition at line 149 of file fg_math.c.

151 {
152 #ifdef HAVE_LIBGSL
153  const gsl_rng * r = flow->r;
154  return gsl_ran_gaussian (r, sigma_square) + mu;
155 #else /* HAVE_LIBGSL */
157  const double x = rn_uniform_minusone_to_one();
158  return (1.0 / sqrt(2.0*M_PI*sigma_square)) *
159  exp((-pow ((x-mu),2)) / (2 * sigma_square));
160 #endif /* HAVE_LIBGSL */
161 }

◆ dist_pareto()

double dist_pareto ( struct flow flow,
const double  k,
const double  x_min 
)

Definition at line 190 of file fg_math.c.

192 {
193 #ifdef HAVE_LIBGSL
194  gsl_rng * r = flow->r;
195  return gsl_ran_pareto (r, k, x_min);
196 #else /* HAVE_LIBGSL */
198  const double x = rn_uniform();
199  if (x < x_min)
200  return 0;
201  else
202  return (k/x_min) * pow (x_min/x,k+1);
203 #endif /* HAVE_LIBGSL */
204 }

◆ dist_uniform()

double dist_uniform ( struct flow flow,
const double  minval,
const double  maxval 
)

Definition at line 136 of file fg_math.c.

138 {
139 #ifdef HAVE_LIBGSL
140  gsl_rng * r = flow->r;
141  return gsl_ran_flat(r, minval, maxval);
142 #else /* HAVE_LIBGSL */
144  const double x = rn_uniform_zero_to_one();
145  return ((maxval-minval) * x) + minval;
146 #endif /* HAVE_LIBGSL */
147 }

◆ dist_weibull()

double dist_weibull ( struct flow flow,
const double  alpha,
const double  beta 
)

Definition at line 206 of file fg_math.c.

208 {
209 #ifdef HAVE_LIBGSL
210  gsl_rng * r = flow->r;
211  return gsl_ran_weibull (r, alpha, beta);
212 #else /* HAVE_LIBGSL */
214  const double x = rn_uniform_zero_to_one();
215  return alpha * beta * pow (x,beta-1.0) * exp(-alpha * pow(x,beta));
216 #endif /* HAVE_LIBGSL */
217 }

◆ free_math_functions()

void free_math_functions ( struct flow flow)

Definition at line 97 of file fg_math.c.

98 {
99 #ifdef HAVE_LIBGSL
100  gsl_rng_free(flow->r);
101 #else /* HAVE_LIBGSL */
103 #endif /* HAVE_LIBGSL */
104 }

◆ init_math_functions()

void init_math_functions ( struct flow flow,
unsigned long  seed 
)

Definition at line 58 of file fg_math.c.

59 {
60  int rc;
61 #ifndef HAVE_LIBGSL
63 #endif /* HAVE_LIBGSL */
64  /* set rounding */
65  fesetround(FE_TONEAREST);
66 
67  /* initalize rng */
68 #ifdef HAVE_LIBGSL
69  const gsl_rng_type * T;
70  gsl_rng_env_setup();
71  T = gsl_rng_default;
72  flow->r = gsl_rng_alloc (T);
73 #endif /* HAVE_LIBGSL */
74 
75  if (!seed) {
76  /* if no seed supplied use urandom */
77  DEBUG_MSG(LOG_WARNING, "client did not supply random seed value");
78  int data = open("/dev/urandom", O_RDONLY);
79  rc = read(data, &seed, sizeof (long) );
80  close(data);
81  if(rc == -1)
82  crit("read /dev/urandom failed");
83  }
84 
85 #ifdef HAVE_LIBGSL
86  gsl_rng_set (flow->r, seed);
87  DEBUG_MSG(LOG_WARNING, "initalized local libgsl random functions for "
88  "flow %d with seed %lu, gsl generator is: %s",
89  flow->id,seed,gsl_rng_name (flow->r));
90 #else /* HAVE_LIBGSL */
91  srand((unsigned)seed);
92  DEBUG_MSG(LOG_WARNING, "initalized posix random functions with seed "
93  "%u", (unsigned)seed);
94 #endif /* HAVE_LIBGSL */
95 }
DEBUG_MSG
#define DEBUG_MSG(LVL, MSG,...)
Print debug message to standard error.
Definition: debug.h:49
flow
Definition: daemon.h:73
crit
#define crit(...)
To report an critical error w/ the corresponding system error message.
Definition: fg_error.h:36
UNUSED_ARGUMENT
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
Definition: fg_definitions.h:38
list_node::data
void * data
Pointer to user defined data stored with this node.
Definition: fg_list.h:38
flow::r
gsl_rng * r
Definition: daemon.h:167
flow::id
int id
Definition: daemon.h:75