From a76141994c3ffec431743b0b4e593f091fb0e0ca Mon Sep 17 00:00:00 2001 From: Gallium Date: Tue, 1 Apr 2025 20:32:22 +0800 Subject: [PATCH 1/2] dynolog --- .../dynolog_npu/dynolog/src/CMakeLists.txt | 69 +++++++++++++++++++ msmonitor/plugin/CMakeLists.txt | 50 ++++++++++++++ msmonitor/plugin/bindings.cpp | 21 +++++- msmonitor/plugin/build.sh | 2 +- msmonitor/plugin/setup.py | 69 +++++++++++++------ 5 files changed, 186 insertions(+), 25 deletions(-) create mode 100644 msmonitor/dynolog_npu/dynolog/src/CMakeLists.txt create mode 100644 msmonitor/plugin/CMakeLists.txt diff --git a/msmonitor/dynolog_npu/dynolog/src/CMakeLists.txt b/msmonitor/dynolog_npu/dynolog/src/CMakeLists.txt new file mode 100644 index 0000000000..3b4b27e278 --- /dev/null +++ b/msmonitor/dynolog_npu/dynolog/src/CMakeLists.txt @@ -0,0 +1,69 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. + +cmake_minimum_required(VERSION 3.16) +add_definitions(-DDYNOLOG_VERSION=${DYNOLOG_VERSION} -DDYNOLOG_GIT_REV=${DYNOLOG_GIT_REV}) + +message("Use Prometheus = ${USE_PROMETHEUS}") +message("Use ODS Graph API = ${USE_ODS_GRAPH_API}") + +# our build script will first create a src/ dir where all source code will exist +file (GLOB dynolog_src "*.h" "*.cpp") + +# Remove main from library, only needed for exec. +list(REMOVE_ITEM dynolog_src "${CMAKE_CURRENT_SOURCE_DIR}/Main.cpp") +add_library(dynolog_lib ${dynolog_src}) + +if(USE_ODS_GRAPH_API) + target_compile_options(dynolog_lib PUBLIC "-DUSE_GRAPH_ENDPOINT") +endif() + +if(USE_PROMETHEUS) + find_package(prometheus-cpp CONFIG REQUIRED) + add_definitions(-DUSE_PROMETHEUS) + target_link_libraries(dynolog_lib PRIVATE prometheus-cpp::pull) +endif() + +target_compile_options(dynolog_lib PRIVATE + -fPIC + -fstack-protector-all + -ftrapv +) + +target_link_options(dynolog_lib PRIVATE + -Wl,-z,relro,-z,now,-z,noexecstack + -s +) + +target_link_libraries(dynolog_lib PUBLIC Monitor) +target_link_libraries(dynolog_lib PUBLIC BuiltinMetrics) + +add_subdirectory(rpc) + +add_subdirectory(ipcfabric) +target_link_libraries(dynolog_lib PUBLIC dynolog_ipcfabric_lib) + +# depends on ipcfabric +add_subdirectory(tracing) +target_link_libraries(dynolog_lib PUBLIC dynolog_ipcmonitor_lib) + +add_subdirectory(gpumon) +target_link_libraries(dynolog_lib PUBLIC dynolog_dcgm_lib "-ldl") + +add_subdirectory(rdmamon) +target_link_libraries(dynolog_lib PUBLIC dynolog_rdmamon_lib) + +add_subdirectory(metric_frame) + +add_executable(dynolog Main.cpp) +target_link_libraries(dynolog PRIVATE dynolog_lib dynolog_rpc_lib) + +target_compile_options(dynolog PRIVATE + -fPIC + -fstack-protector-all + -ftrapv +) + +target_link_options(dynolog PRIVATE + -Wl,-z,relro,-z,now,-z,noexecstack + -s +) \ No newline at end of file diff --git a/msmonitor/plugin/CMakeLists.txt b/msmonitor/plugin/CMakeLists.txt new file mode 100644 index 0000000000..2257211422 --- /dev/null +++ b/msmonitor/plugin/CMakeLists.txt @@ -0,0 +1,50 @@ +cmake_minimum_required(VERSION 3.16) +project(IPCMonitor) + +set(CMAKE_SKIP_RPATH TRUE) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +find_package(pybind11 REQUIRED) +find_package(Python REQUIRED COMPONENTS Interpreter Development) + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/ipc_monitor + ${DYNOLOG_PATH}/third_party/glog/src + ${DYNOLOG_PATH}/build/third_party/glog +) + +file(GLOB_RECURSE IPC_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ipc_monitor/*.cpp) + +set(SOURCES + bindings.cpp + ${IPC_SOURCES} +) + +add_library(IPCMonitor MODULE ${SOURCES}) + +set_target_properties(IPCMonitor + PROPERTIES + OUTPUT_NAME IPCMonitor + PREFIX "" +) + +target_link_libraries(IPCMonitor PRIVATE pybind11::module) +target_link_libraries(IPCMonitor PRIVATE ${DYNOLOG_PATH}/build/third_party/glog/libglog.a) + +target_compile_options(IPCMonitor PRIVATE + -fPIC + -fstack-protector-all + -ftrapv +) + +target_link_options(IPCMonitor PRIVATE + -Wl,-z,relro,-z,now,-z,noexecstack + -s +) + +install(TARGETS IPCMonitor + DESTINATION ${CMAKE_INSTALL_PREFIX}/python-package +) \ No newline at end of file diff --git a/msmonitor/plugin/bindings.cpp b/msmonitor/plugin/bindings.cpp index b7c8a812a0..2624e9d705 100644 --- a/msmonitor/plugin/bindings.cpp +++ b/msmonitor/plugin/bindings.cpp @@ -1,14 +1,31 @@ + #include #include #include "ipc_monitor/PyDynamicMonitorProxy.h" namespace py = pybind11; -PYBIND11_MODULE(IPCMonitor, m) { - py::class_(m, "PyDynamicMonitorProxy") +void init_IPCMonitor(PyObject *module) { + py::class_(module, "PyDynamicMonitorProxy") .def(py::init<>()) .def("init_dyno", &dynolog_npu::ipc_monitor::PyDynamicMonitorProxy::InitDyno, py::arg("npuId")) .def("poll_dyno", &dynolog_npu::ipc_monitor::PyDynamicMonitorProxy::PollDyno) .def("enable_dyno_npu_monitor", &dynolog_npu::ipc_monitor::PyDynamicMonitorProxy::EnableMsptiMonitor, py::arg("cfg_map")) .def("finalize_dyno", &dynolog_npu::ipc_monitor::PyDynamicMonitorProxy::FinalizeDyno); +} + +static PyMethodDef g_moduleMethods[] = {}; + +static struct PyModuleDef ipcMonitor_module = { + PyModuleDef_HEAD_INIT, + "IPCMonitor", + nullptr, + -1, + g_moduleMethods +}; + +PyMODINIT_FUNC PyInit_IPCMonitor(void) { + PyObject* m = PyModule_Create(&ipcMonitor_module); + init_IPCMonitor(m); + return m; } \ No newline at end of file diff --git a/msmonitor/plugin/build.sh b/msmonitor/plugin/build.sh index ce20d9d2be..21ccc6740f 100644 --- a/msmonitor/plugin/build.sh +++ b/msmonitor/plugin/build.sh @@ -3,7 +3,7 @@ # install pybind11 pip install pybind11 -# build dynolog_npu_plugin wheel +# build msmonitor_plugin wheel python3 setup.py bdist_wheel # find .whl files in dist diff --git a/msmonitor/plugin/setup.py b/msmonitor/plugin/setup.py index fb5d2660ad..d3f203a483 100644 --- a/msmonitor/plugin/setup.py +++ b/msmonitor/plugin/setup.py @@ -13,33 +13,58 @@ # See the License for the specific language governing permissions and # limitations under the License. import os -from glob import glob -from setuptools import setup -from pybind11.setup_helpers import Pybind11Extension +import sys + +import shutil +import subprocess +import pybind11 + +from setuptools import setup, Extension +from setuptools.command.build_ext import build_ext + + +class CMakeExtension(Extension): + def __init__(self, name, sourcedir=""): + super().__init__(name, sources=[]) + self.sourcedir = os.path.abspath(sourcedir) + + +class CMakeBuild(build_ext): + def run(self): + for ext in self.extensions: + self.build_extension(ext) + + def build_extension(self, ext): + cfg = 'Debug' if self.debug else 'Release' + build_args = ['--config', cfg] + + ext_dir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) + cmake_args = [ + '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + ext_dir, + '-DPYTHON_EXECUTABLE=' + sys.executable, + '-DCMAKE_PREFIX_PATH=' + pybind11.get_cmake_dir(), + '-DCMAKE_INSTALL_PREFIX=' + ext_dir, + '-DDYNOLOG_PATH=' + os.path.join(os.path.dirname(BASE_DIR), "third_party", "dynolog"), + '-DCMAKE_BUILD_TYPE=' + cfg + ] + + env = os.environ.copy() + env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''), + self.distribution.get_version()) + + if not os.path.exists(self.build_temp): + os.makedirs(self.build_temp) + subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env) + subprocess.check_call(['cmake', '--build', '.', '--target', 'install'] + build_args, cwd=self.build_temp) + shutil.rmtree(self.build_temp) BASE_DIR = os.path.dirname(os.path.realpath(__file__)) -DYNOLOG_PATH = os.path.join(os.path.dirname(BASE_DIR), "third_party", "dynolog") -GLOG_INC_PATH = os.path.join(DYNOLOG_PATH, "third_party", "glog", "src") -GLOG_LIB_PATH = os.path.join(DYNOLOG_PATH, "build", "third_party", "glog") - -# Define the extension module -ext_modules = [ - Pybind11Extension( - "IPCMonitor", # Name of the Python module - sources=["bindings.cpp"] + list(glob("ipc_monitor/*.cpp")), # Source files - include_dirs=[os.path.join(BASE_DIR, "ipc_monitor"), GLOG_INC_PATH, GLOG_LIB_PATH], # Include Pybind11 headers - library_dirs=[GLOG_LIB_PATH], - libraries=["glog"], - language="c++", # Specify the language - ), -] - - -# Set up the package + setup( name="msmonitor_plugin", version="0.1", description="msMonitor plugins", - ext_modules=ext_modules, + ext_modules=[CMakeExtension('IPCMonitor')], + cmdclass=dict(build_ext=CMakeBuild), install_requires=["pybind11"], ) \ No newline at end of file -- Gitee From ef535607dcbc3111e4d2699a4c09be766d97618e Mon Sep 17 00:00:00 2001 From: Gallium Date: Tue, 1 Apr 2025 20:35:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?dyno=20rust=E5=AE=89=E5=85=A8=E7=BC=96?= =?UTF-8?q?=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- msmonitor/dynolog_npu/cli/src/Cargo.toml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 msmonitor/dynolog_npu/cli/src/Cargo.toml diff --git a/msmonitor/dynolog_npu/cli/src/Cargo.toml b/msmonitor/dynolog_npu/cli/src/Cargo.toml new file mode 100644 index 0000000000..5254fed26f --- /dev/null +++ b/msmonitor/dynolog_npu/cli/src/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "dyno" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1.0.57" +clap = { version = "3.1.0", features = ["derive"]} +serde_json = "1.0" + +# Make it work with conda +# See https://github.com/rust-lang/cargo/issues/6652 +[net] +git-fetch-with-cli = true + +[build] +rustflags = [ + "-C", "relocation_model=pie", + "-C", "link-args=-Wl,-z,now", + "-C", "link-args=-Wl,-z,relro", + "-C", "strip=symbols", + "-C", "overflow_checks" +] \ No newline at end of file -- Gitee