Esempio n. 1
0
        public static void ResumeGC()
        {
            var env = LuaProfiler.mainL;

            if (env != IntPtr.Zero)
            {
                LuaDLL.lua_gc(env, LuaGCOptions.LUA_GCRESTART, 0);
            }
        }
Esempio n. 2
0
        public static void StopGC()
        {
            var env = LuaProfiler.mainL;

            if (env != IntPtr.Zero)
            {
                LuaDLL.lua_gc(env, LuaGCOptions.LUA_GCSTOP, 0);
            }
        }
Esempio n. 3
0
        public static long GetLuaMemory(IntPtr luaState)
        {
            long result = 0;

            result = LuaDLL.lua_gc(luaState, LuaGCOptions.LUA_GCCOUNT, 0);
            result = result * 1024 + LuaDLL.lua_gc(luaState, LuaGCOptions.LUA_GCCOUNTB, 0);

            return(result);
        }
Esempio n. 4
0
        public static void RunGC()
        {
            var env = LuaProfiler.mainL;

            if (env != IntPtr.Zero)
            {
                LuaDLL.lua_gc(env, LuaGCOptions.LUA_GCCOLLECT, 0);
            }
        }
        static int RemoveRefFunInfo(IntPtr L)
        {
            string funName = LuaHook.GetRefString(L, 1);
            string funAddr = LuaHook.GetRefString(L, 2);
            byte   type    = (byte)LuaDLL.lua_tonumber(L, 3);

            LuaProfiler.RemoveRef(funName, funAddr, type);
            return(0);
        }
Esempio n. 6
0
        public static void lua_getglobal(IntPtr L, string name)
        {
#if XLUA
            LuaDLL.xlua_getglobal(L, name);
#elif TOLUA
            LuaDLL.lua_getglobal(L, name);
#elif SLUA
            LuaDLL.lua_getglobal(L, name);
#endif
        }
Esempio n. 7
0
        public static IntPtr lua_tostringptr(IntPtr L, int index, out StrLen len)
        {
#if XLUA
            return(LuaDLL.lua_tolstring(L, index, out len));
#elif TOLUA
            return(LuaDLL.tolua_tolstring(L, index, out len));
#elif SLUA
            return(LuaDLL.luaS_tolstring32(L, index, out len));
#endif
        }
Esempio n. 8
0
        public static int luaL_loadbuffer(IntPtr L, byte[] buff, int size, string name)
        {
#if XLUA
            return(LuaDLL.xluaL_loadbuffer(L, buff, size, name));
#elif TOLUA
            return(LuaDLL.tolua_loadbuffer(L, buff, size, name));
#elif SLUA
            return(SLua.LuaDLLWrapper.luaLS_loadbuffer(L, buff, size, name));
#endif
        }
Esempio n. 9
0
            public static void RefString(IntPtr strPoint, int index, string s, IntPtr L)
            {
                int oldTop = LuaDLL.lua_gettop(L);

                LuaDLL.lua_pushvalue(L, index);
                //把字符串ref了之后就不GC了
                LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
                LuaDLL.lua_settop(L, oldTop);
                stringDict[(long)strPoint] = s;
            }
Esempio n. 10
0
        public static void lua_pushstdcallcfunction(IntPtr L, LuaCSFunction fun)
        {
#if XLUA
            LuaDLL.lua_pushstdcallcfunction(L, fun);
#elif TOLUA
            LuaDLL.tolua_pushcfunction(L, fun);
#elif SLUA
            LuaDLL.lua_pushcfunction(L, fun);
#endif
        }
