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/reporter.py ADDED
@@ -0,0 +1,308 @@
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 report generation and formatting.
17
+ """
18
+
19
+ from typing import List, Dict, Any
20
+ from ..models import GlobalDeviceTree, ValidationResult
21
+
22
+
23
+ class ValidationReporter:
24
+ """Generates validation reports in various formats."""
25
+
26
+ def generate_report(
27
+ self,
28
+ result: ValidationResult,
29
+ tree: GlobalDeviceTree,
30
+ verbose: bool = False,
31
+ format: str = "text",
32
+ ) -> str:
33
+ """Generate comprehensive validation report in specified format."""
34
+ if format == "json":
35
+ import json
36
+
37
+ return json.dumps(self.generate_json_report(result, tree), indent=2)
38
+ if format == "yaml":
39
+ return self.generate_yaml_report(result, tree)
40
+ return self._generate_text_report(result, tree, verbose)
41
+
42
+ def _generate_text_report(
43
+ self, result: ValidationResult, tree: GlobalDeviceTree, verbose: bool = False # pylint: disable=unused-argument
44
+ ) -> str:
45
+ """Generate text format validation report."""
46
+ lines = []
47
+
48
+ # Header
49
+ lines.append("Multikernel Device Tree Validation Report")
50
+ lines.append("=" * 42)
51
+
52
+ # Status
53
+ if result.is_valid:
54
+ lines.append("Status: ✓ VALID")
55
+ else:
56
+ lines.append("Status: ✗ INVALID")
57
+
58
+ lines.append("")
59
+
60
+ # Hardware inventory summary
61
+ lines.extend(self._format_hardware_inventory(tree))
62
+ lines.append("")
63
+
64
+ # Instance allocations
65
+ lines.extend(self._format_instance_allocations(tree))
66
+ lines.append("")
67
+
68
+ # Resource utilization
69
+ lines.extend(self._format_resource_utilization(tree))
70
+ lines.append("")
71
+
72
+ # Validation results
73
+ if result.errors:
74
+ lines.append("Validation Errors:")
75
+ for error in result.errors:
76
+ lines.append(f" ✗ {error}")
77
+ lines.append("")
78
+
79
+ if result.warnings:
80
+ lines.append("Validation Warnings:")
81
+ for warning in result.warnings:
82
+ lines.append(f" ⚠ {warning}")
83
+ lines.append("")
84
+
85
+ if result.suggestions:
86
+ lines.append("Suggestions:")
87
+ for suggestion in result.suggestions:
88
+ lines.append(f" 💡 {suggestion}")
89
+ lines.append("")
90
+
91
+ # Summary
92
+ if result.is_valid:
93
+ lines.append("✓ All validations passed")
94
+ else:
95
+ lines.append(f"✗ Validation failed with {len(result.errors)} errors")
96
+ if result.warnings:
97
+ lines.append(f" and {len(result.warnings)} warnings")
98
+
99
+ return "\n".join(lines)
100
+
101
+ def _format_hardware_inventory(self, tree: GlobalDeviceTree) -> List[str]:
102
+ """Format hardware inventory section."""
103
+ lines = []
104
+ lines.append("Hardware Inventory:")
105
+
106
+ # CPU information
107
+ cpus = tree.hardware.cpus
108
+ host_count = len(cpus.host_reserved)
109
+ available_count = len(cpus.available)
110
+ host_percent = (host_count / cpus.total) * 100
111
+ memory_pool_percent = (available_count / cpus.total) * 100
112
+
113
+ lines.append(f" CPUs: {cpus.total} total")
114
+ lines.append(
115
+ f" Host reserved: {cpus.host_reserved[0]}-{cpus.host_reserved[-1]} ({host_count} CPUs, {host_percent:.0f}%)"
116
+ )
117
+ lines.append(
118
+ f" Memory pool: {cpus.available[0]}-{cpus.available[-1]} ({available_count} CPUs, {memory_pool_percent:.0f}%)"
119
+ )
120
+
121
+ # Memory information
122
+ memory = tree.hardware.memory
123
+ total_gb = memory.total_bytes / (1024**3)
124
+ host_gb = memory.host_reserved_bytes / (1024**3)
125
+ memory_pool_gb = memory.memory_pool_bytes / (1024**3)
126
+ host_mem_percent = (memory.host_reserved_bytes / memory.total_bytes) * 100
127
+ memory_pool_mem_percent = (memory.memory_pool_bytes / memory.total_bytes) * 100
128
+
129
+ lines.append(f" Memory: {total_gb:.0f}GB total")
130
+ lines.append(f" Host reserved: {host_gb:.0f}GB ({host_mem_percent:.0f}%)")
131
+ lines.append(
132
+ f" Memory pool: {memory_pool_gb:.0f}GB at {hex(memory.memory_pool_base)} ({memory_pool_mem_percent:.0f}%)"
133
+ )
134
+
135
+ # Device information
136
+ device_count = len(tree.hardware.devices)
137
+ network_devices = sum(
138
+ 1 for d in tree.hardware.devices.values() if "ethernet" in d.compatible.lower()
139
+ )
140
+ storage_devices = sum(
141
+ 1 for d in tree.hardware.devices.values() if "nvme" in d.compatible.lower()
142
+ )
143
+
144
+ lines.append(f" Devices: {device_count} total")
145
+ if network_devices > 0:
146
+ lines.append(f" Network: {network_devices}")
147
+ if storage_devices > 0:
148
+ lines.append(f" Storage: {storage_devices}")
149
+
150
+ return lines
151
+
152
+ def _format_instance_allocations(self, tree: GlobalDeviceTree) -> List[str]:
153
+ """Format instance allocation section."""
154
+ lines = []
155
+ lines.append("Instance Allocations:")
156
+
157
+ for name, instance in tree.instances.items():
158
+ id_str = f"ID: {instance.id}" if instance.id is not None else "ID: auto-assigned"
159
+ lines.append(f" {name} ({id_str}):")
160
+
161
+ # CPU allocation
162
+ cpu_count = len(instance.resources.cpus)
163
+ cpu_range = (
164
+ f"{instance.resources.cpus[0]}-{instance.resources.cpus[-1]}"
165
+ if instance.resources.cpus
166
+ else "none"
167
+ )
168
+ cpu_percent = (cpu_count / len(tree.hardware.cpus.available)) * 100
169
+ lines.append(f" CPUs: {cpu_range} ({cpu_count} CPUs, {cpu_percent:.0f}% of pool)")
170
+
171
+ # Memory allocation
172
+ memory_gb = instance.resources.memory_bytes / (1024**3)
173
+ memory_percent = (
174
+ instance.resources.memory_bytes / tree.hardware.memory.memory_pool_bytes
175
+ ) * 100
176
+ lines.append(
177
+ f" Memory: {memory_gb:.0f}GB at {hex(instance.resources.memory_base)} ({memory_percent:.0f}% of pool)"
178
+ )
179
+
180
+ # Device allocation
181
+ device_count = len(instance.resources.devices)
182
+ if device_count > 0:
183
+ devices_str = ", ".join(instance.resources.devices)
184
+ lines.append(f" Devices: {devices_str}")
185
+ else:
186
+ lines.append(" Devices: none")
187
+
188
+ lines.append(" Status: ✓ Valid")
189
+ lines.append("")
190
+
191
+ return lines
192
+
193
+ def _format_resource_utilization(self, tree: GlobalDeviceTree) -> List[str]:
194
+ """Format resource utilization section."""
195
+ lines = []
196
+ lines.append("Resource Utilization:")
197
+
198
+ # Calculate totals
199
+ total_cpus_allocated = sum(
200
+ len(instance.resources.cpus) for instance in tree.instances.values()
201
+ )
202
+ total_memory_allocated = sum(
203
+ instance.resources.memory_bytes for instance in tree.instances.values()
204
+ )
205
+
206
+ available_cpus = len(tree.hardware.cpus.available)
207
+ memory_pool_bytes = tree.hardware.memory.memory_pool_bytes
208
+
209
+ cpu_percent = (total_cpus_allocated / available_cpus) * 100
210
+ memory_percent = (total_memory_allocated / memory_pool_bytes) * 100
211
+
212
+ lines.append(
213
+ f" CPUs: {total_cpus_allocated}/{available_cpus} allocated ({cpu_percent:.0f}%), {available_cpus - total_cpus_allocated} free"
214
+ )
215
+
216
+ memory_gb_allocated = total_memory_allocated / (1024**3)
217
+ memory_gb_total = memory_pool_bytes / (1024**3)
218
+ lines.append(
219
+ f" Memory: {memory_gb_allocated:.0f}/{memory_gb_total:.0f} GB allocated ({memory_percent:.0f}%), {memory_gb_total - memory_gb_allocated:.0f} GB free"
220
+ )
221
+
222
+ # Device utilization
223
+ total_devices = len(tree.hardware.devices)
224
+ allocated_devices = sum(
225
+ len(instance.resources.devices) for instance in tree.instances.values()
226
+ )
227
+
228
+ # Calculate device-specific utilization
229
+ network_devices = sum(
230
+ 1 for device in tree.hardware.devices.values() if "ethernet" in device.compatible
231
+ )
232
+ storage_devices = sum(
233
+ 1 for device in tree.hardware.devices.values() if "nvme" in device.compatible
234
+ )
235
+
236
+ allocated_network = sum(
237
+ 1
238
+ for instance in tree.instances.values()
239
+ for device_ref in instance.resources.devices
240
+ if "_vf" in device_ref
241
+ )
242
+ allocated_storage = sum(
243
+ 1
244
+ for instance in tree.instances.values()
245
+ for device_ref in instance.resources.devices
246
+ if "_ns" in device_ref
247
+ )
248
+
249
+ if network_devices > 0:
250
+ lines.append(f" Network: {allocated_network}/{network_devices} VFs allocated")
251
+ if storage_devices > 0:
252
+ lines.append(f" Storage: {allocated_storage}/{storage_devices} namespaces allocated")
253
+
254
+ lines.append(f" Devices: {allocated_devices}/{total_devices} allocated")
255
+
256
+ return lines
257
+
258
+ def generate_json_report(
259
+ self, result: ValidationResult, tree: GlobalDeviceTree
260
+ ) -> Dict[str, Any]:
261
+ """Generate JSON format report."""
262
+ return {
263
+ "status": "valid" if result.is_valid else "invalid",
264
+ "errors": result.errors,
265
+ "warnings": result.warnings,
266
+ "suggestions": result.suggestions,
267
+ "hardware": {
268
+ "cpus": {
269
+ "total": tree.hardware.cpus.total,
270
+ "host_reserved": tree.hardware.cpus.host_reserved,
271
+ "available": tree.hardware.cpus.available,
272
+ },
273
+ "memory": {
274
+ "total_bytes": tree.hardware.memory.total_bytes,
275
+ "host_reserved_bytes": tree.hardware.memory.host_reserved_bytes,
276
+ "memory_pool_base": tree.hardware.memory.memory_pool_base,
277
+ "memory_pool_bytes": tree.hardware.memory.memory_pool_bytes,
278
+ },
279
+ "devices": {
280
+ name: {
281
+ "compatible": device.compatible,
282
+ "pci_id": device.pci_id,
283
+ "sriov_vfs": device.sriov_vfs,
284
+ "namespaces": device.namespaces,
285
+ }
286
+ for name, device in tree.hardware.devices.items()
287
+ },
288
+ },
289
+ "instances": {
290
+ name: {
291
+ "id": instance.id if instance.id is not None else "auto-assigned",
292
+ "resources": {
293
+ "cpus": instance.resources.cpus,
294
+ "memory_base": instance.resources.memory_base,
295
+ "memory_bytes": instance.resources.memory_bytes,
296
+ "devices": instance.resources.devices,
297
+ },
298
+ }
299
+ for name, instance in tree.instances.items()
300
+ },
301
+ }
302
+
303
+ def generate_yaml_report(self, result: ValidationResult, tree: GlobalDeviceTree) -> str:
304
+ """Generate YAML format report."""
305
+ import yaml
306
+
307
+ data = self.generate_json_report(result, tree)
308
+ return yaml.dump(data, default_flow_style=False)