kerf-multikernel 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
kerf/dtc/validator.py ADDED
@@ -0,0 +1,792 @@
1
+ # Copyright 2025 Multikernel Technologies, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """
16
+ Validation layer for multikernel device tree configurations.
17
+ """
18
+
19
+ import re
20
+ from pathlib import Path
21
+ from typing import List, Dict, Optional
22
+ from ..models import GlobalDeviceTree, ValidationResult, ResourceUsage
23
+ from ..resources import get_memory_pool_from_iomem
24
+
25
+
26
+ class MultikernelValidator:
27
+ """Validator for multikernel device tree configurations."""
28
+
29
+ def __init__(self):
30
+ self.errors: List[str] = []
31
+ self.warnings: List[str] = []
32
+ self.suggestions: List[str] = []
33
+ self.dts_content: str = ""
34
+ self.dts_filename: str = ""
35
+
36
+ def set_dts_context(self, dts_content: str, filename: str = ""):
37
+ """Set DTS content and filename for enhanced error messages."""
38
+ self.dts_content = dts_content
39
+ self.dts_filename = filename
40
+
41
+ def _find_line_number(self, pattern: str) -> int:
42
+ """Find line number for a pattern in DTS content."""
43
+ if not self.dts_content:
44
+ return 0
45
+
46
+ lines = self.dts_content.split("\n")
47
+ for i, line in enumerate(lines, 1):
48
+ if pattern in line:
49
+ return i
50
+ return 0
51
+
52
+ def _format_error_with_context(
53
+ self,
54
+ error_type: str,
55
+ instance_name: str,
56
+ problem: str,
57
+ current_state: str,
58
+ suggestion: str,
59
+ alternative: str = None,
60
+ pattern: str = None,
61
+ ) -> str:
62
+ """Format enhanced error message with context and suggestions."""
63
+ lines = []
64
+
65
+ lines.append(f"{instance_name}: {error_type}")
66
+ lines.append(f" {problem}")
67
+ lines.append(f" Current state: {current_state}")
68
+
69
+ lines.append(f" Suggestion: {suggestion}")
70
+ if alternative:
71
+ lines.append(f" Alternative: {alternative}")
72
+
73
+ if pattern and self.dts_content:
74
+ line_num = self._find_line_number(pattern)
75
+ if line_num > 0:
76
+ filename = self.dts_filename or "system.dts"
77
+ lines.append(f" In file {filename}:")
78
+ lines.append(f" Line {line_num}: {pattern}")
79
+
80
+ return "\n".join(lines)
81
+
82
+ def validate(self, tree: GlobalDeviceTree) -> ValidationResult:
83
+ """Perform comprehensive validation of the device tree."""
84
+ self.errors.clear()
85
+ self.warnings.clear()
86
+ self.suggestions.clear()
87
+
88
+ self._validate_hardware_inventory(tree)
89
+ self._validate_instances(tree)
90
+ self._validate_resource_allocations(tree)
91
+ self._validate_device_references(tree)
92
+
93
+ usage = self._calculate_resource_usage(tree)
94
+ self._validate_resource_limits(usage, tree)
95
+
96
+ return ValidationResult(
97
+ is_valid=len(self.errors) == 0,
98
+ errors=self.errors.copy(),
99
+ warnings=self.warnings.copy(),
100
+ suggestions=self.suggestions.copy(),
101
+ )
102
+
103
+ def _get_system_cpu_ids(self) -> Optional[set[int]]:
104
+ """
105
+ Get the set of physical CPU IDs from /proc/cpuinfo.
106
+ Maps logical processor IDs to physical IDs and returns the set of physical IDs.
107
+ """
108
+ try:
109
+ cpuinfo_path = Path("/proc/cpuinfo")
110
+ if cpuinfo_path.exists():
111
+ with open(cpuinfo_path, "r", encoding="utf-8") as f:
112
+ content = f.read()
113
+ physical_ids = set()
114
+ current_physical_id = None
115
+
116
+ for line in content.split("\n"):
117
+ if line.startswith("processor"):
118
+ # Extract logical processor ID
119
+ current_physical_id = None
120
+ elif line.startswith("physical id"):
121
+ # Extract physical ID for current processor
122
+ match = re.search(r"physical id\s*:\s*(\d+)", line)
123
+ if match:
124
+ current_physical_id = int(match.group(1))
125
+ physical_ids.add(current_physical_id)
126
+
127
+ if physical_ids:
128
+ return physical_ids
129
+ except (OSError, IOError, ValueError):
130
+ pass
131
+
132
+ return None
133
+
134
+ def _get_processor_to_physical_id_map(self) -> Optional[Dict[int, int]]:
135
+ """
136
+ Get mapping from logical processor ID to physical ID from /proc/cpuinfo.
137
+ Returns dict mapping processor ID -> physical ID.
138
+ """
139
+ try:
140
+ cpuinfo_path = Path("/proc/cpuinfo")
141
+ if cpuinfo_path.exists():
142
+ with open(cpuinfo_path, "r", encoding="utf-8") as f:
143
+ content = f.read()
144
+ processor_to_physical = {}
145
+ current_processor = None
146
+
147
+ for line in content.split("\n"):
148
+ if line.startswith("processor"):
149
+ # Extract logical processor ID
150
+ match = re.search(r"processor\s*:\s*(\d+)", line)
151
+ if match:
152
+ current_processor = int(match.group(1))
153
+ elif line.startswith("physical id") and current_processor is not None:
154
+ # Extract physical ID for current processor
155
+ match = re.search(r"physical id\s*:\s*(\d+)", line)
156
+ if match:
157
+ physical_id = int(match.group(1))
158
+ processor_to_physical[current_processor] = physical_id
159
+
160
+ if processor_to_physical:
161
+ return processor_to_physical
162
+ except (OSError, IOError, ValueError):
163
+ pass
164
+
165
+ return None
166
+
167
+ def _get_system_cpu_count(self) -> Optional[int]:
168
+ """Get the actual CPU count from /proc/cpuinfo."""
169
+ cpu_ids = self._get_system_cpu_ids()
170
+ if cpu_ids is not None:
171
+ return len(cpu_ids)
172
+ return None
173
+
174
+ def _get_system_physical_memory(self) -> Optional[int]:
175
+ """Get total physical memory in bytes from /proc/meminfo."""
176
+ try:
177
+ meminfo_path = Path("/proc/meminfo")
178
+ if meminfo_path.exists():
179
+ with open(meminfo_path, "r", encoding="utf-8") as f:
180
+ for line in f:
181
+ if line.startswith("MemTotal:"):
182
+ # Format: "MemTotal: 16384000 kB"
183
+ match = re.search(r"MemTotal:\s*(\d+)\s*kB", line)
184
+ if match:
185
+ # Convert from kB to bytes
186
+ return int(match.group(1)) * 1024
187
+ except (OSError, IOError, ValueError):
188
+ pass
189
+
190
+ return None
191
+
192
+ def _validate_hardware_inventory(self, tree: GlobalDeviceTree):
193
+ """Validate hardware inventory consistency and against running system."""
194
+ cpus = tree.hardware.cpus
195
+ if cpus.total <= 0:
196
+ self.errors.append("Hardware inventory: Total CPU count must be positive")
197
+
198
+ if not cpus.available:
199
+ self.errors.append("Hardware inventory: No CPUs available for spawn kernels")
200
+
201
+ host_set = set(cpus.host_reserved)
202
+ available_set = set(cpus.available)
203
+ overlap = host_set.intersection(available_set)
204
+ if overlap:
205
+ self.errors.append(
206
+ f"Hardware inventory: CPU overlap between host and available: {sorted(overlap)}"
207
+ )
208
+
209
+ processor_to_physical = self._get_processor_to_physical_id_map()
210
+ if processor_to_physical is not None:
211
+ system_physical_ids = set(processor_to_physical.values())
212
+ system_cpu_count = len(system_physical_ids)
213
+
214
+ if cpus.total > system_cpu_count:
215
+ self.warnings.append(
216
+ f"Hardware inventory: Total CPU count ({cpus.total}) exceeds system physical CPU count ({system_cpu_count}). "
217
+ f"This may indicate CPUs were hot-unplugged after baseline was created."
218
+ )
219
+
220
+ all_cpu_ids = set()
221
+ if cpus.host_reserved:
222
+ all_cpu_ids.update(cpus.host_reserved)
223
+ if cpus.available:
224
+ all_cpu_ids.update(cpus.available)
225
+
226
+ if all_cpu_ids:
227
+ invalid_processors = []
228
+ physical_ids_used = set()
229
+
230
+ for cpu_id in all_cpu_ids:
231
+ if cpu_id not in processor_to_physical:
232
+ invalid_processors.append(cpu_id)
233
+ else:
234
+ physical_ids_used.add(processor_to_physical[cpu_id])
235
+
236
+ if invalid_processors:
237
+ valid_processors = sorted(processor_to_physical.keys())
238
+ self.warnings.append(
239
+ f"Hardware inventory: CPU IDs (logical processors) {sorted(invalid_processors)} do not exist in system. "
240
+ f"Available logical processors: {valid_processors}. "
241
+ f"This may indicate CPUs were hot-unplugged after baseline was created."
242
+ )
243
+
244
+ invalid_physical_ids = physical_ids_used - system_physical_ids
245
+ if invalid_physical_ids:
246
+ self.warnings.append(
247
+ f"Hardware inventory: Physical CPU IDs {sorted(invalid_physical_ids)} do not exist in system. "
248
+ f"Available physical CPU IDs: {sorted(system_physical_ids)}. "
249
+ f"This may indicate CPUs were hot-unplugged after baseline was created."
250
+ )
251
+ else:
252
+ self.warnings.append(
253
+ "Could not determine CPU to physical ID mapping from /proc/cpuinfo - skipping CPU validation"
254
+ )
255
+
256
+ memory = tree.hardware.memory
257
+ if memory.total_bytes <= 0:
258
+ self.errors.append("Hardware inventory: Total memory must be positive")
259
+
260
+ if memory.memory_pool_bytes <= 0:
261
+ self.errors.append("Hardware inventory: Spawn pool size must be positive")
262
+
263
+ iomem_pool = get_memory_pool_from_iomem()
264
+ if iomem_pool is None or (
265
+ memory.memory_pool_base != iomem_pool[0] or memory.memory_pool_bytes != iomem_pool[1]
266
+ ):
267
+ # Pool doesn't match kernel-provided pool, validate against total_bytes
268
+ if memory.memory_pool_base + memory.memory_pool_bytes > memory.total_bytes:
269
+ self.errors.append("Hardware inventory: Spawn pool extends beyond total memory")
270
+ if iomem_pool is not None:
271
+ iomem_base, iomem_size = iomem_pool
272
+ iomem_end = iomem_base + iomem_size
273
+
274
+ if memory.memory_pool_base != iomem_base:
275
+ self.errors.append(
276
+ f"Hardware inventory: Memory pool base ({hex(memory.memory_pool_base)}) "
277
+ f"does not match multikernel pool in /proc/iomem ({hex(iomem_base)})"
278
+ )
279
+
280
+ pool_end = memory.memory_pool_base + memory.memory_pool_bytes
281
+ if pool_end > iomem_end:
282
+ exceeds_by = pool_end - iomem_end
283
+ self.errors.append(
284
+ f"Hardware inventory: Memory pool extends beyond multikernel reserved pool in /proc/iomem "
285
+ f"by {exceeds_by} bytes ({exceeds_by / (1024**3):.2f} GB). "
286
+ f"Configured pool: {hex(memory.memory_pool_base)}-{hex(pool_end-1)}, "
287
+ f"Reserved pool: {hex(iomem_base)}-{hex(iomem_end-1)}"
288
+ )
289
+
290
+ if memory.memory_pool_bytes > iomem_size:
291
+ self.errors.append(
292
+ f"Hardware inventory: Memory pool size ({memory.memory_pool_bytes} bytes = "
293
+ f"{memory.memory_pool_bytes / (1024**3):.2f} GB) exceeds multikernel reserved pool "
294
+ f"({iomem_size} bytes = {iomem_size / (1024**3):.2f} GB) in /proc/iomem"
295
+ )
296
+ else:
297
+ self.warnings.append("Could not find multikernel memory pool in /proc/iomem")
298
+
299
+ def _validate_instances(self, tree: GlobalDeviceTree):
300
+ """Validate instance definitions."""
301
+ instance_names = set()
302
+ instance_ids = set()
303
+
304
+ for _, instance in tree.instances.items():
305
+ if instance.name in instance_names:
306
+ self.errors.append(
307
+ f"Duplicate instance name: '{instance.name}' appears multiple times"
308
+ )
309
+ instance_names.add(instance.name)
310
+
311
+ if instance.id is not None:
312
+ if instance.id in instance_ids:
313
+ self.errors.append(
314
+ f"Duplicate instance ID: {instance.id} assigned to multiple instances"
315
+ )
316
+ instance_ids.add(instance.id)
317
+
318
+ self._validate_instance_resources(instance, tree)
319
+
320
+ def _validate_instance_resources(self, instance, tree: GlobalDeviceTree):
321
+ """Validate resources for a specific instance."""
322
+ self._validate_cpu_allocation(instance, tree)
323
+ self._validate_memory_allocation(instance, tree)
324
+ self._validate_device_allocation(instance, tree)
325
+ self._validate_topology_constraints(instance, tree)
326
+
327
+ def _validate_cpu_allocation(self, instance, tree: GlobalDeviceTree):
328
+ """Validate CPU allocation for an instance."""
329
+ cpus = tree.hardware.cpus
330
+ instance_cpus = set(instance.resources.cpus)
331
+
332
+ for cpu in instance_cpus:
333
+ if cpu < 0 or cpu >= cpus.total:
334
+ error_msg = self._format_error_with_context(
335
+ error_type="CPU allocation error",
336
+ instance_name=instance.name,
337
+ problem=f"CPU {cpu} does not exist in hardware inventory",
338
+ current_state=f"Hardware has CPUs 0-{cpus.total-1}",
339
+ suggestion=f"Use CPUs in range 0-{cpus.total-1}",
340
+ alternative="Check hardware inventory configuration",
341
+ pattern=f"cpus = <{','.join(map(str, sorted(instance_cpus)))}>",
342
+ )
343
+ self.errors.append(error_msg)
344
+
345
+ host_reserved = set(cpus.host_reserved)
346
+ reserved_cpus = instance_cpus.intersection(host_reserved)
347
+ if reserved_cpus:
348
+ available_cpus = sorted(set(cpus.available))
349
+ error_msg = self._format_error_with_context(
350
+ error_type="CPU allocation error",
351
+ instance_name=instance.name,
352
+ problem=f"CPUs {sorted(reserved_cpus)} are reserved for host kernel",
353
+ current_state=f"Host reserved CPUs: {sorted(host_reserved)}",
354
+ suggestion=f"Use available CPUs: {available_cpus}",
355
+ alternative="Modify host-reserved CPU configuration",
356
+ pattern=f"cpus = <{','.join(map(str, sorted(instance_cpus)))}>",
357
+ )
358
+ self.errors.append(error_msg)
359
+
360
+ for other_name, other_instance in tree.instances.items():
361
+ if other_name == instance.name:
362
+ continue
363
+
364
+ other_cpus = set(other_instance.resources.cpus)
365
+ overlap = instance_cpus.intersection(other_cpus)
366
+ if overlap:
367
+ error_msg = self._format_error_with_context(
368
+ error_type="CPU allocation conflict",
369
+ instance_name=instance.name,
370
+ problem=f"CPU overlap with instance '{other_name}'",
371
+ current_state=f"Both instances use CPUs: {sorted(overlap)}",
372
+ suggestion=f"Assign different CPUs to {instance.name} or {other_name}",
373
+ alternative="Use CPU ranges instead of individual CPUs",
374
+ pattern=f"cpus = <{','.join(map(str, sorted(instance_cpus)))}>",
375
+ )
376
+ self.errors.append(error_msg)
377
+ # Add suggestions
378
+ available_cpus = set(cpus.available) - instance_cpus - other_cpus
379
+ if available_cpus:
380
+ self.suggestions.append(
381
+ f"Consider using available CPUs: {sorted(list(available_cpus)[:len(overlap)])}"
382
+ )
383
+
384
+ def _validate_memory_allocation(self, instance, tree: GlobalDeviceTree):
385
+ """Validate memory allocation for an instance."""
386
+ memory = tree.hardware.memory
387
+ instance_memory = instance.resources
388
+
389
+ memory_start = instance_memory.memory_base
390
+ memory_end = memory_start + instance_memory.memory_bytes
391
+
392
+ if memory_start < memory.memory_pool_base:
393
+ error_msg = self._format_error_with_context(
394
+ error_type="Memory allocation error",
395
+ instance_name=instance.name,
396
+ problem=f"Memory base {hex(memory_start)} is before memory pool start",
397
+ current_state=f"Spawn pool starts at {hex(memory.memory_pool_base)}",
398
+ suggestion=f"Use memory base >= {hex(memory.memory_pool_base)}",
399
+ alternative="Adjust memory pool configuration",
400
+ pattern=f"memory-base = <{hex(memory_start)}>",
401
+ )
402
+ self.errors.append(error_msg)
403
+
404
+ if memory_end > memory.memory_pool_base + memory.memory_pool_bytes:
405
+ pool_end = memory.memory_pool_base + memory.memory_pool_bytes
406
+ exceeds_by = memory_end - pool_end
407
+ error_msg = self._format_error_with_context(
408
+ error_type="Memory allocation error",
409
+ instance_name=instance.name,
410
+ problem="Memory region extends beyond memory pool",
411
+ current_state=f"Instance memory: {hex(memory_start)}-{hex(memory_end)}, Pool: {hex(memory.memory_pool_base)}-{hex(pool_end)}",
412
+ suggestion=f"Reduce memory size by {hex(exceeds_by)} or use different base address",
413
+ alternative="Increase memory pool size or adjust memory allocation",
414
+ pattern=f"memory-size = <{hex(instance_memory.memory_bytes)}>",
415
+ )
416
+ self.errors.append(error_msg)
417
+
418
+ if memory_start % 0x1000 != 0:
419
+ self.warnings.append(
420
+ f"Instance {instance.name}: Memory base {hex(memory_start)} not page-aligned\n"
421
+ f" Page size: 4KB (0x1000)\n"
422
+ f" Suggestion: Use base address {hex((memory_start // 0x1000) * 0x1000)}"
423
+ )
424
+
425
+ for other_name, other_instance in tree.instances.items():
426
+ if other_name == instance.name:
427
+ continue
428
+
429
+ other_start = other_instance.resources.memory_base
430
+ other_end = other_start + other_instance.resources.memory_bytes
431
+
432
+ if not (memory_end <= other_start or other_end <= memory_start):
433
+ overlap_start = max(memory_start, other_start)
434
+ overlap_end = min(memory_end, other_end)
435
+ overlap_size = overlap_end - overlap_start
436
+
437
+ self.errors.append(
438
+ f"Instance {instance.name} and {other_name}: Memory region overlap detected\n"
439
+ f" {instance.name} memory: {hex(memory_start)} - {hex(memory_end)}\n"
440
+ f" {other_name} memory: {hex(other_start)} - {hex(other_end)}\n"
441
+ f" Overlapping region: {hex(overlap_start)} - {hex(overlap_end)} ({overlap_size} bytes)"
442
+ )
443
+
444
+ def _validate_device_allocation(self, instance, tree: GlobalDeviceTree):
445
+ """Validate device allocation for an instance."""
446
+
447
+ for device_ref in instance.resources.devices:
448
+ if "_vf" in device_ref:
449
+ device_name = device_ref.split("_vf")[0]
450
+ vf_id = int(device_ref.split("_vf")[1])
451
+
452
+ if device_name not in tree.hardware.devices:
453
+ available_devices = list(tree.hardware.devices.keys())
454
+ error_msg = self._format_error_with_context(
455
+ error_type="Device reference error",
456
+ instance_name=instance.name,
457
+ problem=f"Reference to non-existent device '{device_name}'",
458
+ current_state=f"Device '{device_name}' not found in hardware inventory",
459
+ suggestion=f"Use available devices: {available_devices}",
460
+ alternative="Add device '{device_name}' to hardware inventory",
461
+ pattern=f"devices = <&{device_ref}>",
462
+ )
463
+ self.errors.append(error_msg)
464
+ continue
465
+
466
+ device_info = tree.hardware.devices[device_name]
467
+ if device_info.available_vfs and vf_id not in device_info.available_vfs:
468
+ available_vfs = sorted(device_info.available_vfs)
469
+ error_msg = self._format_error_with_context(
470
+ error_type="VF allocation error",
471
+ instance_name=instance.name,
472
+ problem=f"VF {vf_id} not available for device {device_name}",
473
+ current_state=f"Device {device_name} has VFs: {available_vfs}",
474
+ suggestion=f"Use available VF: {available_vfs[0] if available_vfs else 'none'}",
475
+ alternative="Configure VF {vf_id} for device {device_name}",
476
+ pattern=f"devices = <&{device_ref}>",
477
+ )
478
+ self.errors.append(error_msg)
479
+
480
+ elif "_ns" in device_ref:
481
+ device_name = device_ref.split("_ns")[0]
482
+ ns_id = int(device_ref.split("_ns")[1])
483
+
484
+ if device_name not in tree.hardware.devices:
485
+ self.errors.append(
486
+ f"Instance {instance.name}: Reference to non-existent device '{device_name}'"
487
+ )
488
+ continue
489
+
490
+ device_info = tree.hardware.devices[device_name]
491
+ if device_info.available_ns and ns_id not in device_info.available_ns:
492
+ self.errors.append(
493
+ f"Instance {instance.name}: Namespace {ns_id} not available for device {device_name}"
494
+ )
495
+
496
+ else:
497
+ # Direct device reference
498
+ if device_ref not in tree.hardware.devices:
499
+ self.errors.append(
500
+ f"Instance {instance.name}: Reference to non-existent device '{device_ref}'"
501
+ )
502
+
503
+ def _validate_resource_allocations(self, tree: GlobalDeviceTree):
504
+ """Validate overall resource allocation limits."""
505
+ total_cpus_allocated = 0
506
+ total_memory_allocated = 0
507
+
508
+ for instance in tree.instances.values():
509
+ total_cpus_allocated += len(instance.resources.cpus)
510
+ total_memory_allocated += instance.resources.memory_bytes
511
+
512
+ available_cpus = len(tree.hardware.cpus.available)
513
+ if total_cpus_allocated > available_cpus:
514
+ self.errors.append(
515
+ f"Total CPU allocation ({total_cpus_allocated}) exceeds available CPUs ({available_cpus})"
516
+ )
517
+
518
+ if total_memory_allocated > tree.hardware.memory.memory_pool_bytes:
519
+ self.errors.append(
520
+ f"Total memory allocation ({total_memory_allocated} bytes) "
521
+ f"exceeds memory pool ({tree.hardware.memory.memory_pool_bytes} bytes)"
522
+ )
523
+
524
+ def _validate_device_references(self, tree: GlobalDeviceTree):
525
+ """Validate device references and phandles."""
526
+
527
+ for name, device_ref in tree.device_references.items():
528
+ if hasattr(device_ref, "parent") and device_ref.parent:
529
+ parent_name = device_ref.parent.replace("&", "").replace(":", "")
530
+ if parent_name not in tree.hardware.devices:
531
+ self.errors.append(
532
+ f"Device reference '{name}': Parent device '{parent_name}' not found in hardware inventory"
533
+ )
534
+
535
+ if hasattr(device_ref, "vf_id") and device_ref.vf_id is not None:
536
+ if hasattr(device_ref, "parent") and device_ref.parent:
537
+ parent_name = device_ref.parent.replace("&", "").replace(":", "")
538
+ if parent_name in tree.hardware.devices:
539
+ device_info = tree.hardware.devices[parent_name]
540
+ if (
541
+ device_info.available_vfs
542
+ and device_ref.vf_id not in device_info.available_vfs
543
+ ):
544
+ self.errors.append(
545
+ f"Device reference '{name}': VF {device_ref.vf_id} not available for device {parent_name}"
546
+ )
547
+
548
+ if hasattr(device_ref, "namespace_id") and device_ref.namespace_id is not None:
549
+ if hasattr(device_ref, "parent") and device_ref.parent:
550
+ parent_name = device_ref.parent.replace("&", "").replace(":", "")
551
+ if parent_name in tree.hardware.devices:
552
+ device_info = tree.hardware.devices[parent_name]
553
+ if (
554
+ device_info.available_ns
555
+ and device_ref.namespace_id not in device_info.available_ns
556
+ ):
557
+ self.errors.append(
558
+ f"Device reference '{name}': Namespace {device_ref.namespace_id} not available for device {parent_name}"
559
+ )
560
+
561
+ def _calculate_resource_usage(self, tree: GlobalDeviceTree) -> ResourceUsage:
562
+ """Calculate resource usage summary."""
563
+ total_cpus_allocated = sum(
564
+ len(instance.resources.cpus) for instance in tree.instances.values()
565
+ )
566
+ total_memory_allocated = sum(
567
+ instance.resources.memory_bytes for instance in tree.instances.values()
568
+ )
569
+
570
+ return ResourceUsage(
571
+ cpus_allocated=total_cpus_allocated,
572
+ cpus_total=len(tree.hardware.cpus.available),
573
+ memory_allocated=total_memory_allocated,
574
+ memory_total=tree.hardware.memory.memory_pool_bytes,
575
+ devices_allocated=0, # Would calculate from device allocations
576
+ devices_total=len(tree.hardware.devices),
577
+ )
578
+
579
+ def _validate_resource_limits(self, usage: ResourceUsage, tree: GlobalDeviceTree): # pylint: disable=unused-argument
580
+ """Validate resource limits and generate warnings."""
581
+ unallocated_cpus = usage.cpus_total - usage.cpus_allocated
582
+ if unallocated_cpus > 0:
583
+ percentage = (unallocated_cpus / usage.cpus_total) * 100
584
+ self.warnings.append(
585
+ f"Resource utilization: {unallocated_cpus} CPUs ({percentage:.1f}% of memory pool) remain unallocated"
586
+ )
587
+
588
+ unallocated_memory = usage.memory_total - usage.memory_allocated
589
+ if unallocated_memory > 0:
590
+ percentage = (unallocated_memory / usage.memory_total) * 100
591
+ self.warnings.append(
592
+ f"Resource utilization: {unallocated_memory} bytes ({percentage:.1f}% of memory pool) remain unallocated"
593
+ )
594
+
595
+ def _validate_topology_constraints(self, instance, tree: GlobalDeviceTree):
596
+ """Validate NUMA and CPU topology constraints for an instance."""
597
+ resources = instance.resources
598
+
599
+ if resources.numa_nodes and tree.hardware.topology and tree.hardware.topology.numa_nodes:
600
+ self._validate_numa_constraints(instance, tree)
601
+ if resources.cpu_affinity:
602
+ self._validate_cpu_affinity_constraints(instance, tree)
603
+ if resources.memory_policy:
604
+ self._validate_memory_policy_constraints(instance, tree)
605
+
606
+ def _validate_numa_constraints(self, instance, tree: GlobalDeviceTree):
607
+ """Validate NUMA node constraints for an instance."""
608
+ resources = instance.resources
609
+ topology = tree.hardware.topology
610
+
611
+ if not topology or not topology.numa_nodes:
612
+ return
613
+
614
+ for numa_node in resources.numa_nodes:
615
+ if numa_node not in topology.numa_nodes:
616
+ self.errors.append(
617
+ f"Instance {instance.name}: NUMA node {numa_node} does not exist in hardware topology"
618
+ )
619
+
620
+ if resources.numa_nodes:
621
+ for cpu in resources.cpus:
622
+ cpu_numa_node = topology.get_numa_node_for_cpu(cpu)
623
+
624
+ if cpu_numa_node is not None and cpu_numa_node not in resources.numa_nodes:
625
+ self.warnings.append(
626
+ f"Instance {instance.name}: CPU {cpu} is in NUMA node {cpu_numa_node}, "
627
+ f"but instance is configured for NUMA nodes {resources.numa_nodes}. "
628
+ f"This may cause performance issues due to remote memory access."
629
+ )
630
+
631
+ def _validate_cpu_affinity_constraints(self, instance, tree: GlobalDeviceTree):
632
+ """Validate CPU affinity constraints for an instance."""
633
+ resources = instance.resources
634
+
635
+ if resources.cpu_affinity == "compact":
636
+ self._validate_compact_affinity(instance, tree)
637
+ elif resources.cpu_affinity == "spread":
638
+ self._validate_spread_affinity(instance, tree)
639
+ elif resources.cpu_affinity == "local":
640
+ self._validate_local_affinity(instance, tree)
641
+
642
+ def _validate_compact_affinity(self, instance, tree: GlobalDeviceTree):
643
+ """Validate compact CPU affinity."""
644
+ resources = instance.resources
645
+ cpus = resources.cpus
646
+
647
+ if not tree.hardware.topology or not tree.hardware.topology.numa_nodes:
648
+ return
649
+
650
+ numa_nodes = set()
651
+ for cpu in cpus:
652
+ numa_node = tree.hardware.topology.get_numa_node_for_cpu(cpu)
653
+ if numa_node is not None:
654
+ numa_nodes.add(numa_node)
655
+
656
+ if len(numa_nodes) > 1:
657
+ self.warnings.append(
658
+ f"Instance {instance.name}: Compact CPU affinity requested but CPUs span multiple NUMA nodes: {sorted(numa_nodes)}"
659
+ )
660
+
661
+ if tree.hardware.cpus.topology:
662
+ cores = set()
663
+ for cpu in cpus:
664
+ if cpu in tree.hardware.cpus.topology:
665
+ cores.add(tree.hardware.cpus.topology[cpu].core_id)
666
+
667
+ if len(cores) > len(cpus) // 2:
668
+ self.warnings.append(
669
+ f"Instance {instance.name}: Compact CPU affinity may not be optimal - CPUs span {len(cores)} cores"
670
+ )
671
+
672
+ def _validate_spread_affinity(self, instance, tree: GlobalDeviceTree):
673
+ """Validate spread CPU affinity."""
674
+ resources = instance.resources
675
+ cpus = resources.cpus
676
+
677
+ if not tree.hardware.topology or not tree.hardware.topology.numa_nodes:
678
+ return
679
+
680
+ numa_nodes = set()
681
+ for cpu in cpus:
682
+ numa_node = tree.hardware.topology.get_numa_node_for_cpu(cpu)
683
+ if numa_node is not None:
684
+ numa_nodes.add(numa_node)
685
+
686
+ if len(numa_nodes) < 2:
687
+ self.warnings.append(
688
+ f"Instance {instance.name}: Spread CPU affinity requested but CPUs are from single NUMA node {list(numa_nodes)[0]}"
689
+ )
690
+
691
+ def _validate_local_affinity(self, instance, tree: GlobalDeviceTree):
692
+ """Validate local CPU affinity (CPUs and memory on same NUMA node)."""
693
+ resources = instance.resources
694
+ cpus = resources.cpus
695
+
696
+ if not tree.hardware.topology or not tree.hardware.topology.numa_nodes:
697
+ return
698
+
699
+ memory_numa_node = None
700
+ memory_base = resources.memory_base
701
+
702
+ for node_id, node in tree.hardware.topology.numa_nodes.items():
703
+ if node.memory_base <= memory_base < node.memory_base + node.memory_size:
704
+ memory_numa_node = node_id
705
+ break
706
+
707
+ if memory_numa_node is None:
708
+ self.warnings.append(
709
+ f"Instance {instance.name}: Could not determine NUMA node for memory allocation at {hex(memory_base)}"
710
+ )
711
+ return
712
+
713
+ cpu_numa_nodes = set()
714
+ for cpu in cpus:
715
+ numa_node = tree.hardware.topology.get_numa_node_for_cpu(cpu)
716
+ if numa_node is not None:
717
+ cpu_numa_nodes.add(numa_node)
718
+
719
+ if memory_numa_node not in cpu_numa_nodes:
720
+ self.warnings.append(
721
+ f"Instance {instance.name}: Local affinity requested but CPUs are from NUMA nodes {sorted(cpu_numa_nodes)} "
722
+ f"while memory is on NUMA node {memory_numa_node}"
723
+ )
724
+
725
+ def _validate_memory_policy_constraints(self, instance, tree: GlobalDeviceTree):
726
+ """Validate memory policy constraints for an instance."""
727
+ resources = instance.resources
728
+
729
+ if resources.memory_policy == "local":
730
+ self._validate_local_memory_policy(instance, tree)
731
+ elif resources.memory_policy == "interleave":
732
+ self._validate_interleave_memory_policy(instance, tree)
733
+ elif resources.memory_policy == "bind":
734
+ self._validate_bind_memory_policy(instance, tree)
735
+
736
+ def _validate_local_memory_policy(self, instance, tree: GlobalDeviceTree):
737
+ """Validate local memory policy."""
738
+ resources = instance.resources
739
+
740
+ if not tree.hardware.topology or not tree.hardware.topology.numa_nodes:
741
+ return
742
+
743
+ memory_numa_node = None
744
+ memory_base = resources.memory_base
745
+
746
+ for node_id, node in tree.hardware.topology.numa_nodes.items():
747
+ if node.memory_base <= memory_base < node.memory_base + node.memory_size:
748
+ memory_numa_node = node_id
749
+ break
750
+
751
+ if memory_numa_node is None:
752
+ self.warnings.append(
753
+ f"Instance {instance.name}: Local memory policy requested but memory allocation "
754
+ f"at {hex(memory_base)} is not within any NUMA node memory range"
755
+ )
756
+ return
757
+
758
+ cpu_numa_nodes = set()
759
+ for cpu in resources.cpus:
760
+ numa_node = tree.hardware.topology.get_numa_node_for_cpu(cpu)
761
+ if numa_node is not None:
762
+ cpu_numa_nodes.add(numa_node)
763
+
764
+ if memory_numa_node not in cpu_numa_nodes:
765
+ self.warnings.append(
766
+ f"Instance {instance.name}: Local memory policy requested but CPUs are from NUMA nodes "
767
+ f"{sorted(cpu_numa_nodes)} while memory is on NUMA node {memory_numa_node}"
768
+ )
769
+
770
+ def _validate_interleave_memory_policy(self, instance, tree: GlobalDeviceTree):
771
+ """Validate interleave memory policy."""
772
+ # For interleave policy, we would typically check if memory is distributed
773
+ # across multiple NUMA nodes, but this is complex to validate without
774
+ # knowing the actual memory allocation implementation
775
+ _ = instance, tree # Placeholder for future validation
776
+
777
+ def _validate_bind_memory_policy(self, instance, tree: GlobalDeviceTree):
778
+ """Validate bind memory policy."""
779
+ resources = instance.resources
780
+
781
+ if not resources.numa_nodes:
782
+ self.warnings.append(
783
+ f"Instance {instance.name}: Bind memory policy requested but no NUMA nodes specified"
784
+ )
785
+ return
786
+
787
+ if tree.hardware.topology and tree.hardware.topology.numa_nodes:
788
+ for numa_node in resources.numa_nodes:
789
+ if numa_node not in tree.hardware.topology.numa_nodes:
790
+ self.errors.append(
791
+ f"Instance {instance.name}: Bind memory policy references non-existent NUMA node {numa_node}"
792
+ )