Esempio n. 11
0
        private static void DoString(IntPtr L)
        {
            const string script  = @"
local function getfunction(level)
    local info = debug.getinfo(level + 1, 'f')
    return info and info.func
end

function setfenv(fn, env)
    if type(fn) == 'number' then fn = getfunction(fn + 1) end
    local i = 1
    while true do
    local name = debug.getupvalue(fn, i)
    if name == '_ENV' then
        debug.upvaluejoin(fn, i, (function()
        return env
        end), 1)
        break
    elseif not name then
        break
    end

    i = i + 1
    end

    return fn
end

function getfenv(fn)
    if type(fn) == 'number' then fn = getfunction(fn + 1) end
    local i = 1
    while true do
    local name, val = debug.getupvalue(fn, i)
    if name == '_ENV' then
        return val
    elseif not name then
        break
    end
    i = i + 1
    end
end
";
            int          oldTop  = LuaDLL.lua_gettop(L);
            int          errFunc = LuaDLL.load_error_func(L, -1);

            byte[] chunk = Encoding.UTF8.GetBytes(script);
            if (LuaDLL.xluaL_loadbuffer(L, chunk, chunk.Length, "env") == 0)
            {
                if (LuaDLL.lua_pcall(L, 0, -1, errFunc) == 0)
                {
                    LuaDLL.lua_remove(L, errFunc);
                }
            }
            LuaDLL.lua_settop(L, oldTop);
        }
Esempio n. 12
0
        public static void Record()
        {
#if XLUA || TOLUA || SLUA
            IntPtr L = LuaProfiler.mainL;
            if (L == IntPtr.Zero)
            {
                return;
            }
            isHook = false;
            ClearRecord();
            int oldTop = LuaDLL.lua_gettop(L);
            LuaLib.lua_getglobal(L, "miku_handle_error");

            LuaLib.lua_getglobal(L, "miku_do_record");
            LuaLib.lua_getglobal(L, "_G");
            LuaDLL.lua_pushstring(L, "");
            LuaDLL.lua_pushstring(L, "_G");
            //recrod
            LuaDLL.lua_newtable(L);
            historyRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
            LuaDLL.lua_getref(L, historyRef);
            //history
            LuaDLL.lua_pushnil(L);
            //null_list
            LuaDLL.lua_newtable(L);

            if (LuaDLL.lua_pcall(L, 6, 0, oldTop + 1) == 0)
            {
                LuaDLL.lua_remove(L, oldTop + 1);
            }
            LuaDLL.lua_settop(L, oldTop);

            oldTop = LuaDLL.lua_gettop(L);
            LuaLib.lua_getglobal(L, "miku_handle_error");

            LuaLib.lua_getglobal(L, "miku_do_record");
            LuaDLL.lua_pushvalue(L, LuaIndexes.LUA_REGISTRYINDEX);
            LuaDLL.lua_pushstring(L, "");
            LuaDLL.lua_pushstring(L, "_R");
            LuaDLL.lua_getref(L, historyRef);
            //history
            LuaDLL.lua_pushnil(L);
            //null_list
            LuaDLL.lua_newtable(L);

            if (LuaDLL.lua_pcall(L, 6, 0, oldTop + 1) == 0)
            {
                LuaDLL.lua_remove(L, oldTop + 1);
            }
            LuaDLL.lua_settop(L, oldTop);

            isHook = true;
#endif
        }
Esempio n. 13
0
        public static void HookUnRef(IntPtr L, int reference)
        {
#if XLUA || TOLUA || SLUA
            if (isHook)
            {
                LuaDLL.lua_getref(L, reference);
                LuaLib.DoRefLuaFun(L, "lua_miku_remove_ref_fun_info");
                LuaDLL.lua_pop(L, 1);
            }
#endif
        }
