blob: 44ebe9ed43cb36e8aecb67784cf91bfccff5648e [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 Pesaventoae430302023-05-11 01:42:46 -04003 * Copyright (c) 2014-2023, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::cs {
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050034namespace priority_fifo {
35
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060036using Queue = std::list<Policy::EntryRef>;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050037
38enum QueueType {
39 QUEUE_UNSOLICITED,
40 QUEUE_STALE,
41 QUEUE_FIFO,
42 QUEUE_MAX
43};
44
45struct EntryInfo
46{
47 QueueType queueType;
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060048 Queue::iterator queueIt;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050049 scheduler::EventId moveStaleEventId;
50};
51
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040052/** \brief Priority First-In-First-Out (FIFO) replacement policy.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050053 *
Junxiao Shi30c37ab2018-04-09 14:26:47 +000054 * This policy maintains a set of cleanup queues to decide the eviction order of CS entries.
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060055 * The cleanup queues are three doubly linked lists that store EntryRefs.
Junxiao Shi30c37ab2018-04-09 14:26:47 +000056 * The three queues keep track of unsolicited, stale, and fresh Data packet, respectively.
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060057 * EntryRef is placed into, removed from, and moved between suitable queues
Junxiao Shi30c37ab2018-04-09 14:26:47 +000058 * whenever an Entry is added, removed, or has other attribute changes.
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060059 * Each Entry should be in exactly one queue at any moment.
60 * Within each queue, the EntryRefs are kept in first-in-first-out order.
Junxiao Shi30c37ab2018-04-09 14:26:47 +000061 * Eviction procedure exhausts the first queue before moving onto the next queue,
62 * in the order of unsolicited, stale, and fresh queue.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050063 */
Davide Pesavento3db98072021-03-09 23:03:27 -050064class PriorityFifoPolicy final : public Policy
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050065{
66public:
67 PriorityFifoPolicy();
68
Davide Pesavento3db98072021-03-09 23:03:27 -050069 ~PriorityFifoPolicy() final;
Minsheng Zhang9c903e02015-10-03 18:16:05 -050070
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050071private:
Junxiao Shi30c37ab2018-04-09 14:26:47 +000072 void
Davide Pesavento3db98072021-03-09 23:03:27 -050073 doAfterInsert(EntryRef i) final;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050074
Junxiao Shi30c37ab2018-04-09 14:26:47 +000075 void
Davide Pesavento3db98072021-03-09 23:03:27 -050076 doAfterRefresh(EntryRef i) final;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050077
Junxiao Shi30c37ab2018-04-09 14:26:47 +000078 void
Davide Pesavento3db98072021-03-09 23:03:27 -050079 doBeforeErase(EntryRef i) final;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050080
Junxiao Shi30c37ab2018-04-09 14:26:47 +000081 void
Davide Pesavento3db98072021-03-09 23:03:27 -050082 doBeforeUse(EntryRef i) final;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050083
Junxiao Shi30c37ab2018-04-09 14:26:47 +000084 void
Davide Pesavento3db98072021-03-09 23:03:27 -050085 evictEntries() final;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050086
87private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040088 /** \brief Evicts one entry.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050089 * \pre CS is not empty
90 */
91 void
92 evictOne();
93
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040094 /** \brief Attaches the entry to an appropriate queue.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050095 * \pre the entry is not in any queue
96 */
97 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -060098 attachQueue(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050099
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400100 /** \brief Detaches the entry from its current queue.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500101 * \post the entry is not in any queue
102 */
103 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600104 detachQueue(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500105
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400106 /** \brief Moves an entry from FIFO queue to STALE queue.
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500107 */
108 void
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600109 moveToStaleQueue(EntryRef i);
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500110
Davide Pesaventoae430302023-05-11 01:42:46 -0400111public:
112 static constexpr std::string_view POLICY_NAME{"priority_fifo"};
113
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500114private:
115 Queue m_queues[QUEUE_MAX];
Junxiao Shi07f2e2f2019-07-22 09:10:06 -0600116 std::map<EntryRef, EntryInfo*> m_entryInfoMap;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500117};
118
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200119} // namespace priority_fifo
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500120
121using priority_fifo::PriorityFifoPolicy;
122
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400123} // namespace nfd::cs
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500124
Junxiao Shi30c37ab2018-04-09 14:26:47 +0000125#endif // NFD_DAEMON_TABLE_CS_POLICY_PRIORITY_FIFO_HPP