blob: c453990ddc1d890d51d3c4948dd5620ad225a0bd [file] [log] [blame]
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventob84bd3a2016-04-22 02:21:45 +02003 * Copyright (c) 2014-2016, 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
26#ifndef NFD_DAEMON_TABLE_CS_POLICY_FIFO_HPP
27#define NFD_DAEMON_TABLE_CS_POLICY_FIFO_HPP
28
29#include "cs-policy.hpp"
30#include "common.hpp"
31#include "core/scheduler.hpp"
32
33namespace 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
65/** \brief Priority Fifo cs replacement policy
66 *
67 * The entries that get removed first are unsolicited Data packets,
68 * which are the Data packets that got cached opportunistically without preceding
69 * forwarding of the corresponding Interest packet.
70 * Next, the Data packets with expired freshness are removed.
71 * Last, the Data packets are removed from the Content Store on a pure FIFO basis.
72 */
73class PriorityFifoPolicy : public Policy
74{
75public:
76 PriorityFifoPolicy();
77
Minsheng Zhang9c903e02015-10-03 18:16:05 -050078 virtual
79 ~PriorityFifoPolicy();
80
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050081public:
82 static const std::string POLICY_NAME;
83
84private:
85 virtual void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020086 doAfterInsert(iterator i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050087
88 virtual void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020089 doAfterRefresh(iterator i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050090
91 virtual void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020092 doBeforeErase(iterator i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050093
94 virtual void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020095 doBeforeUse(iterator i) override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050096
97 virtual void
Davide Pesaventob84bd3a2016-04-22 02:21:45 +020098 evictEntries() override;
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -050099
100private:
101 /** \brief evicts one entry
102 * \pre CS is not empty
103 */
104 void
105 evictOne();
106
107 /** \brief attaches the entry to an appropriate queue
108 * \pre the entry is not in any queue
109 */
110 void
111 attachQueue(iterator i);
112
113 /** \brief detaches the entry from its current queue
114 * \post the entry is not in any queue
115 */
116 void
117 detachQueue(iterator i);
118
119 /** \brief moves an entry from FIFO queue to STALE queue
120 */
121 void
122 moveToStaleQueue(iterator i);
123
124private:
125 Queue m_queues[QUEUE_MAX];
126 EntryInfoMapFifo m_entryInfoMap;
127};
128
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200129} // namespace priority_fifo
Minsheng Zhangcb6e05f2015-04-20 15:51:47 -0500130
131using priority_fifo::PriorityFifoPolicy;
132
133} // namespace cs
134} // namespace nfd
135
Davide Pesaventob84bd3a2016-04-22 02:21:45 +0200136#endif // NFD_DAEMON_TABLE_CS_POLICY_FIFO_HPP