GNUnet  0.19.4
testbed_api_sd.c File Reference

functions to calculate standard deviation More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "testbed_api_sd.h"
Include dependency graph for testbed_api_sd.c:

Go to the source code of this file.

Data Structures

struct  SDEntry
 An entry to hold data which will be used to calculate SD. More...
 
struct  SDHandle
 Opaque handle for calculating SD. More...
 

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)
 Calculates the factor by which the given amount differs. 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.c.

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.

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 }
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
#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.
Opaque handle for calculating SD.
unsigned int max_cnt
max number of entries we can have in the DLL

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

Referenced by GNUNET_TESTBED_operation_queue_create_().

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.

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.
#define GNUNET_free(ptr)
Wrapper around free.
An entry to hold data which will be used to calculate SD.

References GNUNET_CONTAINER_DLL_remove, GNUNET_free, and h.

Referenced by queue_destroy().

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.

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;
164  GNUNET_CONTAINER_DLL_insert_tail (h->head, h->tail, entry);
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_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
unsigned int amount
The value to store.

References SDEntry::amount, GNUNET_assert, GNUNET_CONTAINER_DLL_insert_tail, GNUNET_CONTAINER_DLL_remove, GNUNET_new, and h.

Referenced by adapt_parallelism().

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 
)

Calculates the factor by which the given amount differs.

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.

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 }
@ GNUNET_OK
@ GNUNET_SYSERR

References SDEntry::amount, removetrailingwhitespace::f, GNUNET_OK, GNUNET_SYSERR, and h.

Referenced by adapt_parallelism().

Here is the caller graph for this function: