-
Notifications
You must be signed in to change notification settings - Fork 14
#1456: Cleaned up and refactored exaudfclient cc #642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
b563112
2d5a777
ef49152
5fe2ffd
2e800a6
6142c75
35b72a7
0748c70
e5ad9af
bbb9931
d6a441d
64e5554
a98a198
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #include <iostream> | ||
| #include <string> | ||
| #include <sstream> | ||
| #include <dlfcn.h> | ||
| #include <dlfcn.h> | ||
|
|
||
| #include "exa_lib_loader.h" | ||
| #include "utils/debug_message.h" | ||
|
|
||
| void* exa_load_libary(const std::string& stdLibPath) { | ||
| if (stdLibPath.empty()) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| void* handle = dlmopen(LM_ID_NEWLM, stdLibPath.c_str(), RTLD_NOW); | ||
| if (!handle) { | ||
| fprintf(stderr, "dlmopen error: %s; while loading %s\n", dlerror(), stdLibPath.c_str()); | ||
| return nullptr; | ||
| } | ||
| return handle; | ||
| } | ||
|
|
||
| void* exa_load_symbol(void *handle, const std::string& symbol_name) { | ||
| void *p_res = nullptr; | ||
| char *error = nullptr; | ||
| if(handle) { | ||
| p_res = dlsym(handle, symbol_name.c_str()); | ||
|
|
||
| if((error = dlerror()) != nullptr) { | ||
| std::stringstream sb; | ||
| sb << "Error when trying to load symbol '" << symbol_name << "': " << error; | ||
| fprintf(stderr, "dlsym error: %s loading symbol %s\n", error, sb.str().c_str()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already using streams for constructing sb, so we probably can also use cerr directly and don't need the string stream and the fprintf |
||
| return nullptr; | ||
| } | ||
| } | ||
| return p_res; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #pragma once | ||
| #include <string> | ||
|
|
||
| void* exa_load_libary(const std::string& stdLibPath); | ||
| void* exa_load_symbol(void *handle, const std::string& symbol_name); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #include <string> | ||
| #include <cstring> | ||
|
|
||
| #include "exa_parser_cfg.h" | ||
|
|
||
| // Parser option 2 means use ctpg parser. | ||
| // argv_parser_option is command line argument as it is | ||
| bool is_use_ctpg_parser(const std::string& argv_parser_option) { | ||
| bool use_ctpg_option_parser = false; | ||
|
|
||
| // env var has higher priority than argv value. | ||
| const char* env_val = ::getenv("SCRIPT_OPTIONS_PARSER_VERSION"); | ||
| if(env_val) { | ||
| use_ctpg_option_parser = (strcmp(env_val, "2") == 0); | ||
| } | ||
| else if(argv_parser_option.compare("scriptOptionsParserVersion=2") == 0) { | ||
| use_ctpg_option_parser = true; | ||
| } | ||
| return use_ctpg_option_parser; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #pragma once | ||
| #include <string> | ||
|
|
||
| bool is_use_ctpg_parser(const std::string& argv_parser_option); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #include <cstdlib> | ||
| #include <cstdio> | ||
| #include <clocale> | ||
| #include <cstring> | ||
| #include <cerrno> | ||
|
|
||
| #include "exa_set_env.h" | ||
|
|
||
| void setup_environment() { | ||
| if (::setenv("HOME", "/tmp", 1) == -1) { | ||
| fprintf(stderr, "Failed setting HOME env var: %s\n", std::strerror(errno)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably move this also to cerr |
||
| } | ||
| ::setlocale(LC_ALL, "en_US.utf8"); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #pragma once | ||
|
|
||
| void setup_environment(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #include <iostream> | ||
| #include <cstring> | ||
| #include <functional> | ||
| #include <string> | ||
|
|
||
| #include "exa_lib_loader.h" | ||
| #include "exa_parser_cfg.h" | ||
| #include "exa_set_env.h" | ||
| #include "exa_vm_factory.h" | ||
| #include "utils/debug_message.h" | ||
| #include "vm/swig_vm.h" | ||
| #include "exaudf_lib_output_path.h" | ||
|
|
||
| namespace SWIGVMContainers { | ||
| __thread SWIGVM_params_t * SWIGVM_params = nullptr; | ||
| } | ||
|
|
||
| typedef bool (*SET_SWIGVM_PARAMS)(SWIGVM_params_t*); | ||
| typedef int (*MAIN_FUN)(std::function<SWIGVMContainers::SWIGVM*()> vmMaker, int, char**); | ||
|
|
||
| void print_usage(const char *prg_name) { | ||
| std::cerr << "Usage: " << prg_name | ||
| << " <socket> lang=python|lang=r|lang=java|lang=streaming|lang=benchmark <scriptOptionsParserVersion=1|2>" | ||
| << std::endl; | ||
| } | ||
|
|
||
| bool validateArguments(int argc, char** argv) { | ||
| #ifdef UDF_PLUGIN_CLIENT | ||
| if (argc != 2) { | ||
| std::cerr << "Usage: " << argv[0] << " <socket>" << std::endl; | ||
| return false; | ||
| } | ||
| return true; | ||
| #else | ||
| if (argc < 3 || argc > 4) { | ||
| print_usage(argv[0]); | ||
| return false; | ||
| } | ||
|
|
||
| if (argc == 4 && | ||
| strcmp(argv[3], "scriptOptionsParserVersion=1") != 0 && | ||
| strcmp(argv[3], "scriptOptionsParserVersion=2") != 0) { | ||
| print_usage(argv[0]); | ||
| return false; | ||
| } | ||
| return true; | ||
| #endif | ||
| } | ||
|
|
||
| int main(int argc, char **argv) { | ||
|
tkilias marked this conversation as resolved.
|
||
| if (!validateArguments(argc, argv)) { | ||
| exit(EXIT_FAILURE); | ||
| } | ||
| std::string libexaudflibPath; | ||
| #ifdef CUSTOM_LIBEXAUDFLIB_PATH | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should move the ifdef into its own function, this would make this function easier to read |
||
| libexaudflibPath = std::string(CUSTOM_LIBEXAUDFLIB_PATH); | ||
| #else | ||
| libexaudflibPath = std::string(::getenv("LIBEXAUDFLIB_PATH")); | ||
| #endif | ||
| DBGMSG(std::cerr, "Load libexaudflib"); | ||
| DBGVAR(std::cerr, libexaudflibPath); | ||
| void* handle = exa_load_libary(libexaudflibPath); | ||
| if (!handle) { | ||
| fprintf(stderr, "Failed to load library: %s\n", libexaudflibPath.c_str()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are already using cerr, so we probably can use it here, too |
||
| exit(EXIT_FAILURE); | ||
| } | ||
|
|
||
| MAIN_FUN exaudfclient_main = (MAIN_FUN)exa_load_symbol(handle, "exaudfclient_main"); | ||
| SET_SWIGVM_PARAMS set_SWIGVM_params = (SET_SWIGVM_PARAMS)exa_load_symbol(handle, "set_SWIGVM_params"); | ||
|
|
||
| bool is_use_ctpg_parser = is_use_ctpg_parser(argv[3]); | ||
|
|
||
| setup_environment(); | ||
| std::function<SWIGVMContainers::SWIGVM*()>vmMaker = create_vm(argv[2], is_use_ctpg_parser); | ||
|
|
||
| SWIGVM_params = new SWIGVM_params_t(true); | ||
| set_SWIGVM_params(SWIGVM_params); | ||
| return exaudfclient_main(vmMaker, argc, argv); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #include <functional> | ||
| #include "exa_vm_factory.h" | ||
|
|
||
| std::function<SWIGVMContainers::SWIGVM*()> create_vm(const std::string& argv_lang, bool use_ctpg_options_parser) { | ||
| #ifdef UDF_PLUGIN_CLIENT | ||
| return [](){return new SWIGVMContainers::Protegrity(false);}; | ||
| #else | ||
|
sgn4sangar marked this conversation as resolved.
Outdated
|
||
| if(argv_lang.compare("lang=python") == 0) { | ||
| #ifdef ENABLE_PYTHON_VM | ||
| return []() { return new SWIGVMContainers::PythonVM(false); }; | ||
| #else | ||
| throw SWIGVMContainers::SWIGVM::exception("this exaudfclient has been compilied without Python support"); | ||
| #endif | ||
| } | ||
| else if(argv_lang.compare("lang=java") == 0) { | ||
| #ifdef ENABLE_JAVA_VM | ||
| if (use_ctpg_options_parser) { | ||
| return [&](){return SWIGVMContainers::JavaContainerBuilder().useCtpgParser().build();}; | ||
| } else { | ||
| return [&](){return SWIGVMContainers::JavaContainerBuilder().build();}; | ||
| } | ||
| #else | ||
| throw SWIGVMContainers::SWIGVM::exception("this exaudfclient has been compilied without Java support"); | ||
| #endif | ||
| } | ||
| else if(argv_lang.compare("lang=streaming") == 0) { | ||
| #ifdef ENABLE_STREAMING_VM | ||
| return []() { return new StreamingVM(false); }; | ||
| #else | ||
| throw SWIGVMContainers::SWIGVM::exception("this exaudfclient has been compilied without Streaming support"); | ||
| #endif | ||
| } | ||
| else if(argv_lang.compare("lang=benchmark") == 0) { | ||
| #ifdef ENABLE_BENCHMARK_VM | ||
| return []() { return new BenchmarkVM(false); }; | ||
| #else | ||
| throw SWIGVMContainers::SWIGVM::exception("this exaudfclient has been compilied without Benchmark support"); | ||
| #endif | ||
| } | ||
| else { | ||
| throw SWIGVMContainers::SWIGVM::exception("unsupported language specified in argv"); | ||
| } | ||
| #endif | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #pragma once | ||
| #include "swig_vm.h" | ||
|
|
||
| #ifdef ENABLE_JAVA_VM | ||
| #include "javacontainer/javacontainer_builder.h" | ||
| #endif //ENABLE_JAVA_VM | ||
|
|
||
| #ifdef ENABLE_PYTHON_VM | ||
| #include "python/pythoncontainer.h" | ||
| #endif //ENABLE_PYTHON_VM | ||
|
|
||
| #ifdef UDF_PLUGIN_CLIENT | ||
| #include "protegrityclient.h" | ||
| #endif //UDF_PLUGIN_CLIENT | ||
|
|
||
| #ifdef ENABLE_STREAMING_VM | ||
| #include "streaming_container/streamingcontainer.h" | ||
| #endif | ||
|
|
||
| #ifdef ENABLE_BENCHMARK_VM | ||
| #include "benchmark_container/benchmark_container.h" | ||
| #endif | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be moved into exaudfclient/exa_vm_factory.cc |
||
|
|
||
| std::function<SWIGVMContainers::SWIGVM*()> create_vm(const std::string& argv_lang, bool use_ctpg_options_parser); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably could move this also to cerr