From e7f1f5a0fc787d056cbc85937cbae21c1cb6bbc6 Mon Sep 17 00:00:00 2001 From: Elad Ash Date: Wed, 29 Mar 2017 16:41:47 +0300 Subject: [PATCH 1/2] Adding error writing and changing prints to debug mode --- JSON_handler.cpp | 19 ++++++++++-- JSON_handler.h | 3 ++ memtier_benchmark.cpp | 67 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 81 insertions(+), 8 deletions(-) diff --git a/JSON_handler.cpp b/JSON_handler.cpp index 95e1e595..b1160d9b 100644 --- a/JSON_handler.cpp +++ b/JSON_handler.cpp @@ -19,9 +19,9 @@ #include #include +#include "memtier_benchmark.h" #include "JSON_handler.h" - /** * C'tor * ----- @@ -36,7 +36,7 @@ json_handler::json_handler(const char * jsonfilename) : m_json_file(NULL) perror(jsonfilename); } // opening the JSON - fprintf(stderr, "Json file %s created...\n", jsonfilename); + benchmark_debug_log("Json file %s created...\n", jsonfilename); fprintf(m_json_file,"{"); m_nest_closer_types.push_back(NESTED_GENERAL); beutify(); @@ -53,7 +53,7 @@ json_handler::~json_handler() // close nesting... while (close_nesting()); fclose(m_json_file); - fprintf(stderr, "Json file closed.\n"); + benchmark_debug_log("Json file closed.\n"); } } @@ -73,6 +73,19 @@ void json_handler::write_obj(const char * objectname, const char * format, ...) fprintf(m_json_file, ","); } +void json_handler::write_error(const char * format, ...) +{ + // Close all the nesting until the end properly + while (nest_level > 1){ + close_nesting(); + } + // Add object with "FATAL_ERROR" object name + the errorr + va_list argptr; + va_start(argptr, format); + write_obj("FATAL_ERROR", format, argptr); + va_end(argptr); +} + /** * Starts a nesting with a title as defined in objectname * in case objectname == NULL it will not add the title and just start nesting diff --git a/JSON_handler.h b/JSON_handler.h index c5b5ef96..e4ec92ac 100644 --- a/JSON_handler.h +++ b/JSON_handler.h @@ -44,6 +44,9 @@ class json_handler { // Write a single object to JSON void write_obj(const char * objectname, const char * format, ...); + // Write error to JSON object + void write_error(const char * format, ...); + // Starts nesting, the type is used for deciding which charecter to be used for opening and closing // the nesting ('{}','[]') void open_nesting(const char * objectname,eJSON_NESTED_TYPE type = NESTED_GENERAL); diff --git a/memtier_benchmark.cpp b/memtier_benchmark.cpp index 76c5fa03..e6f5cf28 100755 --- a/memtier_benchmark.cpp +++ b/memtier_benchmark.cpp @@ -40,7 +40,6 @@ #include "obj_gen.h" #include "memtier_benchmark.h" - static int log_level = 0; void benchmark_log_file_line(int level, const char *filename, unsigned int line, const char *fmt, ...) { @@ -954,12 +953,22 @@ int main(int argc, char *argv[]) struct rlimit rlim; if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) { benchmark_error_log("error: getrlimit failed: %s\n", strerror(errno)); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: getrlimit failed: %s", strerror(errno)); + delete jsonhandler; + } exit(1); } if (cfg.unix_socket != NULL && (cfg.server != NULL || cfg.port > 0)) { benchmark_error_log("error: UNIX domain socket and TCP cannot be used together.\n"); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: UNIX domain socket and TCP cannot be used together"); + delete jsonhandler; + } exit(1); } @@ -969,6 +978,11 @@ int main(int argc, char *argv[]) } catch (std::runtime_error& e) { benchmark_error_log("%s:%u: error: %s\n", cfg.server, cfg.port, e.what()); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("%s:%u: error: %s",cfg.server, cfg.port, e.what()); + delete jsonhandler; + } exit(1); } } @@ -977,12 +991,22 @@ int main(int argc, char *argv[]) if (fds_needed > rlim.rlim_cur) { if (fds_needed > rlim.rlim_max && getuid() != 0) { benchmark_error_log("error: running the tool with this number of connections requires 'root' privilegs.\n"); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: running the tool with this number of connections requires 'root' privilegs"); + delete jsonhandler; + } exit(1); } rlim.rlim_cur = rlim.rlim_max = fds_needed; if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) { benchmark_error_log("error: setrlimit failed: %s\n", strerror(errno)); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: setrlimit failed: %s", strerror(errno)); + delete jsonhandler; + } exit(1); } } @@ -993,10 +1017,20 @@ int main(int argc, char *argv[]) if (!cfg.data_import) { if (cfg.data_verify) { fprintf(stderr, "error: use data-verify only with data-import\n"); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: use data-verify only with data-import"); + delete jsonhandler; + } exit(1); } if (cfg.no_expiry) { fprintf(stderr, "error: use no-expiry only with data-import\n"); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: use no-expiry only with data-import"); + delete jsonhandler; + } exit(1); } @@ -1008,17 +1042,32 @@ int main(int argc, char *argv[]) cfg.data_size_list.is_defined() || cfg.data_size_range.is_defined()) { fprintf(stderr, "error: data size cannot be specified when importing.\n"); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: data size cannot be specified when importing"); + delete jsonhandler; + } exit(1); } if (cfg.random_data) { fprintf(stderr, "error: random-data cannot be specified when importing.\n"); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: random-data cannot be specified when importing"); + delete jsonhandler; + } exit(1); } if (!cfg.generate_keys && (cfg.key_maximum || cfg.key_minimum || cfg.key_prefix)) { fprintf(stderr, "error: use key-minimum, key-maximum and key-prefix only with generate-keys.\n"); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: use key-minimum, key-maximum and key-prefix only with generate-keys"); + delete jsonhandler; + } exit(1); } @@ -1030,6 +1079,11 @@ int main(int argc, char *argv[]) if (!keylist->read_keys()) { fprintf(stderr, "\nerror: failed to read keys.\n"); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: failed to read keys"); + delete jsonhandler; + } exit(1); } else { fprintf(stderr, " %u keys read.\n", keylist->size()); @@ -1041,6 +1095,11 @@ int main(int argc, char *argv[]) if (dynamic_cast(obj_gen)->open_file() != true) { fprintf(stderr, "error: %s: failed to open.\n", cfg.data_import); + // JSON closing + if (jsonhandler != NULL){ + json_handler->write_error("error: %s: failed to open", cfg.data_import); + delete jsonhandler; + } exit(1); } } @@ -1220,10 +1279,8 @@ int main(int argc, char *argv[]) fclose(outfile); } - if (jsonhandler != NULL) { - // closing the JSON - delete jsonhandler; - } + // closing the JSON + if (jsonhandler != NULL) {delete jsonhandler;} delete obj_gen; if (keylist != NULL) From 4bd4f9c678f669cc090da29be0ef02233f985a6d Mon Sep 17 00:00:00 2001 From: Elad Ash Date: Fri, 31 Mar 2017 00:19:59 +0300 Subject: [PATCH 2/2] Fix build errors in json handler and memtier benchmark CPP files --- JSON_handler.cpp | 2 +- memtier_benchmark.cpp | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/JSON_handler.cpp b/JSON_handler.cpp index b1160d9b..982b62d5 100644 --- a/JSON_handler.cpp +++ b/JSON_handler.cpp @@ -76,7 +76,7 @@ void json_handler::write_obj(const char * objectname, const char * format, ...) void json_handler::write_error(const char * format, ...) { // Close all the nesting until the end properly - while (nest_level > 1){ + while (m_nest_closer_types.size() > 1){ close_nesting(); } // Add object with "FATAL_ERROR" object name + the errorr diff --git a/memtier_benchmark.cpp b/memtier_benchmark.cpp index e6f5cf28..813bb4e8 100755 --- a/memtier_benchmark.cpp +++ b/memtier_benchmark.cpp @@ -955,7 +955,7 @@ int main(int argc, char *argv[]) benchmark_error_log("error: getrlimit failed: %s\n", strerror(errno)); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: getrlimit failed: %s", strerror(errno)); + jsonhandler->write_error("error: getrlimit failed: %s", strerror(errno)); delete jsonhandler; } exit(1); @@ -966,7 +966,7 @@ int main(int argc, char *argv[]) benchmark_error_log("error: UNIX domain socket and TCP cannot be used together.\n"); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: UNIX domain socket and TCP cannot be used together"); + jsonhandler->write_error("error: UNIX domain socket and TCP cannot be used together"); delete jsonhandler; } exit(1); @@ -980,7 +980,7 @@ int main(int argc, char *argv[]) cfg.server, cfg.port, e.what()); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("%s:%u: error: %s",cfg.server, cfg.port, e.what()); + jsonhandler->write_error("%s:%u: error: %s",cfg.server, cfg.port, e.what()); delete jsonhandler; } exit(1); @@ -993,7 +993,7 @@ int main(int argc, char *argv[]) benchmark_error_log("error: running the tool with this number of connections requires 'root' privilegs.\n"); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: running the tool with this number of connections requires 'root' privilegs"); + jsonhandler->write_error("error: running the tool with this number of connections requires 'root' privilegs"); delete jsonhandler; } exit(1); @@ -1004,7 +1004,7 @@ int main(int argc, char *argv[]) benchmark_error_log("error: setrlimit failed: %s\n", strerror(errno)); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: setrlimit failed: %s", strerror(errno)); + jsonhandler->write_error("error: setrlimit failed: %s", strerror(errno)); delete jsonhandler; } exit(1); @@ -1019,7 +1019,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "error: use data-verify only with data-import\n"); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: use data-verify only with data-import"); + jsonhandler->write_error("error: use data-verify only with data-import"); delete jsonhandler; } exit(1); @@ -1028,7 +1028,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "error: use no-expiry only with data-import\n"); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: use no-expiry only with data-import"); + jsonhandler->write_error("error: use no-expiry only with data-import"); delete jsonhandler; } exit(1); @@ -1044,7 +1044,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "error: data size cannot be specified when importing.\n"); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: data size cannot be specified when importing"); + jsonhandler->write_error("error: data size cannot be specified when importing"); delete jsonhandler; } exit(1); @@ -1054,7 +1054,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "error: random-data cannot be specified when importing.\n"); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: random-data cannot be specified when importing"); + jsonhandler->write_error("error: random-data cannot be specified when importing"); delete jsonhandler; } exit(1); @@ -1065,7 +1065,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "error: use key-minimum, key-maximum and key-prefix only with generate-keys.\n"); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: use key-minimum, key-maximum and key-prefix only with generate-keys"); + jsonhandler->write_error("error: use key-minimum, key-maximum and key-prefix only with generate-keys"); delete jsonhandler; } exit(1); @@ -1081,7 +1081,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "\nerror: failed to read keys.\n"); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: failed to read keys"); + jsonhandler->write_error("error: failed to read keys"); delete jsonhandler; } exit(1); @@ -1097,7 +1097,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "error: %s: failed to open.\n", cfg.data_import); // JSON closing if (jsonhandler != NULL){ - json_handler->write_error("error: %s: failed to open", cfg.data_import); + jsonhandler->write_error("error: %s: failed to open", cfg.data_import); delete jsonhandler; } exit(1);