{"action":"created","comment":{"url":"https://api.github.com/repos/birarda/hifi/pulls/comments/36544938","id":36544938,"diff_hunk":"@@ -0,0 +1,205 @@\n+//\n+//  CongestionControl.cpp\n+//  libraries/networking/src/udt\n+//\n+//  Created by Clement on 7/23/15.\n+//  Copyright 2015 High Fidelity, Inc.\n+//\n+//  Distributed under the Apache License, Version 2.0.\n+//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html\n+//\n+\n+#include \"CongestionControl.h\"\n+#include \"Packet.h\"\n+\n+using namespace udt;\n+using namespace std::chrono;\n+\n+static const double USECS_PER_SECOND = 1000000.0;\n+\n+void CongestionControl::setPacketSendPeriod(double newSendPeriod) {\n+    Q_ASSERT_X(newSendPeriod >= 0, \"CongestionControl::setPacketPeriod\", \"Can not set a negative packet send period\");\n+    \n+    if (_maxBandwidth > 0) {\n+        // anytime the packet send period is about to be increased, make sure it stays below the minimum period,\n+        // calculated based on the maximum desired bandwidth\n+        double minPacketSendPeriod = USECS_PER_SECOND / (((double) _maxBandwidth) / _mss);\n+        _packetSendPeriod = std::max(newSendPeriod, minPacketSendPeriod);\n+    } else {\n+        _packetSendPeriod = newSendPeriod;\n+    }\n+}\n+\n+void DefaultCC::init() {\n+    _lastRCTime = high_resolution_clock::now();\n+    \n+    _mss = udt::MAX_PACKET_SIZE_WITH_UDP_HEADER;\n+    \n+    _slowStartLastAck = _sendCurrSeqNum;\n+    _lastDecreaseMaxSeq = SequenceNumber { SequenceNumber::MAX };\n+    \n+    _congestionWindowSize = 16.0;\n+    _packetSendPeriod = 1.0;\n+}\n+\n+void DefaultCC::onACK(SequenceNumber ackNum) {\n+    double increase = 0;\n+    \n+    // Note: 1/24/2012\n+    // The minimum increase parameter is increased from \"1.0 / _mss\" to 0.01\n+    // because the original was too small and caused sending rate to stay at low level\n+    // for long time.\n+    const double minimumIncrease = 0.01;\n+    \n+    // we will only adjust once per sync interval so check that it has been at least that long now\n+    auto now = high_resolution_clock::now();\n+    if (duration_cast<microseconds>(now - _lastRCTime).count() < synInterval()) {\n+        return;\n+    }\n+    \n+    // our last rate increase time is now\n+    _lastRCTime = now;\n+\n+    if (_slowStart) {\n+        // we are in slow start phase - increase the congestion window size by the number of packets just ACKed\n+        _congestionWindowSize += seqlen(_slowStartLastAck, ackNum);\n+        \n+        // update the last ACK\n+        _slowStartLastAck = ackNum;\n+        \n+        // check if we can get out of slow start (is our new congestion window size bigger than the max)\n+        if (_congestionWindowSize > _maxCongestionWindowSize) {\n+            _slowStart = false;\n+            \n+            if (_receiveRate > 0) {\n+                // if we have a valid receive rate we set the send period to whatever the receive rate dictates\n+                _packetSendPeriod = USECS_PER_SECOND / _receiveRate;\n+            } else {\n+                // no valid receive rate, packet send period is dictated by estimated RTT and current congestion window size\n+                _packetSendPeriod = (_rtt + synInterval()) / _congestionWindowSize;\n+            }\n+        }\n+    } else {\n+        // not in slow start - window size should be arrival rate * (RTT + SYN) + 16\n+        _congestionWindowSize = _receiveRate / USECS_PER_SECOND * (_rtt + synInterval()) + 16;\n+    }\n+    \n+    // during slow start we perform no rate increases\n+    if (_slowStart) {\n+        return;\n+    }\n+    \n+    // if loss has happened since the last rate increase we do not perform another increase\n+    if (_loss) {\n+        _loss = false;\n+        return;\n+    }\n+    \n+    double capacitySpeedDelta = (_bandwidth - USECS_PER_SECOND / _packetSendPeriod);\n+    \n+    // UDT uses what they call DAIMD - additive increase multiplicative decrease with decreasing increases\n+    // This factor is a protocol parameter that is part of the DAIMD algorithim\n+    static const int AIMD_DECREASING_INCREASE_FACTOR = 9;\n+    \n+    if ((_packetSendPeriod > _lastDecreasePeriod) && ((_bandwidth / AIMD_DECREASING_INCREASE_FACTOR) < capacitySpeedDelta)) {\n+        capacitySpeedDelta = _bandwidth / AIMD_DECREASING_INCREASE_FACTOR;\n+    }\n+    \n+    if (capacitySpeedDelta <= 0) {\n+        increase = minimumIncrease;\n+    } else {\n+        // use UDTs DAIMD algorithm to figure out what the send period increase factor should be\n+        \n+        // inc = max(10 ^ ceil(log10(B * MSS * 8 ) * Beta / MSS, minimumIncrease)\n+        // B = estimated link capacity\n+        // Beta = 1.5 * 10^(-6)\n+        \n+        static const double BETA = 0.0000015;\n+        static const double BITS_PER_BYTE = 8.0;\n+        \n+        increase = pow(10.0, ceil(log10(capacitySpeedDelta * _mss * BITS_PER_BYTE))) * BETA / _mss;\n+        \n+        if (increase < minimumIncrease) {\n+            increase = minimumIncrease;\n+        }\n+    }\n+    \n+    setPacketSendPeriod((_packetSendPeriod * synInterval()) / (_packetSendPeriod * increase + synInterval()));\n+}\n+\n+void DefaultCC::onLoss(SequenceNumber rangeStart, SequenceNumber rangeEnd) {\n+    // stop the slow start if we haven't yet\n+    if (_slowStart) {\n+        stopSlowStart();\n+        \n+        // if the change to send rate was driven by a known receive rate, then we don't continue with the decrease\n+        if (_receiveRate > 0) {\n+            return;\n+        }\n+    }\n+    \n+    _loss = true;\n+    \n+    static const double INTER_PACKET_ARRIVAL_INCREASE = 1.125;\n+    static const int MAX_DECREASES_PER_CONGESTION_EPOCH = 5;\n+    \n+    // check if this NAK starts a new congestion period - this will be the case if the\n+    // NAK received occured for a packet sent after the last decrease\n+    if (rangeStart > _lastDecreaseMaxSeq) {\n+        _lastDecreasePeriod = _packetSendPeriod;\n+        \n+        _packetSendPeriod = ceil(_packetSendPeriod * INTER_PACKET_ARRIVAL_INCREASE);\n+        \n+        // use EWMA to update the average number of NAKs per congestion\n+        static const double NAK_EWMA_ALPHA = 0.125;\n+        _avgNAKNum = (int)ceil(_avgNAKNum * (1 - NAK_EWMA_ALPHA) + _nakCount * NAK_EWMA_ALPHA);\n+        \n+        // update the count of NAKs and count of decreases in this interval\n+        _nakCount = 1;\n+        _decreaseCount = 1;\n+        \n+        _lastDecreaseMaxSeq = _sendCurrSeqNum;\n+        \n+        // avoid synchronous rate decrease across connections using randomization\n+        srand((unsigned) _lastDecreaseMaxSeq);\n+        _randomDecreaseThreshold = (int) ceil(_avgNAKNum * (double(rand()) / RAND_MAX));\n+        \n+        if (_randomDecreaseThreshold < 1) {\n+            _randomDecreaseThreshold = 1;\n+        }\n+        \n+    } else if ((_decreaseCount++ < MAX_DECREASES_PER_CONGESTION_EPOCH) && ((++_nakCount % _randomDecreaseThreshold) == 0)) {\n+        // there have been fewer than MAX_DECREASES_PER_CONGESTION_EPOCH AND this NAK matches the random count at which we\n+        // decided we would decrease the packet send period\n+        \n+        _packetSendPeriod = ceil(_packetSendPeriod * INTER_PACKET_ARRIVAL_INCREASE);\n+        _lastDecreaseMaxSeq = _sendCurrSeqNum;\n+    }\n+}\n+\n+void DefaultCC::onTimeout() {","path":"libraries/networking/src/udt/CongestionControl.cpp","position":180,"original_position":180,"commit_id":"cf277dca332cc4c7d1b93ff208bf00d222024a0b","original_commit_id":"cf277dca332cc4c7d1b93ff208bf00d222024a0b","user":{"login":"birarda","id":724069,"avatar_url":"https://avatars.githubusercontent.com/u/724069?v=3","gravatar_id":"","url":"https://api.github.com/users/birarda","html_url":"https://github.com/birarda","followers_url":"https://api.github.com/users/birarda/followers","following_url":"https://api.github.com/users/birarda/following{/other_user}","gists_url":"https://api.github.com/users/birarda/gists{/gist_id}","starred_url":"https://api.github.com/users/birarda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/birarda/subscriptions","organizations_url":"https://api.github.com/users/birarda/orgs","repos_url":"https://api.github.com/users/birarda/repos","events_url":"https://api.github.com/users/birarda/events{/privacy}","received_events_url":"https://api.github.com/users/birarda/received_events","type":"User","site_admin":false},"body":"should note that we're not currently firing this because we don't have TTL on our packets","created_at":"2015-08-07T18:08:37Z","updated_at":"2015-08-07T18:08:37Z","html_url":"https://github.com/birarda/hifi/pull/12#discussion_r36544938","pull_request_url":"https://api.github.com/repos/birarda/hifi/pulls/12","_links":{"self":{"href":"https://api.github.com/repos/birarda/hifi/pulls/comments/36544938"},"html":{"href":"https://github.com/birarda/hifi/pull/12#discussion_r36544938"},"pull_request":{"href":"https://api.github.com/repos/birarda/hifi/pulls/12"}}},"pull_request":{"url":"https://api.github.com/repos/birarda/hifi/pulls/12","id":40960163,"html_url":"https://github.com/birarda/hifi/pull/12","diff_url":"https://github.com/birarda/hifi/pull/12.diff","patch_url":"https://github.com/birarda/hifi/pull/12.patch","issue_url":"https://api.github.com/repos/birarda/hifi/issues/12","number":12,"state":"open","locked":false,"title":"groundwork for UDT reliable sending","user":{"login":"birarda","id":724069,"avatar_url":"https://avatars.githubusercontent.com/u/724069?v=3","gravatar_id":"","url":"https://api.github.com/users/birarda","html_url":"https://github.com/birarda","followers_url":"https://api.github.com/users/birarda/followers","following_url":"https://api.github.com/users/birarda/following{/other_user}","gists_url":"https://api.github.com/users/birarda/gists{/gist_id}","starred_url":"https://api.github.com/users/birarda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/birarda/subscriptions","organizations_url":"https://api.github.com/users/birarda/orgs","repos_url":"https://api.github.com/users/birarda/repos","events_url":"https://api.github.com/users/birarda/events{/privacy}","received_events_url":"https://api.github.com/users/birarda/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-07-27T23:13:08Z","updated_at":"2015-08-07T18:08:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c44bc1dfd0b53b1fbd0575d890c2ec06425b2373","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/birarda/hifi/pulls/12/commits","review_comments_url":"https://api.github.com/repos/birarda/hifi/pulls/12/comments","review_comment_url":"https://api.github.com/repos/birarda/hifi/pulls/comments{/number}","comments_url":"https://api.github.com/repos/birarda/hifi/issues/12/comments","statuses_url":"https://api.github.com/repos/birarda/hifi/statuses/cf277dca332cc4c7d1b93ff208bf00d222024a0b","head":{"label":"birarda:atp","ref":"atp","sha":"cf277dca332cc4c7d1b93ff208bf00d222024a0b","user":{"login":"birarda","id":724069,"avatar_url":"https://avatars.githubusercontent.com/u/724069?v=3","gravatar_id":"","url":"https://api.github.com/users/birarda","html_url":"https://github.com/birarda","followers_url":"https://api.github.com/users/birarda/followers","following_url":"https://api.github.com/users/birarda/following{/other_user}","gists_url":"https://api.github.com/users/birarda/gists{/gist_id}","starred_url":"https://api.github.com/users/birarda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/birarda/subscriptions","organizations_url":"https://api.github.com/users/birarda/orgs","repos_url":"https://api.github.com/users/birarda/repos","events_url":"https://api.github.com/users/birarda/events{/privacy}","received_events_url":"https://api.github.com/users/birarda/received_events","type":"User","site_admin":false},"repo":{"id":9376753,"name":"hifi","full_name":"birarda/hifi","owner":{"login":"birarda","id":724069,"avatar_url":"https://avatars.githubusercontent.com/u/724069?v=3","gravatar_id":"","url":"https://api.github.com/users/birarda","html_url":"https://github.com/birarda","followers_url":"https://api.github.com/users/birarda/followers","following_url":"https://api.github.com/users/birarda/following{/other_user}","gists_url":"https://api.github.com/users/birarda/gists{/gist_id}","starred_url":"https://api.github.com/users/birarda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/birarda/subscriptions","organizations_url":"https://api.github.com/users/birarda/orgs","repos_url":"https://api.github.com/users/birarda/repos","events_url":"https://api.github.com/users/birarda/events{/privacy}","received_events_url":"https://api.github.com/users/birarda/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/birarda/hifi","description":"Test platform for various render and interface tests for next-gen VR system","fork":true,"url":"https://api.github.com/repos/birarda/hifi","forks_url":"https://api.github.com/repos/birarda/hifi/forks","keys_url":"https://api.github.com/repos/birarda/hifi/keys{/key_id}","collaborators_url":"https://api.github.com/repos/birarda/hifi/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/birarda/hifi/teams","hooks_url":"https://api.github.com/repos/birarda/hifi/hooks","issue_events_url":"https://api.github.com/repos/birarda/hifi/issues/events{/number}","events_url":"https://api.github.com/repos/birarda/hifi/events","assignees_url":"https://api.github.com/repos/birarda/hifi/assignees{/user}","branches_url":"https://api.github.com/repos/birarda/hifi/branches{/branch}","tags_url":"https://api.github.com/repos/birarda/hifi/tags","blobs_url":"https://api.github.com/repos/birarda/hifi/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/birarda/hifi/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/birarda/hifi/git/refs{/sha}","trees_url":"https://api.github.com/repos/birarda/hifi/git/trees{/sha}","statuses_url":"https://api.github.com/repos/birarda/hifi/statuses/{sha}","languages_url":"https://api.github.com/repos/birarda/hifi/languages","stargazers_url":"https://api.github.com/repos/birarda/hifi/stargazers","contributors_url":"https://api.github.com/repos/birarda/hifi/contributors","subscribers_url":"https://api.github.com/repos/birarda/hifi/subscribers","subscription_url":"https://api.github.com/repos/birarda/hifi/subscription","commits_url":"https://api.github.com/repos/birarda/hifi/commits{/sha}","git_commits_url":"https://api.github.com/repos/birarda/hifi/git/commits{/sha}","comments_url":"https://api.github.com/repos/birarda/hifi/comments{/number}","issue_comment_url":"https://api.github.com/repos/birarda/hifi/issues/comments{/number}","contents_url":"https://api.github.com/repos/birarda/hifi/contents/{+path}","compare_url":"https://api.github.com/repos/birarda/hifi/compare/{base}...{head}","merges_url":"https://api.github.com/repos/birarda/hifi/merges","archive_url":"https://api.github.com/repos/birarda/hifi/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/birarda/hifi/downloads","issues_url":"https://api.github.com/repos/birarda/hifi/issues{/number}","pulls_url":"https://api.github.com/repos/birarda/hifi/pulls{/number}","milestones_url":"https://api.github.com/repos/birarda/hifi/milestones{/number}","notifications_url":"https://api.github.com/repos/birarda/hifi/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/birarda/hifi/labels{/name}","releases_url":"https://api.github.com/repos/birarda/hifi/releases{/id}","created_at":"2013-04-11T17:58:46Z","updated_at":"2015-05-08T19:10:20Z","pushed_at":"2015-08-06T17:23:46Z","git_url":"git://github.com/birarda/hifi.git","ssh_url":"git@github.com:birarda/hifi.git","clone_url":"https://github.com/birarda/hifi.git","svn_url":"https://github.com/birarda/hifi","homepage":null,"size":173366,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"birarda:protocol","ref":"protocol","sha":"052978898e49a02b5e6d84ecf39cf33993a32ffe","user":{"login":"birarda","id":724069,"avatar_url":"https://avatars.githubusercontent.com/u/724069?v=3","gravatar_id":"","url":"https://api.github.com/users/birarda","html_url":"https://github.com/birarda","followers_url":"https://api.github.com/users/birarda/followers","following_url":"https://api.github.com/users/birarda/following{/other_user}","gists_url":"https://api.github.com/users/birarda/gists{/gist_id}","starred_url":"https://api.github.com/users/birarda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/birarda/subscriptions","organizations_url":"https://api.github.com/users/birarda/orgs","repos_url":"https://api.github.com/users/birarda/repos","events_url":"https://api.github.com/users/birarda/events{/privacy}","received_events_url":"https://api.github.com/users/birarda/received_events","type":"User","site_admin":false},"repo":{"id":9376753,"name":"hifi","full_name":"birarda/hifi","owner":{"login":"birarda","id":724069,"avatar_url":"https://avatars.githubusercontent.com/u/724069?v=3","gravatar_id":"","url":"https://api.github.com/users/birarda","html_url":"https://github.com/birarda","followers_url":"https://api.github.com/users/birarda/followers","following_url":"https://api.github.com/users/birarda/following{/other_user}","gists_url":"https://api.github.com/users/birarda/gists{/gist_id}","starred_url":"https://api.github.com/users/birarda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/birarda/subscriptions","organizations_url":"https://api.github.com/users/birarda/orgs","repos_url":"https://api.github.com/users/birarda/repos","events_url":"https://api.github.com/users/birarda/events{/privacy}","received_events_url":"https://api.github.com/users/birarda/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/birarda/hifi","description":"Test platform for various render and interface tests for next-gen VR system","fork":true,"url":"https://api.github.com/repos/birarda/hifi","forks_url":"https://api.github.com/repos/birarda/hifi/forks","keys_url":"https://api.github.com/repos/birarda/hifi/keys{/key_id}","collaborators_url":"https://api.github.com/repos/birarda/hifi/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/birarda/hifi/teams","hooks_url":"https://api.github.com/repos/birarda/hifi/hooks","issue_events_url":"https://api.github.com/repos/birarda/hifi/issues/events{/number}","events_url":"https://api.github.com/repos/birarda/hifi/events","assignees_url":"https://api.github.com/repos/birarda/hifi/assignees{/user}","branches_url":"https://api.github.com/repos/birarda/hifi/branches{/branch}","tags_url":"https://api.github.com/repos/birarda/hifi/tags","blobs_url":"https://api.github.com/repos/birarda/hifi/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/birarda/hifi/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/birarda/hifi/git/refs{/sha}","trees_url":"https://api.github.com/repos/birarda/hifi/git/trees{/sha}","statuses_url":"https://api.github.com/repos/birarda/hifi/statuses/{sha}","languages_url":"https://api.github.com/repos/birarda/hifi/languages","stargazers_url":"https://api.github.com/repos/birarda/hifi/stargazers","contributors_url":"https://api.github.com/repos/birarda/hifi/contributors","subscribers_url":"https://api.github.com/repos/birarda/hifi/subscribers","subscription_url":"https://api.github.com/repos/birarda/hifi/subscription","commits_url":"https://api.github.com/repos/birarda/hifi/commits{/sha}","git_commits_url":"https://api.github.com/repos/birarda/hifi/git/commits{/sha}","comments_url":"https://api.github.com/repos/birarda/hifi/comments{/number}","issue_comment_url":"https://api.github.com/repos/birarda/hifi/issues/comments{/number}","contents_url":"https://api.github.com/repos/birarda/hifi/contents/{+path}","compare_url":"https://api.github.com/repos/birarda/hifi/compare/{base}...{head}","merges_url":"https://api.github.com/repos/birarda/hifi/merges","archive_url":"https://api.github.com/repos/birarda/hifi/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/birarda/hifi/downloads","issues_url":"https://api.github.com/repos/birarda/hifi/issues{/number}","pulls_url":"https://api.github.com/repos/birarda/hifi/pulls{/number}","milestones_url":"https://api.github.com/repos/birarda/hifi/milestones{/number}","notifications_url":"https://api.github.com/repos/birarda/hifi/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/birarda/hifi/labels{/name}","releases_url":"https://api.github.com/repos/birarda/hifi/releases{/id}","created_at":"2013-04-11T17:58:46Z","updated_at":"2015-05-08T19:10:20Z","pushed_at":"2015-08-06T17:23:46Z","git_url":"git://github.com/birarda/hifi.git","ssh_url":"git@github.com:birarda/hifi.git","clone_url":"https://github.com/birarda/hifi.git","svn_url":"https://github.com/birarda/hifi","homepage":null,"size":173366,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/birarda/hifi/pulls/12"},"html":{"href":"https://github.com/birarda/hifi/pull/12"},"issue":{"href":"https://api.github.com/repos/birarda/hifi/issues/12"},"comments":{"href":"https://api.github.com/repos/birarda/hifi/issues/12/comments"},"review_comments":{"href":"https://api.github.com/repos/birarda/hifi/pulls/12/comments"},"review_comment":{"href":"https://api.github.com/repos/birarda/hifi/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/birarda/hifi/pulls/12/commits"},"statuses":{"href":"https://api.github.com/repos/birarda/hifi/statuses/cf277dca332cc4c7d1b93ff208bf00d222024a0b"}}}}