blob: 967140871dfbe2b36b8e640f2d564a42fbcd97af [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
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060037using Queue = std::list<Policy::EntryRef>;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050038
39enum QueueType {
40 QUEUE_UNSOLICITED,
41 QUEUE_STALE,
42 QUEUE_FIFO,
43 QUEUE_MAX
44};
45
46struct EntryInfo
47{
48 QueueType queueType;
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060049 Queue::iterator queueIt;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050050 scheduler::EventId moveStaleEventId;
51};
52
Junxiao Shi30c37ab2018-04-09 14:26:47 +000053/** \brief Priority FIFO replacement policy
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050054 *
Junxiao Shi30c37ab2018-04-09 14:26:47 +000055 * This policy maintains a set of cleanup queues to decide the eviction order of CS entries.
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060056 * The cleanup queues are three doubly linked lists that store EntryRefs.
Junxiao Shi30c37ab2018-04-09 14:26:47 +000057 * The three queues keep track of unsolicited, stale, and fresh Data packet, respectively.
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060058 * EntryRef is placed into, removed from, and moved between suitable queues
Junxiao Shi30c37ab2018-04-09 14:26:47 +000059 * whenever an Entry is added, removed, or has other attribute changes.
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060060 * Each Entry should be in exactly one queue at any moment.
61 * Within each queue, the EntryRefs are kept in first-in-first-out order.
Junxiao Shi30c37ab2018-04-09 14:26:47 +000062 * Eviction procedure exhausts the first queue before moving onto the next queue,
63 * in the order of unsolicited, stale, and fresh queue.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050064 */
65class PriorityFifoPolicy : public Policy
66{
67public:
68 PriorityFifoPolicy();
69
Davide Pesavento3dade002019-03-19 11:29:56 -060070 ~PriorityFifoPolicy() override;
Minsheng Zhang9c903e02015-10-03 18:16:05 -050071
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050072public:
73 static const std::string POLICY_NAME;
74
75private:
Junxiao Shi30c37ab2018-04-09 14:26:47 +000076 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060077 doAfterInsert(EntryRef i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050078
Junxiao Shi30c37ab2018-04-09 14:26:47 +000079 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060080 doAfterRefresh(EntryRef i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050081
Junxiao Shi30c37ab2018-04-09 14:26:47 +000082 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060083 doBeforeErase(EntryRef i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050084
Junxiao Shi30c37ab2018-04-09 14:26:47 +000085 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060086 doBeforeUse(EntryRef i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050087
Junxiao Shi30c37ab2018-04-09 14:26:47 +000088 void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020089 evictEntries() override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050090
91private:
92 /** \brief evicts one entry
93 * \pre CS is not empty
94 */
95 void
96 evictOne();
97
98 /** \brief attaches the entry to an appropriate queue
99 * \pre the entry is not in any queue
100 */
101 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600102 attachQueue(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500103
104 /** \brief detaches the entry from its current queue
105 * \post the entry is not in any queue
106 */
107 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600108 detachQueue(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500109
110 /** \brief moves an entry from FIFO queue to STALE queue
111 */
112 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600113 moveToStaleQueue(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500114
115private:
116 Queue m_queues[QUEUE_MAX];
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600117 std::map<EntryRef, EntryInfo*> m_entryInfoMap;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500118};
119
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200120} // namespace priority_fifo
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500121
122using priority_fifo::PriorityFifoPolicy;
123
124} // namespace cs
125} // namespace nfd
126
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000127#endif // NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP