1function ql = infer_compute_ql_at_arrival(at, at_jobid, rt, rt_jobid,
class, R)
2% INFER_COMPUTE_QL_AT_ARRIVAL Compute per-
class queue lengths at arrival.
4% Reconstructs the queue state seen by each arriving job
using arrival
5% and departure times. Arrival times and response times are matched by
6% job ID, so they need not be in the same order or come from the same
9% At ties, departures are processed before arrivals.
12% at - arrival times (column vector, n x 1)
13% at_jobid - job IDs
for arrival times (column vector, n x 1)
14% rt - response times (column vector, m x 1, m >= n)
15% rt_jobid - job IDs
for response times (column vector, m x 1)
16%
class - class of each arrival sample (column vector, n x 1,
17% ordered consistently with at and at_jobid)
21% ql - n x R matrix of per-class queue lengths at each arrival,
22% rows ordered consistently with the input at/at_jobid
24% Copyright (c) 2012-2026, Imperial College London
26% This code
is released under the 3-Clause BSD License.
30% Match response times to arrivals by job ID
31[found, loc] = ismember(at_jobid, rt_jobid);
33 error(
'infer_compute_ql_at_arrival: not all arrival job IDs found in response time job IDs.');
37% Sort arrivals by time
38[at_sorted, sortIdx] = sort(at);
39class_sorted =
class(sortIdx);
40rt_sorted = rt_matched(sortIdx);
42exitTimes = at_sorted + rt_sorted;
44% Event list: [time, type (-1=dep/+1=arv), sortedIdx,
class]
45events = zeros(2*n, 4);
46events(1:n, :) = [at_sorted, ones(n,1), (1:n)
', class_sorted];
47events(n+1:2*n, :) = [exitTimes, -ones(n,1), (1:n)', class_sorted];
49% Sort by time; departures (-1) before arrivals (+1) at same time
50events = sortrows(events, [1, 2]);
53ql_sorted = zeros(n, R);
56 if events(i, 2) == 1 % arrival
57 state(c) = state(c) + 1;
58 ql_sorted(events(i, 3), :) = state;
60 state(c) = state(c) - 1;
64% Unsort back to original input order
66ql(sortIdx, :) = ql_sorted;