Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed and refactored the code #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions queue-exp/pifo_stfq.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
#
# pifo-stfq.py

"""Start-Time Fair Queuing (STFQ)

This scheduling algorithm is mentioned in the paper "Programmable packet
scheduling at line rate" by Sivaraman, Anirudh, et al.

It schedules packets by their start time within a flow. It defines the start
time as the finish time of the last enqueued packet within a flow.
"""

__copyright__ = """
Copyright (c) 2021 Toke Høiland-Jørgensen <[email protected]>
Copyright (c) 2021 Frey Alfredsson <[email protected]>
Expand All @@ -26,23 +35,39 @@
"""

from pifo_lib import Packet, Runner, Pifo
from pifo_lib import SchedulingAlgorithm


class Stfq(SchedulingAlgorithm):
"""Start-Time Fair Queuing (STFQ)"""

class Stfq(Pifo):
def __init__(self):
super().__init__()
self.last_finish = {}
self.virt_time = 0
self._pifo = Pifo()

self._last_finish = {}
self._virt_time = 0

def get_rank(self, pkt):
flow = pkt.flow
if flow in self.last_finish:
rank = max(self.virt_time, self.last_finish[flow])
flow_id = pkt.flow
if flow_id in self._last_finish:
rank = max(self._virt_time, self._last_finish[flow_id])
else:
rank = self.virt_time
self.last_finish[flow] = rank + pkt.length
rank = self._virt_time
self._last_finish[flow_id] = rank + pkt.length
return rank

def enqueue(self, item):
rank = self.get_rank(item)
self._pifo.enqueue(item, rank)

def dequeue(self):
return self._pifo.dequeue()

def dump(self):
self._pifo.dump()



if __name__ == "__main__":
pkts = [
Packet(flow=1, idn=1, length=2),
Expand Down
79 changes: 79 additions & 0 deletions queue-exp/pifo_wfq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
# coding: utf-8 -*-
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# pifo-wfq.py

"""Weighted Fair Queueing (WFQ)

This scheduling algorithm is mentioned in the paper "Programmable packet
scheduling at line rate" by Sivaraman, Anirudh, et al. It schedules flows by
giving them a fraction of the capacity using predefined weights. In our example,
we defined flows with an odd number to get a weight of 50 and even numbers to
get 100.
"""

__copyright__ = """
Copyright (c) 2021 Toke Høiland-Jørgensen <[email protected]>
Copyright (c) 2021 Frey Alfredsson <[email protected]>
"""

__license__ = """
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

from pifo_lib import Packet, Runner, Pifo
from pifo_lib import SchedulingAlgorithm


class Wfq(SchedulingAlgorithm):
"""Weighted Fair Queueing (WFQ)"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So "WFQ" is really "STFQ + weights", right? So why not just make this a subclass of Stfq (or just straight-up add the weights as an optional feature to the Stfq class itself)? Also, the commit message that adds this is a bit misleading: it says it's "adding" stfq, when it's really just changing the implementation to use the SchedulingAlgorithm class; so better make that a separate commit, then add the weights as a separate thing. (The github review interface is terrible in that it won't let me comment on the commit message, so just sticking that in here).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree. It's terrible not to be able to comment on specific commits. Maybe I should also try to make more PR instead to make it more clear.

I agree it's not that much of a change to warrant the new name. I did this because the PIFO paper and examples talk about them separately, even though they look pretty much the same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's OK to keep them separate if you think that makes things clearer; but then make the relationship between them explicit; i.e., add an update_last_finish(flow, rank, pkt) method to stfq and make WFQ be a subclass of STFQ that only overrides that one method...


def __init__(self):
self._pifo = Pifo()
self._last_finish = {}
self._virt_time = 0

def get_rank(self, pkt):
flow = pkt.flow
weight = 50 if flow % 2 == 1 else 100
if flow in self._last_finish:
rank = max(self._virt_time, self._last_finish[flow])
else:
rank = self._virt_time
self._last_finish[flow] = rank + pkt.length / weight
return rank

def enqueue(self, item):
rank = self.get_rank(item)
self._pifo.enqueue(item, rank)

def dequeue(self):
return self._pifo.dequeue()

def dump(self):
self._pifo.dump()


if __name__ == "__main__":
pkts = [
Packet(flow=1, idn=1, length=100),
Packet(flow=1, idn=2, length=100),
Packet(flow=1, idn=3, length=100),
Packet(flow=2, idn=1, length=100),
Packet(flow=2, idn=2, length=100),
Packet(flow=2, idn=3, length=100),
]
Runner(pkts, Wfq()).run()