Skip to content

Commit 0b4c626

Browse files
complete dependency tree readiness resolution
1 parent e574156 commit 0b4c626

File tree

5 files changed

+45
-40
lines changed

5 files changed

+45
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ __pycache__/
99
.ipynb_checkpoints/
1010
vmlinux.py
1111
~*
12+
vmlinux.h

pythonbpf/vmlinux_parser/class_handler.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ def process_vmlinux_post_ast(
3333
symbols_in_module, imported_module = get_module_symbols("vmlinux")
3434

3535
current_symbol_name = elem_type_class.__name__
36+
logger.info(f"Begin {current_symbol_name} Processing")
3637
field_table = {}
3738
is_complex_type = False
3839
containing_type: Optional[Any] = None
3940
ctype_complex_type: Optional[Any] = None
4041
type_length: Optional[int] = None
4142
module_name = getattr(elem_type_class, "__module__", None)
4243

43-
if current_symbol_name in processing_stack:
44-
logger.info(f"Circular dependency detected for {current_symbol_name}, skipping")
45-
return True
46-
4744
# Check if already processed
4845
if handler.has_node(current_symbol_name):
49-
logger.info(f"Node {current_symbol_name} already processed and ready")
46+
logger.debug(f"Node {current_symbol_name} already processed and ready")
47+
return True
48+
49+
# XXX:Check it's use. It's probably not being used.
50+
if current_symbol_name in processing_stack:
51+
logger.debug(f"Dependency already in processing stack for {current_symbol_name}, skipping")
5052
return True
5153

5254
processing_stack.add(current_symbol_name)
@@ -87,13 +89,12 @@ def process_vmlinux_post_ast(
8789
elem_name, elem_temp_list = elem
8890
[elem_type, elem_bitfield_size] = elem_temp_list
8991
local_module_name = getattr(elem_type, "__module__", None)
92+
new_dep_node.add_field(elem_name, elem_type, ready=False)
9093
if local_module_name == ctypes.__name__:
91-
new_dep_node.add_field(elem_name, elem_type, ready=False)
9294
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
9395
new_dep_node.set_field_ready(elem_name, is_ready=True)
94-
logger.info(f"Field {elem_name} is direct ctypes type: {elem_type}")
96+
logger.debug(f"Field {elem_name} is direct ctypes type: {elem_type}")
9597
elif local_module_name == "vmlinux":
96-
new_dep_node.add_field(elem_name, elem_type, ready=False)
9798
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
9899
logger.debug(
99100
f"Processing vmlinux field: {elem_name}, type: {elem_type}"
@@ -103,6 +104,7 @@ def process_vmlinux_post_ast(
103104
containing_type = elem_type._type_
104105
if hasattr(elem_type, "_length_") and is_complex_type:
105106
type_length = elem_type._length_
107+
106108
if containing_type.__module__ == "vmlinux":
107109
pass
108110
elif containing_type.__module__ == ctypes.__name__:
@@ -117,7 +119,7 @@ def process_vmlinux_post_ast(
117119
raise ImportError(
118120
f"Unsupported module of {containing_type}"
119121
)
120-
logger.info(
122+
logger.debug(
121123
f"{containing_type} containing type of parent {elem_name} with {elem_type} and ctype {ctype_complex_type} and length {type_length}"
122124
)
123125
new_dep_node.set_field_containing_type(
@@ -129,20 +131,17 @@ def process_vmlinux_post_ast(
129131
)
130132
new_dep_node.set_field_type(elem_name, elem_type)
131133
if containing_type.__module__ == "vmlinux":
132-
if process_vmlinux_post_ast(
133-
containing_type, llvm_handler, handler, processing_stack
134-
):
135-
new_dep_node.set_field_ready(elem_name, True)
134+
process_vmlinux_post_ast(containing_type, llvm_handler, handler, processing_stack)
135+
new_dep_node.set_field_ready(elem_name, True)
136136
elif containing_type.__module__ == ctypes.__name__:
137-
logger.info(f"Processing ctype internal{containing_type}")
137+
logger.debug(f"Processing ctype internal{containing_type}")
138+
new_dep_node.set_field_ready(elem_name, True)
138139
else:
139140
raise TypeError(
140141
"Module not supported in recursive resolution"
141142
)
142-
continue
143-
if process_vmlinux_post_ast(
144-
elem_type, llvm_handler, handler, processing_stack
145-
):
143+
else:
144+
process_vmlinux_post_ast(elem_type, llvm_handler, handler, processing_stack)
146145
new_dep_node.set_field_ready(elem_name, True)
147146
else:
148147
raise ValueError(
@@ -152,5 +151,5 @@ def process_vmlinux_post_ast(
152151
else:
153152
raise ImportError("UNSUPPORTED Module")
154153

155-
print(current_symbol_name, "DONE")
156-
print(f"handler readiness {handler.is_ready}")
154+
logging.info(f"{current_symbol_name} processed and handler readiness {handler.is_ready}")
155+
return True

pythonbpf/vmlinux_parser/dependency_node.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,41 @@ def set_ready(self, is_ready: bool = True) -> None:
2020
"""Set the readiness state of this field."""
2121
self.ready = is_ready
2222

23-
def set_value(self, value: Any, mark_ready: bool = True) -> None:
23+
def set_value(self, value: Any, mark_ready: bool = False) -> None:
2424
"""Set the value of this field and optionally mark it as ready."""
2525
self.value = value
2626
if mark_ready:
2727
self.ready = True
2828

29-
def set_type(self, given_type, mark_ready: bool = True) -> None:
29+
def set_type(self, given_type, mark_ready: bool = False) -> None:
3030
"""Set value of the type field and mark as ready"""
3131
self.type = given_type
3232
if mark_ready:
3333
self.ready = True
3434

3535
def set_containing_type(
36-
self, containing_type: Optional[Any], mark_ready: bool = True
36+
self, containing_type: Optional[Any], mark_ready: bool = False
3737
) -> None:
3838
"""Set the containing_type of this field and optionally mark it as ready."""
3939
self.containing_type = containing_type
4040
if mark_ready:
4141
self.ready = True
4242

43-
def set_type_size(self, type_size: Any, mark_ready: bool = True) -> None:
43+
def set_type_size(self, type_size: Any, mark_ready: bool = False) -> None:
4444
"""Set the type_size of this field and optionally mark it as ready."""
4545
self.type_size = type_size
4646
if mark_ready:
4747
self.ready = True
4848

4949
def set_ctype_complex_type(
50-
self, ctype_complex_type: Any, mark_ready: bool = True
50+
self, ctype_complex_type: Any, mark_ready: bool = False
5151
) -> None:
5252
"""Set the ctype_complex_type of this field and optionally mark it as ready."""
5353
self.ctype_complex_type = ctype_complex_type
5454
if mark_ready:
5555
self.ready = True
5656

57-
def set_bitfield_size(self, bitfield_size: Any, mark_ready: bool = True) -> None:
57+
def set_bitfield_size(self, bitfield_size: Any, mark_ready: bool = False) -> None:
5858
"""Set the bitfield_size of this field and optionally mark it as ready."""
5959
self.bitfield_size = bitfield_size
6060
if mark_ready:
@@ -138,7 +138,7 @@ def get_field(self, name: str) -> Field:
138138
"""Get a field by name."""
139139
return self.fields[name]
140140

141-
def set_field_value(self, name: str, value: Any, mark_ready: bool = True) -> None:
141+
def set_field_value(self, name: str, value: Any, mark_ready: bool = False) -> None:
142142
"""Set a field's value and optionally mark it as ready."""
143143
if name not in self.fields:
144144
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
@@ -147,7 +147,7 @@ def set_field_value(self, name: str, value: Any, mark_ready: bool = True) -> Non
147147
# Invalidate readiness cache
148148
self._ready_cache = None
149149

150-
def set_field_type(self, name: str, type: Any, mark_ready: bool = True) -> None:
150+
def set_field_type(self, name: str, type: Any, mark_ready: bool = False) -> None:
151151
"""Set a field's type and optionally mark it as ready."""
152152
if name not in self.fields:
153153
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
@@ -157,7 +157,7 @@ def set_field_type(self, name: str, type: Any, mark_ready: bool = True) -> None:
157157
self._ready_cache = None
158158

159159
def set_field_containing_type(
160-
self, name: str, containing_type: Any, mark_ready: bool = True
160+
self, name: str, containing_type: Any, mark_ready: bool = False
161161
) -> None:
162162
"""Set a field's containing_type and optionally mark it as ready."""
163163
if name not in self.fields:
@@ -168,7 +168,7 @@ def set_field_containing_type(
168168
self._ready_cache = None
169169

170170
def set_field_type_size(
171-
self, name: str, type_size: Any, mark_ready: bool = True
171+
self, name: str, type_size: Any, mark_ready: bool = False
172172
) -> None:
173173
"""Set a field's type_size and optionally mark it as ready."""
174174
if name not in self.fields:
@@ -179,7 +179,7 @@ def set_field_type_size(
179179
self._ready_cache = None
180180

181181
def set_field_ctype_complex_type(
182-
self, name: str, ctype_complex_type: Any, mark_ready: bool = True
182+
self, name: str, ctype_complex_type: Any, mark_ready: bool = False
183183
) -> None:
184184
"""Set a field's ctype_complex_type and optionally mark it as ready."""
185185
if name not in self.fields:
@@ -190,7 +190,7 @@ def set_field_ctype_complex_type(
190190
self._ready_cache = None
191191

192192
def set_field_bitfield_size(
193-
self, name: str, bitfield_size: Any, mark_ready: bool = True
193+
self, name: str, bitfield_size: Any, mark_ready: bool = False
194194
) -> None:
195195
"""Set a field's bitfield_size and optionally mark it as ready."""
196196
if name not in self.fields:
@@ -200,7 +200,7 @@ def set_field_bitfield_size(
200200
# Invalidate readiness cache
201201
self._ready_cache = None
202202

203-
def set_field_ready(self, name: str, is_ready: bool = True) -> None:
203+
def set_field_ready(self, name: str, is_ready: bool = False) -> None:
204204
"""Mark a field as ready or not ready."""
205205
if name not in self.fields:
206206
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
@@ -218,8 +218,8 @@ def is_ready(self) -> bool:
218218

219219
# Calculate readiness only when needed
220220
if not self.fields:
221-
self._ready_cache = False
222-
return False
221+
self._ready_cache = True
222+
return True
223223

224224
self._ready_cache = all(elem.ready for elem in self.fields.values())
225225
return self._ready_cache
@@ -231,3 +231,7 @@ def get_field_values(self) -> Dict[str, Any]:
231231
def get_ready_fields(self) -> Dict[str, Field]:
232232
"""Get all fields that are marked as ready."""
233233
return {name: elem for name, elem in self.fields.items() if elem.ready}
234+
235+
def get_not_ready_fields(self) -> Dict[str, Field]:
236+
"""Get all fields that are marked as not ready."""
237+
return {name: elem for name, elem in self.fields.items() if not elem.ready}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# here, we will iterate through the dependencies and generate IR once dependencies are resolved fully
1+
import logging
22
from .dependency_handler import DependencyHandler
33

4+
logger = logging.getLogger(__name__)
45

56
class IRGenerator:
6-
def __init__(self, module, handler):
7+
def __init__(self, module, handler: DependencyHandler):
78
self.module = module
89
self.handler: DependencyHandler = handler
10+
if not handler.is_ready:
11+
raise ImportError("Semantic analysis of vmlinux imports failed. Cannot generate IR")

tests/failing_tests/xdp_pass.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
from pythonbpf.maps import HashMap
33
from pythonbpf.helper import XDP_PASS
44
from vmlinux import struct_xdp_md
5-
6-
from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
75
from vmlinux import struct_xdp_buff # noqa: F401
6+
from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
87

9-
# from vmlinux import struct_xdp_md
108
from ctypes import c_int64
119

1210
# Instructions to how to run this program

0 commit comments

Comments
 (0)