Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
| 26 | #ifndef NFD_DAEMON_TABLE_CS_POLICY_FIFO_HPP |
| 27 | #define NFD_DAEMON_TABLE_CS_POLICY_FIFO_HPP |
| 28 | |
| 29 | #include "cs-policy.hpp" |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 30 | #include "core/scheduler.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | namespace cs { |
| 34 | namespace priority_fifo { |
| 35 | |
| 36 | typedef std::list<iterator> Queue; |
| 37 | typedef Queue::iterator QueueIt; |
| 38 | |
| 39 | enum QueueType { |
| 40 | QUEUE_UNSOLICITED, |
| 41 | QUEUE_STALE, |
| 42 | QUEUE_FIFO, |
| 43 | QUEUE_MAX |
| 44 | }; |
| 45 | |
| 46 | struct EntryInfo |
| 47 | { |
| 48 | QueueType queueType; |
| 49 | QueueIt queueIt; |
| 50 | scheduler::EventId moveStaleEventId; |
| 51 | }; |
| 52 | |
| 53 | struct EntryItComparator |
| 54 | { |
| 55 | bool |
| 56 | operator()(const iterator& a, const iterator& b) const |
| 57 | { |
| 58 | return *a < *b; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | typedef std::map<iterator, EntryInfo*, EntryItComparator> EntryInfoMapFifo; |
| 63 | |
| 64 | /** \brief Priority Fifo cs replacement policy |
| 65 | * |
| 66 | * The entries that get removed first are unsolicited Data packets, |
| 67 | * which are the Data packets that got cached opportunistically without preceding |
| 68 | * forwarding of the corresponding Interest packet. |
| 69 | * Next, the Data packets with expired freshness are removed. |
| 70 | * Last, the Data packets are removed from the Content Store on a pure FIFO basis. |
| 71 | */ |
| 72 | class PriorityFifoPolicy : public Policy |
| 73 | { |
| 74 | public: |
| 75 | PriorityFifoPolicy(); |
| 76 | |
Minsheng Zhang | 9c903e0 | 2015-10-03 18:16:05 -0500 | [diff] [blame] | 77 | virtual |
| 78 | ~PriorityFifoPolicy(); |
| 79 | |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 80 | public: |
| 81 | static const std::string POLICY_NAME; |
| 82 | |
| 83 | private: |
| 84 | virtual void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 85 | doAfterInsert(iterator i) override; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 86 | |
| 87 | virtual void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 88 | doAfterRefresh(iterator i) override; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 89 | |
| 90 | virtual void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 91 | doBeforeErase(iterator i) override; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 92 | |
| 93 | virtual void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 94 | doBeforeUse(iterator i) override; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 95 | |
| 96 | virtual void |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 97 | evictEntries() override; |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 98 | |
| 99 | private: |
| 100 | /** \brief evicts one entry |
| 101 | * \pre CS is not empty |
| 102 | */ |
| 103 | void |
| 104 | evictOne(); |
| 105 | |
| 106 | /** \brief attaches the entry to an appropriate queue |
| 107 | * \pre the entry is not in any queue |
| 108 | */ |
| 109 | void |
| 110 | attachQueue(iterator i); |
| 111 | |
| 112 | /** \brief detaches the entry from its current queue |
| 113 | * \post the entry is not in any queue |
| 114 | */ |
| 115 | void |
| 116 | detachQueue(iterator i); |
| 117 | |
| 118 | /** \brief moves an entry from FIFO queue to STALE queue |
| 119 | */ |
| 120 | void |
| 121 | moveToStaleQueue(iterator i); |
| 122 | |
| 123 | private: |
| 124 | Queue m_queues[QUEUE_MAX]; |
| 125 | EntryInfoMapFifo m_entryInfoMap; |
| 126 | }; |
| 127 | |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 128 | } // namespace priority_fifo |
Minsheng Zhang | cb6e05f | 2015-04-20 15:51:47 -0500 | [diff] [blame] | 129 | |
| 130 | using priority_fifo::PriorityFifoPolicy; |
| 131 | |
| 132 | } // namespace cs |
| 133 | } // namespace nfd |
| 134 | |
Davide Pesavento | b84bd3a | 2016-04-22 02:21:45 +0200 | [diff] [blame] | 135 | #endif // NFD_DAEMON_TABLE_CS_POLICY_FIFO_HPP |