Skip to content
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
2 changes: 1 addition & 1 deletion Makefile.darwin
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include config.darwin

OBJS= src/clocks.o src/core_profiler.o src/function_meter.o src/stack.o src/lua50_profiler.o
OBJS= src/clocks.o src/cache.o src/core_profiler.o src/function_meter.o src/stack.o src/lua50_profiler.o

profiler: $(OBJS)
export MACOSX_DEPLOYMENT_TARGET=10.3 && mkdir -p bin && gcc -bundle -undefined dynamic_lookup -o $(PROFILER_OUTPUT) $(OBJS)
Expand Down
3 changes: 1 addition & 2 deletions Makefile.linux
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
include config.linux

OBJS= src/clocks.o src/core_profiler.o src/function_meter.o src/stack.o src/lua50_profiler.o

OBJS= src/clocks.o src/cache.o src/core_profiler.o src/function_meter.o src/stack.o src/lua50_profiler.o

profiler: $(OBJS)
mkdir -p bin && $(LD) -Bshareable -o $(PROFILER_OUTPUT) $(OBJS)
Expand Down
2 changes: 1 addition & 1 deletion Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ V= 2.0.1

include config.win

OBJS= src\clocks.obj src\core_profiler.obj src\function_meter.obj src\stack.obj src\lua50_profiler.obj
OBJS= src\clocks.obj src\cache.obj src\core_profiler.obj src\function_meter.obj src\stack.obj src\lua50_profiler.obj

lib: src\$(LIBNAME)

Expand Down
79 changes: 79 additions & 0 deletions src/cache.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifdef __cplusplus
extern "C" {
#endif

#include "cache.h"
#include <stdlib.h>
#include <string.h>

#define LPROF_CACHE_SIZE (1024*1024)
#define LPROF_LINE_SIZE (256)
#define LPROF_OUTPUT_LINE_SIZE (4096)

struct Cache
{
char line_buf[LPROF_LINE_SIZE];
char data[LPROF_CACHE_SIZE];
size_t pos;
};

static struct Cache* cache;

void lprofCache_append(FILE* outfile, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
lprofCache_vappend(outfile, fmt, ap);
va_end(ap);
}

void lprofCache_vappend(FILE* outfile, const char* fmt, va_list args)
{
if (cache == NULL)
{
cache = (struct Cache*)malloc(sizeof(struct Cache));
}

vsprintf(cache->line_buf, fmt, args);
size_t len = strlen(cache->line_buf);
if (cache->pos + len >= LPROF_CACHE_SIZE - 1) /* leave a space for '\0' */
{
lprofCache_write_all(outfile);
}

memcpy(cache->data + cache->pos, cache->line_buf, len + 1); /* len+1 copy '\0' */
cache->pos += len;
}

void lprofCache_write_all(FILE* outfile)
{
size_t start = 0;
while(cache->pos > 0)
{
size_t line_size = cache->pos < LPROF_OUTPUT_LINE_SIZE ?
cache->pos : LPROF_OUTPUT_LINE_SIZE;
fwrite(cache->data + start, sizeof(char), line_size, outfile);
cache->pos -= line_size;
start += line_size;
}
}

/* test cache */
/*
int main(int argc, char const *argv[])
{
FILE* file = fopen("/tmp/test.out", "a");
lprofCache_append(file, "abcdefg");
lprofCache_append(file, "b\n");
lprofCache_append(file, "c\n");

lprofCache_write_all(file);
fclose(file);

return 0;
}
*/

#ifdef __cplusplus
}
#endif
30 changes: 30 additions & 0 deletions src/cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
** LuaProfiler
** Jennal
** 2014-11-15
*/

/*****************************************************************************
cache.h:
Module to faster write file

Design:
'lprofCache_append()' append string to cache
'lprofCache_vappend()' append string to cache with va_list
'lprofCache_write_all()' write all cache data to output and clear cache
*****************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdarg.h>

void lprofCache_append(FILE* outfile, const char* fmt, ...);
void lprofCache_vappend(FILE* outfile, const char* fmt, va_list args);
void lprofCache_write_all(FILE* outfile);

#ifdef __cplusplus
}
#endif
8 changes: 8 additions & 0 deletions src/clocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ clocks.c:
was started
*****************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif


#include <stdio.h>
#include "clocks.h"

Expand Down Expand Up @@ -61,3 +66,6 @@ clock_t clocks;
return (float)clocks / (float)CLOCKS_PER_SEC;
}

#ifdef __cplusplus
}
#endif
9 changes: 9 additions & 0 deletions src/clocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ clocks.h:
was started
*****************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif


#include <time.h>

void lprofC_start_timer(clock_t *time_marker);
float lprofC_get_seconds(clock_t time_marker);

#ifdef __cplusplus
}
#endif
43 changes: 35 additions & 8 deletions src/core_profiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ the vertex is "printed" only after all his descendents have been processed (in
a depth-first search recursive algorithm).
*****************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "function_meter.h"

#include "core_profiler.h"
#include "cache.h" /* Jennal added */

