LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
qsys_mg1_fb.m
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
3%
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).
7%
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.
11%
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):
14%
15% E[T(x)]^FB = (lambda * integral_0^x t*F_bar(t)dt) / (1-rho_x)^2
16% + x / (1 - rho_x)
17%
18% where:
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)
22%
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.
25%
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.
31%
32% PARAMETERS:
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)
36%
37% RETURNS:
38% W : Vector of mean response times per class
39% rho : Overall system utilization (modified for Little's law)
40%
41% REFERENCES:
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",
45% Wiley, 1976.
46%
47% Copyright (c) 2012-2026, Imperial College London
48% All rights reserved.
49
50% Ensure inputs are column vectors
51lambda = reshape(lambda, [], 1);
52mu = reshape(mu, [], 1);
53cs = reshape(cs, [], 1);
54
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');
59end
60
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');
65end
66
67K = length(lambda);
68
69% Overall utilization
70rho_total = sum(lambda ./ mu);
71
72% Stability check
73if rho_total >= 1
74 error('qsys_mg1_fb:UnstableSystem', ...
75 sprintf('System is unstable: utilization rho = %g >= 1', rho_total));
76end
77
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);
82else
83 % General case: use original approximation
84 W = qsys_mg1_fb_general(lambda, mu, cs);
85end
86
87% Compute rhohat = Q/(1+Q) to match qsys convention
88Q = sum(lambda .* W);
89rho_total = Q / (1 + Q);
90
91end
92
93function W = qsys_mg1_fb_exp(lambda, mu)
94% Specialized FB formula for exponential service times
95%
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)
99%
100% The survival function for the mixture is:
101% F_bar(t) = sum_i (lambda_i/lambda_total) * exp(-mu_i * t)
102%
103% The expected response time for class k is:
104% E[T_k] = integral_0^inf E[T(x)] * f_k(x) dx
105%
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)
108
109K = length(lambda);
110lambda_total = sum(lambda);
111p = lambda / lambda_total; % mixing probabilities
112
113W = zeros(K, 1);
114
115for k = 1:K
116 % Integrate E[T(x)] * f_k(x) from 0 to infinity
117 mu_k = mu(k);
118
119 % Upper limit: 20 mean service times covers 99.9999998% of exponential
120 x_max = 20 / mu_k;
121
122 % Response time function for a job of size x
123 T_of_x = @(x) compute_fb_response(x, lambda, mu, p, lambda_total);
124
125 % Exponential density for class k
126 f_k = @(x) mu_k * exp(-mu_k * x);
127
128 % Integrand: E[T(x)] * f_k(x)
129 integrand = @(x) T_of_x(x) .* f_k(x);
130
131 % Numerical integration
132 W(k) = integral(integrand, 0, x_max, 'RelTol', 1e-8, 'AbsTol', 1e-10);
133end
134
135end
136
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
139%
140% E[T(x)] = numerator(x) / (1-rho_x)^2 + x / (1-rho_x)
141%
142% where:
143% rho_x = lambda * integral_0^x F_bar(t) dt
144% numerator(x) = lambda * integral_0^x t * F_bar(t) dt
145%
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
150
151K = length(lambda);
152
153% Handle vectorized x
154T = zeros(size(x));
155
156for j = 1:numel(x)
157 xj = x(j);
158
159 % Compute rho_x = lambda * integral_0^x F_bar(t) dt
160 rho_x = 0;
161 for i = 1:K
162 mu_i = mu(i);
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;
166 end
167
168 % Compute numerator = lambda * integral_0^x t * F_bar(t) dt
169 numerator = 0;
170 for i = 1:K
171 mu_i = mu(i);
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;
175 end
176
177 % FB response time formula
178 if rho_x >= 1
179 T(j) = Inf;
180 else
181 T(j) = numerator / (1 - rho_x)^2 + xj / (1 - rho_x);
182 end
183end
184
185end
186
187function W = qsys_mg1_fb_general(lambda, mu, cs)
188% General FB formula for non-exponential service (deterministic class sizes)
189
190K = length(lambda);
191
192W = zeros(K, 1);
193
194for k = 1:K
195 x = 1 / mu(k);
196
197 rho_x = 0;
198 for i = 1:K
199 if abs(cs(i) - 1) < 1e-6 % Exponential case
200 integral_Fbar = (1 - exp(-mu(i)*x)) / mu(i);
201 else
202 integral_Fbar = min(x, 1/mu(i));
203 end
204 rho_x = rho_x + lambda(i) * integral_Fbar;
205 end
206
207 numerator = 0;
208 for i = 1:K
209 if abs(cs(i) - 1) < 1e-6 % Exponential case
210 mu_i = mu(i);
211 integral_tFbar = (1 - exp(-mu_i*x)*(1 + mu_i*x)) / mu_i^2;
212 else
213 integral_tFbar = min(x^2/2, 1/mu(i)^2);
214 end
215 numerator = numerator + lambda(i) * integral_tFbar;
216 end
217
218 if rho_x >= 1
219 W(k) = Inf;
220 else
221 waiting_term = numerator / (1 - rho_x)^2;
222 service_term = x / (1 - rho_x);
223 W(k) = waiting_term + service_term;
224 end
225end
226
227end