Kernel Resource Manager
Overview
The Kernel Resource Manager provides comprehensive, heuristic-based resource management for FreeWorld OS. It uses static, deterministic algorithms that are:
- Fast: Lightweight, fast execution in kernel critical path
- Predictable: Deterministic behavior, easy to debug
- Reliable: Battle-tested algorithms, well-understood
- Simple: One-size-fits-all approach that works for most workloads
Design Philosophy: Uses static heuristics based on decades of OS design experience. Architecture supports future AI/ML integration while maintaining deterministic core.
Components
1. Resource Manager (`kernel/resource/resource_manager.asm`)
Core resource management functionality:
- CPU Management: Time slice calculation, CPU quotas
- Memory Management: Allocation checks, usage tracking
- I/O Management: Bandwidth limits, I/O quotas
- File Descriptor Management: FD limits, allocation tracking
- Priority Management: Process priorities, nice values
- System Load: Load calculation and monitoring
2. Resource Policies (`kernel/resource/resource_policy.c`)
Heuristic-based policies for resource allocation:
- CPU Scheduling Policies: Multi-factor time slice calculation
- Memory Allocation Policies: Load-based throttling, priority-based limits
- I/O Bandwidth Policies: Priority and load-based bandwidth limits
- File Descriptor Policies: Dynamic FD limits based on system state
- Priority Adjustment: Adaptive priority based on process behavior
- Resource Reclamation: Policies for reclaiming resources from low-priority processes
Resource Types
CPU Resources
- Time Slices: Calculated based on priority, nice value, system load
- CPU Quotas: Percentage of CPU time available to process
- Priority: 0-99 (higher = more CPU time)
- Nice Value: -20 to 19 (lower = higher priority)
Memory Resources
- Memory Limits: Per-process memory quotas
- Allocation Checks: Heuristic-based allocation approval
- Load-Based Throttling: Restrictive allocation during high load
- Priority-Based Limits: Higher priority = larger allocations allowed
I/O Resources
- Bandwidth Limits: Per-process I/O bandwidth quotas
- Load-Based Throttling: Reduce I/O during high system load
- Priority-Based Limits: Higher priority = higher bandwidth
File Descriptors
- FD Limits: Per-process file descriptor limits
- Dynamic Limits: Adjust based on priority and system load
- Allocation Tracking: Monitor FD usage per process
Heuristic Algorithms
CPU Time Slice Calculation
base_timeslice = 10ms
timeslice = base_timeslice + (priority * 100μs) + ((20 - nice) * 50μs)
if (system_load > 50%) {
timeslice -= (timeslice * system_load) / 1000
}
if (cpu_time_used < 1s) {
timeslice *= 1.2 // Prevent starvation
}
clamp(timeslice, 1ms, 100ms)
Memory Allocation Policy
- Hard Limit Check: Deny if would exceed process limit
- System Load Throttling:
- Load > 90%: Only allow if using < 25% of quota
- Load > 80%: Only allow if using < 50% of quota
- Large Allocation Check: Require higher priority for > 10% of quota
- Fragmentation Check: Restrict large allocations during high load
System Load Calculation
load = (memory_usage * 50 / total_memory) +
(active_processes * 30 / max_processes) +
(cpu_usage * 20 / 100)
clamp(load, 0, 100)
API Reference
Initialization
int resource_manager_init(void);
CPU Management
uint32_t resource_get_cpu_quota(uint32_t process_id);
uint64_t resource_calculate_timeslice(uint32_t process_id);
Memory Management
int resource_check_memory_allocation(uint32_t process_id, uint64_t size);
void resource_update_memory(uint32_t process_id, int64_t delta);
File Descriptors
int resource_check_fd_allocation(uint32_t process_id);
int32_t resource_allocate_fd(uint32_t process_id);
Priority Management
void resource_set_priority(uint32_t process_id, uint32_t priority);
void resource_set_nice(uint32_t process_id, int32_t nice_value);
Statistics
void resource_get_stats(resource_stats_t* stats);
uint32_t resource_calculate_system_load(void);
Resource Policies API
CPU Scheduling
uint64_t resource_policy_calculate_timeslice(
uint32_t priority,
int32_t nice_value,
uint64_t cpu_time_used,
uint32_t system_load
);
Memory Allocation
int resource_policy_check_memory_allocation(
uint64_t requested_size,
uint64_t memory_used,
uint64_t memory_max,
uint32_t system_load,
uint32_t priority
);
I/O Bandwidth
uint64_t resource_policy_calculate_io_limit(
uint32_t priority,
uint32_t system_load,
uint64_t base_bandwidth
);
File Descriptors
uint32_t resource_policy_calculate_fd_limit(
uint32_t priority,
uint32_t system_load
);
Access Methods
The Resource Manager is kernel-level and accessed via:
- Direct Kernel Calls: Kernel code calls functions directly
- System Calls: User space processes access via syscalls (INT 0x80)
- No IPC Required: Kernel-level infrastructure doesn't need IPC
System Call Numbers
| Syscall Number | Function | Description |
|---|---|---|
| 20 | SYS_GET_CPU_QUOTA |
Get CPU quota for process |
| 21 | SYS_CALCULATE_TIMESLICE |
Calculate time slice |
| 22 | SYS_CHECK_MEMORY |
Check memory allocation |
| 23 | SYS_UPDATE_MEMORY |
Update memory usage |
| 24 | SYS_CHECK_IO_BANDWIDTH |
Check I/O bandwidth |
| 25 | SYS_CHECK_FD |
Check FD allocation |
| 26 | SYS_ALLOCATE_FD |
Allocate file descriptor |
| 27 | SYS_SET_PRIORITY |
Set process priority |
| 28 | SYS_SET_NICE |
Set nice value |
| 29 | SYS_GET_SYSTEM_LOAD |
Get system load |
| 30 | SYS_GET_RESOURCE_STATS |
Get resource statistics |
Integration
The Resource Manager integrates with:
- Scheduler: Provides time slice calculations
- Memory Manager: Provides allocation checks
- I/O Subsystem: Provides bandwidth limits
- File System: Provides FD limits
- Process Manager: Provides priority management
- System Call Interface: Exposes functions to user space
File Structure
kernel/resource/
├── resource_manager.asm # Core ASM implementation
├── resource_manager.h # C API header
├── resource_policy.c # Heuristic policies
├── resource_policy.h # Policy API header
├── Makefile # Build system
└── README.md # Documentation
kernel/syscalls/
└── resource_syscalls.asm # System call wrappers
Why Heuristics?
- Performance: Lightweight, fast execution in kernel critical path
- Predictability: Deterministic behavior, easy to debug
- Reliability: Battle-tested algorithms, well-understood
- Simplicity: One-size-fits-all approach that works for most workloads
- Domain Knowledge: Codified experience from decades of OS design
Future: AI/ML Integration
While the current implementation uses static heuristics, the architecture is designed to allow future integration of neural networks and AI for adaptive resource management. The policy layer can be extended to support ML-based decisions while maintaining the deterministic core.
Status
Core Resource Manager
✅ Complete
✅ Complete
Resource Policies
✅ Complete
✅ Complete
Heuristic Algorithms
✅ Complete
✅ Complete
API
✅ Complete
✅ Complete
Total: ~1,000 lines of production code