GNUnet  0.10.x
testbed_api_sd.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2008--2013 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "testbed_api_sd.h"
30 
34 struct SDEntry
35 {
39  struct SDEntry *next;
40 
44  struct SDEntry *prev;
45 
49  unsigned int amount;
50 };
51 
52 
56 struct SDHandle
57 {
61  struct SDEntry *head;
62 
66  struct SDEntry *tail;
67 
71  unsigned long long sqsum;
72 
76  unsigned long sum;
77 
81  float avg;
82 
86  double vr;
87 
91  unsigned int cnt;
92 
96  unsigned int max_cnt;
97 };
98 
99 
106 struct SDHandle *
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 }
116 
117 
123 void
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 }
135 
136 
143 void
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 }
173 
174 
184 int
186  int *factor)
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 }
211 
212 /* end of testbed_api_sd.c */
#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.
int GNUNET_TESTBED_SD_deviation_factor_(struct SDHandle *h, unsigned int amount, int *factor)
Calculates the factor by which the given amount differs.
Opaque handle for calculating SD.
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.
struct SDEntry * prev
DLL prev pointer.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#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.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
unsigned long long sqsum
Squared sum of data values.
struct SDHandle * GNUNET_TESTBED_SD_init_(unsigned int max_cnt)
Initialize standard deviation calculation handle.
float avg
The average of data amounts.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#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.
functions to calculate standard deviation
double vr
The variance.
void GNUNET_TESTBED_SD_add_data_(struct SDHandle *h, unsigned int amount)
Add a reading to SD.
void GNUNET_TESTBED_SD_destroy_(struct SDHandle *h)
Frees the memory allocated to the SD handle.
struct SDEntry * next
DLL next pointer.
unsigned long sum
Sum of the data values.
#define GNUNET_free(ptr)
Wrapper around free.