gwr_components/flow_controls/
rate_limiter.rs

1// Copyright (c) 2023 Graphcore Ltd. All rights reserved.
2
3//! Provide effective bandwidth limit for a component.
4//!
5//! A [RateLimiter] is a component that is given a
6//! [clock](gwr_engine::time::clock::Clock) and a rate in `bits per tick`.
7//! It uses this rate limit to enforce a delay determined by the object that is
8//! being rate limited.
9//!
10//! The [RateLimiter] therefore requires objects to implement the
11//! [TotalBytes] trait so that the number of bits of the object can be
12//! determined.
13//!
14//! # Ports
15//!
16//! This component has the following ports:
17//!  - One [input port](gwr_engine::port::InPort): `rx`
18//!  - One [output port](gwr_engine::port::OutPort): `tx`
19//!
20//! # Creating a Rate Limiter
21//!
22//! [RateLimiter]s should normally be constructed using the
23//! [rc_limiter!](crate::rc_limiter) macro. This returns an `Rc<RateLimiter>`
24//! because that is what components normally accept as a rate limiter argument.
25//! They are `Rc`ed because they are used immutably and as a result the same
26//! rate limiter can be shared by all components that have the same bandwidth.
27//!
28//! # Examples:
29//!
30//! Here is a basic example of a rate limiter being used by the
31//! [Limiter](crate::flow_controls::limiter) component which is connected
32//! between a source and sink.
33//!
34//! A [source](crate::source::Source) is used to produce 4-byte packets.
35//! The rate limiter is configured to run on a 1GHz clock at a rate of 16 bits
36//! per tick.
37//!
38//! As a result, the total time for the simulation should be `20.0ns` because
39//! each of the 10 packets should take 2 clock ticks to pass through the
40//! [Limiter](crate::flow_controls::limiter) and be consumed by the
41//! [Sink](crate::sink::Sink).
42//!
43//! ```rust
44//! # use gwr_components::flow_controls::limiter::Limiter;
45//! # use gwr_components::sink::Sink;
46//! # use gwr_components::source::Source;
47//! # use gwr_components::{connect_port, rc_limiter, option_box_repeat};
48//! # use gwr_engine::engine::Engine;
49//! # use gwr_engine::run_simulation;
50//!
51//! // Create the engine.
52//! let mut engine = Engine::default();
53//!
54//! // Create a 1GHz clock.
55//! let clock = engine.clock_ghz(1.0);
56//!
57//! // And build a 16 bits-per-tick rate limiter.
58//! let rate_limiter = rc_limiter!(clock, 16);
59//!
60//! // Build the source (initially with no generator).
61//! let source = Source::new_and_register(&engine, engine.top(), "source", None)
62//!     .expect("should be able to create and register `Source`");
63//!
64//! // Create a packet that uses the source as its trace-control entity.
65//! let packet = 0; // TODO implement a packet type to use here
66//!
67//! // Configure the source to produce ten of these packets.
68//! source.set_generator(option_box_repeat!(packet ; 10));
69//!
70//! // Create the a limiter component to enforce the limit
71//! let limiter = Limiter::new_and_register(&engine, engine.top(), "limit", rate_limiter)
72//!     .expect("should be able to create and register `Limiter`");
73//!
74//! // Create the sink to accept these packets.
75//! let sink = Sink::new_and_register(&engine, engine.top(), "sink")
76//!     .expect("should be able to create and register `Sink`");
77//!
78//! // Connect the components.
79//! connect_port!(source, tx => limiter, rx)
80//!     .expect("should be able to connect `Source` to `Limiter`");
81//! connect_port!(limiter, tx => sink, rx)
82//!     .expect("should be able to connect `Limiter` to `Sink`");
83//!
84//! // Run the simulation.
85//! run_simulation!(engine);
86//!
87//! // Ensure the time is as expected.
88//! assert_eq!(engine.time_now_ns(), 20.0);
89//! ```
90
91use std::marker::PhantomData;
92
93use gwr_engine::time::clock::Clock;
94use gwr_engine::traits::TotalBytes;
95
96/// Create a [RateLimiter] wrapped in an [Rc](std::rc::Rc).
97///
98/// This is the most common form of [RateLimiter] used because all of its
99/// methods can be used immutably and therefore it can be shared by any number
100/// of components with the same bandwidth limit.
101#[macro_export]
102macro_rules! rc_limiter {
103    ($clock:expr, $bits_per_tick:expr) => {
104        std::rc::Rc::new($crate::flow_controls::rate_limiter::RateLimiter::new(
105            $clock,
106            $bits_per_tick,
107        ))
108    };
109}
110
111#[macro_export]
112macro_rules! option_rc_limiter {
113    ($clock:expr, $bits_per_tick:expr) => {
114        Some(std::rc::Rc::new(
115            $crate::flow_controls::rate_limiter::RateLimiter::new($clock, $bits_per_tick),
116        ))
117    };
118}
119
120#[derive(Clone)]
121pub struct RateLimiter<T>
122where
123    T: TotalBytes,
124{
125    /// Clock rate limiter is attached to.
126    clock: Clock,
127
128    /// Bits per tick that can pass through this interface.
129    bits_per_tick: usize,
130
131    phantom: PhantomData<T>,
132}
133
134impl<T> RateLimiter<T>
135where
136    T: TotalBytes,
137{
138    #[must_use]
139    pub fn new(clock: Clock, bits_per_tick: usize) -> Self {
140        Self {
141            clock,
142            bits_per_tick,
143            phantom: PhantomData,
144        }
145    }
146
147    pub async fn delay(&self, value: &T) {
148        let delay_ticks = self.ticks(value);
149        self.clock.wait_ticks(delay_ticks as u64).await;
150    }
151
152    pub async fn delay_ticks(&self, ticks: usize) {
153        self.clock.wait_ticks(ticks as u64).await;
154    }
155
156    pub fn ticks(&self, value: &T) -> usize {
157        let payload_bytes = value.total_bytes();
158        let payload_bits = payload_bytes * 8;
159        self.ticks_from_bits(payload_bits)
160    }
161
162    #[must_use]
163    pub fn ticks_from_bits(&self, bits: usize) -> usize {
164        bits.div_ceil(self.bits_per_tick)
165    }
166}