Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [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. |
| 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/>. |
| 24 | */ |
| 25 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP |
| 27 | #define NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 28 | |
| 29 | #include "cs-policy.hpp" |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 30 | #include "core/scheduler.hpp" |
| 31 | |
Davide Pesavento | c0822fa | 2018-05-10 21:54:10 -0400 | [diff] [blame] | 32 | #include <list> |
| 33 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 34 | namespace nfd { |
| 35 | namespace cs { |
| 36 | namespace priority_fifo { |
| 37 | |
| 38 | typedef std::list<iterator> Queue; |
| 39 | typedef Queue::iterator QueueIt; |
| 40 | |
| 41 | enum QueueType { |
| 42 | QUEUE_UNSOLICITED, |
| 43 | QUEUE_STALE, |
| 44 | QUEUE_FIFO, |
| 45 | QUEUE_MAX |
| 46 | }; |
| 47 | |
| 48 | struct EntryInfo |
| 49 | { |
| 50 | QueueType queueType; |
| 51 | QueueIt queueIt; |
| 52 | scheduler::EventId moveStaleEventId; |
| 53 | }; |
| 54 | |
| 55 | struct EntryItComparator |
| 56 | { |
| 57 | bool |
| 58 | operator()(const iterator& a, const iterator& b) const |
| 59 | { |
| 60 | return *a < *b; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | typedef std::map<iterator, EntryInfo*, EntryItComparator> EntryInfoMapFifo; |
| 65 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 66 | /** \brief Priority FIFO replacement policy |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 67 | * |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 68 | * This policy maintains a set of cleanup queues to decide the eviction order of CS entries. |
| 69 | * The cleanup queues are three doubly linked lists that store Table iterators. |
| 70 | * The three queues keep track of unsolicited, stale, and fresh Data packet, respectively. |
| 71 | * Table iterator is placed into, removed from, and moved between suitable queues |
| 72 | * whenever an Entry is added, removed, or has other attribute changes. |
| 73 | * The Table iterator of an Entry should be in exactly one queue at any moment. |
| 74 | * Within each queue, the iterators are kept in first-in-first-out order. |
| 75 | * Eviction procedure exhausts the first queue before moving onto the next queue, |
| 76 | * in the order of unsolicited, stale, and fresh queue. |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 77 | */ |
| 78 | class PriorityFifoPolicy : public Policy |
| 79 | { |
| 80 | public: |
| 81 | PriorityFifoPolicy(); |
| 82 | |
Minsheng Zhang | 9c903e0 | 2015-10-03 18:16:05 -0500 | [diff] [blame] | 83 | virtual |
| 84 | ~PriorityFifoPolicy(); |
| 85 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 86 | public: |
| 87 | static const std::string POLICY_NAME; |
| 88 | |
| 89 | private: |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 90 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 91 | doAfterInsert(iterator i) override; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 92 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 93 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 94 | doAfterRefresh(iterator i) override; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 95 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 96 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 97 | doBeforeErase(iterator i) override; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 98 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 99 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 100 | doBeforeUse(iterator i) override; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 101 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 102 | void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 103 | evictEntries() override; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 104 | |
| 105 | private: |
| 106 | /** \brief evicts one entry |
| 107 | * \pre CS is not empty |
| 108 | */ |
| 109 | void |
| 110 | evictOne(); |
| 111 | |
| 112 | /** \brief attaches the entry to an appropriate queue |
| 113 | * \pre the entry is not in any queue |
| 114 | */ |
| 115 | void |
| 116 | attachQueue(iterator i); |
| 117 | |
| 118 | /** \brief detaches the entry from its current queue |
| 119 | * \post the entry is not in any queue |
| 120 | */ |
| 121 | void |
| 122 | detachQueue(iterator i); |
| 123 | |
| 124 | /** \brief moves an entry from FIFO queue to STALE queue |
| 125 | */ |
| 126 | void |
| 127 | moveToStaleQueue(iterator i); |
| 128 | |
| 129 | private: |
| 130 | Queue m_queues[QUEUE_MAX]; |
| 131 | EntryInfoMapFifo m_entryInfoMap; |
| 132 | }; |
| 133 | |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 134 | } // namespace priority_fifo |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 135 | |
| 136 | using priority_fifo::PriorityFifoPolicy; |
| 137 | |
| 138 | } // namespace cs |
| 139 | } // namespace nfd |
| 140 | |
Junxiao Shi | 30c37ab | 2018-04-09 14:26:47 +0000 | [diff] [blame] | 141 | #endif // NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP |