From 667002a397f6a514dd19866ac56193d8a019ada5 Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Thu, 18 Nov 2021 10:29:13 +0000 Subject: [PATCH 01/12] Libyaml integration to RVS First draft checkin --- rvs/src/rvsexec_do_yaml_new.cpp | 252 ++++++++++++++++++ yaml-parser/CMakeLists.txt | 8 + yaml-parser/gst_single.conf | 33 +++ yaml-parser/include/gstaction.h | 34 +++ yaml-parser/include/parser.h | 10 + yaml-parser/include/yaml_utils.h | 5 + yaml-parser/src/demo.cpp | 437 +++++++++++++++++++++++++++++++ yaml-parser/src/gstaction.cpp | 26 ++ yaml-parser/src/utils.cpp | 44 ++++ 9 files changed, 849 insertions(+) create mode 100644 rvs/src/rvsexec_do_yaml_new.cpp create mode 100644 yaml-parser/CMakeLists.txt create mode 100644 yaml-parser/gst_single.conf create mode 100644 yaml-parser/include/gstaction.h create mode 100644 yaml-parser/include/parser.h create mode 100644 yaml-parser/include/yaml_utils.h create mode 100644 yaml-parser/src/demo.cpp create mode 100644 yaml-parser/src/gstaction.cpp create mode 100644 yaml-parser/src/utils.cpp diff --git a/rvs/src/rvsexec_do_yaml_new.cpp b/rvs/src/rvsexec_do_yaml_new.cpp new file mode 100644 index 00000000..2b88e0b2 --- /dev/null +++ b/rvs/src/rvsexec_do_yaml_new.cpp @@ -0,0 +1,252 @@ +/******************************************************************************** + * + * Copyright (c) 2018 ROCm Developer Tools + * + * MIT LICENSE: + * 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 +#include +#include +#include + +#include "include/rvsexec.h" +#include "include/yaml_utils.h" + +#include "include/rvsif0.h" +#include "include/rvsif1.h" +#include "include/rvsaction.h" +#include "include/rvsmodule.h" +#include "include/rvsliblogger.h" +#include "include/rvsoptions.h" +#include "include/rvs_util.h" + +#define MODULE_NAME_CAPS "CLI" + +/*** Example rvs.conf file structure + +actions: +- name: action_1 + device: all + module: gpup + properties: + mem_banks_count: + io_links-properties: + version_major: +- name: action_2 + module: gpup + device: all + +***/ + + +using std::string; + +/** + * @brief Executes actions listed in .conf file. + * + * @return 0 if successful, non-zero otherwise + * + */ +int rvs::exec::do_yaml(const std::string& config_file) { + int sts = 0; + + std::string rvsmodule = rocyaml::getModule(config_file); + std::cout <<"MANOJ: modname is " << rvsmodule; + // find "actions" map + std::string rvsmodule; + // for all actions... + for (YAML::const_iterator it = actions.begin(); it != actions.end(); ++it) { + const YAML::Node& action = *it; + + rvs::logger::log("Action name :" + action["name"].as(), rvs::logresults); + + // if stop was requested + if (rvs::logger::Stopping()) { + return -1; + } + + // find module name + std::string rvsmodule; + try { + rvsmodule = action["module"].as(); + } catch(...) { + } + + // not found or empty + if (rvsmodule == "") { + // report error and go to next action + char buff[1024]; + snprintf(buff, sizeof(buff), "action '%s' does not specify module.", + action["name"].as().c_str()); + rvs::logger::Err(buff, MODULE_NAME_CAPS); + return -1; + } + + // create action excutor in .so + rvs::action* pa = module::action_create(rvsmodule.c_str()); + if (!pa) { + char buff[1024]; + snprintf(buff, sizeof(buff), + "action '%s' could not crate action object in module '%s'", + action["name"].as().c_str(), + rvsmodule.c_str()); + rvs::logger::Err(buff, MODULE_NAME_CAPS); + return -1; + } + + if1* pif1 = dynamic_cast(pa->get_interface(1)); + if (!pif1) { + char buff[1024]; + snprintf(buff, sizeof(buff), + "action '%s' could not obtain interface if1", + action["name"].as().c_str()); + module::action_destroy(pa); + return -1; + } + + // load action properties from yaml file + sts += do_yaml_properties(action, rvsmodule, pif1); + if (sts) { + module::action_destroy(pa); + return sts; + } + + // set also command line options: + for (auto clit = rvs::options::get().begin(); + clit != rvs::options::get().end(); ++clit) { + std::string p(clit->first); + p = "cli." + p; + pif1->property_set(p, clit->second); + } + + // execute action + sts = pif1->run(); + + // processing finished, release action object + module::action_destroy(pa); + + // errors? + if (sts) { + // cancel actions and return + return sts; + } + } + + return 0; +} + +/** + * @brief Loads action properties. + * + * @return 0 if successful, non-zero otherwise + * + */ +int rvs::exec::do_yaml_properties(const YAML::Node& node, + const std::string& module_name, + rvs::if1* pif1) { + int sts = 0; + + string indexes; + bool indexes_provided = false; + if (rvs::options::has_option("-i", &indexes) && (!indexes.empty())) + indexes_provided = true; + + rvs::logger::log("Module name :" + module_name, rvs::logresults); + + // for all child nodes + for (YAML::const_iterator it = node.begin(); it != node.end(); it++) { + // if property is collection of module specific properties, + if (is_yaml_properties_collection(module_name, + it->first.as())) { + // pass properties collection to .so action object + sts += do_yaml_properties_collection(it->second, + it->first.as(), + pif1); + } else { + // just set this one propertiy + if (indexes_provided && it->first.as() == "device") { + std::replace(indexes.begin(), indexes.end(), ',', ' '); + sts += pif1->property_set("device", indexes); + } else { + sts += pif1->property_set(it->first.as(), + it->second.as()); + } + } + } + + return sts; +} + +/** + * @brief Loads property collection for collection type node in .conf file. + * + * @return 0 if successful, non-zero otherwise + * + */ +int rvs::exec::do_yaml_properties_collection(const YAML::Node& node, + const std::string& parent_name, + if1* pif1) { + int sts = 0; + + // for all child nodes + for (YAML::const_iterator it = node.begin(); it != node.end(); it++) { + // prepend dot separated parent name and pass property to module + sts += pif1->property_set(parent_name + "." + it->first.as(), + it->second.IsNull() ? std::string("") : it->second.as()); + } + + return sts; +} + +/** + * @brief Checks if property is collection type property in .conf file. + * + * @param module_name module name + * @param property_name property name + * @return 'true' if property is collection, 'false' otherwise + * + */ +bool rvs::exec::is_yaml_properties_collection( + const std::string& module_name, + const std::string& property_name) { + + if (module_name == "gpup") { + if (property_name == "properties") + return true; + + if (property_name == "io_links-properties") + return true; + } else { + if (module_name == "peqt") { + if (property_name == "capability") { + return true; + } + } else { + if (module_name == "gm") { + if (property_name == "metrics") + return true; + } + } + } + + return false; +} + diff --git a/yaml-parser/CMakeLists.txt b/yaml-parser/CMakeLists.txt new file mode 100644 index 00000000..090a12d7 --- /dev/null +++ b/yaml-parser/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required (VERSION 3.5.0) +project (HELLO) +set(CMAKE_BUILD_TYPE Debug) +#find_package(libyaml REQUIRED) +add_executable (helloDemo demo.cpp gstaction.cpp) +#include_directories(${YAML_CPP_INCLUDE_DIRS}) +target_link_libraries(helloDemo yaml) + diff --git a/yaml-parser/gst_single.conf b/yaml-parser/gst_single.conf new file mode 100644 index 00000000..e9a0b364 --- /dev/null +++ b/yaml-parser/gst_single.conf @@ -0,0 +1,33 @@ + + +# GST test +# +# Preconditions: +# Set device to all. If you need to run the rvs only on a subset of GPUs, please run rvs with -g +# option, collect the GPUs IDs (e.g.: GPU[ 5 - 50599] -> 50599 is the GPU ID) and then specify +# all the GPUs IDs separated by white space +# Set parallel execution to false +# Set matrix_size to 8640 (for Vega 10 cards). For Vega 20, the recommended matrix_size is 8640 +# Set run count to 2 (each test will run twice) +# Set copy_matrix to false (the matrices will be copied to GPUs only once) +# +# Run test with: +# cd bin +# sudo ./rvs -c conf/gst_1.conf -d 3 +# +# Expected result: +# The test on each GPU passes (TRUE) if the GPU achieves 5000 gflops +# in maximum 7 seconds and then the GPU sustains the gflops +# for the rest of the test duration (total duration is 18 seconds). +# A single Gflops violation (with a 7% tolerance) is allowed. +# FALSE otherwise + +actions: +- name: gpustress-9000-sgemm-false + count: 1 + duration: 10000 + target_stress: 9000 + module: gst + matrix_size_a: 8640 + matrix_size_b: 8640 + matrix_size_c: 8640 diff --git a/yaml-parser/include/gstaction.h b/yaml-parser/include/gstaction.h new file mode 100644 index 00000000..6b6189b7 --- /dev/null +++ b/yaml-parser/include/gstaction.h @@ -0,0 +1,34 @@ +/* + * Example application data structures. + */ +#ifndef GST_ACTION_H +#define GST_ACTION_H +#include +#include + +struct gst_action{ + gst_action *next; + std::string m_name{}; + std::string m_module_name{}; + std::string m_devices{}; + std::string m_ops{}; + float m_target_stress; + int m_count; + int m_duration; + int m_size_a; + int m_size_b; + int m_size_c; + int m_log_interval; + bool m_parallel; + bool m_copy_matrix; +}; + + +void add_action(std::vector &actions, std::string name, + std::string module_name, int count, + std::string ops, float target_stress, int duration, + int size_a, int size_b, int size_c, int log_interval, + bool parallel, bool copy_matrix); + +void destroy_actions(std::vector &actions); +#endif diff --git a/yaml-parser/include/parser.h b/yaml-parser/include/parser.h new file mode 100644 index 00000000..388282b8 --- /dev/null +++ b/yaml-parser/include/parser.h @@ -0,0 +1,10 @@ +#ifndef PARSER_H +#define PARSER_H + +class Parser{ +public: + Parse(const std::string& config_file); + void parse() + virtual ~Parser() = default; +}; +#endif diff --git a/yaml-parser/include/yaml_utils.h b/yaml-parser/include/yaml_utils.h new file mode 100644 index 00000000..7062d57b --- /dev/null +++ b/yaml-parser/include/yaml_utils.h @@ -0,0 +1,5 @@ +#ifndef UTILS_H +#define UTILS_H +std::string getModuleName(std::string input); +std::string getModule(std::string filename); +#endif diff --git a/yaml-parser/src/demo.cpp b/yaml-parser/src/demo.cpp new file mode 100644 index 00000000..2e3a9b50 --- /dev/null +++ b/yaml-parser/src/demo.cpp @@ -0,0 +1,437 @@ +#include +#include +#include +#include +#include +#include +#include "gstaction.h" +#include +/* Set environment variable DEBUG=1 to enable debug output. */ +int debug = 0; + +/* yaml_* functions return 1 on success and 0 on failure. */ +enum status { + SUCCESS = 1, + FAILURE = 0 +}; + +/* Our example parser states. */ +const std::string ACTIONS{"actions"}; +const std::string NAME{"name"}; +const std::string PARALLEL{"parallel"}; +const std::string DEVICES{"device"}; +const std::string MODULENAME{"module"}; +const std::string TARGET{"target_stress"}; +const std::string COPY_MATRIX{"copy_matrix"}; +const std::string MATRIX_SIZE_A{"matrix_size_a"}; +const std::string MATRIX_SIZE_B{"matrix_size_b"}; +const std::string MATRIX_SIZE_C{"matrix_size_c"}; +const std::string COUNT{"count"}; +const std::string DURATION{"duration"}; +const std::string OPS_TYPE{"ops_type"}; +const std::string LOG_INTERVAL{"log_interval"}; + + +enum parse_state { + STATE_START, /* start state */ + STATE_STREAM, /* start/end stream */ + STATE_DOCUMENT, /* start/end document */ + STATE_SECTION, /* top level */ + + STATE_ACTIONLIST, /* action list */ + STATE_ACTIONVALUES, /* action key-value pairs */ + STATE_ACTIONKEY, /* action key */ + STATE_ACTIONNAME, /* action name value */ + STATE_DEVICES, /* devices value */ + STATE_MODULENAME, /* module name value */ + STATE_PARALLEL, /* module name value */ + + STATE_COUNT, + STATE_DURATION, + STATE_COPY_MATRIX, + STATE_TARGET_STRESS, + STATE_SIZE_A, + STATE_SIZE_B, + STATE_SIZE_C, + STATE_OPS, + STATE_LOGINTERVAL, + STATE_STOP /* end state */ +}; + +/* Our application parser state data. */ +struct parser_state { + parse_state state; /* The current parse state */ + gst_action f; /* data elements. */ + std::vector actionlist; /* List of action objects. */ +}; + + + +/* + * Consume yaml events generated by the libyaml parser to + * import our data into raw c data structures. + */ +int consume_event(struct parser_state *&s, yaml_event_t *event) +{ + char *value; + std::string temp; + gst_action f; + if (debug) { + printf("state=%d event=%d\n", s->state, event->type); + } + switch (s->state) { + case STATE_START: + switch (event->type) { + case YAML_STREAM_START_EVENT: + s->state = STATE_STREAM; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_STREAM: + switch (event->type) { + case YAML_DOCUMENT_START_EVENT: + s->state = STATE_DOCUMENT; + break; + case YAML_STREAM_END_EVENT: + s->state = STATE_STOP; /* All done. */ + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_DOCUMENT: + switch (event->type) { + case YAML_MAPPING_START_EVENT: + s->state = STATE_SECTION; + break; + case YAML_DOCUMENT_END_EVENT: + s->state = STATE_STREAM; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_SECTION: + switch (event->type) { + case YAML_SCALAR_EVENT: + value = (char *)event->data.scalar.value; + if (strcmp(value, ACTIONS.c_str()) == 0) { + s->state = STATE_ACTIONLIST; + } else { + fprintf(stderr, "Unexpected scalar: %s\n", value); + return FAILURE; + } + break; + case YAML_DOCUMENT_END_EVENT: + s->state = STATE_STREAM; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTIONLIST: + switch (event->type) { + case YAML_SEQUENCE_START_EVENT: + s->state = STATE_ACTIONVALUES; + break; + case YAML_MAPPING_END_EVENT: + s->state = STATE_SECTION; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTIONVALUES: + switch (event->type) { + case YAML_MAPPING_START_EVENT: + s->state = STATE_ACTIONKEY; + break; + case YAML_SEQUENCE_END_EVENT: + s->state = STATE_ACTIONLIST; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTIONKEY: + switch (event->type) { + case YAML_SCALAR_EVENT: + value = (char *)event->data.scalar.value; + if (strcmp(value, NAME.c_str()) == 0) { + s->state = STATE_ACTIONNAME; + } else if (strcmp(value, PARALLEL.c_str()) == 0) { + s->state = STATE_PARALLEL; + } else if (strcmp(value, COUNT.c_str()) == 0) { + s->state = STATE_COUNT; + } else if (strcmp(value, TARGET.c_str())== 0) { + s->state = STATE_TARGET_STRESS; + } else if (strcmp(value, COPY_MATRIX.c_str())== 0) { + s->state = STATE_COPY_MATRIX; + } else if (strcmp(value, MATRIX_SIZE_A.c_str())== 0) { + s->state = STATE_SIZE_A; + } else if (strcmp(value, MATRIX_SIZE_B.c_str())== 0) { + s->state = STATE_SIZE_B; + } else if (strcmp(value, MATRIX_SIZE_C.c_str())== 0) { + s->state = STATE_SIZE_C; + } else if (strcmp(value, OPS_TYPE.c_str())== 0) { + s->state = STATE_OPS; + } else if (strcmp(value, DEVICES.c_str())== 0) { + s->state = STATE_DEVICES; + } else if (strcmp(value, MODULENAME.c_str())== 0) { + s->state = STATE_MODULENAME; + } else if (strcmp(value, DURATION.c_str())== 0) { + s->state = STATE_DURATION; + } else if (strcmp(value, LOG_INTERVAL.c_str())== 0) { + s->state = STATE_LOGINTERVAL; + } else { + fprintf(stderr, "Unexpected key: %s\n", value); + return FAILURE; + } + break; + + case YAML_MAPPING_END_EVENT: + add_action(s->actionlist, s->f.m_name, s->f.m_module_name,s->f.m_count, s->f.m_ops, + s->f.m_target_stress, s->f.m_duration, s->f.m_size_a, + s->f.m_size_b, s->f.m_size_c, s->f.m_log_interval, + s->f.m_parallel, s->f.m_copy_matrix); + s->state = STATE_ACTIONVALUES; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTIONNAME: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = (char *)event->data.scalar.value; + s->f.m_name = temp; + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_MODULENAME: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = (char *)event->data.scalar.value; + s->f.m_module_name = temp; + temp.clear(); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_DEVICES: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_devices = "okay"; + s->f.m_devices = (char *)event->data.scalar.value; + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_COUNT: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_count = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_DURATION: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_duration = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_COPY_MATRIX: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = std::string{(char *)event->data.scalar.value}; + s->f.m_copy_matrix = (temp == "true") ? true : false; + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_PARALLEL: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = std::string{(char *)event->data.scalar.value}; + s->f.m_parallel = (temp == "true") ? true : false; + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_SIZE_A: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_size_a = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_SIZE_B: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_size_b = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_SIZE_C: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_size_c = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_LOGINTERVAL: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_log_interval = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_TARGET_STRESS: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_target_stress = atof((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_OPS: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_ops = std::string{(char *)event->data.scalar.value}; + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_STOP: + break; + } + return SUCCESS; +} + +int +main(int argc, char *argv[]) +{ + int code; + int status_; + //struct parser_state state; + parser_state *state = new parser_state{}; + yaml_parser_t parser; + yaml_event_t event; + + if (getenv("DEBUG")) { + debug = 1; + } + FILE *fd = fopen("../gst_single.conf", "r"); + if(!fd){ + std::cout << "Could not open file " << std::endl; + return -1; + } + //memset(&state, 0, sizeof(state)); + state->state = STATE_START; + yaml_parser_initialize(&parser); + yaml_parser_set_input_file(&parser, fd); + do { + status_ = yaml_parser_parse(&parser, &event); + if (status_ == FAILURE) { + fprintf(stderr, "yaml_parser_parse error\n"); + code = EXIT_FAILURE; + goto done; + } + status_ = consume_event(state, &event); + if (status_ == FAILURE) { + fprintf(stderr, "consume_event error\n"); + code = EXIT_FAILURE; + goto done; + } + yaml_event_delete(&event); + } while (state->state != STATE_STOP); + + /* Output the parsed data. */ + for (auto f : state->actionlist ) { + printf("action: name=%s,count=%d, modname=%s\n", f.m_name.c_str(), + f.m_count, f.m_module_name.c_str()); + } + code = EXIT_SUCCESS; + +done: + destroy_actions(state->actionlist); + yaml_parser_delete(&parser); + delete state; + return code; +} diff --git a/yaml-parser/src/gstaction.cpp b/yaml-parser/src/gstaction.cpp new file mode 100644 index 00000000..8d73c6ab --- /dev/null +++ b/yaml-parser/src/gstaction.cpp @@ -0,0 +1,26 @@ +#include +#include "gstaction.h" + +void add_action(std::vector &actions, std::string name, + std::string module_name, int count, + std::string ops, float target_stress, int duration, + int size_a, int size_b, int size_c, int log_interval, + bool parallel, bool copy_matrix){ + gst_action f; + f.m_name = name; + f.m_module_name = module_name; + f.m_count = count; + f.m_ops = ops; + f.m_target_stress = target_stress; + f.m_duration = duration; + f.m_size_a = size_a; + f.m_size_b = size_b; + f.m_size_c = size_c; + f.m_log_interval = log_interval; + f.m_parallel = parallel; + f.m_copy_matrix = copy_matrix; + actions.push_back(f); +} + +void destroy_actions(std::vector &actions){ +} diff --git a/yaml-parser/src/utils.cpp b/yaml-parser/src/utils.cpp new file mode 100644 index 00000000..8d05fef1 --- /dev/null +++ b/yaml-parser/src/utils.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +std::string getModuleName(std::string input){ + std::stringstream is{input}; + std::string temp, last; + while (std::getline(is, temp, ' ')) { + last=temp; + } + return last; +} +std::string getModule(std::string filename){ + static int actions = 0; + std::string module_name{}; + std::ifstream f{filename}; + assert (f.good()); + std::string line; + while(std::getline(f, line)){ + if(line.empty()) + continue; + line = line.substr(line.find_first_not_of(" ")); + if(line[0] == '#') + continue; + if(line.find("name:") != std::string::npos){ + ++actions; + if(actions > 1){ + // should have found module by now, invalid conf. + std::cout <<" Invalid conf file, missing module name " << std::endl; + return module_name; + } else{ + continue; + } + } + if (line.find("module:") != std::string::npos){ + return getModuleName(line); + } else{ + continue; + } + } + return module_name; +} From 6578985536dddd8be1d3695dae954a32a0e81c64 Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Tue, 14 Dec 2021 10:15:52 +0000 Subject: [PATCH 02/12] Cleanup conf reading Making conf data structure generic to avoid code repeat --- yaml-parser/CMakeLists.txt | 5 +- yaml-parser/src/demo.cpp | 10 +- yaml-parser/src/gstaction.cpp | 12 +- yaml-parser/src/node_yaml.cpp | 436 ++++++++++++++++++++++++++++++++++ yaml-parser/src/utils.cpp | 36 +-- 5 files changed, 468 insertions(+), 31 deletions(-) create mode 100644 yaml-parser/src/node_yaml.cpp diff --git a/yaml-parser/CMakeLists.txt b/yaml-parser/CMakeLists.txt index 090a12d7..57b70799 100644 --- a/yaml-parser/CMakeLists.txt +++ b/yaml-parser/CMakeLists.txt @@ -1,8 +1,9 @@ cmake_minimum_required (VERSION 3.5.0) project (HELLO) +set(CMAKE_CXX_STANDARD 14) set(CMAKE_BUILD_TYPE Debug) #find_package(libyaml REQUIRED) -add_executable (helloDemo demo.cpp gstaction.cpp) -#include_directories(${YAML_CPP_INCLUDE_DIRS}) +add_executable (helloDemo src/node_yaml.cpp src/gstaction.cpp) +include_directories(include) target_link_libraries(helloDemo yaml) diff --git a/yaml-parser/src/demo.cpp b/yaml-parser/src/demo.cpp index 2e3a9b50..bde765d7 100644 --- a/yaml-parser/src/demo.cpp +++ b/yaml-parser/src/demo.cpp @@ -205,9 +205,9 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) case YAML_MAPPING_END_EVENT: add_action(s->actionlist, s->f.m_name, s->f.m_module_name,s->f.m_count, s->f.m_ops, - s->f.m_target_stress, s->f.m_duration, s->f.m_size_a, - s->f.m_size_b, s->f.m_size_c, s->f.m_log_interval, - s->f.m_parallel, s->f.m_copy_matrix); + s->f.m_target_stress, s->f.m_duration, s->f.m_size_a, + s->f.m_size_b, s->f.m_size_c, s->f.m_log_interval, + s->f.m_parallel, s->f.m_copy_matrix); s->state = STATE_ACTIONVALUES; break; default: @@ -246,7 +246,7 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) case STATE_DEVICES: switch (event->type) { case YAML_SCALAR_EVENT: - s->f.m_devices = "okay"; + //s->f.m_devices = "okay"; s->f.m_devices = (char *)event->data.scalar.value; s->state = STATE_ACTIONKEY; break; @@ -283,7 +283,7 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) case STATE_COPY_MATRIX: switch (event->type) { case YAML_SCALAR_EVENT: - temp = std::string{(char *)event->data.scalar.value}; + temp = std::string{(char *)event->data.scalar.value}; s->f.m_copy_matrix = (temp == "true") ? true : false; s->state = STATE_ACTIONKEY; break; diff --git a/yaml-parser/src/gstaction.cpp b/yaml-parser/src/gstaction.cpp index 8d73c6ab..48bd0074 100644 --- a/yaml-parser/src/gstaction.cpp +++ b/yaml-parser/src/gstaction.cpp @@ -9,17 +9,17 @@ void add_action(std::vector &actions, std::string name, gst_action f; f.m_name = name; f.m_module_name = module_name; - f.m_count = count; - f.m_ops = ops; - f.m_target_stress = target_stress; - f.m_duration = duration; + f.m_count = count; + f.m_ops = ops; + f.m_target_stress = target_stress; + f.m_duration = duration; f.m_size_a = size_a; f.m_size_b = size_b; f.m_size_c = size_c; f.m_log_interval = log_interval; f.m_parallel = parallel; - f.m_copy_matrix = copy_matrix; - actions.push_back(f); + f.m_copy_matrix = copy_matrix; + actions.push_back(f); } void destroy_actions(std::vector &actions){ diff --git a/yaml-parser/src/node_yaml.cpp b/yaml-parser/src/node_yaml.cpp new file mode 100644 index 00000000..f6377e1f --- /dev/null +++ b/yaml-parser/src/node_yaml.cpp @@ -0,0 +1,436 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "gstaction.h" +/* Set environment variable DEBUG=1 to enable debug output. */ +int debug = 0; + +/* yaml_* functions return 1 on success and 0 on failure. */ +enum status { + SUCCESS = 1, + FAILURE = 0 +}; + +/* Our example parser states. */ +const std::string ACTIONS{"actions"}; +const std::string NAME{"name"}; +const std::string PARALLEL{"parallel"}; +const std::string DEVICES{"device"}; +const std::string MODULENAME{"module"}; +const std::string TARGET{"target_stress"}; +const std::string COPY_MATRIX{"copy_matrix"}; +const std::string MATRIX_SIZE_A{"matrix_size_a"}; +const std::string MATRIX_SIZE_B{"matrix_size_b"}; +const std::string MATRIX_SIZE_C{"matrix_size_c"}; +const std::string COUNT{"count"}; +const std::string DURATION{"duration"}; +const std::string OPS_TYPE{"ops_type"}; +const std::string LOG_INTERVAL{"log_interval"}; + + +enum parse_state { + STATE_START, /* start state */ + STATE_STREAM, /* start/end stream */ + STATE_DOCUMENT, /* start/end document */ + STATE_SECTION, /* top level */ + + STATE_ACTIONLIST, /* action list */ + STATE_ACTIONVALUES, /* action key-value pairs */ + STATE_ACTIONKEY, /* action key */ + STATE_ACTIONNAME, /* action name value */ + STATE_DEVICES, /* devices value */ + STATE_MODULENAME, /* module name value */ + STATE_ACTION_VALUE, /* all values encompassed as one*/ + STATE_PARALLEL, /* module name value */ + + STATE_COUNT, + STATE_DURATION, + STATE_COPY_MATRIX, + STATE_TARGET_STRESS, + STATE_SIZE_A, + STATE_SIZE_B, + STATE_SIZE_C, + STATE_OPS, + STATE_LOGINTERVAL, + STATE_STOP /* end state */ +}; + +using ActionMap = std::map; + +using ActionList = std::vector ; + +using Actions = std::map ; + +/* Our application parser state data. */ +struct parser_state { + parse_state state; /* The current parse state */ + ActionMap f; /* data elements. */ + ActionList actionlist; /* List of action objects. */ + std::string keyname; +}; + + + +/* + * Consume yaml events generated by the libyaml parser to + * import our data into raw c data structures. + */ +int consume_event(struct parser_state *&s, yaml_event_t *event) +{ + char *value; + std::string temp; + std::string key; + ActionMap f; + if (debug) { + printf("state=%d event=%d\n", s->state, event->type); + } + switch (s->state) { + case STATE_START: + switch (event->type) { + case YAML_STREAM_START_EVENT: + s->state = STATE_STREAM; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_STREAM: + switch (event->type) { + case YAML_DOCUMENT_START_EVENT: + s->state = STATE_DOCUMENT; + break; + case YAML_STREAM_END_EVENT: + s->state = STATE_STOP; /* All done. */ + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_DOCUMENT: + switch (event->type) { + case YAML_MAPPING_START_EVENT: + s->state = STATE_SECTION; + break; + case YAML_DOCUMENT_END_EVENT: + s->state = STATE_STREAM; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_SECTION: + switch (event->type) { + case YAML_SCALAR_EVENT: + value = (char *)event->data.scalar.value; + if (strcmp(value, ACTIONS.c_str()) == 0) { + s->state = STATE_ACTIONLIST; + } else { + fprintf(stderr, "Unexpected scalar: %s\n", value); + return FAILURE; + } + break; + case YAML_DOCUMENT_END_EVENT: + s->state = STATE_STREAM; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTIONLIST: + switch (event->type) { + case YAML_SEQUENCE_START_EVENT: + s->state = STATE_ACTIONVALUES; + break; + case YAML_MAPPING_END_EVENT: + s->state = STATE_SECTION; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTIONVALUES: + switch (event->type) { + case YAML_MAPPING_START_EVENT: + s->state = STATE_ACTIONKEY; + break; + case YAML_SEQUENCE_END_EVENT: + s->state = STATE_ACTIONLIST; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTIONKEY: + switch (event->type) { + case YAML_SCALAR_EVENT: + // make a key and save state as value state + key = (char *)event->data.scalar.value; + s->keyname = key; + //value = (char *)event->data.scalar.value; + s->state = STATE_ACTION_VALUE; + break; + + case YAML_MAPPING_END_EVENT: + s->actionlist.push_back( s->f); + s->f.clear(); + s->state = STATE_ACTIONVALUES; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTION_VALUE: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = (char *)event->data.scalar.value; + if(s->keyname.empty()){ + std::cout << "cant have empty key " << std::endl; + return FAILURE; + } + s->f.emplace(s->keyname, std::string(temp)); + s->keyname.clear(); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; +/* + case STATE_ACTIONNAME: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = (char *)event->data.scalar.value; + s->f.m_name = temp; + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_MODULENAME: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = (char *)event->data.scalar.value; + s->f.m_module_name = temp; + temp.clear(); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_DEVICES: + switch (event->type) { + case YAML_SCALAR_EVENT: + //s->f.m_devices = "okay"; + s->f.m_devices = (char *)event->data.scalar.value; + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_COUNT: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_count = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_DURATION: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_duration = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_COPY_MATRIX: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = std::string{(char *)event->data.scalar.value}; + s->f.m_copy_matrix = (temp == "true") ? true : false; + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_PARALLEL: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = std::string{(char *)event->data.scalar.value}; + s->f.m_parallel = (temp == "true") ? true : false; + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_SIZE_A: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_size_a = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_SIZE_B: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_size_b = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_SIZE_C: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_size_c = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_LOGINTERVAL: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_log_interval = atoi((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_TARGET_STRESS: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_target_stress = atof((char *)event->data.scalar.value); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_OPS: + switch (event->type) { + case YAML_SCALAR_EVENT: + s->f.m_ops = std::string{(char *)event->data.scalar.value}; + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; +*/ + case STATE_STOP: + break; + } + return SUCCESS; +} + +int +main(int argc, char *argv[]) +{ + int code; + int status_; + //struct parser_state state; + parser_state *state = new parser_state{}; + yaml_parser_t parser; + yaml_event_t event; + + if (getenv("DEBUG")) { + debug = 1; + } + FILE *fd = fopen("../gst_single.conf", "r"); + if(!fd){ + std::cout << "Could not open file " << std::endl; + return -1; + } + //memset(&state, 0, sizeof(state)); + state->state = STATE_START; + yaml_parser_initialize(&parser); + yaml_parser_set_input_file(&parser, fd); + do { + status_ = yaml_parser_parse(&parser, &event); + if (status_ == FAILURE) { + fprintf(stderr, "yaml_parser_parse error\n"); + code = EXIT_FAILURE; + goto done; + } + status_ = consume_event(state, &event); + if (status_ == FAILURE) { + fprintf(stderr, "consume_event error\n"); + code = EXIT_FAILURE; + goto done; + } + yaml_event_delete(&event); + } while (state->state != STATE_STOP); + + /* Output the parsed data. */ + for (auto f : state->actionlist ) { + std::cout << f["name"]<< std::endl; + } + code = EXIT_SUCCESS; + +done: + //destroy_actions(state->actionlist); + yaml_parser_delete(&parser); + delete state; + return code; +} diff --git a/yaml-parser/src/utils.cpp b/yaml-parser/src/utils.cpp index 8d05fef1..c736be8d 100644 --- a/yaml-parser/src/utils.cpp +++ b/yaml-parser/src/utils.cpp @@ -19,26 +19,26 @@ std::string getModule(std::string filename){ assert (f.good()); std::string line; while(std::getline(f, line)){ - if(line.empty()) - continue; - line = line.substr(line.find_first_not_of(" ")); - if(line[0] == '#') - continue; - if(line.find("name:") != std::string::npos){ - ++actions; - if(actions > 1){ - // should have found module by now, invalid conf. - std::cout <<" Invalid conf file, missing module name " << std::endl; - return module_name; - } else{ - continue; - } - } - if (line.find("module:") != std::string::npos){ - return getModuleName(line); + if(line.empty()) + continue; + line = line.substr(line.find_first_not_of(" ")); + if(line[0] == '#') + continue; + if(line.find("name:") != std::string::npos){ + ++actions; + if(actions > 1){ + // should have found module by now, invalid conf. + std::cout <<" Invalid conf file, missing module name " << std::endl; + return module_name; } else{ - continue; + continue; } + } + if (line.find("module:") != std::string::npos){ + return getModuleName(line); + } else{ + continue; + } } return module_name; } From 0d05ef90a07ee8db491634e8d41eb19965bf3aea Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Tue, 14 Dec 2021 10:34:25 +0000 Subject: [PATCH 03/12] Name cleanup --- yaml-parser/CMakeLists.txt | 2 +- yaml-parser/src/{utils.cpp => yaml_utils.cpp} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename yaml-parser/src/{utils.cpp => yaml_utils.cpp} (98%) diff --git a/yaml-parser/CMakeLists.txt b/yaml-parser/CMakeLists.txt index 57b70799..8d24bf3e 100644 --- a/yaml-parser/CMakeLists.txt +++ b/yaml-parser/CMakeLists.txt @@ -3,7 +3,7 @@ project (HELLO) set(CMAKE_CXX_STANDARD 14) set(CMAKE_BUILD_TYPE Debug) #find_package(libyaml REQUIRED) -add_executable (helloDemo src/node_yaml.cpp src/gstaction.cpp) +add_executable (helloDemo src/node_yaml.cpp src/yaml_utils.cpp) include_directories(include) target_link_libraries(helloDemo yaml) diff --git a/yaml-parser/src/utils.cpp b/yaml-parser/src/yaml_utils.cpp similarity index 98% rename from yaml-parser/src/utils.cpp rename to yaml-parser/src/yaml_utils.cpp index c736be8d..b262728f 100644 --- a/yaml-parser/src/utils.cpp +++ b/yaml-parser/src/yaml_utils.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include std::string getModuleName(std::string input){ std::stringstream is{input}; std::string temp, last; From 62a8189dd94bf77faa66ea7c36a957c7d7d8618d Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Tue, 14 Dec 2021 11:15:55 +0000 Subject: [PATCH 04/12] yaml changes to rvs folder --- rvs/CMakeLists.txt | 2 + rvs/include/node_yaml.h | 39 ++++++++ rvs/include/yaml_utils.h | 5 + rvs/src/node_yaml.cpp | 208 +++++++++++++++++++++++++++++++++++++++ rvs/src/yaml_utils.cpp | 44 +++++++++ 5 files changed, 298 insertions(+) create mode 100644 rvs/include/node_yaml.h create mode 100644 rvs/include/yaml_utils.h create mode 100644 rvs/src/node_yaml.cpp create mode 100644 rvs/src/yaml_utils.cpp diff --git a/rvs/CMakeLists.txt b/rvs/CMakeLists.txt index 1dd221c8..7ee83b99 100644 --- a/rvs/CMakeLists.txt +++ b/rvs/CMakeLists.txt @@ -138,6 +138,8 @@ set(SOURCES src/rvsexec.cpp src/rvsexec_do_yaml.cpp src/rvsoptions.cpp + src/yaml_utils.cpp + src/node_yaml.cpp ) ## define helper lib diff --git a/rvs/include/node_yaml.h b/rvs/include/node_yaml.h new file mode 100644 index 00000000..336a8d41 --- /dev/null +++ b/rvs/include/node_yaml.h @@ -0,0 +1,39 @@ +#ifndef NODE_YAML_H +#define NODE_YAML_H +enum status { + SUCCESS = 1, + FAILURE = 0 +}; + +/* Our example parser states. */ +const std::string ACTIONS{"actions"}; +enum parse_state { + STATE_START, /* start state */ + STATE_STREAM, /* start/end stream */ + STATE_DOCUMENT, /* start/end document */ + STATE_SECTION, /* top level */ + + STATE_ACTIONLIST, /* action list */ + STATE_ACTIONVALUES, /* action key-value pairs */ + STATE_ACTIONKEY, /* action key */ + STATE_ACTIONNAME, /* action name value */ + STATE_ACTION_VALUE, /* all values encompassed as one*/ + STATE_STOP /* end state */ +}; + +//alias definitions +using ActionMap = std::map ; +using ActionList = std::vector ; +using Actions = std::map ; + +/* Our application parser state data. */ +struct parser_state { + parse_state state; /* The current parse state */ + ActionMap f; /* data elements. */ + ActionList actionlist; /* List of action objects. */ + std::string keyname; /* to retain key name from previous state */ +}; + +int consume_event(struct parser_state *&s, yaml_event_t *event); +int parse_config(std::string filename); +#endif diff --git a/rvs/include/yaml_utils.h b/rvs/include/yaml_utils.h new file mode 100644 index 00000000..96505453 --- /dev/null +++ b/rvs/include/yaml_utils.h @@ -0,0 +1,5 @@ +#ifndef YAML_UTILS_H +#define YAML_UTILS_H +std::string getModuleName(std::string input); +std::string getModule(std::string filename); +#endif diff --git a/rvs/src/node_yaml.cpp b/rvs/src/node_yaml.cpp new file mode 100644 index 00000000..fe27530c --- /dev/null +++ b/rvs/src/node_yaml.cpp @@ -0,0 +1,208 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include "include/node_yaml.h" +/* Set environment variable DEBUG=1 to enable debug output. */ +int debug = 0; + + +/* + * Consume yaml events generated by the libyaml parser to + * import our data into raw c++ data structures. + */ +int consume_event(struct parser_state *&s, yaml_event_t *event) +{ + char *value; + std::string temp; + std::string key; + ActionMap f; + if (debug) { + printf("state=%d event=%d\n", s->state, event->type); + } + switch (s->state) { + case STATE_START: + switch (event->type) { + case YAML_STREAM_START_EVENT: + s->state = STATE_STREAM; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_STREAM: + switch (event->type) { + case YAML_DOCUMENT_START_EVENT: + s->state = STATE_DOCUMENT; + break; + case YAML_STREAM_END_EVENT: + s->state = STATE_STOP; /* All done. */ + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_DOCUMENT: + switch (event->type) { + case YAML_MAPPING_START_EVENT: + s->state = STATE_SECTION; + break; + case YAML_DOCUMENT_END_EVENT: + s->state = STATE_STREAM; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_SECTION: + switch (event->type) { + case YAML_SCALAR_EVENT: + value = (char *)event->data.scalar.value; + if (strcmp(value, ACTIONS.c_str()) == 0) { + s->state = STATE_ACTIONLIST; + } else { + fprintf(stderr, "Unexpected scalar: %s\n", value); + return FAILURE; + } + break; + case YAML_DOCUMENT_END_EVENT: + s->state = STATE_STREAM; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTIONLIST: + switch (event->type) { + case YAML_SEQUENCE_START_EVENT: + s->state = STATE_ACTIONVALUES; + break; + case YAML_MAPPING_END_EVENT: + s->state = STATE_SECTION; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTIONVALUES: + switch (event->type) { + case YAML_MAPPING_START_EVENT: + s->state = STATE_ACTIONKEY; + break; + case YAML_SEQUENCE_END_EVENT: + s->state = STATE_ACTIONLIST; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTIONKEY: + switch (event->type) { + case YAML_SCALAR_EVENT: + // make a key and save state as value state + key = (char *)event->data.scalar.value; + s->keyname = key; + //value = (char *)event->data.scalar.value; + s->state = STATE_ACTION_VALUE; + break; + + case YAML_MAPPING_END_EVENT: + s->actionlist.push_back( s->f); + s->f.clear(); + s->state = STATE_ACTIONVALUES; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_ACTION_VALUE: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = (char *)event->data.scalar.value; + if(s->keyname.empty()){ + std::cout << "cant have empty key " << std::endl; + return FAILURE; + } + s->f.emplace(s->keyname, std::string(temp)); + s->keyname.clear(); + s->state = STATE_ACTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_STOP: + break; + } + return SUCCESS; +} + +int parse_config(std::string filename){ + int code; + int status_; + //struct parser_state state; + parser_state *state = new parser_state{}; + yaml_parser_t parser; + yaml_event_t event; + + if (getenv("DEBUG")) { + debug = 1; + } + FILE *fd = fopen(filename.c_str(), "r"); + if(!fd){ + std::cout << "Could not open file " << std::endl; + return -1; + } + //memset(&state, 0, sizeof(state)); + state->state = STATE_START; + yaml_parser_initialize(&parser); + yaml_parser_set_input_file(&parser, fd); + do { + status_ = yaml_parser_parse(&parser, &event); + if (status_ == FAILURE) { + fprintf(stderr, "yaml_parser_parse error\n"); + code = EXIT_FAILURE; + goto done; + } + status_ = consume_event(state, &event); + if (status_ == FAILURE) { + fprintf(stderr, "consume_event error\n"); + code = EXIT_FAILURE; + goto done; + } + yaml_event_delete(&event); + } while (state->state != STATE_STOP); + + /* Output the parsed data. */ + for (auto f : state->actionlist ) { + std::cout << f["name"]<< std::endl; + } + code = EXIT_SUCCESS; + +done: + //destroy_actions(state->actionlist); + yaml_parser_delete(&parser); + delete state; + return code; +} + diff --git a/rvs/src/yaml_utils.cpp b/rvs/src/yaml_utils.cpp new file mode 100644 index 00000000..9a18521c --- /dev/null +++ b/rvs/src/yaml_utils.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +#include +std::string getModuleName(std::string input){ + std::stringstream is{input}; + std::string temp, last; + while (std::getline(is, temp, ' ')) { + last=temp; + } + return last; +} +std::string getModule(std::string filename){ + static int actions = 0; + std::string module_name{}; + std::ifstream f{filename}; + assert (f.good()); + std::string line; + while(std::getline(f, line)){ + if(line.empty()) + continue; + line = line.substr(line.find_first_not_of(" ")); + if(line[0] == '#') + continue; + if(line.find("name:") != std::string::npos){ + ++actions; + if(actions > 1){ + // should have found module by now, invalid conf. + std::cout <<" Invalid conf file, missing module name " << std::endl; + return module_name; + } else{ + continue; + } + } + if (line.find("module:") != std::string::npos){ + return getModuleName(line); + } else{ + continue; + } + } + return module_name; +} From 10b385f1c5ee6390d53fd11ad9d11f786e3692c0 Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Tue, 14 Dec 2021 17:10:56 +0000 Subject: [PATCH 05/12] use new APIs in rvs do yaml --- rvs/src/node_yaml.cpp | 6 +++--- rvs/src/rvsexec_do_yaml.cpp | 33 ++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/rvs/src/node_yaml.cpp b/rvs/src/node_yaml.cpp index fe27530c..0ba69c13 100644 --- a/rvs/src/node_yaml.cpp +++ b/rvs/src/node_yaml.cpp @@ -157,11 +157,11 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) return SUCCESS; } -int parse_config(std::string filename){ +int parse_config(parser_state *&state,std::string filename){ int code; int status_; //struct parser_state state; - parser_state *state = new parser_state{}; + //parser_state *state = new parser_state{}; yaml_parser_t parser; yaml_event_t event; @@ -202,7 +202,7 @@ int parse_config(std::string filename){ done: //destroy_actions(state->actionlist); yaml_parser_delete(&parser); - delete state; + //delete state; return code; } diff --git a/rvs/src/rvsexec_do_yaml.cpp b/rvs/src/rvsexec_do_yaml.cpp index 765b4741..5702b0c5 100644 --- a/rvs/src/rvsexec_do_yaml.cpp +++ b/rvs/src/rvsexec_do_yaml.cpp @@ -37,7 +37,7 @@ #include "include/rvsliblogger.h" #include "include/rvsoptions.h" #include "include/rvs_util.h" - +#include "include/node_yaml.h" #define MODULE_NAME_CAPS "CLI" /*** Example rvs.conf file structure @@ -68,18 +68,21 @@ using std::string; int rvs::exec::do_yaml(const std::string& config_file) { int sts = 0; - YAML::Node config = YAML::LoadFile(config_file); - + //YAML::Node config = YAML::LoadFile(config_file); + parser_state *state = new parser_state{};// TODO: use shared_ptr + int res = parse_config(state, config_file); + if (EXIT_SUCCESS != res){ + rvs::logger::Err("yaml conf read error", MODULE_NAME_CAPS); + delete state; + return sts; + } // find "actions" map - const YAML::Node& actions = config["actions"]; - - + const auto& actions = state->actionlist; // for all actions... - for (YAML::const_iterator it = actions.begin(); it != actions.end(); ++it) { - const YAML::Node& action = *it; - - rvs::logger::log("Action name :" + action["name"].as(), rvs::logresults); + for (const auto& action : actions) { + rvs::logger::log("Action name :" + action["name"], rvs::logresults); + std::cout << "Action name :" << action["name"] << std::endl; // if stop was requested if (rvs::logger::Stopping()) { return -1; @@ -88,7 +91,7 @@ int rvs::exec::do_yaml(const std::string& config_file) { // find module name std::string rvsmodule; try { - rvsmodule = action["module"].as(); + rvsmodule = action.at("module"); } catch(...) { } @@ -97,7 +100,7 @@ int rvs::exec::do_yaml(const std::string& config_file) { // report error and go to next action char buff[1024]; snprintf(buff, sizeof(buff), "action '%s' does not specify module.", - action["name"].as().c_str()); + action["name"].c_str()); rvs::logger::Err(buff, MODULE_NAME_CAPS); return -1; } @@ -108,7 +111,7 @@ int rvs::exec::do_yaml(const std::string& config_file) { char buff[1024]; snprintf(buff, sizeof(buff), "action '%s' could not crate action object in module '%s'", - action["name"].as().c_str(), + action["name"].c_str(), rvsmodule.c_str()); rvs::logger::Err(buff, MODULE_NAME_CAPS); return -1; @@ -119,7 +122,7 @@ int rvs::exec::do_yaml(const std::string& config_file) { char buff[1024]; snprintf(buff, sizeof(buff), "action '%s' could not obtain interface if1", - action["name"].as().c_str()); + action["name"]..c_str()); module::action_destroy(pa); return -1; } @@ -161,7 +164,7 @@ int rvs::exec::do_yaml(const std::string& config_file) { * @return 0 if successful, non-zero otherwise * */ -int rvs::exec::do_yaml_properties(const YAML::Node& node, +int rvs::exec::do_yaml_properties(const Actions& node, const std::string& module_name, rvs::if1* pif1) { int sts = 0; From c693353941265f8671e88d58b6047eca68b97532 Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Wed, 15 Dec 2021 06:51:03 +0000 Subject: [PATCH 06/12] Yaml usage in rvs do yaml Added lib reference in for using yaml --- rvs/CMakeLists.txt | 2 +- rvs/include/node_yaml.h | 8 +++++++- rvs/include/rvsexec.h | 11 +++++++++++ rvs/src/node_yaml.cpp | 4 ---- rvs/src/rvsexec_do_yaml.cpp | 38 ++++++++++++++++++------------------- rvs/tests.cmake | 2 +- 6 files changed, 39 insertions(+), 26 deletions(-) diff --git a/rvs/CMakeLists.txt b/rvs/CMakeLists.txt index 7ee83b99..955fe17b 100644 --- a/rvs/CMakeLists.txt +++ b/rvs/CMakeLists.txt @@ -144,7 +144,7 @@ set(SOURCES ## define helper lib add_library(rvshelper ${SOURCES}) - +target_link_libraries(rvshelper yaml) ## define target add_executable(${RVS_TARGET} src/rvs.cpp) target_link_libraries(${RVS_TARGET} rvshelper rvslib ${PROJECT_LINK_LIBS} ) diff --git a/rvs/include/node_yaml.h b/rvs/include/node_yaml.h index 336a8d41..2fa0e38b 100644 --- a/rvs/include/node_yaml.h +++ b/rvs/include/node_yaml.h @@ -1,5 +1,11 @@ #ifndef NODE_YAML_H #define NODE_YAML_H +#include +#include +#include +#include +#include +#include enum status { SUCCESS = 1, FAILURE = 0 @@ -35,5 +41,5 @@ struct parser_state { }; int consume_event(struct parser_state *&s, yaml_event_t *event); -int parse_config(std::string filename); +int parse_config(struct parser_state *&s,std::string filename); #endif diff --git a/rvs/include/rvsexec.h b/rvs/include/rvsexec.h index 4af3123d..8d92c039 100644 --- a/rvs/include/rvsexec.h +++ b/rvs/include/rvsexec.h @@ -27,6 +27,7 @@ #include #include "yaml-cpp/node/node.h" +#include "node_yaml.h" namespace rvs { @@ -56,6 +57,15 @@ class exec { int do_gpu_list(void); int do_yaml(const std::string& config_file); + int do_yaml_properties(const ActionMap& node, + const std::string& module_name, if1* pif1); + bool is_yaml_properties_collection(const std::string& module_name, + const std::string& proprty_name); + int do_yaml_properties_collection(const ActionMap& node, + const std::string& parent_name, + if1* pif1); + + /* int do_yaml_properties(const YAML::Node& node, const std::string& module_name, if1* pif1); bool is_yaml_properties_collection(const std::string& module_name, @@ -63,6 +73,7 @@ class exec { int do_yaml_properties_collection(const YAML::Node& node, const std::string& parent_name, if1* pif1); + */ }; } // namespace rvs diff --git a/rvs/src/node_yaml.cpp b/rvs/src/node_yaml.cpp index 0ba69c13..389ae40b 100644 --- a/rvs/src/node_yaml.cpp +++ b/rvs/src/node_yaml.cpp @@ -1,11 +1,7 @@ -#include #include #include -#include #include #include -#include -#include #include "include/node_yaml.h" /* Set environment variable DEBUG=1 to enable debug output. */ int debug = 0; diff --git a/rvs/src/rvsexec_do_yaml.cpp b/rvs/src/rvsexec_do_yaml.cpp index 5702b0c5..923dcb3a 100644 --- a/rvs/src/rvsexec_do_yaml.cpp +++ b/rvs/src/rvsexec_do_yaml.cpp @@ -81,8 +81,8 @@ int rvs::exec::do_yaml(const std::string& config_file) { // for all actions... for (const auto& action : actions) { - rvs::logger::log("Action name :" + action["name"], rvs::logresults); - std::cout << "Action name :" << action["name"] << std::endl; + rvs::logger::log("Action name :" + action.at("name"), rvs::logresults); + std::cout << "Action name :" << action.at("name") << std::endl; // if stop was requested if (rvs::logger::Stopping()) { return -1; @@ -100,7 +100,7 @@ int rvs::exec::do_yaml(const std::string& config_file) { // report error and go to next action char buff[1024]; snprintf(buff, sizeof(buff), "action '%s' does not specify module.", - action["name"].c_str()); + action.at("name").c_str()); rvs::logger::Err(buff, MODULE_NAME_CAPS); return -1; } @@ -111,7 +111,7 @@ int rvs::exec::do_yaml(const std::string& config_file) { char buff[1024]; snprintf(buff, sizeof(buff), "action '%s' could not crate action object in module '%s'", - action["name"].c_str(), + action.at("name").c_str(), rvsmodule.c_str()); rvs::logger::Err(buff, MODULE_NAME_CAPS); return -1; @@ -122,7 +122,7 @@ int rvs::exec::do_yaml(const std::string& config_file) { char buff[1024]; snprintf(buff, sizeof(buff), "action '%s' could not obtain interface if1", - action["name"]..c_str()); + action.at("name").c_str()); module::action_destroy(pa); return -1; } @@ -164,7 +164,7 @@ int rvs::exec::do_yaml(const std::string& config_file) { * @return 0 if successful, non-zero otherwise * */ -int rvs::exec::do_yaml_properties(const Actions& node, +int rvs::exec::do_yaml_properties(const ActionMap& node, const std::string& module_name, rvs::if1* pif1) { int sts = 0; @@ -177,22 +177,22 @@ int rvs::exec::do_yaml_properties(const Actions& node, rvs::logger::log("Module name :" + module_name, rvs::logresults); // for all child nodes - for (YAML::const_iterator it = node.begin(); it != node.end(); it++) { + for (auto it = node.begin(); it != node.end(); ++it) { // if property is collection of module specific properties, if (is_yaml_properties_collection(module_name, - it->first.as())) { + it->first)) { // pass properties collection to .so action object - sts += do_yaml_properties_collection(it->second, - it->first.as(), - pif1); + // sts += do_yaml_properties_collection(it->second, + // it->first, + // pif1); } else { // just set this one propertiy - if (indexes_provided && it->first.as() == "device") { + if (indexes_provided && it->first == "device") { std::replace(indexes.begin(), indexes.end(), ',', ' '); sts += pif1->property_set("device", indexes); } else { - sts += pif1->property_set(it->first.as(), - it->second.as()); + sts += pif1->property_set(it->first, + it->second); } } } @@ -206,16 +206,16 @@ int rvs::exec::do_yaml_properties(const Actions& node, * @return 0 if successful, non-zero otherwise * */ -int rvs::exec::do_yaml_properties_collection(const YAML::Node& node, +int rvs::exec::do_yaml_properties_collection(const ActionMap& node, const std::string& parent_name, if1* pif1) { int sts = 0; // for all child nodes - for (YAML::const_iterator it = node.begin(); it != node.end(); it++) { + for (auto it = node.begin(); it != node.end(); it++) { // prepend dot separated parent name and pass property to module - sts += pif1->property_set(parent_name + "." + it->first.as(), - it->second.IsNull() ? std::string("") : it->second.as()); + sts += pif1->property_set(parent_name + "." + it->first, + it->second.empty() ? std::string("") : it->second); } return sts; @@ -232,7 +232,7 @@ int rvs::exec::do_yaml_properties_collection(const YAML::Node& node, bool rvs::exec::is_yaml_properties_collection( const std::string& module_name, const std::string& property_name) { - + return false; if (module_name == "gpup") { if (property_name == "properties") return true; diff --git a/rvs/tests.cmake b/rvs/tests.cmake index 0959880e..1ca57649 100644 --- a/rvs/tests.cmake +++ b/rvs/tests.cmake @@ -32,7 +32,7 @@ ## define target for "test-to-fail" add_executable(${RVS_TARGET}fail src/rvs.cpp) -target_link_libraries(${RVS_TARGET}fail librvshelper.a rvslib ${PROJECT_LINK_LIBS} ) +target_link_libraries(${RVS_TARGET}fail librvshelper.a yaml rvslib ${PROJECT_LINK_LIBS} ) target_compile_definitions(${RVS_TARGET}fail PRIVATE RVS_INVERT_RETURN_STATUS) set_target_properties(${RVS_TARGET}fail PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${RVS_BINTEST_FOLDER} From 171b76fb130642219a8c46a80e817bfeb223f97f Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Wed, 15 Dec 2021 09:23:18 +0000 Subject: [PATCH 07/12] Adding support to collections for gm, gpup , pesm collection support is needed --- yaml-parser/src/node_yaml.cpp | 78 +++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/yaml-parser/src/node_yaml.cpp b/yaml-parser/src/node_yaml.cpp index f6377e1f..f1065f3b 100644 --- a/yaml-parser/src/node_yaml.cpp +++ b/yaml-parser/src/node_yaml.cpp @@ -65,6 +65,7 @@ using ActionMap = std::map; using ActionList = std::vector ; using Actions = std::map ; +using CollectionMap = std::map> ; /* Our application parser state data. */ struct parser_state { @@ -72,8 +73,22 @@ struct parser_state { ActionMap f; /* data elements. */ ActionList actionlist; /* List of action objects. */ std::string keyname; + std::string modulename; }; +CollectionMap collectionMap = +{ "gpup" : std::vector("properties", "io_links-properties"), + "peqt" : std::vector("capability"), + "gm" : std::vector("metrics") +}; +bool isCollection(const std::string& module, const std::string& property){ + if(collectionMap.find(module) == collectionMap.end()) + return false; + auto cvec = collectionMap[module]; + if(std::find(cvec.begin(), cvec.end(), property) != cvec.end()) + return true; + return false; +} /* @@ -182,7 +197,14 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) case YAML_SCALAR_EVENT: // make a key and save state as value state key = (char *)event->data.scalar.value; + // check if collection + bool collection = isCollection(s->modulename, key); + // based on this, append .collname to key and use it in value s->keyname = key; + if(collection){ + s->state = STATE_COLLECTION; + break; + } //value = (char *)event->data.scalar.value; s->state = STATE_ACTION_VALUE; break; @@ -207,6 +229,9 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) return FAILURE; } s->f.emplace(s->keyname, std::string(temp)); + if(s->keyname.compare("module") == 0){ + s->modulename = std::string(temp); + } s->keyname.clear(); s->state = STATE_ACTIONKEY; break; @@ -215,13 +240,11 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) return FAILURE; } break; -/* - case STATE_ACTIONNAME: + + case STATE_COLLECTION: switch (event->type) { - case YAML_SCALAR_EVENT: - temp = (char *)event->data.scalar.value; - s->f.m_name = temp; - s->state = STATE_ACTIONKEY; + case YAML_SEQUENCE_START_EVENT: + s->state = STATE_COLLECTIONLIST; break; default: fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); @@ -229,20 +252,51 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) } break; - case STATE_MODULENAME: + case STATE_COLLECTIONLIST: switch (event->type) { - case YAML_SCALAR_EVENT: - temp = (char *)event->data.scalar.value; - s->f.m_module_name = temp; - temp.clear(); - s->state = STATE_ACTIONKEY; + case YAML_MAPPING_START_EVENT: + s->state = STATE_COLLECTIONKEY; break; + case YAML_SEQUENCE_END_EVENT: + s->state = STATE_ACTIONKEY; default: fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); return FAILURE; } break; + case STATE_COLLECTIONKEY: + switch (event->type) { + case YAML_SCALAR_EVENT: + value = (char *)event->data.scalar.value; + s->colkey = s->key + "." + std::string(value); + std::cout << " collection key is " << s->colkey << std::endl; + s->state = STATE_COLLECTIONVALUE; + break; + case YAML_MAPPING_END_EVENT: + s->state = STATE_COLLECTIONLIST; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_COLLECTIONVALUE: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = (char *)event->data.scalar.value; + s->f.emplace(s->colkey, std::string(temp)); + s->colkey.clear(); + s->state = STATE_COLLECTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + +/* case STATE_DEVICES: switch (event->type) { case YAML_SCALAR_EVENT: From c56862e4730929d9a3bd0a55a66c5f9bbf97bd9c Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Wed, 15 Dec 2021 10:05:40 +0000 Subject: [PATCH 08/12] added collection support --- rvs/include/node_yaml.h | 10 ++++- rvs/src/node_yaml.cpp | 87 +++++++++++++++++++++++++++++++++++-- rvs/src/rvsexec_do_yaml.cpp | 3 ++ 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/rvs/include/node_yaml.h b/rvs/include/node_yaml.h index 2fa0e38b..a4c41202 100644 --- a/rvs/include/node_yaml.h +++ b/rvs/include/node_yaml.h @@ -6,6 +6,7 @@ #include #include #include +#include "yaml_utils.h" enum status { SUCCESS = 1, FAILURE = 0 @@ -24,6 +25,10 @@ enum parse_state { STATE_ACTIONKEY, /* action key */ STATE_ACTIONNAME, /* action name value */ STATE_ACTION_VALUE, /* all values encompassed as one*/ + STATE_COLLECTION, + STATE_COLLECTIONLIST, + STATE_COLLECTIONKEY, + STATE_COLLECTIONVALUE, STATE_STOP /* end state */ }; @@ -31,15 +36,16 @@ enum parse_state { using ActionMap = std::map ; using ActionList = std::vector ; using Actions = std::map ; - +using CollectionMap = std::map> ; /* Our application parser state data. */ struct parser_state { parse_state state; /* The current parse state */ ActionMap f; /* data elements. */ ActionList actionlist; /* List of action objects. */ std::string keyname; /* to retain key name from previous state */ + std::string colkey; /* to retain key name from previous state */ }; - +bool isCollection(const std::string& module, const std::string& property); int consume_event(struct parser_state *&s, yaml_event_t *event); int parse_config(struct parser_state *&s,std::string filename); #endif diff --git a/rvs/src/node_yaml.cpp b/rvs/src/node_yaml.cpp index 389ae40b..8a888b29 100644 --- a/rvs/src/node_yaml.cpp +++ b/rvs/src/node_yaml.cpp @@ -5,7 +5,25 @@ #include "include/node_yaml.h" /* Set environment variable DEBUG=1 to enable debug output. */ int debug = 0; +std::string rvsmodulename; +static CollectionMap collectionMap = +{ + { "gpup", {"properties", "io_links-properties"} }, + { "peqt" , {"capability"} }, + { "gm" , {"metrics"} } +}; +/* check if said property is a collection */ +bool isCollection(const std::string& module, const std::string& property){ + if(module.empty()) + return false; + if(collectionMap.find(module) == collectionMap.end()) + return false; + auto cvec = collectionMap[module]; + if(std::find(cvec.begin(), cvec.end(), property) != cvec.end()) + return true; + return false; +} /* * Consume yaml events generated by the libyaml parser to @@ -17,6 +35,7 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) std::string temp; std::string key; ActionMap f; + bool collection = false; if (debug) { printf("state=%d event=%d\n", s->state, event->type); } @@ -114,10 +133,13 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) // make a key and save state as value state key = (char *)event->data.scalar.value; s->keyname = key; - //value = (char *)event->data.scalar.value; - s->state = STATE_ACTION_VALUE; + collection = isCollection(rvsmodulename, key); + if(collection){ + s->state = STATE_COLLECTION; + }else{ + s->state = STATE_ACTION_VALUE; + } break; - case YAML_MAPPING_END_EVENT: s->actionlist.push_back( s->f); s->f.clear(); @@ -146,7 +168,62 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) return FAILURE; } break; - + + case STATE_COLLECTION: + switch (event->type) { + case YAML_SEQUENCE_START_EVENT: + s->state = STATE_COLLECTIONLIST; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_COLLECTIONLIST: + switch (event->type) { + case YAML_MAPPING_START_EVENT: + s->state = STATE_COLLECTIONKEY; + break; + case YAML_SEQUENCE_END_EVENT: + s->state = STATE_ACTIONKEY; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_COLLECTIONKEY: + switch (event->type) { + case YAML_SCALAR_EVENT: + value = (char *)event->data.scalar.value; + s->colkey = s->keyname + "." + std::string(value); + std::cout << " collection key is " << s->colkey << std::endl; + s->state = STATE_COLLECTIONVALUE; + break; + case YAML_MAPPING_END_EVENT: + s->state = STATE_COLLECTIONLIST; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + + case STATE_COLLECTIONVALUE: + switch (event->type) { + case YAML_SCALAR_EVENT: + temp = (char *)event->data.scalar.value; + s->f.emplace(s->colkey, std::string(temp)); + s->colkey.clear(); + s->state = STATE_COLLECTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; + case STATE_STOP: break; } @@ -169,6 +246,8 @@ int parse_config(parser_state *&state,std::string filename){ std::cout << "Could not open file " << std::endl; return -1; } + rvsmodulename = getModule(filename); + std::cout << "MANOJ: module name is " << rvsmodulename << std::endl; //memset(&state, 0, sizeof(state)); state->state = STATE_START; yaml_parser_initialize(&parser); diff --git a/rvs/src/rvsexec_do_yaml.cpp b/rvs/src/rvsexec_do_yaml.cpp index 923dcb3a..31668008 100644 --- a/rvs/src/rvsexec_do_yaml.cpp +++ b/rvs/src/rvsexec_do_yaml.cpp @@ -178,6 +178,7 @@ int rvs::exec::do_yaml_properties(const ActionMap& node, // for all child nodes for (auto it = node.begin(); it != node.end(); ++it) { + std::cout << "MANOJJJJ: kv is in yaml " << it->first << " and " << it->second << std::endl; // if property is collection of module specific properties, if (is_yaml_properties_collection(module_name, it->first)) { @@ -185,6 +186,8 @@ int rvs::exec::do_yaml_properties(const ActionMap& node, // sts += do_yaml_properties_collection(it->second, // it->first, // pif1); + sts += pif1->property_set(it->first, + it->second); /// handled appending in parsing } else { // just set this one propertiy if (indexes_provided && it->first == "device") { From f4f0257520d9af73dbfef6148923327b7533d01c Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Wed, 15 Dec 2021 11:38:28 +0000 Subject: [PATCH 09/12] make collections explicit in yaml --- rvs/conf/gpup_single.conf | 24 ++++++++++++------------ rvs/src/node_yaml.cpp | 8 ++++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/rvs/conf/gpup_single.conf b/rvs/conf/gpup_single.conf index 77d0a097..7a138202 100644 --- a/rvs/conf/gpup_single.conf +++ b/rvs/conf/gpup_single.conf @@ -19,9 +19,9 @@ actions: device: all module: gpup properties: - all: + - all: io_links-properties: - all: + - all: # GPUP test #2 # @@ -42,14 +42,14 @@ actions: device: all module: gpup properties: - simd_count: + - simd_count: mem_banks_count: io_links_count: vendor_id: location_id: max_engine_clk_ccompute: io_links-properties: - version_major: + - version_major: type: version_major: version_minor: @@ -79,9 +79,9 @@ actions: device: all module: gpup properties: - all: + - all: io_links-properties: - all: + - all: # GPUP test #4 # @@ -102,9 +102,9 @@ actions: module: gpup deviceid: properties: - all: + - all: io_links-properties: - all: + - all: # GPUP test #5 # @@ -128,9 +128,9 @@ actions: module: gpup deviceid: properties: - all: + - all: io_links-properties: - all: + - all: # GPUP test #6 # @@ -154,6 +154,6 @@ actions: module: gpup deviceid: properties: - mem_banks_count: + - mem_banks_count: io_links-properties: - version_major: + - version_major: diff --git a/rvs/src/node_yaml.cpp b/rvs/src/node_yaml.cpp index 8a888b29..4551935d 100644 --- a/rvs/src/node_yaml.cpp +++ b/rvs/src/node_yaml.cpp @@ -36,7 +36,7 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) std::string key; ActionMap f; bool collection = false; - if (debug) { + if (true) { printf("state=%d event=%d\n", s->state, event->type); } switch (s->state) { @@ -183,10 +183,12 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) case STATE_COLLECTIONLIST: switch (event->type) { case YAML_MAPPING_START_EVENT: + std::cout << "new collection starts expect " << STATE_COLLECTIONKEY <state = STATE_COLLECTIONKEY; break; case YAML_SEQUENCE_END_EVENT: s->state = STATE_ACTIONKEY; + break; default: fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); return FAILURE; @@ -197,11 +199,12 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) switch (event->type) { case YAML_SCALAR_EVENT: value = (char *)event->data.scalar.value; + std::cout << "col key is " << s->colkey << " and expected next STATE_COLLECTIONVALUE" << std::endl; s->colkey = s->keyname + "." + std::string(value); - std::cout << " collection key is " << s->colkey << std::endl; s->state = STATE_COLLECTIONVALUE; break; case YAML_MAPPING_END_EVENT: + std::cout <<"ended previous collection " << s->keyname << "expect " << STATE_COLLECTIONLIST<< std::endl; s->state = STATE_COLLECTIONLIST; break; default: @@ -215,6 +218,7 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) case YAML_SCALAR_EVENT: temp = (char *)event->data.scalar.value; s->f.emplace(s->colkey, std::string(temp)); + std::cout << " MANOJ:col key val is " << std::string(s->colkey) << " and " << std::string(temp) << std::endl; s->colkey.clear(); s->state = STATE_COLLECTIONKEY; break; From 40225b75d7a0128d362ed3c3a524c886ab223198 Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Wed, 15 Dec 2021 19:03:47 +0000 Subject: [PATCH 10/12] change to counter device_id script overrides device_id script appends ^M to each line and hence modulenames are faulty, for now fixin it in code. need to do in script --- rvs/include/node_yaml.h | 2 +- rvs/src/node_yaml.cpp | 76 ++++++++++++++++++++++++++++++++++------- 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/rvs/include/node_yaml.h b/rvs/include/node_yaml.h index a4c41202..a28e808a 100644 --- a/rvs/include/node_yaml.h +++ b/rvs/include/node_yaml.h @@ -45,7 +45,7 @@ struct parser_state { std::string keyname; /* to retain key name from previous state */ std::string colkey; /* to retain key name from previous state */ }; -bool isCollection(const std::string& module, const std::string& property); +bool isCollection(const std::string& property); int consume_event(struct parser_state *&s, yaml_event_t *event); int parse_config(struct parser_state *&s,std::string filename); #endif diff --git a/rvs/src/node_yaml.cpp b/rvs/src/node_yaml.cpp index 4551935d..8cd5d896 100644 --- a/rvs/src/node_yaml.cpp +++ b/rvs/src/node_yaml.cpp @@ -2,24 +2,74 @@ #include #include #include +#include #include "include/node_yaml.h" /* Set environment variable DEBUG=1 to enable debug output. */ int debug = 0; +std::mutex mtex; std::string rvsmodulename; -static CollectionMap collectionMap = +CollectionMap collectionMap = { { "gpup", {"properties", "io_links-properties"} }, - { "peqt" , {"capability"} }, - { "gm" , {"metrics"} } + { "peqt" , {"capability",} }, + { "gm" , {"metrics",} } }; /* check if said property is a collection */ -bool isCollection(const std::string& module, const std::string& property){ - if(module.empty()) +/* +bool isCollection(const std::string& property){ + bool retval = false; + if(rvsmodulename == "gpup"){ + if(property == "properties" || property == "io_links-properties") + retval= true; + }else if(rvsmodulename == "peqt"){ + if(property == "capability") + retval = true; + } else if(rvsmodulename == "metrics"){ + if(property == "metrics") + retval = true; + } + std::cout << " here retval is" << retval << std::endl; + return retval; +} +*/ +/* with device_id.sh script, chars added to every line of conf +* , so using this to temp cleanup +*/ +std::string cleanModuleName(std::string modname){ + std::string str; + if(rvsmodulename.find("gpup") != std::string::npos) + str = "gpup"; + if(rvsmodulename.find("gst") != std::string::npos) + str = "gst"; + if(rvsmodulename.find("iet") != std::string::npos) + str = "iet"; + if(rvsmodulename.find("pebb") != std::string::npos) + str = "pebb"; + if(rvsmodulename.find("peqt") != std::string::npos) + str = "peqt"; + if(rvsmodulename.find("pesm") != std::string::npos) + str = "pesm"; + if(rvsmodulename.find("pqt") != std::string::npos) + str = "pqt"; + if(rvsmodulename.find("mem") != std::string::npos) + str = "mem"; + return str; + +} + +bool isCollection(const std::string& property){ + std::lock_guard lm{mtex}; + std::cout << "val12" << rvsmodulename <<"44valend" << std::endl; + if(rvsmodulename.empty()){ + std::cout << "here??? " << std::endl; return false; - if(collectionMap.find(module) == collectionMap.end()) + } + if(collectionMap.find(rvsmodulename) == collectionMap.end()){ + std::cout << " orrrr herte?" << rvsmodulename<<"okay"<< std::endl; return false; - auto cvec = collectionMap[module]; + } + auto cvec = collectionMap[rvsmodulename]; if(std::find(cvec.begin(), cvec.end(), property) != cvec.end()) return true; return false; @@ -133,8 +183,9 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) // make a key and save state as value state key = (char *)event->data.scalar.value; s->keyname = key; - collection = isCollection(rvsmodulename, key); + collection = isCollection(key); if(collection){ + std::cout << " got collecttion key " << key << std::endl; s->state = STATE_COLLECTION; }else{ s->state = STATE_ACTION_VALUE; @@ -183,7 +234,6 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) case STATE_COLLECTIONLIST: switch (event->type) { case YAML_MAPPING_START_EVENT: - std::cout << "new collection starts expect " << STATE_COLLECTIONKEY <state = STATE_COLLECTIONKEY; break; case YAML_SEQUENCE_END_EVENT: @@ -199,12 +249,10 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) switch (event->type) { case YAML_SCALAR_EVENT: value = (char *)event->data.scalar.value; - std::cout << "col key is " << s->colkey << " and expected next STATE_COLLECTIONVALUE" << std::endl; s->colkey = s->keyname + "." + std::string(value); s->state = STATE_COLLECTIONVALUE; break; case YAML_MAPPING_END_EVENT: - std::cout <<"ended previous collection " << s->keyname << "expect " << STATE_COLLECTIONLIST<< std::endl; s->state = STATE_COLLECTIONLIST; break; default: @@ -218,7 +266,6 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) case YAML_SCALAR_EVENT: temp = (char *)event->data.scalar.value; s->f.emplace(s->colkey, std::string(temp)); - std::cout << " MANOJ:col key val is " << std::string(s->colkey) << " and " << std::string(temp) << std::endl; s->colkey.clear(); s->state = STATE_COLLECTIONKEY; break; @@ -251,7 +298,10 @@ int parse_config(parser_state *&state,std::string filename){ return -1; } rvsmodulename = getModule(filename); - std::cout << "MANOJ: module name is " << rvsmodulename << std::endl; + rvsmodulename = cleanModuleName(rvsmodulename); + //if(rvsmodulename.find("peqt") != std::string::npos) + // rvsmodulename = "peqt"; + std::cout << "MANOJ: module name is" << rvsmodulename<< "okay" << std::endl; //memset(&state, 0, sizeof(state)); state->state = STATE_START; yaml_parser_initialize(&parser); From a3c633f2061c74fb317a72d10e73b084ce512b40 Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Fri, 17 Dec 2021 09:14:42 +0000 Subject: [PATCH 11/12] Use smart ptr instead of raw --- rvs/include/node_yaml.h | 4 +- rvs/src/node_yaml.cpp | 92 ++++++++++++------------------------- rvs/src/rvsexec_do_yaml.cpp | 4 +- 3 files changed, 34 insertions(+), 66 deletions(-) diff --git a/rvs/include/node_yaml.h b/rvs/include/node_yaml.h index a28e808a..ddcfcf72 100644 --- a/rvs/include/node_yaml.h +++ b/rvs/include/node_yaml.h @@ -46,6 +46,6 @@ struct parser_state { std::string colkey; /* to retain key name from previous state */ }; bool isCollection(const std::string& property); -int consume_event(struct parser_state *&s, yaml_event_t *event); -int parse_config(struct parser_state *&s,std::string filename); +int consume_event(std::shared_ptr s, yaml_event_t *event); +int parse_config(std::shared_ptr state,std::string filename); #endif diff --git a/rvs/src/node_yaml.cpp b/rvs/src/node_yaml.cpp index 8cd5d896..09b6531e 100644 --- a/rvs/src/node_yaml.cpp +++ b/rvs/src/node_yaml.cpp @@ -2,11 +2,9 @@ #include #include #include -#include #include "include/node_yaml.h" /* Set environment variable DEBUG=1 to enable debug output. */ int debug = 0; -std::mutex mtex; std::string rvsmodulename; CollectionMap collectionMap = { @@ -15,24 +13,6 @@ CollectionMap collectionMap = { "gm" , {"metrics",} } }; -/* check if said property is a collection */ -/* -bool isCollection(const std::string& property){ - bool retval = false; - if(rvsmodulename == "gpup"){ - if(property == "properties" || property == "io_links-properties") - retval= true; - }else if(rvsmodulename == "peqt"){ - if(property == "capability") - retval = true; - } else if(rvsmodulename == "metrics"){ - if(property == "metrics") - retval = true; - } - std::cout << " here retval is" << retval << std::endl; - return retval; -} -*/ /* with device_id.sh script, chars added to every line of conf * , so using this to temp cleanup */ @@ -59,14 +39,10 @@ std::string cleanModuleName(std::string modname){ } bool isCollection(const std::string& property){ - std::lock_guard lm{mtex}; - std::cout << "val12" << rvsmodulename <<"44valend" << std::endl; if(rvsmodulename.empty()){ - std::cout << "here??? " << std::endl; return false; } if(collectionMap.find(rvsmodulename) == collectionMap.end()){ - std::cout << " orrrr herte?" << rvsmodulename<<"okay"<< std::endl; return false; } auto cvec = collectionMap[rvsmodulename]; @@ -79,7 +55,7 @@ bool isCollection(const std::string& property){ * Consume yaml events generated by the libyaml parser to * import our data into raw c++ data structures. */ -int consume_event(struct parser_state *&s, yaml_event_t *event) +int consume_event(std::shared_ptr s, yaml_event_t *event) { char *value; std::string temp; @@ -206,12 +182,12 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) switch (event->type) { case YAML_SCALAR_EVENT: temp = (char *)event->data.scalar.value; - if(s->keyname.empty()){ + if(s->keyname.empty()){ std::cout << "cant have empty key " << std::endl; return FAILURE; } s->f.emplace(s->keyname, std::string(temp)); - s->keyname.clear(); + s->keyname.clear(); s->state = STATE_ACTIONKEY; break; default: @@ -236,9 +212,9 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) case YAML_MAPPING_START_EVENT: s->state = STATE_COLLECTIONKEY; break; - case YAML_SEQUENCE_END_EVENT: - s->state = STATE_ACTIONKEY; - break; + case YAML_SEQUENCE_END_EVENT: + s->state = STATE_ACTIONKEY; + break; default: fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); return FAILURE; @@ -246,32 +222,32 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) break; case STATE_COLLECTIONKEY: - switch (event->type) { - case YAML_SCALAR_EVENT: - value = (char *)event->data.scalar.value; - s->colkey = s->keyname + "." + std::string(value); - s->state = STATE_COLLECTIONVALUE; - break; - case YAML_MAPPING_END_EVENT: - s->state = STATE_COLLECTIONLIST; - break; - default: - fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); - return FAILURE; - } - break; + switch (event->type) { + case YAML_SCALAR_EVENT: + value = (char *)event->data.scalar.value; + s->colkey = s->keyname + "." + std::string(value); + s->state = STATE_COLLECTIONVALUE; + break; + case YAML_MAPPING_END_EVENT: + s->state = STATE_COLLECTIONLIST; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; + } + break; case STATE_COLLECTIONVALUE: switch (event->type) { - case YAML_SCALAR_EVENT: - temp = (char *)event->data.scalar.value; - s->f.emplace(s->colkey, std::string(temp)); - s->colkey.clear(); - s->state = STATE_COLLECTIONKEY; - break; - default: - fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); - return FAILURE; + case YAML_SCALAR_EVENT: + temp = (char *)event->data.scalar.value; + s->f.emplace(s->colkey, std::string(temp)); + s->colkey.clear(); + s->state = STATE_COLLECTIONKEY; + break; + default: + fprintf(stderr, "Unexpected event %d in state %d.\n", event->type, s->state); + return FAILURE; } break; @@ -281,7 +257,7 @@ int consume_event(struct parser_state *&s, yaml_event_t *event) return SUCCESS; } -int parse_config(parser_state *&state,std::string filename){ +int parse_config(std::shared_ptr state,std::string filename){ int code; int status_; //struct parser_state state; @@ -299,10 +275,6 @@ int parse_config(parser_state *&state,std::string filename){ } rvsmodulename = getModule(filename); rvsmodulename = cleanModuleName(rvsmodulename); - //if(rvsmodulename.find("peqt") != std::string::npos) - // rvsmodulename = "peqt"; - std::cout << "MANOJ: module name is" << rvsmodulename<< "okay" << std::endl; - //memset(&state, 0, sizeof(state)); state->state = STATE_START; yaml_parser_initialize(&parser); yaml_parser_set_input_file(&parser, fd); @@ -322,10 +294,6 @@ int parse_config(parser_state *&state,std::string filename){ yaml_event_delete(&event); } while (state->state != STATE_STOP); - /* Output the parsed data. */ - for (auto f : state->actionlist ) { - std::cout << f["name"]<< std::endl; - } code = EXIT_SUCCESS; done: diff --git a/rvs/src/rvsexec_do_yaml.cpp b/rvs/src/rvsexec_do_yaml.cpp index 31668008..0213bc08 100644 --- a/rvs/src/rvsexec_do_yaml.cpp +++ b/rvs/src/rvsexec_do_yaml.cpp @@ -69,11 +69,11 @@ int rvs::exec::do_yaml(const std::string& config_file) { int sts = 0; //YAML::Node config = YAML::LoadFile(config_file); - parser_state *state = new parser_state{};// TODO: use shared_ptr + //parser_state *state = new parser_state{};// TODO: use shared_ptr + std::shared_ptr state{new parser_state{}};// = std::make_shared(); int res = parse_config(state, config_file); if (EXIT_SUCCESS != res){ rvs::logger::Err("yaml conf read error", MODULE_NAME_CAPS); - delete state; return sts; } // find "actions" map From b791db1e9b4a59113ab878cd063b710cdc919c28 Mon Sep 17 00:00:00 2001 From: Manoj S K Date: Fri, 17 Dec 2021 10:44:58 +0000 Subject: [PATCH 12/12] cleanup and changes --- rvs/include/node_yaml.h | 1 + rvs/include/rvsexec.h | 1 - rvs/include/rvsmodule.h | 3 +- rvs/src/node_yaml.cpp | 9 +- rvs/src/rvsexec.cpp | 1 - rvs/src/rvsexec_do_yaml.cpp | 3 - rvs/src/rvsexec_do_yaml_new.cpp | 252 -------------------------------- rvs/src/rvsmodule.cpp | 66 ++++----- 8 files changed, 32 insertions(+), 304 deletions(-) delete mode 100644 rvs/src/rvsexec_do_yaml_new.cpp diff --git a/rvs/include/node_yaml.h b/rvs/include/node_yaml.h index ddcfcf72..b7624d2f 100644 --- a/rvs/include/node_yaml.h +++ b/rvs/include/node_yaml.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include diff --git a/rvs/include/rvsexec.h b/rvs/include/rvsexec.h index 8d92c039..35c39ae8 100644 --- a/rvs/include/rvsexec.h +++ b/rvs/include/rvsexec.h @@ -26,7 +26,6 @@ #define RVS_INCLUDE_RVSEXEC_H_ #include -#include "yaml-cpp/node/node.h" #include "node_yaml.h" diff --git a/rvs/include/rvsmodule.h b/rvs/include/rvsmodule.h index 3f83c3ef..8a22335f 100644 --- a/rvs/include/rvsmodule.h +++ b/rvs/include/rvsmodule.h @@ -30,7 +30,6 @@ #include #include -#include "yaml-cpp/yaml.h" #include "include/rvsmodule_if.h" @@ -63,7 +62,7 @@ typedef std::pair t_mmpair; static module* find_create_module(const char* pShortName); //! YAML configuration - static YAML::Node config; + //static YAML::Node config; //! short name -> rvsmodule* mapping static std::map modulemap; diff --git a/rvs/src/node_yaml.cpp b/rvs/src/node_yaml.cpp index 09b6531e..72de0bf6 100644 --- a/rvs/src/node_yaml.cpp +++ b/rvs/src/node_yaml.cpp @@ -62,7 +62,7 @@ int consume_event(std::shared_ptr s, yaml_event_t *event) std::string key; ActionMap f; bool collection = false; - if (true) { + if (debug) { printf("state=%d event=%d\n", s->state, event->type); } switch (s->state) { @@ -161,7 +161,6 @@ int consume_event(std::shared_ptr s, yaml_event_t *event) s->keyname = key; collection = isCollection(key); if(collection){ - std::cout << " got collecttion key " << key << std::endl; s->state = STATE_COLLECTION; }else{ s->state = STATE_ACTION_VALUE; @@ -182,9 +181,9 @@ int consume_event(std::shared_ptr s, yaml_event_t *event) switch (event->type) { case YAML_SCALAR_EVENT: temp = (char *)event->data.scalar.value; - if(s->keyname.empty()){ - std::cout << "cant have empty key " << std::endl; - return FAILURE; + if(s->keyname.empty()){ + std::cout << "cant have empty key " << std::endl; + return FAILURE; } s->f.emplace(s->keyname, std::string(temp)); s->keyname.clear(); diff --git a/rvs/src/rvsexec.cpp b/rvs/src/rvsexec.cpp index 064e4bc5..bdc02c60 100644 --- a/rvs/src/rvsexec.cpp +++ b/rvs/src/rvsexec.cpp @@ -29,7 +29,6 @@ #include #include #include -#include "yaml-cpp/yaml.h" #include "include/rvsif0.h" #include "include/rvsif1.h" diff --git a/rvs/src/rvsexec_do_yaml.cpp b/rvs/src/rvsexec_do_yaml.cpp index 0213bc08..b0dc67ab 100644 --- a/rvs/src/rvsexec_do_yaml.cpp +++ b/rvs/src/rvsexec_do_yaml.cpp @@ -28,7 +28,6 @@ #include #include "include/rvsexec.h" -#include "yaml-cpp/yaml.h" #include "include/rvsif0.h" #include "include/rvsif1.h" @@ -82,7 +81,6 @@ int rvs::exec::do_yaml(const std::string& config_file) { for (const auto& action : actions) { rvs::logger::log("Action name :" + action.at("name"), rvs::logresults); - std::cout << "Action name :" << action.at("name") << std::endl; // if stop was requested if (rvs::logger::Stopping()) { return -1; @@ -178,7 +176,6 @@ int rvs::exec::do_yaml_properties(const ActionMap& node, // for all child nodes for (auto it = node.begin(); it != node.end(); ++it) { - std::cout << "MANOJJJJ: kv is in yaml " << it->first << " and " << it->second << std::endl; // if property is collection of module specific properties, if (is_yaml_properties_collection(module_name, it->first)) { diff --git a/rvs/src/rvsexec_do_yaml_new.cpp b/rvs/src/rvsexec_do_yaml_new.cpp deleted file mode 100644 index 2b88e0b2..00000000 --- a/rvs/src/rvsexec_do_yaml_new.cpp +++ /dev/null @@ -1,252 +0,0 @@ -/******************************************************************************** - * - * Copyright (c) 2018 ROCm Developer Tools - * - * MIT LICENSE: - * 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 -#include -#include -#include - -#include "include/rvsexec.h" -#include "include/yaml_utils.h" - -#include "include/rvsif0.h" -#include "include/rvsif1.h" -#include "include/rvsaction.h" -#include "include/rvsmodule.h" -#include "include/rvsliblogger.h" -#include "include/rvsoptions.h" -#include "include/rvs_util.h" - -#define MODULE_NAME_CAPS "CLI" - -/*** Example rvs.conf file structure - -actions: -- name: action_1 - device: all - module: gpup - properties: - mem_banks_count: - io_links-properties: - version_major: -- name: action_2 - module: gpup - device: all - -***/ - - -using std::string; - -/** - * @brief Executes actions listed in .conf file. - * - * @return 0 if successful, non-zero otherwise - * - */ -int rvs::exec::do_yaml(const std::string& config_file) { - int sts = 0; - - std::string rvsmodule = rocyaml::getModule(config_file); - std::cout <<"MANOJ: modname is " << rvsmodule; - // find "actions" map - std::string rvsmodule; - // for all actions... - for (YAML::const_iterator it = actions.begin(); it != actions.end(); ++it) { - const YAML::Node& action = *it; - - rvs::logger::log("Action name :" + action["name"].as(), rvs::logresults); - - // if stop was requested - if (rvs::logger::Stopping()) { - return -1; - } - - // find module name - std::string rvsmodule; - try { - rvsmodule = action["module"].as(); - } catch(...) { - } - - // not found or empty - if (rvsmodule == "") { - // report error and go to next action - char buff[1024]; - snprintf(buff, sizeof(buff), "action '%s' does not specify module.", - action["name"].as().c_str()); - rvs::logger::Err(buff, MODULE_NAME_CAPS); - return -1; - } - - // create action excutor in .so - rvs::action* pa = module::action_create(rvsmodule.c_str()); - if (!pa) { - char buff[1024]; - snprintf(buff, sizeof(buff), - "action '%s' could not crate action object in module '%s'", - action["name"].as().c_str(), - rvsmodule.c_str()); - rvs::logger::Err(buff, MODULE_NAME_CAPS); - return -1; - } - - if1* pif1 = dynamic_cast(pa->get_interface(1)); - if (!pif1) { - char buff[1024]; - snprintf(buff, sizeof(buff), - "action '%s' could not obtain interface if1", - action["name"].as().c_str()); - module::action_destroy(pa); - return -1; - } - - // load action properties from yaml file - sts += do_yaml_properties(action, rvsmodule, pif1); - if (sts) { - module::action_destroy(pa); - return sts; - } - - // set also command line options: - for (auto clit = rvs::options::get().begin(); - clit != rvs::options::get().end(); ++clit) { - std::string p(clit->first); - p = "cli." + p; - pif1->property_set(p, clit->second); - } - - // execute action - sts = pif1->run(); - - // processing finished, release action object - module::action_destroy(pa); - - // errors? - if (sts) { - // cancel actions and return - return sts; - } - } - - return 0; -} - -/** - * @brief Loads action properties. - * - * @return 0 if successful, non-zero otherwise - * - */ -int rvs::exec::do_yaml_properties(const YAML::Node& node, - const std::string& module_name, - rvs::if1* pif1) { - int sts = 0; - - string indexes; - bool indexes_provided = false; - if (rvs::options::has_option("-i", &indexes) && (!indexes.empty())) - indexes_provided = true; - - rvs::logger::log("Module name :" + module_name, rvs::logresults); - - // for all child nodes - for (YAML::const_iterator it = node.begin(); it != node.end(); it++) { - // if property is collection of module specific properties, - if (is_yaml_properties_collection(module_name, - it->first.as())) { - // pass properties collection to .so action object - sts += do_yaml_properties_collection(it->second, - it->first.as(), - pif1); - } else { - // just set this one propertiy - if (indexes_provided && it->first.as() == "device") { - std::replace(indexes.begin(), indexes.end(), ',', ' '); - sts += pif1->property_set("device", indexes); - } else { - sts += pif1->property_set(it->first.as(), - it->second.as()); - } - } - } - - return sts; -} - -/** - * @brief Loads property collection for collection type node in .conf file. - * - * @return 0 if successful, non-zero otherwise - * - */ -int rvs::exec::do_yaml_properties_collection(const YAML::Node& node, - const std::string& parent_name, - if1* pif1) { - int sts = 0; - - // for all child nodes - for (YAML::const_iterator it = node.begin(); it != node.end(); it++) { - // prepend dot separated parent name and pass property to module - sts += pif1->property_set(parent_name + "." + it->first.as(), - it->second.IsNull() ? std::string("") : it->second.as()); - } - - return sts; -} - -/** - * @brief Checks if property is collection type property in .conf file. - * - * @param module_name module name - * @param property_name property name - * @return 'true' if property is collection, 'false' otherwise - * - */ -bool rvs::exec::is_yaml_properties_collection( - const std::string& module_name, - const std::string& property_name) { - - if (module_name == "gpup") { - if (property_name == "properties") - return true; - - if (property_name == "io_links-properties") - return true; - } else { - if (module_name == "peqt") { - if (property_name == "capability") { - return true; - } - } else { - if (module_name == "gm") { - if (property_name == "metrics") - return true; - } - } - } - - return false; -} - diff --git a/rvs/src/rvsmodule.cpp b/rvs/src/rvsmodule.cpp index 59bf5106..bda0d0e3 100644 --- a/rvs/src/rvsmodule.cpp +++ b/rvs/src/rvsmodule.cpp @@ -33,19 +33,19 @@ #include #include #include - +#include #include "include/rvsliblogger.h" #include "include/rvsif0.h" #include "include/rvsif1.h" #include "include/rvsaction.h" #include "include/rvsliblog.h" #include "include/rvsoptions.h" - +#include "include/node_yaml.h" #define MODULE_NAME_CAPS "CLI" std::map rvs::module::modulemap; std::map rvs::module::filemap; -YAML::Node rvs::module::config; +//YAML::Node rvs::module::config; using std::string; @@ -79,51 +79,37 @@ rvs::module::~module() { int rvs::module::initialize(const char* pConfig) { // Check if pConfig file exists std::ifstream file(pConfig); - + int sts = -1; if (!file.good()) { char buff[1024]; snprintf(buff, sizeof(buff), "file does not exist: %s", pConfig); rvs::logger::Err(buff, MODULE_NAME_CAPS); return -1; } else { - file.close(); - } - - // load list of supported modules from config file - YAML::Node config = YAML::LoadFile(pConfig); - - // verify that that the file format is supported - YAML::const_iterator it = config.begin(); - if (it == config.end()) { - rvs::logger::Err("unsupported file format. Version string not found.", - MODULE_NAME_CAPS); - return -1; + //file.close(); } - - std::string key = it->first.as(); - std::string value = it->second.as(); - - if (key != "version") { - rvs::logger::Err("unsupported file format. Version string not found.", - MODULE_NAME_CAPS); - return -1; - } - - if (value != "1") { - char buff[1024]; - snprintf(buff, sizeof(buff), "file version is %s, expected 1", - value.c_str()); - rvs::logger::Err(buff, MODULE_NAME_CAPS); - return -1; - } - - // load nam-file pairs: - for (it++; it != config.end(); ++it) { - key = it->first.as(); - value = it->second.as(); - filemap.insert(std::pair(key, value)); + string line; + while (std::getline(file, line)) + { + std::istringstream iss(line); + string key,val; + std::getline(iss, key,':'); + key.erase(remove(key.begin(), key.end(), ' '), key.end()); + std::getline(iss, val,':'); + val.erase(remove(val.begin(), val.end(), ' '), val.end()); + + if(key != "version"){ + filemap.insert(std::pair(key, val)); + } else{ + if (val != "1") { + char buff[1024]; + snprintf(buff, sizeof(buff), "file version is %s, expected 1", + val.c_str()); + rvs::logger::Err(buff, MODULE_NAME_CAPS); + return -1; + } + } } - return 0; }