• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[ANY TFS] Catch scripts that freeze server

Lua:
-- CATCH SCRIPTS THAT COPNSUMES CPU
local log_slow_exec_startExecProg = os.clock()
local log_slow_exec_currExecProg = ""

local log_slow_exec_ReportTime = 0.005 -- where 1 is 1 second and 0.005 is 5 ms

-- enable disable this logger
local ExtendedDebug = true

debug.sethook(function(event, line)
    if ExtendedDebug and log_slow_exec_currExecProg ~= debug.getinfo(2).source then
      if log_slow_exec_currExecProg ~= "" then
        local execTime = os.clock() - log_slow_exec_startExecProg
        if execTime > log_slow_exec_ReportTime then
          print(string.format(">>> long file %s executed in %.3f ms", log_slow_exec_currExecProg, execTime*1000))
        end
      end
    end
    if ExtendedDebug and log_slow_exec_currExecProg ~= debug.getinfo(2).source then
      -- init file changed
      log_slow_exec_startExecProg = os.clock()
      log_slow_exec_currExecProg = debug.getinfo(2).source
      -- print ("source changed: " .. log_slow_exec_currExecProg) -- display source file changed for debugging only
    end
end, "l")

local start = os.time()
local linecount = 0
debug.sethook(function(event, line)
    linecount = linecount + 1
    if os.time() - start >= 1 then
        if linecount >= 1000000 then
            print(string.format("possible infinite loop in file %s near line %s", debug.getinfo(2).source, line))
            debug.sethook()
        end
        linecount = 0
        start = os.time()
    end
end, "l")

ThIs should catch the scripts instantly after server opens or the script must be used in order to check if its looping too long or infinite times?
 
Lua:
-- CATCH SCRIPTS THAT COPNSUMES CPU
local log_slow_exec_startExecProg = os.clock()
local log_slow_exec_currExecProg = ""

local log_slow_exec_ReportTime = 0.005 -- where 1 is 1 second and 0.005 is 5 ms

-- enable disable this logger
local ExtendedDebug = true

debug.sethook(function(event, line)
    if ExtendedDebug and log_slow_exec_currExecProg ~= debug.getinfo(2).source then
      if log_slow_exec_currExecProg ~= "" then
        local execTime = os.clock() - log_slow_exec_startExecProg
        if execTime > log_slow_exec_ReportTime then
          print(string.format(">>> long file %s executed in %.3f ms", log_slow_exec_currExecProg, execTime*1000))
        end
      end
    end
    if ExtendedDebug and log_slow_exec_currExecProg ~= debug.getinfo(2).source then
      -- init file changed
      log_slow_exec_startExecProg = os.clock()
      log_slow_exec_currExecProg = debug.getinfo(2).source
      -- print ("source changed: " .. log_slow_exec_currExecProg) -- display source file changed for debugging only
    end
end, "l")

local start = os.time()
local linecount = 0
debug.sethook(function(event, line)
    linecount = linecount + 1
    if os.time() - start >= 1 then
        if linecount >= 1000000 then
            print(string.format("possible infinite loop in file %s near line %s", debug.getinfo(2).source, line))
            debug.sethook()
        end
        linecount = 0
        start = os.time()
    end
end, "l")

ThIs should catch the scripts instantly after server opens or the script must be used in order to check if its looping too long or infinite times?
The script must be in use, some scripts execute things when they are loaded and others when they are used, in any case it will help you to detect.
It's worth mentioning that this is a debugging trick, I hope you're not considering leaving this code forever on your production server.
 
@Sarah Wesker i'm testing and noticed that have multiple files that are considered like long files most of them are npcs
i have applied all this commits and more
Unless you have a script running at 1000.00 ms. you should'nt be worried. I tracked your post and I think that your memory is too high because:
  • Your map size is too big, start from your own clean data, not from awfull crafted datapacks that are around.
  • You have too much data in your datapack, with this I mean. NPC at 90 ms + 100 ms + 102 ms + 103 ms, and so on. All this in addition makes you consume too much ram. Without even adding the other scripts.
As next step from your develop learning, you gonna need to start learning how to optimize your resources. Why having 200 NPCs that are used to buy and sell the same items, if you can have one for all?, Why using a "realmap" if you're using a low cost virtualization to run your server?, don't expect to have a good performance without knowing the limits of your resources. I think, being very very very honest, that you should start with a very little datapack and map, purely for learning purpose.
 
Unless you have a script running at 1000.00 ms. you should'nt be worried. I tracked your post and I think that your memory is too high because:
  • Your map size is too big, start from your own clean data, not from awfull crafted datapacks that are around.
  • You have too much data in your datapack, with this I mean. NPC at 90 ms + 100 ms + 102 ms + 103 ms, and so on. All this in addition makes you consume too much ram. Without even adding the other scripts.
As next step from your develop learning, you gonna need to start learning how to optimize your resources. Why having 200 NPCs that are used to buy and sell the same items, if you can have one for all?, Why using a "realmap" if you're using a low cost virtualization to run your server?, don't expect to have a good performance without knowing the limits of your resources. I think, being very very very honest, that you should start with a very little datapack and map, purely for learning purpose.
according to "As next step from your develop learning, you gonna need to start learning how to optimize your resources "that's pretty true
host it in my pc 16 ram , ssd disk, intel core 3 11 gen 3.0 ghz 4 corewin 11, use a real map because i like real content. sorry but what doe sthis means? "low cost virtualization"? and regarding to this I think, being very very very honest, that you should start with a very little datapack and map, purely for learning purpose. don't know what i could learn from a minimap liek the ones that comes by default it has so little functions but i going to do something like that because i
would like to release the server and it's features but not with the global/map/actions /Movements etc but with working mosnters pack / spells too fixes custom content etc, as i've stated before. im researching how to do it, without getting my global data files released, that's all i want to keep to myself since i'venn working on this datapack for 3 years or more. other way to ahcieve this is just make a clean branch but people wont be able to view commits and changes and think that sucks :c
 
@Sarah Wesker i'm testing and noticed that have multiple files that are considered like long files most of them are npcs
i have applied all this commits and more
The current TFS library for NPCs is the one that has always been used, it is quite bad, although functional, currently no one has dared to rewrite it.

NPCs are a bit heavy on the CPU since they make use of a lot of methods/functions/tables/ect... and make each npc execution take more milliseconds than it should, although 50 milliseconds is not much, it can be in a performance issue if interacting with hundreds of npc at the same time

It must also be taken into account that this HOOK to calculate the time also adds additional time to the executions, so the results are approximate.
Reporting the execution of a lua file in 5ms as slow or long is quite strange, it's really not slow, considering players interact with npc's at human speed...

There are some files that do consume a lot of time like this: `zombski.lua executed in 5167.
000 ms` I don't know how much this script does to consume 5 seconds xd
 
Back
Top