diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml index 0d3fcd55f9..1bff7f4190 100644 --- a/.github/workflows/continuous_integration.yml +++ b/.github/workflows/continuous_integration.yml @@ -270,7 +270,7 @@ jobs: git config --global --add safe.directory '*' - name: Configure, Build, and Test - timeout-minutes: 30 + timeout-minutes: 45 shell: bash run: python3 ./source/scripts/run-ci.py -B build diff --git a/cmake/rocprofiler_options.cmake b/cmake/rocprofiler_options.cmake index 36ebf863a0..a62ba7d0fb 100644 --- a/cmake/rocprofiler_options.cmake +++ b/cmake/rocprofiler_options.cmake @@ -78,6 +78,9 @@ rocprofiler_add_option(ROCPROFILER_BUILD_STACK_PROTECTOR "Build with -fstack-pro ON ADVANCED) rocprofiler_add_option(ROCPROFILER_UNSAFE_NO_VERSION_CHECK "Disable HSA version checking (for development only)" OFF ADVANCED) +rocprofiler_add_option( + ROCPROFILER_REGENERATE_COUNTERS_PARSER + "Regenerate the counter parser (requires bison and flex)" OFF ADVANCED) # In the future, we will do this even with clang-tidy enabled if(ROCPROFILER_BUILD_CI AND NOT ROCPROFILER_BUILD_WERROR) diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index b5fed82a0f..513bea9db4 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -18,3 +18,4 @@ endif() add_subdirectory(pc_sampling) add_subdirectory(api_callback_tracing) add_subdirectory(api_buffered_tracing) +add_subdirectory(counter_collection) diff --git a/samples/counter_collection/CMakeLists.txt b/samples/counter_collection/CMakeLists.txt new file mode 100644 index 0000000000..3dd1a434e0 --- /dev/null +++ b/samples/counter_collection/CMakeLists.txt @@ -0,0 +1,55 @@ +# +# +# +cmake_minimum_required(VERSION 3.21.0 FATAL_ERROR) + +if(NOT CMAKE_HIP_COMPILER) + find_program( + amdclangpp_EXECUTABLE + NAMES amdclang++ + HINTS ${ROCM_PATH} ENV ROCM_PATH /opt/rocm + PATHS ${ROCM_PATH} ENV ROCM_PATH /opt/rocm + PATH_SUFFIXES bin llvm/bin NO_CACHE) + mark_as_advanced(amdclangpp_EXECUTABLE) + + if(amdclangpp_EXECUTABLE) + set(CMAKE_HIP_COMPILER "${amdclangpp_EXECUTABLE}") + endif() +endif() + +project(rocprofiler-samples-counter-collection LANGUAGES CXX HIP) + +foreach(_TYPE DEBUG MINSIZEREL RELEASE RELWITHDEBINFO) + if("${CMAKE_HIP_FLAGS_${_TYPE}}" STREQUAL "") + set(CMAKE_HIP_FLAGS_${_TYPE} "${CMAKE_CXX_FLAGS_${_TYPE}}") + endif() +endforeach() + +if(NOT TARGET rocprofiler::rocprofiler) + find_package(rocprofiler REQUIRED) +endif() + +add_library(counter-collection-client SHARED) +target_sources(counter-collection-client PRIVATE client.cpp client.hpp) +target_link_libraries(counter-collection-client PRIVATE rocprofiler::rocprofiler) + +set_source_files_properties(main.cpp PROPERTIES LANGUAGE HIP) +find_package(Threads REQUIRED) + +add_executable(counter-collection) +target_sources(counter-collection PRIVATE main.cpp) +target_link_libraries(counter-collection PRIVATE counter-collection-client + Threads::Threads) + +add_test(NAME counter-collection COMMAND $) + +set_tests_properties( + counter-collection + PROPERTIES + TIMEOUT + 45 + LABELS + "samples" + ENVIRONMENT + "${ROCPROFILER_MEMCHECK_PRELOAD_ENV};HSA_TOOLS_LIB=$" + ) diff --git a/samples/counter_collection/client.cpp b/samples/counter_collection/client.cpp new file mode 100644 index 0000000000..07cc8da493 --- /dev/null +++ b/samples/counter_collection/client.cpp @@ -0,0 +1,164 @@ +#include "client.hpp" + +#include +#include // std::stringstream +#include + +#include +#include + +#define ROCPROFILER_CALL(result, msg) \ + { \ + rocprofiler_status_t CHECKSTATUS = result; \ + if(CHECKSTATUS != ROCPROFILER_STATUS_SUCCESS) \ + { \ + std::cerr << #result << " failed with error code " << CHECKSTATUS << std::endl; \ + throw std::runtime_error(#result " failure"); \ + } \ + } + +int +start() +{ + return 1; +} + +namespace +{ +rocprofiler_context_id_t& +get_client_ctx() +{ + static rocprofiler_context_id_t ctx; + return ctx; +} + +void +test_callback(rocprofiler_queue_id_t queue_id, + rocprofiler_agent_t agent_id, + rocprofiler_correlation_id_t corr_id, + const hsa_kernel_dispatch_packet_t*, + void*, + rocprofiler_dispatch_profile_counting_record_t**, + size_t, + rocprofiler_profile_config_id_t) +{ + // Callback containing counter data. + std::clog << "[" << __FUNCTION__ << "] " << queue_id.handle << " | " << agent_id.id.handle + << " | " << corr_id.id << "\n"; +} + +int +tool_init(rocprofiler_client_finalize_t, void*) +{ + std::set counters_to_collect = {"SQ_WAVES"}; + + std::vector gpu_agents; + auto agent_query = [](const rocprofiler_agent_t** agents, size_t num_agents, void* user_data) { + std::vector* vec = + static_cast*>(user_data); + for(size_t i = 0; i < num_agents; i++) + { + const rocprofiler_agent_t* agent = agents[i]; + if(agent->type == ROCPROFILER_AGENT_TYPE_GPU) + { + vec->push_back(*agent); + } + } + return ROCPROFILER_STATUS_SUCCESS; + }; + + ROCPROFILER_CALL(rocprofiler_query_available_agents( + agent_query, sizeof(rocprofiler_agent_t), static_cast(&gpu_agents)), + "Could not query agents"); + + ROCPROFILER_CALL(rocprofiler_create_context(&get_client_ctx()), "context creation failed"); + + std::vector profile_configs; + + for(auto& agent : gpu_agents) + { + std::vector collect_counters; + std::vector gpu_counters; + std::clog << agent.name << "\n"; + ROCPROFILER_CALL( + rocprofiler_iterate_agent_supported_counters( + agent, + [](rocprofiler_counter_id_t* counters, size_t num_counters, void* user_data) { + std::vector* vec = + static_cast*>(user_data); + for(size_t i = 0; i < num_counters; i++) + { + vec->push_back(counters[i]); + } + return ROCPROFILER_STATUS_SUCCESS; + }, + static_cast(&gpu_counters)), + "Could not fetch supported counters"); + + for(auto& counter : gpu_counters) + { + const char* name; + size_t size; + ROCPROFILER_CALL(rocprofiler_query_counter_name(counter, &name, &size), + "Could not query name"); + if(counters_to_collect.count(std::string(name)) > 0) + { + std::clog << "Counter: " << counter.handle << " " << name << "\n"; + collect_counters.push_back(counter); + } + } + rocprofiler_profile_config_id_t profile; + ROCPROFILER_CALL(rocprofiler_create_profile_config( + agent, collect_counters.data(), collect_counters.size(), &profile), + "Could not construct profile cfg"); + ROCPROFILER_CALL(rocprofiler_configure_dispatch_profile_counting_service( + get_client_ctx(), profile, test_callback, nullptr), + "Could not setup dispatch service"); + profile_configs.push_back(profile); + } + rocprofiler_start_context(get_client_ctx()); + + // no errors + return 0; +} + +void +tool_fini(void*) +{ + rocprofiler_stop_context(get_client_ctx()); + std::clog << "In tool fini\n"; +} + +} // namespace + +extern "C" rocprofiler_tool_configure_result_t* +rocprofiler_configure(uint32_t version, + const char* runtime_version, + uint32_t, + rocprofiler_client_id_t* id) +{ + // set the client name + id->name = "CounterClientSample"; + + // compute major/minor/patch version info + uint32_t major = version / 10000; + uint32_t minor = (version % 10000) / 100; + uint32_t patch = version % 100; + + // generate info string + auto info = std::stringstream{}; + info << id->name << " is using rocprofiler v" << major << "." << minor << "." << patch << " (" + << runtime_version << ")"; + + std::clog << info.str() << std::endl; + + // create configure data + static auto cfg = + rocprofiler_tool_configure_result_t{sizeof(rocprofiler_tool_configure_result_t), + &tool_init, + &tool_fini, + static_cast(nullptr)}; + + // return pointer to configure data + return &cfg; +} diff --git a/samples/counter_collection/client.hpp b/samples/counter_collection/client.hpp new file mode 100644 index 0000000000..aa0eee0480 --- /dev/null +++ b/samples/counter_collection/client.hpp @@ -0,0 +1,8 @@ +#pragma once + +#include + +#define CLIENT_API __attribute__((visibility("default"))) + +int +start() CLIENT_API; \ No newline at end of file diff --git a/samples/counter_collection/main.cpp b/samples/counter_collection/main.cpp new file mode 100644 index 0000000000..1a456e92ce --- /dev/null +++ b/samples/counter_collection/main.cpp @@ -0,0 +1,92 @@ +#include + +#include "client.hpp" + +#define HIP_CALL(call) \ + do \ + { \ + hipError_t err = call; \ + if(err != hipSuccess) \ + { \ + fprintf(stderr, "%s\n", hipGetErrorString(err)); \ + abort(); \ + } \ + } while(0) + +__global__ void +kernelA(int x, int y) +{ + x = x + y; +} + +__global__ void +kernelB(int x, int y) +{ + x = x + y; +} + +template +__global__ void +kernelC(T* C_d, const T* A_d, size_t N) +{ + size_t offset = (blockIdx.x * blockDim.x + threadIdx.x); + size_t stride = blockDim.x * gridDim.x; + for(size_t i = offset; i < N; i += stride) + { + C_d[i] = A_d[i] * A_d[i]; + } +} + +void +launchKernals() +{ + const int NUM_LAUNCH = 1000; + // Normal HIP Calls + int* gpuMem; + [[maybe_unused]] hipDeviceProp_t devProp; + HIP_CALL(hipGetDeviceProperties(&devProp, 0)); + HIP_CALL(hipMalloc((void**) &gpuMem, 1 * sizeof(int))); + + for(int i = 0; i < NUM_LAUNCH; i++) + { + // KernelA and KernelB to be profiled as part of the session + hipLaunchKernelGGL(kernelA, dim3(1), dim3(1), 0, 0, 1, 2); + hipLaunchKernelGGL(kernelB, dim3(1), dim3(1), 0, 0, 1, 2); + } + + const int NElems = 512 * 512; + const int Nbytes = NElems * 2; + int * A_d, *C_d; + int A_h[NElems], C_h[NElems]; + + for(int i = 0; i < NElems; i++) + { + A_h[i] = i; + } + + HIP_CALL(hipDeviceSynchronize()); + + HIP_CALL(hipMalloc(&A_d, Nbytes)); + HIP_CALL(hipMalloc(&C_d, Nbytes)); + HIP_CALL(hipMemcpy(A_d, A_h, Nbytes, hipMemcpyHostToDevice)); + HIP_CALL(hipDeviceSynchronize()); + const unsigned blocks = 512; + const unsigned threadsPerBlock = 256; + for(int i = 0; i < NUM_LAUNCH; i++) + { + hipLaunchKernelGGL(kernelC, dim3(blocks), dim3(threadsPerBlock), 0, 0, C_d, A_d, NElems); + } + HIP_CALL(hipMemcpy(C_h, C_d, Nbytes, hipMemcpyDeviceToHost)); + HIP_CALL(hipDeviceSynchronize()); + HIP_CALL(hipFree(gpuMem)); + HIP_CALL(hipFree(A_d)); + HIP_CALL(hipFree(C_d)); + std::cerr << "Run complete\n"; +} + +int +main() +{ + start(); + launchKernals(); +} diff --git a/source/include/rocprofiler/counters.h b/source/include/rocprofiler/counters.h index d5e6d0d078..3bd4b33114 100644 --- a/source/include/rocprofiler/counters.h +++ b/source/include/rocprofiler/counters.h @@ -38,37 +38,44 @@ ROCPROFILER_EXTERN_C_INIT * @brief Query Counter name. * * @param [in] counter_id - * @param [out] name if nullptr, size will be returned - * @param [out] size + * @param [out] name returns a pointer to the name of the counter + * @param [out] size returns the size of the name returned * @return ::rocprofiler_status_t */ rocprofiler_status_t ROCPROFILER_API -rocprofiler_query_counter_name(rocprofiler_counter_id_t counter_id, const char* name, size_t* size) - ROCPROFILER_NONNULL(3); +rocprofiler_query_counter_name(rocprofiler_counter_id_t counter_id, const char** name, size_t* size) + ROCPROFILER_NONNULL(2, 3); /** * @brief Query Counter Instances Count. * - * @param [in] counter_id - * @param [out] instance_count + * @param [in] agent rocprofiler agent + * @param [in] counter_id counter id (obtained from iterate_agent_supported_counters) + * @param [out] instance_count number of instances the counter has * @return rocprofiler_status_t */ rocprofiler_status_t ROCPROFILER_API -rocprofiler_query_counter_instance_count(rocprofiler_counter_id_t counter_id, - size_t* instance_count) ROCPROFILER_NONNULL(2); +rocprofiler_query_counter_instance_count(rocprofiler_agent_t agent, + rocprofiler_counter_id_t counter_id, + size_t* instance_count) ROCPROFILER_NONNULL(3); + +typedef rocprofiler_status_t (*rocprofiler_available_counters_cb_t)( + rocprofiler_counter_id_t* counters, + size_t num_counters, + void* user_data); /** * @brief Query Agent Counters Availability. * * @param [in] agent - * @param [out] counters_list - * @param [out] counters_count + * @param [in] cb callback to caller to get counters + * @param [in] user_data data to pass into the callback * @return ::rocprofiler_status_t */ rocprofiler_status_t ROCPROFILER_API -rocprofiler_query_agent_supported_counters(rocprofiler_agent_t agent, - rocprofiler_counter_id_t* counters_list, - size_t* counters_count) ROCPROFILER_NONNULL(2, 3); +rocprofiler_iterate_agent_supported_counters(rocprofiler_agent_t agent, + rocprofiler_available_counters_cb_t cb, + void* user_data) ROCPROFILER_NONNULL(2); /** @} */ diff --git a/source/include/rocprofiler/dispatch_profile.h b/source/include/rocprofiler/dispatch_profile.h index 9f495624b9..236883c09b 100644 --- a/source/include/rocprofiler/dispatch_profile.h +++ b/source/include/rocprofiler/dispatch_profile.h @@ -69,28 +69,28 @@ typedef struct * @param [in] config */ typedef void (*rocprofiler_profile_counting_dispatch_callback_t)( - rocprofiler_queue_id_t queue_id, - rocprofiler_agent_t agent_id, - rocprofiler_correlation_id_t correlation_id, - const hsa_kernel_dispatch_packet_t* dispatch_packet, - void* callback_data_args, - rocprofiler_profile_config_id_t* config); + rocprofiler_queue_id_t queue_id, + rocprofiler_agent_t agent_id, + rocprofiler_correlation_id_t correlation_id, + const hsa_kernel_dispatch_packet_t* dispatch_packet, + void* callback_data_args, + rocprofiler_dispatch_profile_counting_record_t** records, + size_t record_count, + rocprofiler_profile_config_id_t config); /** * @brief Configure Dispatch Profile Counting Service. * - * @param [in] context_id - * @param [in] agent_id - * @param [in] buffer_id - * @param [in] callback - * @param [in] callback_data_args + * @param [in] context_id context id + * @param [in] profile profile config to use for dispatch + * @param [in] callback callback + * @param [in] callback_data_args callback data * @return ::rocprofiler_status_t */ rocprofiler_status_t ROCPROFILER_API rocprofiler_configure_dispatch_profile_counting_service( rocprofiler_context_id_t context_id, - rocprofiler_agent_t agent_id, - rocprofiler_buffer_id_t buffer_id, + rocprofiler_profile_config_id_t profile, rocprofiler_profile_counting_dispatch_callback_t callback, void* callback_data_args); diff --git a/source/include/rocprofiler/fwd.h b/source/include/rocprofiler/fwd.h index 237a38eedc..01cc0a39ab 100644 --- a/source/include/rocprofiler/fwd.h +++ b/source/include/rocprofiler/fwd.h @@ -69,6 +69,8 @@ typedef enum // NOLINT(performance-enum-size) ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED, ///< Function is not implemented ROCPROFILER_STATUS_ERROR_INCOMPATIBLE_ABI, ///< Data structure provided by user is incompatible ///< with current version of rocprofiler + ROCPROFILER_STATUS_ERROR_AGENT_NOT_FOUND, ///< Agent not found + ROCPROFILER_STATUS_ERROR_COUNTER_NOT_FOUND, ///< Counter does not exist ROCPROFILER_STATUS_LAST, } rocprofiler_status_t; diff --git a/source/lib/common/CMakeLists.txt b/source/lib/common/CMakeLists.txt index c152619de0..8cb86e34d6 100644 --- a/source/lib/common/CMakeLists.txt +++ b/source/lib/common/CMakeLists.txt @@ -3,9 +3,9 @@ # rocprofiler_activate_clang_tidy() -set(common_sources config.cpp environment.cpp demangle.cpp utility.cpp) +set(common_sources config.cpp environment.cpp demangle.cpp utility.cpp xml.cpp) set(common_headers config.hpp defines.hpp environment.hpp demangle.hpp mpl.hpp - utility.hpp xml.hpp) + synchronized.hpp utility.hpp xml.hpp) add_library(rocprofiler-common-library STATIC) add_library(rocprofiler::rocprofiler-common-library ALIAS rocprofiler-common-library) diff --git a/source/lib/common/demangle.hpp b/source/lib/common/demangle.hpp index d71689c8aa..6fa7631a6c 100644 --- a/source/lib/common/demangle.hpp +++ b/source/lib/common/demangle.hpp @@ -1,22 +1,22 @@ -/* Copyright (c) 2022 Advanced Micro Devices, Inc. - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in 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: - - The above copyright notice and this permission notice shall be included in - all copies or substantial portions of the Software. - - 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 - AUTHORS 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 IN - THE SOFTWARE. */ +// Copyright (c) 2023 Advanced Micro Devices, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in 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: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// 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 +// AUTHORS 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 IN +// THE SOFTWARE. #pragma once diff --git a/source/lib/common/synchronized.hpp b/source/lib/common/synchronized.hpp new file mode 100644 index 0000000000..e688ad89cc --- /dev/null +++ b/source/lib/common/synchronized.hpp @@ -0,0 +1,96 @@ +// MIT License +// +// Copyright (c) 2023 ROCm Developer Tools +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in 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: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// 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 +// AUTHORS 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 IN THE +// SOFTWARE. + +#pragma once + +#include +#include +#include +#include + +namespace rocprofiler +{ +namespace common +{ +/** + * Sychronized is a wrapper that adds lock based write/read + * protection around a datatype. The protected data is accessed + * only by rlock/wlock. rlock(lambda) gets a reader lock of the + * protected value, passing the protected value to the lambda as a + * const. wlock(lambda) gets a writer lock on the protective value + * and does the same. The reason for this class is to make it less + * error prone to access shared data and more obvious when a lock + * is being held. + * + * Example usage: + * + * Synchronized x(9); + * x.rlock([](const auto& data){ + * // data = 9 + * }); + * + * x.wlock([](auto& data){ + * // set data to new value + * }); + */ +template +class Synchronized +{ +public: + Synchronized() = default; + Synchronized(LockedType&& data) + : data_(std::move(data)) + {} + // Do not allow this data structure to be copied, std::move only. + Synchronized(const Synchronized&) = delete; + + void rlock(std::function lambda) const + { + std::shared_lock lock(mutex_); + lambda(data_); + } + + void wlock(std::function lambda) + { + std::unique_lock lock(mutex_); + lambda(data_); + } + + // Upgradable lock. If read returns false, write will be called with a unique_lock. + // Essentially a helper function that does .rlock() followed by .wlock(). + void ulock(std::function read, std::function write) + { + { + std::shared_lock lock(mutex_); + if(read(data_)) return; + } + + std::unique_lock lock(mutex_); + write(data_); + } + +private: + mutable std::shared_mutex mutex_; + LockedType data_; +}; +} // namespace common +} // namespace rocprofiler diff --git a/source/lib/common/utility.hpp b/source/lib/common/utility.hpp index e358ae53e5..20361086f5 100644 --- a/source/lib/common/utility.hpp +++ b/source/lib/common/utility.hpp @@ -66,5 +66,36 @@ get_val(Container& map, const Key& key) auto pos = map.find(key); return (pos != map.end() ? &pos->second : nullptr); } + +/** + * A simple wrapper that will call a function when the + * wrapper is being destroyed. This is primarily useful + * for static variables where we want to run some destruction + * operations when the program exits. + */ +template +class static_cleanup_wrapper +{ +public: + static_cleanup_wrapper(T&& data, L&& destroy_func) + : _data(std::move(data)) + , _destroy_func(destroy_func) + {} + + static_cleanup_wrapper(L&& destroy_func) + : _destroy_func(destroy_func) + {} + + ~static_cleanup_wrapper() { _destroy_func(_data); } + + void destroy() { _destroy_func(_data); } + + T& get() { return _data; } + +private: + T _data; + L _destroy_func; +}; + } // namespace common } // namespace rocprofiler diff --git a/source/lib/common/xml.cpp b/source/lib/common/xml.cpp new file mode 100644 index 0000000000..31fab68494 --- /dev/null +++ b/source/lib/common/xml.cpp @@ -0,0 +1,529 @@ +// Copyright (c) 2023 Advanced Micro Devices, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in 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: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// 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 +// AUTHORS 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 IN +// THE SOFTWARE. + +#include "lib/common/xml.hpp" + +#include + +namespace rocprofiler +{ +namespace common +{ +Xml::Xml(std::string file_name, const Xml* obj) +: file_name_(std::move(file_name)) +, state_(BODY_STATE) +{ + if(obj != nullptr) + { + map_ = obj->map_; + level_ = obj->level_; + included_ = true; + } +} + +Xml::~Xml() +{ + for(auto& x : stack_) + { + x->nodes.clear(); + x->copy.reset(); + } + if(!map_) return; + for(auto& [_, nodes] : *map_) + { + for(auto& node : nodes) + { + node->nodes.clear(); + node->copy.reset(); + } + } +} + +std::shared_ptr +Xml::Create(const std::string& file_name, const Xml* obj) +{ + auto xml = std::make_shared(file_name, obj); + if(xml != nullptr) + { + if(xml->Init() != false) + { + const std::size_t pos = file_name.rfind('/'); + const std::string path = (pos != std::string::npos) ? file_name.substr(0, pos + 1) : ""; + + xml->PreProcess(); + nodes_t incl_nodes; + for(const auto& node : xml->GetNodes("top.include")) + { + if(node->opts.find("touch") == node->opts.end()) + { + node->opts["touch"] = ""; + incl_nodes.push_back(node); + } + } + for(const auto& incl : incl_nodes) + { + const std::string& incl_name = path + incl->opts["file"]; + auto ixml = Create(incl_name, xml.get()); + if(!ixml) + { + xml.reset(); + break; + } + } + if(xml) + { + xml->Process(); + } + } + } + + return xml; +} + +void +Xml::AddExpr(const std::string& full_tag, const std::string& name, const std::string& expr) +{ + const std::size_t pos = full_tag.rfind('.'); + const std::size_t pos1 = (pos == std::string::npos) ? 0 : pos + 1; + const std::string level_tag = full_tag.substr(pos1); + auto level = std::make_shared(); + (*map_)[full_tag].push_back(level); + level->tag = level_tag; + level->opts["name"] = name; + level->opts["expr"] = expr; +} + +void +Xml::AddConst(const std::string& full_tag, const std::string& name, const uint64_t& val) +{ + std::ostringstream oss; + oss << val; + AddExpr(full_tag, name, oss.str()); +} + +bool +Xml::print_func::operator()(const std::string& global_tag, const std::shared_ptr& node) +{ + std::cout << global_tag << ":\n"; + for(auto& opt : node->opts) + { + std::cout << global_tag << "." << opt.first << " = " << opt.second << "\n"; + } + return true; +} + +void +Xml::Print() const +{ + std::cout << "XML file '" << file_name_ << "':\n"; + ForEach(print_func{}); +} + +bool +Xml::Init() +{ + fd_ = open(file_name_.c_str(), O_RDONLY); + if(fd_ == -1) + { + // perror((std::string("open XML file ") + file_name_).c_str()); + return false; + } + + if(map_ == nullptr) + { + map_ = std::make_unique(); + AddLevel("top"); + } + + return true; +} + +void +Xml::PreProcess() +{ + uint32_t ind = 0; + char buf[kBufSize]; + bool error = false; + + while(true) + { + const uint32_t pos = lseek(fd_, 0, SEEK_CUR); + uint32_t size = read(fd_, buf, kBufSize); + if(size <= 0) break; + buf[size - 1] = '\0'; + + if(strncmp(buf, "#include \"", 10) == 0) + { + for(ind = 0; (ind < size) && (buf[ind] != '\n'); ++ind) + {} + if(ind < size) + { + buf[ind] = '\0'; + size = ind; + lseek(fd_, pos + ind + 1, SEEK_SET); + } + + for(ind = 10; (ind < size) && (buf[ind] != '"'); ++ind) + {} + if(ind == size) + { + error = true; + break; + } + buf[ind] = '\0'; + + AddLevel("include"); + AddOption("file", &buf[10]); + UpLevel(); + } + } + + if(error) + { + fprintf(stderr, "XML PreProcess failed, line '%s'\n", buf); + abort(); + } + + lseek(fd_, 0, SEEK_SET); +} + +void +Xml::Process() +{ + token_t remainder; + + while(true) + { + token_t token = (!remainder.empty()) ? remainder : NextToken(); + remainder.clear(); + + // token_t token1 = token; + // token1.push_back('\0'); + // std::cout << ">>> " << &token1[0] << std::endl; + + // End of file + if(token.empty()) break; + + switch(state_) + { + case BODY_STATE: + if(token[0] == '<') + { + bool node_begin = true; + unsigned ind = 1; + if(token[1] == '/') + { + node_begin = false; + ++ind; + } + + unsigned i = ind; + while(i < token.size()) + { + if(token[i] == '>') break; + ++i; + } + for(unsigned j = i + 1; j < token.size(); ++j) + remainder.push_back(token[j]); + + if(i == token.size()) + { + if(node_begin) + state_ = DECL_STATE; + else + BadFormat(token); + token.push_back('\0'); + } + else + { + token[i] = '\0'; + } + + const char* tag = &token[ind]; + if(node_begin) + { + AddLevel(tag); + } + else + { + Inherit(GetOption("base")); + + if(strncmp(CurrentLevel().c_str(), tag, strlen(tag)) != 0) + { + token.back() = '>'; + BadFormat(token); + } + UpLevel(); + } + } + else + { + BadFormat(token); + } + break; + case DECL_STATE: + if(token[0] == '>') + { + state_ = BODY_STATE; + for(unsigned j = 1; j < token.size(); ++j) + remainder.push_back(token[j]); + continue; + } + else + { + token.push_back('\0'); + unsigned j = 0; + for(j = 0; j < token.size(); ++j) + if(token[j] == '=') break; + if(j == token.size()) BadFormat(token); + token[j] = '\0'; + const std::string key = token.data(); + const std::string value = &token[j + 1]; + AddOption(key, value); + } + break; + default: + { + LOG(ERROR) << "XML parser error: wrong state: " << state_; + abort(); + } + } + } +} + +bool +Xml::SpaceCheck() const +{ + bool cond = ((buffer_[index_] == ' ') || (buffer_[index_] == '\t')); + return cond; +} + +bool +Xml::LineEndCheck() +{ + bool found = false; + if(buffer_[index_] == '\n') + { + buffer_[index_] = ' '; + ++file_line_; + found = true; + comment_ = false; + } + else if(comment_ || (buffer_[index_] == '#')) + { + found = true; + comment_ = true; + } + return found; +} + +Xml::token_t +Xml::NextToken() +{ + token_t token; + bool in_string = false; + bool special_symb = false; + + while(true) + { + if(data_size_ == 0) + { + data_size_ = read(fd_, buffer_, kBufSize); + if(data_size_ <= 0) break; + } + + if(token.empty()) + { + while((index_ < data_size_) && (SpaceCheck() || LineEndCheck())) + { + ++index_; + } + } + while((index_ < data_size_) && (in_string || !(SpaceCheck() || LineEndCheck()))) + { + const char symb = buffer_[index_]; + bool skip_symb = false; + + switch(symb) + { + case '\\': + if(special_symb) + { + special_symb = false; + } + else + { + special_symb = true; + skip_symb = true; + } + break; + case '"': + if(special_symb) + { + special_symb = false; + } + else + { + in_string = !in_string; + if(!in_string) + { + buffer_[index_] = ' '; + --index_; + } + skip_symb = true; + } + break; + } + + if(!skip_symb) token.push_back(symb); + ++index_; + } + + if(index_ == data_size_) + { + index_ = 0; + data_size_ = 0; + } + else + { + if(special_symb || in_string) BadFormat(token); + break; + } + } + + return token; +} + +void +Xml::BadFormat(token_t token) +{ + token.push_back('\0'); + LOG(ERROR) << "Error: " << file_name_ << ", line " << file_line_ << ", bad XML token '" + << token.data() << "'"; + abort(); +} + +void +Xml::AddLevel(const std::string& tag) +{ + auto level = std::make_shared(); + level->tag = tag; + if(level_) + { + level_->nodes.push_back(level); + stack_.push_back(level_); + } + level_ = level; + + std::string global_tag = GlobalTag(tag); + (*map_)[global_tag].push_back(level_); +} + +void +Xml::UpLevel() +{ + level_ = stack_.back(); + stack_.pop_back(); +} + +void +Xml::Copy(const std::shared_ptr& from, const std::shared_ptr& to) +{ + auto level = to; + if(level == nullptr) + { + AddLevel(from->tag); + level = level_; + } + level->copy = from; + level->opts = from->opts; + + for(const auto& node : from->nodes) + { + bool found = false; + const std::string name = GetOption("name", node); + const std::string global_tag = GlobalTag(level->tag) + "." + node->tag; + for(const auto& item : (*map_)[global_tag]) + { + if((name == GetOption("name", item)) || (node == item->copy)) + { + found = true; + break; + } + } + if(found == false) Copy(node, nullptr); + } + + if(to == nullptr) UpLevel(); +} + +void +Xml::Inherit(const std::string& tag) +{ + if(!tag.empty()) + { + const std::string global_tag = GlobalTag(tag); + auto it = map_->find(global_tag); + if(it == map_->end()) + { + fprintf( + stderr, "Node \"%s\": Base not found \"%s\"\n", level_->tag.c_str(), tag.c_str()); + abort(); + } + for(const auto& node : it->second) + { + Copy(node, level_); + } + } +} + +std::string +Xml::CurrentLevel() const +{ + return level_->tag; +} + +std::string +Xml::GlobalTag(const std::string& tag) const +{ + std::string global_tag; + for(const auto& level : stack_) + { + global_tag += level->tag + "."; + } + global_tag += tag; + return global_tag; +} + +void +Xml::AddOption(const std::string& key, const std::string& value) +{ + level_->opts[key] = value; +} + +std::string +Xml::GetOption(const std::string& key, std::shared_ptr level) +{ + level = (level != nullptr) ? level : level_; + auto it = level->opts.find(key); + return (it != level->opts.end()) ? it->second : ""; +} +} // namespace common +} // namespace rocprofiler diff --git a/source/lib/common/xml.hpp b/source/lib/common/xml.hpp index d07e371f70..3abf0c6af9 100644 --- a/source/lib/common/xml.hpp +++ b/source/lib/common/xml.hpp @@ -1,24 +1,22 @@ -/****************************************************************************** -Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in 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: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -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 -AUTHORS 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 IN -THE SOFTWARE. -*******************************************************************************/ +// Copyright (c) 2023 Advanced Micro Devices, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in 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: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// 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 +// AUTHORS 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 IN +// THE SOFTWARE. #pragma once @@ -34,10 +32,13 @@ THE SOFTWARE. #include #include #include +#include #include #include -namespace xml +namespace rocprofiler +{ +namespace common { class Xml { @@ -66,525 +67,45 @@ public: BODY_STATE }; - static std::shared_ptr Create(const std::string& file_name, const Xml* obj = nullptr) - { - auto xml = std::make_shared(file_name, obj); - if(xml != nullptr) - { - if(xml->Init() != false) - { - const std::size_t pos = file_name.rfind('/'); - const std::string path = - (pos != std::string::npos) ? file_name.substr(0, pos + 1) : ""; - - xml->PreProcess(); - nodes_t incl_nodes; - for(const auto& node : xml->GetNodes("top.include")) - { - if(node->opts.find("touch") == node->opts.end()) - { - node->opts["touch"] = ""; - incl_nodes.push_back(node); - } - } - for(const auto& incl : incl_nodes) - { - const std::string& incl_name = path + incl->opts["file"]; - auto ixml = Create(incl_name, xml.get()); - if(!ixml) - { - xml.reset(); - break; - } - } - if(xml) - { - xml->Process(); - } - } - } - - return xml; - } + static std::shared_ptr Create(const std::string& file_name, const Xml* obj = nullptr); std::string GetName() { return file_name_; } - // clang-tidy incorrectly marks these functions as being staticable. They are not. - // NOLINTBEGIN - void AddExpr(const std::string& full_tag, const std::string& name, const std::string& expr) - { - const std::size_t pos = full_tag.rfind('.'); - const std::size_t pos1 = (pos == std::string::npos) ? 0 : pos + 1; - const std::string level_tag = full_tag.substr(pos1); - auto level = std::make_shared(); - (*map_)[full_tag].push_back(level); - level->tag = level_tag; - level->opts["name"] = name; - level->opts["expr"] = expr; - } - - void AddConst(const std::string& full_tag, const std::string& name, const uint64_t& val) - { - std::ostringstream oss; - oss << val; - AddExpr(full_tag, name, oss.str()); - } - // NOLINTEND + void AddExpr(const std::string& full_tag, const std::string& name, const std::string& expr); + void AddConst(const std::string& full_tag, const std::string& name, const uint64_t& val); nodes_t GetNodes(const std::string& global_tag) { return (*map_)[global_tag]; } const map_t& GetAllNodes() { return (*map_); } - template - F ForEach(const F& f_i) - { - F f = f_i; - if(map_) - { - for(auto& entry : *map_) - { - for(auto node : entry.second) - { - if(f.fun(entry.first, node) == false) break; - } - } - } - return f; - } - - template - F ForEach(const F& f_i) const - { - F f = f_i; - if(map_) - { - for(auto& entry : *map_) - { - for(const auto& node : entry.second) - { - if(f.fun(entry.first, node) == false) break; - } - } - } - return f; - } + template + Tp ForEach(const Tp& v_i) const; struct print_func { - static bool fun(const std::string& global_tag, const std::shared_ptr& node) - { - std::cout << global_tag << ":" << std::endl; - for(auto& opt : node->opts) - { - std::cout << global_tag << "." << opt.first << " = " << opt.second << std::endl; - } - return true; - } + bool operator()(const std::string& global_tag, const std::shared_ptr& node); }; - void Print() const - { - std::cout << "XML file '" << file_name_ << "':" << std::endl; - ForEach(print_func()); - } + void Print() const; - Xml(std::string file_name, const Xml* obj) - : file_name_(std::move(file_name)) - , state_(BODY_STATE) - { - if(obj != nullptr) - { - map_ = obj->map_; - level_ = obj->level_; - included_ = true; - } - } - - ~Xml() - { - for(auto& x : stack_) - { - x->nodes.clear(); - x->copy.reset(); - } - if(!map_) return; - for(auto& [_, nodes] : *map_) - { - for(auto& node : nodes) - { - node->nodes.clear(); - node->copy.reset(); - } - } - } + Xml(std::string file_name, const Xml* obj); + ~Xml(); private: - bool Init() - { - fd_ = open(file_name_.c_str(), O_RDONLY); - if(fd_ == -1) - { - // perror((std::string("open XML file ") + file_name_).c_str()); - return false; - } - - if(map_ == nullptr) - { - map_ = std::make_unique(); - AddLevel("top"); - } - - return true; - } - - void PreProcess() - { - uint32_t ind = 0; - char buf[kBufSize]; - bool error = false; - - while(true) - { - const uint32_t pos = lseek(fd_, 0, SEEK_CUR); - uint32_t size = read(fd_, buf, kBufSize); - if(size <= 0) break; - buf[size - 1] = '\0'; - - if(strncmp(buf, "#include \"", 10) == 0) - { - for(ind = 0; (ind < size) && (buf[ind] != '\n'); ++ind) - {} - if(ind < size) - { - buf[ind] = '\0'; - size = ind; - lseek(fd_, pos + ind + 1, SEEK_SET); - } - - for(ind = 10; (ind < size) && (buf[ind] != '"'); ++ind) - {} - if(ind == size) - { - error = true; - break; - } - buf[ind] = '\0'; - - AddLevel("include"); - AddOption("file", &buf[10]); - UpLevel(); - } - } - - if(error) - { - fprintf(stderr, "XML PreProcess failed, line '%s'\n", buf); - abort(); - } - - lseek(fd_, 0, SEEK_SET); - } - - void Process() - { - token_t remainder; - - while(true) - { - token_t token = (!remainder.empty()) ? remainder : NextToken(); - remainder.clear(); - - // token_t token1 = token; - // token1.push_back('\0'); - // std::cout << ">>> " << &token1[0] << std::endl; - - // End of file - if(token.empty()) break; - - switch(state_) - { - case BODY_STATE: - if(token[0] == '<') - { - bool node_begin = true; - unsigned ind = 1; - if(token[1] == '/') - { - node_begin = false; - ++ind; - } - - unsigned i = ind; - while(i < token.size()) - { - if(token[i] == '>') break; - ++i; - } - for(unsigned j = i + 1; j < token.size(); ++j) - remainder.push_back(token[j]); - - if(i == token.size()) - { - if(node_begin) - state_ = DECL_STATE; - else - BadFormat(token); - token.push_back('\0'); - } - else - { - token[i] = '\0'; - } - - const char* tag = &token[ind]; - if(node_begin) - { - AddLevel(tag); - } - else - { - Inherit(GetOption("base")); - - if(strncmp(CurrentLevel().c_str(), tag, strlen(tag)) != 0) - { - token.back() = '>'; - BadFormat(token); - } - UpLevel(); - } - } - else - { - BadFormat(token); - } - break; - case DECL_STATE: - if(token[0] == '>') - { - state_ = BODY_STATE; - for(unsigned j = 1; j < token.size(); ++j) - remainder.push_back(token[j]); - continue; - } - else - { - token.push_back('\0'); - unsigned j = 0; - for(j = 0; j < token.size(); ++j) - if(token[j] == '=') break; - if(j == token.size()) BadFormat(token); - token[j] = '\0'; - const std::string key = token.data(); - const std::string value = &token[j + 1]; - AddOption(key, value); - } - break; - default: - std::cout << "XML parser error: wrong state: " << state_ << std::endl; - abort(); - } - } - } - - bool SpaceCheck() const - { - bool cond = ((buffer_[index_] == ' ') || (buffer_[index_] == '\t')); - return cond; - } - - bool LineEndCheck() - { - bool found = false; - if(buffer_[index_] == '\n') - { - buffer_[index_] = ' '; - ++file_line_; - found = true; - comment_ = false; - } - else if(comment_ || (buffer_[index_] == '#')) - { - found = true; - comment_ = true; - } - return found; - } - - token_t NextToken() - { - token_t token; - bool in_string = false; - bool special_symb = false; - - while(true) - { - if(data_size_ == 0) - { - data_size_ = read(fd_, buffer_, kBufSize); - if(data_size_ <= 0) break; - } - - if(token.empty()) - { - while((index_ < data_size_) && (SpaceCheck() || LineEndCheck())) - { - ++index_; - } - } - while((index_ < data_size_) && (in_string || !(SpaceCheck() || LineEndCheck()))) - { - const char symb = buffer_[index_]; - bool skip_symb = false; - - switch(symb) - { - case '\\': - if(special_symb) - { - special_symb = false; - } - else - { - special_symb = true; - skip_symb = true; - } - break; - case '"': - if(special_symb) - { - special_symb = false; - } - else - { - in_string = !in_string; - if(!in_string) - { - buffer_[index_] = ' '; - --index_; - } - skip_symb = true; - } - break; - } - - if(!skip_symb) token.push_back(symb); - ++index_; - } - - if(index_ == data_size_) - { - index_ = 0; - data_size_ = 0; - } - else - { - if(special_symb || in_string) BadFormat(token); - break; - } - } - - return token; - } - - void BadFormat(token_t token) - { - token.push_back('\0'); - std::cout << "Error: " << file_name_ << ", line " << file_line_ << ", bad XML token '" - << token.data() << "'" << std::endl; - abort(); - } - - void AddLevel(const std::string& tag) - { - auto level = std::make_shared(); - level->tag = tag; - if(level_) - { - level_->nodes.push_back(level); - stack_.push_back(level_); - } - level_ = level; - - std::string global_tag = GlobalTag(tag); - (*map_)[global_tag].push_back(level_); - } - - void UpLevel() - { - level_ = stack_.back(); - stack_.pop_back(); - } - - void Copy(const std::shared_ptr& from, const std::shared_ptr& to) - { - auto level = to; - if(level == nullptr) - { - AddLevel(from->tag); - level = level_; - } - level->copy = from; - level->opts = from->opts; - - for(const auto& node : from->nodes) - { - bool found = false; - const std::string name = GetOption("name", node); - const std::string global_tag = GlobalTag(level->tag) + "." + node->tag; - for(const auto& item : (*map_)[global_tag]) - { - if((name == GetOption("name", item)) || (node == item->copy)) - { - found = true; - break; - } - } - if(found == false) Copy(node, nullptr); - } - - if(to == nullptr) UpLevel(); - } - - void Inherit(const std::string& tag) - { - if(!tag.empty()) - { - const std::string global_tag = GlobalTag(tag); - auto it = map_->find(global_tag); - if(it == map_->end()) - { - fprintf(stderr, - "Node \"%s\": Base not found \"%s\"\n", - level_->tag.c_str(), - tag.c_str()); - abort(); - } - for(const auto& node : it->second) - { - Copy(node, level_); - } - } - } - - std::string CurrentLevel() const { return level_->tag; } - - std::string GlobalTag(const std::string& tag) const - { - std::string global_tag; - for(const auto& level : stack_) - { - global_tag += level->tag + "."; - } - global_tag += tag; - return global_tag; - } - - void AddOption(const std::string& key, const std::string& value) { level_->opts[key] = value; } - std::string GetOption(const std::string& key, std::shared_ptr level = nullptr) - { - level = (level != nullptr) ? level : level_; - auto it = level->opts.find(key); - return (it != level->opts.end()) ? it->second : ""; - } + bool Init(); + void PreProcess(); + void Process(); + bool SpaceCheck() const; + bool LineEndCheck(); + token_t NextToken(); + void BadFormat(token_t token); + void AddLevel(const std::string& tag); + void UpLevel(); + void Copy(const std::shared_ptr& from, const std::shared_ptr& to); + void Inherit(const std::string& tag); + std::string CurrentLevel() const; + std::string GlobalTag(const std::string& tag) const; + void AddOption(const std::string& key, const std::string& value); + std::string GetOption(const std::string& key, std::shared_ptr level = nullptr); const std::string file_name_; unsigned file_line_{0}; @@ -603,4 +124,22 @@ private: std::shared_ptr map_; }; -} // namespace xml +template +Tp +Xml::ForEach(const Tp& v_i) const +{ + Tp v = v_i; + if(map_) + { + for(auto& entry : *map_) + { + for(const auto& node : entry.second) + { + if(Tp{}(entry.first, node) == false) break; + } + } + } + return v; +} +} // namespace common +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/CMakeLists.txt b/source/lib/rocprofiler/CMakeLists.txt index 36a67de263..2dba956d81 100644 --- a/source/lib/rocprofiler/CMakeLists.txt +++ b/source/lib/rocprofiler/CMakeLists.txt @@ -10,8 +10,11 @@ set(ROCPROFILER_LIB_SOURCES buffer_tracing.cpp callback_tracing.cpp context.cpp + counters.cpp + dispatch_profile.cpp internal_threading.cpp pc_sampling.cpp + profile_config.cpp rocprofiler.cpp registration.cpp) @@ -31,6 +34,7 @@ target_sources(rocprofiler-object-library PRIVATE ${ROCPROFILER_LIB_SOURCES} add_subdirectory(hsa) add_subdirectory(context) add_subdirectory(counters) +add_subdirectory(aql) target_link_libraries( rocprofiler-object-library @@ -59,9 +63,13 @@ target_link_libraries( rocprofiler-shared-library INTERFACE rocprofiler::rocprofiler-headers rocprofiler::rocprofiler-hsa-runtime rocprofiler::rocprofiler-hip - PRIVATE rocprofiler::rocprofiler-build-flags rocprofiler::rocprofiler-memcheck - rocprofiler::rocprofiler-common-library rocprofiler::rocprofiler-stdcxxfs - rocprofiler::rocprofiler-dl rocprofiler::rocprofiler-amd-comgr) + PRIVATE rocprofiler::rocprofiler-build-flags + rocprofiler::rocprofiler-memcheck + rocprofiler::rocprofiler-common-library + rocprofiler::rocprofiler-stdcxxfs + rocprofiler::rocprofiler-dl + rocprofiler::rocprofiler-amd-comgr + rocprofiler::rocprofiler-object-library) set_target_properties( rocprofiler-shared-library @@ -97,7 +105,8 @@ target_link_libraries( rocprofiler-static-library PUBLIC rocprofiler::rocprofiler-headers rocprofiler::rocprofiler-hsa-runtime rocprofiler::rocprofiler-hip - PRIVATE rocprofiler::rocprofiler-common-library) + PRIVATE rocprofiler::rocprofiler-common-library + rocprofiler::rocprofiler-object-library) set_target_properties( rocprofiler-static-library PROPERTIES OUTPUT_NAME rocprofiler64 DEFINE_SYMBOL diff --git a/source/lib/rocprofiler/aql/CMakeLists.txt b/source/lib/rocprofiler/aql/CMakeLists.txt new file mode 100644 index 0000000000..890aeec5b4 --- /dev/null +++ b/source/lib/rocprofiler/aql/CMakeLists.txt @@ -0,0 +1,9 @@ +set(ROCPROFILER_LIB_AQL_SOURCES helpers.cpp packet_construct.cpp) +set(ROCPROFILER_LIB_AQL_HEADERS helpers.hpp packet_construct.hpp) + +target_sources(rocprofiler-object-library PRIVATE ${ROCPROFILER_LIB_AQL_SOURCES} + ${ROCPROFILER_LIB_AQL_HEADERS}) + +if(ROCPROFILER_BUILD_TESTS) + add_subdirectory(tests) +endif() diff --git a/source/lib/rocprofiler/aql/helpers.cpp b/source/lib/rocprofiler/aql/helpers.cpp new file mode 100644 index 0000000000..735462edfd --- /dev/null +++ b/source/lib/rocprofiler/aql/helpers.cpp @@ -0,0 +1,43 @@ +#include "lib/rocprofiler/aql/helpers.hpp" + +#include +#include + +namespace rocprofiler +{ +namespace aql +{ +hsa_ven_amd_aqlprofile_id_query_t +get_query_info(hsa_agent_t agent, const counters::Metric& metric) +{ + DLOG(WARNING) << fmt::format("Querying HSA for Counter: {}", metric); + + hsa_ven_amd_aqlprofile_profile_t profile{.agent = agent}; + hsa_ven_amd_aqlprofile_id_query_t query = {metric.block().c_str(), 0, 0}; + if(hsa_ven_amd_aqlprofile_get_info(&profile, HSA_VEN_AMD_AQLPROFILE_INFO_BLOCK_ID, &query) != + HSA_STATUS_SUCCESS) + { + throw std::runtime_error(fmt::format("AQL failed to query info for counter {}", metric)); + } + return query; +} + +uint32_t +get_block_counters(hsa_agent_t agent, const hsa_ven_amd_aqlprofile_event_t& event) +{ + hsa_ven_amd_aqlprofile_profile_t query = {.agent = agent, + .type = HSA_VEN_AMD_AQLPROFILE_EVENT_TYPE_PMC, + .events = &event, + .event_count = 1}; + uint32_t max_block_counters = 0; + if(hsa_ven_amd_aqlprofile_get_info(&query, + HSA_VEN_AMD_AQLPROFILE_INFO_BLOCK_COUNTERS, + &max_block_counters) != HSA_STATUS_SUCCESS) + { + throw std::runtime_error(fmt::format("AQL failed to max block info for counter {}", + static_cast(event.block_name))); + } + return max_block_counters; +} +} // namespace aql +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/aql/helpers.hpp b/source/lib/rocprofiler/aql/helpers.hpp new file mode 100644 index 0000000000..d36d72c0e0 --- /dev/null +++ b/source/lib/rocprofiler/aql/helpers.hpp @@ -0,0 +1,22 @@ + +#pragma once + +#include + +#include + +#include "lib/rocprofiler/counters/metrics.hpp" + +namespace rocprofiler +{ +namespace aql +{ +// Query HSA_VEN_AMD_AQLPROFILE_INFO_BLOCK_ID from aqlprofile +hsa_ven_amd_aqlprofile_id_query_t +get_query_info(hsa_agent_t agent, const counters::Metric& metric); + +// Query HSA_VEN_AMD_AQLPROFILE_INFO_BLOCK_COUNTERS from aqlprofiler +uint32_t +get_block_counters(hsa_agent_t agent, const hsa_ven_amd_aqlprofile_event_t& event); +} // namespace aql +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/aql/intercept.cpp b/source/lib/rocprofiler/aql/intercept.cpp new file mode 100644 index 0000000000..6dd64b5025 --- /dev/null +++ b/source/lib/rocprofiler/aql/intercept.cpp @@ -0,0 +1,23 @@ +#include "lib/rocprofiler/aql/intercept.hpp" + +#include "lib/rocprofiler/hsa/hsa.hpp" + +namespace rocprofiler +{ +namespace aql +{ +std::shared_ptr +Intercept::create(const std::function& mod_cb) +{ + return std::make_shared(mod_cb); +} + +Intercept::Intercept(const std::function& mod_cb) +: _original(rocprofiler::hsa::get_table()) +, _modified(rocprofiler::hsa::get_table()) +{ + mod_cb(_modified); +}; + +} // namespace aql +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/aql/intercept.hpp b/source/lib/rocprofiler/aql/intercept.hpp new file mode 100644 index 0000000000..b4dc6a1b78 --- /dev/null +++ b/source/lib/rocprofiler/aql/intercept.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +#include +#include +#include + +namespace rocprofiler +{ +namespace aql +{ +class Intercept +{ +public: + static std::shared_ptr create(const std::function& mod_cb); + + explicit Intercept(const std::function& mod_cb); + +private: + HsaApiTable _original; + HsaApiTable& _modified; +}; + +} // namespace aql +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/aql/packet_construct.cpp b/source/lib/rocprofiler/aql/packet_construct.cpp new file mode 100644 index 0000000000..a640ec2b37 --- /dev/null +++ b/source/lib/rocprofiler/aql/packet_construct.cpp @@ -0,0 +1,189 @@ +#include "lib/rocprofiler/aql/packet_construct.hpp" + +#include +#include +#include "glog/logging.h" + +namespace rocprofiler +{ +namespace aql +{ +AQLPacketConstruct::AQLPacketConstruct(const hsa::AgentCache& agent, + const std::vector& metrics) +: _agent(agent) +{ + if(metrics.empty()) + { + throw std::runtime_error("No metrics supplied"); + } + + // Validate that the counter exists and construct the block instances + // for the counter. + for(const auto& x : metrics) + { + auto query_info = get_query_info(_agent.get_agent(), x); + _metrics.emplace_back().metric = x; + uint32_t event_id = std::atoi(x.event().c_str()); + for(unsigned block_index = 0; block_index < query_info.instance_count; ++block_index) + { + _metrics.back().instances.push_back( + {static_cast(query_info.id), + block_index, + event_id}); + bool validate_event_result; + LOG_IF(FATAL, + hsa_ven_amd_aqlprofile_validate_event(_agent.get_agent(), + &_metrics.back().instances.back(), + &validate_event_result) != + HSA_STATUS_SUCCESS); + LOG_IF(FATAL, !validate_event_result) + << "Invalid Metric: " << block_index << " " << event_id; + } + } + // Check that we can collect all of the metrics in a single execution + // with a single AQL packet + can_collect(); + _events = get_all_events(); +} + +std::unique_ptr +AQLPacketConstruct::construct_packet(const AmdExtTable& ext) const +{ + const size_t MEM_PAGE_MASK = 0x1000 - 1; + auto pkt_ptr = std::make_unique(ext.hsa_amd_memory_pool_free_fn); + auto& pkt = *pkt_ptr; + if(_events.empty()) + { + throw std::runtime_error("Constructing packet with no events"); + } + + pkt.profile = hsa_ven_amd_aqlprofile_profile_t{ + _agent.get_agent(), + HSA_VEN_AMD_AQLPROFILE_EVENT_TYPE_PMC, // SPM? + _events.data(), + static_cast(_events.size()), + nullptr, + 0u, + hsa_ven_amd_aqlprofile_descriptor_t{.ptr = nullptr, .size = 0}, + hsa_ven_amd_aqlprofile_descriptor_t{.ptr = nullptr, .size = 0}}; + auto& profile = pkt.profile; + + hsa_amd_memory_pool_access_t _access = HSA_AMD_MEMORY_POOL_ACCESS_NEVER_ALLOWED; + ext.hsa_amd_agent_memory_pool_get_info_fn(_agent.get_agent(), + _agent.kernarg_pool(), + HSA_AMD_AGENT_MEMORY_POOL_INFO_ACCESS, + static_cast(&_access)); + // Memory is accessable by both the GPU and CPU, unlock the command buffer for + // sharing. + if(_access == HSA_AMD_MEMORY_POOL_ACCESS_NEVER_ALLOWED) + { + throw std::runtime_error( + fmt::format("Agent {} does not allow memory pool access for counter collection", + _agent.get_agent().handle)); + } + + auto throw_if_failed = [](auto status, auto& message) { + if(status != HSA_STATUS_SUCCESS) + { + throw std::runtime_error(message); + } + }; + + throw_if_failed(hsa_ven_amd_aqlprofile_start(&profile, nullptr), + "could not generate packet sizes"); + + if(profile.command_buffer.size == 0 || profile.output_buffer.size == 0) + { + throw std::runtime_error( + fmt::format("No command or output buffer size set. CMD_BUF={} PROFILE_BUF={}", + profile.command_buffer.size, + profile.output_buffer.size)); + } + + // Allocate buffers and check the results + auto alloc_and_check = [&](auto& pool, auto** mem_loc, auto size) -> bool { + bool malloced = false; + size_t page_aligned = (size + MEM_PAGE_MASK) & ~MEM_PAGE_MASK; + if(ext.hsa_amd_memory_pool_allocate_fn( + pool, page_aligned, 0, static_cast(mem_loc)) != HSA_STATUS_SUCCESS) + { + *mem_loc = malloc(page_aligned); + malloced = true; + } + else + { + CHECK(*mem_loc); + hsa_agent_t agent = _agent.get_agent(); + // Memory is accessable by both the GPU and CPU, unlock the command buffer for + // sharing. + LOG_IF(FATAL, + ext.hsa_amd_agents_allow_access_fn(1, &agent, nullptr, *mem_loc) != + HSA_STATUS_SUCCESS) + << "Error: Allowing access to Command Buffer"; + } + return malloced; + }; + + // Build command and output buffers + pkt.command_buf_mallocd = alloc_and_check( + _agent.cpu_pool(), &profile.command_buffer.ptr, profile.command_buffer.size); + pkt.output_buffer_malloced = alloc_and_check( + _agent.kernarg_pool(), &profile.output_buffer.ptr, profile.output_buffer.size); + memset(profile.output_buffer.ptr, 0x0, profile.output_buffer.size); + + // throw if we do not construct the packets correctly. + throw_if_failed(hsa_ven_amd_aqlprofile_start(&profile, &pkt.start), + "could not generate start packet"); + throw_if_failed(hsa_ven_amd_aqlprofile_stop(&profile, &pkt.stop), + "could not generate stop packet"); + throw_if_failed(hsa_ven_amd_aqlprofile_read(&profile, &pkt.read), + "could not generate read packet"); + return pkt_ptr; +} + +std::vector +AQLPacketConstruct::get_all_events() const +{ + std::vector ret; + for(const auto& metric : _metrics) + { + ret.insert(ret.end(), metric.instances.begin(), metric.instances.end()); + } + return ret; +} + +void +AQLPacketConstruct::can_collect() +{ + // Verify that the counters fit within harrdware limits + std::map, int64_t> counter_count; + std::map, int64_t> max_allowed; + for(auto& metric : _metrics) + { + for(auto& instance : metric.instances) + { + auto block_pair = std::make_pair(instance.block_name, instance.block_index); + auto [iter, inserted] = counter_count.emplace(block_pair, 0); + iter->second++; + if(inserted) + { + max_allowed.emplace(block_pair, get_block_counters(_agent.get_agent(), instance)); + } + } + } + + // Check if the block count > max count + for(auto& [block_name, count] : counter_count) + { + if(auto* max = CHECK_NOTNULL(common::get_val(max_allowed, block_name)); count > *max) + { + throw std::runtime_error( + fmt::format("Block {} exceeds max number of hardware counters ({} > {})", + static_cast(block_name.first), + count, + *max)); + } + } +} +} // namespace aql +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/aql/packet_construct.hpp b/source/lib/rocprofiler/aql/packet_construct.hpp new file mode 100644 index 0000000000..25214ef3ae --- /dev/null +++ b/source/lib/rocprofiler/aql/packet_construct.hpp @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include + +#include "lib/rocprofiler/aql/helpers.hpp" +#include "lib/rocprofiler/counters/metrics.hpp" +#include "lib/rocprofiler/hsa/agent_cache.hpp" +#include "lib/rocprofiler/hsa/queue.hpp" + +namespace rocprofiler +{ +namespace aql +{ +/** + * Class to construct AQL Packets for a specific agent and metric set. + * Thie class checks that the counters supplied are collectable on the + * agent in question (including making sure that they stay within block + * limits). construct_packet returns an AQLPacket class containing the + * consturcted start/stop/read packets along with allocated buffers needed + * to collect the counter data. + */ +class AQLPacketConstruct +{ +public: + AQLPacketConstruct(const hsa::AgentCache& agent, const std::vector& metrics); + std::unique_ptr construct_packet(const AmdExtTable&) const; + +private: + struct AQLProfileMetric + { + counters::Metric metric; + std::vector instances; + }; + + std::vector get_all_events() const; + void can_collect(); + + const hsa::AgentCache& _agent; + std::vector _metrics; + std::vector _events; +}; + +} // namespace aql +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/aql/tests/CMakeLists.txt b/source/lib/rocprofiler/aql/tests/CMakeLists.txt new file mode 100644 index 0000000000..4f5b424143 --- /dev/null +++ b/source/lib/rocprofiler/aql/tests/CMakeLists.txt @@ -0,0 +1,23 @@ +rocprofiler_deactivate_clang_tidy() + +include(GoogleTest) + +set(ROCPROFILER_LIB_AQL_TEST_SOURCES "aql_test.cpp") + +add_executable(aql-test) + +target_sources(aql-test PRIVATE ${ROCPROFILER_LIB_AQL_TEST_SOURCES}) + +target_link_libraries( + aql-test + PRIVATE rocprofiler::rocprofiler-static-library rocprofiler::rocprofiler-glog + rocprofiler::rocprofiler-hip rocprofiler::rocprofiler-common-library + GTest::gtest GTest::gtest_main) + +gtest_add_tests( + TARGET aql-test + SOURCES ${ROCPROFILER_LIB_AQL_TEST_SOURCES} + TEST_LIST aql-test_TESTS + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + +set_tests_properties(${aql-test_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests") diff --git a/source/lib/rocprofiler/aql/tests/aql_test.cpp b/source/lib/rocprofiler/aql/tests/aql_test.cpp new file mode 100644 index 0000000000..bcffd39d90 --- /dev/null +++ b/source/lib/rocprofiler/aql/tests/aql_test.cpp @@ -0,0 +1,150 @@ +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include "lib/rocprofiler/aql/helpers.hpp" +#include "lib/rocprofiler/aql/packet_construct.hpp" +#include "lib/rocprofiler/counters/metrics.hpp" +#include "lib/rocprofiler/hsa/agent_cache.hpp" +#include "lib/rocprofiler/hsa/queue.hpp" + +namespace rocprofiler +{ +AmdExtTable +get_ext_table() +{ + return {.hsa_amd_memory_pool_get_info_fn = hsa_amd_memory_pool_get_info, + .hsa_amd_agent_iterate_memory_pools_fn = hsa_amd_agent_iterate_memory_pools, + .hsa_amd_memory_pool_allocate_fn = hsa_amd_memory_pool_allocate, + .hsa_amd_memory_pool_free_fn = hsa_amd_memory_pool_free, + .hsa_amd_agent_memory_pool_get_info_fn = hsa_amd_agent_memory_pool_get_info, + .hsa_amd_agents_allow_access_fn = hsa_amd_agents_allow_access}; +} + +auto +findDeviceMetrics(const hsa::AgentCache& agent, const std::unordered_set& metrics) +{ + std::vector ret; + auto all_counters = counters::getBaseHardwareMetrics(); + + auto gfx_metrics = common::get_val(all_counters, std::string(agent.name())); + if(!gfx_metrics) return ret; + + for(auto& counter : *gfx_metrics) + { + if(metrics.count(counter.name()) > 0 || metrics.empty()) + { + ret.push_back(counter); + } + } + return ret; +} + +} // namespace rocprofiler + +using namespace rocprofiler::aql; + +TEST(aql_profile, construct_packets) +{ + hsa_init(); + try + { + auto agents = rocprofiler::hsa::get_queue_controller().get_supported_agents(); + for(const auto& [_, agent] : agents) + { + LOG(WARNING) << fmt::format("Found Agent: {}", agent.get_agent().handle); + auto metrics = rocprofiler::findDeviceMetrics(agent, {"SQ_WAVES"}); + ASSERT_EQ(metrics.size(), 1); + AQLPacketConstruct(agent, metrics); + } + } catch(std::runtime_error&) + { + LOG(WARNING) << "Could not fetch agents on host, skipping test"; + return; + } + hsa_shut_down(); +} + +TEST(aql_profile, too_many_counters) +{ + hsa_init(); + try + { + auto agents = rocprofiler::hsa::get_queue_controller().get_supported_agents(); + + for(const auto& [_, agent] : agents) + { + LOG(WARNING) << fmt::format("Found Agent: {}", agent.get_agent().handle); + + auto metrics = rocprofiler::findDeviceMetrics(agent, {}); + EXPECT_THROW( + { + try + { + AQLPacketConstruct(agent, metrics); + } catch(const std::exception& e) + { + EXPECT_NE(e.what(), nullptr) << e.what(); + throw; + } + }, + std::runtime_error); + } + } catch(std::runtime_error&) + { + LOG(WARNING) << "Could not fetch agents on host, skipping test"; + return; + } + hsa_shut_down(); +} + +TEST(aql_profile, packet_generation_single) +{ + hsa_init(); + try + { + auto agents = rocprofiler::hsa::get_queue_controller().get_supported_agents(); + for(const auto& [_, agent] : agents) + { + auto metrics = rocprofiler::findDeviceMetrics(agent, {"SQ_WAVES"}); + AQLPacketConstruct pkt(agent, metrics); + auto test_pkt = pkt.construct_packet(rocprofiler::get_ext_table()); + EXPECT_TRUE(test_pkt); + } + } catch(std::runtime_error&) + { + LOG(WARNING) << "Could not fetch agents on host, skipping test"; + return; + } + + hsa_shut_down(); +} + +TEST(aql_profile, packet_generation_multi) +{ + hsa_init(); + try + { + auto agents = rocprofiler::hsa::get_queue_controller().get_supported_agents(); + for(const auto& [_, agent] : agents) + { + auto metrics = + rocprofiler::findDeviceMetrics(agent, {"SQ_WAVES", "TA_FLAT_READ_WAVEFRONTS"}); + AQLPacketConstruct pkt(agent, metrics); + auto test_pkt = pkt.construct_packet(rocprofiler::get_ext_table()); + EXPECT_TRUE(test_pkt); + } + } catch(std::runtime_error&) + { + LOG(WARNING) << "Could not fetch agents on host, skipping test"; + return; + } + hsa_shut_down(); +} diff --git a/source/lib/rocprofiler/context/context.cpp b/source/lib/rocprofiler/context/context.cpp index c78a8aeec1..7fd4b0a8d0 100644 --- a/source/lib/rocprofiler/context/context.cpp +++ b/source/lib/rocprofiler/context/context.cpp @@ -26,6 +26,7 @@ #include "lib/common/container/stable_vector.hpp" #include "lib/rocprofiler/buffer.hpp" #include "lib/rocprofiler/context/context.hpp" +#include "lib/rocprofiler/counters/core.hpp" #include @@ -206,6 +207,8 @@ start_context(rocprofiler_context_id_t context_id) if(!success) return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_STARTED; + rocprofiler::counters::start_context(context_id); + return ROCPROFILER_STATUS_SUCCESS; } @@ -221,6 +224,7 @@ stop_context(rocprofiler_context_id_t idx) { bool success = itr.compare_exchange_strong(_expected, nullptr); + rocprofiler::counters::stop_context(idx); if(success) return ROCPROFILER_STATUS_SUCCESS; } } diff --git a/source/lib/rocprofiler/context/context.hpp b/source/lib/rocprofiler/context/context.hpp index 5388f609a7..feb8c9503d 100644 --- a/source/lib/rocprofiler/context/context.hpp +++ b/source/lib/rocprofiler/context/context.hpp @@ -28,6 +28,7 @@ #include "lib/common/container/stable_vector.hpp" #include "lib/rocprofiler/context/domain.hpp" +#include "lib/rocprofiler/counters/core.hpp" #include #include @@ -78,15 +79,29 @@ struct buffer_tracing_service buffer_array_t buffer_data = {}; }; +struct counter_collection_service +{ + // Contains a vector of counter collection instances associated with this context. + // Each instance is assocated with an agent and a counter collection profile. + // Contains callback information along with other data needed to collect/process + // counters. + std::vector> callbacks{}; + // A flag to state wether or not the counter set is currently enabled. This is primarily + // to protect against multithreaded calls to enable a context (and enabling already enabled + // counters). + rocprofiler::common::Synchronized enabled{false}; +}; + struct context { // size is used to ensure that we never read past the end of the version - size_t size = 0; - uint64_t context_idx = 0; // context id - uint32_t client_idx = 0; // tool id - correlation_tracing_service correlation_tracer = {}; - std::unique_ptr callback_tracer = {}; - std::unique_ptr buffered_tracer = {}; + size_t size = 0; + uint64_t context_idx = 0; // context id + uint32_t client_idx = 0; // tool id + correlation_tracing_service correlation_tracer = {}; + std::unique_ptr callback_tracer = {}; + std::unique_ptr buffered_tracer = {}; + std::unique_ptr counter_collection = {}; }; // set the client index needs to be called before allocate_context() diff --git a/source/lib/rocprofiler/counters.cpp b/source/lib/rocprofiler/counters.cpp new file mode 100644 index 0000000000..a642072f97 --- /dev/null +++ b/source/lib/rocprofiler/counters.cpp @@ -0,0 +1,112 @@ +#include + +#include "lib/common/synchronized.hpp" +#include "lib/rocprofiler/aql/helpers.hpp" +#include "lib/rocprofiler/counters/evaluate_ast.hpp" +#include "lib/rocprofiler/counters/metrics.hpp" +#include "lib/rocprofiler/hsa/agent_cache.hpp" +#include "lib/rocprofiler/hsa/queue.hpp" + +extern "C" { +/** + * @brief Query Counter name. + * + * @param [in] counter_id + * @param [out] name if nullptr, size will be returned + * @param [out] size + * @return ::rocprofiler_status_t + */ +rocprofiler_status_t ROCPROFILER_API +rocprofiler_query_counter_name(rocprofiler_counter_id_t counter_id, const char** name, size_t* size) +{ + const auto& id_map = rocprofiler::counters::getMetricIdMap(); + + if(const auto* metric_ptr = rocprofiler::common::get_val(id_map, counter_id.handle)) + { + *name = metric_ptr->name().c_str(); + *size = metric_ptr->name().size(); + return ROCPROFILER_STATUS_SUCCESS; + } + + return ROCPROFILER_STATUS_ERROR_COUNTER_NOT_FOUND; +} + +/** + * @brief Query Counter Instances Count. + * + * @param [in] counter_id + * @param [out] instance_count + * @return rocprofiler_status_t + */ +rocprofiler_status_t ROCPROFILER_API +rocprofiler_query_counter_instance_count(rocprofiler_agent_t agent, + rocprofiler_counter_id_t counter_id, + size_t* instance_count) +{ + const auto& id_map = rocprofiler::counters::getMetricIdMap(); + const auto* metric_ptr = rocprofiler::common::get_val(id_map, counter_id.handle); + if(!metric_ptr) return ROCPROFILER_STATUS_ERROR_COUNTER_NOT_FOUND; + + *instance_count = 0; + // Special counters like KERNEL_DURATION are not real counters and wont + // have any query info. + if(!metric_ptr->special().empty()) + { + *instance_count = 1; + return ROCPROFILER_STATUS_SUCCESS; + } + + // Returns the set of hardware counters needed to evaluate the metric. + // For derived metrics, this can be more than one counter. In that case, + // we return the maximum instance count among all underlying counters. + auto req_counters = + rocprofiler::counters::get_required_hardware_counters(std::string(agent.name), *metric_ptr); + if(!req_counters) return ROCPROFILER_STATUS_ERROR_COUNTER_NOT_FOUND; + + // NOTE: to look up instance information, we require HSA be init'd. Reason + // for this is the call to get instance information is an HSA call. + const auto* maybe_agent = rocprofiler::common::get_val( + rocprofiler::hsa::get_queue_controller().get_supported_agents(), agent.id.handle); + if(!maybe_agent) + { + LOG(ERROR) << "HSA must be loaded to obtain instance information."; + return ROCPROFILER_STATUS_ERROR; + } + + for(const auto& counter : *req_counters) + { + if(!counter.special().empty()) + { + *instance_count = std::max(size_t(1), *instance_count); + continue; + } + auto query_info = rocprofiler::aql::get_query_info(maybe_agent->get_agent(), counter); + *instance_count = std::max(static_cast(query_info.instance_count), *instance_count); + } + + return ROCPROFILER_STATUS_SUCCESS; +} +/** + * @brief Query Agent Counters Availability. + * + * @param [in] agent + * @param [out] counters_list + * @param [out] counters_count + * @return ::rocprofiler_status_t + */ +rocprofiler_status_t ROCPROFILER_API +rocprofiler_iterate_agent_supported_counters(rocprofiler_agent_t agent, + rocprofiler_available_counters_cb_t cb, + void* user_data) +{ + const auto& metrics = rocprofiler::counters::getMetricsForAgent(std::string(agent.name)); + std::vector ids; + ids.reserve(metrics.size()); + for(const auto& metric : metrics) + { + ids.push_back({.handle = metric.id()}); + } + + return cb(ids.data(), ids.size(), user_data); +} +} diff --git a/source/lib/rocprofiler/counters/CMakeLists.txt b/source/lib/rocprofiler/counters/CMakeLists.txt index 6769637755..8b304222fb 100644 --- a/source/lib/rocprofiler/counters/CMakeLists.txt +++ b/source/lib/rocprofiler/counters/CMakeLists.txt @@ -1,10 +1,10 @@ -set(ROCPROFILER_LIB_COUNTERS_SOURCES metrics.cpp) -set(ROCPROFILER_LIB_COUNTERS_HEADERS metrics.hpp) - +set(ROCPROFILER_LIB_COUNTERS_SOURCES metrics.cpp evaluate_ast.cpp core.cpp) +set(ROCPROFILER_LIB_COUNTERS_HEADERS metrics.hpp evaluate_ast.hpp core.hpp) target_sources(rocprofiler-object-library PRIVATE ${ROCPROFILER_LIB_COUNTERS_SOURCES} ${ROCPROFILER_LIB_COUNTERS_HEADERS}) add_subdirectory(xml) +add_subdirectory(parser) if(ROCPROFILER_BUILD_TESTS) add_subdirectory(tests) diff --git a/source/lib/rocprofiler/counters/core.cpp b/source/lib/rocprofiler/counters/core.cpp new file mode 100644 index 0000000000..82b06d6d91 --- /dev/null +++ b/source/lib/rocprofiler/counters/core.cpp @@ -0,0 +1,236 @@ +#include "lib/rocprofiler/counters/core.hpp" + +#include "lib/common/synchronized.hpp" +#include "lib/rocprofiler/aql/helpers.hpp" +#include "lib/rocprofiler/aql/packet_construct.hpp" +#include "lib/rocprofiler/context/context.hpp" +#include "lib/rocprofiler/registration.hpp" + +#include + +namespace rocprofiler +{ +namespace counters +{ +/** + * Callback we get from HSA interceptor when a kernel packet is being enqueued. + * + * We return an AQLPacket containing the start/stop/read packets for injection. + */ +std::unique_ptr +queue_cb(const std::shared_ptr& info, + const hsa::Queue& queue, + hsa::ClientID, + const hsa_ext_amd_aql_pm4_packet_t&) +{ + if(!info) return nullptr; + + std::unique_ptr ret_pkt; + + // Check packet cache + info->packets.wlock([&](auto& pkt_vector) { + // Delay packet generator construction until first HSA packet is processed + // This ensures that HSA exists + if(!info->pkt_generator) + { + info->pkt_generator = std::make_unique( + queue.get_agent(), + std::vector{info->profile_cfg.reqired_hw_counters.begin(), + info->profile_cfg.reqired_hw_counters.end()}); + } + + if(!pkt_vector.empty()) + { + ret_pkt = std::move(pkt_vector.back()); + pkt_vector.pop_back(); + } + }); + + if(!ret_pkt) + { + // If we do not have a packet in the cache, create one. + ret_pkt = + info->pkt_generator->construct_packet(hsa::get_queue_controller().get_ext_table()); + } + return ret_pkt; +} + +/** + * Callback called by HSA interceptor when the kernel has completed processing. + */ +void +completed_cb(const std::shared_ptr& info, + const hsa::Queue& queue, + hsa::ClientID, + const hsa_ext_amd_aql_pm4_packet_t& kernel, + std::unique_ptr pkt) +{ + if(!info) return; + + // auto out_buf = pkt->profile.output_buffer.ptr; + // Read data and create user return.... + + // return AQL packet for reuse. + + info->packets.wlock([&](auto& pkt_vector) { + if(pkt) + { + pkt_vector.emplace_back(std::move(pkt)); + } + }); + + if(!info->user_cb) return; + + info->user_cb(queue.get_id(), + info->profile_cfg.agent, + rocprofiler_correlation_id_t{}, + reinterpret_cast(&kernel), + info->callback_args, + nullptr, // Date pointer does here. + 0, // Number of objects + info->profile_cfg.id); +} + +class CounterController +{ +public: + // Adds a counter collection profile to our global cache. + // Note: these profiles can be used across multiple contexts + // and are independent of the context. + uint64_t add_profile(profile_config&& config) + { + static std::atomic profile_val = 1; + uint64_t ret = 0; + _configs.wlock([&](auto& data) { + config.id = rocprofiler_profile_config_id_t{.handle = profile_val}; + data.emplace(profile_val, std::move(config)); + ret = profile_val; + profile_val++; + }); + return ret; + } + + void destroy_profile(uint64_t id) + { + _configs.wlock([&](auto& data) { data.erase(id); }); + } + + // Setup the counter collection service. counter_callback_info is created here + // to contain the counters that need to be collected (specified in profile_id) and + // the AQL packet generator for injecting packets. Note: the service is created + // in the stop state. + bool configure_dispatch(rocprofiler_context_id_t context_id, + uint64_t profile_id, + rocprofiler_profile_counting_dispatch_callback_t callback, + void* callback_args) const + { + auto& ctx = *rocprofiler::context::get_registered_contexts().at(context_id.handle); + + // Note: A single profile config could be used on multiple contexts + profile_config cfg; + _configs.rlock([&](const auto& map) { cfg = map.at(profile_id); }); + + if(!ctx.counter_collection) + { + ctx.counter_collection = + std::make_unique(); + } + + auto& cb = *ctx.counter_collection->callbacks.emplace_back( + std::make_shared()); + + cb.user_cb = callback; + + // Secondary copy of the config to be shared with async callback + cb.profile_cfg = cfg; + cb.callback_args = callback_args; + cb.context = context_id; + return true; + } + +private: + rocprofiler::common::Synchronized> _configs; +}; + +CounterController& +get_controller() +{ + static CounterController controller; + return controller; +} + +uint64_t +create_counter_profile(profile_config&& config) +{ + return get_controller().add_profile(std::move(config)); +} + +void +destroy_counter_profile(uint64_t id) +{ + get_controller().destroy_profile(id); +} + +void +start_context(rocprofiler_context_id_t context_id) +{ + auto& ctx = *rocprofiler::context::get_registered_contexts().at(context_id.handle); + auto& controller = hsa::get_queue_controller(); + if(!ctx.counter_collection) return; + + // Only one thread should be attempting to enable/disable this context + ctx.counter_collection->enabled.wlock([&](auto& enabled) { + if(enabled) return; + for(auto& cb : ctx.counter_collection->callbacks) + { + // Insert our callbacks into HSA Interceptor. This + // turns on counter instrumentation. + cb->queue_id = controller.add_callback( + cb->profile_cfg.agent, + [=](const hsa::Queue& q, + hsa::ClientID c, + const hsa_ext_amd_aql_pm4_packet_t& kern_pkt) { + return queue_cb(cb, q, c, kern_pkt); + }, + // Completion CB + [=](const hsa::Queue& q, + hsa::ClientID c, + const hsa_ext_amd_aql_pm4_packet_t& kern_pkt, + std::unique_ptr aql) { + completed_cb(cb, q, c, kern_pkt, std::move(aql)); + }); + } + enabled = true; + }); +} + +void +stop_context(rocprofiler_context_id_t context_id) +{ + auto& controller = hsa::get_queue_controller(); + auto& ctx = *rocprofiler::context::get_registered_contexts().at(context_id.handle); + if(!ctx.counter_collection) return; + + ctx.counter_collection->enabled.wlock([&](auto& enabled) { + if(!enabled) return; + for(auto& cb : ctx.counter_collection->callbacks) + { + // Remove our callbacks from HSA's queue controller + controller.remove_callback(cb->queue_id); + cb->queue_id = -1; + } + enabled = false; + }); +} + +bool +configure_dispatch(rocprofiler_context_id_t context_id, + uint64_t profile_id, + rocprofiler_profile_counting_dispatch_callback_t callback, + void* callback_args) +{ + return get_controller().configure_dispatch(context_id, profile_id, callback, callback_args); +} + +} // namespace counters +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/counters/core.hpp b/source/lib/rocprofiler/counters/core.hpp new file mode 100644 index 0000000000..6577b55e20 --- /dev/null +++ b/source/lib/rocprofiler/counters/core.hpp @@ -0,0 +1,69 @@ +#pragma once + +#include +#include + +#include "lib/rocprofiler/aql/helpers.hpp" +#include "lib/rocprofiler/aql/packet_construct.hpp" +#include "lib/rocprofiler/counters/evaluate_ast.hpp" +#include "lib/rocprofiler/counters/metrics.hpp" +#include "lib/rocprofiler/hsa/agent_cache.hpp" + +namespace rocprofiler +{ +namespace counters +{ +// Stores counter profiling information such as the agent +// to collect counters on, the metrics to collect, the hw +// counters needed to evaluate the metrics, and the ASTs. +// This profile can be shared among many rocprof contexts. +struct profile_config +{ + rocprofiler_agent_t agent{}; + std::vector metrics{}; + // HW counters that must be collected to compute the above + // metrics (derived metrics are broken down into hw counters + // in this vector). + std::set reqired_hw_counters{}; + // ASTs to evaluate + std::vector asts{}; + rocprofiler_profile_config_id_t id{.handle = 0}; +}; + +// Internal counter struct that stores the state needed to handle an intercepted +// HSA kernel packet. +struct counter_callback_info +{ + // Packet generator to create AQL packets for insertion + std::unique_ptr pkt_generator{nullptr}; + // A packet cache of AQL packets. This allows reuse of AQL packets (preventing costly + // allocation of new packets/destruction). + rocprofiler::common::Synchronized>> + packets{}; + // User callback + rocprofiler_profile_counting_dispatch_callback_t user_cb{nullptr}; + // Profile configuration used for this callback containing the counters + // to collect and the evaluation ASTs + profile_config profile_cfg{}; + // User id + void* callback_args{nullptr}; + // Link to the context this is associated with + rocprofiler_context_id_t context{.handle = 0}; + // HSA Queue ClientID. This is an ID we get when we insert a callback into the + // HSA queue interceptor. This ID can be used to disable the callback. + rocprofiler::hsa::ClientID queue_id{-1}; +}; + +uint64_t + create_counter_profile(profile_config&& config); +void destroy_counter_profile(uint64_t); +bool + configure_dispatch(rocprofiler_context_id_t context_id, + uint64_t profile_id, + rocprofiler_profile_counting_dispatch_callback_t callback, + void* callback_args); +void start_context(rocprofiler_context_id_t); + +void stop_context(rocprofiler_context_id_t); +} // namespace counters +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/counters/evaluate_ast.cpp b/source/lib/rocprofiler/counters/evaluate_ast.cpp new file mode 100644 index 0000000000..560baf5021 --- /dev/null +++ b/source/lib/rocprofiler/counters/evaluate_ast.cpp @@ -0,0 +1,79 @@ +#include "lib/rocprofiler/counters/evaluate_ast.hpp" + +#include + +#include "lib/common/synchronized.hpp" +#include "lib/common/utility.hpp" +#include "lib/rocprofiler/counters/parser/reader.hpp" + +namespace rocprofiler +{ +namespace counters +{ +const std::unordered_map& +get_ast_map() +{ + static std::unordered_map ast_map = []() { + std::unordered_map data; + const auto& metric_map = counters::getMetricMap(); + for(const auto& [gfx, metrics] : metric_map) + { + // TODO: Remove global XML from derrived counters... + if(gfx == "global") continue; + + std::unordered_map by_name; + for(const auto& metric : metrics) + { + by_name.emplace(metric.name(), metric); + } + + auto& eval_map = data.emplace(gfx, EvaluateASTMap{}).first->second; + for(auto& [_, metric] : by_name) + { + RawAST* ast = nullptr; + auto* buf = + yy_scan_string(metric.expression().empty() ? metric.name().c_str() + : metric.expression().c_str()); + yyparse(&ast); + if(!ast) + { + LOG(ERROR) << fmt::format("Unable to parse metric {}", metric); + throw std::runtime_error(fmt::format("Unable to parse metric {}", metric)); + } + try + { + eval_map.emplace(metric.name(), EvaluateAST(by_name, *ast)); + } catch(std::out_of_range& e) + { + throw std::runtime_error( + fmt::format("AST was not generated for {}:{}, Counter will be unavailable. " + "Likely cause is a base counter not being defined used in a " + "derrived counter.", + gfx, + metric.name())); + } + yy_delete_buffer(buf); + delete ast; + } + } + return data; + }(); + return ast_map; +} + +std::optional> +get_required_hardware_counters(const std::string& agent, const Metric& metric) +{ + const auto& asts = get_ast_map(); + const auto* agent_map = rocprofiler::common::get_val(asts, agent); + if(!agent_map) return std::nullopt; + const auto* counter_ast = rocprofiler::common::get_val(*agent_map, metric.name()); + if(!counter_ast) return std::nullopt; + + std::set required_counters; + counter_ast->get_required_counters(*agent_map, required_counters); + return required_counters; +} + +} // namespace counters +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/counters/evaluate_ast.hpp b/source/lib/rocprofiler/counters/evaluate_ast.hpp new file mode 100644 index 0000000000..361ba35f46 --- /dev/null +++ b/source/lib/rocprofiler/counters/evaluate_ast.hpp @@ -0,0 +1,93 @@ +#pragma once + +#include +#include +#include + +#include "lib/common/utility.hpp" +#include "lib/rocprofiler/counters/metrics.hpp" +#include "lib/rocprofiler/counters/parser/raw_ast.hpp" + +namespace rocprofiler +{ +namespace counters +{ +class EvaluateAST +{ +public: + EvaluateAST(const std::unordered_map& metrics, const RawAST& ast) + : _type(ast.type) + , _op(ast.operation) + { + if(_type == NodeType::REFERENCE_NODE) + { + _metric = metrics.at(std::get(ast.value)); + // LOG(ERROR) << fmt::format("CHILD METRIC {}", _metric); + } + + if(_type == NodeType::NUMBER_NODE) + { + _raw_value = std::get(ast.value); + } + + for(const auto& nextAst : ast.counter_set) + { + _children.emplace_back(metrics, *nextAst); + } + } + + void get_required_counters(const std::unordered_map& asts, + std::set& counters) const + { + if(!_metric.empty() && children().empty() && _type != NodeType::NUMBER_NODE) + { + // Base counter + if(_metric.expression().empty()) + { + counters.insert(_metric); + return; + } + + // Derrived Counter + const auto* expr_ptr = rocprofiler::common::get_val(asts, _metric.name()); + if(!expr_ptr) throw std::runtime_error("could not find derived counter"); + expr_ptr->get_required_counters(asts, counters); + return; + } + + for(const auto& child : children()) + { + child.get_required_counters(asts, counters); + } + } + + NodeType type() const { return _type; } + NodeType op() const { return _op; } + const std::vector& children() const { return _children; } + const Metric& metric() const { return _metric; } + +private: + NodeType _type{NONE}; + NodeType _op{NONE}; + Metric _metric; + double _raw_value{0}; + std::vector _children; +}; + +using EvaluateASTMap = std::unordered_map; + +/** + * Construct the ASTs for all counters appearing in basic/derrived counter + * definition files. + */ +const std::unordered_map& +get_ast_map(); + +/** + * Get the required basic/hardware counters needed to evaluate a + * specific metric (may be multiple HW counters if a derrived metric). + */ +std::optional> +get_required_hardware_counters(const std::string& agent, const Metric& metric); +} // namespace counters +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/counters/metrics.cpp b/source/lib/rocprofiler/counters/metrics.cpp index 2e6caac1d7..d27889778f 100644 --- a/source/lib/rocprofiler/counters/metrics.cpp +++ b/source/lib/rocprofiler/counters/metrics.cpp @@ -22,15 +22,22 @@ THE SOFTWARE. #include "metrics.hpp" +#include + +#include "lib/common/synchronized.hpp" +#include "lib/common/utility.hpp" +#include "lib/common/xml.hpp" + +#include "glog/logging.h" + #include // for dladdr +#include #include #include #include -#include "glog/logging.h" -#include "lib/common/xml.hpp" -#include "rocprofiler/rocprofiler.h" - +namespace rocprofiler +{ namespace counters { namespace @@ -38,10 +45,11 @@ namespace MetricMap loadXml(const std::string& filename) { - MetricMap ret; + static std::atomic id = 0; + MetricMap ret; DLOG(INFO) << "Loading Counter Config: " << filename; // todo: return unique_ptr.... - auto xml = xml::Xml::Create(filename); + auto xml = common::Xml::Create(filename); LOG_IF(FATAL, !xml) << "Could not open XML Counter Config File (set env ROCPROFILER_METRICS_PATH)"; @@ -68,23 +76,25 @@ loadXml(const std::string& filename) node->opts["block"], node->opts["event"], node->opts["descr"], - node->opts["expr"]); + node->opts["expr"], + node->opts["special"], + id); + id++; } } - DLOG(INFO) << fmt::format("{}", ret); return ret; } std::string findViaInstallPath(const std::string& filename) { - Dl_info dl_info; + Dl_info dl_info = {}; DLOG(INFO) << filename << " is being looked up via install path"; if(dladdr(reinterpret_cast(rocprofiler_query_available_agents), &dl_info) != 0) { - return std::filesystem::path{dl_info.dli_fname}.remove_filename() / - fmt::format("../lib/{}", filename); + return std::filesystem::path{dl_info.dli_fname}.parent_path().parent_path() / + fmt::format("share/rocprofiler/{}", filename); } return filename; } @@ -92,10 +102,10 @@ findViaInstallPath(const std::string& filename) std::string findViaEnvironment(const std::string& filename) { - if(getenv("ROCPROFILER_METRICS_PATH")) + if(const char* metrics_path = nullptr; (metrics_path = getenv("ROCPROFILER_METRICS_PATH"))) { DLOG(INFO) << filename << " is being looked up via env variable ROCPROFILER_METRICS_PATH"; - return std::filesystem::path{std::string(getenv("ROCPROFILER_METRICS_PATH"))} / filename; + return std::filesystem::path{std::string{metrics_path}} / filename; } // No environment variable, lookup via install path return findViaInstallPath(filename); @@ -115,4 +125,58 @@ getBaseHardwareMetrics() return loadXml(findViaEnvironment("basic_counters.xml")); } -}; // namespace counters +const MetricIdMap& +getMetricIdMap() +{ + static MetricIdMap id_map = []() { + MetricIdMap map; + for(const auto& [_, val] : getMetricMap()) + { + for(const auto& metric : val) + { + map.emplace(metric.id(), metric); + } + } + return map; + }(); + return id_map; +} + +const MetricMap& +getMetricMap() +{ + static MetricMap map = []() { + MetricMap ret = getBaseHardwareMetrics(); + for(auto& [key, val] : getDerivedHardwareMetrics()) + { + auto [iter, inserted] = ret.emplace(key, val); + if(!inserted) + { + iter->second.insert(iter->second.end(), val.begin(), val.end()); + } + } + return ret; + }(); + return map; +} + +const std::vector& +getMetricsForAgent(const std::string& agent) +{ + static const std::vector empty; + const auto& map = getMetricMap(); + if(const auto* metric_ptr = rocprofiler::common::get_val(map, agent)) + { + return *metric_ptr; + } + + return empty; +} + +bool +operator<(Metric const& lhs, Metric const& rhs) +{ + return lhs.id() < rhs.id(); +} +} // namespace counters +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/counters/metrics.hpp b/source/lib/rocprofiler/counters/metrics.hpp index 641f2bc3b7..0f77c40f4d 100644 --- a/source/lib/rocprofiler/counters/metrics.hpp +++ b/source/lib/rocprofiler/counters/metrics.hpp @@ -10,22 +10,29 @@ #include "fmt/core.h" #include "fmt/ranges.h" +namespace rocprofiler +{ namespace counters { // Base metrics (w/o instance information) defined in gfx_metrics/derrived.xml class Metric { public: + Metric() = default; Metric(std::string name, std::string block, std::string event, std::string dsc, - std::string expr) + std::string expr, + std::string special, + uint64_t id) : name_(std::move(name)) , block_(std::move(block)) , event_(std::move(event)) , description_(std::move(dsc)) , expression_(std::move(expr)) + , special_(std::move(special)) + , id_(id) {} const std::string& name() const { return name_; } @@ -33,30 +40,64 @@ public: const std::string& event() const { return event_; } const std::string& description() const { return description_; } const std::string& expression() const { return expression_; } + const std::string& special() const { return special_; } + uint64_t id() const { return id_; } + bool empty() const { return empty_; } + + friend bool operator<(Metric const& lhs, Metric const& rhs); private: - std::string name_; - std::string block_; - std::string event_; - std::string description_; - std::string expression_; + std::string name_ = {}; + std::string block_ = {}; + std::string event_ = {}; + std::string description_ = {}; + std::string expression_ = {}; + std::string special_ = {}; + int64_t id_ = -1; + bool empty_ = false; }; -using MetricMap = std::unordered_map>; +using MetricMap = std::unordered_map>; +using MetricIdMap = std::unordered_map; +/** + * Get base hardware counters for all GFXs Map + */ MetricMap getBaseHardwareMetrics(); +/** + * Get derived hardware metrics for all GFXs Map + */ MetricMap getDerivedHardwareMetrics(); +/** + * Combined map containing both base and derived counters + */ +const MetricMap& +getMetricMap(); + +/** + * Get the metrics that apply to a specific agent. Supplied parameter + * is the GFXIP of the agent. + */ +const std::vector& +getMetricsForAgent(const std::string&); + +/** + * Get a map of metric::id() -> metric + */ +const MetricIdMap& +getMetricIdMap(); } // namespace counters +} // namespace rocprofiler namespace fmt { // fmt::format support for metric template <> -struct formatter +struct formatter { template constexpr auto parse(ParseContext& ctx) @@ -65,21 +106,23 @@ struct formatter } template - auto format(counters::Metric const& metric, Ctx& ctx) const + auto format(rocprofiler::counters::Metric const& metric, Ctx& ctx) const { - return fmt::format_to(ctx.out(), - "Metric: {} [Block: {}, Event: {}, Expression: {}, Description: {}]", - metric.name(), - metric.block(), - metric.event(), - metric.expression().empty() ? "" : metric.expression(), - metric.description()); + return fmt::format_to( + ctx.out(), + "Metric: {} [Block: {}, Event: {}, Expression: {}, Description: {}, id: {}]", + metric.name(), + metric.block(), + metric.event(), + metric.expression().empty() ? "" : metric.expression(), + metric.description(), + metric.id()); } }; // fmt::format support for MetricMap template <> -struct formatter +struct formatter { template constexpr auto parse(ParseContext& ctx) @@ -88,7 +131,7 @@ struct formatter } template - auto format(counters::MetricMap const& map, Ctx& ctx) const + auto format(rocprofiler::counters::MetricMap const& map, Ctx& ctx) const { std::string out; for(const auto& [gfxName, counters] : map) diff --git a/source/lib/rocprofiler/counters/parser/CMakeLists.txt b/source/lib/rocprofiler/counters/parser/CMakeLists.txt new file mode 100644 index 0000000000..e1b8797fc7 --- /dev/null +++ b/source/lib/rocprofiler/counters/parser/CMakeLists.txt @@ -0,0 +1,50 @@ +rocprofiler_deactivate_clang_tidy() + +set(expr_parser_sources parser.cpp parser.h scanner.cpp raw_ast.hpp reader.hpp) + +add_library(rocprofiler-expr-parser OBJECT) + +if(ROCPROFILER_REGENERATE_COUNTERS_PARSER) + find_package(FLEX REQUIRED) + find_package(BISON REQUIRED) + + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/parser.h + ${CMAKE_CURRENT_BINARY_DIR}/parser.h COPYONLY) + + bison_target( + ExprBison parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp + COMPILE_FLAGS "-t" + DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/parser.h) + flex_target(ExprFlex scanner.l ${CMAKE_CURRENT_BINARY_DIR}/scanner.cpp) + add_flex_bison_dependency(ExprFlex ExprBison) + + set_source_files_properties(${expr_parser_sources} PROPERTIES COMPILE_DEFINITIONS + YYDEBUG=1) + + add_custom_target( + rocprofiler-expr-parser-patch + COMMAND + ${CMAKE_COMMAND} -DPROJECT_SRC_DIR=${PROJECT_SOURCE_DIR} + -DPROJECT_BLD_DIR=${PROJECT_BINARY_DIR} + -DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} + -DBINARY_DIR=${CMAKE_CURRENT_BINARY_DIR} + -DFORMAT_EXE=${ROCPROFILER_CLANG_FORMAT_EXE} -P + ${PROJECT_SOURCE_DIR}/source/scripts/patch-parser.cmake + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp + ${CMAKE_CURRENT_BINARY_DIR}/scanner.cpp + VERBATIM) + + # ensure gets applied when rocprofiler-expr-parser is built + add_dependencies(rocprofiler-expr-parser rocprofiler-expr-parser-patch) +endif() + +target_sources(rocprofiler-expr-parser PRIVATE ${expr_parser_sources}) +target_include_directories(rocprofiler-expr-parser PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(rocprofiler-expr-parser + PRIVATE rocprofiler::rocprofiler-common-library) +target_sources(rocprofiler-object-library + PUBLIC $) + +if(ROCPROFILER_BUILD_TESTS) + add_subdirectory(tests) +endif() diff --git a/source/lib/rocprofiler/counters/parser/parser.cpp b/source/lib/rocprofiler/counters/parser/parser.cpp new file mode 100644 index 0000000000..f5d944be7a --- /dev/null +++ b/source/lib/rocprofiler/counters/parser/parser.cpp @@ -0,0 +1,1585 @@ +/* A Bison parser, made by GNU Bison 3.5.1. */ + +/* Bison implementation for Yacc-like parsers in C + + Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2020 Free Software Foundation, + Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +/* C LALR(1) parser skeleton written by Richard Stallman, by + simplifying the original so-called "semantic" parser. */ + +/* All symbols defined below should begin with yy or YY, to avoid + infringing on user name space. This should be done even for local + variables, as they might otherwise be expanded by user macros. + There are some unavoidable exceptions within include files to + define necessary library symbols; they are noted "INFRINGES ON + USER NAME SPACE" below. */ + +/* Undocumented macros, especially those whose name start with YY_, + are private implementation details. Do not rely on them. */ + +/* Identify Bison output. */ +#define YYBISON 1 + +/* Bison version. */ +#define YYBISON_VERSION "3.5.1" + +/* Skeleton name. */ +#define YYSKELETON_NAME "yacc.c" + +/* Pure parsers. */ +#define YYPURE 0 + +/* Push parsers. */ +#define YYPUSH 0 + +/* Pull parsers. */ +#define YYPULL 1 + +/* First part of user prologue. */ +#line 8 "parser.y" + +#include +#include +#include + +#include + +#include "raw_ast.hpp" + +int +yyparse(rocprofiler::counters::RawAST** result); +int +yylex(void); +void +yyerror(rocprofiler::counters::RawAST**, const char* s) +{ + LOG(ERROR) << s; +} + +#line 84 "parser.cpp" + +#ifndef YY_CAST +# ifdef __cplusplus +# define YY_CAST(Type, Val) static_cast(Val) +# define YY_REINTERPRET_CAST(Type, Val) reinterpret_cast(Val) +# else +# define YY_CAST(Type, Val) ((Type)(Val)) +# define YY_REINTERPRET_CAST(Type, Val) ((Type)(Val)) +# endif +#endif +#ifndef YY_NULLPTR +# if defined __cplusplus +# if 201103L <= __cplusplus +# define YY_NULLPTR nullptr +# else +# define YY_NULLPTR 0 +# endif +# else +# define YY_NULLPTR ((void*) 0) +# endif +#endif + +/* Enabling verbose error messages. */ +#ifdef YYERROR_VERBOSE +# undef YYERROR_VERBOSE +# define YYERROR_VERBOSE 1 +#else +# define YYERROR_VERBOSE 0 +#endif + +/* Use api.header.include to #include this header + instead of duplicating it here. */ +#ifndef YY_YY_ROCPROFILER_SOURCE_LIB_ROCPROFILER_COUNTERS_PARSER_PARSER_H_INCLUDED +# define YY_YY_ROCPROFILER_SOURCE_LIB_ROCPROFILER_COUNTERS_PARSER_PARSER_H_INCLUDED +/* Debug traces. */ +# ifndef YYDEBUG +# define YYDEBUG 1 +# endif +# if YYDEBUG +extern int yydebug; +# endif +/* "%code requires" blocks. */ +# line 2 "parser.y" + +# include "raw_ast.hpp" +using namespace rocprofiler::counters; +# define YYDEBUG 1 + +# line 133 "parser.cpp" + +/* Token type. */ +# ifndef YYTOKENTYPE +# define YYTOKENTYPE +enum yytokentype +{ + ADD = 258, + SUB = 259, + MUL = 260, + DIV = 261, + ABS = 262, + EQUALS = 263, + OP = 264, + CP = 265, + O_SQ = 266, + C_SQ = 267, + COLON = 268, + EOL = 269, + UMINUS = 270, + CM = 271, + NUMBER = 272, + RANGE = 273, + NAME = 274, + REDUCE = 275, + SELECT = 276, + LOWER_THAN_ELSE = 277, + ELSE = 278 +}; +# endif + +/* Value type. */ +# if !defined YYSTYPE && !defined YYSTYPE_IS_DECLARED +union YYSTYPE +{ +# line 34 "parser.y" + + RawAST* a; /* For ast node */ + int64_t d; + char* s; + +# line 174 "parser.cpp" +}; +typedef union YYSTYPE YYSTYPE; +# define YYSTYPE_IS_TRIVIAL 1 +# define YYSTYPE_IS_DECLARED 1 +# endif + +extern YYSTYPE yylval; + +int +yyparse(RawAST** result); + +#endif /* !YY_YY_ROCPROFILER_SOURCE_LIB_ROCPROFILER_COUNTERS_PARSER_PARSER_H_INCLUDED */ + +#ifdef short +# undef short +#endif + +/* On compilers that do not define __PTRDIFF_MAX__ etc., make sure + and (if available) are included + so that the code can choose integer types of a good width. */ + +#ifndef __PTRDIFF_MAX__ +# include /* INFRINGES ON USER NAME SPACE */ +# if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__ +# include /* INFRINGES ON USER NAME SPACE */ +# define YY_STDINT_H +# endif +#endif + +/* Narrow types that promote to a signed type and that can represent a + signed or unsigned integer of at least N bits. In tables they can + save space and decrease cache pressure. Promoting to a signed type + helps avoid bugs in integer arithmetic. */ + +#ifdef __INT_LEAST8_MAX__ +typedef __INT_LEAST8_TYPE__ yytype_int8; +#elif defined YY_STDINT_H +typedef int_least8_t yytype_int8; +#else +typedef signed char yytype_int8; +#endif + +#ifdef __INT_LEAST16_MAX__ +typedef __INT_LEAST16_TYPE__ yytype_int16; +#elif defined YY_STDINT_H +typedef int_least16_t yytype_int16; +#else +typedef short yytype_int16; +#endif + +#if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__ +typedef __UINT_LEAST8_TYPE__ yytype_uint8; +#elif(!defined __UINT_LEAST8_MAX__ && defined YY_STDINT_H && UINT_LEAST8_MAX <= INT_MAX) +typedef uint_least8_t yytype_uint8; +#elif !defined __UINT_LEAST8_MAX__ && UCHAR_MAX <= INT_MAX +typedef unsigned char yytype_uint8; +#else +typedef short yytype_uint8; +#endif + +#if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__ +typedef __UINT_LEAST16_TYPE__ yytype_uint16; +#elif(!defined __UINT_LEAST16_MAX__ && defined YY_STDINT_H && UINT_LEAST16_MAX <= INT_MAX) +typedef uint_least16_t yytype_uint16; +#elif !defined __UINT_LEAST16_MAX__ && USHRT_MAX <= INT_MAX +typedef unsigned short yytype_uint16; +#else +typedef int yytype_uint16; +#endif + +#ifndef YYPTRDIFF_T +# if defined __PTRDIFF_TYPE__ && defined __PTRDIFF_MAX__ +# define YYPTRDIFF_T __PTRDIFF_TYPE__ +# define YYPTRDIFF_MAXIMUM __PTRDIFF_MAX__ +# elif defined PTRDIFF_MAX +# ifndef ptrdiff_t +# include /* INFRINGES ON USER NAME SPACE */ +# endif +# define YYPTRDIFF_T ptrdiff_t +# define YYPTRDIFF_MAXIMUM PTRDIFF_MAX +# else +# define YYPTRDIFF_T long +# define YYPTRDIFF_MAXIMUM LONG_MAX +# endif +#endif + +#ifndef YYSIZE_T +# ifdef __SIZE_TYPE__ +# define YYSIZE_T __SIZE_TYPE__ +# elif defined size_t +# define YYSIZE_T size_t +# elif defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__ +# include /* INFRINGES ON USER NAME SPACE */ +# define YYSIZE_T size_t +# else +# define YYSIZE_T unsigned +# endif +#endif + +#define YYSIZE_MAXIMUM \ + YY_CAST( \ + YYPTRDIFF_T, \ + (YYPTRDIFF_MAXIMUM < YY_CAST(YYSIZE_T, -1) ? YYPTRDIFF_MAXIMUM : YY_CAST(YYSIZE_T, -1))) + +#define YYSIZEOF(X) YY_CAST(YYPTRDIFF_T, sizeof(X)) + +/* Stored state numbers (used for stacks). */ +typedef yytype_int8 yy_state_t; + +/* State numbers in computations. */ +typedef int yy_state_fast_t; + +#ifndef YY_ +# if defined YYENABLE_NLS && YYENABLE_NLS +# if ENABLE_NLS +# include /* INFRINGES ON USER NAME SPACE */ +# define YY_(Msgid) dgettext("bison-runtime", Msgid) +# endif +# endif +# ifndef YY_ +# define YY_(Msgid) Msgid +# endif +#endif + +#ifndef YY_ATTRIBUTE_PURE +# if defined __GNUC__ && 2 < __GNUC__ + (96 <= __GNUC_MINOR__) +# define YY_ATTRIBUTE_PURE __attribute__((__pure__)) +# else +# define YY_ATTRIBUTE_PURE +# endif +#endif + +#ifndef YY_ATTRIBUTE_UNUSED +# if defined __GNUC__ && 2 < __GNUC__ + (7 <= __GNUC_MINOR__) +# define YY_ATTRIBUTE_UNUSED __attribute__((__unused__)) +# else +# define YY_ATTRIBUTE_UNUSED +# endif +#endif + +/* Suppress unused-variable warnings by "using" E. */ +#if !defined lint || defined __GNUC__ +# define YYUSE(E) ((void) (E)) +#else +# define YYUSE(E) /* empty */ +#endif + +#if defined __GNUC__ && !defined __ICC && 407 <= __GNUC__ * 100 + __GNUC_MINOR__ +/* Suppress an incorrect diagnostic about yylval being uninitialized. */ +# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \ + _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wuninitialized\"") \ + _Pragma("GCC diagnostic ignored \"-Wmaybe-uninitialized\"") +# define YY_IGNORE_MAYBE_UNINITIALIZED_END _Pragma("GCC diagnostic pop") +#else +# define YY_INITIAL_VALUE(Value) Value +#endif +#ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN +# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN +# define YY_IGNORE_MAYBE_UNINITIALIZED_END +#endif +#ifndef YY_INITIAL_VALUE +# define YY_INITIAL_VALUE(Value) /* Nothing. */ +#endif + +#if defined __cplusplus && defined __GNUC__ && !defined __ICC && 6 <= __GNUC__ +# define YY_IGNORE_USELESS_CAST_BEGIN \ + _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wuseless-cast\"") +# define YY_IGNORE_USELESS_CAST_END _Pragma("GCC diagnostic pop") +#endif +#ifndef YY_IGNORE_USELESS_CAST_BEGIN +# define YY_IGNORE_USELESS_CAST_BEGIN +# define YY_IGNORE_USELESS_CAST_END +#endif + +#define YY_ASSERT(E) ((void) (0 && (E))) + +#if !defined yyoverflow || YYERROR_VERBOSE + +/* The parser invokes alloca or malloc; define the necessary symbols. */ + +# ifdef YYSTACK_USE_ALLOCA +# if YYSTACK_USE_ALLOCA +# ifdef __GNUC__ +# define YYSTACK_ALLOC __builtin_alloca +# elif defined __BUILTIN_VA_ARG_INCR +# include /* INFRINGES ON USER NAME SPACE */ +# elif defined _AIX +# define YYSTACK_ALLOC __alloca +# elif defined _MSC_VER +# include /* INFRINGES ON USER NAME SPACE */ +# define alloca _alloca +# else +# define YYSTACK_ALLOC alloca +# if !defined _ALLOCA_H && !defined EXIT_SUCCESS +# include /* INFRINGES ON USER NAME SPACE */ +/* Use EXIT_SUCCESS as a witness for stdlib.h. */ +# ifndef EXIT_SUCCESS +# define EXIT_SUCCESS 0 +# endif +# endif +# endif +# endif +# endif + +# ifdef YYSTACK_ALLOC +/* Pacify GCC's 'empty if-body' warning. */ +# define YYSTACK_FREE(Ptr) \ + do \ + { /* empty */ \ + ; \ + } while(0) +# ifndef YYSTACK_ALLOC_MAXIMUM +/* The OS might guarantee only one guard page at the bottom of the stack, + and a page size can be as small as 4096 bytes. So we cannot safely + invoke alloca (N) if N exceeds 4096. Use a slightly smaller number + to allow for a few compiler-allocated temporary stack slots. */ +# define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */ +# endif +# else +# define YYSTACK_ALLOC YYMALLOC +# define YYSTACK_FREE YYFREE +# ifndef YYSTACK_ALLOC_MAXIMUM +# define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM +# endif +# if(defined __cplusplus && !defined EXIT_SUCCESS && \ + !((defined YYMALLOC || defined malloc) && (defined YYFREE || defined free))) +# include /* INFRINGES ON USER NAME SPACE */ +# ifndef EXIT_SUCCESS +# define EXIT_SUCCESS 0 +# endif +# endif +# ifndef YYMALLOC +# define YYMALLOC malloc +# if !defined malloc && !defined EXIT_SUCCESS +void* malloc(YYSIZE_T); /* INFRINGES ON USER NAME SPACE */ +# endif +# endif +# ifndef YYFREE +# define YYFREE free +# if !defined free && !defined EXIT_SUCCESS +void +free(void*); /* INFRINGES ON USER NAME SPACE */ +# endif +# endif +# endif +#endif /* ! defined yyoverflow || YYERROR_VERBOSE */ + +#if(!defined yyoverflow && \ + (!defined __cplusplus || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL))) + +/* A type that is properly aligned for any stack member. */ +union yyalloc +{ + yy_state_t yyss_alloc; + YYSTYPE yyvs_alloc; +}; + +/* The size of the maximum gap between one aligned stack and the next. */ +# define YYSTACK_GAP_MAXIMUM (YYSIZEOF(union yyalloc) - 1) + +/* The size of an array large to enough to hold all stacks, each with + N elements. */ +# define YYSTACK_BYTES(N) \ + ((N) * (YYSIZEOF(yy_state_t) + YYSIZEOF(YYSTYPE)) + YYSTACK_GAP_MAXIMUM) + +# define YYCOPY_NEEDED 1 + +/* Relocate STACK from its old location to the new one. The + local variables YYSIZE and YYSTACKSIZE give the old and new number of + elements in the stack, and YYPTR gives the new location of the + stack. Advance YYPTR to a properly aligned location for the next + stack. */ +# define YYSTACK_RELOCATE(Stack_alloc, Stack) \ + do \ + { \ + YYPTRDIFF_T yynewbytes; \ + YYCOPY(&yyptr->Stack_alloc, Stack, yysize); \ + Stack = &yyptr->Stack_alloc; \ + yynewbytes = yystacksize * YYSIZEOF(*Stack) + YYSTACK_GAP_MAXIMUM; \ + yyptr += yynewbytes / YYSIZEOF(*yyptr); \ + } while(0) + +#endif + +#if defined YYCOPY_NEEDED && YYCOPY_NEEDED +/* Copy COUNT objects from SRC to DST. The source and destination do + not overlap. */ +# ifndef YYCOPY +# if defined __GNUC__ && 1 < __GNUC__ +# define YYCOPY(Dst, Src, Count) \ + __builtin_memcpy(Dst, Src, YY_CAST(YYSIZE_T, (Count)) * sizeof(*(Src))) +# else +# define YYCOPY(Dst, Src, Count) \ + do \ + { \ + YYPTRDIFF_T yyi; \ + for(yyi = 0; yyi < (Count); yyi++) \ + (Dst)[yyi] = (Src)[yyi]; \ + } while(0) +# endif +# endif +#endif /* !YYCOPY_NEEDED */ + +/* YYFINAL -- State number of the termination state. */ +#define YYFINAL 14 +/* YYLAST -- Last index in YYTABLE. */ +#define YYLAST 84 + +/* YYNTOKENS -- Number of terminals. */ +#define YYNTOKENS 25 +/* YYNNTS -- Number of nonterminals. */ +#define YYNNTS 3 +/* YYNRULES -- Number of rules. */ +#define YYNRULES 16 +/* YYNSTATES -- Number of states. */ +#define YYNSTATES 44 + +#define YYUNDEFTOK 2 +#define YYMAXUTOK 278 + +/* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM + as returned by yylex, with out-of-bounds checking. */ +#define YYTRANSLATE(YYX) (0 <= (YYX) && (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK) + +/* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM + as returned by yylex. */ +static const yytype_int8 yytranslate[] = { + 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 15, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, + 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24}; + +#if YYDEBUG +/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ +static const yytype_int8 yyrline[] = + {0, 55, 55, 66, 67, 68, 69, 70, 71, 72, 73, 76, 79, 82, 85, 88, 91}; +#endif + +#if YYDEBUG || YYERROR_VERBOSE || 0 +/* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM. + First, the terminals, then, starting at YYNTOKENS, nonterminals. */ +static const char* const yytname[] = { + "$end", "error", "$undefined", "ADD", "SUB", "MUL", "DIV", "ABS", + "EQUALS", "OP", "CP", "O_SQ", "C_SQ", "COLON", "EOL", "'|'", + "UMINUS", "CM", "NUMBER", "RANGE", "NAME", "REDUCE", "SELECT", "LOWER_THAN_ELSE", + "ELSE", "$accept", "top", "exp", YY_NULLPTR}; +#endif + +#ifdef YYPRINT +/* YYTOKNUM[NUM] -- (External) token number corresponding to the + (internal) symbol number NUM (which must be that of a token). */ +static const yytype_int16 yytoknum[] = {0, 256, 257, 258, 259, 260, 261, 262, 263, + 264, 265, 266, 267, 268, 269, 124, 270, 271, + 272, 273, 274, 275, 276, 277, 278}; +#endif + +#define YYPACT_NINF (-9) + +#define yypact_value_is_default(Yyn) ((Yyn) == YYPACT_NINF) + +#define YYTABLE_NINF (-1) + +#define yytable_value_is_error(Yyn) 0 + +/* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing + STATE-NUM. */ +static const yytype_int8 yypact[] = {24, 24, 24, -9, -5, 4, 28, 8, 21, 58, 44, 24, 24, 24, -9, + 24, 24, 24, 24, -9, 24, 1, 17, 26, 34, 34, -9, -9, 48, 24, + 35, 36, -9, -9, -8, 49, -9, 24, -9, 24, 66, 74, -9, -9}; + +/* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. + Performed when YYTABLE does not specify something else to do. Zero + means the default is an error. */ +static const yytype_int8 yydefact[] = {0, 0, 0, 3, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 8, 0, 11, 0, 0, 4, 5, 6, 7, 0, 0, + 0, 0, 9, 12, 0, 0, 13, 0, 15, 0, 0, 0, 14, 16}; + +/* YYPGOTO[NTERM-NUM]. */ +static const yytype_int8 yypgoto[] = {-9, -9, -1}; + +/* YYDEFGOTO[NTERM-NUM]. */ +static const yytype_int8 yydefgoto[] = {-1, 7, 8}; + +/* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If + positive, shift that token. If negative, reduce the rule whose + number is the opposite. If YYTABLE_NINF, syntax error. */ +static const yytype_int8 yytable[] = { + 9, 10, 36, 11, 15, 16, 17, 18, 14, 37, 21, 22, 23, 12, 24, 25, 26, 27, 29, 28, 15, 16, + 17, 18, 15, 16, 17, 18, 33, 15, 16, 17, 18, 1, 30, 2, 40, 13, 41, 17, 18, 0, 3, 31, + 4, 5, 6, 15, 16, 17, 18, 15, 16, 17, 18, 34, 35, 20, 0, 38, 32, 15, 16, 17, 18, 0, + 39, 0, 19, 15, 16, 17, 18, 0, 0, 0, 42, 15, 16, 17, 18, 0, 0, 0, 43}; + +static const yytype_int8 yycheck[] = { + 1, 2, 10, 8, 3, 4, 5, 6, 0, 17, 11, 12, 13, 9, 15, 16, 17, 18, 17, 20, 3, 4, + 5, 6, 3, 4, 5, 6, 29, 3, 4, 5, 6, 9, 17, 11, 37, 9, 39, 5, 6, -1, 18, 17, + 20, 21, 22, 3, 4, 5, 6, 3, 4, 5, 6, 20, 20, 13, -1, 10, 12, 3, 4, 5, 6, -1, + 17, -1, 10, 3, 4, 5, 6, -1, -1, -1, 10, 3, 4, 5, 6, -1, -1, -1, 10}; + +/* YYSTOS[STATE-NUM] -- The (internal number of the) accessing + symbol of state STATE-NUM. */ +static const yytype_int8 yystos[] = {0, 9, 11, 18, 20, 21, 22, 26, 27, 27, 27, 8, 9, 9, 0, + 3, 4, 5, 6, 10, 13, 27, 27, 27, 27, 27, 27, 27, 27, 17, + 17, 17, 12, 27, 20, 20, 10, 17, 10, 17, 27, 27, 10, 10}; + +/* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ +static const yytype_int8 yyr1[] = + {0, 25, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27}; + +/* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */ +static const yytype_int8 yyr2[] = {0, 2, 1, 1, 3, 3, 3, 3, 3, 5, 1, 3, 5, 6, 8, 6, 8}; + +#define yyerrok (yyerrstatus = 0) +#define yyclearin (yychar = YYEMPTY) +#define YYEMPTY (-2) +#define YYEOF 0 + +#define YYACCEPT goto yyacceptlab +#define YYABORT goto yyabortlab +#define YYERROR goto yyerrorlab + +#define YYRECOVERING() (!!yyerrstatus) + +#define YYBACKUP(Token, Value) \ + do \ + if(yychar == YYEMPTY) \ + { \ + yychar = (Token); \ + yylval = (Value); \ + YYPOPSTACK(yylen); \ + yystate = *yyssp; \ + goto yybackup; \ + } \ + else \ + { \ + yyerror(result, YY_("syntax error: cannot back up")); \ + YYERROR; \ + } \ + while(0) + +/* Error token number */ +#define YYTERROR 1 +#define YYERRCODE 256 + +/* Enable debugging if requested. */ +#if YYDEBUG + +# ifndef YYFPRINTF +# include /* INFRINGES ON USER NAME SPACE */ +# define YYFPRINTF fprintf +# endif + +# define YYDPRINTF(Args) \ + do \ + { \ + if(yydebug) YYFPRINTF Args; \ + } while(0) + +/* This macro is provided for backward compatibility. */ +# ifndef YY_LOCATION_PRINT +# define YY_LOCATION_PRINT(File, Loc) ((void) 0) +# endif + +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) \ + do \ + { \ + if(yydebug) \ + { \ + YYFPRINTF(stderr, "%s ", Title); \ + yy_symbol_print(stderr, Type, Value, result); \ + YYFPRINTF(stderr, "\n"); \ + } \ + } while(0) + +/*-----------------------------------. +| Print this symbol's value on YYO. | +`-----------------------------------*/ + +static void +yy_symbol_value_print(FILE* yyo, int yytype, YYSTYPE const* const yyvaluep, RawAST** result) +{ + FILE* yyoutput = yyo; + YYUSE(yyoutput); + YYUSE(result); + if(!yyvaluep) return; +# ifdef YYPRINT + if(yytype < YYNTOKENS) YYPRINT(yyo, yytoknum[yytype], *yyvaluep); +# endif + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + YYUSE(yytype); + YY_IGNORE_MAYBE_UNINITIALIZED_END +} + +/*---------------------------. +| Print this symbol on YYO. | +`---------------------------*/ + +static void +yy_symbol_print(FILE* yyo, int yytype, YYSTYPE const* const yyvaluep, RawAST** result) +{ + YYFPRINTF(yyo, "%s %s (", yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]); + + yy_symbol_value_print(yyo, yytype, yyvaluep, result); + YYFPRINTF(yyo, ")"); +} + +/*------------------------------------------------------------------. +| yy_stack_print -- Print the state stack from its BOTTOM up to its | +| TOP (included). | +`------------------------------------------------------------------*/ + +static void +yy_stack_print(yy_state_t* yybottom, yy_state_t* yytop) +{ + YYFPRINTF(stderr, "Stack now"); + for(; yybottom <= yytop; yybottom++) + { + int yybot = *yybottom; + YYFPRINTF(stderr, " %d", yybot); + } + YYFPRINTF(stderr, "\n"); +} + +# define YY_STACK_PRINT(Bottom, Top) \ + do \ + { \ + if(yydebug) yy_stack_print((Bottom), (Top)); \ + } while(0) + +/*------------------------------------------------. +| Report that the YYRULE is going to be reduced. | +`------------------------------------------------*/ + +static void +yy_reduce_print(yy_state_t* yyssp, YYSTYPE* yyvsp, int yyrule, RawAST** result) +{ + int yylno = yyrline[yyrule]; + int yynrhs = yyr2[yyrule]; + int yyi; + YYFPRINTF(stderr, "Reducing stack by rule %d (line %d):\n", yyrule - 1, yylno); + /* The symbols being reduced. */ + for(yyi = 0; yyi < yynrhs; yyi++) + { + YYFPRINTF(stderr, " $%d = ", yyi + 1); + yy_symbol_print( + stderr, yystos[+yyssp[yyi + 1 - yynrhs]], &yyvsp[(yyi + 1) - (yynrhs)], result); + YYFPRINTF(stderr, "\n"); + } +} + +# define YY_REDUCE_PRINT(Rule) \ + do \ + { \ + if(yydebug) yy_reduce_print(yyssp, yyvsp, Rule, result); \ + } while(0) + +/* Nonzero means print parse trace. It is left uninitialized so that + multiple parsers can coexist. */ +int yydebug; +#else /* !YYDEBUG */ +# define YYDPRINTF(Args) +# define YY_SYMBOL_PRINT(Title, Type, Value, Location) +# define YY_STACK_PRINT(Bottom, Top) +# define YY_REDUCE_PRINT(Rule) +#endif /* !YYDEBUG */ + +/* YYINITDEPTH -- initial size of the parser's stacks. */ +#ifndef YYINITDEPTH +# define YYINITDEPTH 200 +#endif + +/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only + if the built-in stack extension method is used). + + Do not make this value too large; the results are undefined if + YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH) + evaluated with infinite-precision integer arithmetic. */ + +#ifndef YYMAXDEPTH +# define YYMAXDEPTH 10000 +#endif + +#if YYERROR_VERBOSE + +# ifndef yystrlen +# if defined __GLIBC__ && defined _STRING_H +# define yystrlen(S) (YY_CAST(YYPTRDIFF_T, strlen(S))) +# else +/* Return the length of YYSTR. */ +static YYPTRDIFF_T +yystrlen(const char* yystr) +{ + YYPTRDIFF_T yylen; + for(yylen = 0; yystr[yylen]; yylen++) + continue; + return yylen; +} +# endif +# endif + +# ifndef yystpcpy +# if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE +# define yystpcpy stpcpy +# else +/* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in + YYDEST. */ +static char* +yystpcpy(char* yydest, const char* yysrc) +{ + char* yyd = yydest; + const char* yys = yysrc; + + while((*yyd++ = *yys++) != '\0') + continue; + + return yyd - 1; +} +# endif +# endif + +# ifndef yytnamerr +/* Copy to YYRES the contents of YYSTR after stripping away unnecessary + quotes and backslashes, so that it's suitable for yyerror. The + heuristic is that double-quoting is unnecessary unless the string + contains an apostrophe, a comma, or backslash (other than + backslash-backslash). YYSTR is taken from yytname. If YYRES is + null, do not copy; instead, return the length of what the result + would have been. */ +static YYPTRDIFF_T +yytnamerr(char* yyres, const char* yystr) +{ + if(*yystr == '"') + { + YYPTRDIFF_T yyn = 0; + char const* yyp = yystr; + + for(;;) + switch(*++yyp) + { + case '\'': + case ',': goto do_not_strip_quotes; + + case '\\': + if(*++yyp != '\\') + goto do_not_strip_quotes; + else + goto append; + + append: + default: + if(yyres) yyres[yyn] = *yyp; + yyn++; + break; + + case '"': + if(yyres) yyres[yyn] = '\0'; + return yyn; + } + do_not_strip_quotes:; + } + + if(yyres) + return yystpcpy(yyres, yystr) - yyres; + else + return yystrlen(yystr); +} +# endif + +/* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message + about the unexpected token YYTOKEN for the state stack whose top is + YYSSP. + + Return 0 if *YYMSG was successfully written. Return 1 if *YYMSG is + not large enough to hold the message. In that case, also set + *YYMSG_ALLOC to the required number of bytes. Return 2 if the + required number of bytes is too large to store. */ +static int +yysyntax_error(YYPTRDIFF_T* yymsg_alloc, char** yymsg, yy_state_t* yyssp, int yytoken) +{ + enum + { + YYERROR_VERBOSE_ARGS_MAXIMUM = 5 + }; + /* Internationalized format string. */ + const char* yyformat = YY_NULLPTR; + /* Arguments of yyformat: reported tokens (one for the "unexpected", + one per "expected"). */ + char const* yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM]; + /* Actual size of YYARG. */ + int yycount = 0; + /* Cumulated lengths of YYARG. */ + YYPTRDIFF_T yysize = 0; + + /* There are many possibilities here to consider: + - If this state is a consistent state with a default action, then + the only way this function was invoked is if the default action + is an error action. In that case, don't check for expected + tokens because there are none. + - The only way there can be no lookahead present (in yychar) is if + this state is a consistent state with a default action. Thus, + detecting the absence of a lookahead is sufficient to determine + that there is no unexpected or expected token to report. In that + case, just report a simple "syntax error". + - Don't assume there isn't a lookahead just because this state is a + consistent state with a default action. There might have been a + previous inconsistent state, consistent state with a non-default + action, or user semantic action that manipulated yychar. + - Of course, the expected token list depends on states to have + correct lookahead information, and it depends on the parser not + to perform extra reductions after fetching a lookahead from the + scanner and before detecting a syntax error. Thus, state merging + (from LALR or IELR) and default reductions corrupt the expected + token list. However, the list is correct for canonical LR with + one exception: it will still contain any token that will not be + accepted due to an error action in a later state. + */ + if(yytoken != YYEMPTY) + { + int yyn = yypact[+*yyssp]; + YYPTRDIFF_T yysize0 = yytnamerr(YY_NULLPTR, yytname[yytoken]); + yysize = yysize0; + yyarg[yycount++] = yytname[yytoken]; + if(!yypact_value_is_default(yyn)) + { + /* Start YYX at -YYN if negative to avoid negative indexes in + YYCHECK. In other words, skip the first -YYN actions for + this state because they are default actions. */ + int yyxbegin = yyn < 0 ? -yyn : 0; + /* Stay within bounds of both yycheck and yytname. */ + int yychecklim = YYLAST - yyn + 1; + int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS; + int yyx; + + for(yyx = yyxbegin; yyx < yyxend; ++yyx) + if(yycheck[yyx + yyn] == yyx && yyx != YYTERROR && + !yytable_value_is_error(yytable[yyx + yyn])) + { + if(yycount == YYERROR_VERBOSE_ARGS_MAXIMUM) + { + yycount = 1; + yysize = yysize0; + break; + } + yyarg[yycount++] = yytname[yyx]; + { + YYPTRDIFF_T yysize1 = yysize + yytnamerr(YY_NULLPTR, yytname[yyx]); + if(yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM) + yysize = yysize1; + else + return 2; + } + } + } + } + + switch(yycount) + { +# define YYCASE_(N, S) \ + case N: yyformat = S; break + default: /* Avoid compiler warnings. */ + YYCASE_(0, YY_("syntax error")); + YYCASE_(1, YY_("syntax error, unexpected %s")); + YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s")); + YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s")); + YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s")); + YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s")); +# undef YYCASE_ + } + + { + /* Don't count the "%s"s in the final size, but reserve room for + the terminator. */ + YYPTRDIFF_T yysize1 = yysize + (yystrlen(yyformat) - 2 * yycount) + 1; + if(yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM) + yysize = yysize1; + else + return 2; + } + + if(*yymsg_alloc < yysize) + { + *yymsg_alloc = 2 * yysize; + if(!(yysize <= *yymsg_alloc && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM)) + *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM; + return 1; + } + + /* Avoid sprintf, as that infringes on the user's name space. + Don't have undefined behavior even if the translation + produced a string with the wrong number of "%s"s. */ + { + char* yyp = *yymsg; + int yyi = 0; + while((*yyp = *yyformat) != '\0') + if(*yyp == '%' && yyformat[1] == 's' && yyi < yycount) + { + yyp += yytnamerr(yyp, yyarg[yyi++]); + yyformat += 2; + } + else + { + ++yyp; + ++yyformat; + } + } + return 0; +} +#endif /* YYERROR_VERBOSE */ + +/*-----------------------------------------------. +| Release the memory associated to this symbol. | +`-----------------------------------------------*/ + +static void +yydestruct(const char* yymsg, int yytype, YYSTYPE* yyvaluep, RawAST** result) +{ + YYUSE(yyvaluep); + YYUSE(result); + if(!yymsg) yymsg = "Deleting"; + YY_SYMBOL_PRINT(yymsg, yytype, yyvaluep, yylocationp); + + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + YYUSE(yytype); + YY_IGNORE_MAYBE_UNINITIALIZED_END +} + +/* The lookahead symbol. */ +int yychar; + +/* The semantic value of the lookahead symbol. */ +YYSTYPE yylval; +/* Number of syntax errors so far. */ +int yynerrs; + +/*----------. +| yyparse. | +`----------*/ + +int +yyparse(RawAST** result) +{ + yy_state_fast_t yystate; + /* Number of tokens to shift before error messages enabled. */ + int yyerrstatus; + + /* The stacks and their tools: + 'yyss': related to states. + 'yyvs': related to semantic values. + + Refer to the stacks through separate pointers, to allow yyoverflow + to reallocate them elsewhere. */ + + /* The state stack. */ + yy_state_t yyssa[YYINITDEPTH]; + yy_state_t* yyss; + yy_state_t* yyssp; + + /* The semantic value stack. */ + YYSTYPE yyvsa[YYINITDEPTH]; + YYSTYPE* yyvs; + YYSTYPE* yyvsp; + + YYPTRDIFF_T yystacksize; + + int yyn; + int yyresult; + /* Lookahead token as an internal (translated) token number. */ + int yytoken = 0; + /* The variables used to return semantic value and location from the + action routines. */ + YYSTYPE yyval; + +#if YYERROR_VERBOSE + /* Buffer for error messages, and its allocated size. */ + char yymsgbuf[128]; + char* yymsg = yymsgbuf; + YYPTRDIFF_T yymsg_alloc = sizeof yymsgbuf; +#endif + +#define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N)) + + /* The number of symbols on the RHS of the reduced rule. + Keep to zero when no symbol should be popped. */ + int yylen = 0; + + yyssp = yyss = yyssa; + yyvsp = yyvs = yyvsa; + yystacksize = YYINITDEPTH; + + YYDPRINTF((stderr, "Starting parse\n")); + + yystate = 0; + yyerrstatus = 0; + yynerrs = 0; + yychar = YYEMPTY; /* Cause a token to be read. */ + goto yysetstate; + +/*------------------------------------------------------------. +| yynewstate -- push a new state, which is found in yystate. | +`------------------------------------------------------------*/ +yynewstate: + /* In all cases, when you get here, the value and location stacks + have just been pushed. So pushing a state here evens the stacks. */ + yyssp++; + +/*--------------------------------------------------------------------. +| yysetstate -- set current state (the top of the stack) to yystate. | +`--------------------------------------------------------------------*/ +yysetstate: + YYDPRINTF((stderr, "Entering state %d\n", yystate)); + YY_ASSERT(0 <= yystate && yystate < YYNSTATES); + YY_IGNORE_USELESS_CAST_BEGIN + *yyssp = YY_CAST(yy_state_t, yystate); + YY_IGNORE_USELESS_CAST_END + + if(yyss + yystacksize - 1 <= yyssp) +#if !defined yyoverflow && !defined YYSTACK_RELOCATE + goto yyexhaustedlab; +#else + { + /* Get the current used size of the three stacks, in elements. */ + YYPTRDIFF_T yysize = yyssp - yyss + 1; + +# if defined yyoverflow + { + /* Give user a chance to reallocate the stack. Use copies of + these so that the &'s don't force the real ones into + memory. */ + yy_state_t* yyss1 = yyss; + YYSTYPE* yyvs1 = yyvs; + + /* Each stack pointer address is followed by the size of the + data in use in that stack, in bytes. This used to be a + conditional around just the two extra args, but that might + be undefined if yyoverflow is a macro. */ + yyoverflow(YY_("memory exhausted"), + &yyss1, + yysize * YYSIZEOF(*yyssp), + &yyvs1, + yysize * YYSIZEOF(*yyvsp), + &yystacksize); + yyss = yyss1; + yyvs = yyvs1; + } +# else /* defined YYSTACK_RELOCATE */ + /* Extend the stack our own way. */ + if(YYMAXDEPTH <= yystacksize) goto yyexhaustedlab; + yystacksize *= 2; + if(YYMAXDEPTH < yystacksize) yystacksize = YYMAXDEPTH; + + { + yy_state_t* yyss1 = yyss; + union yyalloc* yyptr = YY_CAST( + union yyalloc*, YYSTACK_ALLOC(YY_CAST(YYSIZE_T, YYSTACK_BYTES(yystacksize)))); + if(!yyptr) goto yyexhaustedlab; + YYSTACK_RELOCATE(yyss_alloc, yyss); + YYSTACK_RELOCATE(yyvs_alloc, yyvs); +# undef YYSTACK_RELOCATE + if(yyss1 != yyssa) YYSTACK_FREE(yyss1); + } +# endif + + yyssp = yyss + yysize - 1; + yyvsp = yyvs + yysize - 1; + + YY_IGNORE_USELESS_CAST_BEGIN + YYDPRINTF((stderr, "Stack size increased to %ld\n", YY_CAST(long, yystacksize))); + YY_IGNORE_USELESS_CAST_END + + if(yyss + yystacksize - 1 <= yyssp) YYABORT; + } +#endif /* !defined yyoverflow && !defined YYSTACK_RELOCATE */ + + if(yystate == YYFINAL) YYACCEPT; + + goto yybackup; + +/*-----------. +| yybackup. | +`-----------*/ +yybackup: + /* Do appropriate processing given the current state. Read a + lookahead token if we need one and don't already have one. */ + + /* First try to decide what to do without reference to lookahead token. */ + yyn = yypact[yystate]; + if(yypact_value_is_default(yyn)) goto yydefault; + + /* Not known => get a lookahead token if don't already have one. */ + + /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol. */ + if(yychar == YYEMPTY) + { + YYDPRINTF((stderr, "Reading a token: ")); + yychar = yylex(); + } + + if(yychar <= YYEOF) + { + yychar = yytoken = YYEOF; + YYDPRINTF((stderr, "Now at end of input.\n")); + } + else + { + yytoken = YYTRANSLATE(yychar); + YY_SYMBOL_PRINT("Next token is", yytoken, &yylval, &yylloc); + } + + /* If the proper action on seeing token YYTOKEN is to reduce or to + detect an error, take that action. */ + yyn += yytoken; + if(yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken) goto yydefault; + yyn = yytable[yyn]; + if(yyn <= 0) + { + if(yytable_value_is_error(yyn)) goto yyerrlab; + yyn = -yyn; + goto yyreduce; + } + + /* Count tokens shifted since error; after three, turn off error + status. */ + if(yyerrstatus) yyerrstatus--; + + /* Shift the lookahead token. */ + YY_SYMBOL_PRINT("Shifting", yytoken, &yylval, &yylloc); + yystate = yyn; + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + *++yyvsp = yylval; + YY_IGNORE_MAYBE_UNINITIALIZED_END + + /* Discard the shifted token. */ + yychar = YYEMPTY; + goto yynewstate; + +/*-----------------------------------------------------------. +| yydefault -- do the default action for the current state. | +`-----------------------------------------------------------*/ +yydefault: + yyn = yydefact[yystate]; + if(yyn == 0) goto yyerrlab; + goto yyreduce; + +/*-----------------------------. +| yyreduce -- do a reduction. | +`-----------------------------*/ +yyreduce: + /* yyn is the number of a rule to reduce with. */ + yylen = yyr2[yyn]; + + /* If YYLEN is nonzero, implement the default value of the action: + '$$ = $1'. + + Otherwise, the following line sets YYVAL to garbage. + This behavior is undocumented and Bison + users should not rely upon it. Assigning to YYVAL + unconditionally makes the parser a bit smaller, and it avoids a + GCC warning that YYVAL may be used uninitialized. */ + yyval = yyvsp[1 - yylen]; + + YY_REDUCE_PRINT(yyn); + switch(yyn) + { + case 2: +#line 55 "parser.y" + { + *result = (yyvsp[0].a); + } +#line 1375 "parser.cpp" + break; + + case 3: +#line 66 "parser.y" + { + (yyval.a) = new RawAST(NUMBER_NODE, (yyvsp[0].d)); + } +#line 1381 "parser.cpp" + break; + + case 4: +#line 67 "parser.y" + { + (yyval.a) = new RawAST(ADDITION_NODE, {(yyvsp[-2].a), (yyvsp[0].a)}); + } +#line 1387 "parser.cpp" + break; + + case 5: +#line 68 "parser.y" + { + (yyval.a) = new RawAST(SUBTRACTION_NODE, {(yyvsp[-2].a), (yyvsp[0].a)}); + } +#line 1393 "parser.cpp" + break; + + case 6: +#line 69 "parser.y" + { + (yyval.a) = new RawAST(MULTIPLY_NODE, {(yyvsp[-2].a), (yyvsp[0].a)}); + } +#line 1399 "parser.cpp" + break; + + case 7: +#line 70 "parser.y" + { + (yyval.a) = new RawAST(DIVIDE_NODE, {(yyvsp[-2].a), (yyvsp[0].a)}); + } +#line 1405 "parser.cpp" + break; + + case 8: +#line 71 "parser.y" + { + (yyval.a) = (yyvsp[-1].a); + } +#line 1411 "parser.cpp" + break; + + case 9: +#line 72 "parser.y" + { + (yyval.a) = new RawAST(RANGE_NODE, {(yyvsp[-3].a), (yyvsp[-1].a)}); + } +#line 1417 "parser.cpp" + break; + + case 10: +#line 73 "parser.y" + { + (yyval.a) = new RawAST(REFERENCE_NODE, (yyvsp[0].s)); + free((yyvsp[0].s)); + } +#line 1425 "parser.cpp" + break; + + case 11: +#line 76 "parser.y" + { + (yyval.a) = new RawAST(REFERENCE_SET, (yyvsp[-2].s), (yyvsp[0].a)); + free((yyvsp[-2].s)); + } +#line 1433 "parser.cpp" + break; + + case 12: +#line 79 "parser.y" + { + (yyval.a) = new RawAST(REFERENCE_SET, (yyvsp[-4].s), (yyvsp[-2].a), (yyvsp[0].a)); + free((yyvsp[-4].s)); + } +#line 1441 "parser.cpp" + break; + + case 13: +#line 82 "parser.y" + { + (yyval.a) = new RawAST(REDUCE_NODE, (yyvsp[-3].a), (yyvsp[-1].s)); + free((yyvsp[-1].s)); + } +#line 1449 "parser.cpp" + break; + + case 14: +#line 85 "parser.y" + { + (yyval.a) = new RawAST(REDUCE_NODE, (yyvsp[-5].a), (yyvsp[-3].s), (yyvsp[-1].a)); + free((yyvsp[-3].s)); + } +#line 1457 "parser.cpp" + break; + + case 15: +#line 88 "parser.y" + { + (yyval.a) = new RawAST(SELECT_NODE, (yyvsp[-3].a), (yyvsp[-1].s)); + free((yyvsp[-1].s)); + } +#line 1465 "parser.cpp" + break; + + case 16: +#line 91 "parser.y" + { + (yyval.a) = new RawAST(SELECT_NODE, (yyvsp[-5].a), (yyvsp[-3].s), (yyvsp[-1].a)); + free((yyvsp[-3].s)); + } +#line 1473 "parser.cpp" + break; + +#line 1477 "parser.cpp" + + default: break; + } + /* User semantic actions sometimes alter yychar, and that requires + that yytoken be updated with the new translation. We take the + approach of translating immediately before every use of yytoken. + One alternative is translating here after every semantic action, + but that translation would be missed if the semantic action invokes + YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or + if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an + incorrect destructor might then be invoked immediately. In the + case of YYERROR or YYBACKUP, subsequent parser actions might lead + to an incorrect destructor call or verbose syntax error message + before the lookahead is translated. */ + YY_SYMBOL_PRINT("-> $$ =", yyr1[yyn], &yyval, &yyloc); + + YYPOPSTACK(yylen); + yylen = 0; + YY_STACK_PRINT(yyss, yyssp); + + *++yyvsp = yyval; + + /* Now 'shift' the result of the reduction. Determine what state + that goes to, based on the state we popped back to and the rule + number reduced by. */ + { + const int yylhs = yyr1[yyn] - YYNTOKENS; + const int yyi = yypgoto[yylhs] + *yyssp; + yystate = + (0 <= yyi && yyi <= YYLAST && yycheck[yyi] == *yyssp ? yytable[yyi] : yydefgoto[yylhs]); + } + + goto yynewstate; + +/*--------------------------------------. +| yyerrlab -- here on detecting error. | +`--------------------------------------*/ +yyerrlab: + /* Make sure we have latest lookahead translation. See comments at + user semantic actions for why this is necessary. */ + yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE(yychar); + + /* If not already recovering from an error, report this error. */ + if(!yyerrstatus) + { + ++yynerrs; +#if !YYERROR_VERBOSE + yyerror(result, YY_("syntax error")); +#else +# define YYSYNTAX_ERROR yysyntax_error(&yymsg_alloc, &yymsg, yyssp, yytoken) + { + char const* yymsgp = YY_("syntax error"); + int yysyntax_error_status; + yysyntax_error_status = YYSYNTAX_ERROR; + if(yysyntax_error_status == 0) + yymsgp = yymsg; + else if(yysyntax_error_status == 1) + { + if(yymsg != yymsgbuf) YYSTACK_FREE(yymsg); + yymsg = YY_CAST(char*, YYSTACK_ALLOC(YY_CAST(YYSIZE_T, yymsg_alloc))); + if(!yymsg) + { + yymsg = yymsgbuf; + yymsg_alloc = sizeof yymsgbuf; + yysyntax_error_status = 2; + } + else + { + yysyntax_error_status = YYSYNTAX_ERROR; + yymsgp = yymsg; + } + } + yyerror(result, yymsgp); + if(yysyntax_error_status == 2) goto yyexhaustedlab; + } +# undef YYSYNTAX_ERROR +#endif + } + + if(yyerrstatus == 3) + { + /* If just tried and failed to reuse lookahead token after an + error, discard it. */ + + if(yychar <= YYEOF) + { + /* Return failure if at end of input. */ + if(yychar == YYEOF) YYABORT; + } + else + { + yydestruct("Error: discarding", yytoken, &yylval, result); + yychar = YYEMPTY; + } + } + + /* Else will try to reuse lookahead token after shifting the error + token. */ + goto yyerrlab1; + +/*---------------------------------------------------. +| yyerrorlab -- error raised explicitly by YYERROR. | +`---------------------------------------------------*/ +yyerrorlab: + /* Pacify compilers when the user code never invokes YYERROR and the + label yyerrorlab therefore never appears in user code. */ + if(0) YYERROR; + + /* Do not reclaim the symbols of the rule whose action triggered + this YYERROR. */ + YYPOPSTACK(yylen); + yylen = 0; + YY_STACK_PRINT(yyss, yyssp); + yystate = *yyssp; + goto yyerrlab1; + +/*-------------------------------------------------------------. +| yyerrlab1 -- common code for both syntax error and YYERROR. | +`-------------------------------------------------------------*/ +yyerrlab1: + yyerrstatus = 3; /* Each real token shifted decrements this. */ + + for(;;) + { + yyn = yypact[yystate]; + if(!yypact_value_is_default(yyn)) + { + yyn += YYTERROR; + if(0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR) + { + yyn = yytable[yyn]; + if(0 < yyn) break; + } + } + + /* Pop the current state because it cannot handle the error token. */ + if(yyssp == yyss) YYABORT; + + yydestruct("Error: popping", yystos[yystate], yyvsp, result); + YYPOPSTACK(1); + yystate = *yyssp; + YY_STACK_PRINT(yyss, yyssp); + } + + YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN + *++yyvsp = yylval; + YY_IGNORE_MAYBE_UNINITIALIZED_END + + /* Shift the error token. */ + YY_SYMBOL_PRINT("Shifting", yystos[yyn], yyvsp, yylsp); + + yystate = yyn; + goto yynewstate; + +/*-------------------------------------. +| yyacceptlab -- YYACCEPT comes here. | +`-------------------------------------*/ +yyacceptlab: + yyresult = 0; + goto yyreturn; + +/*-----------------------------------. +| yyabortlab -- YYABORT comes here. | +`-----------------------------------*/ +yyabortlab: + yyresult = 1; + goto yyreturn; + +#if !defined yyoverflow || YYERROR_VERBOSE +/*-------------------------------------------------. +| yyexhaustedlab -- memory exhaustion comes here. | +`-------------------------------------------------*/ +yyexhaustedlab: + yyerror(result, YY_("memory exhausted")); + yyresult = 2; + /* Fall through. */ +#endif + +/*-----------------------------------------------------. +| yyreturn -- parsing is finished, return the result. | +`-----------------------------------------------------*/ +yyreturn: + if(yychar != YYEMPTY) + { + /* Make sure we have latest lookahead translation. See comments at + user semantic actions for why this is necessary. */ + yytoken = YYTRANSLATE(yychar); + yydestruct("Cleanup: discarding lookahead", yytoken, &yylval, result); + } + /* Do not reclaim the symbols of the rule whose action triggered + this YYABORT or YYACCEPT. */ + YYPOPSTACK(yylen); + YY_STACK_PRINT(yyss, yyssp); + while(yyssp != yyss) + { + yydestruct("Cleanup: popping", yystos[+*yyssp], yyvsp, result); + YYPOPSTACK(1); + } +#ifndef yyoverflow + if(yyss != yyssa) YYSTACK_FREE(yyss); +#endif +#if YYERROR_VERBOSE + if(yymsg != yymsgbuf) YYSTACK_FREE(yymsg); +#endif + return yyresult; +} +#line 98 "parser.y" + +// void yyerror(char const *s) +// { +// fprintf(stderr, "check error saurabh: %s\n", s); +// } diff --git a/source/lib/rocprofiler/counters/parser/parser.h b/source/lib/rocprofiler/counters/parser/parser.h new file mode 100644 index 0000000000..0fe37fd999 --- /dev/null +++ b/source/lib/rocprofiler/counters/parser/parser.h @@ -0,0 +1,106 @@ +/* A Bison parser, made by GNU Bison 3.5.1. */ + +/* Bison interface for Yacc-like parsers in C + + Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2020 Free Software Foundation, + Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* As a special exception, you may create a larger work that contains + part or all of the Bison parser skeleton and distribute that work + under terms of your choice, so long as that work isn't itself a + parser generator using the skeleton or a modified version thereof + as a parser skeleton. Alternatively, if you modify or redistribute + the parser skeleton itself, you may (at your option) remove this + special exception, which will cause the skeleton and the resulting + Bison output files to be licensed under the GNU General Public + License without this special exception. + + This special exception was added by the Free Software Foundation in + version 2.2 of Bison. */ + +/* Undocumented macros, especially those whose name start with YY_, + are private implementation details. Do not rely on them. */ + +#ifndef YY_YY_ROCPROFILER_SOURCE_LIB_ROCPROFILER_COUNTERS_PARSER_PARSER_H_INCLUDED +#define YY_YY_ROCPROFILER_SOURCE_LIB_ROCPROFILER_COUNTERS_PARSER_PARSER_H_INCLUDED +/* Debug traces. */ +#ifndef YYDEBUG +# define YYDEBUG 1 +#endif +#if YYDEBUG +extern int yydebug; +#endif +/* "%code requires" blocks. */ +#line 2 "parser.y" + +#include "raw_ast.hpp" +using namespace rocprofiler::counters; +#define YYDEBUG 1 + +#line 54 "parser.h" + +/* Token type. */ +#ifndef YYTOKENTYPE +# define YYTOKENTYPE +enum yytokentype +{ + ADD = 258, + SUB = 259, + MUL = 260, + DIV = 261, + ABS = 262, + EQUALS = 263, + OP = 264, + CP = 265, + O_SQ = 266, + C_SQ = 267, + COLON = 268, + EOL = 269, + UMINUS = 270, + CM = 271, + NUMBER = 272, + RANGE = 273, + NAME = 274, + REDUCE = 275, + SELECT = 276, + LOWER_THAN_ELSE = 277, + ELSE = 278 +}; +#endif + +/* Value type. */ +#if !defined YYSTYPE && !defined YYSTYPE_IS_DECLARED +union YYSTYPE +{ +# line 34 "parser.y" + + RawAST* a; /* For ast node */ + int64_t d; + char* s; + +# line 95 "parser.h" +}; +typedef union YYSTYPE YYSTYPE; +# define YYSTYPE_IS_TRIVIAL 1 +# define YYSTYPE_IS_DECLARED 1 +#endif + +extern YYSTYPE yylval; + +int +yyparse(RawAST** result); + +#endif /* !YY_YY_ROCPROFILER_SOURCE_LIB_ROCPROFILER_COUNTERS_PARSER_PARSER_H_INCLUDED */ diff --git a/source/lib/rocprofiler/counters/parser/parser.y b/source/lib/rocprofiler/counters/parser/parser.y new file mode 100644 index 0000000000..e16dc03663 --- /dev/null +++ b/source/lib/rocprofiler/counters/parser/parser.y @@ -0,0 +1,103 @@ +%parse-param {RawAST** result} +%code requires { +#include "raw_ast.hpp" +using namespace rocprofiler::counters; +#define YYDEBUG 1 +} + +%{ +#include +#include +#include + +#include + +#include "raw_ast.hpp" + +int yyparse(rocprofiler::counters::RawAST** result); +int yylex(void); +void yyerror(rocprofiler::counters::RawAST**, const char *s) { LOG(ERROR) << s; } +%} + +/* declare tokens */ +%token ADD SUB MUL DIV ABS EQUALS +%token OP CP O_SQ C_SQ COLON +%token EOL + +/* set associativity rules for operand tokens */ +%right EQUALS +%left ADD SUB +%left MUL DIV +%nonassoc '|' UMINUS CM + +/*declare data types*/ +%union { + RawAST* a; /* For ast node */ + int64_t d; + char* s; +} + +%token NUMBER RANGE /* set data type for numbers */ +%token NAME /* set data type for variables and user-defined functions */ +%token REDUCE SELECT /* set data type for special functions */ +%type exp /* set data type for expressions */ +%type NAME +%type NUMBER + +%nonassoc LOWER_THAN_ELSE +%nonassoc ELSE + +// %token POS_INTEGER + +%% + +top: + exp { *result = $1;}; + +// line: /* nothing */ +// | line exp EOL { +// // TODO +// //printf("= %g\n", eval($2)); //evaluate and print the AST +// //printf("> "); +// } +// | line EOL { printf("> "); } /* blank line or a comment */ +// ; + +exp: NUMBER { $$ = new RawAST(NUMBER_NODE, $1); } + | exp ADD exp { $$ = new RawAST(ADDITION_NODE, {$1, $3}); } + | exp SUB exp { $$ = new RawAST(SUBTRACTION_NODE, {$1, $3}); } + | exp MUL exp { $$ = new RawAST(MULTIPLY_NODE, {$1, $3}); } + | exp DIV exp { $$ = new RawAST(DIVIDE_NODE, {$1, $3}); } + | OP exp CP { $$ = $2; } + | O_SQ exp COLON exp C_SQ { $$ = new RawAST(RANGE_NODE, {$2, $4}); } + | NAME { $$ = new RawAST(REFERENCE_NODE, $1); + free($1); + } + | NAME EQUALS exp { $$ = new RawAST(REFERENCE_SET, $1, $3); + free($1); + } + | NAME EQUALS exp CM exp { $$ = new RawAST(REFERENCE_SET, $1, $3, $5); + free($1); + } + | REDUCE OP exp CM NAME CP { $$ = new RawAST(REDUCE_NODE, $3, $5); + free($5); + } + | REDUCE OP exp CM NAME CM exp CP { $$ = new RawAST(REDUCE_NODE, $3, $5, $7); + free($5); + } + | SELECT OP exp CM NAME CP { $$ = new RawAST(SELECT_NODE, $3, $5); + free($5); + } + | SELECT OP exp CM NAME CM exp CP { $$ = new RawAST(SELECT_NODE, $3, $5, $7); + free($5); + } + // | NAME O_SQ POS_INTEGER C_SQ { $$ = create_index_access_node($1, $3); } + ; + + +%% + +// void yyerror(char const *s) +// { +// fprintf(stderr, "check error saurabh: %s\n", s); +// } diff --git a/source/lib/rocprofiler/counters/parser/raw_ast.hpp b/source/lib/rocprofiler/counters/parser/raw_ast.hpp new file mode 100644 index 0000000000..8c90f7186d --- /dev/null +++ b/source/lib/rocprofiler/counters/parser/raw_ast.hpp @@ -0,0 +1,189 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace rocprofiler +{ +namespace counters +{ +enum NodeType +{ + NONE = 0, + ADDITION_NODE, + DIVIDE_NODE, + MULTIPLY_NODE, + NUMBER_NODE, + RANGE_NODE, + REDUCE_NODE, + REFERENCE_NODE, + REFERENCE_SET, + SELECT_NODE, + SUBTRACTION_NODE, +}; + +struct RawAST +{ + // Node type + NodeType type{NONE}; // Operation to perform on the counter set + NodeType operation{NONE}; + + // Stores either the name or digit dependening on whether this + // is a name or number + std::variant value{std::monostate{}}; + + // Counter set of ASTs needed to compute this counter. + // Operation is applied to all counters in this set. + std::vector counter_set; + + // Reference set to remove dimensions (such as shader) + // from the result. This is a future looking change and + // will be unsupported in 6.0. + std::vector reference_set; + + // Range restriction on this node + RawAST* range{nullptr}; + + ~RawAST() + { + auto deleteVec = [](auto& vec) { + for(auto val : vec) + { + delete val; + } + }; + + deleteVec(reference_set); + deleteVec(counter_set); + delete range; + } + + // Constructors for raw value types + RawAST(NodeType t, const char* v) + : type(t) + , value(std::string{CHECK_NOTNULL(v)}) + {} + + RawAST(NodeType t, int64_t v) + : type(t) + , value(v) + {} + + // Reduce/Select operation constructor. Counter is the counter AST + // to use for the reduce/select op, op is how to reduce (i.e. SUM,AVG,etc), + // refs is the reference set AST. This reference set is copied to flatten + // the AST. + RawAST(NodeType t, RawAST* counter, const char* op, RawAST* refs = nullptr) + : type(t) + , value(std::string{CHECK_NOTNULL(op)}) + , counter_set({counter}) + { + copy_reference_set(refs); + } + + RawAST(NodeType t, std::vector c) + : type(t) + , counter_set(std::move(c)) + {} + + // Following two calls are for future reference set settings + // for select/reduce ops. + + // Referene set constructor, refs is a pointer to an existing + // reference set when multiple references are given (i.e. + // shader=X,anotherRef=Y,....). + RawAST(NodeType t, const char* v, RawAST* r, RawAST* refs = nullptr) + : type(t) + , value(std::string{CHECK_NOTNULL(v)}) + , range(r) + { + LOG(ERROR) << "BUilding bad ast"; + copy_reference_set(refs); + } + + // Flattens reference set tree into this node. + void copy_reference_set(RawAST* ast) + { + if(!ast) return; + reference_set.push_back(ast); + reference_set.insert( + reference_set.end(), ast->reference_set.begin(), ast->reference_set.end()); + ast->reference_set.clear(); + } +}; +} // namespace counters +} // namespace rocprofiler + +namespace fmt +{ +// fmt::format support for RawAST +template <> +struct formatter +{ + template + constexpr auto parse(ParseContext& ctx) + { + return ctx.begin(); + } + + template + auto format(rocprofiler::counters::RawAST const& ast, Ctx& ctx) const + { + static const std::map NodeTypeToString = { + {rocprofiler::counters::NONE, "NONE"}, + {rocprofiler::counters::ADDITION_NODE, "ADDITION_NODE"}, + {rocprofiler::counters::DIVIDE_NODE, "DIVIDE_NODE"}, + {rocprofiler::counters::MULTIPLY_NODE, "MULTIPLY_NODE"}, + {rocprofiler::counters::NUMBER_NODE, "NUMBER_NODE"}, + {rocprofiler::counters::RANGE_NODE, "RANGE_NODE"}, + {rocprofiler::counters::REDUCE_NODE, "REDUCE_NODE"}, + {rocprofiler::counters::REFERENCE_NODE, "REFERENCE_NODE"}, + {rocprofiler::counters::REFERENCE_SET, "REFERENCE_SET"}, + {rocprofiler::counters::SELECT_NODE, "SELECT_NODE"}, + {rocprofiler::counters::SUBTRACTION_NODE, "SUBTRACTION_NODE"}, + }; + + auto out = fmt::format_to(ctx.out(), + "{{\"Type\":\"{}\", \"Operation\":\"{}\",", + NodeTypeToString.at(ast.type), + NodeTypeToString.at(ast.operation)); + + if(const auto* string_val = std::get_if(&ast.value)) + { + out = fmt::format_to(out, " \"Value\":\"{}\",", *string_val); + } + else if(const auto* int_val = std::get_if(&ast.value)) + { + out = fmt::format_to(out, " \"Value\":{},", *int_val); + } + + if(ast.range) + { + out = fmt::format_to(out, " \"Range\":{},", *ast.range); + } + + out = fmt::format_to(out, "\"ReferenceSet\":["); + for(const auto& ref : ast.reference_set) + { + out = fmt::format_to( + out, "{}{}", *CHECK_NOTNULL(ref), ref == ast.reference_set.back() ? "" : ","); + } + + out = fmt::format_to(out, "], \"CounterSet\":["); + for(const auto& ref : ast.counter_set) + { + out = fmt::format_to( + out, "{}{}", *CHECK_NOTNULL(ref), ref == ast.counter_set.back() ? "" : ","); + } + return fmt::format_to(out, "]}}"); + } +}; +} // namespace fmt diff --git a/source/lib/rocprofiler/counters/parser/reader.hpp b/source/lib/rocprofiler/counters/parser/reader.hpp new file mode 100644 index 0000000000..bf894981c5 --- /dev/null +++ b/source/lib/rocprofiler/counters/parser/reader.hpp @@ -0,0 +1,12 @@ +#pragma ONCE + +#include "parser.h" + +// Bison functions for parsers +typedef struct yy_buffer_state* YY_BUFFER_STATE; +extern int +yyparse(rocprofiler::counters::RawAST** result); +extern YY_BUFFER_STATE +yy_scan_string(const char* str); +extern void +yy_delete_buffer(YY_BUFFER_STATE buffer); diff --git a/source/lib/rocprofiler/counters/parser/scanner.cpp b/source/lib/rocprofiler/counters/parser/scanner.cpp new file mode 100644 index 0000000000..e773ca9060 --- /dev/null +++ b/source/lib/rocprofiler/counters/parser/scanner.cpp @@ -0,0 +1,1867 @@ +#line 2 "scanner.cpp" + +#line 4 "scanner.cpp" + +#define YY_INT_ALIGNED short int + +/* A lexical scanner generated by flex */ + +#define FLEX_SCANNER +#define YY_FLEX_MAJOR_VERSION 2 +#define YY_FLEX_MINOR_VERSION 6 +#define YY_FLEX_SUBMINOR_VERSION 4 +#if YY_FLEX_SUBMINOR_VERSION > 0 +# define FLEX_BETA +#endif + +/* First, we deal with platform-specific or compiler-specific issues. */ + +/* begin standard C headers. */ +#include +#include +#include +#include + +/* end standard C headers. */ + +/* flex integer type definitions */ + +#ifndef FLEXINT_H +# define FLEXINT_H + +/* C99 systems have . Non-C99 systems may or may not. */ + +# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + +/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h, + * if you want the limit (max/min) macros for int types. + */ +# ifndef __STDC_LIMIT_MACROS +# define __STDC_LIMIT_MACROS 1 +# endif + +# include +typedef int8_t flex_int8_t; +typedef uint8_t flex_uint8_t; +typedef int16_t flex_int16_t; +typedef uint16_t flex_uint16_t; +typedef int32_t flex_int32_t; +typedef uint32_t flex_uint32_t; +# else +typedef signed char flex_int8_t; +typedef short int flex_int16_t; +typedef int flex_int32_t; +typedef unsigned char flex_uint8_t; +typedef unsigned short int flex_uint16_t; +typedef unsigned int flex_uint32_t; + +/* Limits of integral types. */ +# ifndef INT8_MIN +# define INT8_MIN (-128) +# endif +# ifndef INT16_MIN +# define INT16_MIN (-32767 - 1) +# endif +# ifndef INT32_MIN +# define INT32_MIN (-2147483647 - 1) +# endif +# ifndef INT8_MAX +# define INT8_MAX (127) +# endif +# ifndef INT16_MAX +# define INT16_MAX (32767) +# endif +# ifndef INT32_MAX +# define INT32_MAX (2147483647) +# endif +# ifndef UINT8_MAX +# define UINT8_MAX (255U) +# endif +# ifndef UINT16_MAX +# define UINT16_MAX (65535U) +# endif +# ifndef UINT32_MAX +# define UINT32_MAX (4294967295U) +# endif + +# ifndef SIZE_MAX +# define SIZE_MAX (~(size_t) 0) +# endif + +# endif /* ! C99 */ + +#endif /* ! FLEXINT_H */ + +/* begin standard C++ headers. */ + +/* TODO: this is always defined, so inline it */ +#define yyconst const + +#if defined(__GNUC__) && __GNUC__ >= 3 +# define yynoreturn __attribute__((__noreturn__)) +#else +# define yynoreturn +#endif + +/* Returned upon end-of-file. */ +#define YY_NULL 0 + +/* Promotes a possibly negative, possibly signed char to an + * integer in range [0..255] for use as an array index. + */ +#define YY_SC_TO_UI(c) ((YY_CHAR)(c)) + +/* Enter a start condition. This macro really ought to take a parameter, + * but we do it the disgusting crufty way forced on us by the ()-less + * definition of BEGIN. + */ +#define BEGIN (yy_start) = 1 + 2 * +/* Translate the current start state into a value that can be later handed + * to BEGIN to return to the state. The YYSTATE alias is for lex + * compatibility. + */ +#define YY_START (((yy_start) -1) / 2) +#define YYSTATE YY_START +/* Action number for EOF rule of a given start state. */ +#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1) +/* Special action meaning "start processing a new file". */ +#define YY_NEW_FILE yyrestart(yyin) +#define YY_END_OF_BUFFER_CHAR 0 + +/* Size of default input buffer. */ +#ifndef YY_BUF_SIZE +# ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k. + * Moreover, YY_BUF_SIZE is 2*YY_READ_BUF_SIZE in the general case. + * Ditto for the __ia64__ case accordingly. + */ +# define YY_BUF_SIZE 32768 +# else +# define YY_BUF_SIZE 16384 +# endif /* __ia64__ */ +#endif + +/* The state buf must be large enough to hold one state per character in the main buffer. + */ +#define YY_STATE_BUF_SIZE ((YY_BUF_SIZE + 2) * sizeof(yy_state_type)) + +#ifndef YY_TYPEDEF_YY_BUFFER_STATE +# define YY_TYPEDEF_YY_BUFFER_STATE +typedef struct yy_buffer_state* YY_BUFFER_STATE; +#endif + +#ifndef YY_TYPEDEF_YY_SIZE_T +# define YY_TYPEDEF_YY_SIZE_T +typedef size_t yy_size_t; +#endif + +extern int yyleng; + +extern FILE *yyin, *yyout; + +#define EOB_ACT_CONTINUE_SCAN 0 +#define EOB_ACT_END_OF_FILE 1 +#define EOB_ACT_LAST_MATCH 2 + +/* Note: We specifically omit the test for yy_rule_can_match_eol because it requires + * access to the local variable yy_act. Since yyless() is a macro, it would break + * existing scanners that call yyless() from OUTSIDE yylex. + * One obvious solution it to make yy_act a global. I tried that, and saw + * a 5% performance hit in a non-yylineno scanner, because yy_act is + * normally declared as a register variable-- so it is not worth it. + */ +#define YY_LESS_LINENO(n) \ + do \ + { \ + int yyl; \ + for(yyl = n; yyl < yyleng; ++yyl) \ + if(yytext[yyl] == '\n') --yylineno; \ + } while(0) +#define YY_LINENO_REWIND_TO(dst) \ + do \ + { \ + const char* p; \ + for(p = yy_cp - 1; p >= (dst); --p) \ + if(*p == '\n') --yylineno; \ + } while(0) + +/* Return all but the first "n" matched characters back to the input stream. */ +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg); \ + *yy_cp = (yy_hold_char); \ + YY_RESTORE_YY_MORE_OFFSET(yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \ + YY_DO_BEFORE_ACTION; /* set up yytext again */ \ + } while(0) +#define unput(c) yyunput(c, (yytext_ptr)) + +#ifndef YY_STRUCT_YY_BUFFER_STATE +# define YY_STRUCT_YY_BUFFER_STATE +struct yy_buffer_state +{ + FILE* yy_input_file; + + char* yy_ch_buf; /* input buffer */ + char* yy_buf_pos; /* current position in input buffer */ + + /* Size of input buffer in bytes, not including room for EOB + * characters. + */ + int yy_buf_size; + + /* Number of characters read into yy_ch_buf, not including EOB + * characters. + */ + int yy_n_chars; + + /* Whether we "own" the buffer - i.e., we know we created it, + * and can realloc() it to grow it, and should free() it to + * delete it. + */ + int yy_is_our_buffer; + + /* Whether this is an "interactive" input source; if so, and + * if we're using stdio for input, then we want to use getc() + * instead of fread(), to make sure we stop fetching input after + * each newline. + */ + int yy_is_interactive; + + /* Whether we're considered to be at the beginning of a line. + * If so, '^' rules will be active on the next match, otherwise + * not. + */ + int yy_at_bol; + + int yy_bs_lineno; /**< The line count. */ + int yy_bs_column; /**< The column count. */ + + /* Whether to try to fill the input buffer when we reach the + * end of it. + */ + int yy_fill_buffer; + + int yy_buffer_status; + +# define YY_BUFFER_NEW 0 +# define YY_BUFFER_NORMAL 1 + /* When an EOF's been seen but there's still some text to process + * then we mark the buffer as YY_EOF_PENDING, to indicate that we + * shouldn't try reading from the input source any more. We might + * still have a bunch of tokens to match, though, because of + * possible backing-up. + * + * When we actually see the EOF, we change the status to "new" + * (via yyrestart()), so that the user can continue scanning by + * just pointing yyin at a new input file. + */ +# define YY_BUFFER_EOF_PENDING 2 +}; +#endif /* !YY_STRUCT_YY_BUFFER_STATE */ + +/* Stack of input buffers. */ +static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */ +static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */ +static YY_BUFFER_STATE* yy_buffer_stack = NULL; /**< Stack as an array. */ + +/* We provide macros for accessing buffer states in case in the + * future we want to put the buffer states in a more general + * "scanner state". + * + * Returns the top of the stack, or NULL. + */ +#define YY_CURRENT_BUFFER ((yy_buffer_stack) ? (yy_buffer_stack)[(yy_buffer_stack_top)] : NULL) +/* Same as previous macro, but useful when we know that the buffer stack is not + * NULL or when we need an lvalue. For internal use only. + */ +#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)] + +/* yy_hold_char holds the character lost when yytext is formed. */ +static char yy_hold_char; +static int yy_n_chars; /* number of characters read into yy_ch_buf */ +int yyleng; + +/* Points to current character in buffer. */ +static char* yy_c_buf_p = NULL; +static int yy_init = 0; /* whether we need to initialize */ +static int yy_start = 0; /* start state number */ + +/* Flag which is used to allow yywrap()'s to do buffer switches + * instead of setting up a fresh yyin. A bit of a hack ... + */ +static int yy_did_buffer_switch_on_eof; + +void +yyrestart(FILE* input_file); +void +yy_switch_to_buffer(YY_BUFFER_STATE new_buffer); +YY_BUFFER_STATE +yy_create_buffer(FILE* file, int size); +void +yy_delete_buffer(YY_BUFFER_STATE b); +void +yy_flush_buffer(YY_BUFFER_STATE b); +void +yypush_buffer_state(YY_BUFFER_STATE new_buffer); +void +yypop_buffer_state(void); + +static void +yyensure_buffer_stack(void); +static void +yy_load_buffer_state(void); +static void +yy_init_buffer(YY_BUFFER_STATE b, FILE* file); +#define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER) + +YY_BUFFER_STATE +yy_scan_buffer(char* base, yy_size_t size); +YY_BUFFER_STATE +yy_scan_string(const char* yy_str); +YY_BUFFER_STATE +yy_scan_bytes(const char* bytes, int len); + +void* yyalloc(yy_size_t); +void* +yyrealloc(void*, yy_size_t); +void +yyfree(void*); + +#define yy_new_buffer yy_create_buffer +#define yy_set_interactive(is_interactive) \ + { \ + if(!YY_CURRENT_BUFFER) \ + { \ + yyensure_buffer_stack(); \ + YY_CURRENT_BUFFER_LVALUE = yy_create_buffer(yyin, YY_BUF_SIZE); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \ + } +#define yy_set_bol(at_bol) \ + { \ + if(!YY_CURRENT_BUFFER) \ + { \ + yyensure_buffer_stack(); \ + YY_CURRENT_BUFFER_LVALUE = yy_create_buffer(yyin, YY_BUF_SIZE); \ + } \ + YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \ + } +#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol) + +/* Begin user sect3 */ + +#define yywrap() (/*CONSTCOND*/ 1) +#define YY_SKIP_YYWRAP +typedef flex_uint8_t YY_CHAR; + +FILE *yyin = NULL, *yyout = NULL; + +typedef int yy_state_type; + +extern int yylineno; +int yylineno = 1; + +extern char* yytext; +#ifdef yytext_ptr +# undef yytext_ptr +#endif +#define yytext_ptr yytext + +static yy_state_type +yy_get_previous_state(void); +static yy_state_type +yy_try_NUL_trans(yy_state_type current_state); +static int +yy_get_next_buffer(void); +static void yynoreturn +yy_fatal_error(const char* msg); + +/* Done after the current pattern has been matched and before the + * corresponding action - sets up yytext. + */ +#define YY_DO_BEFORE_ACTION \ + (yytext_ptr) = yy_bp; \ + yyleng = (int) (yy_cp - yy_bp); \ + (yy_hold_char) = *yy_cp; \ + *yy_cp = '\0'; \ + (yy_c_buf_p) = yy_cp; +#define YY_NUM_RULES 22 +#define YY_END_OF_BUFFER 23 +/* This struct is not used in this scanner, + but its presence is necessary. */ +struct yy_trans_info +{ + flex_int32_t yy_verify; + flex_int32_t yy_nxt; +}; +static const flex_int16_t yy_accept[48] = { + 0, 0, 0, 23, 21, 20, 18, 6, 7, 3, 1, 9, 2, 21, 4, 14, 10, 8, 17, 11, 12, 17, 17, 5, + 14, 19, 13, 14, 0, 17, 17, 17, 19, 13, 0, 0, 14, 17, 17, 0, 13, 17, 17, 17, 17, 15, 16, 0}; + +static const YY_CHAR yy_ec[256] = { + 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 4, 5, + 6, 7, 8, 9, 10, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 1, 1, 14, 1, + 1, 1, 15, 15, 15, 15, 16, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 17, 1, 18, 1, 15, 1, 15, 15, 19, 20, + + 21, 15, 15, 15, 15, 15, 15, 22, 15, 15, 15, 15, 15, 23, 24, 25, 26, 15, 15, 15, 15, + 15, 1, 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + +static const YY_CHAR yy_meta[28] = {0, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, + 1, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 1}; + +static const flex_int16_t yy_base[50] = {0, 0, 0, 70, 71, 71, 71, 71, 71, 71, 71, 71, 71, + 57, 57, 18, 71, 71, 0, 71, 71, 46, 45, 71, 17, 0, + 19, 31, 37, 0, 45, 42, 0, 38, 44, 51, 49, 32, 36, + 43, 36, 26, 23, 16, 11, 0, 0, 71, 29, 59}; + +static const flex_int16_t yy_def[50] = {0, 47, 1, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, + 47, 47, 47, 47, 47, 48, 47, 47, 48, 48, 47, 47, 49, + 47, 47, 47, 48, 48, 48, 49, 47, 47, 47, 47, 48, 48, + 47, 47, 48, 48, 48, 48, 48, 48, 0, 47, 47}; + +static const flex_int16_t yy_nxt[99] = { + 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 18, 19, 20, 18, + 18, 18, 18, 21, 22, 18, 18, 23, 26, 24, 27, 33, 29, 28, 28, 34, 46, 45, 28, 28, + 34, 26, 44, 27, 35, 43, 35, 28, 40, 36, 33, 39, 28, 39, 34, 40, 40, 42, 41, 34, + 32, 36, 32, 36, 38, 37, 31, 30, 25, 24, 47, 3, 47, 47, 47, 47, 47, 47, 47, 47, + 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47 + +}; + +static const flex_int16_t yy_chk[99] = { + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 15, 24, 15, 26, 48, 24, 15, 26, 44, 43, 24, 15, + 26, 27, 42, 27, 28, 41, 28, 27, 40, 28, 33, 34, 27, 34, 33, 39, 34, 38, 37, 33, + 49, 36, 49, 35, 31, 30, 22, 21, 14, 13, 3, 47, 47, 47, 47, 47, 47, 47, 47, 47, + 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47 + +}; + +/* Table of booleans, true if rule could match eol. */ +static const flex_int32_t yy_rule_can_match_eol[23] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, +}; + +static yy_state_type yy_last_accepting_state; +static char* yy_last_accepting_cpos; + +extern int yy_flex_debug; +int yy_flex_debug = 0; + +/* The intent behind this definition is that it'll catch + * any uses of REJECT which flex missed. + */ +#define REJECT reject_used_but_not_detected +#define yymore() yymore_used_but_not_detected +#define YY_MORE_ADJ 0 +#define YY_RESTORE_YY_MORE_OFFSET +char* yytext; +#line 1 "scanner.l" +#line 4 "scanner.l" +#include + +#include "parser.h" +#include "raw_ast.hpp" +using namespace std; +#define YYDEBUG 1 +#line 511 "scanner.cpp" +/* float exponent */ +#line 513 "scanner.cpp" + +#define INITIAL 0 + +#ifndef YY_NO_UNISTD_H +/* Special case for "unistd.h", since it is non-ANSI. We include it way + * down here because we want the user's section 1 to have been scanned first. + * The user has a chance to override it with an option. + */ +# include +#endif + +#ifndef YY_EXTRA_TYPE +# define YY_EXTRA_TYPE void* +#endif + +static int +yy_init_globals(void); + +/* Accessor methods to globals. + These are made visible to non-reentrant scanners for convenience. */ + +int +yylex_destroy(void); + +int +yyget_debug(void); + +void +yyset_debug(int debug_flag); + +YY_EXTRA_TYPE +yyget_extra(void); + +void +yyset_extra(YY_EXTRA_TYPE user_defined); + +FILE* +yyget_in(void); + +void +yyset_in(FILE* _in_str); + +FILE* +yyget_out(void); + +void +yyset_out(FILE* _out_str); + +int +yyget_leng(void); + +char* +yyget_text(void); + +int +yyget_lineno(void); + +void +yyset_lineno(int _line_number); + +/* Macros after this point can all be overridden by user definitions in + * section 1. + */ + +#ifndef YY_SKIP_YYWRAP +# ifdef __cplusplus +extern "C" int +yywrap(void); +# else +extern int +yywrap(void); +# endif +#endif + +#ifndef YY_NO_UNPUT + +#endif + +#ifndef yytext_ptr +static void +yy_flex_strncpy(char*, const char*, int); +#endif + +#ifdef YY_NEED_STRLEN +static int +yy_flex_strlen(const char*); +#endif + +#ifndef YY_NO_INPUT +# ifdef __cplusplus +static int +yyinput(void); +# else +static int +input(void); +# endif + +#endif + +/* Amount of stuff to slurp up with each read. */ +#ifndef YY_READ_BUF_SIZE +# ifdef __ia64__ +/* On IA-64, the buffer size is 16k, not 8k */ +# define YY_READ_BUF_SIZE 16384 +# else +# define YY_READ_BUF_SIZE 8192 +# endif /* __ia64__ */ +#endif + +/* Copy whatever the last rule matched to the standard output. */ +#ifndef ECHO +/* This used to be an fputs(), but since the string might contain NUL's, + * we now use fwrite(). + */ +# define ECHO \ + do \ + { \ + if(fwrite(yytext, (size_t) yyleng, 1, yyout)) \ + {} \ + } while(0) +#endif + +/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL, + * is returned in "result". + */ +#ifndef YY_INPUT +# define YY_INPUT(buf, result, max_size) \ + if(YY_CURRENT_BUFFER_LVALUE->yy_is_interactive) \ + { \ + int c = '*'; \ + int n; \ + for(n = 0; n < max_size && (c = getc(yyin)) != EOF && c != '\n'; ++n) \ + buf[n] = (char) c; \ + if(c == '\n') buf[n++] = (char) c; \ + if(c == EOF && ferror(yyin)) YY_FATAL_ERROR("input in flex scanner failed"); \ + result = n; \ + } \ + else \ + { \ + errno = 0; \ + while((result = (int) fread(buf, 1, (yy_size_t) max_size, yyin)) == 0 && ferror(yyin)) \ + { \ + if(errno != EINTR) \ + { \ + YY_FATAL_ERROR("input in flex scanner failed"); \ + break; \ + } \ + errno = 0; \ + clearerr(yyin); \ + } \ + } + +#endif + +/* No semi-colon after return; correct usage is to write "yyterminate();" - + * we don't want an extra ';' after the "return" because that will cause + * some compilers to complain about unreachable statements. + */ +#ifndef yyterminate +# define yyterminate() return YY_NULL +#endif + +/* Number of entries by which start-condition stack grows. */ +#ifndef YY_START_STACK_INCR +# define YY_START_STACK_INCR 25 +#endif + +/* Report a fatal error. */ +#ifndef YY_FATAL_ERROR +# define YY_FATAL_ERROR(msg) yy_fatal_error(msg) +#endif + +/* end tables serialization structures and prototypes */ + +/* Default declaration of generated scanner - a define so the user can + * easily add parameters. + */ +#ifndef YY_DECL +# define YY_DECL_IS_OURS 1 + +extern int +yylex(void); + +# define YY_DECL int yylex(void) +#endif /* !YY_DECL */ + +/* Code executed at the beginning of each rule, after yytext and yyleng + * have been set up. + */ +#ifndef YY_USER_ACTION +# define YY_USER_ACTION +#endif + +/* Code executed at the end of each rule. */ +#ifndef YY_BREAK +# define YY_BREAK /*LINTED*/ break; +#endif + +#define YY_RULE_SETUP YY_USER_ACTION + +/** The main scanner function which does all the work. + */ +YY_DECL +{ + yy_state_type yy_current_state; + char * yy_cp, *yy_bp; + int yy_act; + + if(!(yy_init)) + { + (yy_init) = 1; + +#ifdef YY_USER_INIT + YY_USER_INIT; +#endif + + if(!(yy_start)) (yy_start) = 1; /* first start state */ + + if(!yyin) yyin = stdin; + + if(!yyout) yyout = stdout; + + if(!YY_CURRENT_BUFFER) + { + yyensure_buffer_stack(); + YY_CURRENT_BUFFER_LVALUE = yy_create_buffer(yyin, YY_BUF_SIZE); + } + + yy_load_buffer_state(); + } + + { +#line 15 "scanner.l" + +#line 730 "scanner.cpp" + + while(/*CONSTCOND*/ 1) /* loops until end-of-file is reached */ + { + yy_cp = (yy_c_buf_p); + + /* Support of yytext. */ + *yy_cp = (yy_hold_char); + + /* yy_bp points to the position in yy_ch_buf of the start of + * the current run. + */ + yy_bp = yy_cp; + + yy_current_state = (yy_start); + yy_match: + do + { + YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)]; + if(yy_accept[yy_current_state]) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while(yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state) + { + yy_current_state = (int) yy_def[yy_current_state]; + if(yy_current_state >= 48) yy_c = yy_meta[yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; + ++yy_cp; + } while(yy_base[yy_current_state] != 71); + + yy_find_action: + yy_act = yy_accept[yy_current_state]; + if(yy_act == 0) + { /* have to back up */ + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + yy_act = yy_accept[yy_current_state]; + } + + YY_DO_BEFORE_ACTION; + + if(yy_act != YY_END_OF_BUFFER && yy_rule_can_match_eol[yy_act]) + { + int yyl; + for(yyl = 0; yyl < yyleng; ++yyl) + if(yytext[yyl] == '\n') yylineno++; + ; + } + + do_action: /* This label is used only to access EOF actions. */ + + switch(yy_act) + { /* beginning of action switch */ + case 0: /* must back up */ + /* undo the effects of YY_DO_BEFORE_ACTION */ + *yy_cp = (yy_hold_char); + yy_cp = (yy_last_accepting_cpos); + yy_current_state = (yy_last_accepting_state); + goto yy_find_action; + + case 1: YY_RULE_SETUP +#line 16 "scanner.l" + { + return ADD; + } + YY_BREAK + case 2: YY_RULE_SETUP +#line 17 "scanner.l" + { + return SUB; + } + YY_BREAK + case 3: YY_RULE_SETUP +#line 18 "scanner.l" + { + return MUL; + } + YY_BREAK + case 4: YY_RULE_SETUP +#line 19 "scanner.l" + { + return DIV; + } + YY_BREAK + case 5: YY_RULE_SETUP +#line 20 "scanner.l" + { + return ABS; + } + YY_BREAK + case 6: YY_RULE_SETUP +#line 21 "scanner.l" + { + return OP; + } + YY_BREAK + case 7: YY_RULE_SETUP +#line 22 "scanner.l" + { + return CP; + } + YY_BREAK + case 8: YY_RULE_SETUP +#line 23 "scanner.l" + { + return EQUALS; + } + YY_BREAK + case 9: YY_RULE_SETUP +#line 24 "scanner.l" + { + return CM; + } + YY_BREAK + case 10: YY_RULE_SETUP +#line 25 "scanner.l" + { + return COLON; + } + YY_BREAK + case 11: YY_RULE_SETUP +#line 26 "scanner.l" + { + return O_SQ; + } + YY_BREAK + case 12: YY_RULE_SETUP +#line 27 "scanner.l" + { + return C_SQ; + } + YY_BREAK + case 13: +#line 30 "scanner.l" + case 14: YY_RULE_SETUP +#line 30 "scanner.l" + { + yylval.d = atoi(yytext); + return NUMBER; + } + YY_BREAK + case 15: YY_RULE_SETUP +#line 34 "scanner.l" + { + return REDUCE; + } + YY_BREAK + case 16: YY_RULE_SETUP +#line 35 "scanner.l" + { + return SELECT; + } + YY_BREAK + case 17: YY_RULE_SETUP +#line 37 "scanner.l" + { + yylval.s = strdup(yytext); + return NAME; + } + YY_BREAK + case 18: + /* rule 18 can match eol */ + YY_RULE_SETUP +#line 42 "scanner.l" + { + return EOL; + } + YY_BREAK + case 19: YY_RULE_SETUP +#line 43 "scanner.l" + + YY_BREAK + case 20: YY_RULE_SETUP +#line 44 "scanner.l" + { /* ignore white space */ + } + YY_BREAK + case 21: YY_RULE_SETUP +#line 45 "scanner.l" + { + throw std::runtime_error(fmt::format("Mystery character {}", *yytext)); + } + YY_BREAK + case 22: YY_RULE_SETUP +#line 46 "scanner.l" + YY_FATAL_ERROR("flex scanner jammed"); + YY_BREAK +#line 909 "scanner.cpp" + case YY_STATE_EOF(INITIAL): yyterminate(); + + case YY_END_OF_BUFFER: + { + /* Amount of text matched not including the EOB char. */ + int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1; + + /* Undo the effects of YY_DO_BEFORE_ACTION. */ + *yy_cp = (yy_hold_char); + YY_RESTORE_YY_MORE_OFFSET + + if(YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW) + { + /* We're scanning a new file or input source. It's + * possible that this happened because the user + * just pointed yyin at a new source and called + * yylex(). If so, then we have to assure + * consistency between YY_CURRENT_BUFFER and our + * globals. Here is the right place to do so, because + * this is the first action (other than possibly a + * back-up) that will match for the new input source. + */ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL; + } + + /* Note that here we test for yy_c_buf_p "<=" to the position + * of the first EOB in the buffer, since yy_c_buf_p will + * already have been incremented past the NUL character + * (since all states make transitions on EOB to the + * end-of-buffer state). Contrast this with the test + * in input(). + */ + if((yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]) + { /* This was really a NUL. */ + yy_state_type yy_next_state; + + (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state(); + + /* Okay, we're now positioned to make the NUL + * transition. We couldn't have + * yy_get_previous_state() go ahead and do it + * for us because it doesn't know how to deal + * with the possibility of jamming (and we don't + * want to build jamming into it because then it + * will run more slowly). + */ + + yy_next_state = yy_try_NUL_trans(yy_current_state); + + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + + if(yy_next_state) + { + /* Consume the NUL. */ + yy_cp = ++(yy_c_buf_p); + yy_current_state = yy_next_state; + goto yy_match; + } + + else + { + yy_cp = (yy_c_buf_p); + goto yy_find_action; + } + } + + else + switch(yy_get_next_buffer()) + { + case EOB_ACT_END_OF_FILE: + { + (yy_did_buffer_switch_on_eof) = 0; + + if(yywrap()) + { + /* Note: because we've taken care in + * yy_get_next_buffer() to have set up + * yytext, we can now set up + * yy_c_buf_p so that if some total + * hoser (like flex itself) wants to + * call the scanner after we return the + * YY_NULL, it'll still work - another + * YY_NULL will get returned. + */ + (yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ; + + yy_act = YY_STATE_EOF(YY_START); + goto do_action; + } + + else + { + if(!(yy_did_buffer_switch_on_eof)) YY_NEW_FILE; + } + break; + } + + case EOB_ACT_CONTINUE_SCAN: + (yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text; + + yy_current_state = yy_get_previous_state(); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_match; + + case EOB_ACT_LAST_MATCH: + (yy_c_buf_p) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]; + + yy_current_state = yy_get_previous_state(); + + yy_cp = (yy_c_buf_p); + yy_bp = (yytext_ptr) + YY_MORE_ADJ; + goto yy_find_action; + } + break; + } + + default: YY_FATAL_ERROR("fatal flex scanner internal error--no action found"); + } /* end of action switch */ + } /* end of scanning one token */ + } /* end of user's declarations */ +} /* end of yylex */ + +/* yy_get_next_buffer - try to read in a new buffer + * + * Returns a code representing an action: + * EOB_ACT_LAST_MATCH - + * EOB_ACT_CONTINUE_SCAN - continue scanning from current position + * EOB_ACT_END_OF_FILE - end of file + */ +static int +yy_get_next_buffer(void) +{ + char* dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf; + char* source = (yytext_ptr); + int number_to_move, i; + int ret_val; + + if((yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1]) + YY_FATAL_ERROR("fatal flex scanner internal error--end of buffer missed"); + + if(YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0) + { /* Don't try to fill the buffer, so this is an EOF. */ + if((yy_c_buf_p) - (yytext_ptr) -YY_MORE_ADJ == 1) + { + /* We matched a single character, the EOB, so + * treat this as a final EOF. + */ + return EOB_ACT_END_OF_FILE; + } + + else + { + /* We matched some text prior to the EOB, first + * process it. + */ + return EOB_ACT_LAST_MATCH; + } + } + + /* Try to read more data. */ + + /* First move last chars to start of buffer. */ + number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr) -1); + + for(i = 0; i < number_to_move; ++i) + *(dest++) = *(source++); + + if(YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING) + /* don't do the read, it's not guaranteed to return an EOF, + * just force an EOF + */ + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0; + + else + { + int num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; + + while(num_to_read <= 0) + { /* Not enough room in the buffer - grow it. */ + + /* just a shorter name for the current buffer */ + YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE; + + int yy_c_buf_p_offset = (int) ((yy_c_buf_p) -b->yy_ch_buf); + + if(b->yy_is_our_buffer) + { + int new_size = b->yy_buf_size * 2; + + if(new_size <= 0) + b->yy_buf_size += b->yy_buf_size / 8; + else + b->yy_buf_size *= 2; + + b->yy_ch_buf = (char*) + /* Include room in for 2 EOB chars. */ + yyrealloc((void*) b->yy_ch_buf, (yy_size_t)(b->yy_buf_size + 2)); + } + else + /* Can't grow it, we don't own it. */ + b->yy_ch_buf = NULL; + + if(!b->yy_ch_buf) YY_FATAL_ERROR("fatal error - scanner input buffer overflow"); + + (yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset]; + + num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1; + } + + if(num_to_read > YY_READ_BUF_SIZE) num_to_read = YY_READ_BUF_SIZE; + + /* Read in more data. */ + YY_INPUT((&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), (yy_n_chars), num_to_read); + + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + if((yy_n_chars) == 0) + { + if(number_to_move == YY_MORE_ADJ) + { + ret_val = EOB_ACT_END_OF_FILE; + yyrestart(yyin); + } + + else + { + ret_val = EOB_ACT_LAST_MATCH; + YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_EOF_PENDING; + } + } + + else + ret_val = EOB_ACT_CONTINUE_SCAN; + + if(((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) + { + /* Extend the array by 50%, plus the number we really need. */ + int new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1); + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = + (char*) yyrealloc((void*) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf, (yy_size_t) new_size); + if(!YY_CURRENT_BUFFER_LVALUE->yy_ch_buf) + YY_FATAL_ERROR("out of dynamic memory in yy_get_next_buffer()"); + /* "- 2" to take care of EOB's */ + YY_CURRENT_BUFFER_LVALUE->yy_buf_size = (int) (new_size - 2); + } + + (yy_n_chars) += number_to_move; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR; + YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR; + + (yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0]; + + return ret_val; +} + +/* yy_get_previous_state - get the state just before the EOB char was reached */ + +static yy_state_type +yy_get_previous_state(void) +{ + yy_state_type yy_current_state; + char* yy_cp; + + yy_current_state = (yy_start); + + for(yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp) + { + YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1); + if(yy_accept[yy_current_state]) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while(yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state) + { + yy_current_state = (int) yy_def[yy_current_state]; + if(yy_current_state >= 48) yy_c = yy_meta[yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; + } + + return yy_current_state; +} + +/* yy_try_NUL_trans - try to make a transition on the NUL character + * + * synopsis + * next_state = yy_try_NUL_trans( current_state ); + */ +static yy_state_type +yy_try_NUL_trans(yy_state_type yy_current_state) +{ + int yy_is_jam; + char* yy_cp = (yy_c_buf_p); + + YY_CHAR yy_c = 1; + if(yy_accept[yy_current_state]) + { + (yy_last_accepting_state) = yy_current_state; + (yy_last_accepting_cpos) = yy_cp; + } + while(yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state) + { + yy_current_state = (int) yy_def[yy_current_state]; + if(yy_current_state >= 48) yy_c = yy_meta[yy_c]; + } + yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c]; + yy_is_jam = (yy_current_state == 47); + + return yy_is_jam ? 0 : yy_current_state; +} + +#ifndef YY_NO_UNPUT + +#endif + +#ifndef YY_NO_INPUT +# ifdef __cplusplus +static int +yyinput(void) +# else +static int +input(void) +# endif + +{ + int c; + + *(yy_c_buf_p) = (yy_hold_char); + + if(*(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR) + { + /* yy_c_buf_p now points to the character we want to return. + * If this occurs *before* the EOB characters, then it's a + * valid NUL; if not, then we've hit the end of the buffer. + */ + if((yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)]) + /* This was really a NUL. */ + *(yy_c_buf_p) = '\0'; + + else + { /* need more input */ + int offset = (int) ((yy_c_buf_p) - (yytext_ptr)); + ++(yy_c_buf_p); + + switch(yy_get_next_buffer()) + { + case EOB_ACT_LAST_MATCH: + /* This happens because yy_g_n_b() + * sees that we've accumulated a + * token and flags that we need to + * try matching the token before + * proceeding. But for input(), + * there's no matching to consider. + * So convert the EOB_ACT_LAST_MATCH + * to EOB_ACT_END_OF_FILE. + */ + + /* Reset buffer status. */ + yyrestart(yyin); + + /*FALLTHROUGH*/ + + case EOB_ACT_END_OF_FILE: + { + if(yywrap()) return 0; + + if(!(yy_did_buffer_switch_on_eof)) YY_NEW_FILE; +# ifdef __cplusplus + return yyinput(); +# else + return input(); +# endif + } + + case EOB_ACT_CONTINUE_SCAN: (yy_c_buf_p) = (yytext_ptr) + offset; break; + } + } + } + + c = *(unsigned char*) (yy_c_buf_p); /* cast for 8-bit char's */ + *(yy_c_buf_p) = '\0'; /* preserve yytext */ + (yy_hold_char) = *++(yy_c_buf_p); + + if(c == '\n') yylineno++; + ; + + return c; +} +#endif /* ifndef YY_NO_INPUT */ + +/** Immediately switch to a different input stream. + * @param input_file A readable stream. + * + * @note This function does not reset the start condition to @c INITIAL . + */ +void +yyrestart(FILE* input_file) +{ + if(!YY_CURRENT_BUFFER) + { + yyensure_buffer_stack(); + YY_CURRENT_BUFFER_LVALUE = yy_create_buffer(yyin, YY_BUF_SIZE); + } + + yy_init_buffer(YY_CURRENT_BUFFER, input_file); + yy_load_buffer_state(); +} + +/** Switch to a different input buffer. + * @param new_buffer The new input buffer. + * + */ +void +yy_switch_to_buffer(YY_BUFFER_STATE new_buffer) +{ + /* TODO. We should be able to replace this entire function body + * with + * yypop_buffer_state(); + * yypush_buffer_state(new_buffer); + */ + yyensure_buffer_stack(); + if(YY_CURRENT_BUFFER == new_buffer) return; + + if(YY_CURRENT_BUFFER) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + YY_CURRENT_BUFFER_LVALUE = new_buffer; + yy_load_buffer_state(); + + /* We don't actually know whether we did this switch during + * EOF (yywrap()) processing, but the only time this flag + * is looked at is after yywrap() is called, so it's safe + * to go ahead and always set it. + */ + (yy_did_buffer_switch_on_eof) = 1; +} + +static void +yy_load_buffer_state(void) +{ + (yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars; + (yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos; + yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file; + (yy_hold_char) = *(yy_c_buf_p); +} + +/** Allocate and initialize an input buffer state. + * @param file A readable stream. + * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE. + * + * @return the allocated buffer state. + */ +YY_BUFFER_STATE +yy_create_buffer(FILE* file, int size) +{ + YY_BUFFER_STATE b; + + b = (YY_BUFFER_STATE) yyalloc(sizeof(struct yy_buffer_state)); + if(!b) YY_FATAL_ERROR("out of dynamic memory in yy_create_buffer()"); + + b->yy_buf_size = size; + + /* yy_ch_buf has to be 2 characters longer than the size given because + * we need to put in 2 end-of-buffer characters. + */ + b->yy_ch_buf = (char*) yyalloc((yy_size_t)(b->yy_buf_size + 2)); + if(!b->yy_ch_buf) YY_FATAL_ERROR("out of dynamic memory in yy_create_buffer()"); + + b->yy_is_our_buffer = 1; + + yy_init_buffer(b, file); + + return b; +} + +/** Destroy the buffer. + * @param b a buffer created with yy_create_buffer() + * + */ +void +yy_delete_buffer(YY_BUFFER_STATE b) +{ + if(!b) return; + + if(b == YY_CURRENT_BUFFER) /* Not sure if we should pop here. */ + YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0; + + if(b->yy_is_our_buffer) yyfree((void*) b->yy_ch_buf); + + yyfree((void*) b); +} + +/* Initializes or reinitializes a buffer. + * This function is sometimes called more than once on the same buffer, + * such as during a yyrestart() or at EOF. + */ +static void +yy_init_buffer(YY_BUFFER_STATE b, FILE* file) + +{ + int oerrno = errno; + + yy_flush_buffer(b); + + b->yy_input_file = file; + b->yy_fill_buffer = 1; + + /* If b is the current buffer, then yy_init_buffer was _probably_ + * called from yyrestart() or through yy_get_next_buffer. + * In that case, we don't want to reset the lineno or column. + */ + if(b != YY_CURRENT_BUFFER) + { + b->yy_bs_lineno = 1; + b->yy_bs_column = 0; + } + + b->yy_is_interactive = file ? (isatty(fileno(file)) > 0) : 0; + + errno = oerrno; +} + +/** Discard all buffered characters. On the next scan, YY_INPUT will be called. + * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER. + * + */ +void +yy_flush_buffer(YY_BUFFER_STATE b) +{ + if(!b) return; + + b->yy_n_chars = 0; + + /* We always need two end-of-buffer characters. The first causes + * a transition to the end-of-buffer state. The second causes + * a jam in that state. + */ + b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR; + b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR; + + b->yy_buf_pos = &b->yy_ch_buf[0]; + + b->yy_at_bol = 1; + b->yy_buffer_status = YY_BUFFER_NEW; + + if(b == YY_CURRENT_BUFFER) yy_load_buffer_state(); +} + +/** Pushes the new state onto the stack. The new state becomes + * the current state. This function will allocate the stack + * if necessary. + * @param new_buffer The new state. + * + */ +void +yypush_buffer_state(YY_BUFFER_STATE new_buffer) +{ + if(new_buffer == NULL) return; + + yyensure_buffer_stack(); + + /* This block is copied from yy_switch_to_buffer. */ + if(YY_CURRENT_BUFFER) + { + /* Flush out information for old buffer. */ + *(yy_c_buf_p) = (yy_hold_char); + YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p); + YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars); + } + + /* Only push if top exists. Otherwise, replace top. */ + if(YY_CURRENT_BUFFER) (yy_buffer_stack_top)++; + YY_CURRENT_BUFFER_LVALUE = new_buffer; + + /* copied from yy_switch_to_buffer. */ + yy_load_buffer_state(); + (yy_did_buffer_switch_on_eof) = 1; +} + +/** Removes and deletes the top of the stack, if present. + * The next element becomes the new top. + * + */ +void +yypop_buffer_state(void) +{ + if(!YY_CURRENT_BUFFER) return; + + yy_delete_buffer(YY_CURRENT_BUFFER); + YY_CURRENT_BUFFER_LVALUE = NULL; + if((yy_buffer_stack_top) > 0) --(yy_buffer_stack_top); + + if(YY_CURRENT_BUFFER) + { + yy_load_buffer_state(); + (yy_did_buffer_switch_on_eof) = 1; + } +} + +/* Allocates the stack if it does not exist. + * Guarantees space for at least one push. + */ +static void +yyensure_buffer_stack(void) +{ + yy_size_t num_to_alloc; + + if(!(yy_buffer_stack)) + { + /* First allocation is just for 2 elements, since we don't know if this + * scanner will even need a stack. We use 2 instead of 1 to avoid an + * immediate realloc on the next call. + */ + num_to_alloc = 1; /* After all that talk, this was set to 1 anyways... */ + (yy_buffer_stack) = + (struct yy_buffer_state**) yyalloc(num_to_alloc * sizeof(struct yy_buffer_state*)); + if(!(yy_buffer_stack)) YY_FATAL_ERROR("out of dynamic memory in yyensure_buffer_stack()"); + + memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*)); + + (yy_buffer_stack_max) = num_to_alloc; + (yy_buffer_stack_top) = 0; + return; + } + + if((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1) + { + /* Increase the buffer to prepare for a possible push. */ + yy_size_t grow_size = 8 /* arbitrary grow size */; + + num_to_alloc = (yy_buffer_stack_max) + grow_size; + (yy_buffer_stack) = (struct yy_buffer_state**) yyrealloc( + (yy_buffer_stack), num_to_alloc * sizeof(struct yy_buffer_state*)); + if(!(yy_buffer_stack)) YY_FATAL_ERROR("out of dynamic memory in yyensure_buffer_stack()"); + + /* zero only the new slots.*/ + memset((yy_buffer_stack) + (yy_buffer_stack_max), + 0, + grow_size * sizeof(struct yy_buffer_state*)); + (yy_buffer_stack_max) = num_to_alloc; + } +} + +/** Setup the input buffer state to scan directly from a user-specified character buffer. + * @param base the character buffer + * @param size the size in bytes of the character buffer + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE +yy_scan_buffer(char* base, yy_size_t size) +{ + YY_BUFFER_STATE b; + + if(size < 2 || base[size - 2] != YY_END_OF_BUFFER_CHAR || + base[size - 1] != YY_END_OF_BUFFER_CHAR) + /* They forgot to leave room for the EOB's. */ + return NULL; + + b = (YY_BUFFER_STATE) yyalloc(sizeof(struct yy_buffer_state)); + if(!b) YY_FATAL_ERROR("out of dynamic memory in yy_scan_buffer()"); + + b->yy_buf_size = (int) (size - 2); /* "- 2" to take care of EOB's */ + b->yy_buf_pos = b->yy_ch_buf = base; + b->yy_is_our_buffer = 0; + b->yy_input_file = NULL; + b->yy_n_chars = b->yy_buf_size; + b->yy_is_interactive = 0; + b->yy_at_bol = 1; + b->yy_fill_buffer = 0; + b->yy_buffer_status = YY_BUFFER_NEW; + + yy_switch_to_buffer(b); + + return b; +} + +/** Setup the input buffer state to scan a string. The next call to yylex() will + * scan from a @e copy of @a str. + * @param yystr a NUL-terminated string to scan + * + * @return the newly allocated buffer state object. + * @note If you want to scan bytes that may contain NUL values, then use + * yy_scan_bytes() instead. + */ +YY_BUFFER_STATE +yy_scan_string(const char* yystr) { return yy_scan_bytes(yystr, (int) strlen(yystr)); } + +/** Setup the input buffer state to scan the given bytes. The next call to yylex() will + * scan from a @e copy of @a bytes. + * @param yybytes the byte buffer to scan + * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes. + * + * @return the newly allocated buffer state object. + */ +YY_BUFFER_STATE +yy_scan_bytes(const char* yybytes, int _yybytes_len) +{ + YY_BUFFER_STATE b; + char* buf; + yy_size_t n; + int i; + + /* Get memory for full buffer, including space for trailing EOB's. */ + n = (yy_size_t)(_yybytes_len + 2); + buf = (char*) yyalloc(n); + if(!buf) YY_FATAL_ERROR("out of dynamic memory in yy_scan_bytes()"); + + for(i = 0; i < _yybytes_len; ++i) + buf[i] = yybytes[i]; + + buf[_yybytes_len] = buf[_yybytes_len + 1] = YY_END_OF_BUFFER_CHAR; + + b = yy_scan_buffer(buf, n); + if(!b) YY_FATAL_ERROR("bad buffer in yy_scan_bytes()"); + + /* It's okay to grow etc. this buffer, and we should throw it + * away when we're done. + */ + b->yy_is_our_buffer = 1; + + return b; +} + +#ifndef YY_EXIT_FAILURE +# define YY_EXIT_FAILURE 2 +#endif + +static void yynoreturn +yy_fatal_error(const char* msg) +{ + fprintf(stderr, "%s\n", msg); + exit(YY_EXIT_FAILURE); +} + +/* Redefine yyless() so it works in section 3 code. */ + +#undef yyless +#define yyless(n) \ + do \ + { \ + /* Undo effects of setting up yytext. */ \ + int yyless_macro_arg = (n); \ + YY_LESS_LINENO(yyless_macro_arg); \ + yytext[yyleng] = (yy_hold_char); \ + (yy_c_buf_p) = yytext + yyless_macro_arg; \ + (yy_hold_char) = *(yy_c_buf_p); \ + *(yy_c_buf_p) = '\0'; \ + yyleng = yyless_macro_arg; \ + } while(0) + +/* Accessor methods (get/set functions) to struct members. */ + +/** Get the current line number. + * + */ +int +yyget_lineno(void) +{ + return yylineno; +} + +/** Get the input stream. + * + */ +FILE* +yyget_in(void) +{ + return yyin; +} + +/** Get the output stream. + * + */ +FILE* +yyget_out(void) +{ + return yyout; +} + +/** Get the length of the current token. + * + */ +int +yyget_leng(void) +{ + return yyleng; +} + +/** Get the current token. + * + */ + +char* +yyget_text(void) +{ + return yytext; +} + +/** Set the current line number. + * @param _line_number line number + * + */ +void +yyset_lineno(int _line_number) +{ + yylineno = _line_number; +} + +/** Set the input stream. This does not discard the current + * input buffer. + * @param _in_str A readable stream. + * + * @see yy_switch_to_buffer + */ +void +yyset_in(FILE* _in_str) +{ + yyin = _in_str; +} + +void +yyset_out(FILE* _out_str) +{ + yyout = _out_str; +} + +int +yyget_debug(void) +{ + return yy_flex_debug; +} + +void +yyset_debug(int _bdebug) +{ + yy_flex_debug = _bdebug; +} + +static int +yy_init_globals(void) +{ + /* Initialization is the same as for the non-reentrant scanner. + * This function is called from yylex_destroy(), so don't allocate here. + */ + + /* We do not touch yylineno unless the option is enabled. */ + yylineno = 1; + + (yy_buffer_stack) = NULL; + (yy_buffer_stack_top) = 0; + (yy_buffer_stack_max) = 0; + (yy_c_buf_p) = NULL; + (yy_init) = 0; + (yy_start) = 0; + +/* Defined in main.c */ +#ifdef YY_STDINIT + yyin = stdin; + yyout = stdout; +#else + yyin = NULL; + yyout = NULL; +#endif + + /* For future reference: Set errno on error, since we are called by + * yylex_init() + */ + return 0; +} + +/* yylex_destroy is for both reentrant and non-reentrant scanners. */ +int +yylex_destroy(void) +{ + /* Pop the buffer stack, destroying each element. */ + while(YY_CURRENT_BUFFER) + { + yy_delete_buffer(YY_CURRENT_BUFFER); + YY_CURRENT_BUFFER_LVALUE = NULL; + yypop_buffer_state(); + } + + /* Destroy the stack itself. */ + yyfree((yy_buffer_stack)); + (yy_buffer_stack) = NULL; + + /* Reset the globals. This is important in a non-reentrant scanner so the next time + * yylex() is called, initialization will occur. */ + yy_init_globals(); + + return 0; +} + +/* + * Internal utility routines. + */ + +#ifndef yytext_ptr +static void +yy_flex_strncpy(char* s1, const char* s2, int n) +{ + int i; + for(i = 0; i < n; ++i) + s1[i] = s2[i]; +} +#endif + +#ifdef YY_NEED_STRLEN +static int +yy_flex_strlen(const char* s) +{ + int n; + for(n = 0; s[n]; ++n) + ; + + return n; +} +#endif + +void* +yyalloc(yy_size_t size) +{ + return malloc(size); +} + +void* +yyrealloc(void* ptr, yy_size_t size) +{ + /* The cast to (char *) in the following accommodates both + * implementations that use char* generic pointers, and those + * that use void* generic pointers. It works with the latter + * because both ANSI C and C++ allow castless assignment from + * any pointer type to void*, and deal with argument conversions + * as though doing an assignment. + */ + return realloc(ptr, size); +} + +void +yyfree(void* ptr) +{ + free((char*) ptr); /* see yyrealloc() for (char *) cast */ +} + +#define YYTABLES_NAME "yytables" + +#line 46 "scanner.l" diff --git a/source/lib/rocprofiler/counters/parser/scanner.l b/source/lib/rocprofiler/counters/parser/scanner.l new file mode 100644 index 0000000000..96cff46720 --- /dev/null +++ b/source/lib/rocprofiler/counters/parser/scanner.l @@ -0,0 +1,47 @@ +%option noyywrap nodefault yylineno nounput + +%{ +#include + +#include "raw_ast.hpp" +#include "parser.h" +using namespace std; +#define YYDEBUG 1 +%} + +/* float exponent */ +EXP ([Ee][-+]?[0-9]+) + +%% +"+" { return ADD; } +"-" { return SUB; } +"*" { return MUL; } +"/" { return DIV; } +"|" { return ABS; } +"(" { return OP; } +")" { return CP; } +"=" { return EQUALS; } +"," { return CM; } +":" { return COLON; } +\[ { return O_SQ; } +\] { return C_SQ; } + +[0-9]+"."[0-9]*{EXP}? | +"."?[0-9]+{EXP}? { + yylval.d = atoi(yytext); + return NUMBER; } + +"reduce" { return REDUCE; } +"select" { return SELECT; } + +[a-z_A-Z][a-z_A-Z0-9]* { + yylval.s = strdup(yytext); + return NAME; } + + +\n { return EOL; } +"//".* +[ \t] { /* ignore white space */ } +. { throw std::runtime_error(fmt::format("Mystery character {}", *yytext)); } +%% + diff --git a/source/lib/rocprofiler/counters/parser/tests/CMakeLists.txt b/source/lib/rocprofiler/counters/parser/tests/CMakeLists.txt new file mode 100644 index 0000000000..82b423ae5f --- /dev/null +++ b/source/lib/rocprofiler/counters/parser/tests/CMakeLists.txt @@ -0,0 +1,25 @@ +# +# +# +rocprofiler_deactivate_clang_tidy() + +include(GoogleTest) + +set(ROCPROFILER_LIB_PARSER_TEST_SOURCES "parser_test.cpp") + +add_executable(parser-test) + +target_sources(parser-test PRIVATE ${ROCPROFILER_LIB_PARSER_TEST_SOURCES}) + +target_link_libraries( + parser-test + PRIVATE rocprofiler::rocprofiler-common-library + rocprofiler::rocprofiler-static-library GTest::gtest GTest::gtest_main) + +gtest_add_tests( + TARGET parser-test + SOURCES ${ROCPROFILER_LIB_PARSER_TEST_SOURCES} + TEST_LIST parser-tests_TESTS + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + +set_tests_properties(${parser-tests_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests") diff --git a/source/lib/rocprofiler/counters/parser/tests/parser_test.cpp b/source/lib/rocprofiler/counters/parser/tests/parser_test.cpp new file mode 100644 index 0000000000..d6eba18535 --- /dev/null +++ b/source/lib/rocprofiler/counters/parser/tests/parser_test.cpp @@ -0,0 +1,192 @@ +#include +#include + +#include + +#include "lib/rocprofiler/counters/metrics.hpp" +#include "lib/rocprofiler/counters/parser/reader.hpp" + +TEST(parser, base_ops) +{ + std::map expressionToExpected = { + {"AB + BA", + "{\"Type\":\"ADDITION_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"AB\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"BA\",\"ReferenceSet\":[], \"CounterSet\":[]}]}"}, + {"CD - ZX", + "{\"Type\":\"SUBTRACTION_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"CD\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"ZX\",\"ReferenceSet\":[], \"CounterSet\":[]}]}"}, + {"NM / DB", + "{\"Type\":\"DIVIDE_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"NM\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"DB\",\"ReferenceSet\":[], \"CounterSet\":[]}]}"}, + {"AB * BA", + "{\"Type\":\"MULTIPLY_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"AB\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"BA\",\"ReferenceSet\":[], \"CounterSet\":[]}]}"}}; + + for(auto [op, expected] : expressionToExpected) + { + RawAST* ast = nullptr; + auto* buf = yy_scan_string(op.c_str()); + yyparse(&ast); + ASSERT_TRUE(ast); + EXPECT_EQ(fmt::format("{}", *ast), expected); + yy_delete_buffer(buf); + delete ast; + } +} + +TEST(parser, order_of_ops) +{ + std::map expressionToExpected = { + {"(AB + BA) / CD", + "{\"Type\":\"DIVIDE_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"ADDITION_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"AB\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"BA\",\"ReferenceSet\":[], " + "\"CounterSet\":[]}]},{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"CD\",\"ReferenceSet\":[], \"CounterSet\":[]}]}"}, + {"AD / (CD - ZX)", + "{\"Type\":\"DIVIDE_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"AD\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"SUBTRACTION_NODE\", " + "\"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"CD\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"ZX\",\"ReferenceSet\":[], \"CounterSet\":[]}]}]}"}, + {"MN * (NM / DB)", + "{\"Type\":\"MULTIPLY_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"MN\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"DIVIDE_NODE\", " + "\"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"NM\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"DB\",\"ReferenceSet\":[], \"CounterSet\":[]}]}]}"}, + {"(AB / BA) - BN", + "{\"Type\":\"SUBTRACTION_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"DIVIDE_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"AB\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"BA\",\"ReferenceSet\":[], " + "\"CounterSet\":[]}]},{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"BN\",\"ReferenceSet\":[], \"CounterSet\":[]}]}"}}; + + for(auto [op, expected] : expressionToExpected) + { + RawAST* ast = nullptr; + auto* buf = yy_scan_string(op.c_str()); + yyparse(&ast); + ASSERT_TRUE(ast); + EXPECT_EQ(fmt::format("{}", *ast), expected); + yy_delete_buffer(buf); + delete ast; + } +} + +TEST(parser, reduction) +{ + std::map expressionToExpected = { + {"reduce(AB, SUM)", + "{\"Type\":\"REDUCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"SUM\",\"ReferenceSet\":[], \"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"AB\",\"ReferenceSet\":[], \"CounterSet\":[]}]}"}, + {"reduce(AB+CD, SUM)", + "{\"Type\":\"REDUCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"SUM\",\"ReferenceSet\":[], \"CounterSet\":[{\"Type\":\"ADDITION_NODE\", " + "\"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"AB\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"CD\",\"ReferenceSet\":[], \"CounterSet\":[]}]}]}"}, + {"reduce(AB,DIV)+reduce(DC,SUM)", + "{\"Type\":\"ADDITION_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REDUCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"DIV\",\"ReferenceSet\":[], \"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"AB\",\"ReferenceSet\":[], " + "\"CounterSet\":[]}]},{\"Type\":\"REDUCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"SUM\",\"ReferenceSet\":[], \"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"DC\",\"ReferenceSet\":[], \"CounterSet\":[]}]}]}"}, + {"reduce(AB, SUM, shader)", + "{\"Type\":\"REDUCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"SUM\",\"ReferenceSet\":[{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"shader\",\"ReferenceSet\":[], \"CounterSet\":[]}], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"AB\",\"ReferenceSet\":[], \"CounterSet\":[]}]}"}}; + + for(auto [op, expected] : expressionToExpected) + { + RawAST* ast = nullptr; + auto* buf = yy_scan_string(op.c_str()); + yyparse(&ast); + ASSERT_TRUE(ast); + EXPECT_EQ(fmt::format("{}", *ast), expected); + yy_delete_buffer(buf); + delete ast; + } +} + +TEST(parser, selection) +{ + std::map expressionToExpected = { + {"select(AB, SUM)", + "{\"Type\":\"SELECT_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"SUM\",\"ReferenceSet\":[], \"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"AB\",\"ReferenceSet\":[], \"CounterSet\":[]}]}"}, + {"select(AB+CD, SUM)", + "{\"Type\":\"SELECT_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"SUM\",\"ReferenceSet\":[], \"CounterSet\":[{\"Type\":\"ADDITION_NODE\", " + "\"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"AB\",\"ReferenceSet\":[], \"CounterSet\":[]},{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"CD\",\"ReferenceSet\":[], \"CounterSet\":[]}]}]}"}, + {"select(AB,DIV)+select(DC,SUM)", + "{\"Type\":\"ADDITION_NODE\", \"Operation\":\"NONE\",\"ReferenceSet\":[], " + "\"CounterSet\":[{\"Type\":\"SELECT_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"DIV\",\"ReferenceSet\":[], \"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"AB\",\"ReferenceSet\":[], " + "\"CounterSet\":[]}]},{\"Type\":\"SELECT_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"SUM\",\"ReferenceSet\":[], \"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"DC\",\"ReferenceSet\":[], \"CounterSet\":[]}]}]}"}, + {"select(AB, SUM, shader)", + "{\"Type\":\"SELECT_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"SUM\",\"ReferenceSet\":[{\"Type\":\"REFERENCE_NODE\", " + "\"Operation\":\"NONE\", \"Value\":\"shader\",\"ReferenceSet\":[], \"CounterSet\":[]}], " + "\"CounterSet\":[{\"Type\":\"REFERENCE_NODE\", \"Operation\":\"NONE\", " + "\"Value\":\"AB\",\"ReferenceSet\":[], \"CounterSet\":[]}]}"}}; + + for(auto [op, expected] : expressionToExpected) + { + RawAST* ast = nullptr; + auto* buf = yy_scan_string(op.c_str()); + yyparse(&ast); + ASSERT_TRUE(ast); + EXPECT_EQ(fmt::format("{}", *ast), expected); + yy_delete_buffer(buf); + delete ast; + } +} + +TEST(parser, parse_derived_counters) +{ + // Checks that ASTs are properly formed from derived counters defined in XML + // Does not check accuracy, only parseability + auto derived_counters = rocprofiler::counters::getDerivedHardwareMetrics(); + for(auto& [gfx, counter_list] : derived_counters) + { + for(const auto& v : counter_list) + { + RawAST* ast = nullptr; + auto* buf = yy_scan_string(v.expression().c_str()); + yyparse(&ast); + ASSERT_TRUE(ast); + yy_delete_buffer(buf); + delete ast; + } + } +} diff --git a/source/lib/rocprofiler/counters/tests/CMakeLists.txt b/source/lib/rocprofiler/counters/tests/CMakeLists.txt index 5536d48c4e..a8cf10d8cb 100644 --- a/source/lib/rocprofiler/counters/tests/CMakeLists.txt +++ b/source/lib/rocprofiler/counters/tests/CMakeLists.txt @@ -2,7 +2,7 @@ rocprofiler_deactivate_clang_tidy() include(GoogleTest) -set(ROCPROFILER_LIB_COUNTER_TEST_SOURCES "metrics_test.cpp") +set(ROCPROFILER_LIB_COUNTER_TEST_SOURCES metrics_test.cpp evaluate_ast_test.cpp) add_executable(counter-test) diff --git a/source/lib/rocprofiler/counters/tests/evaluate_ast_test.cpp b/source/lib/rocprofiler/counters/tests/evaluate_ast_test.cpp new file mode 100644 index 0000000000..c29ad4c5f8 --- /dev/null +++ b/source/lib/rocprofiler/counters/tests/evaluate_ast_test.cpp @@ -0,0 +1,190 @@ +#include + +#include + +#include "lib/rocprofiler/counters/evaluate_ast.hpp" +#include "lib/rocprofiler/counters/parser/reader.hpp" + +namespace +{ +bool +isIdentical(const EvaluateAST& eval_ast, const RawAST& raw_ast) +{ + if(raw_ast.counter_set.size() != eval_ast.children().size() || + raw_ast.type != eval_ast.type() || raw_ast.operation != eval_ast.op()) + { + return false; + } + + for(size_t i = 0; i < raw_ast.counter_set.size(); i++) + { + if(!isIdentical(eval_ast.children()[i], *raw_ast.counter_set[i])) + { + return false; + } + } + return true; +} +} // namespace + +TEST(evaluate_ast, basic_copy) +{ + std::unordered_map metrics = { + {"SQ_WAVES", Metric("a", "a", "a", "a", "a", "", 0)}, + {"TCC_HIT", Metric("b", "b", "b", "b", "b", "", 1)}}; + + RawAST* ast = nullptr; + auto* buf = yy_scan_string("SQ_WAVES + TCC_HIT"); + yyparse(&ast); + ASSERT_TRUE(ast); + + auto eval_ast = EvaluateAST(metrics, *ast); + + EXPECT_TRUE(isIdentical(eval_ast, *ast)); + yy_delete_buffer(buf); + delete ast; +} + +TEST(evaluate_ast, counter_expansion) +{ + std::unordered_map metrics = { + {"SQ_WAVES", Metric("SQ_WAVES", "a", "a", "a", "", "", 0)}, + {"TCC_HIT", Metric("TCC_HIT", "b", "b", "b", "", "", 1)}, + {"TEST_DERRIVED", Metric("TEST_DERRIVED", "C", "C", "C", "SQ_WAVES+TCC_HIT", "", 2)}}; + + std::unordered_map asts; + for(auto [val, metric] : metrics) + { + RawAST* ast = nullptr; + auto buf = yy_scan_string(metric.expression().empty() ? metric.name().c_str() + : metric.expression().c_str()); + yyparse(&ast); + ASSERT_TRUE(ast); + asts.emplace(val, std::move(EvaluateAST(metrics, *ast))); + yy_delete_buffer(buf); + delete ast; + } + + std::set required_counters; + asts.at("TEST_DERRIVED").get_required_counters(asts, required_counters); + EXPECT_EQ(required_counters.size(), 2); + auto expected = std::set{{Metric("TCC_HIT", "b", "b", "b", "", "", 1), + Metric("SQ_WAVES", "a", "a", "a", "", "", 0)}}; + + for(auto& counter_found : required_counters) + { + EXPECT_NE(expected.find(counter_found), expected.end()); + } +} + +TEST(evaluate_ast, counter_expansion_multi_derived) +{ + std::unordered_map metrics = { + {"SQ_WAVES", Metric("SQ_WAVES", "a", "a", "a", "", "", 0)}, + {"TCC_HIT", Metric("TCC_HIT", "b", "b", "b", "", "", 1)}, + {"TEST_DERRIVED", Metric("TEST_DERRIVED", "C", "C", "C", "SQ_WAVES+TCC_HIT", "", 2)}, + {"TEST_DERRIVED3", + Metric("TEST_DERRIVED3", "C", "C", "C", "TEST_DERRIVED+SQ_WAVES+TCC_HIT", "", 3)}}; + + std::unordered_map asts; + for(auto [val, metric] : metrics) + { + RawAST* ast = nullptr; + auto buf = yy_scan_string(metric.expression().empty() ? metric.name().c_str() + : metric.expression().c_str()); + yyparse(&ast); + ASSERT_TRUE(ast); + asts.emplace(val, std::move(EvaluateAST(metrics, *ast))); + yy_delete_buffer(buf); + delete ast; + } + + std::set required_counters; + asts.at("TEST_DERRIVED3").get_required_counters(asts, required_counters); + EXPECT_EQ(required_counters.size(), 2); + auto expected = std::set{{Metric("TCC_HIT", "b", "b", "b", "", "", 1), + Metric("SQ_WAVES", "a", "a", "a", "", "", 0)}}; + + for(auto& counter_found : required_counters) + { + EXPECT_NE(expected.find(counter_found), expected.end()); + } +} + +TEST(evaluate_ast, counter_expansion_order) +{ + std::unordered_map metrics = { + {"SQ_WAVES", Metric("SQ_WAVES", "a", "a", "a", "", "", 0)}, + {"TCC_HIT", Metric("TCC_HIT", "b", "b", "b", "", "", 1)}, + {"VLL", Metric("VLL", "b", "b", "b", "", "", 4)}, + {"TEST_DERRIVED", Metric("TEST_DERRIVED", "C", "C", "C", "SQ_WAVES+VLL", "", 2)}, + {"TEST_DERRIVED3", + Metric("TEST_DERRIVED3", "C", "C", "C", "TEST_DERRIVED+SQ_WAVES+TCC_HIT", "", 3)}}; + + std::unordered_map asts; + for(auto [val, metric] : metrics) + { + RawAST* ast = nullptr; + auto buf = yy_scan_string(metric.expression().empty() ? metric.name().c_str() + : metric.expression().c_str()); + yyparse(&ast); + ASSERT_TRUE(ast); + asts.emplace(val, std::move(EvaluateAST(metrics, *ast))); + yy_delete_buffer(buf); + delete ast; + } + + std::set required_counters; + asts.at("TEST_DERRIVED3").get_required_counters(asts, required_counters); + EXPECT_EQ(required_counters.size(), 3); + auto expected = std::set{{Metric("VLL", "b", "b", "b", "", "", 4), + Metric("TCC_HIT", "b", "b", "b", "", "", 1), + Metric("SQ_WAVES", "a", "a", "a", "", "", 0)}}; + + for(auto& counter_found : required_counters) + { + EXPECT_NE(expected.find(counter_found), expected.end()); + } +} + +TEST(evaluate_ast, counter_expansion_function) +{ + std::unordered_map metrics = { + {"SQ_WAVES", Metric("SQ_WAVES", "a", "a", "a", "", "", 0)}, + {"TCC_HIT", Metric("TCC_HIT", "b", "b", "b", "", "", 1)}, + {"VLL", Metric("VLL", "b", "b", "b", "", "", 4)}, + {"TEST_DERRIVED", Metric("TEST_DERRIVED", "C", "C", "C", "SQ_WAVES+VLL", "", 2)}, + {"TEST_DERRIVED3", + Metric("TEST_DERRIVED3", + "C", + "C", + "C", + "reduce(TEST_DERRIVED,max)+SQ_WAVES+TCC_HIT", + "", + 3)}}; + + std::unordered_map asts; + for(auto [val, metric] : metrics) + { + RawAST* ast = nullptr; + auto buf = yy_scan_string(metric.expression().empty() ? metric.name().c_str() + : metric.expression().c_str()); + yyparse(&ast); + ASSERT_TRUE(ast); + asts.emplace(val, std::move(EvaluateAST(metrics, *ast))); + yy_delete_buffer(buf); + delete ast; + } + + std::set required_counters; + asts.at("TEST_DERRIVED3").get_required_counters(asts, required_counters); + EXPECT_EQ(required_counters.size(), 3); + auto expected = std::set{{Metric("VLL", "b", "b", "b", "", "", 4), + Metric("TCC_HIT", "b", "b", "b", "", "", 1), + Metric("SQ_WAVES", "a", "a", "a", "", "", 0)}}; + + for(auto& counter_found : required_counters) + { + EXPECT_NE(expected.find(counter_found), expected.end()); + } +} diff --git a/source/lib/rocprofiler/counters/tests/metrics_test.cpp b/source/lib/rocprofiler/counters/tests/metrics_test.cpp index fa6b2f317e..5d34ade974 100644 --- a/source/lib/rocprofiler/counters/tests/metrics_test.cpp +++ b/source/lib/rocprofiler/counters/tests/metrics_test.cpp @@ -6,6 +6,8 @@ namespace { +namespace counters = ::rocprofiler::counters; + auto loadTestData(const std::unordered_map>>& map) { @@ -15,8 +17,13 @@ loadTestData(const std::unordered_map{}).first->second; for(const auto& data_vec : dataMap) { - metric_vec.emplace_back( - data_vec.at(0), data_vec.at(1), data_vec.at(2), data_vec.at(4), data_vec.at(3)); + metric_vec.emplace_back(data_vec.at(0), + data_vec.at(1), + data_vec.at(2), + data_vec.at(4), + data_vec.at(3), + "", + 0); } } return ret; @@ -25,18 +32,61 @@ loadTestData(const std::unordered_map std::optional { + for(const auto& ditr : rocp_data_v) + if(ditr.name() == v.name()) return ditr; + return std::nullopt; + }; + auto equal = [](const auto& lhs, const auto& rhs) { + return std::tie(lhs.name(), lhs.block(), lhs.event(), lhs.description()) == + std::tie(rhs.name(), rhs.block(), rhs.event(), rhs.description()); + }; + for(const auto& itr : test_data_v) + { + auto val = find(itr); + if(!val) + { + EXPECT_TRUE(val) << "failed to find " << fmt::format("{}", itr); + continue; + } + EXPECT_TRUE(equal(itr, *val)) << fmt::format("\n\t{} \n\t\t!= \n\t{}", itr, *val); + } } TEST(metrics, derived_load) { - auto x = counters::getDerivedHardwareMetrics(); - auto test_data = loadTestData(derrived_gfx908); - ASSERT_EQ(x.count("gfx908"), 1); + auto rocp_data = counters::getDerivedHardwareMetrics(); + auto test_data = loadTestData(derived_gfx908); + ASSERT_EQ(rocp_data.count("gfx908"), 1); ASSERT_EQ(test_data.count("gfx908"), 1); - EXPECT_EQ(fmt::format("{}", x["gfx908"]), fmt::format("{}", test_data["gfx908"])); + auto rocp_data_v = rocp_data.at("gfx908"); + auto test_data_v = test_data.at("gfx908"); + EXPECT_EQ(rocp_data_v.size(), test_data_v.size()); + auto find = [&rocp_data_v](const auto& v) -> std::optional { + for(const auto& ditr : rocp_data_v) + if(ditr.name() == v.name()) return ditr; + return std::nullopt; + }; + auto equal = [](const auto& lhs, const auto& rhs) { + return std::tie( + lhs.name(), lhs.block(), lhs.event(), lhs.description(), lhs.expression()) == + std::tie(rhs.name(), rhs.block(), rhs.event(), rhs.description(), rhs.expression()); + }; + for(const auto& itr : test_data_v) + { + auto val = find(itr); + if(!val) + { + EXPECT_TRUE(val) << "failed to find " << fmt::format("{}", itr); + continue; + } + EXPECT_TRUE(equal(itr, *val)) << fmt::format("\n\t{} \n\t\t!= \n\t{}", itr, *val); + } } diff --git a/source/lib/rocprofiler/counters/tests/metrics_test.h b/source/lib/rocprofiler/counters/tests/metrics_test.h index a29e7fcca7..5cc9c162b0 100644 --- a/source/lib/rocprofiler/counters/tests/metrics_test.h +++ b/source/lib/rocprofiler/counters/tests/metrics_test.h @@ -9,7 +9,12 @@ // Layout is: {name, block, event, expression, description}. static const std::unordered_map>> basic_gfx908 = { {"gfx908", - {{"SQ_INSTS_VMEM_WR", + {{"MAX_WAVE_SIZE", "", "", "1", "Max wave size constant"}, + {"KERNEL_DURATION", "", "", "1", "The duration of the kernel dispatch"}, + {"SE_NUM", "", "", "1", "SE_NUM"}, + {"SIMD_NUM", "", "", "1", "SIMD Number"}, + {"CU_NUM", "", "", "1", "CU_NUM"}, + {"SQ_INSTS_VMEM_WR", "SQ", "28", "", @@ -138,186 +143,192 @@ static const std::unordered_map", "TCP stalls TA data interface. Now Windowed."}}}}; -static const std::unordered_map>> - derrived_gfx908 = { - {"gfx908", - {{"TCC_HIT_sum", - "", - "", - "sum(TCC_HIT,32)", - "Number of cache hits. Sum over TCC instances."}, - {"TCC_MISS_sum", - "", - "", - "sum(TCC_MISS,32)", - "Number of cache misses. Sum over TCC instances."}, - {"TCC_EA_RDREQ_32B_sum", - "", - "", - "sum(TCC_EA_RDREQ_32B,32)", - "Number of 32-byte TCC/EA read requests. Sum over TCC instances."}, - {"TCC_EA_RDREQ_sum", - "", - "", - "sum(TCC_EA_RDREQ,32)", - "Number of TCC/EA read requests (either 32-byte or 64-byte). Sum over TCC instances."}, - {"TCC_EA_WRREQ_sum", - "", - "", - "sum(TCC_EA_WRREQ,32)", - "Number of transactions (either 32-byte or 64-byte) going over the TC_EA_wrreq " - "interface. Sum over TCC instances."}, - {"TCC_EA_WRREQ_64B_sum", - "", - "", - "sum(TCC_EA_WRREQ_64B,32)", - "Number of 64-byte transactions going (64-byte write or CMPSWAP) over the TC_EA_wrreq " - "interface. Sum over TCC instances."}, - {"TCC_WRREQ_STALL_max", - "", - "", - "max(TCC_EA_WRREQ_STALL,32)", - "Number of cycles a write request was stalled. Max over TCC instances."}, - {"CU_UTILIZATION", - "", - "", - "GRBM_GUI_ACTIVE/GRBM_COUNT", - "The total number of active cycles divided by total number of elapsed cycles"}, - {"KERNEL_DURATION", "", "", "1", "The duration of the kernel dispatch"}, - {"TA_BUSY_avr", - "", - "", - "avr(TA_TA_BUSY,16)", - "TA block is busy. Average over TA instances."}, - {"TA_BUSY_max", "", "", "max(TA_TA_BUSY,16)", "TA block is busy. Max over TA instances."}, - {"TA_BUSY_min", "", "", "min(TA_TA_BUSY,16)", "TA block is busy. Min over TA instances."}, - {"TA_FLAT_READ_WAVEFRONTS_sum", - "", - "", - "sum(TA_FLAT_READ_WAVEFRONTS,16)", - "Number of flat opcode reads processed by the TA. Sum over TA instances."}, - {"TA_FLAT_WRITE_WAVEFRONTS_sum", - "", - "", - "sum(TA_FLAT_WRITE_WAVEFRONTS,16)", - "Number of flat opcode writes processed by the TA. Sum over TA instances."}, - {"TCP_TCP_TA_DATA_STALL_CYCLES_sum", - "", - "", - "sum(TCP_TCP_TA_DATA_STALL_CYCLES,16)", - "Total number of TCP stalls TA data interface."}, - {"TCP_TCP_TA_DATA_STALL_CYCLES_max", - "", - "", - "max(TCP_TCP_TA_DATA_STALL_CYCLES,16)", - "Maximum number of TCP stalls TA data interface."}, - {"FETCH_SIZE", - "", - "", - "(TCC_EA_RDREQ_32B_sum*32+(TCC_EA_RDREQ_sum-TCC_EA_RDREQ_32B_sum)*64)/1024", - "The total kilobytes fetched from the video memory. This is measured with all extra " - "fetches and any cache or memory effects taken into account."}, - {"WRITE_SIZE", - "", - "", - "((TCC_EA_WRREQ_sum-TCC_EA_WRREQ_64B_sum)*32+TCC_EA_WRREQ_64B_sum*64)/1024", - "The total kilobytes written to the video memory. This is measured with all extra " - "fetches and any cache or memory effects taken into account."}, - {"WRITE_REQ_32B", - "", - "", - "TCC_EA_WRREQ_64B_sum*2+(TCC_EA_WRREQ_sum-TCC_EA_WRREQ_64B_sum)", - "The total number of 32-byte effective memory writes."}, - {"VFetchInsts", - "", - "", - "(SQ_INSTS_VMEM_RD-TA_FLAT_READ_WAVEFRONTS_sum)/SQ_WAVES", - "The average number of vector fetch instructions from the video memory executed per " - "work-item (affected by flow control). Excludes FLAT instructions that fetch from video " - "memory."}, - {"VWriteInsts", - "", - "", - "(SQ_INSTS_VMEM_WR-TA_FLAT_WRITE_WAVEFRONTS_sum)/SQ_WAVES", - "The average number of vector write instructions to the video memory executed per " - "work-item (affected by flow control). Excludes FLAT instructions that write to video " - "memory."}, - {"FlatVMemInsts", - "", - "", - "(SQ_INSTS_FLAT-SQ_INSTS_FLAT_LDS_ONLY)/SQ_WAVES", - "The average number of FLAT instructions that read from or write to the video memory " - "executed per work item (affected by flow control). Includes FLAT instructions that " - "read from or write to scratch."}, - {"LDSInsts", - "", - "", - "(SQ_INSTS_LDS-SQ_INSTS_FLAT_LDS_ONLY)/SQ_WAVES", - "The average number of LDS read or LDS write instructions executed per work item " - "(affected by flow control). Excludes FLAT instructions that read from or write to " - "LDS."}, - {"FlatLDSInsts", - "", - "", - "SQ_INSTS_FLAT_LDS_ONLY/SQ_WAVES", - "The average number of FLAT instructions that read or write to LDS executed per work " - "item (affected by flow control)."}, - {"VALUUtilization", - "", - "", - "100*SQ_THREAD_CYCLES_VALU/(SQ_ACTIVE_INST_VALU*MAX_WAVE_SIZE)", - "The percentage of active vector ALU threads in a wave. A lower number can mean either " - "more thread divergence in a wave or that the work-group size is not a multiple of 64. " - "Value range: 0\% (bad), 100\% (ideal - no thread divergence)."}, - {"VALUBusy", - "", - "", - "100*SQ_ACTIVE_INST_VALU*4/SIMD_NUM/GRBM_GUI_ACTIVE", - "The percentage of GPUTime vector ALU instructions are processed. Value range: 0\% " - "(bad) to 100\% (optimal)."}, - {"SALUBusy", - "", - "", - "100*SQ_INST_CYCLES_SALU*4/SIMD_NUM/GRBM_GUI_ACTIVE", - "The percentage of GPUTime scalar ALU instructions are processed. Value range: 0% (bad) " - "to 100% (optimal)."}, - {"FetchSize", - "", - "", - "FETCH_SIZE", - "The total kilobytes fetched from the video memory. This is measured with all extra " - "fetches and any cache or memory effects taken into account."}, - {"WriteSize", - "", - "", - "WRITE_SIZE", - "The total kilobytes written to the video memory. This is measured with all extra " - "fetches and any cache or memory effects taken into account."}, - {"MemWrites32B", - "", - "", - "WRITE_REQ_32B", - "The total number of effective 32B write transactions to the memory"}, - {"L2CacheHit", - "", - "", - "100*sum(TCC_HIT,16)/(sum(TCC_HIT,16)+sum(TCC_MISS,16))", - "The percentage of fetch, write, atomic, and other instructions that hit the data in L2 " - "cache. Value range: 0\% (no hit) to 100\% (optimal)."}, - {"MemUnitStalled", - "", - "", - "100*TCP_TCP_TA_DATA_STALL_CYCLES_max/GRBM_GUI_ACTIVE/SE_NUM", - "The percentage of GPUTime the memory unit is stalled. Try reducing the number or size " - "of fetches and writes if possible. Value range: 0\% (optimal) to 100\% (bad)."}, - {"WriteUnitStalled", - "", - "", - "100*TCC_WRREQ_STALL_max/GRBM_GUI_ACTIVE", - "The percentage of GPUTime the Write unit is stalled. Value range: 0\% to 100\% (bad)."}, - {"LDSBankConflict", - "", - "", - "100*SQ_LDS_BANK_CONFLICT/GRBM_GUI_ACTIVE/CU_NUM", - "The percentage of GPUTime LDS is stalled by bank conflicts. Value range: 0\% (optimal) " - "to 100\% (bad)."}}}}; \ No newline at end of file +static const std::unordered_map>> derived_gfx908 = + {{"gfx908", + {{"TCC_HIT_sum", + "", + "", + "reduce(TCC_HIT,sum)", + "Number of cache hits. Sum over TCC instances."}, + {"TCC_MISS_sum", + "", + "", + "reduce(TCC_MISS,sum)", + "Number of cache misses. Sum over TCC instances."}, + {"TCC_EA_RDREQ_32B_sum", + "", + "", + "reduce(TCC_EA_RDREQ_32B,sum)", + "Number of 32-byte TCC/EA read requests. Sum over TCC instances."}, + {"TCC_EA_RDREQ_sum", + "", + "", + "reduce(TCC_EA_RDREQ,sum)", + "Number of TCC/EA read requests (either 32-byte or 64-byte). Sum over TCC instances."}, + {"TCC_EA_WRREQ_sum", + "", + "", + "reduce(TCC_EA_WRREQ,sum)", + "Number of transactions (either 32-byte or 64-byte) going over the TC_EA_wrreq " + "interface. Sum over TCC instances."}, + {"TCC_EA_WRREQ_64B_sum", + "", + "", + "reduce(TCC_EA_WRREQ_64B,sum)", + "Number of 64-byte transactions going (64-byte write or CMPSWAP) over the TC_EA_wrreq " + "interface. Sum over TCC instances."}, + {"TCC_WRREQ_STALL_max", + "", + "", + "reduce(TCC_EA_WRREQ_STALL,max)", + "Number of cycles a write request was stalled. Max over TCC instances."}, + {"CU_UTILIZATION", + "", + "", + "GRBM_GUI_ACTIVE/GRBM_COUNT", + "The total number of active cycles divided by total number of elapsed cycles"}, + {"TA_BUSY_avr", + "", + "", + "reduce(TA_TA_BUSY,average)", + "TA block is busy. Average over TA instances."}, + {"TA_BUSY_max", + "", + "", + "reduce(TA_TA_BUSY,max)", + "TA block is busy. Max over TA instances."}, + {"TA_BUSY_min", + "", + "", + "reduce(TA_TA_BUSY,min)", + "TA block is busy. Min over TA instances."}, + {"TA_FLAT_READ_WAVEFRONTS_sum", + "", + "", + "reduce(TA_FLAT_READ_WAVEFRONTS,sum)", + "Number of flat opcode reads processed by the TA. Sum over TA instances."}, + {"TA_FLAT_WRITE_WAVEFRONTS_sum", + "", + "", + "reduce(TA_FLAT_WRITE_WAVEFRONTS,sum)", + "Number of flat opcode writes processed by the TA. Sum over TA instances."}, + {"TCP_TCP_TA_DATA_STALL_CYCLES_sum", + "", + "", + "reduce(TCP_TCP_TA_DATA_STALL_CYCLES,sum)", + "Total number of TCP stalls TA data interface."}, + {"TCP_TCP_TA_DATA_STALL_CYCLES_max", + "", + "", + "reduce(TCP_TCP_TA_DATA_STALL_CYCLES,max)", + "Maximum number of TCP stalls TA data interface."}, + {"FETCH_SIZE", + "", + "", + "(TCC_EA_RDREQ_32B_sum*32+(TCC_EA_RDREQ_sum-TCC_EA_RDREQ_32B_sum)*64)/1024", + "The total kilobytes fetched from the video memory. This is measured with all extra " + "fetches and any cache or memory effects taken into account."}, + {"WRITE_SIZE", + "", + "", + "((TCC_EA_WRREQ_sum-TCC_EA_WRREQ_64B_sum)*32+TCC_EA_WRREQ_64B_sum*64)/1024", + "The total kilobytes written to the video memory. This is measured with all extra " + "fetches and any cache or memory effects taken into account."}, + {"WRITE_REQ_32B", + "", + "", + "TCC_EA_WRREQ_64B_sum*2+(TCC_EA_WRREQ_sum-TCC_EA_WRREQ_64B_sum)", + "The total number of 32-byte effective memory writes."}, + {"VFetchInsts", + "", + "", + "(SQ_INSTS_VMEM_RD-TA_FLAT_READ_WAVEFRONTS_sum)/SQ_WAVES", + "The average number of vector fetch instructions from the video memory executed per " + "work-item (affected by flow control). Excludes FLAT instructions that fetch from video " + "memory."}, + {"VWriteInsts", + "", + "", + "(SQ_INSTS_VMEM_WR-TA_FLAT_WRITE_WAVEFRONTS_sum)/SQ_WAVES", + "The average number of vector write instructions to the video memory executed per " + "work-item (affected by flow control). Excludes FLAT instructions that write to video " + "memory."}, + {"FlatVMemInsts", + "", + "", + "(SQ_INSTS_FLAT-SQ_INSTS_FLAT_LDS_ONLY)/SQ_WAVES", + "The average number of FLAT instructions that read from or write to the video memory " + "executed per work item (affected by flow control). Includes FLAT instructions that " + "read from or write to scratch."}, + {"LDSInsts", + "", + "", + "(SQ_INSTS_LDS-SQ_INSTS_FLAT_LDS_ONLY)/SQ_WAVES", + "The average number of LDS read or LDS write instructions executed per work item " + "(affected by flow control). Excludes FLAT instructions that read from or write to " + "LDS."}, + {"FlatLDSInsts", + "", + "", + "SQ_INSTS_FLAT_LDS_ONLY/SQ_WAVES", + "The average number of FLAT instructions that read or write to LDS executed per work " + "item (affected by flow control)."}, + {"VALUUtilization", + "", + "", + "100*SQ_THREAD_CYCLES_VALU/(SQ_ACTIVE_INST_VALU*MAX_WAVE_SIZE)", + "The percentage of active vector ALU threads in a wave. A lower number can mean either " + "more thread divergence in a wave or that the work-group size is not a multiple of 64. " + "Value range: 0\% (bad), 100\% (ideal - no thread divergence)."}, + {"VALUBusy", + "", + "", + "100*SQ_ACTIVE_INST_VALU*4/SIMD_NUM/GRBM_GUI_ACTIVE", + "The percentage of GPUTime vector ALU instructions are processed. Value range: 0\% " + "(bad) to 100\% (optimal)."}, + {"SALUBusy", + "", + "", + "100*SQ_INST_CYCLES_SALU*4/SIMD_NUM/GRBM_GUI_ACTIVE", + "The percentage of GPUTime scalar ALU instructions are processed. Value range: 0% (bad) " + "to 100% (optimal)."}, + {"FetchSize", + "", + "", + "FETCH_SIZE", + "The total kilobytes fetched from the video memory. This is measured with all extra " + "fetches and any cache or memory effects taken into account."}, + {"WriteSize", + "", + "", + "WRITE_SIZE", + "The total kilobytes written to the video memory. This is measured with all extra " + "fetches and any cache or memory effects taken into account."}, + {"MemWrites32B", + "", + "", + "WRITE_REQ_32B", + "The total number of effective 32B write transactions to the memory"}, + {"L2CacheHit", + "", + "", + "100*reduce(TCC_HIT,sum)/(reduce(TCC_HIT,sum)+reduce(TCC_MISS,sum))", + "The percentage of fetch, write, atomic, and other instructions that hit the data in L2 " + "cache. Value range: 0\% (no hit) to 100\% (optimal)."}, + {"MemUnitStalled", + "", + "", + "100*TCP_TCP_TA_DATA_STALL_CYCLES_max/GRBM_GUI_ACTIVE/SE_NUM", + "The percentage of GPUTime the memory unit is stalled. Try reducing the number or size " + "of fetches and writes if possible. Value range: 0\% (optimal) to 100\% (bad)."}, + {"WriteUnitStalled", + "", + "", + "100*TCC_WRREQ_STALL_max/GRBM_GUI_ACTIVE", + "The percentage of GPUTime the Write unit is stalled. Value range: 0\% to 100\% (bad)."}, + {"LDSBankConflict", + "", + "", + "100*SQ_LDS_BANK_CONFLICT/GRBM_GUI_ACTIVE/CU_NUM", + "The percentage of GPUTime LDS is stalled by bank conflicts. Value range: 0\% (optimal) " + "to 100\% (bad)."}}}}; diff --git a/source/lib/rocprofiler/counters/xml/CMakeLists.txt b/source/lib/rocprofiler/counters/xml/CMakeLists.txt index 309987bf3c..34aaf4ba06 100644 --- a/source/lib/rocprofiler/counters/xml/CMakeLists.txt +++ b/source/lib/rocprofiler/counters/xml/CMakeLists.txt @@ -1,3 +1,10 @@ -configure_file(basic_counters.xml ${PROJECT_BINARY_DIR}/lib/basic_counters.xml COPYONLY) -configure_file(derived_counters.xml ${PROJECT_BINARY_DIR}/lib/derived_counters.xml - COPYONLY) +configure_file(basic_counters.xml + ${PROJECT_BINARY_DIR}/share/rocprofiler/basic_counters.xml COPYONLY) +configure_file(derived_counters.xml + ${PROJECT_BINARY_DIR}/share/rocprofiler/derived_counters.xml COPYONLY) + +install( + FILES ${PROJECT_BINARY_DIR}/share/rocprofiler/basic_counters.xml + ${PROJECT_BINARY_DIR}/share/rocprofiler/derived_counters.xml + DESTINATION share/rocprofiler + COMPONENT core) diff --git a/source/lib/rocprofiler/counters/xml/basic_counters.xml b/source/lib/rocprofiler/counters/xml/basic_counters.xml index 3ffb3191c4..9de1aef1c0 100755 --- a/source/lib/rocprofiler/counters/xml/basic_counters.xml +++ b/source/lib/rocprofiler/counters/xml/basic_counters.xml @@ -1,4 +1,9 @@ + + + + + @@ -33,6 +38,11 @@ + + + + + @@ -70,6 +80,11 @@ # EA1 + + + + + @@ -103,6 +118,11 @@ + + + + + @@ -366,6 +386,11 @@ + + + + + @@ -634,6 +659,11 @@ + + + + + @@ -678,7 +708,7 @@ - + @@ -694,6 +724,11 @@ + + + + + @@ -741,4 +776,3 @@ - diff --git a/source/lib/rocprofiler/counters/xml/derived_counters.xml b/source/lib/rocprofiler/counters/xml/derived_counters.xml index 05fd8c6603..d9a47743b4 100755 --- a/source/lib/rocprofiler/counters/xml/derived_counters.xml +++ b/source/lib/rocprofiler/counters/xml/derived_counters.xml @@ -1,16 +1,15 @@ - - - - - - - + + + + + + - - - - - + + + + + @@ -26,31 +25,30 @@ - - + + # LDSBankConflict The percentage of GPUTime LDS is stalled by bank conflicts. Value range: 0% (optimal) to 100% (bad). - + - - - - - - - + + + + + + - - - - - - - + + + + + + + - - + + @@ -66,20 +64,20 @@ - + # LDSBankConflict The percentage of GPUTime LDS is stalled by bank conflicts. Value range: 0% (optimal) to 100% (bad). - + - + # EA1 - - - - - + + + + + @@ -88,134 +86,134 @@ - + - - - - - - - - + + + + + + + + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -290,11 +288,11 @@ - + - - - + + + @@ -307,114 +305,114 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -423,10 +421,9 @@ - + - - + @@ -439,90 +436,89 @@ - - - - - + + + + + - - - - - - - - - - - + + + + + + + + + + + - + - - + + - - + + - - + + - - + - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - + - - + + - - + + - - - - + + + + # Vega20 - + # Arcturus - + # Aldebaran - + #Mi300 - - - + + + #Navi21 - - - + + + #Navi31 - - + + @@ -572,7 +568,7 @@ # ALUStalledByLDS The percentage of GPUTime ALU units are stalled by the LDS input queue being full or the output queue being not ready. If there are LDS bank conflicts, reduce them. Otherwise, try reducing the number of LDS accesses if possible. Value range: 0% (optimal) to 100% (bad). diff --git a/source/lib/rocprofiler/dispatch_profile.cpp b/source/lib/rocprofiler/dispatch_profile.cpp new file mode 100644 index 0000000000..2c8b4dbb08 --- /dev/null +++ b/source/lib/rocprofiler/dispatch_profile.cpp @@ -0,0 +1,32 @@ +#include + +#include "lib/rocprofiler/aql/helpers.hpp" +#include "lib/rocprofiler/counters/core.hpp" +#include "lib/rocprofiler/counters/evaluate_ast.hpp" +#include "lib/rocprofiler/counters/metrics.hpp" +#include "lib/rocprofiler/hsa/agent_cache.hpp" + +extern "C" { +/** + * @brief Configure Dispatch Profile Counting Service. + * + * @param [in] context_id + * @param [in] agent_id + * @param [in] buffer_id + * @param [in] callback + * @param [in] callback_data_args + * @return ::rocprofiler_status_t + */ +rocprofiler_status_t ROCPROFILER_API +rocprofiler_configure_dispatch_profile_counting_service( + rocprofiler_context_id_t context_id, + rocprofiler_profile_config_id_t profile, + rocprofiler_profile_counting_dispatch_callback_t callback, + void* callback_data_args) +{ + return rocprofiler::counters::configure_dispatch( + context_id, profile.handle, callback, callback_data_args) + ? ROCPROFILER_STATUS_SUCCESS + : ROCPROFILER_STATUS_ERROR; +} +} diff --git a/source/lib/rocprofiler/hsa/CMakeLists.txt b/source/lib/rocprofiler/hsa/CMakeLists.txt index 6ee898915c..aadef71a80 100644 --- a/source/lib/rocprofiler/hsa/CMakeLists.txt +++ b/source/lib/rocprofiler/hsa/CMakeLists.txt @@ -1,5 +1,6 @@ -set(ROCPROFILER_LIB_HSA_SOURCES hsa.cpp) -set(ROCPROFILER_LIB_HSA_HEADERS hsa.hpp defines.hpp types.hpp utils.hpp) +set(ROCPROFILER_LIB_HSA_SOURCES hsa.cpp queue.cpp agent_cache.cpp) +set(ROCPROFILER_LIB_HSA_HEADERS hsa.hpp defines.hpp types.hpp utils.hpp queue.hpp + agent_cache.hpp) target_sources(rocprofiler-object-library PRIVATE ${ROCPROFILER_LIB_HSA_SOURCES} ${ROCPROFILER_LIB_HSA_HEADERS}) diff --git a/source/lib/rocprofiler/hsa/agent_cache.cpp b/source/lib/rocprofiler/hsa/agent_cache.cpp new file mode 100644 index 0000000000..31f0326424 --- /dev/null +++ b/source/lib/rocprofiler/hsa/agent_cache.cpp @@ -0,0 +1,196 @@ +// Copyright (c) 2018-2023 Advanced Micro Devices, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in 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: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// 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 +// AUTHORS 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 IN +// THE SOFTWARE. + +#include "agent_cache.hpp" + +#include +#include +#include +#include + +#include "lib/common/synchronized.hpp" +#include "lib/common/utility.hpp" + +namespace +{ +// This function checks to see if the provided +// pool has the HSA_AMD_SEGMENT_GLOBAL property. If the kern_arg flag is true, +// the function adds an additional requirement that the pool have the +// HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_KERNARG_INIT property. If kern_arg is false, +// pools must NOT have this property. +// Upon finding a pool that meets these conditions, HSA_STATUS_INFO_BREAK is +// returned. HSA_STATUS_SUCCESS is returned if no errors were encountered, but +// no pool was found meeting the requirements. If an error is encountered, we +// return that error. +hsa_status_t +FindGlobalPool(hsa_amd_memory_pool_t pool, void* data, bool kern_arg) +{ + if(!data) return HSA_STATUS_ERROR_INVALID_ARGUMENT; + + auto [api_ptr, pool_ptr] = + *static_cast*>(data); + hsa_amd_segment_t segment; + LOG_IF(FATAL, + api_ptr->hsa_amd_memory_pool_get_info_fn( + pool, HSA_AMD_MEMORY_POOL_INFO_SEGMENT, &segment) == HSA_STATUS_ERROR) + << "Could not get pool segment"; + if(HSA_AMD_SEGMENT_GLOBAL != segment) return HSA_STATUS_SUCCESS; + + uint32_t flag; + LOG_IF(FATAL, + api_ptr->hsa_amd_memory_pool_get_info_fn( + pool, HSA_AMD_MEMORY_POOL_INFO_GLOBAL_FLAGS, &flag) == HSA_STATUS_ERROR) + << "Could not get flag value"; + uint32_t karg_st = flag & HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_KERNARG_INIT; + if((karg_st == 0 && kern_arg) || (karg_st != 0 && !kern_arg)) + { + return HSA_STATUS_SUCCESS; + } + *(pool_ptr) = pool; + return HSA_STATUS_INFO_BREAK; +} + +// This is the call-back function for hsa_amd_agent_iterate_memory_pools() that +// finds a pool with the properties of HSA_AMD_SEGMENT_GLOBAL and that is NOT +// HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_KERNARG_INIT +hsa_status_t +FindStandardPool(hsa_amd_memory_pool_t pool, void* data) +{ + return FindGlobalPool(pool, data, false); +} + +// This is the call-back function for hsa_amd_agent_iterate_memory_pools() that +// finds a pool with the properties of HSA_AMD_SEGMENT_GLOBAL and that IS +// HSA_AMD_MEMORY_POOL_GLOBAL_FLAG_KERNARG_INIT +hsa_status_t +FindKernArgPool(hsa_amd_memory_pool_t pool, void* data) +{ + return FindGlobalPool(pool, data, true); +} + +void +init_cpu_pool(const AmdExtTable& api, rocprofiler::hsa::AgentCache& agent) +{ + std::pair params = + std::make_pair(&api, &agent.cpu_pool()); + + auto status = + api.hsa_amd_agent_iterate_memory_pools_fn(agent.near_cpu(), FindStandardPool, ¶ms); + LOG_IF(FATAL, status != HSA_STATUS_SUCCESS && status != HSA_STATUS_INFO_BREAK) + << "Error: Command Buffer Pool is not initialized"; + + params.second = &agent.kernarg_pool(); + status = + api.hsa_amd_agent_iterate_memory_pools_fn(agent.near_cpu(), FindKernArgPool, &(params)); + LOG_IF(FATAL, status != HSA_STATUS_SUCCESS && status != HSA_STATUS_INFO_BREAK) + << "Error: Output Buffer Pool is not initialized"; +} + +void +init_gpu_pool(const AmdExtTable& api, rocprofiler::hsa::AgentCache& agent) +{ + std::pair params = + std::make_pair(&api, &agent.gpu_pool()); + auto status = + api.hsa_amd_agent_iterate_memory_pools_fn(agent.get_agent(), FindStandardPool, ¶ms); + + LOG_IF(FATAL, status != HSA_STATUS_SUCCESS && status != HSA_STATUS_INFO_BREAK) + << "Error: GPU Pool is not initialized"; +} + +} // namespace + +namespace rocprofiler +{ +namespace hsa +{ +AgentCache::AgentCache(rocprofiler_agent_t agent_t, + size_t index, + const ::CoreApiTable& table, + const AmdExtTable& ext) +: _agent_t(agent_t) +, _index(index) +, _name(agent_t.name) +{ + // Get HSA Agents + std::vector agents; + table.hsa_iterate_agents_fn( + [](hsa_agent_t agent, void* data) { + CHECK_NOTNULL(static_cast*>(data))->emplace_back(agent); + return HSA_STATUS_SUCCESS; + }, + &agents); + + // In case HSA_AMD_AGENT_INFO_NEAREST_CPU is non-functional, default to original v1 behavior + // of last CPU agent being nearest. + std::optional last_cpu; + + bool found = false; + // Find the HSA agent that is represented by rocprofiler_agent_t + for(const auto& agent : agents) + { + hsa_device_type_t type = HSA_DEVICE_TYPE_CPU; + if(table.hsa_agent_get_info_fn(agent, HSA_AGENT_INFO_DEVICE, &type) != HSA_STATUS_SUCCESS) + { + throw std::runtime_error("hsa_agent_get_info failed to find device"); + } + + if(type != HSA_DEVICE_TYPE_GPU) + { + if(type == HSA_DEVICE_TYPE_CPU && !last_cpu) last_cpu = agent; + continue; + } + + uint32_t node_id = 0; + if(table.hsa_agent_get_info_fn( + agent, static_cast(HSA_AMD_AGENT_INFO_DRIVER_NODE_ID), &node_id) != + HSA_STATUS_SUCCESS) + { + throw std::runtime_error("hsa_agent_get_info failed to find driver id"); + } + + // Match rocprofiler_agent_t to hsa_agent for GPU agents + if(_index != node_id) continue; + + if(table.hsa_agent_get_info_fn( + agent, + static_cast(HSA_AMD_AGENT_INFO_NEAREST_CPU), + &_nearest_cpu) != HSA_STATUS_SUCCESS) + { + if(!last_cpu) throw std::runtime_error("HSA_AMD_AGENT_INFO_NEAREST_CPU failed!"); + _nearest_cpu = *last_cpu; + } + + found = true; + _agent = agent; + } + + if(!found) + { + throw std::runtime_error(fmt::format("Could not find GPU id = {}", agent_t.id.handle)); + } + + // Construct CPU/GPU pools + init_cpu_pool(ext, *this); + init_gpu_pool(ext, *this); +} + +} // namespace hsa +} // namespace rocprofiler \ No newline at end of file diff --git a/source/lib/rocprofiler/hsa/agent_cache.hpp b/source/lib/rocprofiler/hsa/agent_cache.hpp new file mode 100644 index 0000000000..4184081592 --- /dev/null +++ b/source/lib/rocprofiler/hsa/agent_cache.hpp @@ -0,0 +1,86 @@ +// Copyright (c) 2018-2023 Advanced Micro Devices, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in 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: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// 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 +// AUTHORS 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 IN +// THE SOFTWARE. + +#pragma once + +#include +#include +#include + +#include "fmt/core.h" +#include "fmt/ranges.h" + +#include +#include +#include +#include + +#include +#include "lib/common/utility.hpp" + +// Construct const and non-const accessor functions +#define CONST_NONCONST_ACCESSOR(RTYPE, NAME, VAL) \ + const RTYPE& NAME() const { return VAL; } \ + RTYPE& NAME() { return VAL; } + +namespace rocprofiler +{ +namespace hsa +{ +static const uint32_t LDS_BLOCK_SIZE = 128 * 4; + +// Stores per-agent HSA information such as GPU and Kernel pools +// along with nearest CPU agent and its pool. Links rocprofiler_agent_t +// to its HSA agent. Note this class is only valid when HSA is +// init'd +class AgentCache +{ +public: + AgentCache(rocprofiler_agent_t, size_t index, const ::CoreApiTable&, const AmdExtTable&); + + // Provides const and a non-const accessor functions. + CONST_NONCONST_ACCESSOR(hsa_amd_memory_pool_t, cpu_pool, _cpu_pool); + CONST_NONCONST_ACCESSOR(hsa_amd_memory_pool_t, kernarg_pool, _kernarg_pool); + CONST_NONCONST_ACCESSOR(hsa_amd_memory_pool_t, gpu_pool, _gpu_pool); + CONST_NONCONST_ACCESSOR(rocprofiler_agent_t, agent_t, _agent_t); + CONST_NONCONST_ACCESSOR(hsa_agent_t, get_agent, _agent); + CONST_NONCONST_ACCESSOR(hsa_agent_t, near_cpu, _nearest_cpu); + + const std::string& name() const { return _name; } + +private: + // Agent info + rocprofiler_agent_t _agent_t; + size_t _index{0}; // rocprofiler_agent index + + // GPU Agent + hsa_agent_t _agent{.handle = 0}; + hsa_agent_t _nearest_cpu{.handle = 0}; + + // memory pools + hsa_amd_memory_pool_t _cpu_pool{.handle = 0}; + hsa_amd_memory_pool_t _kernarg_pool{.handle = 0}; + hsa_amd_memory_pool_t _gpu_pool{.handle = 0}; + + std::string _name; +}; + +} // namespace hsa +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/hsa/queue.cpp b/source/lib/rocprofiler/hsa/queue.cpp new file mode 100644 index 0000000000..a09b660850 --- /dev/null +++ b/source/lib/rocprofiler/hsa/queue.cpp @@ -0,0 +1,471 @@ +/* Copyright (c) 2022 Advanced Micro Devices, Inc. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 + AUTHORS 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 IN + THE SOFTWARE. */ + +#include "lib/rocprofiler/hsa/queue.hpp" + +#include + +namespace rocprofiler +{ +namespace hsa +{ +namespace +{ +bool +AsyncSignalHandler(hsa_signal_value_t, void* data) +{ + if(!data) return true; + auto& queue_info_session = *static_cast(data); + + // Calls our internal callbacks to callers who need to be notified post + // kernel execution. + queue_info_session.queue.signal_callback([&](const auto& map) { + for(const auto& [client_id, cb_pair] : map) + { + // If this is the client that gave us the AQLPacket, + // return it to that client otherwise notify. + if(queue_info_session.inst_pkt_id == client_id) + { + cb_pair.second(queue_info_session.queue, + client_id, + queue_info_session.kernel_pkt, + std::move(queue_info_session.inst_pkt)); + } + else + { + cb_pair.second( + queue_info_session.queue, client_id, queue_info_session.kernel_pkt, nullptr); + } + } + }); + + // Delete signals and packets, signal we have completed. + if(queue_info_session.interrupt_signal.handle != 0u) + queue_info_session.queue.core_api().hsa_signal_destroy_fn( + queue_info_session.interrupt_signal); + if(queue_info_session.kernel_pkt.completion_signal.handle != 0u) + { + queue_info_session.queue.core_api().hsa_signal_destroy_fn( + queue_info_session.kernel_pkt.completion_signal); + } + queue_info_session.queue.async_complete(); + + delete static_cast(data); + return false; +} + +void +CreateBarrierPacket(const hsa_signal_t& packet_completion_signal, + std::vector& transformed_packets) +{ + hsa_barrier_and_packet_t barrier{}; + barrier.header = HSA_PACKET_TYPE_BARRIER_AND << HSA_PACKET_HEADER_TYPE; + barrier.dep_signal[0] = packet_completion_signal; + void* barrier_ptr = &barrier; + transformed_packets.emplace_back(*reinterpret_cast(barrier_ptr)); +} + +void +AddVendorSpecificPacket(const hsa_ext_amd_aql_pm4_packet_t& packet, + std::vector& transformed_packets, + const hsa_signal_t& packet_completion_signal) +{ + transformed_packets.emplace_back(packet).completion_signal = packet_completion_signal; +} +} // namespace + +void +Queue::signal_async_handler(const hsa_signal_t& signal, Queue::queue_info_session_t* data) const +{ + hsa_status_t status = _ext_api.hsa_amd_signal_async_handler_fn( + signal, HSA_SIGNAL_CONDITION_EQ, 0, AsyncSignalHandler, static_cast(data)); + LOG_IF(FATAL, status != HSA_STATUS_SUCCESS && status != HSA_STATUS_INFO_BREAK) + << "Error: hsa_amd_signal_async_handler failed"; +} + +void +Queue::create_signal(uint32_t attribute, hsa_signal_t* signal) const +{ + hsa_status_t status = _ext_api.hsa_amd_signal_create_fn(1, 0, nullptr, attribute, signal); + LOG_IF(FATAL, status != HSA_STATUS_SUCCESS && status != HSA_STATUS_INFO_BREAK) + << "Error: hsa_amd_signal_create failed"; +} + +template +constexpr Integral +bit_mask(int first, int last) +{ + assert(last >= first && "Error: hsa_support::bit_mask -> invalid argument"); + size_t num_bits = last - first + 1; + return ((num_bits >= sizeof(Integral) * 8) ? ~Integral{0} + /* num_bits exceed the size of Integral */ + : ((Integral{1} << num_bits) - 1)) + << first; +} + +/* Extract bits [last:first] from t. */ +template +constexpr Integral +bit_extract(Integral x, int first, int last) +{ + return (x >> first) & bit_mask(0, last - first); +} + +/** + * @brief This function is a queue write interceptor. It intercepts the + * packet write function. Creates an instance of packet class with the raw + * pointer. invoke the populate function of the packet class which returns a + * pointer to the packet. This packet is written into the queue by this + * interceptor by invoking the writer function. + */ +void +WriteInterceptor(const void* packets, + uint64_t pkt_count, + uint64_t, + void* data, + hsa_amd_queue_intercept_packet_writer writer) +{ + Queue& queue_info = *static_cast(data); + + // We have no packets or no one who needs to be notified, do nothing. + if(pkt_count == 0 || queue_info.get_notifiers() == 0) + { + writer(packets, pkt_count); + return; + } + + // hsa_ext_amd_aql_pm4_packet_t + const hsa_ext_amd_aql_pm4_packet_t* packets_arr = + static_cast(packets); + std::vector transformed_packets; + + // Searching accross all the packets given during this write + for(size_t i = 0; i < pkt_count; ++i) + { + const auto& original_packet = static_cast(packets)[i]; + if(bit_extract(original_packet.header, + HSA_PACKET_HEADER_TYPE, + HSA_PACKET_HEADER_TYPE + HSA_PACKET_HEADER_WIDTH_TYPE - 1) != + HSA_PACKET_TYPE_KERNEL_DISPATCH) + { + transformed_packets.emplace_back(packets_arr[i]); + continue; + } + + // Copy kernel pkt, copy is to allow for signal to be modified + hsa_ext_amd_aql_pm4_packet_t kernel_pkt = packets_arr[i]; + queue_info.create_signal(HSA_AMD_SIGNAL_AMD_GPU_ONLY, &kernel_pkt.completion_signal); + + // Stores the instrumentation pkt (i.e. AQL packets for counter collection) + // along with an ID of the client we got the packet from (this will be returned via + // CompletedCB) + ClientID inst_pkt_id = -1; + std::unique_ptr inst_pkt; + + // Signal callbacks that a kernel_pkt is being enqueued + queue_info.signal_callback([&](const auto& map) { + for(const auto& [client_id, cb_pair] : map) + { + if(auto maybe_pkt = cb_pair.first(queue_info, client_id, kernel_pkt)) + { + LOG_IF(FATAL, inst_pkt) + << "We do not support two injections into the HSA queue"; + inst_pkt = std::move(maybe_pkt); + inst_pkt_id = client_id; + } + } + }); + + // Write instrumentation start packet (if one exists) + if(inst_pkt) + { + hsa_signal_t dummy_signal{}; + dummy_signal.handle = 0; + inst_pkt->start.header = HSA_PACKET_TYPE_VENDOR_SPECIFIC << HSA_PACKET_HEADER_TYPE; + AddVendorSpecificPacket(inst_pkt->start, transformed_packets, dummy_signal); + + CreateBarrierPacket(inst_pkt->start.completion_signal, transformed_packets); + } + + transformed_packets.emplace_back(kernel_pkt); + + // Make a copy of the original packet, adding its signal to a barrier + // packet and create a new signal for it to get timestamps + if(original_packet.completion_signal.handle != 0u) + { + hsa_barrier_and_packet_t barrier{}; + barrier.header = HSA_PACKET_TYPE_BARRIER_AND << HSA_PACKET_HEADER_TYPE; + hsa_ext_amd_aql_pm4_packet_t* __attribute__((__may_alias__)) pkt = + (reinterpret_cast(&barrier)); + transformed_packets.emplace_back(*pkt).completion_signal = + original_packet.completion_signal; + } + + hsa_signal_t interrupt_signal{}; + // Adding a barrier packet with the original packet's completion signal. + queue_info.create_signal(0, &interrupt_signal); + + if(inst_pkt) + { + hsa_signal_t dummy_signal{}; + dummy_signal.handle = 0; + inst_pkt->stop.header = HSA_PACKET_TYPE_VENDOR_SPECIFIC << HSA_PACKET_HEADER_TYPE; + AddVendorSpecificPacket(inst_pkt->stop, transformed_packets, dummy_signal); + inst_pkt->read.header = HSA_PACKET_TYPE_VENDOR_SPECIFIC << HSA_PACKET_HEADER_TYPE; + AddVendorSpecificPacket(inst_pkt->read, transformed_packets, interrupt_signal); + + // Added Interrupt Signal with barrier and provided handler for it + CreateBarrierPacket(interrupt_signal, transformed_packets); + } + else + { + hsa_barrier_and_packet_t barrier{}; + barrier.header = HSA_PACKET_TYPE_BARRIER_AND << HSA_PACKET_HEADER_TYPE; + barrier.completion_signal = interrupt_signal; + hsa_ext_amd_aql_pm4_packet_t* __attribute__((__may_alias__)) pkt = + (reinterpret_cast(&barrier)); + transformed_packets.emplace_back(*pkt); + } + + // Enqueue the signal into the handler. Will call completed_cb when + // signal completes. + queue_info.async_started(); + queue_info.signal_async_handler( + interrupt_signal, + new Queue::queue_info_session_t{.queue = queue_info, + .inst_pkt = std::move(inst_pkt), + .inst_pkt_id = inst_pkt_id, + .kernel_pkt = kernel_pkt, + .interrupt_signal = interrupt_signal}); + } + + writer(transformed_packets.data(), transformed_packets.size()); +} + +Queue::Queue(const AgentCache& agent, + uint32_t size, + hsa_queue_type32_t type, + void (*callback)(hsa_status_t status, hsa_queue_t* source, void* data), + void* data, + uint32_t private_segment_size, + uint32_t group_segment_size, + CoreApiTable core_api, + AmdExtTable ext_api, + hsa_queue_t** queue) +: _core_api(core_api) +, _ext_api(ext_api) +, _agent(agent) + +{ + LOG_IF(FATAL, + _ext_api.hsa_amd_queue_intercept_create_fn(_agent.get_agent(), + size, + type, + callback, + data, + private_segment_size, + group_segment_size, + &_intercept_queue) != HSA_STATUS_SUCCESS) + << "Could not create intercept queue"; + + LOG_IF(FATAL, + _ext_api.hsa_amd_profiling_set_profiler_enabled_fn(_intercept_queue, true) != + HSA_STATUS_SUCCESS) + << "Could not setup intercept profiler"; + + LOG_IF(FATAL, + _ext_api.hsa_amd_queue_intercept_register_fn(_intercept_queue, WriteInterceptor, this)) + << "Could not register interceptor"; + *queue = _intercept_queue; +} + +void +Queue::register_callback(ClientID id, QueueCB enqueue_cb, CompletedCB complete_cb) +{ + _callbacks.wlock([&](auto& map) { + LOG_IF(FATAL, rocprofiler::common::get_val(map, id)) << "ID already exists!"; + _notifiers++; + map[id] = std::make_pair(enqueue_cb, complete_cb); + }); +} + +void +Queue::remove_callback(ClientID id) +{ + _callbacks.wlock([&](auto& map) { + if(map.erase(id) == 1) _notifiers--; + }); +} + +void +QueueController::add_queue(hsa_queue_t* id, std::unique_ptr queue) +{ + CHECK(queue); + _callback_cache.wlock([&](auto& callbacks) { + _queues.wlock([&](auto& map) { + const auto agent_id = queue->get_agent().agent_t().id.handle; + map[id] = std::move(queue); + for(const auto& [cbid, cb_tuple] : callbacks) + { + auto& [agent, qcb, ccb] = cb_tuple; + if(agent.id.handle == agent_id) + { + map[id]->register_callback(cbid, qcb, ccb); + } + } + }); + }); +} + +void +QueueController::destory_queue(hsa_queue_t* id) +{ + _queues.wlock([&](auto& map) { map.erase(id); }); +} + +ClientID +QueueController::add_callback(const rocprofiler_agent_t& agent, + Queue::QueueCB qcb, + Queue::CompletedCB ccb) +{ + static std::atomic client_id = 1; + ClientID return_id; + _callback_cache.wlock([&](auto& cb_cache) { + return_id = client_id; + cb_cache[client_id] = std::tuple(agent, qcb, ccb); + client_id++; + _queues.wlock([&](auto& map) { + for(auto& [_, queue] : map) + { + if(queue->get_agent().agent_t().id.handle == agent.id.handle) + { + queue->register_callback(return_id, qcb, ccb); + } + } + }); + }); + return return_id; +} + +void +QueueController::remove_callback(ClientID id) +{ + _callback_cache.wlock([&](auto& cb_cache) { + cb_cache.erase(id); + _queues.wlock([&](auto& map) { + for(auto& [_, queue] : map) + { + queue->remove_callback(id); + } + }); + }); +} + +// HSA Intercept Functions (create_queue/destroy_queue) +hsa_status_t +create_queue(hsa_agent_t agent, + uint32_t size, + hsa_queue_type32_t type, + void (*callback)(hsa_status_t status, hsa_queue_t* source, void* data), + void* data, + uint32_t private_segment_size, + uint32_t group_segment_size, + hsa_queue_t** queue) +{ + for(const auto& [_, agent_info] : get_queue_controller().get_supported_agents()) + { + if(agent_info.get_agent().handle == agent.handle) + { + auto new_queue = std::make_unique(agent_info, + size, + type, + callback, + data, + private_segment_size, + group_segment_size, + get_queue_controller().get_core_table(), + get_queue_controller().get_ext_table(), + queue); + get_queue_controller().add_queue(*queue, std::move(new_queue)); + return HSA_STATUS_SUCCESS; + } + } + LOG(FATAL) << "Could not find agent - " << agent.handle; + return HSA_STATUS_ERROR_FATAL; +} + +hsa_status_t +destroy_queue(hsa_queue_t* hsa_queue) +{ + get_queue_controller().destory_queue(hsa_queue); + return HSA_STATUS_SUCCESS; +} + +void +QueueController::Init(CoreApiTable& core_table, AmdExtTable& ext_table) +{ + _core_table = core_table; + _ext_table = ext_table; + + core_table.hsa_queue_create_fn = create_queue; + core_table.hsa_queue_destroy_fn = destroy_queue; + + // Generate supported agents + rocprofiler_query_available_agents( + [](const rocprofiler_agent_t** agents, size_t num_agents, void* user_data) { + CHECK(user_data); + QueueController& queue = *reinterpret_cast(user_data); + for(size_t i = 0; i < num_agents; i++) + { + const auto& agent = *agents[i]; + if(agent.type != ROCPROFILER_AGENT_TYPE_GPU) continue; + try + { + queue.get_supported_agents().emplace( + i, AgentCache{agent, i, queue.get_core_table(), queue.get_ext_table()}); + } catch(std::runtime_error& error) + { + LOG(ERROR) << fmt::format("GPU Agent Construction Failed (HSA queue will not " + "be intercepted): {} ({})", + agent.id.handle, + error.what()); + } + } + return ROCPROFILER_STATUS_SUCCESS; + }, + sizeof(rocprofiler_agent_t), + this); +} + +QueueController& +get_queue_controller() +{ + static QueueController controller; + return controller; +} + +void +queue_controller_init(HsaApiTable* table) +{ + get_queue_controller().Init(*table->core_, *table->amd_ext_); +} + +} // namespace hsa +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/hsa/queue.hpp b/source/lib/rocprofiler/hsa/queue.hpp new file mode 100644 index 0000000000..82a2fbc6b9 --- /dev/null +++ b/source/lib/rocprofiler/hsa/queue.hpp @@ -0,0 +1,237 @@ +/* Copyright (c) 2022 Advanced Micro Devices, Inc. + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in 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: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 + AUTHORS 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 IN + THE SOFTWARE. */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include "lib/common/synchronized.hpp" +#include "lib/rocprofiler/hsa/agent_cache.hpp" + +namespace rocprofiler +{ +namespace hsa +{ +/** + * Struct containing AQL packet information. Including start/stop/read + * packets along with allocated buffers + */ +struct AQLPacket +{ + hsa_ven_amd_aqlprofile_profile_t profile; + hsa_ext_amd_aql_pm4_packet_t start{.header = 0, + .pm4_command = {0}, + .completion_signal = {.handle = 0}}; + hsa_ext_amd_aql_pm4_packet_t stop{.header = 0, + .pm4_command = {0}, + .completion_signal = {.handle = 0}}; + hsa_ext_amd_aql_pm4_packet_t read{.header = 0, + .pm4_command = {0}, + .completion_signal = {.handle = 0}}; + bool command_buf_mallocd{false}; + bool output_buffer_malloced{false}; + std::function free_func; + AQLPacket(std::function func) + : free_func(std::move(func)) + {} + + ~AQLPacket() + { + if(!command_buf_mallocd) + { + free_func(profile.command_buffer.ptr); + } + else + { + free(profile.command_buffer.ptr); + } + + if(!output_buffer_malloced) + { + free_func(profile.output_buffer.ptr); + } + else + { + free(profile.output_buffer.ptr); + } + } + + // Keep move constuctors (i.e. std::move()) + AQLPacket(AQLPacket&& other) = default; + AQLPacket& operator=(AQLPacket&& other) = default; + + // Do not allow copying this class + AQLPacket(const AQLPacket&) = delete; + AQLPacket& operator=(const AQLPacket&) = delete; +}; + +using ClientID = int64_t; + +// Interceptor for a single specific queue +class Queue +{ +public: + // Internal session information that is used by write interceptor + // to track state of the intercepted kernel. + struct queue_info_session_t + { + Queue& queue; + std::unique_ptr inst_pkt; + ClientID inst_pkt_id; + hsa_ext_amd_aql_pm4_packet_t kernel_pkt; + hsa_signal_t interrupt_signal; + }; + + Queue(const AgentCache& agent, + uint32_t size, + hsa_queue_type32_t type, + void (*callback)(hsa_status_t status, hsa_queue_t* source, void* data), + void* data, + uint32_t private_segment_size, + uint32_t group_segment_size, + CoreApiTable core_api, + AmdExtTable ext_api, + hsa_queue_t** queue); + + const hsa_queue_t* intercept_queue() const { return _intercept_queue; }; + const AgentCache& get_agent() const { return _agent; } + + void create_signal(uint32_t attribute, hsa_signal_t* signal) const; + void signal_async_handler(const hsa_signal_t& signal, Queue::queue_info_session_t* data) const; + + rocprofiler_queue_id_t get_id() const + { + return {.handle = reinterpret_cast(intercept_queue())}; + }; + + template + void signal_callback(Func&& func) const + { + _callbacks.rlock([&func](const auto& data) { func(data); }); + } + + // Fast check to see if we have any callbacks we need to notify + int get_notifiers() const { return _notifiers; } + + // Tracks the number of in flight kernel executions we + // are waiting on. We cannot destroy Queue until all kernels + // have comleted. + void async_started() { _active_async_packets++; } + void async_complete() { _active_async_packets--; } + + ~Queue() + { + // Potentially replace with condition variable at some point + // but performance may not matter here. + while(_active_async_packets > 0) + {} + } + + // Function prototype used to notify consumers that a kernel has been + // enqueued. An AQL packet can be returned that will be injected into + // the queue. + using QueueCB = std::function< + std::unique_ptr(const Queue&, ClientID, const hsa_ext_amd_aql_pm4_packet_t&)>; + // Signals the completion of the kernel packet. + using CompletedCB = std::function)>; + + void register_callback(ClientID id, QueueCB enqueue_cb, CompletedCB complete_cb); + void remove_callback(ClientID id); + + const CoreApiTable& core_api() const { return _core_api; } + const AmdExtTable& ext_api() const { return _ext_api; } + +private: + std::atomic _active_async_packets{0}; + CoreApiTable _core_api; + AmdExtTable _ext_api; + const AgentCache& _agent; + std::atomic _notifiers; + rocprofiler::common::Synchronized>> + _callbacks; + hsa_queue_t* _intercept_queue; +}; + +// Tracks and manages HSA queues +class QueueController +{ +public: + QueueController() = default; + // Initializes the QueueInterceptor. This must be delayed until + // HSA has been inited. + void Init(CoreApiTable& core_table, AmdExtTable& ext_table); + // Called to add a queue that was created by the user program + void add_queue(hsa_queue_t*, std::unique_ptr); + void destory_queue(hsa_queue_t*); + + // Add callback to queues associated with the agent. Returns a client + // id that can be used by callers to remove the callback. + ClientID add_callback(const rocprofiler_agent_t&, Queue::QueueCB, Queue::CompletedCB); + void remove_callback(ClientID); + + const CoreApiTable& get_core_table() const { return _core_table; } + const AmdExtTable& get_ext_table() const { return _ext_table; } + + // Gets the list of supported HSA agents that can be intercepted + const std::unordered_map& get_supported_agents() const + { + return _supported_agents; + } + + std::unordered_map& get_supported_agents() { return _supported_agents; } + +private: + CoreApiTable _core_table; + AmdExtTable _ext_table; + rocprofiler::common::Synchronized>> + _queues; + rocprofiler::common::Synchronized< + std::unordered_map>> + _callback_cache; + + std::unordered_map _supported_agents; +}; + +QueueController& +get_queue_controller(); + +void +queue_controller_init(HsaApiTable* table); + +} // namespace hsa +} // namespace rocprofiler diff --git a/source/lib/rocprofiler/hsa/types.hpp b/source/lib/rocprofiler/hsa/types.hpp index 8b41bd1db7..4768e72e4a 100644 --- a/source/lib/rocprofiler/hsa/types.hpp +++ b/source/lib/rocprofiler/hsa/types.hpp @@ -20,9 +20,10 @@ #pragma once +#include +#include + #include "lib/common/defines.hpp" -#include "rocprofiler/hsa.h" -#include "rocprofiler/version.h" #ifndef ROCPROFILER_UNSAFE_NO_VERSION_CHECK # if defined(ROCPROFILER_CI) && ROCPROFILER_CI > 0 diff --git a/source/lib/rocprofiler/internal_threading.cpp b/source/lib/rocprofiler/internal_threading.cpp index d1d439302f..16df79415e 100644 --- a/source/lib/rocprofiler/internal_threading.cpp +++ b/source/lib/rocprofiler/internal_threading.cpp @@ -25,6 +25,7 @@ #include #include "lib/common/container/stable_vector.hpp" +#include "lib/common/utility.hpp" #include "lib/rocprofiler/buffer.hpp" #include "lib/rocprofiler/context/context.hpp" #include "lib/rocprofiler/internal_threading.hpp" @@ -141,21 +142,25 @@ execute_creation_notifiers(rocprofiler_internal_thread_library_t libs, (execute(get_creation_notifier()), ...); } -auto*& +// using thread_pool_vec_t = std::vector>; +// using task_group_vec_t = std::vector>; + +auto& get_thread_pools() { - // use raw pointers here because of the access to this variable via atexit function call. - // if not a raw pointer, this may be destroyed automatically when the atexit handler is invoked - static auto* _v = new thread_pool_vec_t{}; + static auto _v = thread_pool_vec_t{}; return _v; } -auto*& +auto& get_task_groups() { - // use raw pointers here because of the access to this variable via atexit function call. - // if not a raw pointer, this may be destroyed automatically when the atexit handler is invoked - static auto* _v = new task_group_vec_t{}; + static auto _v = task_group_vec_t([](auto& data) { + for(auto& itr : data) + itr.first->join(); + data.clear(); + }); + return _v; } } // namespace @@ -166,33 +171,21 @@ initialize() { static auto _once = std::once_flag{}; std::call_once(_once, []() { - atexit(®istration::finalize); + // Note: create_callback_thread() must occur before atexit + // registration or else the static objects it is pointing to + // will be destroyed before finalize is invoked. create_callback_thread(); + atexit(®istration::finalize); }); } -// sync all the task groups and destroy the thread pools void finalize() { - if(get_task_groups()) - { - for(auto& itr : *get_task_groups()) - if(itr) itr->join(); - get_task_groups()->clear(); - } - - if(get_thread_pools()) - { - for(auto& itr : *get_thread_pools()) - if(itr) itr->destroy_threadpool(); - get_thread_pools()->clear(); - } - - delete get_task_groups(); - delete get_thread_pools(); - get_task_groups() = nullptr; - get_thread_pools() = nullptr; + // PLT::ThreadPool::f_thread_ids() is not destruction order safe + // if it does become safe, these two calls could be removed. + get_task_groups().destroy(); + get_thread_pools().clear(); } void @@ -210,20 +203,19 @@ notify_post_internal_thread_create(rocprofiler_internal_thread_library_t libs) rocprofiler_callback_thread_t create_callback_thread() { - if(!get_thread_pools()) throw std::runtime_error{"thread pools already deleted"}; - if(!get_task_groups()) throw std::runtime_error{"task groups already deleted"}; - // notify that rocprofiler library is about to create an inernal thread notify_pre_internal_thread_create(ROCPROFILER_LIBRARY); // this will be index after emplace_back - auto idx = get_thread_pools()->size(); + auto idx = get_thread_pools().size(); - auto& thr_pool = - get_thread_pools()->emplace_back(new thread_pool_t{thread_pool_config_t{.pool_size = 1}}); + auto& thr_pool = get_thread_pools().emplace_back(std::make_shared( + std::make_unique(thread_pool_config_t{.pool_size = 1}), + [](auto& tp) { tp->destroy_threadpool(); })); // construct the task group to use the newly created thread pool - get_task_groups()->emplace_back(new task_group_t{thr_pool.get()}); + get_task_groups().get().emplace_back(std::make_unique(thr_pool->get().get()), + thr_pool); // notify that rocprofiler library finished creating an internal thread notify_post_internal_thread_create(ROCPROFILER_LIBRARY); @@ -235,7 +227,9 @@ create_callback_thread() task_group_t* get_task_group(rocprofiler_callback_thread_t cb_tid) { - return (get_task_groups()) ? get_task_groups()->at(cb_tid.handle).get() : nullptr; + return (!get_task_groups().get().empty()) + ? get_task_groups().get().at(cb_tid.handle).first.get() + : nullptr; } } // namespace internal_threading } // namespace rocprofiler @@ -275,8 +269,7 @@ rocprofiler_status_t ROCPROFILER_API rocprofiler_assign_callback_thread(rocprofiler_buffer_id_t buffer_id, rocprofiler_callback_thread_t cb_thread_id) { - if(!rocprofiler::internal_threading::get_task_groups() || - cb_thread_id.handle >= rocprofiler::internal_threading::get_task_groups()->size()) + if(cb_thread_id.handle >= rocprofiler::internal_threading::get_task_groups().get().size()) return ROCPROFILER_STATUS_ERROR_THREAD_NOT_FOUND; for(auto& bitr : rocprofiler::buffer::get_buffers()) diff --git a/source/lib/rocprofiler/internal_threading.hpp b/source/lib/rocprofiler/internal_threading.hpp index deed7294e6..02417444f2 100644 --- a/source/lib/rocprofiler/internal_threading.hpp +++ b/source/lib/rocprofiler/internal_threading.hpp @@ -26,6 +26,7 @@ #include "lib/common/container/stable_vector.hpp" #include "lib/common/defines.hpp" +#include "lib/common/utility.hpp" #include #include @@ -38,10 +39,20 @@ namespace rocprofiler { namespace internal_threading { -using thread_pool_t = PTL::ThreadPool; -using task_group_t = PTL::TaskGroup; -using thread_pool_vec_t = std::vector>; -using task_group_vec_t = std::vector>; +using thread_pool_t = PTL::ThreadPool; +using task_group_t = PTL::TaskGroup; +using thread_pool_cleanup_t = rocprofiler::common::static_cleanup_wrapper< + std::unique_ptr, + std::function&)>>; +using task_group_cleanup_t = + std::pair, std::shared_ptr>; +using thread_pool_vec_t = std::vector>; + +// Note: task_group maintains a shared_ptr copy to thread_pool to ensure it is not destroyed +// before the task can be sync'd. +using task_group_vec_t = rocprofiler::common::static_cleanup_wrapper< + std::vector, + std::function&)>>; void notify_pre_internal_thread_create(rocprofiler_internal_thread_library_t); void notify_post_internal_thread_create(rocprofiler_internal_thread_library_t); diff --git a/source/lib/rocprofiler/profile_config.cpp b/source/lib/rocprofiler/profile_config.cpp new file mode 100644 index 0000000000..8233633431 --- /dev/null +++ b/source/lib/rocprofiler/profile_config.cpp @@ -0,0 +1,64 @@ +#include + +#include "lib/common/synchronized.hpp" +#include "lib/common/utility.hpp" +#include "lib/rocprofiler/aql/helpers.hpp" +#include "lib/rocprofiler/counters/core.hpp" +#include "lib/rocprofiler/counters/evaluate_ast.hpp" +#include "lib/rocprofiler/counters/metrics.hpp" +#include "lib/rocprofiler/hsa/agent_cache.hpp" + +extern "C" { +/** + * @brief Create Profile Configuration. + * + * @param [in] agent Agent identifier + * @param [in] counters_list List of GPU counters + * @param [in] counters_count Size of counters list + * @param [out] config_id Identifier for GPU counters group + * @return ::rocprofiler_status_t + */ +rocprofiler_status_t ROCPROFILER_API +rocprofiler_create_profile_config(rocprofiler_agent_t agent, + rocprofiler_counter_id_t* counters_list, + size_t counters_count, + rocprofiler_profile_config_id_t* config_id) +{ + rocprofiler::counters::profile_config config; + const auto& id_map = rocprofiler::counters::getMetricIdMap(); + + for(size_t i = 0; i < counters_count; i++) + { + auto& counter_id = counters_list[i]; + + const auto* metric_ptr = rocprofiler::common::get_val(id_map, counter_id.handle); + if(!metric_ptr) return ROCPROFILER_STATUS_ERROR_COUNTER_NOT_FOUND; + config.metrics.push_back(*metric_ptr); + + auto agent_name = std::string(agent.name); + auto req_counters = + rocprofiler::counters::get_required_hardware_counters(agent_name, *metric_ptr); + if(!req_counters) return ROCPROFILER_STATUS_ERROR_COUNTER_NOT_FOUND; + config.reqired_hw_counters.insert(req_counters->begin(), req_counters->end()); + + const auto& asts = rocprofiler::counters::get_ast_map(); + const auto* agent_map = rocprofiler::common::get_val(asts, agent_name); + if(!agent_map) return ROCPROFILER_STATUS_ERROR_COUNTER_NOT_FOUND; + const auto* counter_ast = rocprofiler::common::get_val(*agent_map, metric_ptr->name()); + if(!counter_ast) return ROCPROFILER_STATUS_ERROR_COUNTER_NOT_FOUND; + config.asts.push_back(*counter_ast); + } + + config.agent = agent; + config_id->handle = rocprofiler::counters::create_counter_profile(std::move(config)); + + return ROCPROFILER_STATUS_SUCCESS; +} + +rocprofiler_status_t ROCPROFILER_API +rocprofiler_destroy_profile_config(rocprofiler_profile_config_id_t config_id) +{ + rocprofiler::counters::destroy_counter_profile(config_id.handle); + return ROCPROFILER_STATUS_SUCCESS; +} +} diff --git a/source/lib/rocprofiler/registration.cpp b/source/lib/rocprofiler/registration.cpp index b6d59c3ae0..2e926c55d8 100644 --- a/source/lib/rocprofiler/registration.cpp +++ b/source/lib/rocprofiler/registration.cpp @@ -23,6 +23,7 @@ #include "lib/rocprofiler/registration.hpp" #include "lib/rocprofiler/context/context.hpp" #include "lib/rocprofiler/hsa/hsa.hpp" +#include "lib/rocprofiler/hsa/queue.hpp" #include "lib/rocprofiler/internal_threading.hpp" #include @@ -542,6 +543,7 @@ rocprofiler_set_api_table(const char* name, auto& saved_hsa_api_table = rocprofiler::hsa::get_table(); ::copyTables(hsa_api_table, &saved_hsa_api_table); + rocprofiler::hsa::queue_controller_init(hsa_api_table); rocprofiler::hsa::update_table(hsa_api_table); } else if(std::string_view{name} == "roctx") diff --git a/source/lib/rocprofiler/tests/CMakeLists.txt b/source/lib/rocprofiler/tests/CMakeLists.txt index 5bf9263edb..11a967af91 100644 --- a/source/lib/rocprofiler/tests/CMakeLists.txt +++ b/source/lib/rocprofiler/tests/CMakeLists.txt @@ -51,5 +51,5 @@ gtest_add_tests( set_tests_properties( ${shared_lib_TESTS} - PROPERTIES TIMEOUT 45 LABELS "unittests" ENVIRONMENT + PROPERTIES TIMEOUT 360 LABELS "unittests" ENVIRONMENT "HSA_TOOLS_LIB=$") diff --git a/source/lib/rocprofiler/tests/details/agent.cpp b/source/lib/rocprofiler/tests/details/agent.cpp index fb1cf578c7..aaafadcd91 100644 --- a/source/lib/rocprofiler/tests/details/agent.cpp +++ b/source/lib/rocprofiler/tests/details/agent.cpp @@ -18,8 +18,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -#include "agent.hpp" - +#include "lib/rocprofiler/tests/details/agent.hpp" #include "lib/common/utility.hpp" #include diff --git a/source/lib/tests/buffering/CMakeLists.txt b/source/lib/tests/buffering/CMakeLists.txt index 28fcb8054b..54ed7312ad 100644 --- a/source/lib/tests/buffering/CMakeLists.txt +++ b/source/lib/tests/buffering/CMakeLists.txt @@ -20,4 +20,4 @@ gtest_add_tests( TEST_LIST buffering-tests_TESTS WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) -set_tests_properties(${buffering-tests_TESTS} PROPERTIES TIMEOUT 120 LABELS "unittests") +set_tests_properties(${buffering-tests_TESTS} PROPERTIES TIMEOUT 360 LABELS "unittests") diff --git a/source/scripts/patch-parser.cmake b/source/scripts/patch-parser.cmake new file mode 100644 index 0000000000..1068826659 --- /dev/null +++ b/source/scripts/patch-parser.cmake @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.16 FATAL_ERROR) + +foreach(BINARY_OUTPUT ${BINARY_DIR}/parser.h ${BINARY_DIR}/parser.cpp + ${BINARY_DIR}/scanner.cpp) + string(REPLACE "${BINARY_DIR}" "${SOURCE_DIR}" SOURCE_OUTPUT "${BINARY_OUTPUT}") + foreach(VAR PROJECT_SRC_DIR PROJECT_BLD_DIR) + string(REPLACE "/" "_" ${VAR} "${${VAR}}") + string(REPLACE "-" "_" ${VAR} "${${VAR}}") + string(REPLACE "+" "" ${VAR} "${${VAR}}") + string(TOUPPER "${${VAR}}" ${VAR}) + endforeach() + + # remove absolute path from file + if(NOT SOURCE_OUTPUT STREQUAL BINARY_OUTPUT) + file(READ ${BINARY_OUTPUT} OUTPUT_DATA) + string(REPLACE "${SOURCE_DIR}/" "" OUTPUT_DATA "${OUTPUT_DATA}") + string(REPLACE "${BINARY_DIR}/" "" OUTPUT_DATA "${OUTPUT_DATA}") + string(REPLACE "${PROJECT_BLD_DIR}" "_ROCPROFILER" OUTPUT_DATA "${OUTPUT_DATA}") + string(REPLACE "${PROJECT_SRC_DIR}" "_ROCPROFILER" OUTPUT_DATA "${OUTPUT_DATA}") + file(WRITE ${BINARY_OUTPUT} "${OUTPUT_DATA}") + + if(FORMAT_EXE) + execute_process(COMMAND ${FORMAT_EXE} -i ${BINARY_OUTPUT}) + endif() + + configure_file(${BINARY_OUTPUT} ${SOURCE_OUTPUT} COPYONLY) + endif() +endforeach() diff --git a/source/scripts/run-ci.py b/source/scripts/run-ci.py index 838d29ed13..a496260785 100755 --- a/source/scripts/run-ci.py +++ b/source/scripts/run-ci.py @@ -92,6 +92,7 @@ def generate_custom(args, cmake_args, ctest_args): "samples/.*", "tests/.*", ".*/details/.*", + "*/counters/parser/.*", ] if args.coverage == "samples": codecov_exclude += [".*/lib/common/.*"]