diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2013-06-21 13:16:22 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2013-06-21 13:16:22 +0000 |
commit | 3ae08734c3357d11a09671ed4fe4e507f24e6f14 (patch) | |
tree | 23ea5674f879a1fe14858f0e52ea257b3b043186 | |
parent | 658350a65fa1f0cfaac11460e7d7db65dcbcb9f2 (diff) | |
download | gnunet-3ae08734c3357d11a09671ed4fe4e507f24e6f14.tar.gz gnunet-3ae08734c3357d11a09671ed4fe4e507f24e6f14.zip |
normalization docu
-rw-r--r-- | src/ats/gnunet-service-ats_normalization.c | 131 | ||||
-rw-r--r-- | src/ats/gnunet-service-ats_normalization.h | 10 |
2 files changed, 111 insertions, 30 deletions
diff --git a/src/ats/gnunet-service-ats_normalization.c b/src/ats/gnunet-service-ats_normalization.c index 97efc02ba..f5dd6f1db 100644 --- a/src/ats/gnunet-service-ats_normalization.c +++ b/src/ats/gnunet-service-ats_normalization.c | |||
@@ -129,14 +129,48 @@ struct PeerRelative | |||
129 | struct GNUNET_PeerIdentity id; | 129 | struct GNUNET_PeerIdentity id; |
130 | }; | 130 | }; |
131 | 131 | ||
132 | |||
133 | /** | ||
134 | * Callback to call on changing preference values | ||
135 | */ | ||
132 | static GAS_Normalization_preference_changed_cb pref_changed_cb; | 136 | static GAS_Normalization_preference_changed_cb pref_changed_cb; |
137 | |||
138 | |||
139 | /** | ||
140 | * Closure for callback to call on changing preference values | ||
141 | */ | ||
133 | static void *pref_changed_cb_cls; | 142 | static void *pref_changed_cb_cls; |
143 | |||
144 | |||
145 | /** | ||
146 | * Hashmap to store peer information | ||
147 | */ | ||
134 | static struct GNUNET_CONTAINER_MultiHashMap *peers; | 148 | static struct GNUNET_CONTAINER_MultiHashMap *peers; |
149 | |||
150 | |||
151 | /** | ||
152 | * Clients in DLL: head | ||
153 | */ | ||
135 | static struct PreferenceClient *pc_head; | 154 | static struct PreferenceClient *pc_head; |
155 | |||
156 | |||
157 | /** | ||
158 | * Clients in DLL: tail | ||
159 | */ | ||
136 | static struct PreferenceClient *pc_tail; | 160 | static struct PreferenceClient *pc_tail; |
161 | |||
162 | |||
163 | /** | ||
164 | * Default values | ||
165 | */ | ||
137 | static struct PeerRelative defvalues; | 166 | static struct PeerRelative defvalues; |
138 | 167 | ||
139 | 168 | ||
169 | /** | ||
170 | * Update a peer | ||
171 | * @param id peer id | ||
172 | * @pram kind the kind | ||
173 | */ | ||
140 | static double | 174 | static double |
141 | update_peers (struct GNUNET_PeerIdentity *id, | 175 | update_peers (struct GNUNET_PeerIdentity *id, |
142 | enum GNUNET_ATS_PreferenceKind kind) | 176 | enum GNUNET_ATS_PreferenceKind kind) |
@@ -169,7 +203,8 @@ update_peers (struct GNUNET_PeerIdentity *id, | |||
169 | } | 203 | } |
170 | 204 | ||
171 | /* Find a client */ | 205 | /* Find a client */ |
172 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u clients have a total relative preference for peer `%s''s `%s' of %.3f\n", | 206 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
207 | "%u clients have a total relative preference for peer `%s''s `%s' of %.3f\n", | ||
173 | count, | 208 | count, |
174 | GNUNET_i2s (id), | 209 | GNUNET_i2s (id), |
175 | GNUNET_ATS_print_preference_type (kind), | 210 | GNUNET_ATS_print_preference_type (kind), |
@@ -199,12 +234,14 @@ update_peers (struct GNUNET_PeerIdentity *id, | |||
199 | return rp->f_rel[kind]; | 234 | return rp->f_rel[kind]; |
200 | } | 235 | } |
201 | 236 | ||
237 | |||
202 | /** | 238 | /** |
203 | * Recalculate preference for a specific ATS property | 239 | * Recalculate preference for a specific ATS property |
204 | * | 240 | * |
205 | * @param c the preference client | 241 | * @param c the preference client |
206 | * @param p the peer | 242 | * @param p the peer |
207 | * @param kind the preference kind | 243 | * @param kind the preference kind |
244 | * @return the result | ||
208 | */ | 245 | */ |
209 | static double | 246 | static double |
210 | recalculate_rel_preferences (struct PreferenceClient *c, | 247 | recalculate_rel_preferences (struct PreferenceClient *c, |
@@ -221,7 +258,8 @@ recalculate_rel_preferences (struct PreferenceClient *c, | |||
221 | c->f_abs_sum[kind] = 0; | 258 | c->f_abs_sum[kind] = 0; |
222 | for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next) | 259 | for (p_cur = c->p_head; NULL != p_cur; p_cur = p_cur->next) |
223 | c->f_abs_sum[kind] += p_cur->f_abs[kind]; | 260 | c->f_abs_sum[kind] += p_cur->f_abs[kind]; |
224 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p has total preference for %s of %.3f\n", | 261 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
262 | "Client %p has total preference for %s of %.3f\n", | ||
225 | c->client, | 263 | c->client, |
226 | GNUNET_ATS_print_preference_type (kind), | 264 | GNUNET_ATS_print_preference_type (kind), |
227 | c->f_abs_sum[kind]); | 265 | c->f_abs_sum[kind]); |
@@ -233,12 +271,15 @@ recalculate_rel_preferences (struct PreferenceClient *c, | |||
233 | /* Calculate relative preference for specific kind */ | 271 | /* Calculate relative preference for specific kind */ |
234 | backup = p_cur->f_rel[kind]; | 272 | backup = p_cur->f_rel[kind]; |
235 | if (DEFAULT_ABS_PREFERENCE == c->f_abs_sum[kind]) | 273 | if (DEFAULT_ABS_PREFERENCE == c->f_abs_sum[kind]) |
236 | /* No peer has a preference for this property, so set default preference */ | 274 | /* No peer has a preference for this property, |
275 | * so set default preference */ | ||
237 | p_cur->f_rel[kind] = DEFAULT_REL_PREFERENCE; | 276 | p_cur->f_rel[kind] = DEFAULT_REL_PREFERENCE; |
238 | else | 277 | else |
239 | p_cur->f_rel[kind] = (c->f_abs_sum[kind] + p_cur->f_abs[kind]) / c->f_abs_sum[kind]; | 278 | p_cur->f_rel[kind] = (c->f_abs_sum[kind] + p_cur->f_abs[kind]) / |
279 | c->f_abs_sum[kind]; | ||
240 | 280 | ||
241 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p: peer `%s' has relative preference for %s of %.3f\n", | 281 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
282 | "Client %p: peer `%s' has relative preference for %s of %.3f\n", | ||
242 | c->client, | 283 | c->client, |
243 | GNUNET_i2s (&p_cur->id), | 284 | GNUNET_i2s (&p_cur->id), |
244 | GNUNET_ATS_print_preference_type (kind), | 285 | GNUNET_ATS_print_preference_type (kind), |
@@ -255,13 +296,21 @@ recalculate_rel_preferences (struct PreferenceClient *c, | |||
255 | else | 296 | else |
256 | { | 297 | { |
257 | /* Value did not chang, return old value*/ | 298 | /* Value did not chang, return old value*/ |
258 | GNUNET_assert (NULL != (rp = GNUNET_CONTAINER_multihashmap_get (peers, &p->id.hashPubKey))); | 299 | GNUNET_assert (NULL != (rp = GNUNET_CONTAINER_multihashmap_get (peers, |
300 | &p->id.hashPubKey))); | ||
259 | ret = rp->f_rel[kind]; | 301 | ret = rp->f_rel[kind]; |
260 | } | 302 | } |
261 | } | 303 | } |
262 | return ret; | 304 | return ret; |
263 | } | 305 | } |
264 | 306 | ||
307 | |||
308 | /** | ||
309 | * Update the absolute preference value for a peer | ||
310 | * @param id peer id | ||
311 | * @param kind the kind | ||
312 | * @return the new relative preference value | ||
313 | */ | ||
265 | static double | 314 | static double |
266 | update_preference (struct PreferenceClient *c, | 315 | update_preference (struct PreferenceClient *c, |
267 | struct PreferencePeer *p, | 316 | struct PreferencePeer *p, |
@@ -284,11 +333,17 @@ update_preference (struct PreferenceClient *c, | |||
284 | return recalculate_rel_preferences (c, p, kind); | 333 | return recalculate_rel_preferences (c, p, kind); |
285 | } | 334 | } |
286 | 335 | ||
336 | |||
337 | /** | ||
338 | * Reduce absolute preferences since they got old | ||
339 | * | ||
340 | * @param cls the PreferencePeer | ||
341 | * @param tc context | ||
342 | */ | ||
287 | static void | 343 | static void |
288 | preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 344 | preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
289 | { | 345 | { |
290 | int i; | 346 | int i; |
291 | //double *t = NULL; | ||
292 | double backup; | 347 | double backup; |
293 | struct PreferencePeer *p = cls; | 348 | struct PreferencePeer *p = cls; |
294 | GNUNET_assert (NULL != p); | 349 | GNUNET_assert (NULL != p); |
@@ -298,18 +353,6 @@ preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
298 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Aging preferences for peer `%s'\n", | 353 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Aging preferences for peer `%s'\n", |
299 | GNUNET_i2s (&p->id)); | 354 | GNUNET_i2s (&p->id)); |
300 | 355 | ||
301 | /* Issue for aging : | ||
302 | * | ||
303 | * Not for every peer preference values are set by default, so reducing the | ||
304 | * absolute preference value does not help for aging because it does not have | ||
305 | * influence on the relative values. | ||
306 | * | ||
307 | * So we have to reduce the relative value to have an immediate impact on | ||
308 | * quota calculation. In addition we cannot call recalculate_preferences here | ||
309 | * but instead reduce the absolute value to have an aging impact on future | ||
310 | * calls to change_preference where recalculate_preferences is called | ||
311 | * | ||
312 | */ | ||
313 | /* Aging absolute values: */ | 356 | /* Aging absolute values: */ |
314 | for (i = 0; i < GNUNET_ATS_PreferenceCount; i++) | 357 | for (i = 0; i < GNUNET_ATS_PreferenceCount; i++) |
315 | { | 358 | { |
@@ -327,6 +370,7 @@ preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
327 | &preference_aging, p); | 370 | &preference_aging, p); |
328 | } | 371 | } |
329 | 372 | ||
373 | |||
330 | /** | 374 | /** |
331 | * Normalize an updated preference value | 375 | * Normalize an updated preference value |
332 | * | 376 | * |
@@ -351,11 +395,12 @@ GAS_normalization_change_preference (void *src, | |||
351 | GNUNET_assert (NULL != src); | 395 | GNUNET_assert (NULL != src); |
352 | GNUNET_assert (NULL != peer); | 396 | GNUNET_assert (NULL != peer); |
353 | 397 | ||
354 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p changes preference for peer `%s' for `%s' to %.2f\n", | 398 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
355 | src, | 399 | "Client %p changes preference for peer `%s' for `%s' to %.2f\n", |
356 | GNUNET_i2s (peer), | 400 | src, |
357 | GNUNET_ATS_print_preference_type (kind), | 401 | GNUNET_i2s (peer), |
358 | score_abs); | 402 | GNUNET_ATS_print_preference_type (kind), |
403 | score_abs); | ||
359 | 404 | ||
360 | if (kind >= GNUNET_ATS_PreferenceCount) | 405 | if (kind >= GNUNET_ATS_PreferenceCount) |
361 | { | 406 | { |
@@ -396,28 +441,34 @@ GAS_normalization_change_preference (void *src, | |||
396 | /* Default value per peer relative preference for a quality: 1.0 */ | 441 | /* Default value per peer relative preference for a quality: 1.0 */ |
397 | p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE; | 442 | p_cur->f_rel[i] = DEFAULT_REL_PREFERENCE; |
398 | } | 443 | } |
399 | p_cur->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL, &preference_aging, p_cur); | 444 | p_cur->aging_task = GNUNET_SCHEDULER_add_delayed (PREF_AGING_INTERVAL, |
445 | &preference_aging, p_cur); | ||
400 | GNUNET_CONTAINER_DLL_insert (c_cur->p_head, c_cur->p_tail, p_cur); | 446 | GNUNET_CONTAINER_DLL_insert (c_cur->p_head, c_cur->p_tail, p_cur); |
401 | } | 447 | } |
402 | 448 | ||
403 | if (NULL == (r_cur = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey))) | 449 | if (NULL == (r_cur = GNUNET_CONTAINER_multihashmap_get (peers, |
450 | &peer->hashPubKey))) | ||
404 | { | 451 | { |
405 | r_cur = GNUNET_malloc (sizeof (struct PeerRelative)); | 452 | r_cur = GNUNET_malloc (sizeof (struct PeerRelative)); |
406 | r_cur->id = (*peer); | 453 | r_cur->id = (*peer); |
407 | for (i = 0; i < GNUNET_ATS_PreferenceCount; i++) | 454 | for (i = 0; i < GNUNET_ATS_PreferenceCount; i++) |
408 | r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE; | 455 | r_cur->f_rel[i] = DEFAULT_REL_PREFERENCE; |
409 | GNUNET_CONTAINER_multihashmap_put (peers, &r_cur->id.hashPubKey, r_cur, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | 456 | GNUNET_CONTAINER_multihashmap_put (peers, &r_cur->id.hashPubKey, |
457 | r_cur, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
410 | } | 458 | } |
411 | 459 | ||
412 | score_rel = update_preference (c_cur, p_cur, kind, score_abs); | 460 | score_rel = update_preference (c_cur, p_cur, kind, score_abs); |
413 | return score_rel; | 461 | return score_rel; |
414 | } | 462 | } |
415 | 463 | ||
464 | |||
416 | /** | 465 | /** |
417 | * Get the normalized preference values for a specific peer | 466 | * Get the normalized preference values for a specific peer or |
467 | * the default values if | ||
418 | * | 468 | * |
419 | * @param id the peer | 469 | * @param id the peer |
420 | * @return pointer to the values, can be indexed with GNUNET_ATS_PreferenceKind, default preferences if peer does not exist | 470 | * @return pointer to the values, can be indexed with GNUNET_ATS_PreferenceKind, |
471 | * default preferences if peer does not exist | ||
421 | */ | 472 | */ |
422 | const double * | 473 | const double * |
423 | GAS_normalization_get_preferences (const struct GNUNET_PeerIdentity *id) | 474 | GAS_normalization_get_preferences (const struct GNUNET_PeerIdentity *id) |
@@ -434,8 +485,15 @@ GAS_normalization_get_preferences (const struct GNUNET_PeerIdentity *id) | |||
434 | } | 485 | } |
435 | 486 | ||
436 | 487 | ||
488 | /** | ||
489 | * Start the normalization component | ||
490 | * | ||
491 | * @param pref_ch_cb callback to call on relative preference changing | ||
492 | * @param ref_ch_cb_cls cls for the callback | ||
493 | */ | ||
437 | void | 494 | void |
438 | GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb, void *pref_ch_cb_cls) | 495 | GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb, |
496 | void *pref_ch_cb_cls) | ||
439 | { | 497 | { |
440 | int i; | 498 | int i; |
441 | peers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO); | 499 | peers = GNUNET_CONTAINER_multihashmap_create(10, GNUNET_NO); |
@@ -446,6 +504,15 @@ GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb, voi | |||
446 | return; | 504 | return; |
447 | } | 505 | } |
448 | 506 | ||
507 | |||
508 | /** | ||
509 | * Free a peer | ||
510 | * | ||
511 | * @param cls unused | ||
512 | * @param key the key | ||
513 | * @param value RelativePeer | ||
514 | * @return GNUNET_OK to continue | ||
515 | */ | ||
449 | static int | 516 | static int |
450 | free_peer (void *cls, | 517 | free_peer (void *cls, |
451 | const struct GNUNET_HashCode * key, | 518 | const struct GNUNET_HashCode * key, |
@@ -457,6 +524,10 @@ free_peer (void *cls, | |||
457 | return GNUNET_OK; | 524 | return GNUNET_OK; |
458 | } | 525 | } |
459 | 526 | ||
527 | |||
528 | /** | ||
529 | * Stop the normalization component and free all items | ||
530 | */ | ||
460 | void | 531 | void |
461 | GAS_normalization_stop () | 532 | GAS_normalization_stop () |
462 | { | 533 | { |
diff --git a/src/ats/gnunet-service-ats_normalization.h b/src/ats/gnunet-service-ats_normalization.h index 78fddbc56..dbd68ee95 100644 --- a/src/ats/gnunet-service-ats_normalization.h +++ b/src/ats/gnunet-service-ats_normalization.h | |||
@@ -64,9 +64,19 @@ GAS_normalization_change_preference (void *src, | |||
64 | enum GNUNET_ATS_PreferenceKind kind, | 64 | enum GNUNET_ATS_PreferenceKind kind, |
65 | float score_abs); | 65 | float score_abs); |
66 | 66 | ||
67 | /** | ||
68 | * Start the normalization component | ||
69 | * | ||
70 | * @param pref_ch_cb callback to call on relative preference changing | ||
71 | * @param ref_ch_cb_cls cls for the callback | ||
72 | */ | ||
67 | void | 73 | void |
68 | GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb, void *pref_ch_cb_cls); | 74 | GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb, void *pref_ch_cb_cls); |
69 | 75 | ||
76 | |||
77 | /** | ||
78 | * Stop the normalization component and free all items | ||
79 | */ | ||
70 | void | 80 | void |
71 | GAS_normalization_stop (); | 81 | GAS_normalization_stop (); |
72 | 82 | ||