blob: 39b456f80d1e21f9c774db700b2721443b4c7bf2 [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi30c37ab2018-04-09 14:26:47 +00002/*
Davide Pesavento3dade002019-03-19 11:29:56 -06003 * Copyright (c) 2014-2019, Regents of the University of California,
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05004 * 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 Shi30c37ab2018-04-09 14:26:47 +000026#ifndef NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP
27#define NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050028
29#include "cs-policy.hpp"
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050030
Davide Pesaventoc0822fa2018-05-10 21:54:10 -040031#include <list>
32
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050033namespace nfd {
34namespace cs {
35namespace priority_fifo {
36
37typedef std::list<iterator> Queue;
38typedef Queue::iterator QueueIt;
39
40enum QueueType {
41 QUEUE_UNSOLICITED,
42 QUEUE_STALE,
43 QUEUE_FIFO,
44 QUEUE_MAX
45};
46
47struct EntryInfo
48{
49 QueueType queueType;
50 QueueIt queueIt;
51 scheduler::EventId moveStaleEventId;
52};
53
54struct EntryItComparator
55{
56 bool
57 operator()(const iterator& a, const iterator& b) const
58 {
59 return *a < *b;
60 }
61};
62
63typedef std::map<iterator, EntryInfo*, EntryItComparator> EntryInfoMapFifo;
64
Junxiao Shi30c37ab2018-04-09 14:26:47 +000065/** \brief Priority FIFO replacement policy
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050066 *
Junxiao Shi30c37ab2018-04-09 14:26:47 +000067 * This policy maintains a set of cleanup queues to decide the eviction order of CS entries.
68 * The cleanup queues are three doubly linked lists that store Table iterators.
69 * The three queues keep track of unsolicited, stale, and fresh Data packet, respectively.
70 * Table iterator is placed into, removed from, and moved between suitable queues
71 * whenever an Entry is added, removed, or has other attribute changes.
72 * The Table iterator of an Entry should be in exactly one queue at any moment.
73 * Within each queue, the iterators are kept in first-in-first-out order.
74 * Eviction procedure exhausts the first queue before moving onto the next queue,
75 * in the order of unsolicited, stale, and fresh queue.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050076 */
77class PriorityFifoPolicy : public Policy
78{
79public:
80 PriorityFifoPolicy();
81
Davide Pesavento3dade002019-03-19 11:29:56 -060082 ~PriorityFifoPolicy() override;
Minsheng Zhang9c903e02015-10-03 18:16:05 -050083
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050084public:
85 static const std::string POLICY_NAME;
86
87private:
Junxiao Shi30c37ab2018-04-09 14:26:47 +000088 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020089 doAfterInsert(iterator i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050090
Junxiao Shi30c37ab2018-04-09 14:26:47 +000091 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020092 doAfterRefresh(iterator i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050093
Junxiao Shi30c37ab2018-04-09 14:26:47 +000094 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020095 doBeforeErase(iterator i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050096
Junxiao Shi30c37ab2018-04-09 14:26:47 +000097 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020098 doBeforeUse(iterator i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050099
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000100 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200101 evictEntries() override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500102
103private:
104 /** \brief evicts one entry
105 * \pre CS is not empty
106 */
107 void
108 evictOne();
109
110 /** \brief attaches the entry to an appropriate queue
111 * \pre the entry is not in any queue
112 */
113 void
114 attachQueue(iterator i);
115
116 /** \brief detaches the entry from its current queue
117 * \post the entry is not in any queue
118 */
119 void
120 detachQueue(iterator i);
121
122 /** \brief moves an entry from FIFO queue to STALE queue
123 */
124 void
125 moveToStaleQueue(iterator i);
126
127private:
128 Queue m_queues[QUEUE_MAX];
129 EntryInfoMapFifo m_entryInfoMap;
130};
131
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200132} // namespace priority_fifo
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500133
134using priority_fifo::PriorityFifoPolicy;
135
136} // namespace cs
137} // namespace nfd
138
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000139#endif // NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP