Skip to content

Commit 19ebee7

Browse files
andy31415andreilitvinrestyled-commits
authored
Split out the analysis of sizes better: detect ember clusters and functions better, move openthread a bit (#37691)
* Split out ember implementations in a much more consistent manner * Relocate ember callbacks to be together with clusters * Some updates, tests do NOT pass yet * Things work, but anon namespace is awkward... * Better result without odd mangling * Also split ember data types (they are very large) into ember, place openthread functions into the ot C++ namespace * Restyled by autopep8 --------- Co-authored-by: Andrei Litvin <andreilitvin@google.com> Co-authored-by: Restyled.io <commits@restyled.io>
1 parent d618ccb commit 19ebee7

File tree

1 file changed

+106
-9
lines changed

1 file changed

+106
-9
lines changed

scripts/tools/file_size_from_nm.py

+106-9
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,35 @@ class Symbol:
9494
size: int
9595
tree_path: list[str]
9696

97+
# These expressions are callbacks defined in
98+
# callbacks.zapt
99+
# - void emberAf{{asUpperCamelCase label}}ClusterInitCallback(chip::EndpointId endpoint);
100+
# - void emberAf{{asUpperCamelCase label}}ClusterServerInitCallback(chip::EndpointId endpoint);
101+
# - void Matter{{asUpperCamelCase label}}ClusterServerShutdownCallback(chip::EndpointId endpoint);
102+
# - void emberAf{{asUpperCamelCase label}}ClusterClientInitCallback(chip::EndpointId endpoint);
103+
# - void Matter{{asUpperCamelCase label}}ClusterServerAttributeChangedCallback(const chip::app::ConcreteAttributePath & attributePath);
104+
# - chip::Protocols::InteractionModel::Status Matter{{asUpperCamelCase label}}ClusterServerPreAttributeChangedCallback(const chip::app::ConcreteAttributePath & attributePath, EmberAfAttributeType attributeType, uint16_t size, uint8_t * value);
105+
# - void emberAf{{asUpperCamelCase label}}ClusterServerTickCallback(chip::EndpointId endpoint);
106+
#
107+
# and for commands:
108+
# - bool emberAf{{asUpperCamelCase parent.label}}Cluster{{asUpperCamelCase name}}Callback
109+
110+
111+
_CLUSTER_EXPRESSIONS = [
112+
re.compile(r'emberAf(?P<cluster>.+)ClusterClientInitCallback\('),
113+
re.compile(r'emberAf(?P<cluster>.+)ClusterInitCallback\('),
114+
re.compile(r'emberAf(?P<cluster>.+)ClusterServerInitCallback\('),
115+
116+
117+
118+
re.compile(r'emberAf(?P<cluster>.+)ClusterServerTickCallback\('),
119+
re.compile(r'Matter(?P<cluster>.+)ClusterServerAttributeChangedCallback\('),
120+
re.compile(r'Matter(?P<cluster>.+)ClusterServerPreAttributeChangedCallback\('),
121+
re.compile(r'Matter(?P<cluster>.+)ClusterServerShutdownCallback\('),
122+
# commands
123+
re.compile(r'emberAf(?P<cluster>.+)Cluster(?P<command>.+)Callback\('),
124+
]
125+
97126

98127
def tree_display_name(name: str) -> list[str]:
99128
"""
@@ -110,11 +139,61 @@ def tree_display_name(name: str) -> list[str]:
110139
if name.startswith("vtable for "):
111140
name = name[11:]
112141

113-
# These are C-style methods really, we have no top-level namespaces named
114-
# like this but still want to see these differently
115-
for special_prefix in {"emberAf", "Matter"}:
116-
if name.startswith(special_prefix):
117-
return [special_prefix, name]
142+
# Known variables for ember:
143+
for variable_name in ['::generatedAttributes', '::generatedClusters', '::generatedEmberAfEndpointTypes', '::fixedDeviceTypeList', '::generatedCommands']:
144+
if variable_name in name:
145+
return ["EMBER", "METADATA", name]
146+
147+
# to abvoid treating '(anonymous namespace)::' as special because of space,
148+
# replace it with something that looks similar
149+
name = name.replace('(anonymous namespace)::', 'ANONYMOUS_NAMESPACE::')
150+
151+
# Ember methods are generally c-style that are in a particular format:
152+
# - emAf* are INTERNAL ember functions
153+
# - emberAf* are PUBLIC (from an ember perspective) functions
154+
# - Several callbacks:
155+
# - Matter<Cluster>ClusterServerInitCallback
156+
# - emberAf<Cluster>ClusterInitCallback
157+
# - Matter<Cluster>serverShutdownCallback
158+
# - MatterPreAttributeChangedCallback
159+
# - Matter<Cluster>PreAttributeChangedCallback
160+
# - emberAfPluginLevelControlCoupledColorTempChangeCallback
161+
# The code below splits the above an "ember" namespace
162+
163+
# First consider the cluster functions.
164+
# These are technically ember, however place them in `::chip::app::Clusters::<Cluster>::`
165+
# so that they are grouped with AAI/CHI
166+
for expr in _CLUSTER_EXPRESSIONS:
167+
m = expr.match(name)
168+
if not m:
169+
continue
170+
d = m.groupdict()
171+
logging.debug("Ember callback found: %s -> %r", name, d)
172+
if 'command' in d:
173+
return ["chip", "app", "Clusters", d['cluster'], "EMBER", d['command'], name]
174+
else:
175+
return ["chip", "app", "Clusters", d['cluster'], "EMBER", name]
176+
177+
if 'MatterPreAttributeChangeCallback' in name:
178+
return ["EMBER", "CALLBACKS", name]
179+
180+
if name.startswith("emberAfPlugin"):
181+
# these methods are callbacks defined by some clusters to call into application code.
182+
# They look like:
183+
# - emberAfPluginTimeFormatLocalizationOnCalendarTypeChange
184+
# - emberAfPluginOnOffClusterServerPostInitCallback
185+
# - emberAfPluginDoorLockGetFaceCredentialLengthConstraints
186+
# - emberAfPluginDoorLockOnOperatingModeChange
187+
# - emberAfPluginColorControlServerXyTransitionEventHandler
188+
#
189+
# They are generally quite free form and seem to be used instead of "delegates" as
190+
# application hook points.
191+
return ["EMBER", "CALLBACKS", "PLUGIN", name]
192+
193+
# We also capture '(anonymous namespace)::emAfWriteAttribute or similar
194+
if name.startswith("emAf") or name.startswith("emberAf") or ("::emAf" in name) or ('::emberAf' in name):
195+
# Place this as ::EMBER::API (these are internal and public functions from ember)
196+
return ["EMBER", "API", name]
118197

119198
# If the first element contains a space, it is either within `<>` for templates or it means it is a
120199
# separator of the type. Try to find the type separator
@@ -194,18 +273,32 @@ def tree_display_name(name: str) -> list[str]:
194273

195274
if len(result) == 1:
196275
if result[0].startswith("ot"): # Show openthread methods a bit grouped
197-
result = ["ot"] + result
276+
return ["ot", "C"] + result
198277
return ["C"] + result
199278

200-
return result
279+
return [r.replace('ANONYMOUS_NAMESPACE', '(anonymous namespace)') for r in result]
201280

202281

203282
# TO run the test, install pytest and do
204283
# pytest file_size_from_nm.py
205284
def test_tree_display_name():
206285
assert tree_display_name("fooBar") == ["C", "fooBar"]
207-
assert tree_display_name("emberAfTest") == ["emberAf", "emberAfTest"]
208-
assert tree_display_name("MatterSomeCall") == ["Matter", "MatterSomeCall"]
286+
assert tree_display_name("emberAfTest") == ["EMBER", "API", "emberAfTest"]
287+
assert tree_display_name("MatterSomeCall") == ["C", "MatterSomeCall"]
288+
289+
assert tree_display_name("emberAfFooBarClusterServerInitCallback()") == [
290+
"chip", "app", "Clusters", "FooBar", "EMBER", "emberAfFooBarClusterServerInitCallback()"
291+
]
292+
assert tree_display_name("emberAfFooBarClusterInitCallback()") == [
293+
"chip", "app", "Clusters", "FooBar", "EMBER", "emberAfFooBarClusterInitCallback()"
294+
]
295+
assert tree_display_name("MatterFooBarClusterServerShutdownCallback()") == [
296+
"chip", "app", "Clusters", "FooBar", "EMBER", "MatterFooBarClusterServerShutdownCallback()"
297+
]
298+
assert tree_display_name("emberAfFooBarClusterSomeCommandCallback()") == [
299+
"chip", "app", "Clusters", "FooBar", "EMBER", "SomeCommand", "emberAfFooBarClusterSomeCommandCallback()"
300+
]
301+
209302
assert tree_display_name("chip::Some::Constructor()") == [
210303
"chip",
211304
"Some",
@@ -261,6 +354,10 @@ def test_tree_display_name():
261354
"void foo::bar<baz>::method(my::arg name, other::arg::type)"
262355
) == ["foo", "bar<baz>", "void method(my::arg name, other::arg::type)"]
263356

357+
assert tree_display_name(
358+
"(anonymous namespace)::AccessControlAttribute::Read(args)"
359+
) == ["(anonymous namespace)", "AccessControlAttribute", "Read(args)"]
360+
264361

265362
def build_treemap(
266363
name: str,

0 commit comments

Comments
 (0)