Esempio n. 14
0
        public static void EndSample(IntPtr luaState)
        {
#if DEBUG
            if (beginSampleMemoryStack.Count <= 0)
            {
                return;
            }
            int    count          = beginSampleMemoryStack.Count;
            Sample sample         = beginSampleMemoryStack[beginSampleMemoryStack.Count - 1];
            long   oldMemoryCount = sample.currentLuaMemory;
            beginSampleMemoryStack.RemoveAt(count - 1);
            long nowMemoryCount = GetLuaMemory(luaState);
            sample.fahter = count > 1 ? beginSampleMemoryStack[count - 2] : null;

            if (!isDeep)
            {
                long delta = nowMemoryCount - oldMemoryCount;

                long tmpDelta = delta;
                if (delta > 0)
                {
                    delta = Math.Max(delta - 32, 0);//byte[0] 的字节占用是40
                    byte[] luagc = new byte[delta];
                }
                for (int i = 0, imax = beginSampleMemoryStack.Count; i < imax; i++)
                {
                    Sample s = beginSampleMemoryStack[i];
                    s.currentLuaMemory       += tmpDelta;
                    beginSampleMemoryStack[i] = s;
                }
                Profiler.EndSample();
            }

            sample.costTime = Time.realtimeSinceStartup - sample.currentTime;
            var gc = nowMemoryCount - sample.realCurrentLuaMemory;
            sample.costGC = gc > 0 ? gc : 0;
            if (beginSampleMemoryStack.Count == 0 && _stableGC)
            {
                LuaLib.lua_gc(luaState, LuaGCOptions.LUA_GCRESTART, 0);
                LuaLib.lua_gc(luaState, LuaGCOptions.LUA_GCCOLLECT, 0);
            }


            if (m_SampleEndAction != null && beginSampleMemoryStack.Count == 0)
            {
                m_SampleEndAction(sample);
            }

            if (sample.fahter == null)
            {
                sample.Restore();
            }
#endif
        }
Esempio n. 15
0
        public static void RefString(IntPtr strPoint, int index, string s, IntPtr L)
        {
#if XLUA || TOLUA || SLUA
            int oldTop = LuaDLL.lua_gettop(L);
            //把字符串ref了之后就不GC了
            LuaLib.lua_getglobal(L, "MikuLuaProfilerStrTb");
            LuaDLL.lua_pushvalue(L, index);
            LuaDLL.lua_insert(L, -2);

            LuaDLL.lua_settop(L, oldTop);
            stringDict[(long)strPoint] = s;
#endif
        }
Esempio n. 16
0
        public static long GetLuaMemory(IntPtr luaState)
        {
            long result = 0;

#if XLUA || TOLUA || SLUA
            if (luaState != IntPtr.Zero)
            {
                result = LuaDLL.lua_gc(luaState, LuaGCOptions.LUA_GCCOUNT, 0);
                result = result * 1024 + LuaDLL.lua_gc(luaState, LuaGCOptions.LUA_GCCOUNTB, 0);
            }
#endif
            return(result);
        }
Esempio n. 17
0
        //创建
        public bool Create(object env, string callback)
        {
            m_luaEnv       = env as LuaEnv;
            m_errorFuncRef = m_luaEnv.errorFuncRef;
            RealStatePtr L    = m_luaEnv.rawL;
            int          nTop = LuaAPI.lua_gettop(L);
            int          nRef = LuaAPI.xlua_getglobal(L, callback);

            m_callbackFuncRef = LuaAPI.luaL_ref(L);
            LuaAPI.lua_settop(L, nTop);

            return(true);
        }
