aboutsummaryrefslogtreecommitdiff
path: root/src/testbed/testbed_api_sd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testbed/testbed_api_sd.c')
-rw-r--r--src/testbed/testbed_api_sd.c82
1 files changed, 40 insertions, 42 deletions
diff --git a/src/testbed/testbed_api_sd.c b/src/testbed/testbed_api_sd.c
index c9cbb27a1..2bff246a7 100644
--- a/src/testbed/testbed_api_sd.c
+++ b/src/testbed/testbed_api_sd.c
@@ -11,7 +11,7 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
@@ -31,8 +31,7 @@
31/** 31/**
32 * An entry to hold data which will be used to calculate SD 32 * An entry to hold data which will be used to calculate SD
33 */ 33 */
34struct SDEntry 34struct SDEntry {
35{
36 /** 35 /**
37 * DLL next pointer 36 * DLL next pointer
38 */ 37 */
@@ -53,8 +52,7 @@ struct SDEntry
53/** 52/**
54 * Opaque handle for calculating SD 53 * Opaque handle for calculating SD
55 */ 54 */
56struct SDHandle 55struct SDHandle {
57{
58 /** 56 /**
59 * DLL head for storing entries 57 * DLL head for storing entries
60 */ 58 */
@@ -104,12 +102,12 @@ struct SDHandle
104 * @return the initialized handle 102 * @return the initialized handle
105 */ 103 */
106struct SDHandle * 104struct SDHandle *
107GNUNET_TESTBED_SD_init_ (unsigned int max_cnt) 105GNUNET_TESTBED_SD_init_(unsigned int max_cnt)
108{ 106{
109 struct SDHandle *h; 107 struct SDHandle *h;
110 108
111 GNUNET_assert (1 < max_cnt); 109 GNUNET_assert(1 < max_cnt);
112 h = GNUNET_new (struct SDHandle); 110 h = GNUNET_new(struct SDHandle);
113 h->max_cnt = max_cnt; 111 h->max_cnt = max_cnt;
114 return h; 112 return h;
115} 113}
@@ -121,16 +119,16 @@ GNUNET_TESTBED_SD_init_ (unsigned int max_cnt)
121 * @param h the SD handle 119 * @param h the SD handle
122 */ 120 */
123void 121void
124GNUNET_TESTBED_SD_destroy_ (struct SDHandle *h) 122GNUNET_TESTBED_SD_destroy_(struct SDHandle *h)
125{ 123{
126 struct SDEntry *entry; 124 struct SDEntry *entry;
127 125
128 while (NULL != (entry = h->head)) 126 while (NULL != (entry = h->head))
129 { 127 {
130 GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry); 128 GNUNET_CONTAINER_DLL_remove(h->head, h->tail, entry);
131 GNUNET_free (entry); 129 GNUNET_free(entry);
132 } 130 }
133 GNUNET_free (h); 131 GNUNET_free(h);
134} 132}
135 133
136 134
@@ -141,7 +139,7 @@ GNUNET_TESTBED_SD_destroy_ (struct SDHandle *h)
141 * @param amount the reading value 139 * @param amount the reading value
142 */ 140 */
143void 141void
144GNUNET_TESTBED_SD_add_data_ (struct SDHandle *h, unsigned int amount) 142GNUNET_TESTBED_SD_add_data_(struct SDHandle *h, unsigned int amount)
145{ 143{
146 struct SDEntry *entry; 144 struct SDEntry *entry;
147 double sqavg; 145 double sqavg;
@@ -149,25 +147,25 @@ GNUNET_TESTBED_SD_add_data_ (struct SDHandle *h, unsigned int amount)
149 147
150 entry = NULL; 148 entry = NULL;
151 if (h->cnt == h->max_cnt) 149 if (h->cnt == h->max_cnt)
152 { 150 {
153 entry = h->head; 151 entry = h->head;
154 GNUNET_CONTAINER_DLL_remove (h->head, h->tail, entry); 152 GNUNET_CONTAINER_DLL_remove(h->head, h->tail, entry);
155 h->sum -= entry->amount; 153 h->sum -= entry->amount;
156 h->sqsum -= 154 h->sqsum -=
157 ((unsigned long) entry->amount) * ((unsigned long) entry->amount); 155 ((unsigned long)entry->amount) * ((unsigned long)entry->amount);
158 h->cnt--; 156 h->cnt--;
159 } 157 }
160 GNUNET_assert (h->cnt < h->max_cnt); 158 GNUNET_assert(h->cnt < h->max_cnt);
161 if (NULL == entry) 159 if (NULL == entry)
162 entry = GNUNET_new (struct SDEntry); 160 entry = GNUNET_new(struct SDEntry);
163 entry->amount = amount; 161 entry->amount = amount;
164 GNUNET_CONTAINER_DLL_insert_tail (h->head, h->tail, entry); 162 GNUNET_CONTAINER_DLL_insert_tail(h->head, h->tail, entry);
165 h->sum += amount; 163 h->sum += amount;
166 h->cnt++; 164 h->cnt++;
167 h->avg = ((float) h->sum) / ((float) h->cnt); 165 h->avg = ((float)h->sum) / ((float)h->cnt);
168 h->sqsum += ((unsigned long) amount) * ((unsigned long) amount); 166 h->sqsum += ((unsigned long)amount) * ((unsigned long)amount);
169 sqsum_avg = ((double) h->sqsum) / ((double) h->cnt); 167 sqsum_avg = ((double)h->sqsum) / ((double)h->cnt);
170 sqavg = ((double) h->avg) * ((double) h->avg); 168 sqavg = ((double)h->avg) * ((double)h->avg);
171 h->vr = sqsum_avg - sqavg; 169 h->vr = sqsum_avg - sqavg;
172} 170}
173 171
@@ -182,8 +180,8 @@ GNUNET_TESTBED_SD_add_data_ (struct SDHandle *h, unsigned int amount)
182 * be calculated; GNUNET_OK if the deviation is returned through factor 180 * be calculated; GNUNET_OK if the deviation is returned through factor
183 */ 181 */
184int 182int
185GNUNET_TESTBED_SD_deviation_factor_ (struct SDHandle *h, unsigned int amount, 183GNUNET_TESTBED_SD_deviation_factor_(struct SDHandle *h, unsigned int amount,
186 int *factor) 184 int *factor)
187{ 185{
188 double diff; 186 double diff;
189 int f; 187 int f;
@@ -191,19 +189,19 @@ GNUNET_TESTBED_SD_deviation_factor_ (struct SDHandle *h, unsigned int amount,
191 189
192 if (h->cnt < 2) 190 if (h->cnt < 2)
193 return GNUNET_SYSERR; 191 return GNUNET_SYSERR;
194 if (((float) amount) > h->avg) 192 if (((float)amount) > h->avg)
195 { 193 {
196 diff = ((float) amount) - h->avg; 194 diff = ((float)amount) - h->avg;
197 f = 1; 195 f = 1;
198 } 196 }
199 else 197 else
200 { 198 {
201 diff = h->avg - ((float) amount); 199 diff = h->avg - ((float)amount);
202 f = -1; 200 f = -1;
203 } 201 }
204 diff *= diff; 202 diff *= diff;
205 for (n = 1; n < 4; n++) 203 for (n = 1; n < 4; n++)
206 if (diff < (((double) (n * n)) * h->vr)) 204 if (diff < (((double)(n * n)) * h->vr))
207 break; 205 break;
208 *factor = f * n; 206 *factor = f * n;
209 return GNUNET_OK; 207 return GNUNET_OK;