mirror of
https://github.com/allaunthefox/Research-Stack.git
synced 2026-07-31 03:05:21 +00:00
960 lines
44 KiB
Python
960 lines
44 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Architect Node Topology Driver Based on Accurate Container Map
|
|
with Functional Collapse Paradigm Cognitive Load Metrics
|
|
|
|
This driver uses the 100% accurate container map to maximize the architect node's
|
|
topology utilization by leveraging every bit of its available resources.
|
|
|
|
Functional Collapse Paradigm:
|
|
- Cognitive Load is the informational cost of lawful assemblage between current state and optimal state
|
|
- Metrics are type-instances of bind(A, B, Metric) where Metric is typically KL-Divergence
|
|
- Criticality Threshold (τ_c) aligned with Abelian Sandpile threshold
|
|
- N-Local Topology Scaling with path-dependence
|
|
|
|
Based on actual container mapping data:
|
|
- 6 physical cores, 12 logical cores
|
|
- 30.40 GB RAM
|
|
- 213 processes
|
|
- 359 network edges
|
|
- 32 file systems
|
|
- 3376 memory regions
|
|
- 359 open sockets
|
|
"""
|
|
|
|
import sys
|
|
import json
|
|
import time
|
|
import threading
|
|
import multiprocessing
|
|
import psutil
|
|
import math
|
|
import numpy as np
|
|
import signal
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
from dataclasses import dataclass, field
|
|
from typing import List, Dict, Optional, Any, Callable, Tuple
|
|
from collections import defaultdict, deque
|
|
from enum import Enum
|
|
|
|
class WorkloadType(Enum):
|
|
"""Types of workloads for topology scheduling"""
|
|
COMPUTE_INTENSIVE = "compute_intensive"
|
|
MEMORY_INTENSIVE = "memory_intensive"
|
|
IO_INTENSIVE = "io_intensive"
|
|
NETWORK_INTENSIVE = "network_intensive"
|
|
MIXED = "mixed"
|
|
|
|
class OperationalThreshold(Enum):
|
|
"""Operational thresholds for cognitive load"""
|
|
RELATIONAL = "relational" # < 0.25: Low friction; full 5D torus expansion
|
|
SEMANTIC = "semantic" # < 0.50: Standard operating range; active S3C compression
|
|
TOPOLOGICAL = "topological" # < 0.75: High stress; gear teeth modulations active
|
|
CRITICAL = "critical" # ≥ 0.75: Criticality reached (τ_c). Sandbox collapse initiated
|
|
|
|
@dataclass
|
|
class CognitiveLoadMetrics:
|
|
"""Functional Collapse Paradigm cognitive load metrics"""
|
|
intrinsic_load: float # L_I: bind(p(b|x), uniform, KL)
|
|
effort_load: float # L_E: bind(P_w_prior(x), P_optimal(x), KL)
|
|
total_system_load: float # L_total: bind(load_vector, target_vector, weighted_L2)
|
|
efficiency_factor: float # η: bind(intrinsic, total, ratio_metric)
|
|
distribution_precision: float # P_w: bind(ensemble, mixture, simplex_metric)
|
|
operational_threshold: OperationalThreshold
|
|
criticality_reached: bool
|
|
|
|
@dataclass
|
|
class Workload:
|
|
"""Workload to be scheduled"""
|
|
workload_id: str
|
|
workload_type: WorkloadType
|
|
cpu_required: float # 0-1 (percentage of total CPU)
|
|
memory_required: float # 0-1 (percentage of total RAM)
|
|
storage_required: float # 0-1 (percentage of total storage)
|
|
bandwidth_required: float # 0-1 (percentage of total bandwidth)
|
|
priority: int # 1-10
|
|
duration: float # seconds
|
|
executable: Callable
|
|
status: str = "pending"
|
|
assigned_core: Optional[int] = None
|
|
start_time: Optional[float] = None
|
|
end_time: Optional[float] = None
|
|
|
|
@dataclass
|
|
class AccurateTopologyResource:
|
|
"""Topology resource state based on accurate container map"""
|
|
total_cores: int
|
|
total_ram_gb: float
|
|
total_storage_gb: float
|
|
total_bandwidth_mbps: float
|
|
|
|
available_cores: int
|
|
available_ram_gb: float
|
|
available_storage_gb: float
|
|
available_bandwidth_mbps: float
|
|
|
|
core_utilization: List[float] # Per-core utilization
|
|
memory_utilization: float
|
|
storage_utilization: float
|
|
bandwidth_utilization: float
|
|
|
|
# Container map data
|
|
total_processes: int
|
|
total_network_edges: int
|
|
total_file_systems: int
|
|
total_memory_regions: int
|
|
total_open_sockets: int
|
|
total_devices: int
|
|
total_kernel_parameters: int
|
|
|
|
# Functional Collapse Paradigm state
|
|
cognitive_load_metrics: Optional[CognitiveLoadMetrics] = None
|
|
history_trace: List[Dict[str, float]] = field(default_factory=list)
|
|
avalanche_count: int = 0
|
|
gear_pitch: float = 1.0 # Gear teeth modulation factor
|
|
|
|
def bind_kl_divergence(p: np.ndarray, q: np.ndarray) -> float:
|
|
"""
|
|
Bind operator using KL-divergence as the metric.
|
|
|
|
D_KL(P || Q) = sum(P(i) * log(P(i) / Q(i)))
|
|
|
|
This measures the informational cost of lawful assemblage between
|
|
the current state (P) and the optimal state (Q).
|
|
"""
|
|
# Ensure distributions sum to 1
|
|
p = p / np.sum(p)
|
|
q = q / np.sum(q)
|
|
|
|
# Add small epsilon to avoid log(0)
|
|
epsilon = 1e-10
|
|
p = p + epsilon
|
|
q = q + epsilon
|
|
|
|
# Calculate KL-divergence
|
|
kl_div = np.sum(p * np.log(p / q))
|
|
|
|
return float(kl_div)
|
|
|
|
def bind_weighted_l2(vector_a: np.ndarray, vector_b: np.ndarray, weights: Optional[np.ndarray] = None) -> float:
|
|
"""
|
|
Bind operator using weighted L2 distance.
|
|
|
|
||A - B||_w = sqrt(sum(w_i * (A_i - B_i)^2))
|
|
"""
|
|
if weights is None:
|
|
weights = np.ones_like(vector_a)
|
|
|
|
diff = vector_a - vector_b
|
|
weighted_diff = weights * diff
|
|
l2_distance = np.sqrt(np.sum(weighted_diff ** 2))
|
|
|
|
return float(l2_distance)
|
|
|
|
def bind_ratio_metric(a: float, b: float) -> float:
|
|
"""
|
|
Bind operator using ratio metric.
|
|
|
|
ratio = a / b (with epsilon to avoid division by zero)
|
|
"""
|
|
epsilon = 1e-10
|
|
return a / (b + epsilon)
|
|
|
|
def bind_simplex_metric(ensemble: np.ndarray, mixture: np.ndarray) -> float:
|
|
"""
|
|
Bind operator using simplex metric.
|
|
|
|
Measures the "sharpness" of the current agent consensus.
|
|
Uses entropy-based distance on the probability simplex.
|
|
"""
|
|
# Ensure distributions are on simplex
|
|
ensemble = ensemble / np.sum(ensemble)
|
|
mixture = mixture / np.sum(mixture)
|
|
|
|
# Calculate entropy difference
|
|
epsilon = 1e-10
|
|
ensemble_entropy = -np.sum(ensemble * np.log(ensemble + epsilon))
|
|
mixture_entropy = -np.sum(mixture * np.log(mixture + epsilon))
|
|
|
|
# Simplex distance as entropy difference
|
|
simplex_distance = abs(ensemble_entropy - mixture_entropy)
|
|
|
|
return float(simplex_distance)
|
|
|
|
def calculate_cognitive_load_metrics(
|
|
current_state: np.ndarray,
|
|
optimal_state: np.ndarray,
|
|
prior_distribution: np.ndarray,
|
|
optimal_distribution: np.ndarray,
|
|
load_vector: np.ndarray,
|
|
target_vector: np.ndarray,
|
|
ensemble: np.ndarray,
|
|
mixture: np.ndarray,
|
|
history_trace: List[Dict[str, float]]
|
|
) -> CognitiveLoadMetrics:
|
|
"""
|
|
Calculate Functional Collapse Paradigm cognitive load metrics.
|
|
|
|
All metrics are type-instances of bind(A, B, Metric).
|
|
"""
|
|
# Intrinsic Load (L_I): bind(p(b|x), uniform, KL)
|
|
# Measures how far the current bitseed distribution is from high-entropy uniform state
|
|
uniform_distribution = np.ones_like(current_state) / len(current_state)
|
|
intrinsic_load = bind_kl_divergence(current_state, uniform_distribution)
|
|
|
|
# Effort Load (L_E): bind(P_w_prior(x), P_optimal(x), KL)
|
|
# Measures delta between swarm's current prediction and bit-accurate optimal predictor
|
|
effort_load = bind_kl_divergence(prior_distribution, optimal_distribution)
|
|
|
|
# Total System Load (L_total): bind(load_vector, target_vector, weighted_L2)
|
|
# Global pressure on the manifold substrate
|
|
# Use history-dependent weights for N-Local Topology Scaling
|
|
if history_trace:
|
|
# Calculate weights based on history (recent avalanches increase weight)
|
|
recent_avalanches = sum(1 for h in history_trace[-10:] if h.get('avalanche', False))
|
|
weights = np.ones_like(load_vector) * (1.0 + 0.1 * recent_avalanches)
|
|
else:
|
|
weights = None
|
|
total_system_load = bind_weighted_l2(load_vector, target_vector, weights)
|
|
|
|
# Efficiency Factor (η): bind(intrinsic, total, ratio_metric)
|
|
# Ratio of effective informatic work to total energy dissipated
|
|
efficiency_factor = bind_ratio_metric(intrinsic_load, total_system_load + epsilon)
|
|
|
|
# Distribution Precision (P_w): bind(ensemble, mixture, simplex_metric)
|
|
# Measures the "sharpness" of the current agent consensus
|
|
distribution_precision = bind_simplex_metric(ensemble, mixture)
|
|
|
|
# Determine operational threshold
|
|
operational_threshold = determine_operational_threshold(total_system_load)
|
|
|
|
# Check criticality (τ_c)
|
|
criticality_reached = total_system_load >= 0.75
|
|
|
|
return CognitiveLoadMetrics(
|
|
intrinsic_load=intrinsic_load,
|
|
effort_load=effort_load,
|
|
total_system_load=total_system_load,
|
|
efficiency_factor=efficiency_factor,
|
|
distribution_precision=distribution_precision,
|
|
operational_threshold=operational_threshold,
|
|
criticality_reached=criticality_reached
|
|
)
|
|
|
|
def determine_operational_threshold(load: float) -> OperationalThreshold:
|
|
"""Determine operational threshold based on load value."""
|
|
if load < 0.25:
|
|
return OperationalThreshold.RELATIONAL
|
|
elif load < 0.50:
|
|
return OperationalThreshold.SEMANTIC
|
|
elif load < 0.75:
|
|
return OperationalThreshold.TOPOLOGICAL
|
|
else:
|
|
return OperationalThreshold.CRITICAL
|
|
|
|
def abelian_sandpile_collapse(load: float, gear_pitch: float) -> Tuple[bool, float]:
|
|
"""
|
|
Abelian Sandpile collapse logic.
|
|
|
|
When Load ≥ τ_c (0.75), trigger a "Topological Collapse" to prevent
|
|
irreversible informatic damage. This modulates the gear pitch to
|
|
reduce cognitive load.
|
|
|
|
Returns:
|
|
(collapse_triggered, new_gear_pitch)
|
|
"""
|
|
criticality_threshold = 0.75
|
|
|
|
if load >= criticality_threshold:
|
|
# Avalanche triggered - collapse to reduce load
|
|
# Modulate gear pitch to reduce cognitive load
|
|
new_gear_pitch = gear_pitch * 0.8 # Reduce gear pitch by 20%
|
|
return True, new_gear_pitch
|
|
else:
|
|
# No collapse needed
|
|
# Gradually restore gear pitch toward 1.0
|
|
new_gear_pitch = min(1.0, gear_pitch + 0.01)
|
|
return False, new_gear_pitch
|
|
|
|
epsilon = 1e-10
|
|
|
|
class ArchitectTopologyDriverAccurate:
|
|
"""
|
|
Driver to maximize architect node topology utilization based on accurate container map.
|
|
|
|
Uses actual container mapping data to make intelligent scheduling decisions.
|
|
"""
|
|
|
|
def __init__(self, container_map_path: Optional[str] = None):
|
|
# Load container map
|
|
self.container_map = self._load_container_map(container_map_path)
|
|
|
|
# Extract actual specs from container map
|
|
self.node_specs = {
|
|
"cores": self.container_map["cpu_info"]["physical_cores"],
|
|
"logical_cores": self.container_map["cpu_info"]["logical_cores"],
|
|
"ram_gb": self.container_map["memory_info"]["virtual_memory"]["total"] / (1024**3),
|
|
"storage_gb": 500, # From resource map
|
|
"bandwidth_mbps": 500 # From resource map
|
|
}
|
|
|
|
# Topology resource state
|
|
self.topology_resource = AccurateTopologyResource(
|
|
total_cores=self.node_specs["cores"],
|
|
total_ram_gb=self.node_specs["ram_gb"],
|
|
total_storage_gb=self.node_specs["storage_gb"],
|
|
total_bandwidth_mbps=self.node_specs["bandwidth_mbps"],
|
|
available_cores=self.node_specs["cores"],
|
|
available_ram_gb=self.node_specs["ram_gb"],
|
|
available_storage_gb=self.node_specs["storage_gb"],
|
|
available_bandwidth_mbps=self.node_specs["bandwidth_mbps"],
|
|
core_utilization=[0.0] * self.node_specs["logical_cores"],
|
|
memory_utilization=0.0,
|
|
storage_utilization=0.0,
|
|
bandwidth_utilization=0.0,
|
|
total_processes=self.container_map.get("processes", []),
|
|
total_network_edges=len(self.container_map.get("network_edges", [])),
|
|
total_file_systems=len(self.container_map.get("file_systems", [])),
|
|
total_memory_regions=len(self.container_map.get("memory_regions", [])),
|
|
total_open_sockets=len(self.container_map.get("open_sockets", [])),
|
|
total_devices=len(self.container_map.get("devices", [])),
|
|
total_kernel_parameters=len(self.container_map.get("kernel_parameters", {}))
|
|
)
|
|
|
|
# Workload queues
|
|
self.workload_queue: deque[Workload] = deque()
|
|
self.active_workloads: Dict[str, Workload] = {}
|
|
self.completed_workloads: List[Workload] = []
|
|
|
|
# Scheduling state
|
|
self.scheduling_strategy = "topology_aware_accurate_functional_collapse"
|
|
self.max_concurrent_workloads = self.node_specs["logical_cores"] * 2 # 2x oversubscription
|
|
|
|
# Background processing
|
|
self._running = False
|
|
self._lock = threading.Lock()
|
|
self._scheduler_thread: Optional[threading.Thread] = None
|
|
self._monitor_thread: Optional[threading.Thread] = None
|
|
self._scheduling_interval = 0.1 # 100ms
|
|
self._monitoring_interval = 1.0 # 1s
|
|
|
|
# Performance metrics
|
|
self.metrics: Dict[str, List[float]] = defaultdict(list)
|
|
self.total_utilization_score = 0.0
|
|
|
|
# Functional Collapse Paradigm state
|
|
self.cognitive_load_history: List[Dict[str, float]] = []
|
|
self.avalanche_count = 0
|
|
self.gear_pitch = 1.0
|
|
|
|
print(f"[ArchitectTopologyDriverAccurate] Initialized based on accurate container map")
|
|
print(f" Cores: {self.node_specs['cores']} physical, {self.node_specs['logical_cores']} logical")
|
|
print(f" RAM: {self.node_specs['ram_gb']:.2f}GB")
|
|
print(f" Storage: {self.node_specs['storage_gb']}GB")
|
|
print(f" Bandwidth: {self.node_specs['bandwidth_mbps']}Mbps")
|
|
print(f" Container Map Processes: {len(self.topology_resource.total_processes)}")
|
|
print(f" Container Map Network Edges: {self.topology_resource.total_network_edges}")
|
|
print(f" Container Map File Systems: {self.topology_resource.total_file_systems}")
|
|
print(f" Container Map Memory Regions: {self.topology_resource.total_memory_regions}")
|
|
print(f" Scheduling Strategy: {self.scheduling_strategy}")
|
|
print(f" Functional Collapse Paradigm: Enabled")
|
|
|
|
def _load_container_map(self, path: Optional[str]) -> Dict[str, Any]:
|
|
"""Load container map from file."""
|
|
if path is None:
|
|
# Find most recent container map
|
|
map_dir = Path("shared-data/data/swarm_responses")
|
|
maps = sorted(map_dir.glob("architect_container_map_remote_*.json"))
|
|
if maps:
|
|
path = str(maps[-1])
|
|
else:
|
|
raise FileNotFoundError("No container map found")
|
|
|
|
print(f"[ArchitectTopologyDriverAccurate] Loading container map from {path}")
|
|
|
|
with open(path, 'r') as f:
|
|
return json.load(f)
|
|
|
|
def update_topology_state(self):
|
|
"""Update topology resource state from actual system metrics and calculate cognitive load."""
|
|
try:
|
|
# Get actual CPU utilization
|
|
cpu_percent = psutil.cpu_percent(interval=0.1, percpu=True)
|
|
self.topology_resource.core_utilization = [c / 100.0 for c in cpu_percent]
|
|
|
|
# Get actual memory utilization
|
|
memory = psutil.virtual_memory()
|
|
self.topology_resource.memory_utilization = memory.percent / 100.0
|
|
self.topology_resource.available_ram_gb = memory.available / (1024**3)
|
|
|
|
# Get actual disk utilization
|
|
disk = psutil.disk_usage('/')
|
|
self.topology_resource.storage_utilization = disk.percent / 100.0
|
|
self.topology_resource.available_storage_gb = disk.free / (1024**3)
|
|
|
|
# Calculate available cores (cores with < 80% utilization)
|
|
available_cores = sum(1 for util in self.topology_resource.core_utilization if util < 0.8)
|
|
self.topology_resource.available_cores = max(0, available_cores)
|
|
|
|
# Calculate total utilization score
|
|
core_avg = sum(self.topology_resource.core_utilization) / len(self.topology_resource.core_utilization)
|
|
self.total_utilization_score = (
|
|
core_avg * 0.4 +
|
|
self.topology_resource.memory_utilization * 0.3 +
|
|
self.topology_resource.storage_utilization * 0.2 +
|
|
self.topology_resource.bandwidth_utilization * 0.1
|
|
)
|
|
|
|
# Calculate Functional Collapse Paradigm cognitive load metrics
|
|
self._calculate_cognitive_load()
|
|
|
|
except Exception as e:
|
|
print(f"[ArchitectTopologyDriverAccurate] Error updating topology state: {e}")
|
|
|
|
def _calculate_cognitive_load(self):
|
|
"""Calculate cognitive load metrics using Refined Functional Collapse Paradigm."""
|
|
try:
|
|
# Create state vectors from current topology state
|
|
current_state = np.array(self.topology_resource.core_utilization)
|
|
optimal_state = np.ones_like(current_state) * 0.5 # Target 50% utilization per core
|
|
|
|
prior_distribution = np.array([self.topology_resource.memory_utilization,
|
|
self.topology_resource.storage_utilization,
|
|
self.topology_resource.bandwidth_utilization])
|
|
optimal_distribution = np.array([0.5, 0.5, 0.5]) # Target 50% utilization
|
|
|
|
load_vector = np.array([self.total_utilization_score,
|
|
len(self.active_workloads) / self.max_concurrent_workloads,
|
|
self.topology_resource.memory_utilization])
|
|
target_vector = np.array([0.5, 0.5, 0.5]) # Target load vector
|
|
|
|
# Ensemble distribution (workload types distribution)
|
|
workload_types = [w.workload_type.value for w in self.active_workloads.values()]
|
|
if workload_types:
|
|
type_counts = defaultdict(int)
|
|
for wt in workload_types:
|
|
type_counts[wt] += 1
|
|
ensemble = np.array([type_counts.get(wt.value, 0) for wt in WorkloadType])
|
|
ensemble = ensemble / np.sum(ensemble) if np.sum(ensemble) > 0 else np.ones(len(WorkloadType)) / len(WorkloadType)
|
|
else:
|
|
ensemble = np.ones(len(WorkloadType)) / len(WorkloadType)
|
|
|
|
# Mixture distribution (target uniform distribution)
|
|
mixture = np.ones(len(WorkloadType)) / len(WorkloadType)
|
|
|
|
# Refined Intrinsic Load: use capacity-matched baseline instead of uniform
|
|
# Calculate baseline distribution based on current structural constraints
|
|
baseline_distribution = self._calculate_capacity_matched_baseline(current_state)
|
|
|
|
# Override the intrinsic load calculation to use capacity-matched baseline
|
|
intrinsic_load = bind_kl_divergence(current_state, baseline_distribution)
|
|
|
|
# Calculate remaining metrics
|
|
effort_load = bind_kl_divergence(prior_distribution, optimal_distribution)
|
|
|
|
# Calculate history-dependent weights for N-Local Topology Scaling
|
|
if self.cognitive_load_history:
|
|
recent_avalanches = sum(1 for h in self.cognitive_load_history[-10:] if h.get('avalanche', False))
|
|
weights = np.ones_like(load_vector) * (1.0 + 0.1 * recent_avalanches)
|
|
else:
|
|
weights = None
|
|
total_system_load = bind_weighted_l2(load_vector, target_vector, weights)
|
|
|
|
efficiency_factor = bind_ratio_metric(intrinsic_load, total_system_load + epsilon)
|
|
distribution_precision = bind_simplex_metric(ensemble, mixture)
|
|
|
|
# Determine operational threshold
|
|
operational_threshold = determine_operational_threshold(total_system_load)
|
|
|
|
# Check criticality (τ_c)
|
|
criticality_reached = total_system_load >= 0.75
|
|
|
|
self.topology_resource.cognitive_load_metrics = CognitiveLoadMetrics(
|
|
intrinsic_load=intrinsic_load,
|
|
effort_load=effort_load,
|
|
total_system_load=total_system_load,
|
|
efficiency_factor=efficiency_factor,
|
|
distribution_precision=distribution_precision,
|
|
operational_threshold=operational_threshold,
|
|
criticality_reached=criticality_reached
|
|
)
|
|
|
|
# Check for criticality and trigger Abelian Sandpile collapse if needed
|
|
collapse_triggered, new_gear_pitch = abelian_sandpile_collapse(
|
|
load=self.topology_resource.cognitive_load_metrics.total_system_load,
|
|
gear_pitch=self.gear_pitch
|
|
)
|
|
|
|
if collapse_triggered:
|
|
self.avalanche_count += 1
|
|
print(f"[ArchitectTopologyDriverAccurate] ⚠️ AVALANCHE TRIGGERED - Topological Collapse initiated")
|
|
print(f" Total System Load: {self.topology_resource.cognitive_load_metrics.total_system_load:.3f} ≥ τ_c (0.75)")
|
|
print(f" Avalanche Count: {self.avalanche_count}")
|
|
print(f" Gear Pitch: {self.gear_pitch:.3f} → {new_gear_pitch:.3f}")
|
|
|
|
self.gear_pitch = new_gear_pitch
|
|
self.topology_resource.gear_pitch = new_gear_pitch
|
|
|
|
# Record history trace for N-Local Topology Scaling
|
|
history_entry = {
|
|
"timestamp": time.time(),
|
|
"total_system_load": self.topology_resource.cognitive_load_metrics.total_system_load,
|
|
"intrinsic_load": self.topology_resource.cognitive_load_metrics.intrinsic_load,
|
|
"effort_load": self.topology_resource.cognitive_load_metrics.effort_load,
|
|
"efficiency_factor": self.topology_resource.cognitive_load_metrics.efficiency_factor,
|
|
"distribution_precision": self.topology_resource.cognitive_load_metrics.distribution_precision,
|
|
"operational_threshold": self.topology_resource.cognitive_load_metrics.operational_threshold.value,
|
|
"avalanche": collapse_triggered,
|
|
"gear_pitch": self.gear_pitch
|
|
}
|
|
|
|
self.cognitive_load_history.append(history_entry)
|
|
|
|
# Keep history limited to last 100 entries
|
|
if len(self.cognitive_load_history) > 100:
|
|
self.cognitive_load_history = self.cognitive_load_history[-100:]
|
|
|
|
except Exception as e:
|
|
print(f"[ArchitectTopologyDriverAccurate] Error calculating cognitive load: {e}")
|
|
|
|
def _calculate_capacity_matched_baseline(self, current_state: np.ndarray) -> np.ndarray:
|
|
"""
|
|
Calculate capacity-matched baseline distribution for refined intrinsic load.
|
|
|
|
Instead of using unconstrained uniformity, this calculates the maximum-entropy
|
|
distribution under current structural and historical constraints.
|
|
|
|
This means:
|
|
- Unnecessary over-constraint increases load
|
|
- Lawful structure does NOT automatically count as friction
|
|
- Intrinsic load becomes "excess rigidity relative to what this system can stably sustain"
|
|
"""
|
|
# Calculate baseline as maximum-entropy distribution under constraints
|
|
# Constraints: current gear pitch, recent avalanche history, available cores
|
|
|
|
# Base uniform distribution
|
|
baseline = np.ones_like(current_state) / len(current_state)
|
|
|
|
# Apply structural constraints based on gear pitch
|
|
# Higher gear pitch = more constrained baseline (lower capacity)
|
|
constrained_baseline = baseline * self.gear_pitch
|
|
|
|
# Normalize to maintain probability distribution
|
|
constrained_baseline = constrained_baseline / np.sum(constrained_baseline)
|
|
|
|
# Apply historical constraints - recent avalanches tighten baseline
|
|
if self.cognitive_load_history:
|
|
recent_avalanches = sum(1 for h in self.cognitive_load_history[-10:] if h.get('avalanche', False))
|
|
# More avalanches = tighter baseline (system becomes more conservative)
|
|
history_factor = 1.0 / (1.0 + 0.1 * recent_avalanches)
|
|
constrained_baseline = constrained_baseline * history_factor
|
|
constrained_baseline = constrained_baseline / np.sum(constrained_baseline)
|
|
|
|
return constrained_baseline
|
|
|
|
def schedule_workload(self, workload: Workload) -> bool:
|
|
"""Schedule a workload using topology-aware scheduling with cognitive load consideration."""
|
|
with self._lock:
|
|
# Check if resources are available
|
|
if self.topology_resource.available_cores < 1:
|
|
return False
|
|
|
|
if self.topology_resource.available_ram_gb < workload.memory_required * self.node_specs["ram_gb"]:
|
|
return False
|
|
|
|
if self.topology_resource.available_storage_gb < workload.storage_required * self.node_specs["storage_gb"]:
|
|
return False
|
|
|
|
# Check cognitive load - if critical, reject new workloads
|
|
if (self.topology_resource.cognitive_load_metrics and
|
|
self.topology_resource.cognitive_load_metrics.criticality_reached):
|
|
print(f"[ArchitectTopologyDriverAccurate] ⚠️ Rejecting {workload.workload_id} - Critical load reached")
|
|
return False
|
|
|
|
# Adjust resource requirements based on gear pitch (N-Local Topology Scaling)
|
|
adjusted_cpu_required = workload.cpu_required * self.gear_pitch
|
|
adjusted_memory_required = workload.memory_required * self.gear_pitch
|
|
|
|
# Find best core for this workload based on container map data
|
|
best_core = self._find_best_core_accurate(workload)
|
|
|
|
if best_core is None:
|
|
return False
|
|
|
|
# Assign workload
|
|
workload.assigned_core = best_core
|
|
workload.status = "running"
|
|
workload.start_time = time.time()
|
|
|
|
# Update resource availability
|
|
self.topology_resource.available_cores -= 1
|
|
self.topology_resource.available_ram_gb -= adjusted_memory_required * self.node_specs["ram_gb"]
|
|
self.topology_resource.available_storage_gb -= workload.storage_required * self.node_specs["storage_gb"]
|
|
|
|
# Add to active workloads
|
|
self.active_workloads[workload.workload_id] = workload
|
|
|
|
print(f"[ArchitectTopologyDriverAccurate] Scheduled {workload.workload_id} on core {best_core}")
|
|
print(f" Type: {workload.workload_type.value}")
|
|
print(f" CPU: {adjusted_cpu_required * 100:.1f}% (adjusted by gear pitch {self.gear_pitch:.3f})")
|
|
print(f" RAM: {adjusted_memory_required * 100:.1f}%")
|
|
|
|
return True
|
|
|
|
def _find_best_core_accurate(self, workload: Workload) -> Optional[int]:
|
|
"""Find best core for workload based on accurate container map data."""
|
|
# Find core with lowest utilization
|
|
core_utilizations = self.topology_resource.core_utilization
|
|
best_core = None
|
|
best_utilization = 1.0
|
|
|
|
for i, util in enumerate(core_utilizations):
|
|
if util < best_utilization:
|
|
best_utilization = util
|
|
best_core = i
|
|
|
|
# Check if best core is available (< 80% utilization)
|
|
if best_core is not None and best_utilization < 0.8:
|
|
return best_core
|
|
|
|
return None
|
|
|
|
def submit_workload(self, workload: Workload) -> bool:
|
|
"""Submit a workload for scheduling."""
|
|
with self._lock:
|
|
self.workload_queue.append(workload)
|
|
print(f"[ArchitectTopologyDriverAccurate] Submitted workload {workload.workload_id}")
|
|
return True
|
|
|
|
def _scheduler_loop(self):
|
|
"""Background scheduler loop."""
|
|
while self._running:
|
|
try:
|
|
with self._lock:
|
|
# Update topology state
|
|
self.update_topology_state()
|
|
|
|
# Schedule pending workloads
|
|
while (len(self.workload_queue) > 0 and
|
|
len(self.active_workloads) < self.max_concurrent_workloads):
|
|
workload = self.workload_queue.popleft()
|
|
if not self.schedule_workload(workload):
|
|
# Can't schedule now, put back in queue
|
|
self.workload_queue.appendleft(workload)
|
|
break
|
|
|
|
# Check for completed workloads
|
|
current_time = time.time()
|
|
completed = []
|
|
for workload_id, workload in self.active_workloads.items():
|
|
if workload.end_time and current_time >= workload.end_time:
|
|
completed.append(workload_id)
|
|
|
|
for workload_id in completed:
|
|
self._complete_workload(workload_id)
|
|
|
|
time.sleep(self._scheduling_interval)
|
|
|
|
except Exception as e:
|
|
print(f"[ArchitectTopologyDriverAccurate] Scheduler loop error: {e}")
|
|
time.sleep(1.0)
|
|
|
|
def _complete_workload(self, workload_id: str):
|
|
"""Complete a workload and free resources."""
|
|
with self._lock:
|
|
if workload_id not in self.active_workloads:
|
|
return
|
|
|
|
workload = self.active_workloads[workload_id]
|
|
workload.status = "completed"
|
|
workload.end_time = time.time()
|
|
|
|
# Free resources
|
|
self.topology_resource.available_cores += 1
|
|
self.topology_resource.available_ram_gb += workload.memory_required * self.node_specs["ram_gb"]
|
|
self.topology_resource.available_storage_gb += workload.storage_required * self.node_specs["storage_gb"]
|
|
|
|
# Move to completed
|
|
self.completed_workloads.append(workload)
|
|
del self.active_workloads[workload_id]
|
|
|
|
print(f"[ArchitectTopologyDriverAccurate] Completed {workload_id}")
|
|
|
|
def _monitor_loop(self):
|
|
"""Background monitoring loop with cognitive load tracking."""
|
|
while self._running:
|
|
try:
|
|
with self._lock:
|
|
# Record metrics
|
|
self.metrics['total_utilization'].append(self.total_utilization_score)
|
|
self.metrics['core_utilization_avg'].append(
|
|
sum(self.topology_resource.core_utilization) / len(self.topology_resource.core_utilization)
|
|
)
|
|
self.metrics['memory_utilization'].append(self.topology_resource.memory_utilization)
|
|
self.metrics['active_workloads'].append(len(self.active_workloads))
|
|
self.metrics['queued_workloads'].append(len(self.workload_queue))
|
|
|
|
# Record cognitive load metrics
|
|
if self.topology_resource.cognitive_load_metrics:
|
|
self.metrics['intrinsic_load'].append(self.topology_resource.cognitive_load_metrics.intrinsic_load)
|
|
self.metrics['effort_load'].append(self.topology_resource.cognitive_load_metrics.effort_load)
|
|
self.metrics['total_system_load'].append(self.topology_resource.cognitive_load_metrics.total_system_load)
|
|
self.metrics['efficiency_factor'].append(self.topology_resource.cognitive_load_metrics.efficiency_factor)
|
|
self.metrics['distribution_precision'].append(self.topology_resource.cognitive_load_metrics.distribution_precision)
|
|
self.metrics['gear_pitch'].append(self.gear_pitch)
|
|
self.metrics['avalanche_count'].append(self.avalanche_count)
|
|
|
|
time.sleep(self._monitoring_interval)
|
|
|
|
except Exception as e:
|
|
print(f"[ArchitectTopologyDriverAccurate] Monitor loop error: {e}")
|
|
time.sleep(1.0)
|
|
|
|
def start(self) -> bool:
|
|
"""Start the topology driver."""
|
|
try:
|
|
self._running = True
|
|
self._scheduler_thread = threading.Thread(target=self._scheduler_loop, daemon=True)
|
|
self._monitor_thread = threading.Thread(target=self._monitor_loop, daemon=True)
|
|
|
|
self._scheduler_thread.start()
|
|
self._monitor_thread.start()
|
|
|
|
print(f"[ArchitectTopologyDriverAccurate] Started topology driver")
|
|
print(f" Scheduling strategy: {self.scheduling_strategy}")
|
|
print(f" Max concurrent workloads: {self.max_concurrent_workloads}")
|
|
print(f" Based on accurate container map with {self.topology_resource.total_processes} processes")
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"[ArchitectTopologyDriverAccurate] Failed to start: {e}")
|
|
return False
|
|
|
|
def stop(self):
|
|
"""Stop the topology driver."""
|
|
self._running = False
|
|
if self._scheduler_thread:
|
|
self._scheduler_thread.join(timeout=5.0)
|
|
if self._monitor_thread:
|
|
self._monitor_thread.join(timeout=5.0)
|
|
print("[ArchitectTopologyDriverAccurate] Stopped")
|
|
|
|
def get_status(self) -> Dict[str, Any]:
|
|
"""Get current driver status including cognitive load metrics."""
|
|
with self._lock:
|
|
status = {
|
|
"running": self._running,
|
|
"scheduling_strategy": self.scheduling_strategy,
|
|
"node_specs": self.node_specs,
|
|
"topology_resource": {
|
|
"total_cores": self.topology_resource.total_cores,
|
|
"available_cores": self.topology_resource.available_cores,
|
|
"total_ram_gb": self.topology_resource.total_ram_gb,
|
|
"available_ram_gb": self.topology_resource.available_ram_gb,
|
|
"total_storage_gb": self.topology_resource.total_storage_gb,
|
|
"available_storage_gb": self.topology_resource.available_storage_gb,
|
|
"core_utilization": self.topology_resource.core_utilization,
|
|
"memory_utilization": self.topology_resource.memory_utilization,
|
|
"storage_utilization": self.topology_resource.storage_utilization,
|
|
"total_utilization_score": self.total_utilization_score,
|
|
"container_map_stats": {
|
|
"total_processes": len(self.topology_resource.total_processes),
|
|
"total_network_edges": self.topology_resource.total_network_edges,
|
|
"total_file_systems": self.topology_resource.total_file_systems,
|
|
"total_memory_regions": self.topology_resource.total_memory_regions,
|
|
"total_open_sockets": self.topology_resource.total_open_sockets,
|
|
"total_devices": self.topology_resource.total_devices,
|
|
"total_kernel_parameters": self.topology_resource.total_kernel_parameters
|
|
}
|
|
},
|
|
"workloads": {
|
|
"active": len(self.active_workloads),
|
|
"queued": len(self.workload_queue),
|
|
"completed": len(self.completed_workloads)
|
|
},
|
|
"functional_collapse_paradigm": {
|
|
"cognitive_load_metrics": None,
|
|
"gear_pitch": self.gear_pitch,
|
|
"avalanche_count": self.avalanche_count,
|
|
"history_length": len(self.cognitive_load_history)
|
|
},
|
|
"metrics": {
|
|
key: {
|
|
"avg": sum(values) / len(values) if values else 0,
|
|
"max": max(values) if values else 0,
|
|
"min": min(values) if values else 0,
|
|
"count": len(values)
|
|
}
|
|
for key, values in self.metrics.items()
|
|
}
|
|
}
|
|
|
|
# Add cognitive load metrics if available
|
|
if self.topology_resource.cognitive_load_metrics:
|
|
status["functional_collapse_paradigm"]["cognitive_load_metrics"] = {
|
|
"intrinsic_load": self.topology_resource.cognitive_load_metrics.intrinsic_load,
|
|
"effort_load": self.topology_resource.cognitive_load_metrics.effort_load,
|
|
"total_system_load": self.topology_resource.cognitive_load_metrics.total_system_load,
|
|
"efficiency_factor": self.topology_resource.cognitive_load_metrics.efficiency_factor,
|
|
"distribution_precision": self.topology_resource.cognitive_load_metrics.distribution_precision,
|
|
"operational_threshold": self.topology_resource.cognitive_load_metrics.operational_threshold.value,
|
|
"criticality_reached": self.topology_resource.cognitive_load_metrics.criticality_reached
|
|
}
|
|
|
|
return status
|
|
|
|
def print_status(self):
|
|
"""Print current driver status including cognitive load metrics."""
|
|
status = self.get_status()
|
|
|
|
print("\n" + "=" * 70)
|
|
print("ARCHITECT TOPOLOGY DRIVER STATUS (FUNCTIONAL COLLAPSE PARADIGM)")
|
|
print("=" * 70)
|
|
|
|
print(f"\n📊 Node Specifications:")
|
|
print(f" Cores: {status['node_specs']['cores']} physical, {status['node_specs']['logical_cores']} logical")
|
|
print(f" RAM: {status['node_specs']['ram_gb']:.2f}GB")
|
|
print(f" Storage: {status['node_specs']['storage_gb']}GB")
|
|
print(f" Bandwidth: {status['node_specs']['bandwidth_mbps']}Mbps")
|
|
|
|
print(f"\n📊 Topology Utilization:")
|
|
print(f" Total Utilization Score: {status['topology_resource']['total_utilization_score']:.3f}")
|
|
print(f" Cores: {status['topology_resource']['available_cores']}/{status['topology_resource']['total_cores']} available")
|
|
print(f" RAM: {status['topology_resource']['available_ram_gb']:.1f}/{status['topology_resource']['total_ram_gb']:.1f}GB available")
|
|
print(f" Storage: {status['topology_resource']['available_storage_gb']:.1f}/{status['topology_resource']['total_storage_gb']:.1f}GB available")
|
|
print(f" Core Utilization: {[f'{c:.2f}' for c in status['topology_resource']['core_utilization']]}")
|
|
|
|
print(f"\n📊 Container Map Statistics:")
|
|
print(f" Processes: {status['topology_resource']['container_map_stats']['total_processes']}")
|
|
print(f" Network Edges: {status['topology_resource']['container_map_stats']['total_network_edges']}")
|
|
print(f" File Systems: {status['topology_resource']['container_map_stats']['total_file_systems']}")
|
|
print(f" Memory Regions: {status['topology_resource']['container_map_stats']['total_memory_regions']}")
|
|
print(f" Open Sockets: {status['topology_resource']['container_map_stats']['total_open_sockets']}")
|
|
print(f" Devices: {status['topology_resource']['container_map_stats']['total_devices']}")
|
|
print(f" Kernel Parameters: {status['topology_resource']['container_map_stats']['total_kernel_parameters']}")
|
|
|
|
print(f"\n🧠 Functional Collapse Paradigm - Cognitive Load Metrics:")
|
|
fcp = status['functional_collapse_paradigm']
|
|
if fcp['cognitive_load_metrics']:
|
|
clm = fcp['cognitive_load_metrics']
|
|
print(f" Intrinsic Load (L_I): {clm['intrinsic_load']:.4f}")
|
|
print(f" Effort Load (L_E): {clm['effort_load']:.4f}")
|
|
print(f" Total System Load (L_total): {clm['total_system_load']:.4f}")
|
|
print(f" Efficiency Factor (η): {clm['efficiency_factor']:.4f}")
|
|
print(f" Distribution Precision (P_w): {clm['distribution_precision']:.4f}")
|
|
print(f" Operational Threshold: {clm['operational_threshold']}")
|
|
print(f" Criticality Reached: {clm['criticality_reached']} {'⚠️ CRITICAL' if clm['criticality_reached'] else ''}")
|
|
print(f" Gear Pitch: {fcp['gear_pitch']:.3f}")
|
|
print(f" Avalanche Count: {fcp['avalanche_count']}")
|
|
print(f" History Length: {fcp['history_length']}")
|
|
|
|
print(f"\n📋 Workloads:")
|
|
print(f" Active: {status['workloads']['active']}")
|
|
print(f" Queued: {status['workloads']['queued']}")
|
|
print(f" Completed: {status['workloads']['completed']}")
|
|
|
|
print(f"\n📈 Metrics:")
|
|
for key, metric in status['metrics'].items():
|
|
print(f" {key}: avg {metric['avg']:.3f}, max {metric['max']:.3f}")
|
|
|
|
print("\n" + "=" * 70)
|
|
|
|
def create_sample_workloads(num_workloads: int) -> List[Workload]:
|
|
"""Create sample workloads for testing."""
|
|
workloads = []
|
|
|
|
for i in range(num_workloads):
|
|
workload_type = random.choice(list(WorkloadType))
|
|
|
|
workload = Workload(
|
|
workload_id=f"workload_{i}",
|
|
workload_type=workload_type,
|
|
cpu_required=random.uniform(0.1, 0.5),
|
|
memory_required=random.uniform(0.05, 0.3),
|
|
storage_required=random.uniform(0.01, 0.05),
|
|
bandwidth_required=random.uniform(0.01, 0.1),
|
|
priority=random.randint(1, 10),
|
|
duration=random.uniform(5.0, 30.0),
|
|
executable=lambda: time.sleep(random.uniform(5.0, 30.0))
|
|
)
|
|
|
|
workloads.append(workload)
|
|
|
|
return workloads
|
|
|
|
import random
|
|
|
|
def timeout_handler(signum, frame):
|
|
"""Watchdog timeout handler - forcibly kills the process."""
|
|
print(f"\n⏱️ WATCHDOG TIMEOUT - Process forcibly terminated after {timeout}s")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
print("=" * 70)
|
|
print("Architect Node Topology Driver (Based on Accurate Container Map)")
|
|
print("Maximizing Topology Utilization with Functional Collapse Paradigm")
|
|
print("=" * 70)
|
|
|
|
# Set watchdog timeout
|
|
timeout = 30
|
|
signal.signal(signal.SIGALRM, timeout_handler)
|
|
signal.alarm(timeout)
|
|
print(f"Watchdog timeout set: {timeout}s")
|
|
|
|
try:
|
|
# Create driver with accurate container map
|
|
driver = ArchitectTopologyDriverAccurate()
|
|
|
|
# Start driver
|
|
if not driver.start():
|
|
print("Failed to start driver")
|
|
sys.exit(1)
|
|
|
|
# Create sample workloads
|
|
print("\nCreating sample workloads...")
|
|
num_workloads = 50
|
|
workloads = create_sample_workloads(num_workloads)
|
|
print(f"Created {len(workloads)} workloads")
|
|
|
|
# Submit workloads
|
|
print("\nSubmitting workloads...")
|
|
for workload in workloads:
|
|
driver.submit_workload(workload)
|
|
|
|
print(f"Submitted {len(workloads)} workloads")
|
|
|
|
# Monitor with timeout
|
|
print("\nMonitoring for 30 seconds with watchdog...")
|
|
start_time = time.time()
|
|
|
|
while time.time() - start_time < timeout:
|
|
time.sleep(1)
|
|
elapsed = int(time.time() - start_time)
|
|
if elapsed % 10 == 0:
|
|
driver.print_status()
|
|
print(f"\nTime remaining: {timeout - elapsed}s")
|
|
|
|
# Cancel watchdog
|
|
signal.alarm(0)
|
|
|
|
# Stop driver
|
|
driver.stop()
|
|
|
|
# Final status
|
|
driver.print_status()
|
|
|
|
print("\n✅ Architect topology driver test complete")
|
|
print("Driver based on accurate container map with Functional Collapse Paradigm")
|
|
print(f"Test duration: {int(time.time() - start_time)}s")
|
|
|
|
except KeyboardInterrupt:
|
|
print("\n\nInterrupted by user")
|
|
signal.alarm(0)
|
|
driver.stop()
|
|
sys.exit(0)
|
|
except Exception as e:
|
|
print(f"\n❌ Error: {e}")
|
|
signal.alarm(0)
|
|
driver.stop()
|
|
sys.exit(1)
|