Esempio n. 18
0
        public static void Diff()
        {
#if XLUA || TOLUA || SLUA
            IntPtr L = LuaProfiler.mainL;
            if (L == IntPtr.Zero)
            {
                return;
            }
            isHook = false;

            if (historyRef == -100)
            {
                Debug.LogError("has no history");
                return;
            }

            int oldTop = LuaDLL.lua_gettop(L);
            LuaLib.lua_getglobal(L, "miku_handle_error");

            LuaLib.lua_getglobal(L, "miku_diff");
            LuaDLL.lua_getref(L, historyRef);
            if (LuaDLL.lua_type(L, -1) != LuaTypes.LUA_TTABLE)
            {
                Debug.LogError(LuaDLL.lua_type(L, -1));
                LuaDLL.lua_settop(L, oldTop);
                historyRef = -100;
                return;
            }

            if (LuaDLL.lua_pcall(L, 1, 3, oldTop + 1) == 0)
            {
                LuaDLL.lua_remove(L, oldTop + 1);
            }
            int         nullObjectRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
            int         rmRef         = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
            int         addRef        = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
            LuaDiffInfo ld            = LuaDiffInfo.Create();
            SetNullObject(nullObjectRef, ld.nullRef);
            SetAddOrRm(rmRef, ld.rmRef, ld.rmDetail);
            SetAddOrRm(addRef, ld.addRef, ld.addDetail);

            NetWorkClient.SendMessage(ld);

            LuaDLL.lua_unref(L, nullObjectRef);
            LuaDLL.lua_unref(L, rmRef);
            LuaDLL.lua_unref(L, addRef);
            LuaDLL.lua_settop(L, oldTop);

            isHook = true;
#endif
        }
Esempio n. 19
0
        private static void ClearRecord()
        {
#if XLUA || TOLUA || SLUA
            IntPtr L = LuaProfiler.mainL;
            if (L == IntPtr.Zero)
            {
                return;
            }
            if (historyRef != -100)
            {
                LuaDLL.lua_unref(L, historyRef);
                historyRef = -100;
            }
#endif
        }
Esempio n. 20
0
        public static void Diff()
        {
#if XLUA || TOLUA || SLUA
            IntPtr L = LuaProfiler.mainL;
            if (L == IntPtr.Zero)
            {
                return;
            }
            isHook = false;

            if (historyRef == -100)
            {
                Debug.LogError("has no history");
                return;
            }

            int oldTop = LuaDLL.lua_gettop(L);
            LuaLib.lua_getglobal(L, "miku_handle_error");

            LuaLib.lua_getglobal(L, "miku_diff");
            LuaDLL.lua_getref(L, historyRef);
            if (LuaDLL.lua_type(L, -1) != LuaTypes.LUA_TTABLE)
            {
                Debug.LogError(LuaDLL.lua_type(L, -1));
                LuaDLL.lua_settop(L, oldTop);
                historyRef = -100;
                return;
            }

            if (LuaDLL.lua_pcall(L, 1, 3, oldTop + 1) == 0)
            {
                LuaDLL.lua_remove(L, oldTop + 1);
            }
            int nullObjectRef = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
            int rmRef         = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
            int addRef        = LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);
            LogTable(addRef, "add: ");
            LogTable(rmRef, "rm: ");
            LogNullObject(nullObjectRef);

            LuaDLL.lua_unref(L, nullObjectRef);
            LuaDLL.lua_unref(L, rmRef);
            LuaDLL.lua_unref(L, addRef);
            LuaDLL.lua_settop(L, oldTop);

            isHook = true;
#endif
        }
Esempio n. 21
0
 static int CheckType(IntPtr L)
 {
     if (LuaDLL.lua_isfunction(L, 1))
     {
         LuaDLL.lua_pushnumber(L, 1);
     }
     else if (LuaDLL.lua_istable(L, 1))
     {
         LuaDLL.lua_pushnumber(L, 2);
     }
     else
     {
         LuaDLL.lua_pushnumber(L, 0);
     }
     return(1);
 }
Esempio n. 22
0
        public void OnHandler(byte serverID, ushort msgID, byte[] buffer, int nLen)
        {
            RealStatePtr L        = m_luaEnv.rawL;
            int          err_func = LuaAPI.load_error_func(L, m_errorFuncRef);

            LuaAPI.lua_getref(L, m_callbackFuncRef);
            LuaAPI.xlua_pushinteger(L, serverID);
            LuaAPI.xlua_pushinteger(L, msgID);
            LuaAPI.xlua_pushlstring(L, buffer, nLen);
            int __gen_error = LuaAPI.lua_pcall(L, 6, 0, err_func);

            if (__gen_error != 0)
            {
                m_luaEnv.ThrowExceptionFromError(err_func - 1);
            }
            LuaAPI.lua_settop(L, err_func - 1);
        }
