Flowgrind
Advanced TCP traffic generator
fg_math.c File Reference

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

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <syslog.h>
#include <float.h>
#include <fenv.h>
#include "debug.h"
#include "fg_math.h"
#include "fg_error.h"
#include "fg_definitions.h"
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.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.c.

Function Documentation

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 */
185  UNUSED_ARGUMENT(flow);
186  return rn_uniform_zero_to_one() <= p;
187 #endif /* HAVE_LIBGSL */
188 }
gsl_rng * r
Definition: daemon.h:167
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
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 */
226  UNUSED_ARGUMENT(flow);
227  UNUSED_ARGUMENT(nu);
228  return 0;
229 #endif /* HAVE_LIBGSL */
230 }
gsl_rng * r
Definition: daemon.h:167
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
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 */
129  UNUSED_ARGUMENT(flow);
130  return -log(rn_uniform())+mu;
131 #endif /* HAVE_LIBGSL */
132 }
gsl_rng * r
Definition: daemon.h:167
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
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 */
171  UNUSED_ARGUMENT(flow);
172  UNUSED_ARGUMENT(zeta);
173  UNUSED_ARGUMENT(sigma);
174  return 0;
175 #endif /* HAVE_LIBGSL */
176 }
gsl_rng * r
Definition: daemon.h:167
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
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 */
156  UNUSED_ARGUMENT(flow);
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 }
gsl_rng * r
Definition: daemon.h:167
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
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 */
197  UNUSED_ARGUMENT(flow);
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 }
gsl_rng * r
Definition: daemon.h:167
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
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 */
143  UNUSED_ARGUMENT(flow);
144  const double x = rn_uniform_zero_to_one();
145  return ((maxval-minval) * x) + minval;
146 #endif /* HAVE_LIBGSL */
147 }
gsl_rng * r
Definition: daemon.h:167
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
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 */
213  UNUSED_ARGUMENT(flow);
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 }
gsl_rng * r
Definition: daemon.h:167
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
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 */
102  UNUSED_ARGUMENT(flow);
103 #endif /* HAVE_LIBGSL */
104 }
gsl_rng * r
Definition: daemon.h:167
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
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
62  UNUSED_ARGUMENT(flow);
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 }
gsl_rng * r
Definition: daemon.h:167
#define DEBUG_MSG(LVL, MSG,...)
Print debug message to standard error.
Definition: debug.h:49
void * data
Pointer to user defined data stored with this node.
Definition: fg_list.h:38
#define UNUSED_ARGUMENT(x)
Suppress warning for unused argument.
#define crit(...)
To report an critical error w/ the corresponding system error message.
Definition: fg_error.h:36
int id
Definition: daemon.h:75