<commit_before>#include "skynet.h"
#include "trace_service.h"
#include "lua-seri.h"
#include "service_lua.h"

#include "luacompat52.h"

#define KNRM  "\x1B[0m"
#define KRED  "\x1B[31m"

#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>

#if defined(__APPLE__)
#include <mach/task.h>
#include <mach/mach.h>
#endif

struct stat {
	lua_State *L;
	int count;
	uint32_t ti_sec;
	uint32_t ti_nsec;
	struct trace_pool *trace;
	struct snlua *lua;
};

static void
_stat_begin(struct stat *S, struct timespec *ti) {
	S->count++;
	current_time(ti);
}

inline static void
_stat_end(struct stat *S, struct timespec *ti) {
	diff_time(ti, &S->ti_sec, &S->ti_nsec);
}

static int
_stat(lua_State *L) {
	lua_rawgetp(L, LUA_REGISTRYINDEX, _stat);
	struct stat *S = lua_touserdata(L,-1);
	if (S==NULL) {
		luaL_error(L, "set callback first");
	}
	const char * what = luaL_checkstring(L,1);
	if (strcmp(what,"count")==0) {
		lua_pushinteger(L, S->count);
		return 1;
	}
	if (strcmp(what,"time")==0) {
		double t = (double)S->ti_sec + (double)S->ti_nsec / NANOSEC;
		lua_pushnumber(L, t);
		return 1;
	}
	if (strcmp(what,"trace")==0) {
		lua_pushlightuserdata(L, S->trace);
		return 1;
	}
	return 0;
}

static int
_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
	struct stat *S = ud;
	lua_State *L = S->L;
	struct timespec ti;
	_stat_begin(S, &ti);
	int trace = 1;
	int top = lua_gettop(L);
	if (top == 1) {
		lua_rawgetp(L, LUA_REGISTRYINDEX, _cb);
	} else {
		assert(top == 2);
		lua_pushvalue(L,2);
	}

	lua_pushinteger(L, type);
	lua_pushlightuserdata(L, (void *)msg);
	lua_pushinteger(L,sz);
	lua_pushinteger(L, session);
	lua_pushnumber(L, source);

	int r = lua_pcall(L, 5, 0 , trace);

	_stat_end(S, &ti);
	if (r == LUA_OK) {
		if (S->lua->reload) {
			skynet_callback(context, NULL, 0);
			struct snlua * lua = S->lua;
			assert(lua->L == L);
			const char * cmd = lua->reload;
			lua->reload = NULL;
			lua->L = luaL_newstate();
			int err = lua->init(lua, context, cmd);
			if (err) {<commit_msg>error on untraced time<commit_after>#include "skynet.h"
#include "trace_service.h"
#include "lua-seri.h"
#include "service_lua.h"

#include "luacompat52.h"

#define KNRM  "\x1B[0m"
#define KRED  "\x1B[31m"

#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>

#if defined(__APPLE__)
#include <mach/task.h>
#include <mach/mach.h>
#endif

struct stat {
	lua_State *L;
	int count;
	uint32_t ti_sec;
	uint32_t ti_nsec;
	struct trace_pool *trace;
	struct snlua *lua;
};

static void
_stat_begin(struct stat *S, struct timespec *ti) {
	S->count++;
	current_time(ti);
}

inline static void
_stat_end(struct stat *S, struct timespec *ti) {
	diff_time(ti, &S->ti_sec, &S->ti_nsec);
}

static int
_stat(lua_State *L) {
	lua_rawgetp(L, LUA_REGISTRYINDEX, _stat);
	struct stat *S = lua_touserdata(L,-1);
	if (S==NULL) {
		luaL_error(L, "set callback first");
	}
	const char * what = luaL_checkstring(L,1);
	if (strcmp(what,"count")==0) {
		lua_pushinteger(L, S->count);
		return 1;
	}
	if (strcmp(what,"time")==0) {
		double t = (double)S->ti_sec + (double)S->ti_nsec / NANOSEC;
		lua_pushnumber(L, t);
		return 1;
	}
	if (strcmp(what,"trace")==0) {
		lua_pushlightuserdata(L, S->trace);
		return 1;
	}
	return 0;
}

static int
_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
	struct stat *S = ud;
	lua_State *L = S->L;
	struct timespec ti;
	_stat_begin(S, &ti);
	int trace = 1;
	int top = lua_gettop(L);
	if (top == 1) {
		lua_rawgetp(L, LUA_REGISTRYINDEX, _cb);
	} else {
		assert(top == 2);
		lua_pushvalue(L,2);
	}

	lua_pushinteger(L, type);
	lua_pushlightuserdata(L, (void *)msg);
	lua_pushinteger(L,sz);
	lua_pushinteger(L, session);
	lua_pushnumber(L, source);

	int r = lua_pcall(L, 5, 0 , trace);

	_stat_end(S, &ti);

	struct trace_info *tti = trace_yield(S->trace);
	if (tti) {
		skynet_error(context, "Untraced time %f",  trace_delete(S->trace, tti));
	}

	if (r == LUA_OK) {
		if (S->lua->reload) {
			skynet_callback(context, NULL, 0);
			struct snlua * lua = S->lua;
			assert(lua->L == L);
			const char * cmd = lua->reload;
			lua->reload = NULL;
			lua->L = luaL_newstate();
			int err = lua->init(lua, context, cmd);
			if (err) {
{'fraction_non_alphanumeric': 0.1696997270245678, 'fraction_numerical': 0.013193812556869881, 'mean_word_length': 4.342648845686512, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5360124', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1910, 'n_words': 439}