A production-grade simulator for dynamic load balancing algorithms with AI-powered optimization
Quick Start โข Features โข Algorithms โข Documentation โข Contributing
# Clone the repository
git clone https://github.com/Auankj/dynamic_load_balancer.git
cd dynamic_load_balancer
# Create virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Launch the GUI
python main.py๐ผ๏ธ Screenshot Preview
The GUI features:
- Real-time processor load visualization with color-coded bars
- Interactive Gantt chart showing process execution timeline
- Live metrics dashboard with performance statistics
- Algorithm comparison with side-by-side analysis
Load balancing is a critical operating system technique that distributes workloads across multiple processors to maximize efficiency. This simulator provides:
| Goal | Description |
|---|---|
| ๐ Maximize Throughput | Complete more work in less time |
| โก Minimize Response Time | Users get faster responses |
| โ๏ธ Optimize Utilization | All processors stay busy |
| ๐ก๏ธ Prevent Bottlenecks | No single processor gets overwhelmed |
| Feature | Description |
|---|---|
| Multi-Processor | Configure 2-16 virtual processors with customizable speed |
| Process Types | CPU-bound, I/O-bound, Real-time, Batch, Interactive |
| Workload Patterns | Uniform, Bursty, Poisson, Diurnal, Spike, Wave |
| 13 Algorithms | Load Balancing + Classic CPU Scheduling |
| AI-Powered | Deep reinforcement learning with PyTorch (GPU accelerated) |
| Process Migration | Dynamic load rebalancing across processors |
| Feature | Description |
|---|---|
| Q-Learning | Discrete state-space reinforcement learning |
| Deep Q-Network (DQN) | Neural network with experience replay |
| Double DQN | Reduced overestimation bias |
| Prioritized Replay | Focus on important experiences |
| Model Persistence | Save/load trained models automatically |
| Feature | Description |
|---|---|
| Process Types | CPU_BOUND, IO_BOUND, MIXED, REAL_TIME, BATCH, INTERACTIVE |
| Workload Patterns | UNIFORM, BURSTY, POISSON, DIURNAL, SPIKE, GRADUAL_RAMP, WAVE |
| Advanced Processors | Multi-level feedback queue, cache simulation, power states |
| Scenario System | Predefined and custom simulation scenarios |
| SLA Tracking | Service Level Agreement metrics and violations |
Real-time visualization of processor loads with intelligent color-coding:
0% 50% 100%
โโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
๐ข Green (0-40%) ๐ก Yellow (40-70%) ๐ด Red (70-100%)
- Load Bars: Animated horizontal bars showing current CPU utilization
- Color Gradient: Smooth transitions between load states
- Queue Indicators: Number badges showing pending processes per CPU
Interactive process execution timeline with rich details:
- Process Blocks: Color-coded by process priority/type
- Time Scale: Auto-adjusting based on simulation duration
- Hover Details: Process ID, burst time, start/end times
- Migration Lines: Dashed connectors showing process migrations
- Zoom & Pan: Mouse wheel zoom, drag to navigate
Live updating charts with multiple metrics:
| Metric Type | Visualization | Update Frequency |
|---|---|---|
| CPU Utilization | Line chart with fill | Every simulation tick |
| Throughput | Stacked area chart | Per completed process |
| Wait Times | Box plot with outliers | Per process completion |
| Queue Depths | Multi-line overlay | Every scheduling decision |
| Load Variance | Gradient heat strip | Continuous |
Side-by-side performance analysis for all 13 algorithms:
- Grouped Bar Charts: Compare avg wait, turnaround, response times
- Radar Charts: Multi-dimensional algorithm profiling
- Ranking Tables: Sortable by any metric
- Export Options: PNG, SVG, CSV data export
| Chart Type | Description |
|---|---|
| ๐ Queue Visualization | Ready queue depth per processor with process IDs |
| ๐ฏ Fairness Index | Jain's fairness index dial (0.0 to 1.0) |
| โฑ๏ธ Response Time Distribution | Histogram with percentile markers (p50, p95, p99) |
| ๐ฅ Utilization Heatmap | Time ร Processor matrix showing load intensity |
| ๐ Process Timeline | Individual process lifecycle from arrival to completion |
| ๐ก๏ธ System Temperature | Overall system load gauge with thresholds |
- Process Metrics โ Turnaround time, waiting time, response time
- Processor Metrics โ CPU utilization, queue length, throughput
- System Metrics โ Load variance, Jain's fairness index, migrations
- Data Export โ JSON and CSV export for external analysis
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ GUI Layer โ
โ (gui.py) โ
โ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ
โ โ Load Bars โ โ Metrics โ โ Charts โ โ Controls โ โ
โ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Simulation Layer โ
โ (simulation.py / enhanced_simulation.py) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ SimulationEngine โ โ
โ โ Time Management โข Event Processing โข State Control โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโฌโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโผโโโโโโโ โโโโโโโโโผโโโโโโโโ โโโโโโโโโผโโโโโโโโ
โLoad Balancerโ โ Processor โ โ Metrics โ
โ โ โ โ โ โ
โโข RoundRobin โ โโข Execution โ โโข Process โ
โโข LeastLoadedโ โโข Queue Mgmt โ โโข Processor โ
โโข Threshold โ โโข Migration โ โโข System โ
โโข Q-Learning โ โโข Power States โ โโข SLA Tracking โ
โโข DQN โ โโข Cache Sim โ โ โ
โโโโโโโโฌโโโโโโโ โโโโโโโโโฌโโโโโโโโ โโโโโโโโโโโโโโโโโ
โ โ
โโโโโโโโผโโโโโโโโโโโโโโโโโโโผโโโโโโโ
โ Core Layer โ
โ config.py โข process.py โ
โ utils.py โข validators.py โ
โ advanced_simulation.py โ
โ integration.py โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Pattern | Implementation | Purpose |
|---|---|---|
| Strategy | LoadBalancer ABC | Swappable algorithms |
| Factory | LoadBalancerFactory | Algorithm instantiation |
| Observer | GUI callbacks | Real-time updates |
| Builder | ScenarioBuilder | Custom scenario creation |
| Singleton | SimulationLogger | Centralized logging |
| Algorithm | Speed | Balance | Adaptability | Best For |
|---|---|---|---|---|
| Round Robin | โญโญโญ | โญ | โญ | Uniform workloads |
| Least Loaded | โญโญ | โญโญโญ | โญโญ | Variable workloads |
| Threshold | โญโญ | โญโญโญ | โญโญโญ | Dynamic environments |
| Q-Learning | โญโญโญ | โญโญโญ | โญโญโญ | Pattern-rich workloads |
| DQN | โญโญ | โญโญโญ | โญโญโญ | Complex continuous states |
Distributes processes cyclically: P0โP1โP2โP3โP0...
def assign(self, process, processors):
target = processors[self.current_index]
self.current_index = (self.current_index + 1) % len(processors)
return targetโ
Simple, predictable, zero overhead
โ Ignores actual load, can create imbalance
Assigns to the processor with the lowest current load
def assign(self, process, processors):
return min(processors, key=lambda p: p.current_load)โ
Optimal distribution, adapts to state
โ O(n) per assignment, monitoring overhead
Migrates processes when load difference exceeds threshold
def check_balance(self, processors):
loads = [p.current_load for p in processors]
if max(loads) - min(loads) > self.threshold:
self.migrate_process(overloaded, underloaded)โ
Dynamic rebalancing, prevents severe imbalance
โ Migration has cost, needs threshold tuning
Learns optimal assignments through reinforcement learning
def assign(self, process, processors):
state = self.encode_state(processors, process)
if self.training and random() < self.epsilon:
action = random_choice(len(processors)) # Explore
else:
action = argmax(self.Q[state]) # Exploit
return processors[action]โ
Learns optimal strategy, improves over time
โ Needs training, initial random behavior
Neural network approximates Q-function for continuous states
class DQNetwork(nn.Module):
def __init__(self, state_dim, action_dim):
self.fc1 = nn.Linear(state_dim, 128)
self.fc2 = nn.Linear(128, 256)
self.fc3 = nn.Linear(256, 128)
self.out = nn.Linear(128, action_dim)โ
Handles continuous states, excellent generalization
โ Requires PyTorch, more computationally intensive
| Mode | Exploration (ฮต) | Purpose | When to Use |
|---|---|---|---|
| Train | 100% โ 5% | Learn optimal strategies | First runs, new patterns |
| Exploit | Fixed 1% | Use learned knowledge | After training complete |
Recommended Training:
- Q-Learning: 500-2000+ process assignments
- DQN: 1000-5000+ process assignments
The simulator also includes 8 classic CPU scheduling algorithms that every OS student should know!
| Algorithm | Type | Preemptive | Optimal For | Starvation |
|---|---|---|---|---|
| FCFS | ๐ FIFO | โ | Simplicity | โ |
| SJF | โฑ๏ธ Burst | โ | Avg Wait Time | |
| SRTF | โฑ๏ธ Burst | โ | Response Time | |
| Priority | ๐ฏ Priority | โ | Critical Tasks | |
| Priority (P) | ๐ฏ Priority | โ | Urgent Tasks | |
| Multilevel Queue | ๐ Class | โ | Mixed Workloads | |
| MLFQ | ๐ง Adaptive | โ | General Purpose | โ |
| EDF | โฐ Deadline | โ | Real-Time | โ |
The OG of schedulers. Whoever comes first gets the CPU first.
def select_next(self):
return sorted(self.queue, key=lambda p: p.arrival_time)[0]โ
Simple, no starvation, minimal overhead
โ Convoy effect โ one big process blocks everyone
Picks the process with the shortest burst time. Productivity king! ๐
def select_next(self):
return min(self.queue, key=lambda p: p.burst_time)โ
Optimal average waiting time (provably!)
โ Long jobs may starve, needs burst time prediction
The chaotic younger sibling of SJF. Preemptive version!
def should_preempt(self, new_process):
return new_process.remaining_time < self.current.remaining_timeโ
Even better response time than SJF
โ High overhead, long jobs get ghosted constantly ๐ป
CPU goes to the highest priority process. VIP treatment! ๐๏ธ
def select_next(self):
# Uses aging to prevent starvation
for p in self.queue:
p.effective_priority = p.priority - p.wait_time // 10
return min(self.queue, key=lambda p: p.effective_priority)โ
Critical processes get attention, flexible
โ Can starve low priority (solved with aging!)
Every process gets a time slice (quantum). Fair & democratic! ๐ณ๏ธ
def tick(self):
self.quantum_remaining -= 1
if self.quantum_remaining <= 0:
self.queue.append(self.current) # Back of queue
self.current = self.queue.popleft()โ
Fair, good response time, no starvation
โ Quantum too small = too many switches, too big = becomes FCFS
Think of it like a school with different sections! ๐ซ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Queue 0: System Processes (RR, q=8) โ โ Highest Priority
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Queue 1: Interactive (RR, q=4) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Queue 2: Batch Jobs (FCFS) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Queue 3: Idle Processes โ โ Lowest Priority
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
Good for categorized workloads, optimizes each queue
โ Inflexible โ processes stuck in their queue
The genius, adaptive version. Processes can MOVE between queues! ๐ง
The Rules:
- ๐ New processes start at top queue (highest priority)
- โฌ๏ธ Use full quantum? Get demoted to lower queue
- โฌ๏ธ Give up CPU early (I/O)? Stay or get promoted
- ๐ Periodic priority boost prevents starvation
def after_quantum(self, process):
if process.used_full_quantum:
self.demote(process) # CPU hog detected!
else:
self.promote(process) # Nice I/O-bound processโ
Adapts automatically, no prior knowledge needed
โ Complex to tune, can be gamed
For real-time systems. Nearest deadline = gets the CPU! โฐ
def select_next(self):
return min(self.queue, key=lambda p: p.deadline)โ
Optimal for single processor real-time (can achieve 100% utilization!)
โ Deadline miss cascade โ one miss can cause many
dynamic_load_balancer/
โโโ ๐ฏ Core Modules
โ โโโ main.py # Application entry point
โ โโโ config.py # Configuration and constants
โ โโโ process.py # Process model and generator
โ โโโ processor.py # Processor execution logic
โ โโโ simulation.py # Standard simulation engine
โ
โโโ ๐ค AI & Scheduling Modules
โ โโโ load_balancer.py # Algorithm implementations
โ โโโ ai_balancer.py # Q-Learning balancer
โ โโโ dqn_balancer.py # Deep Q-Network balancer
โ โโโ scheduling_algorithms.py # Classic CPU schedulers (FCFS, SJF, etc.)
โ
โโโ ๐ Advanced Simulation
โ โโโ advanced_simulation.py # Enhanced process/processor models
โ โโโ enhanced_simulation.py # Production-grade engine
โ โโโ integration.py # Scenario management
โ
โโโ ๐ Support Modules
โ โโโ metrics.py # Performance metrics
โ โโโ gui.py # Tkinter GUI
โ โโโ utils.py # Logging and export
โ โโโ validators.py # Input validation
โ
โโโ ๐งช Testing
โ โโโ test_suite.py # 133 comprehensive tests
โ
โโโ ๐ Documentation
โโโ README.md # This file
โโโ requirements.txt # Dependencies
| Module | Purpose | Key Classes |
|---|---|---|
config.py |
Configuration | SimulationConfig, LoadBalancingAlgorithm |
process.py |
Process model | Process, ProcessGenerator |
processor.py |
Execution | Processor, ProcessorManager |
load_balancer.py |
Algorithms | RoundRobin, LeastLoaded, Threshold |
ai_balancer.py |
Q-Learning | QLearningAgent, StateEncoder |
dqn_balancer.py |
Deep RL | DQNAgent, DQNetwork, PrioritizedReplay |
scheduling_algorithms.py |
CPU Scheduling | FCFS, SJF, SRTF, Priority, MLFQ, EDF |
advanced_simulation.py |
Advanced models | AdvancedProcess, AdvancedProcessor |
enhanced_simulation.py |
Production engine | EnhancedSimulationEngine |
integration.py |
Scenarios | ScenarioBuilder, PerformanceAnalyzer |
| Scenario | Processors | Processes | Pattern | Description |
|---|---|---|---|---|
| Basic | 4 | 20 | Uniform | Standard simulation |
| CPU Intensive | 8 | 30 | Uniform | Long-running computation |
| I/O Intensive | 4 | 40 | Bursty | Frequent blocking |
| Mixed Workload | 6 | 50 | Diurnal | Real-world simulation |
| Bursty Traffic | 4 | 60 | Spike | Sudden load spikes |
| Real-Time | 8 | 25 | Uniform | Strict deadlines |
| Stress Test | 4 | 100 | Spike | Maximum load testing |
| Metric | Formula | Description |
|---|---|---|
| Turnaround Time | completion - arrival |
Total time in system |
| Waiting Time | start - arrival |
Time in queue |
| Response Time | first_run - arrival |
Time to first execution |
| CPU Utilization | busy_time / total_time |
Processor efficiency |
| Throughput | completed / time |
Processes per time unit |
| Jain's Fairness | (ฮฃx)ยฒ / (n ร ฮฃxยฒ) |
Load distribution fairness |
- J = 1.0: Perfect fairness (equal load)
- J = 1/n: Worst case (all load on one processor)
# Run all 125 tests
python -m pytest test_suite.py -v
# Run specific test class
python -m pytest test_suite.py::TestDQNBalancer -v
# Run with coverage
python -m pytest test_suite.py --cov=. --cov-report=html| Category | Tests | Coverage |
|---|---|---|
| Configuration | 6 | Config creation, defaults |
| Process Model | 12 | Lifecycle, state transitions |
| Processor | 14 | Execution, queue management |
| Load Balancers | 20 | All algorithm correctness |
| Q-Learning | 15 | Agent training, inference |
| DQN | 20 | Neural network, replay buffer |
| Simulation | 12 | Engine initialization, execution |
| Metrics | 13 | Calculations, edge cases |
| Integration | 6 | End-to-end workflows |
| Edge Cases | 7 | Boundary conditions |
# Create and run simulation
from simulation import SimulationEngine
from config import SimulationConfig, LoadBalancingAlgorithm
config = SimulationConfig(num_processors=4, num_processes=20)
engine = SimulationEngine(config)
engine.initialize(algorithm=LoadBalancingAlgorithm.DQN)
while not engine.is_complete():
engine.step()
result = engine.get_result()
print(f"Avg Turnaround: {result.system_metrics.avg_turnaround_time:.2f}")# Use scenario builder
from integration import ScenarioBuilder, IntegratedSimulationManager
from advanced_simulation import WorkloadPattern, ProcessType
scenario = (ScenarioBuilder("Custom Test")
.with_processors(8)
.with_processes(50)
.with_workload(WorkloadPattern.BURSTY)
.with_algorithm(LoadBalancingAlgorithm.DQN)
.build())
manager = IntegratedSimulationManager(use_enhanced=True)
manager.load_scenario(scenario)
manager.initialize()
manager.start()| Parameter | Default | Range | Description |
|---|---|---|---|
num_processors |
4 | 2-16 | Number of processors |
num_processes |
20 | 1-100 | Processes to generate |
time_quantum |
4 | 1-20 | Round robin time slice |
min_burst_time |
1 | 1-100 | Minimum burst time |
max_burst_time |
20 | 1-1000 | Maximum burst time |
migration_threshold |
0.3 | 0.0-1.0 | Load diff for migration |
source venv/bin/activate
python main.py.\venv\Scripts\Activate.ps1
python main.pyvenv\Scripts\activate.bat
python main.py- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Write tests for your changes
- Ensure all 125 tests pass:
python -m pytest test_suite.py - Commit with conventional format:
feat(scope): description - Push to your branch:
git push origin feature/amazing-feature - Open a Pull Request
type(scope): Brief description
Types: feat, fix, docs, refactor, test, perf
Examples:
- feat(balancer): Add weighted round robin
- fix(gui): Resolve chart rendering issue
- test(dqn): Add edge case tests
This project is licensed under the MIT License โ see the LICENSE file for details.