From 1c13bc2da73d039af552d5d8fe43520b37304e88 Mon Sep 17 00:00:00 2001 From: Veniamin Gvozdikov Date: Sat, 28 Sep 2013 12:59:01 +0400 Subject: [PATCH 1/6] Added DTrace support --- cjson_dtrace.d | 5 +++++ lua_cjson.c | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 cjson_dtrace.d diff --git a/cjson_dtrace.d b/cjson_dtrace.d new file mode 100644 index 00000000..581ed561 --- /dev/null +++ b/cjson_dtrace.d @@ -0,0 +1,5 @@ +provider cjson { + probe new__entry(); + probe encode__start(); + probe encode__done(int len, char *); +}; diff --git a/lua_cjson.c b/lua_cjson.c index c14a1c5c..4e397816 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -46,6 +46,10 @@ #include "strbuf.h" #include "fpconv.h" +#ifdef ENABLE_DTRACE +#include "cjson_dtrace.h" +#endif + #ifndef CJSON_MODNAME #define CJSON_MODNAME "cjson" #endif @@ -708,6 +712,11 @@ static void json_append_data(lua_State *l, json_config_t *cfg, static int json_encode(lua_State *l) { + + if (CJSON_ENCODE_START_ENABLED()) { + CJSON_ENCODE_START(); + }; + json_config_t *cfg = json_fetch_config(l); strbuf_t local_encode_buf; strbuf_t *encode_buf; @@ -734,6 +743,10 @@ static int json_encode(lua_State *l) if (!cfg->encode_keep_buffer) strbuf_free(encode_buf); + if (CJSON_ENCODE_DONE_ENABLED()) { + CJSON_ENCODE_DONE(len, json); + }; + return 1; } From f0d78957202d6bddfef3869d56af80e82d3f454b Mon Sep 17 00:00:00 2001 From: Veniamin Gvozdikov Date: Sat, 28 Sep 2013 17:33:05 +0400 Subject: [PATCH 2/6] Added external.h for more convient changes DTrace defines --- external.h | 6 ++++++ lua_cjson.c | 14 ++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 external.h diff --git a/external.h b/external.h new file mode 100644 index 00000000..6942280f --- /dev/null +++ b/external.h @@ -0,0 +1,6 @@ +#ifdef ENABLE_DTRACE +#include "cjson_dtrace.h" +#else +#define CJSON_ENCODE_START() +#define CJSON_ENCODE_DONE(arg0, arg1) +#endif diff --git a/lua_cjson.c b/lua_cjson.c index 4e397816..b81a2e9f 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -43,13 +43,11 @@ #include #include +#include "external.h" + #include "strbuf.h" #include "fpconv.h" -#ifdef ENABLE_DTRACE -#include "cjson_dtrace.h" -#endif - #ifndef CJSON_MODNAME #define CJSON_MODNAME "cjson" #endif @@ -713,9 +711,7 @@ static void json_append_data(lua_State *l, json_config_t *cfg, static int json_encode(lua_State *l) { - if (CJSON_ENCODE_START_ENABLED()) { - CJSON_ENCODE_START(); - }; + CJSON_ENCODE_START(); json_config_t *cfg = json_fetch_config(l); strbuf_t local_encode_buf; @@ -743,9 +739,7 @@ static int json_encode(lua_State *l) if (!cfg->encode_keep_buffer) strbuf_free(encode_buf); - if (CJSON_ENCODE_DONE_ENABLED()) { - CJSON_ENCODE_DONE(len, json); - }; + CJSON_ENCODE_DONE(len, json); return 1; } From 0eb50265c599d83d844a4d444a9f1869299ef3ff Mon Sep 17 00:00:00 2001 From: Veniamin Gvozdikov Date: Sat, 28 Sep 2013 22:57:58 +0400 Subject: [PATCH 3/6] Added DTrace option in cmake --- CMakeLists.txt | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c17239b2..f531bd40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,11 @@ cmake_minimum_required(VERSION 2.6) option(USE_INTERNAL_FPCONV "Use internal strtod() / g_fmt() code for performance") option(MULTIPLE_THREADS "Support multi-threaded apps with internal fpconv - recommended" ON) +option(ENABLE_DTRACE "DTrace support" OFF) + +if(ENABLE_DTRACE) + find_program(DTRACE dtrace) +endif() if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release CACHE STRING @@ -68,9 +73,46 @@ else() set(_lua_module_dir "${_lua_lib_dir}/lua/5.1") endif() -add_library(cjson MODULE lua_cjson.c strbuf.c ${FPCONV_SOURCES}) +# Init empty cjson_obj for adding dtrace.o after generation one +set(cjson_obj) + +add_library(cjson_objs OBJECT lua_cjson.c strbuf.c ${FPCONV_SOURCES}) +set_target_properties(cjson_objs PROPERTIES POSITION_INDEPENDENT_CODE ON) +set(cjson_obj ${cjson_obj} $) + +if(ENABLE_DTRACE AND DTRACE) + message(STATUS "DTrace found and enabled to build with probes") + add_definitions(-DENABLE_DTRACE) + set(D_FILE ${PROJECT_SOURCE_DIR}/cjson_dtrace) + execute_process( + COMMAND ${DTRACE} -h -s ${D_FILE}.d -o ${D_FILE}.h + ) + if(${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD") +# Dirty hack, because isn't supported see for more details: +# http://public.kitware.com/Bug/view.php?id=14447 + set(cjson_c_o ${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/cjson_objs.dir/lua_cjson.c.o) + set(dtrace_obj ${CMAKE_CURRENT_BINARY_DIR}/dtrace.o) + add_custom_command(OUTPUT ${dtrace_obj} + COMMAND ${DTRACE} -G -s ${D_FILE}.d -o ${dtrace_obj} ${cjson_c_o} + DEPENDS ${cjson_c_o} + ) + set_source_files_properties(${dtrace_obj} + PROPERTIES + EXTERNAL_OBJECT true + GENERATED true + ) + set(_MODULE_LINK ${_MODULE_LINK} elf) + set(cjson_obj ${cjson_obj} ${dtrace_obj}) + unset(cjson_c_o) + unset(dtrace_obj) + unset(D_FILE) + endif() +endif() + +add_library(cjson MODULE ${cjson_obj}) set_target_properties(cjson PROPERTIES PREFIX "") target_link_libraries(cjson ${_MODULE_LINK}) + install(TARGETS cjson DESTINATION "${_lua_module_dir}") # vi:ai et sw=4 ts=4: From e51190cd65b2dbfe38e9d2e3429632c6ad7abf3a Mon Sep 17 00:00:00 2001 From: Veniamin Gvozdikov Date: Tue, 1 Oct 2013 07:51:47 +0000 Subject: [PATCH 4/6] Renamed providers and added probe to json_decode() --- cjson_dtrace.d | 7 +++---- lua_cjson.c | 6 ++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/cjson_dtrace.d b/cjson_dtrace.d index 581ed561..8ca356a9 100644 --- a/cjson_dtrace.d +++ b/cjson_dtrace.d @@ -1,5 +1,4 @@ -provider cjson { - probe new__entry(); - probe encode__start(); - probe encode__done(int len, char *); +provider lua_cjson { + probe start(); + probe end(int, char *); }; diff --git a/lua_cjson.c b/lua_cjson.c index b81a2e9f..0beea070 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -711,7 +711,7 @@ static void json_append_data(lua_State *l, json_config_t *cfg, static int json_encode(lua_State *l) { - CJSON_ENCODE_START(); + LUA_CJSON_START(); json_config_t *cfg = json_fetch_config(l); strbuf_t local_encode_buf; @@ -739,7 +739,7 @@ static int json_encode(lua_State *l) if (!cfg->encode_keep_buffer) strbuf_free(encode_buf); - CJSON_ENCODE_DONE(len, json); + LUA_CJSON_END(len, json); return 1; } @@ -1265,6 +1265,7 @@ static void json_process_value(lua_State *l, json_parse_t *json, static int json_decode(lua_State *l) { + LUA_CJSON_START(); json_parse_t json; json_token_t token; size_t json_len; @@ -1299,6 +1300,7 @@ static int json_decode(lua_State *l) json_throw_parse_error(l, &json, "the end", &token); strbuf_free(json.tmp); + LUA_CJSON_END(json_len, (char *)json.data); return 1; } From ef3893b9ed21bb67d638e32bef5ef43bc5f3e1e4 Mon Sep 17 00:00:00 2001 From: Veniamin Gvozdikov Date: Tue, 1 Oct 2013 08:13:01 +0000 Subject: [PATCH 5/6] Fix build without dtrace --- external.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/external.h b/external.h index 6942280f..865b0321 100644 --- a/external.h +++ b/external.h @@ -1,6 +1,6 @@ #ifdef ENABLE_DTRACE #include "cjson_dtrace.h" #else -#define CJSON_ENCODE_START() -#define CJSON_ENCODE_DONE(arg0, arg1) +#define LUA_CJSON_START() +#define LUA_CJSON_END(arg0, arg1) #endif From 402af6e707d52092e82a05060f25bdc31c9c51ea Mon Sep 17 00:00:00 2001 From: Veniamin Gvozdikov Date: Wed, 27 Nov 2013 00:18:04 +0400 Subject: [PATCH 6/6] Added checks enabled probes --- external.h | 2 ++ lua_cjson.c | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/external.h b/external.h index 865b0321..26061a19 100644 --- a/external.h +++ b/external.h @@ -1,6 +1,8 @@ #ifdef ENABLE_DTRACE #include "cjson_dtrace.h" #else +#define LUA_CJSON_START_ENABLED() (0) #define LUA_CJSON_START() +#define LUA_CJSON_END_ENABLED() (0) #define LUA_CJSON_END(arg0, arg1) #endif diff --git a/lua_cjson.c b/lua_cjson.c index 0beea070..63429522 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -710,8 +710,8 @@ static void json_append_data(lua_State *l, json_config_t *cfg, static int json_encode(lua_State *l) { - - LUA_CJSON_START(); + if (LUA_CJSON_START_ENABLED()) + LUA_CJSON_START(); json_config_t *cfg = json_fetch_config(l); strbuf_t local_encode_buf; @@ -739,7 +739,8 @@ static int json_encode(lua_State *l) if (!cfg->encode_keep_buffer) strbuf_free(encode_buf); - LUA_CJSON_END(len, json); + if (LUA_CJSON_END_ENABLED()) + LUA_CJSON_END(len, json); return 1; } @@ -1265,7 +1266,8 @@ static void json_process_value(lua_State *l, json_parse_t *json, static int json_decode(lua_State *l) { - LUA_CJSON_START(); + if (LUA_CJSON_START_ENABLED()) + LUA_CJSON_START(); json_parse_t json; json_token_t token; size_t json_len; @@ -1300,7 +1302,9 @@ static int json_decode(lua_State *l) json_throw_parse_error(l, &json, "the end", &token); strbuf_free(json.tmp); - LUA_CJSON_END(json_len, (char *)json.data); + + if (LUA_CJSON_END_ENABLED()) + LUA_CJSON_END(json_len, (char *)json.data); return 1; }