1function [W, rho_total] = qsys_mg1_fb(lambda, mu, cs)
2% QSYS_MG1_FB Compute mean response time
for M/G/1/FB (Feedback/LAS) queue
4% [W, RHO] = QSYS_MG1_FB(LAMBDA, MU, CS) computes the mean response time
5%
for each job
class in an M/G/1 queue with Feedback (FB) scheduling,
6% also known as Least Attained Service (LAS) or Shortest Elapsed Time (SET).
8% Under FB/LAS, the job with the least attained service (smallest age)
9% receives priority. This
is an age-based policy where priority depends
10% on how much service a job has received, not its original or remaining size.
12% For FB, the mean response time
for a job of size x
is given by
13% (Section 3.3 of Wierman-Harchol-Balter 2003):
15% E[T(x)]^FB = (lambda * integral_0^x t*F_bar(t)dt) / (1-rho_x)^2
19% - rho_x = lambda * integral_0^x F_bar(t)dt (load from jobs completing
20% service before reaching age x)
21% - F_bar(t) = 1 - F(t) (complementary CDF / survival function)
23% For exponential service, the expected response time for class k
is computed
24% by integrating E[T(x)] over the exponential distribution of class k sizes.
26% CLASSIFICATION (Wierman-Harchol-Balter 2003):
27% FB
is "Always Unfair" - some job size
is treated unfairly under all
28% loads and all service distributions. However, FB approximates SRPT
29% for heavy-tailed distributions and
is practical since job sizes
30% need not be known in advance.
33% lambda : Vector of arrival rates per class
34% mu : Vector of service rates per class
35% cs : Vector of coefficients of variation per class (cs=1 for exponential)
38% W : Vector of mean response times per class
39% rho : Overall system utilization (modified for Little
's law)
42% - A. Wierman and M. Harchol-Balter, "Classifying scheduling policies with
43% respect to unfairness in an M/GI/1", SIGMETRICS 2003, Section 3.3.
44% - L. Kleinrock, "Queueing Systems, Volume II: Computer Applications",
47% Copyright (c) 2012-2026, Imperial College London
50% Ensure inputs are column vectors
51lambda = reshape(lambda, [], 1);
52mu = reshape(mu, [], 1);
53cs = reshape(cs, [], 1);
55% Validate input lengths
56if ~isequal(length(lambda), length(mu), length(cs))
57 error('qsys_mg1_fb:InvalidInput
', ...
58 'lambda, mu, and cs must have the same length
');
61% Validate positive values
62if any(lambda <= 0) || any(mu <= 0) || any(cs < 0)
63 error('qsys_mg1_fb:InvalidInput
', ...
64 'lambda and mu must be positive, cs must be non-negative
');
70rho_total = sum(lambda ./ mu);
74 error('qsys_mg1_fb:UnstableSystem
', ...
75 sprintf('System
is unstable: utilization rho = %g >= 1', rho_total));
78% Check
if all
classes are exponential (cs = 1)
79if all(abs(cs - 1) < 1e-6)
80 % Use specialized formula for exponential service
81 W = qsys_mg1_fb_exp(lambda, mu);
83 % General case: use original approximation
84 W = qsys_mg1_fb_general(lambda, mu, cs);
87% Compute rhohat = Q/(1+Q) to match qsys convention
89rho_total = Q / (1 + Q);
93function W = qsys_mg1_fb_exp(lambda, mu)
94% Specialized FB formula for exponential service times
96% For exponential service, we integrate E[T(x)] over the exponential
97% distribution of job sizes. The service time mixture PDF
is:
98% f(t) = sum_i (lambda_i/lambda_total) * mu_i * exp(-mu_i * t)
100% The survival function for the mixture
is:
101% F_bar(t) = sum_i (lambda_i/lambda_total) * exp(-mu_i * t)
103% The expected response time for class k
is:
104% E[T_k] = integral_0^inf E[T(x)] * f_k(x) dx
106% where f_k(x) = mu_k * exp(-mu_k * x) and
107% E[T(x)] = numerator(x)/(1-rho_x)^2 + x/(1-rho_x)
110lambda_total = sum(lambda);
111p = lambda / lambda_total; % mixing probabilities
116 % Integrate E[T(x)] * f_k(x) from 0 to infinity
119 % Upper limit: 20 mean service times covers 99.9999998% of exponential
122 % Response time function for a job of size x
123 T_of_x = @(x) compute_fb_response(x, lambda, mu, p, lambda_total);
125 % Exponential density for class k
126 f_k = @(x) mu_k * exp(-mu_k * x);
128 % Integrand: E[T(x)] * f_k(x)
129 integrand = @(x) T_of_x(x) .* f_k(x);
131 % Numerical integration
132 W(k) = integral(integrand, 0, x_max, 'RelTol', 1e-8, 'AbsTol', 1e-10);
137function T = compute_fb_response(x, lambda, mu, p, lambda_total)
138% Compute FB/LAS response time E[T(x)] for a job of size x
140% E[T(x)] = numerator(x) / (1-rho_x)^2 + x / (1-rho_x)
143% rho_x = lambda * integral_0^x F_bar(t) dt
144% numerator(x) = lambda * integral_0^x t * F_bar(t) dt
146% For mixture of exponentials:
147% F_bar(t) = sum_i p_i * exp(-mu_i * t)
148% integral_0^x exp(-mu_i*t) dt = (1 - exp(-mu_i*x)) / mu_i
149% integral_0^x t*exp(-mu_i*t) dt = (1 - exp(-mu_i*x)*(1 + mu_i*x)) / mu_i^2
159 % Compute rho_x = lambda * integral_0^x F_bar(t) dt
163 % integral_0^x exp(-mu_i*t) dt = (1 - exp(-mu_i*x)) / mu_i
164 int_Fbar = (1 - exp(-mu_i * xj)) / mu_i;
165 rho_x = rho_x + p(i) * lambda_total * int_Fbar;
168 % Compute numerator = lambda * integral_0^x t * F_bar(t) dt
172 % integral_0^x t*exp(-mu_i*t) dt = (1 - exp(-mu_i*x)*(1 + mu_i*x)) / mu_i^2
173 int_tFbar = (1 - exp(-mu_i * xj) * (1 + mu_i * xj)) / mu_i^2;
174 numerator = numerator + p(i) * lambda_total * int_tFbar;
177 % FB response time formula
181 T(j) = numerator / (1 - rho_x)^2 + xj / (1 - rho_x);
187function W = qsys_mg1_fb_general(lambda, mu, cs)
188% General FB formula for non-exponential service (deterministic class sizes)
199 if abs(cs(i) - 1) < 1e-6 % Exponential case
200 integral_Fbar = (1 - exp(-mu(i)*x)) / mu(i);
202 integral_Fbar = min(x, 1/mu(i));
204 rho_x = rho_x + lambda(i) * integral_Fbar;
209 if abs(cs(i) - 1) < 1e-6 % Exponential case
211 integral_tFbar = (1 - exp(-mu_i*x)*(1 + mu_i*x)) / mu_i^2;
213 integral_tFbar = min(x^2/2, 1/mu(i)^2);
215 numerator = numerator + lambda(i) * integral_tFbar;
221 waiting_term = numerator / (1 - rho_x)^2;
222 service_term = x / (1 - rho_x);
223 W(k) = waiting_term + service_term;