diff --git a/src/platform/Darwin/BUILD.gn b/src/platform/Darwin/BUILD.gn index 11372cb5f4c627..fea1fb39c8da41 100644 --- a/src/platform/Darwin/BUILD.gn +++ b/src/platform/Darwin/BUILD.gn @@ -97,6 +97,7 @@ static_library("Darwin") { deps = [ ":logging", + ":tracing", "${chip_root}/src/lib/dnssd:platform_header", "${chip_root}/src/platform/logging:headers", "${chip_root}/src/setup_payload", @@ -131,6 +132,18 @@ static_library("Darwin") { } } +source_set("tracing") { + sources = [ + "Tracing.mm", + "Tracing.h", + ] + + deps = [ + ":logging", + "${chip_root}/src/tracing", + ] +} + static_library("logging") { sources = [ "Logging.h", diff --git a/src/platform/Darwin/Tracing.h b/src/platform/Darwin/Tracing.h new file mode 100644 index 00000000000000..baf01bd8d486ec --- /dev/null +++ b/src/platform/Darwin/Tracing.h @@ -0,0 +1,64 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +#define _MATTER_TRACE_DISABLE(...) \ + do \ + { \ + } while (false) + +#define MATTER_TRACE_BEGIN(label,group) os_signpost_interval_begin(_DARWIN_MATTER_SIGNPOST_LOGGER, OS_SIGNPOST_ID_EXCLUSIVE, group "-" label) +#define MATTER_TRACE_END(label,group) os_signpost_interval_end(_DARWIN_MATTER_SIGNPOST_LOGGER, OS_SIGNPOST_ID_EXCLUSIVE, group "-" label) +#define MATTER_TRACE_INSTANT(label,group) os_signpost_event_emit(_DARWIN_MATTER_SIGNPOST_LOGGER, OS_SIGNPOST_ID_EXCLUSIVE, group "-" label) + +#define MATTER_TRACE_COUNTER(label) \ + do \ + { \ + static unsigned int count##_label = 0; \ + os_signpost_event_emit(_DARWIN_MATTER_SIGNPOST_LOGGER, OS_SIGNPOST_ID_EXCLUSIVE, label, "%u", ++count##_label); \ + } while (0) + + +#define MATTER_SDK_SIGNPOST_NAME "com.csa.matter.signpost" +#define _DARWIN_MATTER_SIGNPOST_LOGGER chip::Tracing::signposts::GetMatterSignpostLogger() + +namespace chip { +namespace Tracing { +namespace signposts { + +os_log_t GetMatterSignpostLogger(); + +class Scoped +{ +public: + inline Scoped(const char * label, const char * group) : mLabel(label), mGroup(group) { os_signpost_interval_begin(_DARWIN_MATTER_SIGNPOST_LOGGER, OS_SIGNPOST_ID_EXCLUSIVE, MATTER_SDK_SIGNPOST_NAME, "%s-%s", group, label); } + inline ~Scoped() { os_signpost_interval_end(_DARWIN_MATTER_SIGNPOST_LOGGER, OS_SIGNPOST_ID_EXCLUSIVE, MATTER_SDK_SIGNPOST_NAME, "%s-%s", mGroup, mLabel); } + +private: + const char * mLabel; + const char * mGroup; +}; +} // namespace signposts +} // namespace Tracing +} // namespace chip +#define _CONCAT_IMPL(a, b) a##b +#define _MACRO_CONCAT(a, b) _CONCAT_IMPL(a, b) + +#define MATTER_TRACE_SCOPE(label, group) ::chip::Tracing::signposts::Scoped _MACRO_CONCAT(_trace_scope, __COUNTER__)(label, group) diff --git a/src/platform/Darwin/Tracing.mm b/src/platform/Darwin/Tracing.mm new file mode 100644 index 00000000000000..4da1b16f2605a7 --- /dev/null +++ b/src/platform/Darwin/Tracing.mm @@ -0,0 +1,35 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +namespace chip { +namespace Tracing { +namespace signposts { + os_log_t GetMatterSignpostLogger() + { + static dispatch_once_t onceToken; + static os_log_t logger; + dispatch_once(&onceToken, ^{ + logger = os_log_create("com.csa.matter.signposts", "com.csa.matter.sdk"); + }); + return logger; + } +} // namespace signposts +} // namespace Tracing +} // namespace chip + diff --git a/src/tracing/darwin/BUILD.gn b/src/tracing/darwin/BUILD.gn new file mode 100644 index 00000000000000..3bd9b7685b594e --- /dev/null +++ b/src/tracing/darwin/BUILD.gn @@ -0,0 +1,25 @@ +# Copyright (c) 2024 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build_overrides/build.gni") +import("//build_overrides/chip.gni") + +config("tracing") { + include_dirs = [ "include" ] +} + +source_set("darwin_tracing") { + public = [ "include/matter/tracing/macros_impl.h" ] + public_configs = [ ":tracing" ] +} diff --git a/src/tracing/darwin/include/matter/tracing/macros_impl.h b/src/tracing/darwin/include/matter/tracing/macros_impl.h new file mode 100644 index 00000000000000..36c79fa6133ec4 --- /dev/null +++ b/src/tracing/darwin/include/matter/tracing/macros_impl.h @@ -0,0 +1,27 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +/* Ensure we do not have double tracing macros defined */ +#if defined(MATTER_TRACE_BEGIN) +#error "Tracing macros seem to be double defined" +#endif + + +#include + diff --git a/src/tracing/tracing_args.gni b/src/tracing/tracing_args.gni index d3741b5eba5f10..d554a670aa251e 100644 --- a/src/tracing/tracing_args.gni +++ b/src/tracing/tracing_args.gni @@ -25,6 +25,11 @@ declare_args() { # backends explicitly matter_enable_tracing_support = current_os == "android" + if (chip_device_platform == "darwin") { + matter_enable_tracing_support = true + } + + # Defines the trace backend. Current matter tracing splits the logic # into two parts: # - data logging, well defined and using type-safe data @@ -46,6 +51,8 @@ declare_args() { # if (current_os == "linux" || current_os == "android") { matter_trace_config = "${chip_root}/src/tracing/perfetto:perfetto_tracing" + } else if (chip_device_platform == "darwin") { + matter_trace_config = "${chip_root}/src/tracing/darwin:darwin_tracing" } else { matter_trace_config = "${chip_root}/src/tracing/none" }