/* default log name (%s is used to place a random string) */
#define OUT_FILENAME "lprof_%s.out"
Expand All @@ -57,13 +62,19 @@ static float function_call_time;
/* output a line to the log file, using 'printf()' syntax */
/* assume the timer is off */
static void output(const char *format, ...) {
/* Jennal modified */
// va_list ap;
// va_start(ap, format);
// vfprintf(outf, format, ap);
// va_end(ap);

// /* write now to avoid delays when the timer is on */
// fflush(outf);

va_list ap;
va_start(ap, format);
vfprintf(outf, format, ap);
lprofCache_vappend(outf, format, ap);
va_end(ap);

/* write now to avoid delays when the timer is on */
fflush(outf);
}


Expand Down Expand Up @@ -97,10 +108,18 @@ int lprofP_callhookOUT(lprofP_STATE* S) {

S->stack_level--;

/* 0: do not resume the parent function's timer yet... */
info = lprofM_leave_function(S, 0);
//Jennal move up
/* original code is
// 0: do not resume the parent function's timer yet...
info = lprofM_leave_function(S, 0);
// writing a log may take too long to be computed with the function's time ...
lprofM_pause_total_time(S);
*/
/* writing a log may take too long to be computed with the function's time ...*/
lprofM_pause_total_time(S);
/* 0: do not resume the parent function's timer yet... */
info = lprofM_leave_function(S, 0);

info->local_time += function_call_time;
info->total_time += function_call_time;

Expand Down Expand Up @@ -169,6 +188,7 @@ lprofP_STATE* lprofP_init_core_profiler(const char *_out_filename, int isto_prin
/* initialize the 'function_meter' */
S = lprofM_init();
if(!S) {
lprofCache_write_all(outf);
fclose(outf);
return 0;
}
Expand All @@ -177,7 +197,11 @@ lprofP_STATE* lprofP_init_core_profiler(const char *_out_filename, int isto_prin
}

void lprofP_close_core_profiler(lprofP_STATE* S) {
if(outf) fclose(outf);
if(outf) {
lprofCache_write_all(outf);
fclose(outf);
}

if(S) free(S);
}

Expand All @@ -195,3 +219,6 @@ lprofP_STATE* lprofP_create_profiler(float _function_call_time) {
return S;
}

#ifdef __cplusplus
}
#endif
8 changes: 8 additions & 0 deletions src/core_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Design (using the Lua callhook mechanism) :
'lprofP_callhookOUT' called whenever Lua leaves a function
*****************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

#include "stack.h"

/* computes new stack and new timer */
Expand All @@ -25,7 +29,11 @@ void lprofP_callhookIN(lprofP_STATE* S, char *func_name, char *file, int linedef
/* returns if there is another function in the stack */
int lprofP_callhookOUT(lprofP_STATE* S);

void lprofP_close_core_profiler(lprofP_STATE* S);
/* opens the log file */
/* returns true if the file could be opened */
lprofP_STATE* lprofP_init_core_profiler(const char *_out_filename, int isto_printheader, float _function_call_time);

#ifdef __cplusplus
}
#endif
10 changes: 10 additions & 0 deletions src/function_meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ function_meter.c:
another one or when the function terminates
*****************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -201,3 +206,8 @@ lprofP_STATE* lprofM_init() {
return S;
} else return NULL;
}


#ifdef __cplusplus
}
#endif
12 changes: 12 additions & 0 deletions src/function_meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ function_meter.c:
another one or when the function terminates
*****************************************************************************/


#ifdef __cplusplus
extern "C" {
#endif




#include "stack.h"


Expand Down Expand Up @@ -62,3 +70,7 @@ lprofS_STACK_RECORD *lprofM_leave_function(lprofP_STATE* S, int isto_resume);

/* init stack */
lprofP_STATE* lprofM_init();

#ifdef __cplusplus
}
#endif
12 changes: 11 additions & 1 deletion src/lua50_profiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ lua50_profiler.c:
Lua version dependent profiler interface
*****************************************************************************/

#include "lua50_profiler.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -17,9 +18,14 @@ lua50_profiler.c:
#include "core_profiler.h"
#include "function_meter.h"

#include "lua.h"
#include "lauxlib.h"

#ifdef __cplusplus
extern "C" {
#endif



/* Indices for the main profiler stack and for the original exit function */
static int exit_id;
static int profstate_id;
Expand All @@ -36,6 +42,7 @@ static void callhook(lua_State *L, lua_Debug *ar) {
lua_pushlightuserdata(L, &profstate_id);
lua_gettable(L, LUA_REGISTRYINDEX);
S = (lprofP_STATE*)lua_touserdata(L, -1);
lua_pop(L, 1);

if (lua_getstack(L, 1, &previous_ar) == 0) {
currentline = -1;
Expand Down Expand Up @@ -238,3 +245,6 @@ int luaopen_profiler(lua_State *L) {
lua_settable (L, -3);
return 1;
}
#ifdef __cplusplus
}
#endif
25 changes: 25 additions & 0 deletions src/lua50_profiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* lua50_profiler.h
* RDTower
*
* Created by changshuai on 14/11/10.
*
*/

#ifndef RDTower_lua50_profiler_h
#define RDTower_lua50_profiler_h


#ifdef __cplusplus
extern "C" {
#endif

#include "lua.h"

int luaopen_profiler(lua_State *L);

#ifdef __cplusplus
}
#endif

#endif
Loading