Skip to content
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

Add json error #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions JSON_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include <stdlib.h>
#include <string.h>

#include "memtier_benchmark.h"
#include "JSON_handler.h"


/**
* C'tor
* -----
Expand All @@ -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();
Expand All @@ -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");
}
}

Expand All @@ -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 (m_nest_closer_types.size() > 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
Expand Down
3 changes: 3 additions & 0 deletions JSON_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
67 changes: 62 additions & 5 deletions memtier_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)
{
Expand Down Expand Up @@ -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){
jsonhandler->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){
jsonhandler->write_error("error: UNIX domain socket and TCP cannot be used together");
delete jsonhandler;
}
exit(1);
}

Expand All @@ -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){
jsonhandler->write_error("%s:%u: error: %s",cfg.server, cfg.port, e.what());
delete jsonhandler;
}
exit(1);
}
}
Expand All @@ -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){
jsonhandler->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){
jsonhandler->write_error("error: setrlimit failed: %s", strerror(errno));
delete jsonhandler;
}
exit(1);
}
}
Expand All @@ -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){
jsonhandler->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){
jsonhandler->write_error("error: use no-expiry only with data-import");
delete jsonhandler;
}
exit(1);
}

Expand All @@ -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){
jsonhandler->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){
jsonhandler->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){
jsonhandler->write_error("error: use key-minimum, key-maximum and key-prefix only with generate-keys");
delete jsonhandler;
}
exit(1);
}

Expand All @@ -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){
jsonhandler->write_error("error: failed to read keys");
delete jsonhandler;
}
exit(1);
} else {
fprintf(stderr, " %u keys read.\n", keylist->size());
Expand All @@ -1041,6 +1095,11 @@ int main(int argc, char *argv[])

if (dynamic_cast<import_object_generator*>(obj_gen)->open_file() != true) {
fprintf(stderr, "error: %s: failed to open.\n", cfg.data_import);
// JSON closing
if (jsonhandler != NULL){
jsonhandler->write_error("error: %s: failed to open", cfg.data_import);
delete jsonhandler;
}
exit(1);
}
}
Expand Down Expand Up @@ -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)
Expand Down