Simulation and ModelingUnit 810 min read
Computer System Simulation: Queues, Schedulers & Performance
Unit 8 of Simulation and Modeling explores how to model CPU scheduling, memory allocation, and network traffic using discrete-event simulation, queueing theory, and probabilistic workloads—key for designing efficient IT systems like cloud servers or Ncell’s call routing.
Core Concepts
1. Why Simulate Computer Systems?
Computer systems (CPUs, networks, databases) are complex, dynamic, and probabilistic. Simulation lets us:
- Predict bottlenecks (e.g., Ncell’s call drops during peak hours).
- Test new algorithms (e.g., Google’s load balancers) without real-world risks.
- Optimize resource use (e.g., Daraz’s server farms during sales).
Real-world tie:
- WhatsApp’s message routing: Uses priority queues to prioritize urgent messages (e.g., OTPs) over regular chats. Simulation helps balance server load across regions.
- Nepal Rastra Bank’s core banking: Models transaction queues to avoid crashes during Diwali season.
- Pathao’s driver dispatch: Simulates geospatial queues to match riders to nearest drivers faster.
2. Key Components of Computer System Simulation
A. Discrete-Event Simulation (DES) for IT Systems
DES models events like:
- Job arrival (e.g., a user submitting a request to eSewa).
- CPU scheduling (e.g., switching between tasks in a virtual machine).
- Network packet arrival (e.g., data packets in NTC’s fiber-optic backbone).
How it works:
- Event list: A priority queue of pending events (e.g.,
[Task1 arrives at t=2s, Task2 completes at t=5s]). - Simulation clock: Advances only when an event occurs (no wasted computation).
- State updates: Modify system state (e.g., CPU switches from Task1 to Task2).
flowchart LR
A["Event List (Priority Queue)"] -->|"Process Next Event"| B["Simulation Clock"]
B --> C["Update System State"]
C --> D["Schedule Next Event"]
D --> AWorked Example: CPU Scheduling Simulation Assume a single-core CPU with these tasks:
| Task | Arrival Time (ms) | Burst Time (ms) |
|---|---|---|
| T1 | 0 | 5 |
| T2 | 1 | 3 |
| T3 | 2 | 2 |
Simulation Steps:
- t=0ms: T1 arrives → CPU starts T1. Event: T1 completes at t=5ms.
- t=1ms: T2 arrives → CPU is busy (T1 running). Event: T2 completes at t=4ms (1ms wait + 3ms burst).
- t=2ms: T3 arrives → CPU still busy. Event: T3 completes at t=6ms (4ms wait + 2ms burst).
- t=4ms: T2 completes → CPU switches to T3.
- t=5ms: T1 completes → CPU idle (all tasks done).
Visualization of CPU State Over Time:
Time (ms) | CPU State | Queue
----------|-----------------|-----------------------
0 | Running T1 | [T2, T3]
1 | Running T1 | [T2, T3]
2 | Running T1 | [T2, T3]
3 | Running T1 | [T2, T3]
4 | Running T2 | [T3]
5 | Idle | []
6 | Done | []
B. Queueing Models for IT Systems
Most computer systems involve queues:
- CPU scheduling queues (Ready, Blocked, I/O).
- Network buffers (e.g., NTC’s packet queues).
- Database transaction logs (e.g., NEPSE’s order matching).
Common Queue Types in IT:
| Queue Type | Example | Service Discipline |
|---|---|---|
| FIFO (First-In-First-Out) | Print spooler in Windows | Tasks processed in arrival order |
| Priority Queue | WhatsApp OTPs over chats | High-priority tasks first |
| Round Robin | Linux’s time-slicing scheduler | Equal time slices per task |
| Shortest Job First | Cloud auto-scaling (AWS) | Shortest task next |
Worked Example: Network Router Queue A router has a buffer of size 3 packets. Packets arrive with random inter-arrival times (exponential distribution, λ=2 packets/sec). Service time is constant (100ms per packet).
Simulation Trace:
| Time (s) | Event | Queue State | Action |
|---|---|---|---|
| 0.0 | Packet 1 arrives | [P1] | Start transmitting P1 |
| 0.1 | Packet 2 arrives | [P1, P2] | P1 still transmitting |
| 0.2 | Packet 3 arrives | [P1, P2, P3] | Buffer full (P3 dropped) |
| 0.3 | P1 completes | [P2, P3] | Start P2 |
| 0.4 | Packet 4 arrives | [P2, P3, P4] | Buffer full (P4 dropped) |
Visualization of Packet Queue:
graph LR
A["Packet 1"] -->|"Transmitted"| B["Packet 2"]
B --> C["Packet 3"]
C --> D["Packet 4 (Dropped)"]Key Metrics:
- Throughput: Packets/sec successfully transmitted = 2/3 ≈ 0.67 packets/sec.
- Drop Rate: 2/4 = 50% (high! Need a larger buffer or faster CPU).
C. Probabilistic Workloads
Real-world IT systems have random arrival/service times. Common distributions:
- Exponential: Models unpredictable events (e.g., user clicks on Daraz).
- Mean inter-arrival time = (λ = arrival rate).
- Uniform: Fixed range (e.g., CPU burst time between 1–5ms).
- Normal: Clustered around a mean (e.g., Ncell call durations).
Worked Example: eSewa Transaction Processing Assume:
- Transactions arrive exponentially (λ=5 transactions/min).
- Service time is uniform (1–3 seconds).
Simulation for 10 minutes:
- Generate 50 arrival times using exponential distribution.
- Generate 50 service times using uniform distribution.
- Simulate a queue with a single server (e.g., eSewa’s payment processor).
Sample Output:
| Transaction | Arrival Time (s) | Service Time (s) | Start Time (s) | Completion Time (s) | Wait Time (s) |
|---|---|---|---|---|---|
| T1 | 0 | 2.1 | 0 | 2.1 | 0 |
| T2 | 0.5 | 1.8 | 2.1 | 3.9 | 1.6 |
| T3 | 1.2 | 2.5 | 3.9 | 6.4 | 2.7 |
Visualization of eSewa Queue:
gantt
title eSewa Transaction Queue (10s Window)
dateFormat HH:mm:ss
section Server
T1 :a1, 0, 2s
T2 :a2, 2s, 2s
T3 :a3, 4s, 3s
section Queue
T2 :crit, 0s, 2s
T3 :crit, 2s, 2sKey Insight:
- Average wait time = (1.6 + 2.7 + ...) / 50 ≈ 1.2 seconds.
- Server utilization = Total busy time / Total time ≈ 75%.
3. Simulation Tools for Computer Systems
| Tool | Use Case | Example Output |
|---|---|---|
| SimPy (Python) | CPU scheduling, network routing | Gantt charts, queue length graphs |
| OMNeT++ | Packet-level network simulation | Packet delay histograms |
| CloudSim | Cloud data center modeling | VM allocation heatmaps |
| NS-3 | Wireless network protocols | Throughput vs. interference graphs |
4. Validation and Verification
Simulation results must be realistic. Methods:
- Tracing: Compare simulation logs to real system traces (e.g., Ncell’s call logs).
- Analytical Models: Use queueing theory (e.g., M/M/1 for CPU scheduling).
- Benchmarking: Test against known workloads (e.g., TPC-C for databases).
Worked Example: Validating a Web Server Simulation
- Real Data: Google’s web server handles 1000 requests/sec with 95% <200ms latency.
- Simulation: Your model shows 900 requests/sec with 90% <200ms.
- Action: Increase buffer size or optimize CPU scheduling.
5. Applications in Nepal’s IT Sector
| Company/Product | Simulation Use Case | Benefit |
|---|---|---|
| Ncell | Call routing queue simulation | Reduce drop calls during festivals |
| eSewa | Transaction processing workload modeling | Handle Diwali rush without crashes |
| NTC | Fiber-optic network traffic simulation | Optimize bandwidth allocation |
| Pathao | Driver-rider matching queue optimization | Faster pickups, lower wait times |
| Nepal Rastra Bank | ATM transaction queue modeling | Reduce customer wait times |
Exam Tip
- Focus on queueing theory: Know M/M/1, M/G/1, and G/G/1 notations for CPU/network models.
- Draw timelines: For CPU scheduling, always show a Gantt chart.
- Calculate metrics: Expect questions on throughput, utilization, and wait time.
- Compare algorithms: Contrast FCFS, Round Robin, and Priority Scheduling in terms of fairness and efficiency.
- Real-world mapping: Relate simulations to Ncell’s call drops, eSewa’s transaction limits, or Daraz’s server crashes.
Key Formula Cheat Sheet:
| Metric | Formula | Example Context |
|---|---|---|
| Utilization (ρ) | CPU busy fraction | |
| Average Queue Length (Lq) | Number of waiting tasks | |
| Average Wait Time (Wq) | Time spent waiting in queue | |
| Throughput (X) | Tasks completed per second |
Visual Summary of CPU Scheduling Algorithms:
mindmap
root((CPU Scheduling))
FCFS
Pros: Simple
Cons: Convoy effect
Round Robin
Pros: Fair
Cons: Overhead
Priority
Pros: Urgent tasks first
Cons: Starvation
SJF
Pros: Optimal for short jobs
Cons: Hard to predict burst timeBased on the TU BIT syllabus for Simulation and Modeling, unit 8.
Discussion
Loading…