feat: implement Multilevel Queue (MLQ) scheduling algorithm

- Added MLQ scheduling logic in multilevel.ts, including queue selection, preemption rules, and quantum expiration checks.
- Updated SchedulerConfig to include mlqQueues for MLQ configuration.
- Created default MLQ queue definitions for use in presets and process store.
- Modified process store to auto-increment process IDs.
- Enhanced tests for MLQ scheduling, covering selection, preemption, quantum expiration, and integration scenarios.
- Updated existing presets to support MLQ and adjusted process definitions to include auto-incremented IDs.
This commit is contained in:
Lucas Guerra committed 2026-03-19 18:28:04 -03:00
1 parent 64bbc4ca27
commit 9c552e53fa
12 files changed
+550 -67

No files matched your search

+6 -1
View File
@@ -23,7 +23,7 @@ const MetricsTable = ({ metrics, processMap }: MetricsTableProps) => {
return (
<div className="overflow-x-auto">
<table className="w-full text-xs text-slate-300 border-collapse min-w-[700px]">
<table className="w-full text-xs text-slate-300 border-collapse min-w-175">
<thead>
<tr className="text-[10px] text-slate-400 uppercase tracking-wider border-b border-slate-700">
<th className="text-left px-3 py-2 font-medium">Processo</th>
@@ -55,6 +55,11 @@ const MetricsTable = ({ metrics, processMap }: MetricsTableProps) => {
className="inline-block w-2.5 h-2.5 rounded-full shrink-0"
style={{ backgroundColor: color }}
/>
{proc?.pid !== undefined && (
<span className="text-[11px] font-mono font-semibold text-slate-400 bg-slate-700 px-1.5 py-0.5 rounded shrink-0">
#{proc.pid}
</span>
)}
<span className="font-medium text-slate-200">{name}</span>
</div>
</td>
+7 -2
View File
@@ -24,7 +24,7 @@ const ProcessCard = ({ process, onEdit, onRemove }: ProcessCardProps) => {
>
{/* Color indicator */}
<div
className="mt-0.5 h-8 w-1.5 rounded-full flex-shrink-0"
className="mt-0.5 h-8 w-1.5 rounded-full shrink-0"
style={{ backgroundColor: process.color }}
aria-hidden="true"
/>
@@ -32,6 +32,11 @@ const ProcessCard = ({ process, onEdit, onRemove }: ProcessCardProps) => {
{/* Info */}
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
{process.pid !== undefined && (
<span className="text-[11px] font-mono font-semibold text-slate-400 bg-slate-700 px-1.5 py-0.5 rounded shrink-0">
#{process.pid}
</span>
)}
<span className="font-semibold text-sm text-slate-100 truncate">{process.name}</span>
<Badge variant={boundVariant}>{bound}</Badge>
</div>
@@ -47,7 +52,7 @@ const ProcessCard = ({ process, onEdit, onRemove }: ProcessCardProps) => {
</div>
{/* Actions */}
<div className="flex gap-1 opacity-0 group-hover:opacity-100 transition-opacity flex-shrink-0">
<div className="flex gap-1 opacity-0 group-hover:opacity-100 transition-opacity shrink-0">
<IconButton
size="sm"
variant="ghost"
+5 -6
View File
@@ -1,6 +1,6 @@
import { useState, useEffect } from 'react';
import { Plus, Minus, Check, X } from 'lucide-react';
import type { Process, BurstSegment } from '@core/types';
import type { BurstSegment, Process } from '@core/types';
import { useProcessStore } from '../../store/processStore';
import IconButton from '../shared/IconButton';
import { generateId, nextColor } from '../shared/processUtils';
@@ -32,7 +32,7 @@ const ProcessForm = ({ editingProcess, onClose }: ProcessFormProps) => {
setArrivalTime(editingProcess.arrivalTime);
setPriority(editingProcess.priority);
setColor(editingProcess.color);
setBursts([...editingProcess.bursts.map(b => ({ ...b }))]);
setBursts([...editingProcess.bursts.map((b: BurstSegment) => ({ ...b }))]);
} else {
setName('');
setArrivalTime(0);
@@ -89,15 +89,14 @@ const ProcessForm = ({ editingProcess, onClose }: ProcessFormProps) => {
if (isEditing && editingProcess) {
updateProcess(editingProcess.id, { name: name.trim(), arrivalTime, priority, color, bursts });
} else {
const p: Process = {
addProcess({
id: generateId(),
name: name.trim(),
arrivalTime,
priority,
color,
bursts,
};
addProcess(p);
});
}
onClose();
};
@@ -141,7 +140,7 @@ const ProcessForm = ({ editingProcess, onClose }: ProcessFormProps) => {
type="color"
value={color}
onChange={e => setColor(e.target.value)}
className="h-[34px] w-12 rounded-md border border-slate-600 bg-slate-700 cursor-pointer"
className="h-8.5 w-12 rounded-md border border-slate-600 bg-slate-700 cursor-pointer"
aria-label="Cor do processo"
/>
</div>
@@ -10,7 +10,7 @@ const ALGORITHM_OPTIONS: { value: SchedulingAlgorithm; label: string; disabled?:
{ value: 'PRIORITY_P', label: 'Priority (Preemptivo)' },
{ value: 'PRIORITY_RR',label: 'Priority Round Robin (tempo real)' },
{ value: 'HRRN', label: 'HRRN — Highest Response Ratio Next' },
{ value: 'MULTILEVEL', label: 'Multilevel Queue (em breve)', disabled: true },
{ value: 'MULTILEVEL', label: 'Multilevel Queue (MLQ)' },
];
const AlgorithmSelector = () => {
@@ -1,8 +1,9 @@
import { Plus, Trash2 } from 'lucide-react';
import { useProcessStore } from '../../store/processStore';
import type { SchedulingAlgorithm } from '@core/types';
import type { MlqQueueDef, SchedulingAlgorithm } from '@core/types';
// Algorithms that require a quantum parameter
const QUANTUM_ALGORITHMS = new Set<SchedulingAlgorithm>(['RR', 'PRIORITY_RR', 'MULTILEVEL']);
// Algorithms that require a global quantum parameter
const QUANTUM_ALGORITHMS = new Set<SchedulingAlgorithm>(['RR', 'PRIORITY_RR']);
// Algorithms where aging (anti-starvation) is applicable
const AGING_ALGORITHMS = new Set<SchedulingAlgorithm>(['PRIORITY_NP', 'PRIORITY_P', 'PRIORITY_RR']);
@@ -44,6 +45,114 @@ const ToggleRow = ({
</div>
);
const MLQ_ALGO_OPTIONS: MlqQueueDef['algorithm'][] = ['RR', 'PRIORITY_NP', 'FCFS'];
const MlqQueuesConfig = ({
queues,
onChange,
}: {
queues: MlqQueueDef[];
onChange: (queues: MlqQueueDef[]) => void;
}) => {
const update = (idx: number, patch: Partial<MlqQueueDef>) => {
onChange(queues.map((q, i) => (i === idx ? { ...q, ...patch } : q)));
};
const addQueue = () => {
if (queues.length >= 5) return;
onChange([...queues, { priorityMin: 1, priorityMax: 10, algorithm: 'FCFS', quantum: 2 }]);
};
const removeQueue = (idx: number) => {
if (queues.length <= 1) return;
onChange(queues.filter((_, i) => i !== idx));
};
return (
<div className="space-y-2 pt-1" data-testid="mlq-queues-config">
<div className="flex items-center justify-between">
<span className="text-xs text-slate-400 font-medium">Filas MLQ (prioridade decrescente)</span>
<button
onClick={addQueue}
disabled={queues.length >= 5}
className="flex items-center gap-1 text-[10px] text-blue-400 hover:text-blue-300 disabled:text-slate-600 disabled:cursor-not-allowed transition-colors"
aria-label="Adicionar fila"
>
<Plus size={11} /> Fila
</button>
</div>
{queues.map((q, idx) => (
<div
key={idx}
className="rounded-md border border-slate-600 bg-slate-800/60 p-2 space-y-1.5"
data-testid={`mlq-queue-${idx}`}
>
<div className="flex items-center justify-between">
<span className="text-[10px] text-slate-400 font-semibold uppercase tracking-wide">
Fila {idx} {idx === 0 ? '(mais alta)' : idx === queues.length - 1 ? '(mais baixa)' : ''}
</span>
<button
onClick={() => removeQueue(idx)}
disabled={queues.length <= 1}
className="text-slate-500 hover:text-red-400 disabled:text-slate-700 disabled:cursor-not-allowed transition-colors"
aria-label={`Remover fila ${idx}`}
>
<Trash2 size={11} />
</button>
</div>
<div className="grid grid-cols-2 gap-1.5">
<div>
<label className="block text-[10px] text-slate-500 mb-0.5">Prio. mín</label>
<input
type="number" min={1} max={10}
value={q.priorityMin}
onChange={e => update(idx, { priorityMin: Math.min(10, Math.max(1, Number(e.target.value))) })}
className="w-full rounded bg-slate-700 border border-slate-600 text-slate-200 text-xs px-2 py-1 focus:outline-none focus:ring-1 focus:ring-blue-500"
aria-label={`Fila ${idx}: prioridade mínima`}
/>
</div>
<div>
<label className="block text-[10px] text-slate-500 mb-0.5">Prio. máx</label>
<input
type="number" min={1} max={10}
value={q.priorityMax}
onChange={e => update(idx, { priorityMax: Math.min(10, Math.max(1, Number(e.target.value))) })}
className="w-full rounded bg-slate-700 border border-slate-600 text-slate-200 text-xs px-2 py-1 focus:outline-none focus:ring-1 focus:ring-blue-500"
aria-label={`Fila ${idx}: prioridade máxima`}
/>
</div>
</div>
<div>
<label className="block text-[10px] text-slate-500 mb-0.5">Algoritmo</label>
<select
value={q.algorithm}
onChange={e => update(idx, { algorithm: e.target.value as MlqQueueDef['algorithm'] })}
className="w-full rounded bg-slate-700 border border-slate-600 text-slate-200 text-xs px-2 py-1 focus:outline-none focus:ring-1 focus:ring-blue-500 cursor-pointer"
aria-label={`Fila ${idx}: algoritmo`}
>
{MLQ_ALGO_OPTIONS.map(a => (
<option key={a} value={a}>{a}</option>
))}
</select>
</div>
{q.algorithm === 'RR' && (
<div>
<label className="block text-[10px] text-slate-500 mb-0.5">Quantum</label>
<input
type="number" min={1}
value={q.quantum}
onChange={e => update(idx, { quantum: Math.max(1, Number(e.target.value)) })}
className="w-full rounded bg-slate-700 border border-slate-600 text-slate-200 text-xs px-2 py-1 focus:outline-none focus:ring-1 focus:ring-blue-500"
aria-label={`Fila ${idx}: quantum`}
/>
</div>
)}
</div>
))}
</div>
);
};
const SchedulerConfig = () => {
const config = useProcessStore(s => s.config);
const setConfig = useProcessStore(s => s.setConfig);
@@ -133,6 +242,14 @@ const SchedulerConfig = () => {
)}
</div>
)}
{/* MLQ Queue Configuration */}
{algo === 'MULTILEVEL' && (
<MlqQueuesConfig
queues={config.mlqQueues ?? []}
onChange={queues => setConfig({ mlqQueues: queues })}
/>
)}
</div>
);
};
@@ -65,10 +65,10 @@ describe('SchedulerConfig', () => {
expect(screen.getByLabelText('Quantum do Round Robin')).toBeInTheDocument();
});
it('shows Quantum field when algorithm is MULTILEVEL', () => {
it('shows MLQ queue config panel when algorithm is MULTILEVEL', () => {
setAlgorithm('MULTILEVEL');
render(<SchedulerConfig />);
expect(screen.getByTestId('quantum-field')).toBeInTheDocument();
expect(screen.getByTestId('mlq-queues-config')).toBeInTheDocument();
});
it('does NOT show Quantum field for SJF_NP', () => {