GNUnet  0.10.x
Functions
testbed_api_sd.h File Reference

functions to calculate standard deviation More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

struct SDHandleGNUNET_TESTBED_SD_init_ (unsigned int max_cnt)
 Initialize standard deviation calculation handle. More...
 
void GNUNET_TESTBED_SD_destroy_ (struct SDHandle *h)
 Frees the memory allocated to the SD handle. More...
 
void GNUNET_TESTBED_SD_add_data_ (struct SDHandle *h, unsigned int amount)
 Add a reading to SD. More...
 
int GNUNET_TESTBED_SD_deviation_factor_ (struct SDHandle *h, unsigned int amount, int *factor)
 Returns the factor by which the given amount differs from the standard deviation. More...
 

Detailed Description

functions to calculate standard deviation

Author
Sree Harsha Totakura sreeh.nosp@m.arsh.nosp@m.a@tot.nosp@m.akur.nosp@m.a.in

Definition in file testbed_api_sd.h.

Function Documentation

◆ GNUNET_TESTBED_SD_init_()

struct SDHandle* GNUNET_TESTBED_SD_init_ ( unsigned int  max_cnt)

Initialize standard deviation calculation handle.

Parameters
max_cntthe maximum number of readings to keep
Returns
the initialized handle

Definition at line 107 of file testbed_api_sd.c.

References GNUNET_assert, GNUNET_new, h, and SDHandle::max_cnt.

Referenced by GNUNET_TESTBED_operation_queue_create_().

108 {
109  struct SDHandle *h;
110 
111  GNUNET_assert (1 < max_cnt);
112  h = GNUNET_new (struct SDHandle);
113  h->max_cnt = max_cnt;
114  return h;
115 }
Opaque handle for calculating SD.
unsigned int max_cnt
max number of entries we can have in the DLL
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
Here is the caller graph for this function:

◆ GNUNET_TESTBED_SD_destroy_()

void GNUNET_TESTBED_SD_destroy_ ( struct SDHandle h)

Frees the memory allocated to the SD handle.

Parameters
hthe SD handle

Definition at line 124 of file testbed_api_sd.c.

References GNUNET_CONTAINER_DLL_remove, GNUNET_free, SDHandle::head, and SDHandle::tail.

Referenced by queue_destroy().

125 {
126  struct SDEntry *entry;
127 
128  while (NULL != (entry = h->head))
129  {
130  GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry);
131  GNUNET_free (entry);
132  }
133  GNUNET_free (h);
134 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct SDEntry * tail
DLL tail for storing entries.
An entry to hold data which will be used to calculate SD.
struct SDEntry * head
DLL head for storing entries.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ GNUNET_TESTBED_SD_add_data_()

void GNUNET_TESTBED_SD_add_data_ ( struct SDHandle h,
unsigned int  amount 
)

Add a reading to SD.

Parameters
hthe SD handle
amountthe reading value

Definition at line 144 of file testbed_api_sd.c.

References SDEntry::amount, SDHandle::avg, SDHandle::cnt, GNUNET_assert, GNUNET_CONTAINER_DLL_insert_tail, GNUNET_CONTAINER_DLL_remove, GNUNET_new, SDHandle::head, SDHandle::max_cnt, SDHandle::sqsum, SDHandle::sum, SDHandle::tail, and SDHandle::vr.

Referenced by adapt_parallelism().

145 {
146  struct SDEntry *entry;
147  double sqavg;
148  double sqsum_avg;
149 
150  entry = NULL;
151  if (h->cnt == h->max_cnt)
152  {
153  entry = h->head;
154  GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry);
155  h->sum -= entry->amount;
156  h->sqsum -=
157  ((unsigned long) entry->amount) * ((unsigned long) entry->amount);
158  h->cnt--;
159  }
160  GNUNET_assert (h->cnt < h->max_cnt);
161  if (NULL == entry)
162  entry = GNUNET_new (struct SDEntry);
163  entry->amount = amount;
165  h->sum += amount;
166  h->cnt++;
167  h->avg = ((float) h->sum) / ((float) h->cnt);
168  h->sqsum += ((unsigned long) amount) * ((unsigned long) amount);
169  sqsum_avg = ((double) h->sqsum) / ((double) h->cnt);
170  sqavg = ((double) h->avg) * ((double) h->avg);
171  h->vr = sqsum_avg - sqavg;
172 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
unsigned int cnt
Number of data values; also the length of DLL containing SDEntries.
unsigned int max_cnt
max number of entries we can have in the DLL
struct SDEntry * tail
DLL tail for storing entries.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
unsigned int amount
The value to store.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
An entry to hold data which will be used to calculate SD.
unsigned long long sqsum
Squared sum of data values.
float avg
The average of data amounts.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
struct SDEntry * head
DLL head for storing entries.
double vr
The variance.
unsigned long sum
Sum of the data values.
Here is the caller graph for this function:

◆ GNUNET_TESTBED_SD_deviation_factor_()

int GNUNET_TESTBED_SD_deviation_factor_ ( struct SDHandle h,
unsigned int  amount,
int *  factor 
)

Returns the factor by which the given amount differs from the standard deviation.

Parameters
hthe SDhandle
amountthe value for which the deviation is returned
factorthe factor by which the given amont differs
Returns
the deviation from the average; GNUNET_SYSERR if the deviation cannot be calculated OR 0 if the deviation is less than the average; a maximum of 4 is returned for deviations equal to or larger than 4

Returns the factor by which the given amount differs from the standard deviation.

Parameters
hthe SDhandle
amountthe value for which the deviation is returned
factorthe factor by which the given amont differs
Returns
GNUNET_SYSERR if the deviation cannot be calculated; GNUNET_OK if the deviation is returned through factor

Definition at line 185 of file testbed_api_sd.c.

References SDHandle::avg, SDHandle::cnt, removetrailingwhitespace::f, GNUNET_OK, GNUNET_SYSERR, and SDHandle::vr.

Referenced by adapt_parallelism().

187 {
188  double diff;
189  int f;
190  int n;
191 
192  if (h->cnt < 2)
193  return GNUNET_SYSERR;
194  if (((float) amount) > h->avg)
195  {
196  diff = ((float) amount) - h->avg;
197  f = 1;
198  }
199  else
200  {
201  diff = h->avg - ((float) amount);
202  f = -1;
203  }
204  diff *= diff;
205  for (n = 1; n < 4; n++)
206  if (diff < (((double) (n * n)) * h->vr))
207  break;
208  *factor = f * n;
209  return GNUNET_OK;
210 }
unsigned int cnt
Number of data values; also the length of DLL containing SDEntries.
unsigned int amount
The value to store.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
float avg
The average of data amounts.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
double vr
The variance.
Here is the caller graph for this function: