Skip to content

Commit

Permalink
Tweaks to diagnostics (#467)
Browse files Browse the repository at this point in the history
* Tweaks to diagnostics

* Update docs
  • Loading branch information
almarklein authored Feb 15, 2024
1 parent 2c39e7f commit 4c54ea9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ To inspect (for example) the total buffer usage:
:members:


.. autoclass:: wgpu._diagnostics.Diagnostics
.. autoclass:: wgpu.DiagnosticsBase
:members:


Expand Down
4 changes: 2 additions & 2 deletions tests/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from wgpu import _diagnostics
from wgpu._diagnostics import (
DiagnosticsRoot,
Diagnostics,
DiagnosticsBase,
ObjectTracker,
dict_to_text,
int_repr,
Expand All @@ -29,7 +29,7 @@ def __exit__(self, *args):
_diagnostics.diagnostics = wgpu.diagnostics


class CustomDiagnostics(Diagnostics):
class CustomDiagnostics(DiagnosticsBase):
def __init__(self, name):
super().__init__(name)
self.tracker = ObjectTracker()
Expand Down
2 changes: 1 addition & 1 deletion wgpu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from ._coreutils import logger # noqa: F401,F403
from ._diagnostics import diagnostics # noqa: F401,F403
from ._diagnostics import diagnostics, DiagnosticsBase # noqa: F401,F403
from .flags import * # noqa: F401,F403
from .enums import * # noqa: F401,F403
from .classes import * # noqa: F401,F403
Expand Down
52 changes: 28 additions & 24 deletions wgpu/_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,21 @@ def print_report(self):
print(self.get_report(), end="")


class Diagnostics:
class DiagnosticsBase:
"""Object that represents diagnostics on a specific topic.
This is a base class that must be subclassed to provide diagnostics
on a certain topic. Instantiating the class registers it with the
root diagnostics object.
This is a base class that must be subclassed to provide diagnostics on
a certain topic. Typically only ``get_dict()`` needs to be implemented.
Instantiating the class registers it with the root diagnostics object.
"""

def __init__(self, name):
diagnostics._register_diagnostics(name, self)
if not (isinstance(name, str) and name.isidentifier()):
raise ValueError(
"Diagnostics name must be an identifier (i.e. use underscore instead of spaces)."
)
self.name = name
self.object_counts = {}
diagnostics._register_diagnostics(name, self)

def __repr__(self):
return f"<Diagnostics for '{self.name}'>"
Expand Down Expand Up @@ -263,6 +266,8 @@ def dict_to_table(d, header, header_offest=0):
row.append("")
elif isinstance(val, str):
row.append(val)
elif isinstance(val, bool):
row.append("✓" if val else "-")
elif isinstance(val, int):
row.append(int_repr(val))
elif isinstance(val, float):
Expand All @@ -285,26 +290,25 @@ def dict_to_table(d, header, header_offest=0):
def int_repr(val):
"""Represent an integer using K and M suffixes."""
prefix = "-" if val < 0 else ""
suffix = ""
val = abs(val)
if val >= 1_000_000_000: # >= 1G
s = str(val / 1_000_000_000)
suffix = "G"
elif val >= 1_000_000: # >= 1M
s = str(val / 1_000_000)
suffix = "M"
elif val >= 1_000: # >= 1K
s = str(val / 1_000)
suffix = "K"
else:
s = str(val)
suffix = ""
if "." in s:
s1, _, s2 = s.partition(".")
s = str(val)
tail = 3 * ((len(s) - 1) // 3)
if tail > 0:
s1, s2 = s[:-tail], s[-tail:]
n_decimals = max(0, 3 - len(s1))
s = s1
if n_decimals:
s2 += "000"
s = s1 + "." + s2[:n_decimals]
if tail == 3:
suffix = "K"
elif tail == 6:
suffix = "M"
elif tail == 9:
suffix = "G"
else:
suffix = f"E{tail}"
return prefix + s + suffix


Expand Down Expand Up @@ -427,7 +431,7 @@ def int_repr(val):
diagnostics = DiagnosticsRoot()


class SystemDiagnostics(Diagnostics):
class SystemDiagnostics(DiagnosticsBase):
"""Provides basic system info."""

def get_dict(self):
Expand All @@ -439,7 +443,7 @@ def get_dict(self):
}


class WgpuNativeInfoDiagnostics(Diagnostics):
class WgpuNativeInfoDiagnostics(DiagnosticsBase):
"""Provides metadata about the wgpu-native backend."""

def get_dict(self):
Expand All @@ -463,7 +467,7 @@ def get_dict(self):
}


class VersionDiagnostics(Diagnostics):
class VersionDiagnostics(DiagnosticsBase):
"""Provides version numbers from relevant libraries."""

def get_dict(self):
Expand All @@ -485,7 +489,7 @@ def get_dict(self):
return info


class ObjectCountDiagnostics(Diagnostics):
class ObjectCountDiagnostics(DiagnosticsBase):
"""Provides object counts and resource consumption, used in _classes.py."""

def __init__(self, name):
Expand Down
4 changes: 2 additions & 2 deletions wgpu/backends/wgpu_native/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import ctypes

from ._ffi import ffi, lib
from ..._diagnostics import Diagnostics
from ..._diagnostics import DiagnosticsBase
from ...classes import (
GPUError,
GPUOutOfMemoryError,
Expand Down Expand Up @@ -357,7 +357,7 @@ def generate_report():
return report


class WgpuNativeCountsDiagnostics(Diagnostics):
class WgpuNativeCountsDiagnostics(DiagnosticsBase):
def get_subscript(self):
text = ""
text += " * The a, k, r, e are allocated, kept, released, and error, respectively.\n"
Expand Down

0 comments on commit 4c54ea9

Please sign in to comment.