Esempio n. 23
0
            public static string lua_tostring(IntPtr L, int index)
            {
                StrLen strlen;

#if SLUA
                IntPtr str = LuaDLL.luaS_tolstring32(L, index, out strlen);
#else
                IntPtr str = LuaDLL.lua_tolstring(L, index, out strlen);
#endif
                string ret;
                if (!TryGetLuaString(str, out ret))
                {
                    ret = PoxyToString(L, index);
                    RefString(str, index, ret, L);
                }
                return(ret);
            }
        private void WaitDestory()
        {
#if XLUA || TOLUA || SLUA
            desotryCount++;
            if (desotryCount > 10)
            {
                UnityEditor.EditorApplication.update -= WaitDestory;
                if (LuaProfiler.mainL != IntPtr.Zero)
                {
                    LuaDLL.lua_close(LuaProfiler.mainL);
                }
                LuaProfiler.mainL = IntPtr.Zero;
                NetWorkClient.Close();
                desotryCount = 0;
            }
#endif
        }
        public static string GetRefString(IntPtr L, int index)
        {
            StrLen len;
            IntPtr intPtr = LuaLib.lua_tostringptr(L, index, out len);
            string text;

            if (!LuaHook.TryGetLuaString(intPtr, out text))
            {
                text = LuaDLL.lua_tostring(L, index);
                if (!string.IsNullOrEmpty(text))
                {
                    text = string.Intern(text);
                }
                LuaHook.RefString(intPtr, index, text, L);
            }
            return(text);
        }
Esempio n. 26
0
        private static void SetAddOrRm(int refIndex, Dictionary <string, int> dict, Dictionary <string, List <string> > detailDict)
        {
#if XLUA || TOLUA || SLUA
            IntPtr L = LuaProfiler.mainL;
            if (L == IntPtr.Zero)
            {
                return;
            }
            dict.Clear();
            int oldTop = LuaDLL.lua_gettop(L);

            LuaDLL.lua_getref(L, refIndex);
            if (LuaDLL.lua_type(L, -1) != LuaTypes.LUA_TTABLE)
            {
                LuaDLL.lua_pop(L, 1);
                return;
            }
            int t = oldTop + 1;
            LuaDLL.lua_pushnil(L);  /* 第一个 key */
            while (LuaDLL.lua_next(L, t) != 0)
            {
                /* 用一下 'key' (在索引 -2 处) 和 'value' (在索引 -1 处) */
                int key_t = LuaDLL.lua_gettop(L);
                LuaDLL.lua_pushnil(L);  /* 第一个 key */
                string        firstKey   = null;
                List <string> detailList = new List <string>();
                while (LuaDLL.lua_next(L, key_t) != 0)
                {
                    string key = LuaHook.GetRefString(L, -1);
                    if (string.IsNullOrEmpty(firstKey))
                    {
                        firstKey = key;
                    }
                    detailList.Add(key);
                    LuaDLL.lua_pop(L, 1);
                }
                LuaDLL.lua_settop(L, key_t);
                dict[firstKey]       = (int)LuaDLL.lua_type(L, -2);
                detailDict[firstKey] = detailList;
                /* 移除 'value' ;保留 'key' 做下一次迭代 */
                LuaDLL.lua_pop(L, 1);
            }
            LuaDLL.lua_settop(L, oldTop);
#endif
        }
        public TResult Func <TResult>(params SystemObject[] objs)
        {
#if THREAD_SAFE || HOTFIX_ENABLE
            lock (luaEnv.luaEnvLock)
            {
#endif
            var L          = luaEnv.L;
            var translator = luaEnv.translator;
            int oldTop     = LuaAPI.lua_gettop(L);
            int errFunc    = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
            LuaAPI.lua_getref(L, luaReference);
            int paramCount = 0;
            if (objs != null && objs.Length > 0)
            {
                paramCount = objs.Length;
                foreach (var obj in objs)
                {
                    translator.PushByType(L, obj);
                }
            }
            int error = LuaAPI.lua_pcall(L, paramCount, 1, errFunc);
            if (error != 0)
            {
                luaEnv.ThrowExceptionFromError(oldTop);
            }
            TResult ret;
            try
            {
                translator.Get(L, -1, out ret);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                LuaAPI.lua_settop(L, oldTop);
            }
            return(ret);

#if THREAD_SAFE || HOTFIX_ENABLE
        }
#endif
        }
