From 05a3155d5debed40c5351e9bc639f0a842bea976 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 30 May 2019 12:24:23 +0900 Subject: [PATCH] [xray] A lightweight message passing interface (#5305) This commit introduces xray which implements a lightweight message passing channel (based on weak symbol) for event trace. Signed-off-by: Jonghyun Park --- libs/xray/CMakeLists.txt | 3 + libs/xray/event/CMakeLists.txt | 2 + libs/xray/event/include/xray/event.h | 53 +++++++++++++++++ libs/xray/event/include/xray/event_category.h | 33 +++++++++++ libs/xray/event/include/xray/event_code.h | 42 +++++++++++++ libs/xray/mux/CMakeLists.txt | 5 ++ libs/xray/mux/include/xray/mux.h | 62 +++++++++++++++++++ libs/xray/mux/src/mux.cc | 34 +++++++++++ libs/xray/pipe/CMakeLists.txt | 3 + libs/xray/pipe/include/xray/pipe.h | 85 +++++++++++++++++++++++++++ 10 files changed, 322 insertions(+) create mode 100644 libs/xray/CMakeLists.txt create mode 100644 libs/xray/event/CMakeLists.txt create mode 100644 libs/xray/event/include/xray/event.h create mode 100644 libs/xray/event/include/xray/event_category.h create mode 100644 libs/xray/event/include/xray/event_code.h create mode 100644 libs/xray/mux/CMakeLists.txt create mode 100644 libs/xray/mux/include/xray/mux.h create mode 100644 libs/xray/mux/src/mux.cc create mode 100644 libs/xray/pipe/CMakeLists.txt create mode 100644 libs/xray/pipe/include/xray/pipe.h diff --git a/libs/xray/CMakeLists.txt b/libs/xray/CMakeLists.txt new file mode 100644 index 0000000..5d46daa --- /dev/null +++ b/libs/xray/CMakeLists.txt @@ -0,0 +1,3 @@ +add_subdirectory(event) +add_subdirectory(pipe) +add_subdirectory(mux) diff --git a/libs/xray/event/CMakeLists.txt b/libs/xray/event/CMakeLists.txt new file mode 100644 index 0000000..a5c44c0 --- /dev/null +++ b/libs/xray/event/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(nnfw_lib_xray_event INTERFACE) +target_include_directories(nnfw_lib_xray_event INTERFACE include) diff --git a/libs/xray/event/include/xray/event.h b/libs/xray/event/include/xray/event.h new file mode 100644 index 0000000..3c6fbba --- /dev/null +++ b/libs/xray/event/include/xray/event.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. 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. + */ + +#ifndef __NNFW_XRAY_EVENT_H__ +#define __NNFW_XRAY_EVENT_H__ + +#include "xray/event_category.h" +#include "xray/event_code.h" + +namespace xray +{ + +/** + * @brief Abstract event + * + * "event" class describes an abstract event that XRay recognizes, which consists of its category + * and code. + * + * The interpretation depends on "category" that it belongs to. + */ +class event final +{ +public: + event(const event_category *cat, const event_code &code) : _cat{cat}, _code{code} + { + // DO NOTHING + } + +public: + const event_category *cat(void) const { return _cat; } + const event_code &code(void) const { return _code; } + +private: + const event_category *_cat; + event_code _code; +}; + +} // namespace xray + +#endif // __NNFW_XRAY_EVENT_H__ diff --git a/libs/xray/event/include/xray/event_category.h b/libs/xray/event/include/xray/event_category.h new file mode 100644 index 0000000..4984ade --- /dev/null +++ b/libs/xray/event/include/xray/event_category.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. 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. + */ + +#ifndef __NNFW_XRAY_EVENT_CATEGORY_H__ +#define __NNFW_XRAY_EVENT_CATEGORY_H__ + +namespace xray +{ + +/** + * Each derived category is expected to have static "get" method. + */ +struct event_category +{ + // Non-virtual +}; + +} // namespace xray + +#endif // __NNFW_XRAY_EVENT_CATEGORY_H__ diff --git a/libs/xray/event/include/xray/event_code.h b/libs/xray/event/include/xray/event_code.h new file mode 100644 index 0000000..6879e54 --- /dev/null +++ b/libs/xray/event/include/xray/event_code.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. 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. + */ + +#ifndef __NNFW_XRAY_EVENT_CODE_H__ +#define __NNFW_XRAY_EVENT_CODE_H__ + +#include + +namespace xray +{ + +class event_code final +{ +public: + explicit event_code(uint64_t value) : _value{value} + { + // DO NOTHING + } + +public: + const uint64_t &value(void) const { return _value; } + +private: + uint64_t _value; +}; + +} // namespace xray + +#endif // __NNFW_XRAY_EVENT_CODE_H__ diff --git a/libs/xray/mux/CMakeLists.txt b/libs/xray/mux/CMakeLists.txt new file mode 100644 index 0000000..0484a82 --- /dev/null +++ b/libs/xray/mux/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(nnfw_lib_xray_mux STATIC src/mux.cc) +set_target_properties(nnfw_lib_xray_mux PROPERTIES POSITION_INDEPENDENT_CODE ON) +target_include_directories(nnfw_lib_xray_mux PUBLIC include) +target_link_libraries(nnfw_lib_xray_mux PUBLIC nnfw_lib_xray_event) +target_link_libraries(nnfw_lib_xray_mux PUBLIC nnfw_lib_xray_pipe) diff --git a/libs/xray/mux/include/xray/mux.h b/libs/xray/mux/include/xray/mux.h new file mode 100644 index 0000000..4cdfc14 --- /dev/null +++ b/libs/xray/mux/include/xray/mux.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. 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. + */ + +#ifndef __NNFW_XRAY_MUX_H__ +#define __NNFW_XRAY_MUX_H__ + +#include + +#include + +namespace xray +{ + +struct listener +{ + virtual ~listener() = default; + + virtual void notify(const event *) = 0; +}; + +class mux +{ +private: + // Use "get()" below + mux() = default; + +public: + void attach(listener *l) { _listeners.insert(l); } + void detach(listener *l) { _listeners.erase(l); } + +public: + void notify(const event *e) const + { + for (auto listener : _listeners) + { + listener->notify(e); + } + } + +private: + std::set _listeners; + +public: + static mux &get(void); +}; + +} // namespace xray + +#endif // __NNFW_XRAY_MUX_H__ diff --git a/libs/xray/mux/src/mux.cc b/libs/xray/mux/src/mux.cc new file mode 100644 index 0000000..a224294 --- /dev/null +++ b/libs/xray/mux/src/mux.cc @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. 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 "xray/mux.h" + +#define XRAY_TRACER +#include +#undef XRAY_TRACER + +namespace xray +{ + +mux &mux::get(void) +{ + static mux m; + return m; +} + +void pipe::post(const event *e) { mux::get().notify(e); } + +} // namespace xray diff --git a/libs/xray/pipe/CMakeLists.txt b/libs/xray/pipe/CMakeLists.txt new file mode 100644 index 0000000..19f8e50 --- /dev/null +++ b/libs/xray/pipe/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(nnfw_lib_xray_pipe INTERFACE) +target_include_directories(nnfw_lib_xray_pipe INTERFACE include) +target_link_libraries(nnfw_lib_xray_pipe INTERFACE nnfw_lib_xray_event) diff --git a/libs/xray/pipe/include/xray/pipe.h b/libs/xray/pipe/include/xray/pipe.h new file mode 100644 index 0000000..3a9eb5c --- /dev/null +++ b/libs/xray/pipe/include/xray/pipe.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. 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. + */ + +#ifndef __NNFW_XRAY_PIPE_H__ +#define __NNFW_XRAY_PIPE_H__ + +#include + +/** + * Define XRAY_TRACER before include this file for tracer implementation + */ +#ifndef XRAY_ATTR +#ifdef XRAY_TRACER +#define XRAY_ATTR __attribute__(()) +#else +#define XRAY_ATTR __attribute__((weak)) +#endif // XRAY_TRACER +#endif // XRAY_ATTR + +namespace xray +{ + +/** + * @brief A lightweight communication channel between tracee-tracer + * + * NOTE 1. + * + * "pipe" intentionally begins with a lowercase to use the following code pattern: + * + * xray::pipe::post(...) + * + * NOTE 2. + * + * The use of static member method here enables early error detection. + * + * For example, C++ compiler is unable to detect the following error at compile-time. + * + * "Method.h" + * void meth(T); + * + * "Method.cpp" + * void meth(U) { ... } + * + * The following error, on the other hand, is detected at compile-time. + * + * "Method.h" + * struct K { void meth(T); } + * + * "Method.cpp" + * void K::meth(U) { ... } + * + */ +struct pipe +{ + /** + * @brief Post event through underlying pipe + * + * WARNING FOR TRACER IMPLEMENTOR + * + * There is no guarantee that event is valid after post call. + * Use event only inside post method implementation, or copy its content if necessary. + * + * WARNING FOR TRACEE IMPELEMENTOR + * + * xray framework does not release any resource. Be careful about resource leak. + */ + static XRAY_ATTR void post(const event *); +}; + +} // namespace xray + +#endif // __NNFW_XRAY_PIPE_H__ -- 2.7.4