pylibfdt: add FdtRo.get_path()
PATCH v2 1/1 pylibfdt: add FdtRo.get_path()
Add a new Python method wrapping fdt_get_path() from the C API.
Also add a test for the new method.
---
Changes in v2:
- dynamically increase size of string buffer until it fits. The values are chosen relatively arbitrary, and terminates at lengths over than 4096 as I don't expect paths to be longer and there should be an exit condition to not eat up RAM in case of some bug.
pylibfdt/libfdt.i | 27 +++++++++++++++++++++++++++ tests/pylibfdt_tests.py | 7 +++++++ 2 files changed, 34 insertions(+)
pylibfdt/libfdt.i +27 -0
@@ -443,6 +443,28 @@ class FdtRo(object): """ return fdt_get_alias(self._fdt, name) + def get_path(self, nodeoffset): + """Get the full path of a node + + Args: + nodeoffset: Node offset to check + + Returns: + Full path to the node + + Raises: + FdtException if the path is longer than 'size' or other error occurs + """ + size = 64 + while size < 4096: + ret, path = fdt_get_path(self._fdt, nodeoffset, size) + if ret == -NOSPACE: + size += 64 + continue + check_err(ret) + return path + raise ValueError('Node path is unexpectedly long') + def parent_offset(self, nodeoffset, quiet=()): """Get the offset of a node's parent
@@ -1115,6 +1137,11 @@ typedef uint32_t fdt32_t; } } +%include "cstring.i" + +%cstring_output_maxsize(char *buf, int buflen); +int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen); + /* We have both struct fdt_property and a function fdt_property() */ %warnfilter(302) fdt_property;
tests/pylibfdt_tests.py +7 -0
@@ -348,6 +348,13 @@ class PyLibfdtBasicTests(unittest.TestCase): self.assertEqual("/subnode@1/subsubnode", self.fdt3.get_alias('ss1')) self.assertEqual("/subnode@1/subsubnode/subsubsubnode", self.fdt3.get_alias('sss1')) + def testGetPath(self): + """Test for the get_path() method""" + node = self.fdt.path_offset('/subnode@1') + node2 = self.fdt.path_offset('/subnode@1/subsubnode') + self.assertEqual("/subnode@1", self.fdt.get_path(node)) + self.assertEqual("/subnode@1/subsubnode", self.fdt.get_path(node2)) + def testParentOffset(self): """Test for the parent_offset() method""" self.assertEqual(-libfdt.NOTFOUND,
Signature · 1 line
2.34.1
A message that is not in the archive (sent off-list or not yet received): 20220122105653.31219-1-luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org
Hi Luca,
Quoted 36 lines · Luca Weiss
Add a new Python method wrapping
fdt_get_path()from the C API.Also add a test for the new method.
Signed-off-by Luca Weiss <[email protected]>---
Changes in v2:
- dynamically increase size of string buffer until it fits. The values are chosen relatively arbitrary, and terminates at lengths over than 4096 as I don't expect paths to be longer and there should be an exit condition to not eat up RAM in case of some bug.pylibfdt/libfdt.i | 27 +++++++++++++++++++++++++++ tests/pylibfdt_tests.py | 7 +++++++ 2 files changed, 34 insertions(+)pylibfdt/libfdt.i +11
-0@@ -443,6 +443,28 @@ class FdtRo(object): """ return fdt_get_alias(self._fdt, name) + def get_path(self, nodeoffset): + """Get the full path of a node + + Args: + nodeoffset: Node offset to check + + Returns: + Full path to the node + + Raises: + FdtException if the path is longer than 'size' or other error occurs
pylibfdt/libfdt.i class FdtRo(object): · line 466
Quoted 10 lines
pylibfdt/libfdt.i +10
-0+ """ + size = 64 + while size < 4096: + ret, path = fdt_get_path(self._fdt, nodeoffset, size) + if ret == -NOSPACE: + size += 64 + continue + check_err(ret) + return path + raise ValueError('Node path is unexpectedly long')
Do you think it would hurt to go with 4096 immediately? Python is not the most efficient language so it should not matter. You can also copy it to another object of the correct size, perhaps, to avoid memory wastage.
Also can you please explain how swig works here? I'm a bit lost as to the magic for calling fdt_get_path().
Quoted 35 lines
pylibfdt/libfdt.i +6
-0+ def parent_offset(self, nodeoffset, quiet=()): """Get the offset of a node's parent@@ -1115,6 +1137,11 @@ typedef uint32_t fdt32_t; } } +%include "cstring.i" + +%cstring_output_maxsize(char *buf, int buflen); +int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen); + /* We have both struct fdt_property and a function fdt_property() */ %warnfilter(302) fdt_property;tests/pylibfdt_tests.py +7
-1@@ -348,6 +348,13 @@ class PyLibfdtBasicTests(unittest.TestCase): self.assertEqual("/subnode@1/subsubnode", self.fdt3.get_alias('ss1')) self.assertEqual("/subnode@1/subsubnode/subsubsubnode", self.fdt3.get_alias('sss1')) + def testGetPath(self): + """Test for the get_path() method""" + node = self.fdt.path_offset('/subnode@1') + node2 = self.fdt.path_offset('/subnode@1/subsubnode') + self.assertEqual("/subnode@1", self.fdt.get_path(node)) + self.assertEqual("/subnode@1/subsubnode", self.fdt.get_path(node2)) + def testParentOffset(self): """Test for the parent_offset() method""" self.assertEqual(-libfdt.NOTFOUND, --
Quoted 2 lines
2.34.1
Regards,
Simon
A message that is not in the archive (sent off-list or not yet received): CAPnjgZ2Zj7gaT8G6WrVCKunVZgRMgef-rQLRhSRk9siR-f69DQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Hi Simon,
Quoted 47 lines · On Montag, 24. Jänner 2022 18:57:47 CET Simon Glass wrote:
Hi Luca,
Quoted 44 lines · Luca Weiss
Add a new Python method wrapping
fdt_get_path()from the C API.Also add a test for the new method.
Signed-off-by Luca Weiss <[email protected]>---
Changes in v2:
- dynamically increase size of string buffer until it fits.The values are chosen relatively arbitrary, and terminates at lengths over than 4096 as I don't expect paths to be longer and there should be an exit condition to not eat up RAM in case of some bug.
pylibfdt/libfdt.i | 27 +++++++++++++++++++++++++++ tests/pylibfdt_tests.py | 7 +++++++ 2 files changed, 34 insertions(+)pylibfdt/libfdt.i +11
-0@@ -443,6 +443,28 @@ class FdtRo(object): """ return fdt_get_alias(self._fdt, name) + def get_path(self, nodeoffset): + """Get the full path of a node + + Args: + nodeoffset: Node offset to check + + Returns: + Full path to the node + + Raises: + FdtException if the path is longer than 'size' or other erroroccurs + """
pylibfdt/libfdt.i +6
-0+ size = 64 + while size < 4096: + ret, path = fdt_get_path(self._fdt, nodeoffset, size) + if ret == -NOSPACE: + size += 64 + continue
Quoted 8 lines
pylibfdt/libfdt.i class FdtRo(object): · line 466
Quoted 3 lines
pylibfdt/libfdt.i +3
-0+ check_err(ret) + return path + raise ValueError('Node path is unexpectedly long')Do you think it would hurt to go with 4096 immediately? Python is not the most efficient language so it should not matter. You can also copy it to another object of the correct size, perhaps, to avoid memory wastage.
The returned string won't be of $size but of the actual string size, swig doc mentions of the buffer size being used to dynamically allocate memory on heap but the result will be returned as string.
Probably it's not a problem to start with a bigger size, but maybe 4096 is a bit overkill for a normal use case? Paths (at least in Linux dts files) are normally relatively short so 512 or 1024 feels better for this.
Which values do you think are appropriate here? I made this loop because of David's reply to v1:
https://www.spinics.net/lists/devicetree-compiler/msg03857.html
Quoted 2 lines
Also can you please explain how swig works here? I'm a bit lost as to the magic for calling
fdt_get_path().
It's quite well described in the doc (ctrl-f for "cstring_output_maxsize")
http://www.swig.org/Doc4.0/SWIGDocumentation.html
I don't think I've worked with swig before so I found this on stackoverflow or somewhere, but this function definitely seems to be made for this use case.
As far as I understand it, it just allocates a buffer of x bytes, does the C call with the temporary buffer and then converts the result into a proper Python object and discards the buffer.
Regards
Luca
Quoted 35 lines · Luca Weiss
Add a new Python method wrapping
fdt_get_path()from the C API.Also add a test for the new method.
Signed-off-by Luca Weiss <[email protected]>---
Changes in v2:
- dynamically increase size of string buffer until it fits. The values are chosen relatively arbitrary, and terminates at lengths over than 4096 as I don't expect paths to be longer and there should be an exit condition to not eat up RAM in case of some bug.pylibfdt/libfdt.i | 27 +++++++++++++++++++++++++++ tests/pylibfdt_tests.py | 7 +++++++ 2 files changed, 34 insertions(+)pylibfdt/libfdt.i +11
-0@@ -443,6 +443,28 @@ class FdtRo(object): """ return fdt_get_alias(self._fdt, name) + def get_path(self, nodeoffset): + """Get the full path of a node + + Args: + nodeoffset: Node offset to check + + Returns: + Full path to the node + + Raises: + FdtException if the path is longer than 'size' or other error occurs
pylibfdt/libfdt.i class FdtRo(object): · line 466
Quoted 10 lines
pylibfdt/libfdt.i +10
-0+ """ + size = 64 + while size < 4096: + ret, path = fdt_get_path(self._fdt, nodeoffset, size) + if ret == -NOSPACE: + size += 64 + continue + check_err(ret) + return path + raise ValueError('Node path is unexpectedly long')
Ah... sorry, this isn't quite what I had in mind. The idea of resizing the buffer is to avoid having an arbitrary limit, but you still have the 4096 byte arbitrary limit here. As Simon suggests, 4k would probably be a good *starting* point, rather than an ending point. Not only are repeated calls expensive in Python, but fdt_get_path() itself is also quite expensive, since it needs to scan the dtb from the start. I'd also suggest doubling the buffer size on each attempt, rather than increasing linearly.
Quoted 33 lines
pylibfdt/libfdt.i +5
-0def parent_offset(self, nodeoffset, quiet=()): """Get the offset of a node's parent@@ -1115,6 +1137,11 @@ typedef uint32_t fdt32_t; } } +%include "cstring.i" + +%cstring_output_maxsize(char *buf, int buflen); +int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen); + /* We have both struct fdt_property and a function fdt_property() */ %warnfilter(302) fdt_property;tests/pylibfdt_tests.py +7
-0@@ -348,6 +348,13 @@ class PyLibfdtBasicTests(unittest.TestCase): self.assertEqual("/subnode@1/subsubnode", self.fdt3.get_alias('ss1')) self.assertEqual("/subnode@1/subsubnode/subsubsubnode", self.fdt3.get_alias('sss1')) + def testGetPath(self): + """Test for the get_path() method""" + node = self.fdt.path_offset('/subnode@1') + node2 = self.fdt.path_offset('/subnode@1/subsubnode') + self.assertEqual("/subnode@1", self.fdt.get_path(node)) + self.assertEqual("/subnode@1/subsubnode", self.fdt.get_path(node2)) + def testParentOffset(self): """Test for the parent_offset() method""" self.assertEqual(-libfdt.NOTFOUND,
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Hi David,
Quoted 49 lines · On Dienstag, 25. Jänner 2022 05:48:34 CET David Gibson wrote:
Quoted 38 lines · Luca Weiss
Add a new Python method wrapping
fdt_get_path()from the C API.Also add a test for the new method.
Signed-off-by Luca Weiss <[email protected]>---
Changes in v2:
- dynamically increase size of string buffer until it fits.The values are chosen relatively arbitrary, and terminates at lengths over than 4096 as I don't expect paths to be longer and there should be an exit condition to not eat up RAM in case of some bug.
pylibfdt/libfdt.i | 27 +++++++++++++++++++++++++++ tests/pylibfdt_tests.py | 7 +++++++ 2 files changed, 34 insertions(+)pylibfdt/libfdt.i +11
-0@@ -443,6 +443,28 @@ class FdtRo(object): """ return fdt_get_alias(self._fdt, name) + def get_path(self, nodeoffset): + """Get the full path of a node + + Args: + nodeoffset: Node offset to check + + Returns: + Full path to the node + + Raises: + FdtException if the path is longer than 'size' or other erroroccurs + """
pylibfdt/libfdt.i class FdtRo(object): · line 466
Quoted 9 lines
pylibfdt/libfdt.i +9
-0+ size = 64 + while size < 4096: + ret, path = fdt_get_path(self._fdt, nodeoffset, size) + if ret == -NOSPACE: + size += 64 + continue + check_err(ret) + return path + raise ValueError('Node path is unexpectedly long')
Quoted 8 lines
Ah... sorry, this isn't quite what I had in mind. The idea of resizing the buffer is to avoid having an arbitrary limit, but you still have the 4096 byte arbitrary limit here. As Simon suggests, 4k would probably be a good *starting* point, rather than an ending point. Not only are repeated calls expensive in Python, but
fdt_get_path()itself is also quite expensive, since it needs to scan the dtb from the start. I'd also suggest doubling the buffer size on each attempt, rather than increasing linearly.
I do think that there should be a limit *somewhere* instead of hitting OOM if there would be some bug in the underlying implementation or the file provided? I'm fine with limiting it at 100MB or whatever but I would rather have a limit there than not have it.
Once this is clarified I'll make a v3 of this patch.
Regards
Luca
Quoted 40 lines
Quoted 40 lines
def
parent_offset(self, nodeoffset, quiet=()):
"""Get the offset of a node's parentpylibfdt/libfdt.i +5
-0@@ -1115,6 +1137,11 @@ typedef uint32_t fdt32_t; } } +%include "cstring.i" + +%cstring_output_maxsize(char *buf, int buflen); +int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen); +/* We have both struct
fdt_propertyand a functionfdt_property()*/ %warnfilter(302)fdt_property;tests/pylibfdt_tests.py +6
-0@@ -348,6 +348,13 @@ class PyLibfdtBasicTests(unittest.TestCase): self.assertEqual("/subnode@1/subsubnode", self.fdt3.get_alias('ss1')) self.assertEqual("/subnode@1/subsubnode/subsubsubnode", self.fdt3.get_alias('sss1'))> + def testGetPath(self): + """Test for the get_path() method""" + node = self.fdt.path_offset('/subnode@1') + node2 = self.fdt.path_offset('/subnode@1/subsubnode') + self.assertEqual("/subnode@1", self.fdt.get_path(node)) + self.assertEqual("/subnode@1/subsubnode",
self.fdt.get_path(node2)) +def testParentOffset(self):
"""Test for theparent_offset()method""" self.assertEqual(-libfdt.NOTFOUND,
Quoted 61 lines · Luca Weiss
Hi David,
Quoted 49 lines · On Dienstag, 25. Jänner 2022 05:48:34 CET David Gibson wrote:
Quoted 38 lines · Luca Weiss
Add a new Python method wrapping
fdt_get_path()from the C API.Also add a test for the new method.
Signed-off-by Luca Weiss <[email protected]>---
Changes in v2:
- dynamically increase size of string buffer until it fits.The values are chosen relatively arbitrary, and terminates at lengths over than 4096 as I don't expect paths to be longer and there should be an exit condition to not eat up RAM in case of some bug.
pylibfdt/libfdt.i | 27 +++++++++++++++++++++++++++ tests/pylibfdt_tests.py | 7 +++++++ 2 files changed, 34 insertions(+)pylibfdt/libfdt.i +11
-0@@ -443,6 +443,28 @@ class FdtRo(object): """ return fdt_get_alias(self._fdt, name) + def get_path(self, nodeoffset): + """Get the full path of a node + + Args: + nodeoffset: Node offset to check + + Returns: + Full path to the node + + Raises: + FdtException if the path is longer than 'size' or other erroroccurs + """
pylibfdt/libfdt.i class FdtRo(object): · line 466
Quoted 9 lines
pylibfdt/libfdt.i +9
-0+ size = 64 + while size < 4096: + ret, path = fdt_get_path(self._fdt, nodeoffset, size) + if ret == -NOSPACE: + size += 64 + continue + check_err(ret) + return path + raise ValueError('Node path is unexpectedly long')Quoted 8 lines
Ah... sorry, this isn't quite what I had in mind. The idea of resizing the buffer is to avoid having an arbitrary limit, but you still have the 4096 byte arbitrary limit here. As Simon suggests, 4k would probably be a good *starting* point, rather than an ending point. Not only are repeated calls expensive in Python, but
fdt_get_path()itself is also quite expensive, since it needs to scan the dtb from the start. I'd also suggest doubling the buffer size on each attempt, rather than increasing linearly.
Quoted 4 lines
I do think that there should be a limit *somewhere* instead of hitting OOM if there would be some bug in the underlying implementation or the file provided? I'm fine with limiting it at 100MB or whatever but I would rather have a limit there than not have it.
Well, it can never exceed the size of the whole dtb, structurally, so I don't think it's really a problem.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson