Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Junxiao Shi | 6316220 | 2015-01-14 22:27:33 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 25 | |
| 26 | #include "ncc-strategy.hpp" |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 27 | #include "pit-algorithm.hpp" |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 28 | #include "core/random.hpp" |
| 29 | #include <boost/random/uniform_int_distribution.hpp> |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
| 32 | namespace fw { |
| 33 | |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 34 | const Name NccStrategy::STRATEGY_NAME("ndn:/localhost/nfd/strategy/ncc/%FD%01"); |
Junxiao Shi | faf3eb0 | 2015-02-16 10:50:36 -0700 | [diff] [blame] | 35 | NFD_REGISTER_STRATEGY(NccStrategy); |
Junxiao Shi | f3c0781 | 2014-03-11 21:48:49 -0700 | [diff] [blame] | 36 | |
| 37 | NccStrategy::NccStrategy(Forwarder& forwarder, const Name& name) |
| 38 | : Strategy(forwarder, name) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 42 | const time::microseconds NccStrategy::DEFER_FIRST_WITHOUT_BEST_FACE = time::microseconds(4000); |
| 43 | const time::microseconds NccStrategy::DEFER_RANGE_WITHOUT_BEST_FACE = time::microseconds(75000); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 44 | const time::nanoseconds NccStrategy::MEASUREMENTS_LIFETIME = time::seconds(16); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 45 | |
| 46 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 47 | NccStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest, |
| 48 | const shared_ptr<pit::Entry>& pitEntry) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 49 | { |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 50 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
| 51 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 52 | if (nexthops.size() == 0) { |
| 53 | this->rejectPendingInterest(pitEntry); |
| 54 | return; |
| 55 | } |
| 56 | |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 57 | shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getOrCreateStrategyInfo<PitEntryInfo>(); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 58 | bool isNewPitEntry = !hasPendingOutRecords(*pitEntry); |
Junxiao Shi | 8bfd56d | 2014-10-08 10:06:00 -0700 | [diff] [blame] | 59 | if (!isNewPitEntry) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 60 | return; |
| 61 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 62 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 63 | MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 64 | |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 65 | time::microseconds deferFirst = DEFER_FIRST_WITHOUT_BEST_FACE; |
| 66 | time::microseconds deferRange = DEFER_RANGE_WITHOUT_BEST_FACE; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 67 | size_t nUpstreams = nexthops.size(); |
| 68 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 69 | shared_ptr<Face> bestFace = meInfo.getBestFace(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 70 | if (bestFace != nullptr && fibEntry.hasNextHop(*bestFace) && |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 71 | canForwardToLegacy(*pitEntry, *bestFace)) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 72 | // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ? |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 73 | deferFirst = meInfo.prediction; |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 74 | deferRange = time::microseconds((deferFirst.count() + 1) / 2); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 75 | --nUpstreams; |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 76 | this->sendInterest(pitEntry, *bestFace); |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 77 | pitEntryInfo->bestFaceTimeout = scheduler::schedule( |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 78 | meInfo.prediction, |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 79 | bind(&NccStrategy::timeoutOnBestFace, this, weak_ptr<pit::Entry>(pitEntry))); |
| 80 | } |
| 81 | else { |
Junxiao Shi | 6316220 | 2015-01-14 22:27:33 -0700 | [diff] [blame] | 82 | // use first eligible nexthop |
| 83 | auto firstEligibleNexthop = std::find_if(nexthops.begin(), nexthops.end(), |
| 84 | [&pitEntry] (const fib::NextHop& nexthop) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 85 | return canForwardToLegacy(*pitEntry, nexthop.getFace()); |
Junxiao Shi | 6316220 | 2015-01-14 22:27:33 -0700 | [diff] [blame] | 86 | }); |
| 87 | if (firstEligibleNexthop != nexthops.end()) { |
| 88 | this->sendInterest(pitEntry, firstEligibleNexthop->getFace()); |
| 89 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 92 | shared_ptr<Face> previousFace = meInfo.previousFace.lock(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 93 | if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) && |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 94 | canForwardToLegacy(*pitEntry, *previousFace)) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 95 | --nUpstreams; |
| 96 | } |
| 97 | |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 98 | if (nUpstreams > 0) { |
| 99 | pitEntryInfo->maxInterval = std::max(time::microseconds(1), |
| 100 | time::microseconds((2 * deferRange.count() + nUpstreams - 1) / nUpstreams)); |
| 101 | } |
Junxiao Shi | cbb490a | 2014-08-13 12:24:24 -0700 | [diff] [blame] | 102 | else { |
| 103 | // Normally, maxInterval is unused if there aren't any face beyond best and previousBest. |
| 104 | // However, in case FIB entry gains a new nexthop before doPropagate executes (bug 1853), |
| 105 | // this maxInterval would be used to determine when the next doPropagate would happen. |
| 106 | pitEntryInfo->maxInterval = deferFirst; |
| 107 | } |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 108 | pitEntryInfo->propagateTimer = scheduler::schedule(deferFirst, |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 109 | bind(&NccStrategy::doPropagate, this, weak_ptr<pit::Entry>(pitEntry))); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 113 | NccStrategy::doPropagate(weak_ptr<pit::Entry> pitEntryWeak) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 114 | { |
| 115 | shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock(); |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 116 | if (pitEntry == nullptr) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 119 | const fib::Entry& fibEntry = this->lookupFib(*pitEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 120 | |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 121 | shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>(); |
| 122 | // pitEntryInfo is guaranteed to exist here, because doPropagate is triggered |
| 123 | // from a timer set by NccStrategy. |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 124 | BOOST_ASSERT(pitEntryInfo != nullptr); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 125 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 126 | MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 127 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 128 | shared_ptr<Face> previousFace = meInfo.previousFace.lock(); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 129 | if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) && |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 130 | canForwardToLegacy(*pitEntry, *previousFace)) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 131 | this->sendInterest(pitEntry, *previousFace); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Junxiao Shi | 8d84314 | 2016-07-11 22:42:42 +0000 | [diff] [blame] | 134 | const fib::NextHopList& nexthops = fibEntry.getNextHops(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 135 | bool isForwarded = false; |
| 136 | for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) { |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 137 | Face& face = it->getFace(); |
| 138 | if (canForwardToLegacy(*pitEntry, face)) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 139 | isForwarded = true; |
| 140 | this->sendInterest(pitEntry, face); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (isForwarded) { |
Junxiao Shi | cbb490a | 2014-08-13 12:24:24 -0700 | [diff] [blame] | 146 | boost::random::uniform_int_distribution<time::nanoseconds::rep> dist(0, |
| 147 | pitEntryInfo->maxInterval.count() - 1); |
Junxiao Shi | af6569a | 2014-06-14 00:01:34 -0700 | [diff] [blame] | 148 | time::nanoseconds deferNext = time::nanoseconds(dist(getGlobalRng())); |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 149 | pitEntryInfo->propagateTimer = scheduler::schedule(deferNext, |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 150 | bind(&NccStrategy::doPropagate, this, weak_ptr<pit::Entry>(pitEntry))); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | NccStrategy::timeoutOnBestFace(weak_ptr<pit::Entry> pitEntryWeak) |
| 156 | { |
| 157 | shared_ptr<pit::Entry> pitEntry = pitEntryWeak.lock(); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 158 | if (pitEntry == nullptr) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 159 | return; |
| 160 | } |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 161 | measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 162 | |
| 163 | for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 164 | if (measurementsEntry == nullptr) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 165 | // going out of this strategy's namespace |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 166 | break; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 167 | } |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 168 | this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 169 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 170 | MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry); |
| 171 | meInfo.adjustPredictUp(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 172 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 173 | measurementsEntry = this->getMeasurements().getParent(*measurementsEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | void |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 178 | NccStrategy::beforeSatisfyInterest(const shared_ptr<pit::Entry>& pitEntry, |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 179 | const Face& inFace, const Data& data) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 180 | { |
Junxiao Shi | 4846f37 | 2016-04-05 13:39:30 -0700 | [diff] [blame] | 181 | if (!pitEntry->hasInRecords()) { |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 182 | // PIT entry has already been satisfied (and is now waiting for straggler timer to expire) |
| 183 | // NCC does not collect measurements for non-best face |
| 184 | return; |
| 185 | } |
| 186 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 187 | measurements::Entry* measurementsEntry = this->getMeasurements().get(*pitEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 188 | |
| 189 | for (int i = 0; i < UPDATE_MEASUREMENTS_N_LEVELS; ++i) { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 190 | if (measurementsEntry == nullptr) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 191 | // going out of this strategy's namespace |
| 192 | return; |
| 193 | } |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 194 | this->getMeasurements().extendLifetime(*measurementsEntry, MEASUREMENTS_LIFETIME); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 195 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 196 | MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(measurementsEntry); |
| 197 | meInfo.updateBestFace(inFace); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 198 | |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 199 | measurementsEntry = this->getMeasurements().getParent(*measurementsEntry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 202 | shared_ptr<PitEntryInfo> pitEntryInfo = pitEntry->getStrategyInfo<PitEntryInfo>(); |
Hila Ben Abraham | 39a79be | 2016-03-30 22:00:55 -0700 | [diff] [blame] | 203 | if (pitEntryInfo != nullptr) { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 204 | scheduler::cancel(pitEntryInfo->propagateTimer); |
Hila Ben Abraham | 39a79be | 2016-03-30 22:00:55 -0700 | [diff] [blame] | 205 | |
| 206 | // Verify that the best face satisfied the interest before canceling the timeout call |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 207 | MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry); |
| 208 | shared_ptr<Face> bestFace = meInfo.getBestFace(); |
Hila Ben Abraham | 39a79be | 2016-03-30 22:00:55 -0700 | [diff] [blame] | 209 | |
| 210 | if (bestFace.get() == &inFace) |
| 211 | scheduler::cancel(pitEntryInfo->bestFaceTimeout); |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 212 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 215 | NccStrategy::MeasurementsEntryInfo& |
Junxiao Shi | 15e98b0 | 2016-08-12 11:21:44 +0000 | [diff] [blame] | 216 | NccStrategy::getMeasurementsEntryInfo(const shared_ptr<pit::Entry>& entry) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 217 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 218 | measurements::Entry* measurementsEntry = this->getMeasurements().get(*entry); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 219 | return this->getMeasurementsEntryInfo(measurementsEntry); |
| 220 | } |
| 221 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 222 | NccStrategy::MeasurementsEntryInfo& |
| 223 | NccStrategy::getMeasurementsEntryInfo(measurements::Entry* entry) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 224 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 225 | BOOST_ASSERT(entry != nullptr); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 226 | shared_ptr<MeasurementsEntryInfo> info = entry->getStrategyInfo<MeasurementsEntryInfo>(); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 227 | if (info != nullptr) { |
| 228 | return *info; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | info = make_shared<MeasurementsEntryInfo>(); |
| 232 | entry->setStrategyInfo(info); |
| 233 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 234 | measurements::Entry* parentEntry = this->getMeasurements().getParent(*entry); |
| 235 | if (parentEntry != nullptr) { |
| 236 | MeasurementsEntryInfo& parentInfo = this->getMeasurementsEntryInfo(parentEntry); |
| 237 | info->inheritFrom(parentInfo); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 240 | return *info; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 244 | const time::microseconds NccStrategy::MeasurementsEntryInfo::INITIAL_PREDICTION = |
| 245 | time::microseconds(8192); |
| 246 | const time::microseconds NccStrategy::MeasurementsEntryInfo::MIN_PREDICTION = |
| 247 | time::microseconds(127); |
| 248 | const time::microseconds NccStrategy::MeasurementsEntryInfo::MAX_PREDICTION = |
| 249 | time::microseconds(160000); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 250 | |
| 251 | NccStrategy::MeasurementsEntryInfo::MeasurementsEntryInfo() |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 252 | : prediction(INITIAL_PREDICTION) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 253 | { |
| 254 | } |
| 255 | |
| 256 | void |
| 257 | NccStrategy::MeasurementsEntryInfo::inheritFrom(const MeasurementsEntryInfo& other) |
| 258 | { |
| 259 | this->operator=(other); |
| 260 | } |
| 261 | |
| 262 | shared_ptr<Face> |
| 263 | NccStrategy::MeasurementsEntryInfo::getBestFace(void) { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 264 | shared_ptr<Face> best = this->bestFace.lock(); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 265 | if (best != nullptr) { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 266 | return best; |
| 267 | } |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 268 | this->bestFace = best = this->previousFace.lock(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 269 | return best; |
| 270 | } |
| 271 | |
| 272 | void |
| 273 | NccStrategy::MeasurementsEntryInfo::updateBestFace(const Face& face) { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 274 | if (this->bestFace.expired()) { |
| 275 | this->bestFace = const_cast<Face&>(face).shared_from_this(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 276 | return; |
| 277 | } |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 278 | shared_ptr<Face> bestFace = this->bestFace.lock(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 279 | if (bestFace.get() == &face) { |
| 280 | this->adjustPredictDown(); |
| 281 | } |
| 282 | else { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 283 | this->previousFace = this->bestFace; |
| 284 | this->bestFace = const_cast<Face&>(face).shared_from_this(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | NccStrategy::MeasurementsEntryInfo::adjustPredictDown() { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 290 | prediction = std::max(MIN_PREDICTION, |
| 291 | time::microseconds(prediction.count() - (prediction.count() >> ADJUST_PREDICT_DOWN_SHIFT))); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void |
| 295 | NccStrategy::MeasurementsEntryInfo::adjustPredictUp() { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 296 | prediction = std::min(MAX_PREDICTION, |
| 297 | time::microseconds(prediction.count() + (prediction.count() >> ADJUST_PREDICT_UP_SHIFT))); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void |
| 301 | NccStrategy::MeasurementsEntryInfo::ageBestFace() { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 302 | this->previousFace = this->bestFace; |
| 303 | this->bestFace.reset(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 306 | NccStrategy::PitEntryInfo::~PitEntryInfo() |
| 307 | { |
Junxiao Shi | 1391d61 | 2014-03-27 22:27:20 -0700 | [diff] [blame] | 308 | scheduler::cancel(this->bestFaceTimeout); |
| 309 | scheduler::cancel(this->propagateTimer); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | } // namespace fw |
| 313 | } // namespace nfd |