From 1a7ef1a6dd3ca8ee328973e128e362b556a1ac3a Mon Sep 17 00:00:00 2001 From: Chris Freehill Date: Mon, 30 Oct 2017 12:12:40 -0500 Subject: [PATCH] first commit [ROCm/rocm_smi_lib commit: e948ee5e00d9fab78f7baa929f18bcbda37a4434] --- projects/rocm-smi-lib/CMakeLists.txt | 66 +++++ projects/rocm-smi-lib/README.md | 12 + .../rocm-smi-lib/example/rocm_smi_example.cc | 150 +++++++++++ .../rocm-smi-lib/include/rocm_smi/rocm_smi.h | 51 ++++ .../include/rocm_smi/rocm_smi_device.h | 92 +++++++ .../include/rocm_smi/rocm_smi_main.h | 83 ++++++ .../include/rocm_smi/rocm_smi_monitor.h | 78 ++++++ projects/rocm-smi-lib/src/rocm_smi.cc | 47 ++++ projects/rocm-smi-lib/src/rocm_smi_device.cc | 196 ++++++++++++++ projects/rocm-smi-lib/src/rocm_smi_main.cc | 240 ++++++++++++++++++ projects/rocm-smi-lib/src/rocm_smi_monitor.cc | 136 ++++++++++ 11 files changed, 1151 insertions(+) create mode 100755 projects/rocm-smi-lib/CMakeLists.txt create mode 100644 projects/rocm-smi-lib/README.md create mode 100755 projects/rocm-smi-lib/example/rocm_smi_example.cc create mode 100755 projects/rocm-smi-lib/include/rocm_smi/rocm_smi.h create mode 100755 projects/rocm-smi-lib/include/rocm_smi/rocm_smi_device.h create mode 100755 projects/rocm-smi-lib/include/rocm_smi/rocm_smi_main.h create mode 100755 projects/rocm-smi-lib/include/rocm_smi/rocm_smi_monitor.h create mode 100755 projects/rocm-smi-lib/src/rocm_smi.cc create mode 100755 projects/rocm-smi-lib/src/rocm_smi_device.cc create mode 100755 projects/rocm-smi-lib/src/rocm_smi_main.cc create mode 100755 projects/rocm-smi-lib/src/rocm_smi_monitor.cc diff --git a/projects/rocm-smi-lib/CMakeLists.txt b/projects/rocm-smi-lib/CMakeLists.txt new file mode 100755 index 0000000000..131daa8e39 --- /dev/null +++ b/projects/rocm-smi-lib/CMakeLists.txt @@ -0,0 +1,66 @@ +# +# Minimum version of cmake required +# +cmake_minimum_required(VERSION 2.8.0) + +project(rocm_smi) + + +if("${ROCM_SMI_BLD_BITS}" STREQUAL 64) + set (ONLY64STR "64") + set (IS64BIT 1) +else() + set (ONLY64STR "") + set (IS64BIT 0) +endif() + +string(TOLOWER "${ROCM_SMI_BUILD_TYPE}" tmp) +if("${tmp}" STREQUAL release) + set(BUILD_TYPE "Release") + set(ISDEBUG 0) +else() + set(BUILD_TYPE "Debug") + set(ISDEBUG 1) +endif() + +set(CMAKE_CXX_FLAGS "-std=c++11 ") +if(ISDEBUG) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ggdb -O0") +endif() + + +if (IS64BIT) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64 -msse -msse2") +else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32") +endif() + +set(ROCM_SMI "rocm_smi${ONLY64STR}") + +set(CMAKE_BUILD_TYPE Debug) +set(SRC_DIR "src") +set(INC_DIR "include/rocm_smi") + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) +set(SMI_SRC_LIST "${SRC_DIR}/rocm_smi_device.cc") +set(SMI_SRC_LIST ${SMI_SRC_LIST} "${SRC_DIR}/rocm_smi_main.cc") +set(SMI_SRC_LIST ${SMI_SRC_LIST} "${SRC_DIR}/rocm_smi_monitor.cc") + +set(SMI_INC_LIST "${INC_DIR}/rocm_smi_device.h") +set(SMI_INC_LIST ${SMI_INC_LIST} "${INC_DIR}/rocm_smi_main.h") +set(SMI_INC_LIST ${SMI_INC_LIST} "${INC_DIR}/rocm_smi_monitor.h") + + +# rocm_smi_device.h +# rocm_smi.h rocm_smi_main.h rocm_smi_monitor.h") + +set(SMI_EXAMPLE_EXE "rocm_smi_ex") + +add_executable(${SMI_EXAMPLE_EXE} "example/rocm_smi_example.cc") +target_link_libraries(${SMI_EXAMPLE_EXE} ${ROCM_SMI}) +add_library(${ROCM_SMI} SHARED ${SMI_SRC_LIST} ${SMI_INC_LIST}) +install(TARGETS ${ROCM_SMI} + LIBRARY DESTINATION ${PROJECT_BINARY_DIR} + RUNTIME DESTINATION ${PROJECT_BINARY_DIR}) + + diff --git a/projects/rocm-smi-lib/README.md b/projects/rocm-smi-lib/README.md new file mode 100644 index 0000000000..bd9ce390ff --- /dev/null +++ b/projects/rocm-smi-lib/README.md @@ -0,0 +1,12 @@ +# rocm_smi_lib + +## To build library and example: +mkdir -p build +cd build +cmake .. +make +cd .. + +The above commands will result in building the library librocm_smi.so and +an example, rocm_smi_ex, which links with this library. + diff --git a/projects/rocm-smi-lib/example/rocm_smi_example.cc b/projects/rocm-smi-lib/example/rocm_smi_example.cc new file mode 100755 index 0000000000..533243c06e --- /dev/null +++ b/projects/rocm-smi-lib/example/rocm_smi_example.cc @@ -0,0 +1,150 @@ +/* + * ============================================================================= + * ROC Runtime Conformance Release License + * ============================================================================= + * The University of Illinois/NCSA + * Open Source License (NCSA) + * + * Copyright (c) 2017, Advanced Micro Devices, Inc. + * All rights reserved. + * + * Developed by: + * + * AMD Research and AMD ROC Software Development + * + * Advanced Micro Devices, Inc. + * + * www.amd.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal with the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimers. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimers in + * the documentation and/or other materials provided with the distribution. + * - Neither the names of , + * nor the names of its contributors may be used to endorse or promote + * products derived from this Software without specific prior written + * permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS WITH THE SOFTWARE. + * + */ + +#include + +#include +#include +#include "rocm_smi/rocm_smi.h" + +// Call-back function to append to a vector of Devices +static bool GetMonitorDevices(const std::shared_ptr &d, + void *p) { + std::string val_str; + + assert(p != nullptr); + + std::vector> *device_list = + reinterpret_cast> *>(p); + + if (d->monitor() != nullptr) { + device_list->push_back(d); + } + return false; +} + +int main() { + amd::smi::RocmSMI hw; + std::vector> monitor_devices; + + // DiscoverDevices() will seach for devices and monitors and update internal + // data structures. + hw.DiscoverDevices(); + + // IterateSMIDevices will iterate through all the known devices and apply + // the provided call-back to each device found. + hw.IterateSMIDevices(GetMonitorDevices, + reinterpret_cast(&monitor_devices)); + + std::string val_str; + std::vector val_vec; + uint32_t value; + uint32_t value2; + int ret; + + // Iterate through the list of devices and print out information related to + // that device. + for (auto dev : monitor_devices) { + dev->readDevInfo(amd::smi::kDevDevID, &val_str); + std::cout << "\t**Device ID:" << val_str << std::endl; + + dev->readDevInfo(amd::smi::kDevPerfLevel, &val_str); + std::cout << "\t**Performance Level:" << val_str << std::endl; + + dev->readDevInfo(amd::smi::kDevOverDriveLevel, &val_str); + std::cout << "\t**OverDrive Level:" << val_str << std::endl; + + dev->readDevInfo(amd::smi::kDevGPUMClk, &val_vec); + std::cout << "\t**Supported GPU Memory clock frequencies:" << std::endl; + for (auto vs : val_vec) { + std::cout << "\t** " << vs << std::endl; + } + val_vec.clear(); + + dev->readDevInfo(amd::smi::kDevGPUSClk, &val_vec); + std::cout << "\t**Supported GPU clock frequencies:" << val_str << std::endl; + for (auto vs : val_vec) { + std::cout << "\t** " << vs << std::endl; + } + val_vec.clear(); + + // See if there is a monitor associated with the current device, and if so, + // print out the associated monitor information. + if (dev->monitor() != nullptr) { + ret = dev->monitor()->readMonitor(amd::smi::kMonName, &val_str); + std::cout << "\t**Monitor name: "; + + if (ret != -1) { + std::cout << val_str << std::endl; + } else { + std::cout << "Not available" << std::endl; + } + + std::cout << "\t**Temperature: "; + ret = dev->monitor()->readMonitor(amd::smi::kMonTemp, &value); + if (ret != -1) { + std::cout << static_cast(value)/1000.0 << "C" << std::endl; + } else { + std::cout << "Not available" << std::endl; + } + + std::cout << "\t**Current Fan Speed: "; + + ret = dev->monitor()->readMonitor(amd::smi::kMonMaxFanSpeed, &value); + if (ret == 0) { + ret = dev->monitor()->readMonitor(amd::smi::kMonFanSpeed, &value2); + if (ret != -1) { + std::cout.setf(std::ios::dec, std::ios::basefield); + std::cout << value2/static_cast(value) * 100 << "% (" << + value2 << "/" << value << ")" << std::endl; + } else { + std::cout << "Not available" << std::endl; + } + } + } + std::cout << "\t=======" << std::endl; + } + return 0; +} diff --git a/projects/rocm-smi-lib/include/rocm_smi/rocm_smi.h b/projects/rocm-smi-lib/include/rocm_smi/rocm_smi.h new file mode 100755 index 0000000000..f59b5511d3 --- /dev/null +++ b/projects/rocm-smi-lib/include/rocm_smi/rocm_smi.h @@ -0,0 +1,51 @@ +/* + * ============================================================================= + * ROC Runtime Conformance Release License + * ============================================================================= + * The University of Illinois/NCSA + * Open Source License (NCSA) + * + * Copyright (c) 2017, Advanced Micro Devices, Inc. + * All rights reserved. + * + * Developed by: + * + * AMD Research and AMD ROC Software Development + * + * Advanced Micro Devices, Inc. + * + * www.amd.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal with the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimers. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimers in + * the documentation and/or other materials provided with the distribution. + * - Neither the names of , + * nor the names of its contributors may be used to endorse or promote + * products derived from this Software without specific prior written + * permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS WITH THE SOFTWARE. + * + */ +#ifndef ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_H_ +#define ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_H_ +#include "rocm_smi/rocm_smi_monitor.h" +#include "rocm_smi/rocm_smi_device.h" +#include "rocm_smi/rocm_smi_main.h" + +#endif // ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_H_ diff --git a/projects/rocm-smi-lib/include/rocm_smi/rocm_smi_device.h b/projects/rocm-smi-lib/include/rocm_smi/rocm_smi_device.h new file mode 100755 index 0000000000..83896c12fb --- /dev/null +++ b/projects/rocm-smi-lib/include/rocm_smi/rocm_smi_device.h @@ -0,0 +1,92 @@ +/* + * ============================================================================= + * ROC Runtime Conformance Release License + * ============================================================================= + * The University of Illinois/NCSA + * Open Source License (NCSA) + * + * Copyright (c) 2017, Advanced Micro Devices, Inc. + * All rights reserved. + * + * Developed by: + * + * AMD Research and AMD ROC Software Development + * + * Advanced Micro Devices, Inc. + * + * www.amd.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal with the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimers. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimers in + * the documentation and/or other materials provided with the distribution. + * - Neither the names of , + * nor the names of its contributors may be used to endorse or promote + * products derived from this Software without specific prior written + * permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS WITH THE SOFTWARE. + * + */ +#ifndef ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_DEVICE_H_ +#define ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_DEVICE_H_ +#include +#include +#include +#include +#include + +#include "rocm_smi/rocm_smi_monitor.h" + +namespace amd { +namespace smi { + +enum DevInfoTypes { + kDevPerfLevel, + kDevOverDriveLevel, + kDevDevID, + kDevGPUMClk, + kDevGPUSClk +}; + +class Device { + public: + explicit Device(std::string path); + ~Device(void); + + void set_monitor(std::shared_ptr m) {monitor_ = m;} + std::string path(void) const {return path_;} + const std::shared_ptr& monitor() {return monitor_;} + + int readDevInfo(DevInfoTypes type, uint32_t *val); + int readDevInfo(DevInfoTypes type, std::string *val); + int readDevInfo(DevInfoTypes type, std::vector *retVec); + + + private: + std::shared_ptr monitor_; + std::string path_; + + int readDevInfoStr(DevInfoTypes type, std::string *retStr); + int readDevInfoMultiLineStr(DevInfoTypes type, + std::vector *retVec); +}; + +} // namespace smi +} // namespace amd + +#endif // ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_DEVICE_H_ diff --git a/projects/rocm-smi-lib/include/rocm_smi/rocm_smi_main.h b/projects/rocm-smi-lib/include/rocm_smi/rocm_smi_main.h new file mode 100755 index 0000000000..9862efeefe --- /dev/null +++ b/projects/rocm-smi-lib/include/rocm_smi/rocm_smi_main.h @@ -0,0 +1,83 @@ +/* + * ============================================================================= + * ROC Runtime Conformance Release License + * ============================================================================= + * The University of Illinois/NCSA + * Open Source License (NCSA) + * + * Copyright (c) 2017, Advanced Micro Devices, Inc. + * All rights reserved. + * + * Developed by: + * + * AMD Research and AMD ROC Software Development + * + * Advanced Micro Devices, Inc. + * + * www.amd.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal with the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimers. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimers in + * the documentation and/or other materials provided with the distribution. + * - Neither the names of , + * nor the names of its contributors may be used to endorse or promote + * products derived from this Software without specific prior written + * permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS WITH THE SOFTWARE. + * + */ +#ifndef ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_MAIN_H_ +#define ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_MAIN_H_ + +#include +#include +#include +#include +#include + +#include "rocm_smi/rocm_smi_device.h" +#include "rocm_smi/rocm_smi_monitor.h" + +namespace amd { +namespace smi { + +class RocmSMI { + public: + RocmSMI(void); + ~RocmSMI(void); + + uint32_t DiscoverDevices(void); + + // Will execute "func" for every Device object known about, or until func + // returns true; + void IterateSMIDevices( + std::function&, void *)> func, void *); + private: + std::vector> devices_; + std::vector> monitors_; + std::set amd_monitor_types_; + void AddToDeviceList(std::string dev_name); + + uint32_t DiscoverAMDMonitors(void); +}; + +} // namespace smi +} // namespace amd + +#endif // ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_MAIN_H_ diff --git a/projects/rocm-smi-lib/include/rocm_smi/rocm_smi_monitor.h b/projects/rocm-smi-lib/include/rocm_smi/rocm_smi_monitor.h new file mode 100755 index 0000000000..77fead3e8f --- /dev/null +++ b/projects/rocm-smi-lib/include/rocm_smi/rocm_smi_monitor.h @@ -0,0 +1,78 @@ +/* + * ============================================================================= + * ROC Runtime Conformance Release License + * ============================================================================= + * The University of Illinois/NCSA + * Open Source License (NCSA) + * + * Copyright (c) 2017, Advanced Micro Devices, Inc. + * All rights reserved. + * + * Developed by: + * + * AMD Research and AMD ROC Software Development + * + * Advanced Micro Devices, Inc. + * + * www.amd.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal with the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimers. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimers in + * the documentation and/or other materials provided with the distribution. + * - Neither the names of , + * nor the names of its contributors may be used to endorse or promote + * products derived from this Software without specific prior written + * permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS WITH THE SOFTWARE. + * + */ +#ifndef ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_MONITOR_H_ +#define ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_MONITOR_H_ + +#include +#include + +namespace amd { +namespace smi { + +enum MonitorTypes { + kMonName, + kMonTemp, // Temperature in millidegrees + kMonFanSpeed, + kMonMaxFanSpeed, +}; + + +class Monitor { + public: + explicit Monitor(std::string path); + ~Monitor(void); + const std::string path(void) const {return path_;} + int readMonitor(MonitorTypes type, uint32_t *val); + int readMonitor(MonitorTypes type, std::string *val); + + private: + std::string path_; + int readMonitorStr(MonitorTypes type, std::string *retStr); +}; + +} // namespace smi +} // namespace amd + +#endif // ROCM_SMI_LIB_INCLUDE_ROCM_SMI_ROCM_SMI_MONITOR_H_ diff --git a/projects/rocm-smi-lib/src/rocm_smi.cc b/projects/rocm-smi-lib/src/rocm_smi.cc new file mode 100755 index 0000000000..ada4ff47c1 --- /dev/null +++ b/projects/rocm-smi-lib/src/rocm_smi.cc @@ -0,0 +1,47 @@ +/* + * ============================================================================= + * ROC Runtime Conformance Release License + * ============================================================================= + * The University of Illinois/NCSA + * Open Source License (NCSA) + * + * Copyright (c) 2017, Advanced Micro Devices, Inc. + * All rights reserved. + * + * Developed by: + * + * AMD Research and AMD ROC Software Development + * + * Advanced Micro Devices, Inc. + * + * www.amd.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal with the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimers. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimers in + * the documentation and/or other materials provided with the distribution. + * - Neither the names of , + * nor the names of its contributors may be used to endorse or promote + * products derived from this Software without specific prior written + * permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS WITH THE SOFTWARE. + * + */ + +#include "rocm_smi/rocm_smi.h" + diff --git a/projects/rocm-smi-lib/src/rocm_smi_device.cc b/projects/rocm-smi-lib/src/rocm_smi_device.cc new file mode 100755 index 0000000000..9dbba5743e --- /dev/null +++ b/projects/rocm-smi-lib/src/rocm_smi_device.cc @@ -0,0 +1,196 @@ +/* ROC Runtime Conformance Release License + * ============================================================================= + * The University of Illinois/NCSA + * Open Source License (NCSA) + * + * Copyright (c) 2017, Advanced Micro Devices, Inc. + * All rights reserved. + * + * Developed by: + * + * AMD Research and AMD ROC Software Development + * + * Advanced Micro Devices, Inc. + * + * www.amd.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal with the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimers. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimers in + * the documentation and/or other materials provided with the distribution. + * - Neither the names of , + * nor the names of its contributors may be used to endorse or promote + * products derived from this Software without specific prior written + * permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS WITH THE SOFTWARE. + * + */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "rocm_smi/rocm_smi_main.h" +#include "rocm_smi/rocm_smi_device.h" + +namespace amd { +namespace smi { + +static const char *kDevPerfLevelFName = "power_dpm_force_performance_level"; +static const char *kDevDevIDFName = "device"; +static const char *kDevOverDriveLevelFName = "pp_sclk_od"; +static const char *kDevGPUSClkFName = "pp_dpm_sclk"; +static const char *kDevGPUMClkFName = "pp_dpm_mclk"; + +static const std::map kDevAttribNameMap = { + {kDevPerfLevel, kDevPerfLevelFName}, + {kDevOverDriveLevel, kDevOverDriveLevelFName}, + {kDevDevID, kDevDevIDFName}, + {kDevGPUMClk, kDevGPUMClkFName}, + {kDevGPUSClk, kDevGPUSClkFName}, +}; + +static bool isRegularFile(std::string fname) { + struct stat file_stat; + stat(fname.c_str(), &file_stat); + return S_ISREG(file_stat.st_mode); +} + +Device::Device(std::string p) : path_(p) { + monitor_ = nullptr; +} + +Device:: ~Device() { +} + +// TODO(cfreehil): cache values that are constant +int Device::readDevInfoStr(DevInfoTypes type, std::string *retStr) { + auto tempPath = path_; + + assert(retStr != nullptr); + + tempPath += "/device/"; + tempPath += kDevAttribNameMap.at(type); + + std::ifstream fs; + fs.open(tempPath); + + if (!fs.is_open() || !isRegularFile(tempPath)) { + return -1; + } + fs >> *retStr; + fs.close(); + + return 0; +} + +int Device::readDevInfoMultiLineStr(DevInfoTypes type, + std::vector *retVec) { + auto tempPath = path_; + std::string line; + + assert(retVec != nullptr); + + tempPath += "/device/"; + tempPath += kDevAttribNameMap.at(type); + + std::ifstream fs(tempPath); + std::stringstream buffer; + + + if (!isRegularFile(tempPath)) { + return -1; + } + + while (std::getline(fs, line)) { + retVec->push_back(line); + } + return 0; +} + +int Device::readDevInfo(DevInfoTypes type, uint32_t *val) { + assert(val != nullptr); + + std::string tempStr; + + switch (type) { + case kDevDevID: + if (readDevInfoStr(type, &tempStr)) { + return -1; + } + *val = std::stoi(tempStr, 0, 16); + break; + + case kDevOverDriveLevel: + if (readDevInfoStr(type, &tempStr)) { + return -1; + } + *val = std::stoi(tempStr, 0); + break; + + default: + return -1; + } + return 0; +} + +int Device::readDevInfo(DevInfoTypes type, std::vector *val) { + assert(val != nullptr); + + switch (type) { + case kDevGPUMClk: + case kDevGPUSClk: + if (readDevInfoMultiLineStr(type, val)) { + return -1; + } + break; + + default: + return -1; + } + + return 0; +} + +int Device::readDevInfo(DevInfoTypes type, std::string *val) { + assert(val != nullptr); + + switch (type) { + case kDevPerfLevel: + case kDevOverDriveLevel: + case kDevDevID: + if (readDevInfoStr(type, val)) { + return -1; + } + break; + + default: + return -1; + } + return 0; +} + +} // namespace smi +} // namespace amd diff --git a/projects/rocm-smi-lib/src/rocm_smi_main.cc b/projects/rocm-smi-lib/src/rocm_smi_main.cc new file mode 100755 index 0000000000..0c86822d46 --- /dev/null +++ b/projects/rocm-smi-lib/src/rocm_smi_main.cc @@ -0,0 +1,240 @@ +/* + * ============================================================================= + * ROC Runtime Conformance Release License + * ============================================================================= + * The University of Illinois/NCSA + * Open Source License (NCSA) + * + * Copyright (c) 2017, Advanced Micro Devices, Inc. + * All rights reserved. + * + * Developed by: + * + * AMD Research and AMD ROC Software Development + * + * Advanced Micro Devices, Inc. + * + * www.amd.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal with the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimers. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimers in + * the documentation and/or other materials provided with the distribution. + * - Neither the names of , + * nor the names of its contributors may be used to endorse or promote + * products derived from this Software without specific prior written + * permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS WITH THE SOFTWARE. + * + */ +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rocm_smi/rocm_smi.h" +#include "rocm_smi/rocm_smi_main.h" + +static const char *kPathDRMRoot = "/sys/class/drm"; +static const char *kPathHWMonRoot = "/sys/class/hwmon"; +static const char *kDeviceNamePrefix = "card"; + +static const char *kAMDMonitorTypes[] = {"radeon", "amdgpu", ""}; + +namespace amd { +namespace smi { + +static bool FileExists(char const *filename) { + struct stat buf; + return (stat(filename, &buf) == 0); +} + +// Return 0 if same file, 1 if not, and -1 for error +static int SameFile(const std::string fileA, const std::string fileB) { + struct stat aStat; + struct stat bStat; + int ret; + + ret = stat(fileA.c_str(), &aStat); + if (ret) { + return -1; + } + + ret = stat(fileB.c_str(), &bStat); + if (ret) { + return -1; + } + + if (aStat.st_dev != bStat.st_dev) { + return 1; + } + + if (aStat.st_ino != bStat.st_ino) { + return 1; + } + + return 0; +} + +static int SameDevice(const std::string fileA, const std::string fileB) { + return SameFile(fileA + "/device", fileB + "/device"); +} + +void ShowAllTemperatures(); + +RocmSMI::RocmSMI() { + auto i = 0; + + while (std::string(kAMDMonitorTypes[i]) != "") { + amd_monitor_types_.insert(kAMDMonitorTypes[i]); + ++i; + } +} + +RocmSMI::~RocmSMI() { + devices_.clear(); + monitors_.clear(); +} + +void +RocmSMI::AddToDeviceList(std::string dev_name) { + auto ret = 0; + + auto dev_path = std::string(kPathDRMRoot); + dev_path += "/"; + dev_path += dev_name; + + auto dev = std::shared_ptr(new Device(dev_path)); + + auto m = monitors_.begin(); + + while (m != monitors_.end()) { + ret = SameDevice(dev->path(), (*m)->path()); + + if (ret == 0) { + dev->set_monitor(*m); + + m = monitors_.erase(m); + } else { + assert(ret == 1); + ++m; + } + } + + devices_.push_back(dev); + + return; +} + +uint32_t RocmSMI::DiscoverDevices(void) { + auto ret = 0; + + ret = DiscoverAMDMonitors(); + + if (ret) { + return ret; + } + + auto drm_dir = opendir(kPathDRMRoot); + auto dentry = readdir(drm_dir); + + while (dentry != nullptr) { + if (memcmp(dentry->d_name, kDeviceNamePrefix, strlen(kDeviceNamePrefix)) + == 0) { + AddToDeviceList(dentry->d_name); + } + dentry = readdir(drm_dir); + } + + if (closedir(drm_dir)) { + return 1; + } + return 0; +} + +uint32_t RocmSMI::DiscoverAMDMonitors(void) { + auto mon_dir = opendir(kPathHWMonRoot); + + auto dentry = readdir(mon_dir); + + std::string mon_name; + std::string tmp; + + while (dentry != nullptr) { + if (dentry->d_name[0] == '.') { + dentry = readdir(mon_dir); + continue; + } + + mon_name = kPathHWMonRoot; + mon_name += "/"; + mon_name += dentry->d_name; + tmp = mon_name + "/name"; + + if (FileExists(tmp.c_str())) { + std::ifstream fs; + fs.open(tmp); + + if (!fs.is_open()) { + return 1; + } + std::string mon_type; + fs >> mon_type; + fs.close(); + + if (amd_monitor_types_.find(mon_type) != amd_monitor_types_.end()) { + monitors_.push_back(std::shared_ptr(new Monitor(mon_name))); + } + } + dentry = readdir(mon_dir); + } + + if (closedir(mon_dir)) { + return 1; + } + return 0; +} + +void RocmSMI::IterateSMIDevices( + std::function&, void *)> func, void *p) { + + if (func == nullptr) { + return; + } + + auto d = devices_.begin(); + + while (d != devices_.end()) { + if (func(*d, p)) { + return; + } + ++d; + } +} + +} // namespace smi +} // namespace amd diff --git a/projects/rocm-smi-lib/src/rocm_smi_monitor.cc b/projects/rocm-smi-lib/src/rocm_smi_monitor.cc new file mode 100755 index 0000000000..e3b85bfbdd --- /dev/null +++ b/projects/rocm-smi-lib/src/rocm_smi_monitor.cc @@ -0,0 +1,136 @@ +/* + * ============================================================================= + * ROC Runtime Conformance Release License + * ============================================================================= + * The University of Illinois/NCSA + * Open Source License (NCSA) + * + * Copyright (c) 2017, Advanced Micro Devices, Inc. + * All rights reserved. + * + * Developed by: + * + * AMD Research and AMD ROC Software Development + * + * Advanced Micro Devices, Inc. + * + * www.amd.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal with the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimers. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimers in + * the documentation and/or other materials provided with the distribution. + * - Neither the names of , + * nor the names of its contributors may be used to endorse or promote + * products derived from this Software without specific prior written + * permission. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS WITH THE SOFTWARE. + * + */ + +#include + +#include +#include +#include +#include +#include + +#include "rocm_smi/rocm_smi_main.h" +#include "rocm_smi/rocm_smi_monitor.h" + +namespace amd { +namespace smi { + +struct MonitorNameEntry { + MonitorTypes type; + const char *name; +}; + + +static const char *kMonTempFName = "temp1_input"; +static const char *kMonFanSpeedFName = "pwm1"; +static const char *kMonMaxFanSpeedFName = "pwm1_max"; +static const char *kMonNameFName = "name"; + +static const std::map kMonitorNameMap = { + {kMonName, kMonNameFName}, + {kMonTemp, kMonTempFName}, + {kMonFanSpeed, kMonFanSpeedFName}, + {kMonMaxFanSpeed, kMonMaxFanSpeedFName} +}; + +Monitor::Monitor(std::string path) : path_(path) { +} +Monitor::~Monitor(void) { +} + +int Monitor::readMonitorStr(MonitorTypes type, std::string *retStr) { + auto tempPath = path_; + + assert(retStr != nullptr); + + tempPath += "/"; + tempPath += kMonitorNameMap.at(type); + + std::ifstream fs; + fs.open(tempPath); + + if (!fs.is_open()) { + return -1; + } + fs >> *retStr; + fs.close(); + + return 0; +} + +int Monitor::readMonitor(MonitorTypes type, uint32_t *val) { + assert(val != nullptr); + + std::string tempStr; + + switch (type) { + case kMonTemp: // Temperature in millidegrees + case kMonFanSpeed: + case kMonMaxFanSpeed: + if (readMonitorStr(type, &tempStr)) { + return -1; + } + *val = std::stoi(tempStr); + return 0; + + default: + return -1; + } +} + +// This string version should work for all valid monitor types +int Monitor::readMonitor(MonitorTypes type, std::string *val) { + assert(val != nullptr); + + if (readMonitorStr(type, val)) { + return -1; + } + + return 0; +} + + +} // namespace smi +} // namespace amd