Skip to content
Open
38 changes: 38 additions & 0 deletions exaudfclient/exa_lib_loader.cc
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());

Copy link
Copy Markdown
Collaborator

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

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());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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;
}

5 changes: 5 additions & 0 deletions exaudfclient/exa_lib_loader.h
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);
20 changes: 20 additions & 0 deletions exaudfclient/exa_parser_cfg.cc
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;
}
4 changes: 4 additions & 0 deletions exaudfclient/exa_parser_cfg.h
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);
14 changes: 14 additions & 0 deletions exaudfclient/exa_set_env.cc
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));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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");
}
3 changes: 3 additions & 0 deletions exaudfclient/exa_set_env.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void setup_environment();
79 changes: 79 additions & 0 deletions exaudfclient/exa_udfclient.cc
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) {
Comment thread
tkilias marked this conversation as resolved.
if (!validateArguments(argc, argv)) {
exit(EXIT_FAILURE);
}
std::string libexaudflibPath;
#ifdef CUSTOM_LIBEXAUDFLIB_PATH

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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);
}
44 changes: 44 additions & 0 deletions exaudfclient/exa_vm_factory.cc
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
Comment thread
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
}
24 changes: 24 additions & 0 deletions exaudfclient/exa_vm_factory.h
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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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);
31 changes: 0 additions & 31 deletions exaudfclient/exaudfclient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,46 +78,15 @@ int main(int argc, char **argv) {
std::string libexaudflibPath = string(CUSTOM_LIBEXAUDFLIB_PATH);
#else
std::string libexaudflibPath = ::getenv("LIBEXAUDFLIB_PATH");
//std::string libexaudflibPath="libexaudflib_complete.so";
//std::string libexaudflibPath = std::string(argv[3]);
//std::string libexaudflibPath = std::string("/exaudf/libexaudflib_complete.so");
#endif
#if 1

Lmid_t my_namespace_id;
// DBGMSG(std::cerr, "Load libprotobuf into new namespace");
// DBGVAR(std::cerr, libProtobufPath);
// handle = dlmopen(LM_ID_NEWLM, libProtobufPath.c_str(),RTLD_NOW);
// if (!handle) {
// std::cerr << "Error when dynamically loading libprotobuf: " << dlerror() << endl;
// exit(EXIT_FAILURE);
// }
// if(dlinfo(handle, RTLD_DI_LMID, &my_namespace_id) != 0) {
// cerr << "Error when getting namespace id " << dlerror() << endl;
// exit(EXIT_FAILURE);
// }
DBGMSG(cerr, "Load libexaudflib");
DBGVAR(cerr, libexaudflibPath);
handle = dlmopen(LM_ID_NEWLM, libexaudflibPath.c_str(), RTLD_NOW);
// handle = dlopen(libexaudflibPath.c_str(), RTLD_NOW);

if (!handle) {
fprintf(stderr, "dmlopen: %s\n", dlerror());
exit(EXIT_FAILURE);
}
#else
handle = dlopen(libProtobufPath.c_str(),RTLD_NOW|RTLD_GLOBAL);
if (!handle) {
cerr << "Error when dynamically loading libprotobuf: " << dlerror() << endl;
exit(EXIT_FAILURE);
}
handle = dlopen("/exaudf/libexaudflib.so",RTLD_NOW);
if (!handle) {
fprintf(stderr, "dlopen: %s\n", dlerror());
exit(EXIT_FAILURE);
}
#endif


MAIN_FUN exaudfclient_main = (MAIN_FUN)load_dynamic("exaudfclient_main");
VOID_FUN_WITH_SWIGVM_PARAMS_P set_SWIGVM_params = (VOID_FUN_WITH_SWIGVM_PARAMS_P)load_dynamic("set_SWIGVM_params");
Expand Down
Loading