Skip to content

DTrace support #13

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

Open
wants to merge 6 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
44 changes: 43 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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} $<TARGET_OBJECTS:cjson_objs>)

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:
4 changes: 4 additions & 0 deletions cjson_dtrace.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provider lua_cjson {
probe start();
probe end(int, char *);
};
8 changes: 8 additions & 0 deletions external.h
Original file line number Diff line number Diff line change
@@ -0,0 +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
13 changes: 13 additions & 0 deletions lua_cjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include <lua.h>
#include <lauxlib.h>

#include "external.h"

#include "strbuf.h"
#include "fpconv.h"

Expand Down Expand Up @@ -708,6 +710,9 @@ static void json_append_data(lua_State *l, json_config_t *cfg,

static int json_encode(lua_State *l)
{
if (LUA_CJSON_START_ENABLED())
LUA_CJSON_START();

json_config_t *cfg = json_fetch_config(l);
strbuf_t local_encode_buf;
strbuf_t *encode_buf;
Expand All @@ -734,6 +739,9 @@ static int json_encode(lua_State *l)
if (!cfg->encode_keep_buffer)
strbuf_free(encode_buf);

if (LUA_CJSON_END_ENABLED())
LUA_CJSON_END(len, json);

return 1;
}

Expand Down Expand Up @@ -1258,6 +1266,8 @@ static void json_process_value(lua_State *l, json_parse_t *json,

static int json_decode(lua_State *l)
{
if (LUA_CJSON_START_ENABLED())
LUA_CJSON_START();
json_parse_t json;
json_token_t token;
size_t json_len;
Expand Down Expand Up @@ -1293,6 +1303,9 @@ static int json_decode(lua_State *l)

strbuf_free(json.tmp);

if (LUA_CJSON_END_ENABLED())
LUA_CJSON_END(json_len, (char *)json.data);

return 1;
}

Expand Down