源战役客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

303 рядки
9.0 KiB

4 тижднів тому
  1. --[[lua obj tool]]
  2. luaobj = luaobj or {}
  3. function luaobj.isobj(obj)
  4. local meta = getmetatable(obj)
  5. if meta ~= nil then
  6. return meta.__is_co_meta or false
  7. end
  8. return false
  9. end
  10. function luaobj.getid(obj)
  11. if luaobj.isobj(obj) then
  12. return obj._iid, obj._cid
  13. end
  14. return 0, 0
  15. end
  16. function luaobj.getclasscountinfo( class_type )
  17. local all_alloc_count = _in_obj_count_map[class_type]
  18. local not_call_del_count = 0
  19. local total_live_count = 0
  20. for k, v in pairs(_in_obj_ins_map[class_type]) do
  21. if not v._use_delete_method then
  22. not_call_del_count = not_call_del_count + 1
  23. end
  24. total_live_count = total_live_count + 1
  25. end
  26. return total_live_count, not_call_del_count, all_alloc_count
  27. end
  28. _snap_luaobj_map = snap_luaobj_map or {}
  29. function luaobj.memtool( action, extend )
  30. local handle_content = action or "info"
  31. local save_file = extend or "console"
  32. local is_use_file = false
  33. if save_file ~= "console" then
  34. is_use_file = true
  35. end
  36. local open_write_func = function()
  37. local tmp_file = nil
  38. local out_func = print
  39. if is_use_file then
  40. tmp_file = io.open(save_file, "w+")
  41. if tmp_file ~= nil then
  42. out_func = function( ... )
  43. tmp_file:write(...)
  44. end
  45. end
  46. end
  47. return tmp_file, out_func
  48. end
  49. local close_write_func = function(write_file)
  50. if write_file ~= nil then
  51. write_file:close()
  52. end
  53. end
  54. if handle_content == "info" then
  55. local tmp_file, out_func = open_write_func()
  56. -- -- print("_in_index_Count = ",_in_index_Count)
  57. local index = 0
  58. -- for k,v in pairs(_in_ctype_map) do
  59. -- t = Split(v._source,"/")
  60. -- class_name = t[#t]
  61. -- class_name = string.gsub(class_name,".lua","")
  62. -- if class_name and LuaMemManager.Instance.agent_list[class_name] then
  63. -- print("not delete agent_list = ",class_name)
  64. -- index = index + 1
  65. -- end
  66. -- end
  67. -- local class_name = ""
  68. -- local t = nil
  69. -- for class_name,v in pairs(LuaMemManager.Instance.agent_list) do
  70. -- if class_name and _in_ctype_map[_G[class_name]] then
  71. -- print("not delete agent_list = ",class_name)
  72. -- index = index + 1
  73. -- end
  74. -- end
  75. -- print("index = ",index)
  76. -- local index = 0
  77. -- -- for pro,v in pairs(_G) do
  78. -- -- if LuaMemManager.Instance.notPreLoadGame_table[pro] then
  79. -- -- print("_G pro=",pro)
  80. -- -- index = index + 1
  81. -- -- end
  82. -- -- end
  83. -- -- print("对象总数:",index)
  84. -- for k,v in pairs(_in_ctype_map) do
  85. -- local total_live, not_del_count, all_alloc_count = luaobj.getclasscountinfo(v)
  86. -- -- if v._source then
  87. -- -- -- print(v._source)
  88. -- -- local t = Split(v._source,"/")
  89. -- -- local class_name = t[#t]
  90. -- -- class_name = string.gsub(class_name,".lua","")
  91. -- -- if LuaMemManager.Instance.notPreLoadGame_table[class_name] then
  92. -- if total_live > 10 then
  93. -- -- if string.find(v._source,"SingleServiceBossEnterView") then
  94. -- out_func(string.format("live num %4d, cid:%8d, not call del num:%4d, all alloc:%8d|src: %s[%d]\n",
  95. -- total_live, k, not_del_count, all_alloc_count, v._source, v._cline))
  96. -- index = index + 1
  97. -- end
  98. -- -- end
  99. -- -- end
  100. -- -- end
  101. -- end
  102. -- print("类对象总数:",index,"实例化对象总数:",_in_obj_ins_id)
  103. -- close_write_func(tmp_file)
  104. -- index = 0
  105. for k,v in pairs(Config) do
  106. -- if _in_index_count_map[k] and _in_index_count_map[k] > 10 then
  107. -- if LuaMemManager.Instance.notPreLoadCfg_table[k] then
  108. print("~~~~~~~~配置属性:",k)
  109. index = index + 1
  110. -- end
  111. -- end
  112. end
  113. print("配置总数:",index)
  114. elseif handle_content == "pmc" then
  115. local tmp_file, out_func = open_write_func()
  116. local index = 0
  117. _test_baseclass_list = _test_baseclass_list or {}
  118. local test_vo = nil
  119. for k,v in pairs(_in_ctype_map) do
  120. test_vo = _test_baseclass_list[k]
  121. local total_live, not_del_count, all_alloc_count = luaobj.getclasscountinfo(v)
  122. if test_vo then
  123. total_live = total_live - test_vo.total_live
  124. not_del_count = not_del_count - test_vo.not_del_count
  125. all_alloc_count = all_alloc_count - test_vo.all_alloc_count
  126. test_vo["total_live"] = total_live + test_vo.total_live
  127. test_vo["not_del_count"] = not_del_count + test_vo.not_del_count
  128. test_vo["all_alloc_count"] = all_alloc_count + test_vo.all_alloc_count
  129. else
  130. test_vo = {}
  131. _test_baseclass_list[k] = test_vo
  132. test_vo["total_live"] = total_live
  133. test_vo["not_del_count"] = not_del_count
  134. test_vo["all_alloc_count"] = all_alloc_count
  135. end
  136. if total_live ~= 0 or not_del_count ~= 0 or all_alloc_count ~= 0 then
  137. out_func(string.format("此过程中创建的类对象src: %s[%d]增加了:%4d个,其中存活的 %2d个,没有调用deleteMe的:%2d个\n",
  138. v._source, v._cline,all_alloc_count,total_live, not_del_count))
  139. index = index + 1
  140. end
  141. end
  142. print("类对象总数:",index)
  143. index = 0
  144. _test_config_list = _test_config_list or {}
  145. for k,v in pairs(Config) do
  146. if LuaMemManager.Instance.notPreLoadCfg_table[k] and _test_config_list[k] == nil then
  147. print("~~~~~~~~配置属性:",k)
  148. _test_config_list[k] = true
  149. index = index + 1
  150. else
  151. _test_config_list[k] = nil
  152. end
  153. end
  154. print("配置总数:",index)
  155. elseif handle_content == "snap" then
  156. _snap_luaobj_map = {}
  157. for k,v in pairs(_in_ctype_map) do
  158. local total_live, not_del_count, all_alloc_count = luaobj.getclasscountinfo(v)
  159. _snap_luaobj_map[k] = total_live
  160. end
  161. print("Lua memory tools: handle snap action suc!")
  162. elseif handle_content == "diff" then
  163. local tmp_file, out_func = open_write_func()
  164. for k,v in pairs(_in_ctype_map) do
  165. local total_live, not_del_count, all_alloc_count = luaobj.getclasscountinfo(v)
  166. local old_live_num = _snap_luaobj_map[k] or 0
  167. if old_live_num < total_live then
  168. out_func(string.format("add_num:%4d, cid:%8d | src: %s[%d]\n", total_live - old_live_num, k, v._source, v._cline))
  169. end
  170. end
  171. close_write_func(tmp_file)
  172. print("Lua memory tools: handle diff action suc!")
  173. elseif handle_content == "search" then
  174. if extend ~= nil then
  175. for k,v in pairs(_in_ctype_map) do
  176. if string.find(v._source, extend) ~= nil then
  177. print(string.format("search class info: cid = %8d, source = %s[%d]\n", k, v._source, v._cline))
  178. end
  179. end
  180. end
  181. print("Lua memory tools: handle search action suc!")
  182. elseif handle_content == "list" then
  183. if extend ~= nil then
  184. local val = tonumber(extend)
  185. local ctype = _in_ctype_map[val]
  186. if ctype ~= nil then
  187. for k, v in pairs(_in_obj_ins_map[ctype]) do
  188. print(string.format("list live obj: id = %8d, is_del = %d", k, v._use_delete_method and 1 or 0))
  189. end
  190. end
  191. end
  192. else
  193. print("Lua memory tools: error args when call lua mem tool!")
  194. end
  195. end
  196. --[[lua debug tool]]
  197. luaobj_debug_tool = luaobj_debug_tool or {}
  198. function luaobj_debug_tool.is_lua_class_type( obj )
  199. return obj.__is_cc_type or false
  200. end
  201. function luaobj_debug_tool.get_lua_class_type_vtable( obj )
  202. local meta = getmetatable(obj)
  203. return meta.__index
  204. end
  205. function luaobj_debug_tool.detect_lua_obj_in_val( v, id, is_instance_id, path_desc, detect_list )
  206. --print("now detect", v, path_desc)
  207. local type_name = type(v)
  208. if type_name == "table" then
  209. if not detect_list[v] then
  210. if luaobj.isobj(v) then
  211. local new_path_desc = path_desc .."[luaobj]"
  212. local obj_id, obj_cid = luaobj.getid(v)
  213. if is_instance_id then
  214. if obj_id == id then
  215. print("Find Obj Path", path_desc)
  216. end
  217. else
  218. if obj_cid == id then
  219. print("Find Obj Path", path_desc)
  220. end
  221. end
  222. local delegate_tbl = v
  223. --print("delegate table is:")
  224. --PrintTable(delegate_tbl)
  225. luaobj_debug_tool.search_obj_in_table(delegate_tbl, id, is_instance_id, new_path_desc, detect_list)
  226. detect_list[v] = true
  227. else
  228. luaobj_debug_tool.search_obj_in_table(v, id, is_instance_id, path_desc, detect_list)
  229. if luaobj_debug_tool.is_lua_class_type(v) then
  230. luaobj_debug_tool.search_obj_in_table(luaobj_debug_tool.get_lua_class_type_vtable(v), id, is_instance_id, path_desc, detect_list)
  231. end
  232. end
  233. end
  234. elseif type_name == "function" then
  235. detect_list[v] = true
  236. local func_path_desc = path_desc .. "[function]"
  237. local up_index = 1
  238. while true do
  239. local up_val_name, up_val = debug.getupvalue(v, up_index)
  240. if up_val == nil then
  241. break
  242. end
  243. local new_path_desc = func_path_desc .. "->[up(" .. up_val_name .. ")]"
  244. --print("up value detect", new_path_desc)
  245. luaobj_debug_tool.detect_lua_obj_in_val(up_val, id, is_instance_id, new_path_desc, detect_list)
  246. up_index = up_index + 1
  247. end
  248. end
  249. end
  250. function luaobj_debug_tool.search_obj_in_table( tbl, id, is_instance_id, path_desc, detect_list)
  251. --print("now detect", tbl, path_desc)
  252. -- body
  253. if detect_list[tbl] then
  254. return
  255. end
  256. detect_list[tbl] = true
  257. for k, v in pairs(tbl) do
  258. --io.stderr:write(tostring(k))
  259. local new_path_desc = path_desc .. "->" .. tostring(k)
  260. luaobj_debug_tool.detect_lua_obj_in_val(v, id, is_instance_id, new_path_desc, detect_list)
  261. new_path_desc = path_desc .. "->[key]" .. tostring(k)
  262. luaobj_debug_tool.detect_lua_obj_in_val(k, id, is_instance_id, new_path_desc, detect_list)
  263. end
  264. end