Esempio n. 28
0
        public void Action()
        {
#if THREAD_SAFE || HOTFIX_ENABLE
            lock (luaEnv.luaEnvLock) {
#endif
            var L       = luaEnv.L;
            int oldTop  = LuaAPI.lua_gettop(L);
            int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
            LuaAPI.lua_getref(L, luaReference);
            int error = LuaAPI.lua_pcall(L, 0, 0, errFunc);
            if (error != 0)
            {
                luaEnv.ThrowExceptionFromError(oldTop);
            }
            LuaAPI.lua_settop(L, oldTop);
#if THREAD_SAFE || HOTFIX_ENABLE
        }
#endif
        }
Esempio n. 29
0
            public static void RefString(IntPtr strPoint, int index, string s, IntPtr L)
            {
                //超过缓存上限就等着 释放吧
                if (weakDictionary.Count >= MAX_WEAK_STRING)
                {
                    return;
                }

                int oldTop = LuaLib.lua_gettop(L);

                LuaLib.lua_pushvalue(L, index);
                int refIndex = LuaLib.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX);

                LuaLib.lua_settop(L, oldTop);
                //TODO 这里要不要改下,暂时 XLUA没有 用L查询 LuaEnv的接口
                LuaString ls = new LuaString(refIndex, LuaProfiler.mainEnv, s, strPoint);

                weakDictionary[(long)strPoint] = ls;
            }
Esempio n. 30
0
        public static void __Register(IntPtr L)
        {
            LuaDLL.lua_newtable(L);
            LuaDLL.lua_pushstring(L, "LuaProfiler");
            LuaDLL.lua_newtable(L);

            LuaDLL.lua_pushstring(L, "BeginSample");

            LuaLib.lua_pushstdcallcfunction(L, BeginSample);
            LuaDLL.lua_rawset(L, -3);

            LuaDLL.lua_pushstring(L, "EndSample");
            LuaLib.lua_pushstdcallcfunction(L, EndSample);
            LuaDLL.lua_rawset(L, -3);

            LuaDLL.lua_rawset(L, -3);
            LuaLib.lua_setglobal(L, "MikuLuaProfiler");

            LuaLib.lua_pushstdcallcfunction(L, UnpackReturnValue);
            LuaLib.lua_setglobal(L, "miku_unpack_return_value");

            LuaLib.lua_pushstdcallcfunction(L, AddRefFunInfo);
            LuaLib.lua_setglobal(L, "miku_add_ref_fun_info");

            LuaLib.lua_pushstdcallcfunction(L, RemoveRefFunInfo);
            LuaLib.lua_setglobal(L, "miku_remove_ref_fun_info");

            LuaLib.lua_pushstdcallcfunction(L, CheckType);
            LuaLib.lua_setglobal(L, "miku_check_type");

            LuaLib.lua_pushstdcallcfunction(L, HandleError);
            LuaLib.lua_setglobal(L, "miku_handle_error");

            LuaDLL.lua_newtable(L);
            LuaLib.lua_setglobal(L, "MikuLuaProfilerStrTb");
#if XLUA
            LuaLib.DoString(L, env_script);
#endif
            LuaLib.DoString(L, get_ref_string);
            LuaLib.DoString(L, null_script);
            LuaLib.DoString(L, diff_script);
        }