LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
qsys_mg1_setf.m
1function [W, rho_total] = qsys_mg1_setf(lambda, mu, cs)
2% QSYS_MG1_SETF Compute mean response time for M/G/1/SETF queue
3%
4% [W, RHO] = QSYS_MG1_SETF(LAMBDA, MU, CS) computes the mean response time
5% for each job class in an M/G/1 queue with SETF (Shortest Elapsed Time
6% First) scheduling.
7%
8% SETF is the non-preemptive version of FB/LAS (Feedback/Least Attained
9% Service). Under SETF:
10% - Jobs are ordered by their attained service (elapsed processing time)
11% - The job with the least attained service has highest priority
12% - However, once a job begins service, it runs to completion (non-preemptive)
13%
14% The difference from FB/LAS is that arriving jobs with less attained service
15% must wait until the currently serving job completes, rather than preempting it.
16%
17% For SETF, the mean response time follows a modified FB formula that accounts
18% for the non-preemptive nature. The formula includes an additional residual
19% service time term due to the non-preemptive blocking:
20%
21% E[T(x)]^SETF = E[T(x)]^FB + E[R] / (1 - rho_x)
22%
23% where E[R] is the mean residual service time and rho_x is the truncated load.
24%
25% PARAMETERS:
26% lambda : Vector of arrival rates per class
27% mu : Vector of service rates per class
28% cs : Vector of coefficients of variation per class
29%
30% RETURNS:
31% W : Vector of mean response times per class
32% rho : Overall system utilization (modified for Little's law)
33%
34% REFERENCES:
35% - M. Nuyens and A. Wierman, "The Foreground-Background queue: A survey",
36% Performance Evaluation, 2008.
37% - A. Wierman and M. Harchol-Balter, "Classifying scheduling policies with
38% respect to unfairness in an M/GI/1", SIGMETRICS 2003.
39%
40% Copyright (c) 2012-2026, Imperial College London
41% All rights reserved.
42
43% Ensure inputs are column vectors
44lambda = reshape(lambda, [], 1);
45mu = reshape(mu, [], 1);
46cs = reshape(cs, [], 1);
47
48% Validate input lengths
49if ~isequal(length(lambda), length(mu), length(cs))
50 error('qsys_mg1_setf:InvalidInput', ...
51 'lambda, mu, and cs must have the same length');
52end
53
54% Validate positive values
55if any(lambda <= 0) || any(mu <= 0) || any(cs < 0)
56 error('qsys_mg1_setf:InvalidInput', ...
57 'lambda and mu must be positive, cs must be non-negative');
58end
59
60K = length(lambda);
61
62% Compute per-class utilizations
63rho_i = lambda ./ mu;
64
65% Overall utilization
66rho_total = sum(rho_i);
67
68% Stability check
69if rho_total >= 1
70 error('qsys_mg1_setf:UnstableSystem', ...
71 sprintf('System is unstable: utilization rho = %g >= 1', rho_total));
72end
73
74% Compute mean residual service time for the mixture distribution
75% E[R] = sum_i (lambda_i / lambda_total) * E[S_i^2] / (2 * E[S_i])
76% = sum_i (lambda_i / lambda_total) * (1 + cs_i^2) / (2 * mu_i)
77lambda_total = sum(lambda);
78E_R = 0;
79for i = 1:K
80 p_i = lambda(i) / lambda_total;
81 E_S_i = 1 / mu(i);
82 E_S2_i = (1 + cs(i)^2) / mu(i)^2;
83 E_R = E_R + p_i * E_S2_i / (2 * E_S_i);
84end
85
86% Compute response times using SETF formula
87% SETF adds a non-preemptive penalty term to the FB formula
88W = zeros(K, 1);
89
90for k = 1:K
91 % Mean service time for this class (job size x)
92 x = 1 / mu(k);
93
94 % For SETF formula (similar to FB but with residual):
95 % rho_x = lambda * integral_0^x F_bar(t)dt
96 rho_x = 0;
97 for i = 1:K
98 if cs(i) == 1 % Exponential case
99 % integral_0^x exp(-mu_i*t)dt = (1 - exp(-mu_i*x)) / mu_i
100 integral_Fbar = (1 - exp(-mu(i)*x)) / mu(i);
101 else
102 % For non-exponential, approximate using mean and variance
103 % This is an approximation; exact requires knowing distribution
104 integral_Fbar = min(x, 1/mu(i)); % Bounded approximation
105 end
106 rho_x = rho_x + lambda(i) * integral_Fbar;
107 end
108
109 % Numerator integral: lambda * integral_0^x t*F_bar(t)dt
110 numerator = 0;
111 for i = 1:K
112 if cs(i) == 1 % Exponential case
113 % integral_0^x t*exp(-mu_i*t)dt
114 % = 1/mu_i^2 * (1 - exp(-mu_i*x)*(1 + mu_i*x))
115 mu_i = mu(i);
116 integral_tFbar = (1 - exp(-mu_i*x)*(1 + mu_i*x)) / mu_i^2;
117 else
118 % Approximation for non-exponential
119 integral_tFbar = min(x^2/2, 1/mu(i)^2);
120 end
121 numerator = numerator + lambda(i) * integral_tFbar;
122 end
123
124 % SETF formula: E[T(x)]^SETF = E[T(x)]^FB + E[R] / (1 - rho_x)
125 % where E[T(x)]^FB = numerator / (1-rho_x)^2 + x / (1-rho_x)
126 if rho_x >= 1
127 W(k) = Inf;
128 else
129 % FB waiting term
130 fb_waiting_term = numerator / (1 - rho_x)^2;
131 % FB service term (slowdown)
132 fb_service_term = x / (1 - rho_x);
133 % Non-preemptive penalty: residual service time adjusted
134 np_penalty = E_R / (1 - rho_x);
135
136 W(k) = fb_waiting_term + fb_service_term + np_penalty;
137 end
138end
139
140% Compute rhohat = Q/(1+Q) to match qsys convention
141Q = sum(lambda .* W);
142rho_total = Q / (1 + Q);
143
144end