-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
--trace-to
for python and use it in repl tests (#28179
) * Start adding tracing start/stop functions * Add raii-like support for tracing * Add raii-like support for tracing * Fix compile logic * Switch linux, mac, android to C++17 by default * Start outputting trace data * Upload traces that are gathered * Fix names * Allow placeholders in script args too * Allow from-string tracing * Do not newline-separate restyle path otherwise only the first argument is processed * Restyle * Add some additional types * Minor python fixes * Import ctypes * Things run now * Add trace bits to our tests * Undo restyle-diff change * Fix some typos in naming * Add perfetto for darwin too * mobile-device-test.py does not suppor trace-to yet * Make mobile device test also be able to trace. Mobile device test seems super slow * Restyled by autopep8 * Restyled by isort --------- Co-authored-by: Andrei Litvin <[email protected]> Co-authored-by: Restyled.io <[email protected]>
- Loading branch information
Showing
9 changed files
with
331 additions
and
56 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* | ||
* Copyright (c) 2023 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 <controller/python/chip/native/PyChipError.h> | ||
#include <platform/PlatformManager.h> | ||
|
||
#include <tracing/json/json_tracing.h> | ||
#include <tracing/perfetto/event_storage.h> | ||
#include <tracing/perfetto/file_output.h> | ||
#include <tracing/perfetto/perfetto_tracing.h> | ||
#include <tracing/perfetto/simple_initialize.h> | ||
#include <tracing/registry.h> | ||
|
||
namespace { | ||
|
||
using chip::DeviceLayer::PlatformMgr; | ||
|
||
class ScopedStackLock | ||
{ | ||
public: | ||
ScopedStackLock() { PlatformMgr().LockChipStack(); } | ||
|
||
~ScopedStackLock() { PlatformMgr().UnlockChipStack(); } | ||
}; | ||
|
||
chip::Tracing::Json::JsonBackend gJsonBackend; | ||
|
||
chip::Tracing::Perfetto::FileTraceOutput gPerfettoFileOutput; | ||
chip::Tracing::Perfetto::PerfettoBackend gPerfettoBackend; | ||
|
||
} // namespace | ||
|
||
extern "C" void pychip_tracing_start_json_log(const char * file_name) | ||
{ | ||
|
||
ScopedStackLock lock; | ||
|
||
gJsonBackend.CloseFile(); // just in case, ensure no file output | ||
chip::Tracing::Register(gJsonBackend); | ||
} | ||
|
||
extern "C" PyChipError pychip_tracing_start_json_file(const char * file_name) | ||
{ | ||
ScopedStackLock lock; | ||
|
||
CHIP_ERROR err = gJsonBackend.OpenFile(file_name); | ||
if (err != CHIP_NO_ERROR) | ||
{ | ||
return ToPyChipError(err); | ||
} | ||
chip::Tracing::Register(gJsonBackend); | ||
return ToPyChipError(CHIP_NO_ERROR); | ||
} | ||
|
||
extern "C" void pychip_tracing_start_perfetto_system() | ||
{ | ||
ScopedStackLock lock; | ||
|
||
chip::Tracing::Perfetto::Initialize(perfetto::kSystemBackend); | ||
chip::Tracing::Perfetto::RegisterEventTrackingStorage(); | ||
chip::Tracing::Register(gPerfettoBackend); | ||
} | ||
|
||
extern "C" PyChipError pychip_tracing_start_perfetto_file(const char * file_name) | ||
{ | ||
ScopedStackLock lock; | ||
|
||
chip::Tracing::Perfetto::Initialize(perfetto::kInProcessBackend); | ||
chip::Tracing::Perfetto::RegisterEventTrackingStorage(); | ||
|
||
CHIP_ERROR err = gPerfettoFileOutput.Open(file_name); | ||
if (err != CHIP_NO_ERROR) | ||
{ | ||
return ToPyChipError(err); | ||
} | ||
chip::Tracing::Register(gPerfettoBackend); | ||
|
||
return ToPyChipError(CHIP_NO_ERROR); | ||
} | ||
|
||
extern "C" void pychip_tracing_stop() | ||
{ | ||
ScopedStackLock lock; | ||
|
||
chip::Tracing::Perfetto::FlushEventTrackingStorage(); | ||
gPerfettoFileOutput.Close(); | ||
chip::Tracing::Unregister(gPerfettoBackend); | ||
chip::Tracing::Unregister(gJsonBackend); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# | ||
# Copyright (c) 2023 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 ctypes | ||
from enum import Enum, auto | ||
from typing import Optional | ||
|
||
import chip.native | ||
from chip.native import PyChipError | ||
|
||
|
||
def _GetTracingLibraryHandle() -> ctypes.CDLL: | ||
""" Get the native library handle with tracing methods initialized. | ||
Retreives the CHIP native library handle and attaches signatures to | ||
native methods. | ||
""" | ||
|
||
# Getting a handle without requiring init, as tracing methods | ||
# do not require chip stack startup | ||
handle = chip.native.GetLibraryHandle(chip.native.HandleFlags(0)) | ||
|
||
# Uses one of the type decorators as an indicator for everything being | ||
# initialized. | ||
if not handle.pychip_tracing_start_json_file.argtypes: | ||
setter = chip.native.NativeLibraryHandleMethodArguments(handle) | ||
|
||
setter.Set('pychip_tracing_start_json_log', None, []) | ||
setter.Set('pychip_tracing_start_json_file', PyChipError, [ctypes.c_char_p]) | ||
|
||
setter.Set('pychip_tracing_start_perfetto_system', None, []) | ||
setter.Set('pychip_tracing_start_perfetto_file', PyChipError, [ctypes.c_char_p]) | ||
|
||
setter.Set('pychip_tracing_stop', None, []) | ||
|
||
return handle | ||
|
||
|
||
class TraceType(Enum): | ||
JSON = auto() | ||
PERFETTO = auto() | ||
|
||
|
||
def StartTracingTo(trace_type: TraceType, file_name: Optional[str] = None): | ||
""" | ||
Initiate tracing to the specified destination. | ||
Note that only one active trace can exist of a given type (i.e. cannot trace both | ||
to files and logs/system). | ||
""" | ||
handle = _GetTracingLibraryHandle() | ||
|
||
if trace_type == TraceType.JSON: | ||
if file_name is None: | ||
handle.pychip_tracing_start_json_log() | ||
else: | ||
handle.pychip_tracing_start_json_file(file_name.encode('utf-8')).raise_on_error() | ||
elif trace_type == TraceType.PERFETTO: | ||
if file_name is None: | ||
handle.pychip_tracing_start_perfetto_system() | ||
else: | ||
handle.pychip_tracing_start_perfetto_file(file_name.encode('utf-8')).raise_on_error() | ||
else: | ||
raise ValueError("unknown trace type") | ||
|
||
|
||
def StopTracing(): | ||
""" | ||
Make sure tracing is stopped. | ||
MUST be called before application exits. | ||
""" | ||
_GetTracingLibraryHandle().pychip_tracing_stop() | ||
|
||
|
||
class TracingContext: | ||
"""Allows scoped enter/exit for tracing, like: | ||
with TracingContext() as tracing: | ||
tracing.Start(TraceType.JSON) | ||
# ... | ||
""" | ||
|
||
def Start(self, trace_type: TraceType, file_name: Optional[str] = None): | ||
StartTracingTo(trace_type, file_name) | ||
|
||
def StartFromString(self, destination: str): | ||
""" | ||
Convert a human string to a perfetto start. | ||
Supports json:log, json:path, perfetto, perfetto:path | ||
""" | ||
if destination == 'perfetto': | ||
self.Start(TraceType.PERFETTO) | ||
elif destination == 'json:log': | ||
self.Start(TraceType.JSON) | ||
elif destination.startswith("json:"): | ||
self.Start(TraceType.JSON, destination[5:]) | ||
elif destination.startswith("perfetto:"): | ||
self.Start(TraceType.PERFETTO, destination[9:]) | ||
else: | ||
raise ValueError("Invalid trace-to destination: %r", destination) | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, type, value, traceback): | ||
StopTracing() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.