-
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 all commits
b563112
2d5a777
ef49152
5fe2ffd
2e800a6
6142c75
35b72a7
0748c70
e5ad9af
bbb9931
d6a441d
64e5554
a98a198
b3333c6
fe080ff
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 |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| #ifndef EXAUDFLIB_LOAD_DYNAMIC | ||
| #define EXAUDFLIB_LOAD_DYNAMIC | ||
|
|
||
| void set_exaudflib_handle(void* handle); | ||
| void* load_dynamic(const char* name); | ||
|
|
||
| extern void* handle; | ||
|
|
||
| #endif //EXAUDFLIB_LOAD_DYNAMIC |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #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) { | ||
| std::cerr << "dlmopen error: " << dlerror() << "; while loading " << stdLibPath << std::endl; | ||
| return nullptr; | ||
| } | ||
| return handle; | ||
| } | ||
|
|
||
| void* exa_load_symbol(void *handle, const std::string& symbol_name) { | ||
|
tkilias marked this conversation as resolved.
|
||
| void *p_res = nullptr; | ||
| char *error = nullptr; | ||
| if(handle) { | ||
| p_res = dlsym(handle, symbol_name.c_str()); | ||
|
|
||
| if((error = dlerror()) != nullptr) { | ||
| std::cerr << "Error when trying to load symbol '" << symbol_name << "': " << error << std::endl; | ||
| 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,17 @@ | ||
| #include <iostream> | ||
| #include <cstdlib> | ||
| #include <cstdio> | ||
| #include <clocale> | ||
| #include <cstring> | ||
| #include <cerrno> | ||
|
|
||
| #include "exa_set_env.h" | ||
|
|
||
| void setup_environment() { | ||
|
tkilias marked this conversation as resolved.
|
||
| if (::setenv("HOME", "/tmp", 1) == -1) { | ||
| std::cerr << "Failed setting HOME env var: " << std::strerror(errno) << std::endl; | ||
| } | ||
| if (::setlocale(LC_ALL, "en_US.utf8") == nullptr) { | ||
| std::cerr << "Failed setting locale: " << std::strerror(errno) << std::endl; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #pragma once | ||
|
|
||
| void setup_environment(); |
|
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 should choose or ".cc" or ".cpp" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #include <iostream> | ||
| #include <cstring> | ||
| #include <cstdlib> | ||
| #include <string> | ||
|
|
||
| #include "exaudf_lib_output_path.h" | ||
| #include "exaudflib/vm/swig_vm.h" | ||
| #include "exa_udf_base.h" | ||
| #include "exa_lib_loader.h" | ||
| #include "exa_set_env.h" | ||
| #include "exaudflib/load_dynamic.h" | ||
| #include "utils/debug_message.h" | ||
|
|
||
| namespace SWIGVMContainers { | ||
| __thread SWIGVM_params_t * SWIGVM_params = nullptr; | ||
| } | ||
|
|
||
|
|
||
| int ExaUdfClientBase::startClientBase(int argc, char** argv) { | ||
| if (!validate_arguments(argc, argv)) { | ||
| usage(argv[0]); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
| parse_arguments(argc, argv); | ||
|
|
||
| std::string libexaudflibPath; | ||
| #ifdef CUSTOM_LIBEXAUDFLIB_PATH | ||
| libexaudflibPath = std::string(CUSTOM_LIBEXAUDFLIB_PATH); | ||
| #else | ||
| libexaudflibPath = std::string(::getenv("LIBEXAUDFLIB_PATH")); | ||
| #endif | ||
| DBGMSG(std::cerr, "Load libexaudflib"); | ||
| DBGVAR(std::cerr, libexaudflibPath); | ||
| void* exaudflib_handle = exa_load_libary(libexaudflibPath); | ||
| if (!exaudflib_handle) { | ||
| std::cerr << "Failed to load library: " << libexaudflibPath << std::endl; | ||
| exit(EXIT_FAILURE); | ||
| } | ||
| set_exaudflib_handle(exaudflib_handle); | ||
|
|
||
| MAIN_FUN exaudfclient_main = (MAIN_FUN)exa_load_symbol(exaudflib_handle, "exaudfclient_main"); | ||
| SET_SWIGVM_PARAMS set_SWIGVM_params = (SET_SWIGVM_PARAMS)exa_load_symbol(exaudflib_handle, "set_SWIGVM_params"); | ||
|
|
||
| setup_environment(); | ||
| std::function<SWIGVMContainers::SWIGVM*()> vmMaker = create_vm(); | ||
|
|
||
| SWIGVMContainers::SWIGVM_params = new SWIGVMContainers::SWIGVM_params_t(true); | ||
| set_SWIGVM_params(SWIGVMContainers::SWIGVM_params); | ||
| return exaudfclient_main(vmMaker, argc, argv); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #pragma once | ||
| #include <functional> | ||
| #include <string> | ||
|
|
||
| #include "exaudflib/vm/swig_vm.h" | ||
|
|
||
| typedef bool (*SET_SWIGVM_PARAMS)(SWIGVMContainers::SWIGVM_params_t*); | ||
| typedef int (*MAIN_FUN)(std::function<SWIGVMContainers::SWIGVM*()> vmMaker, int, char**); | ||
|
|
||
| class ExaUdfClientBase { | ||
|
tkilias marked this conversation as resolved.
|
||
| public: | ||
| virtual ~ExaUdfClientBase() = default; | ||
| virtual void usage(const std::string& programName) = 0; | ||
| virtual std::function<SWIGVMContainers::SWIGVM*()> create_vm() = 0; | ||
| virtual bool validate_arguments(int argc, char** argv) = 0; | ||
| virtual void parse_arguments(int argc, char** argv) = 0; | ||
| virtual int startClientBase(int argc, char** argv); | ||
| }; | ||
|
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. Rename to exaudfclient/exa_common/exa_udf_client.cpp |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include <iostream> | ||
| #include <cstring> | ||
|
|
||
| #include "exa_udf_client.h" | ||
| #include "exa_vm_factory.h" | ||
|
|
||
| void ExaUdfClient::parse_arguments(int argc, char** argv) { | ||
| // Now assumption is that all cmd line aguments are already validated. | ||
| m_languageArg = argv[2]; | ||
|
|
||
| m_useCtpgParser = false; | ||
| const char* env_val = ::getenv("SCRIPT_OPTIONS_PARSER_VERSION"); | ||
| if(env_val && strcmp(env_val, "2") == 0) { | ||
| m_useCtpgParser = true; | ||
| } else if(argc == 4) { | ||
| std::string parse_option(argv[3]); | ||
| m_useCtpgParser = (parse_option.compare("scriptOptionsParserVersion=2") == 0); | ||
| } | ||
| } | ||
|
|
||
| bool ExaUdfClient::validate_arguments(int argc, char** argv) { | ||
| if (argc < 3 || argc > 4) { | ||
| usage(argv[0]); | ||
| return false; | ||
| } | ||
|
|
||
| if (argc == 4 && | ||
| strcmp(argv[3], "scriptOptionsParserVersion=1") != 0 && | ||
| strcmp(argv[3], "scriptOptionsParserVersion=2") != 0) { | ||
| usage(argv[0]); | ||
| return false; | ||
| } | ||
|
|
||
| if (!((strcmp(argv[2], "lang=python") == 0) | ||
| || (strcmp(argv[2], "lang=java") == 0) | ||
| || (strcmp(argv[2], "lang=streaming") == 0) | ||
| || (strcmp(argv[2], "lang=benchmark") == 0))) { | ||
| usage(argv[0]); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void ExaUdfClient::usage(const std::string& programName) { | ||
| std::cerr << "Usage: " << programName | ||
| << " <socket> lang=python|lang=java|lang=streaming|lang=benchmark <scriptOptionsParserVersion=1|2>" | ||
| << std::endl; | ||
| } | ||
|
|
||
| std::function<SWIGVMContainers::SWIGVM*()> ExaUdfClient::create_vm() { | ||
| return ::create_vm(m_languageArg, m_useCtpgParser); | ||
| } |
|
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. Rename to exaudfclient/exa_common/exa_udf_client.h |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #pragma once | ||
| #include <string> | ||
|
|
||
| #include "exa_udf_base.h" | ||
| #include "exaudflib/vm/swig_vm.h" | ||
|
|
||
| class ExaUdfClient : public ExaUdfClientBase { | ||
| public: | ||
| ~ExaUdfClient() override = default; | ||
| void parse_arguments(int argc, char** argv) override; | ||
| void usage(const std::string& programName) override; | ||
| bool validate_arguments(int argc, char** argv) override; | ||
| std::function<SWIGVMContainers::SWIGVM*()> create_vm() override; | ||
|
|
||
| protected: | ||
| std::string m_languageArg; | ||
| bool m_useCtpgParser = false; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #include <functional> | ||
| #include "exa_vm_factory.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 ENABLE_STREAMING_VM | ||
| #include "streaming_container/streamingcontainer.h" | ||
| #endif | ||
|
|
||
| #ifdef ENABLE_BENCHMARK_VM | ||
| #include "benchmark_container/benchmark_container.h" | ||
| #endif | ||
|
|
||
| std::function<SWIGVMContainers::SWIGVM*()> create_vm(const std::string& argv_lang, bool use_ctpg_options_parser) { | ||
|
tkilias marked this conversation as resolved.
|
||
| 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 SWIGVMContainers::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 SWIGVMContainers::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"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #pragma once | ||
| #include "exaudflib/vm/swig_vm.h" | ||
|
|
||
| std::function<SWIGVMContainers::SWIGVM*()> create_vm(const std::string& argv_lang, bool use_ctpg_options_parser); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #include "exa_common/exa_udf_client.h" | ||
|
|
||
| int main(int argc, char **argv) { | ||
|
tkilias marked this conversation as resolved.
|
||
| ExaUdfClient client; | ||
| return client.startClientBase(argc, argv); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.