diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2013-06-24 15:06:45 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2013-06-24 15:06:45 +0000 |
commit | 7ba01a86dfa31c8e4b97e099437fbbfba32d191b (patch) | |
tree | 9cba59bb369223f840dbc55cd68f1095c10ad92f /src/ats/gnunet-service-ats_normalization.c | |
parent | 1e007a89197769e4e0f5ad36b583689a27f33cac (diff) | |
download | gnunet-7ba01a86dfa31c8e4b97e099437fbbfba32d191b.tar.gz gnunet-7ba01a86dfa31c8e4b97e099437fbbfba32d191b.zip |
implemented ats property normalization with callback to address
to do: recalculate min/max to prevent outdated max values have an impact on normalization
Diffstat (limited to 'src/ats/gnunet-service-ats_normalization.c')
-rw-r--r-- | src/ats/gnunet-service-ats_normalization.c | 125 |
1 files changed, 87 insertions, 38 deletions
diff --git a/src/ats/gnunet-service-ats_normalization.c b/src/ats/gnunet-service-ats_normalization.c index f5dbf04c9..84a07b0d8 100644 --- a/src/ats/gnunet-service-ats_normalization.c +++ b/src/ats/gnunet-service-ats_normalization.c | |||
@@ -29,6 +29,7 @@ | |||
29 | #include "gnunet-service-ats_addresses.h" | 29 | #include "gnunet-service-ats_addresses.h" |
30 | #include "gnunet-service-ats_normalization.h" | 30 | #include "gnunet-service-ats_normalization.h" |
31 | 31 | ||
32 | #define LOG(kind,...) GNUNET_log_from (kind, "ats-normalization",__VA_ARGS__) | ||
32 | 33 | ||
33 | 34 | ||
34 | /** | 35 | /** |
@@ -144,6 +145,17 @@ static void *pref_changed_cb_cls; | |||
144 | 145 | ||
145 | 146 | ||
146 | /** | 147 | /** |
148 | * Callback to call on changing property values | ||
149 | */ | ||
150 | GAS_Normalization_property_changed_cb prop_ch_cb; | ||
151 | |||
152 | /** | ||
153 | * Closure for callback to call on changing property values | ||
154 | */ | ||
155 | void *prop_ch_cb_cls; | ||
156 | |||
157 | |||
158 | /** | ||
147 | * Hashmap to store peer information for preference normalization | 159 | * Hashmap to store peer information for preference normalization |
148 | */ | 160 | */ |
149 | static struct GNUNET_CONTAINER_MultiHashMap *preference_peers; | 161 | static struct GNUNET_CONTAINER_MultiHashMap *preference_peers; |
@@ -395,13 +407,12 @@ preference_aging (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
395 | * @param kind the kind to change the preference | 407 | * @param kind the kind to change the preference |
396 | * @param score_abs the normalized score | 408 | * @param score_abs the normalized score |
397 | */ | 409 | */ |
398 | float | 410 | void |
399 | GAS_normalization_normalize_preference (void *src, | 411 | GAS_normalization_normalize_preference (void *src, |
400 | const struct GNUNET_PeerIdentity *peer, | 412 | const struct GNUNET_PeerIdentity *peer, |
401 | enum GNUNET_ATS_PreferenceKind kind, | 413 | enum GNUNET_ATS_PreferenceKind kind, |
402 | float score_abs) | 414 | float score_abs) |
403 | { | 415 | { |
404 | float score_rel; | ||
405 | struct PreferenceClient *c_cur; | 416 | struct PreferenceClient *c_cur; |
406 | struct PreferencePeer *p_cur; | 417 | struct PreferencePeer *p_cur; |
407 | struct PeerRelative *r_cur; | 418 | struct PeerRelative *r_cur; |
@@ -421,7 +432,7 @@ GAS_normalization_normalize_preference (void *src, | |||
421 | if (kind >= GNUNET_ATS_PreferenceCount) | 432 | if (kind >= GNUNET_ATS_PreferenceCount) |
422 | { | 433 | { |
423 | GNUNET_break (0); | 434 | GNUNET_break (0); |
424 | return 0.0; | 435 | return; |
425 | } | 436 | } |
426 | 437 | ||
427 | /* Find preference client */ | 438 | /* Find preference client */ |
@@ -472,9 +483,7 @@ GAS_normalization_normalize_preference (void *src, | |||
472 | GNUNET_CONTAINER_multihashmap_put (preference_peers, &r_cur->id.hashPubKey, | 483 | GNUNET_CONTAINER_multihashmap_put (preference_peers, &r_cur->id.hashPubKey, |
473 | r_cur, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | 484 | r_cur, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); |
474 | } | 485 | } |
475 | 486 | update_preference (c_cur, p_cur, kind, score_abs); | |
476 | score_rel = update_preference (c_cur, p_cur, kind, score_abs); | ||
477 | return score_rel; | ||
478 | } | 487 | } |
479 | 488 | ||
480 | 489 | ||
@@ -521,14 +530,15 @@ struct Property properties[GNUNET_ATS_QualityPropertiesCount]; | |||
521 | * @return the new average or GNUNET_ATS_VALUE_UNDEFINED | 530 | * @return the new average or GNUNET_ATS_VALUE_UNDEFINED |
522 | */ | 531 | */ |
523 | 532 | ||
524 | uint32_t property_average (struct ATS_Address *address, | 533 | uint32_t |
525 | const struct GNUNET_ATS_Information *atsi) | 534 | property_average (struct ATS_Address *address, |
535 | const struct GNUNET_ATS_Information *atsi) | ||
526 | { | 536 | { |
527 | struct GAS_NormalizationInfo *ni; | 537 | struct GAS_NormalizationInfo *ni; |
528 | uint32_t current_type; | 538 | uint32_t current_type; |
529 | uint32_t current_val; | 539 | uint32_t current_val; |
530 | 540 | uint32_t res; | |
531 | uint32_t sum; | 541 | uint64_t sum; |
532 | uint32_t count; | 542 | uint32_t count; |
533 | unsigned int c1; | 543 | unsigned int c1; |
534 | unsigned int index; | 544 | unsigned int index; |
@@ -557,6 +567,7 @@ uint32_t property_average (struct ATS_Address *address, | |||
557 | ni->index = 0; | 567 | ni->index = 0; |
558 | 568 | ||
559 | count = 0; | 569 | count = 0; |
570 | sum = 0; | ||
560 | for (c1 = 0; c1 < GAS_normalization_queue_length; c1++) | 571 | for (c1 = 0; c1 < GAS_normalization_queue_length; c1++) |
561 | { | 572 | { |
562 | if (GNUNET_ATS_VALUE_UNDEFINED != ni->atsi_abs[c1]) | 573 | if (GNUNET_ATS_VALUE_UNDEFINED != ni->atsi_abs[c1]) |
@@ -572,52 +583,84 @@ uint32_t property_average (struct ATS_Address *address, | |||
572 | } | 583 | } |
573 | } | 584 | } |
574 | GNUNET_assert (0 != count); | 585 | GNUNET_assert (0 != count); |
575 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New average from %u elements: %u\n", count, sum / count); | 586 | res = sum / count; |
576 | return 0; | 587 | LOG (GNUNET_ERROR_TYPE_DEBUG, "New average of `%s' created by adding %u from %u elements: %u\n", |
588 | GNUNET_ATS_print_property_type(current_type), | ||
589 | current_val, count, res , sum); | ||
590 | ni->avg = res; | ||
591 | return res; | ||
577 | } | 592 | } |
578 | 593 | ||
579 | double property_normalize (struct Property *p, | 594 | /** |
580 | struct ATS_Address *address, | 595 | * Normalize avg_value to a range of values between [1.0, 2.0] |
581 | uint32_t type, | 596 | * based on min max values currently known. |
582 | uint32_t avg_value) | 597 | * |
598 | * @param property p the property | ||
599 | * @param address the address | ||
600 | * @param type the atsi type | ||
601 | * @param avg_value the value to normalize | ||
602 | */ | ||
603 | |||
604 | static void | ||
605 | property_normalize (struct Property *p, | ||
606 | struct ATS_Address *address, | ||
607 | uint32_t type, | ||
608 | uint32_t avg_value) | ||
583 | { | 609 | { |
584 | double res; | 610 | double res; |
585 | double delta; | 611 | double delta; |
612 | uint32_t current_min; | ||
613 | |||
586 | /* Normalize the values of this property */ | 614 | /* Normalize the values of this property */ |
587 | if (p->max < avg_value) | 615 | if (avg_value > p->max) |
588 | { | 616 | { |
589 | p->max = avg_value; | 617 | p->max = avg_value; |
590 | if (GNUNET_NO == p->have_max) | 618 | if (GNUNET_NO == p->have_max) |
591 | p->have_max = GNUNET_YES; | 619 | p->have_max = GNUNET_YES; |
592 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 620 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
593 | "New maximum of %u for property %u\n", | 621 | "New maximum of %u for property %s\n", |
594 | p->max, avg_value); | 622 | p->max, GNUNET_ATS_print_property_type (type)); |
595 | } | 623 | } |
596 | if (p->min > avg_value) | 624 | |
625 | if ((avg_value < p->min) && (avg_value < p->max)) | ||
597 | { | 626 | { |
598 | p->min = avg_value; | 627 | p->min = avg_value; |
599 | if (GNUNET_NO == p->have_min) | 628 | if (GNUNET_NO == p->have_min) |
600 | p->have_min = GNUNET_YES; | 629 | p->have_min = GNUNET_YES; |
601 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 630 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
602 | "New minimum of %u for property %u\n", | 631 | "New minimum of %u for property %s\n", |
603 | p->min, avg_value); | 632 | p->min, GNUNET_ATS_print_property_type (type)); |
604 | } | 633 | } |
605 | 634 | ||
606 | if ((GNUNET_YES == p->have_max) && (GNUNET_YES == p->have_min)) | 635 | current_min = p->min; |
636 | if (UINT32_MAX == p->min) | ||
637 | current_min = 0; /* If we do not have a minimum we use 0.0 */ | ||
638 | |||
639 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Normalizing %u: new normalized property `%s' using min=%u max=%u\n", | ||
640 | avg_value, | ||
641 | GNUNET_ATS_print_property_type (type), | ||
642 | current_min, p->max); | ||
643 | |||
644 | if (GNUNET_YES == p->have_max) | ||
607 | { | 645 | { |
608 | delta = p->max - p->min; | 646 | delta = p->max - current_min; |
609 | res = (delta + avg_value) / (delta); | 647 | res = (delta + (avg_value - current_min)) / (delta); |
610 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 648 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
611 | "New normalized value of %f for property %u\n", | 649 | "Peer `%s': New normalized value of %f for property %s\n", |
612 | res, type); | 650 | GNUNET_i2s (&address->peer), res, GNUNET_ATS_print_property_type (type)); |
613 | return res; | 651 | if (NULL != prop_ch_cb) |
652 | prop_ch_cb (prop_ch_cb_cls, address, type, res); | ||
614 | } | 653 | } |
615 | |||
616 | return DEFAULT_REL_QUALITY; | ||
617 | } | 654 | } |
618 | 655 | ||
619 | 656 | ||
620 | 657 | /** | |
658 | * Update and normalize a atsi performance information | ||
659 | * | ||
660 | * @param address the address to update | ||
661 | * @param atsi the array of performance information | ||
662 | * @param atsi_count the number of atsi information in the array | ||
663 | */ | ||
621 | void | 664 | void |
622 | GAS_normalization_normalize_property (struct ATS_Address *address, | 665 | GAS_normalization_normalize_property (struct ATS_Address *address, |
623 | const struct GNUNET_ATS_Information *atsi, | 666 | const struct GNUNET_ATS_Information *atsi, |
@@ -633,6 +676,9 @@ GAS_normalization_normalize_property (struct ATS_Address *address, | |||
633 | GNUNET_assert (NULL != address); | 676 | GNUNET_assert (NULL != address); |
634 | GNUNET_assert (NULL != atsi); | 677 | GNUNET_assert (NULL != atsi); |
635 | 678 | ||
679 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Updating %u elements for peer `%s'\n", | ||
680 | atsi_count, GNUNET_i2s (&address->peer)); | ||
681 | |||
636 | for (c1 = 0; c1 < atsi_count; c1++) | 682 | for (c1 = 0; c1 < atsi_count; c1++) |
637 | { | 683 | { |
638 | current_type = ntohl (atsi[c1].type); | 684 | current_type = ntohl (atsi[c1].type); |
@@ -648,7 +694,6 @@ GAS_normalization_normalize_property (struct ATS_Address *address, | |||
648 | /* Invalid property, continue with next element */ | 694 | /* Invalid property, continue with next element */ |
649 | continue; | 695 | continue; |
650 | } | 696 | } |
651 | |||
652 | /* Averaging */ | 697 | /* Averaging */ |
653 | current_val = property_average (address, &atsi[c1]); | 698 | current_val = property_average (address, &atsi[c1]); |
654 | if (GNUNET_ATS_VALUE_UNDEFINED == current_val) | 699 | if (GNUNET_ATS_VALUE_UNDEFINED == current_val) |
@@ -662,7 +707,6 @@ GAS_normalization_normalize_property (struct ATS_Address *address, | |||
662 | cur_prop = &properties[c2]; | 707 | cur_prop = &properties[c2]; |
663 | property_normalize (cur_prop, address, ntohl(atsi[c1].type), current_val); | 708 | property_normalize (cur_prop, address, ntohl(atsi[c1].type), current_val); |
664 | } | 709 | } |
665 | |||
666 | } | 710 | } |
667 | 711 | ||
668 | 712 | ||
@@ -676,7 +720,9 @@ GAS_normalization_normalize_property (struct ATS_Address *address, | |||
676 | */ | 720 | */ |
677 | void | 721 | void |
678 | GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb, | 722 | GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb, |
679 | void *pref_ch_cb_cls) | 723 | void *pref_ch_cb_cls, |
724 | GAS_Normalization_property_changed_cb property_ch_cb, | ||
725 | void *property_ch_cb_cls) | ||
680 | { | 726 | { |
681 | int c1; | 727 | int c1; |
682 | int i; | 728 | int i; |
@@ -685,7 +731,7 @@ GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb, | |||
685 | 731 | ||
686 | for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++) | 732 | for (c1 = 0; c1 < GNUNET_ATS_QualityPropertiesCount; c1++) |
687 | { | 733 | { |
688 | properties[c1].min = 0; | 734 | properties[c1].min = UINT32_MAX; |
689 | properties[c1].max = 0; | 735 | properties[c1].max = 0; |
690 | properties[c1].have_max = GNUNET_NO; | 736 | properties[c1].have_max = GNUNET_NO; |
691 | properties[c1].have_min = GNUNET_NO; | 737 | properties[c1].have_min = GNUNET_NO; |
@@ -693,6 +739,9 @@ GAS_normalization_start (GAS_Normalization_preference_changed_cb pref_ch_cb, | |||
693 | 739 | ||
694 | pref_changed_cb = pref_ch_cb; | 740 | pref_changed_cb = pref_ch_cb; |
695 | pref_changed_cb_cls = pref_ch_cb_cls; | 741 | pref_changed_cb_cls = pref_ch_cb_cls; |
742 | prop_ch_cb = property_ch_cb; | ||
743 | prop_ch_cb_cls = pref_ch_cb_cls; | ||
744 | |||
696 | for (i = 0; i < GNUNET_ATS_PreferenceCount; i++) | 745 | for (i = 0; i < GNUNET_ATS_PreferenceCount; i++) |
697 | defvalues.f_rel[i] = DEFAULT_REL_PREFERENCE; | 746 | defvalues.f_rel[i] = DEFAULT_REL_PREFERENCE; |
698 | return; | 747 | return; |