Monitor.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. local global = _G.global
  2. local Log = require("common.Log")
  3. local Lang = require("common.Lang")
  4. local MiddleFilter = require("common.MiddleFilter")
  5. local InnerMsg = require("core.InnerMsg")
  6. local Broadcast = require("broadcast.Broadcast")
  7. MONITOR_ENABLE = false
  8. MONITOR_PACKET = MONITOR_PACKET or {} -- 用户包相关
  9. MONITOR_TIMER = MONITOR_ADMIN or {} -- 定时器相关
  10. MONITOR_CUSTOM = MONITOR_CUSTOM or {} -- 自定制
  11. MONITOR_INNER_PACKET = MONITOR_INNER_PACKET or {} -- 内部通信协议相关
  12. local handleTime1 = nil
  13. local handleTime2 = nil
  14. local Msg = require("core.Msg")
  15. function handlePacket(human, container, protoId)
  16. if _G.is_middle then -- 跨服功能受限于白名单配置
  17. if not MiddleFilter.isPermit(protoId) then
  18. if type(human) == "table" then
  19. Broadcast.sendErr(human, Lang.MIDDLE_CG_NOT_IN_WHITE .. " " .. protoId)
  20. end
  21. return
  22. end
  23. else
  24. -- 处于跨服中,本服功能部分屏蔽
  25. if type(human) == "table" and human.db and human.db.middleFlag then
  26. if not MiddleFilter.isPermit(protoId) then
  27. Broadcast.sendErr(human, Lang.MIDDLE_CG_NOT_IN_WHITE2 .. " " .. protoId)
  28. return
  29. end
  30. end
  31. end
  32. if MONITOR_ENABLE then
  33. MONITOR_PACKET[0] = MONITOR_PACKET[0] or {0, 0}
  34. MONITOR_PACKET[protoId] = MONITOR_PACKET[protoId] or {0, 0}
  35. handleTime1 = global.get_usec()
  36. end
  37. -- 调试日志:检查 CG_DRILL_ONE_CLICK_SAODANG 协议
  38. -- if protoId == 1748 or Msg.proto_name[protoId] == "CG_DRILL_ONE_CLICK_SAODANG" then
  39. -- Log.write(Log.LOGID_DEBUG, "[Monitor.handlePacket] 收到 CG_DRILL_ONE_CLICK_SAODANG 协议, protoId="..protoId..", handler存在="..tostring(Msg.proto_handler[protoId] ~= nil))
  40. -- end
  41. if not Msg.proto_handler[protoId] then
  42. -- Log.write(Log.LOGID_DEBUG, "[Monitor.handlePacket] WARNING: protoId="..protoId.." 没有对应的handler, protoName="..tostring(Msg.proto_name[protoId]))
  43. return
  44. end
  45. Msg.proto_handler[protoId](human, container)
  46. if MONITOR_ENABLE then
  47. handleTime2 = global.get_usec()
  48. MONITOR_PACKET[0][1] = MONITOR_PACKET[0][1] + 1 -- 包量
  49. MONITOR_PACKET[0][2] = MONITOR_PACKET[0][2] + handleTime2 - handleTime1 -- 处理时间
  50. MONITOR_PACKET[protoId][1] = MONITOR_PACKET[protoId][1] + 1
  51. MONITOR_PACKET[protoId][2] = MONITOR_PACKET[protoId][2] + handleTime2 - handleTime1
  52. end
  53. end
  54. function handleTimer(func, t, key)
  55. if MONITOR_ENABLE then
  56. handleTime1 = global.get_usec()
  57. end
  58. func(t)
  59. if MONITOR_ENABLE then
  60. MONITOR_TIMER[0] = MONITOR_TIMER[0] or {0, 0}
  61. MONITOR_TIMER[key] = MONITOR_TIMER[key] or {0, 0}
  62. handleTime2 = global.get_usec()
  63. MONITOR_TIMER[0][1] = MONITOR_TIMER[0][1] + 1
  64. MONITOR_TIMER[0][2] = MONITOR_TIMER[0][2] + handleTime2 - handleTime1
  65. MONITOR_TIMER[key][1] = MONITOR_TIMER[key][1] + 1
  66. MONITOR_TIMER[key][2] = MONITOR_TIMER[key][2] + handleTime2 - handleTime1
  67. end
  68. end
  69. local InnerMsg = require("core.InnerMsg")
  70. function handleInnerPacket(fd, container, protoId)
  71. if MONITOR_ENABLE then
  72. MONITOR_INNER_PACKET[0] = MONITOR_INNER_PACKET[0] or {0, 0}
  73. MONITOR_INNER_PACKET[protoId] = MONITOR_INNER_PACKET[protoId] or {0, 0}
  74. handleTime1 = global.get_usec()
  75. end
  76. InnerMsg.proto_handler[protoId](fd, container)
  77. if MONITOR_ENABLE then
  78. handleTime2 = global.get_usec()
  79. MONITOR_INNER_PACKET[0][1] = MONITOR_INNER_PACKET[0][1] + 1 -- 包量
  80. MONITOR_INNER_PACKET[0][2] = MONITOR_INNER_PACKET[0][2] + handleTime2 - handleTime1 -- 处理时间
  81. MONITOR_INNER_PACKET[protoId][1] = MONITOR_INNER_PACKET[protoId][1] + 1
  82. MONITOR_INNER_PACKET[protoId][2] = MONITOR_INNER_PACKET[protoId][2] + handleTime2 - handleTime1
  83. end
  84. end
  85. custom_table_temp = custom_table_temp or {}
  86. function custom1(key)
  87. if not MONITOR_ENABLE then return end
  88. custom_table_temp[key] = global.get_usec()
  89. end
  90. function custom2(key)
  91. if not MONITOR_ENABLE then return end
  92. MONITOR_CUSTOM[key] = MONITOR_CUSTOM[key] or {0, 0}
  93. local time2 = global.get_usec()
  94. MONITOR_CUSTOM[key][1] = MONITOR_CUSTOM[key][1] + 1
  95. MONITOR_CUSTOM[key][2] = MONITOR_CUSTOM[key][2] + time2 - custom_table_temp[key]
  96. end
  97. local function cmp(a, b)
  98. return a[3] > b[3]
  99. end
  100. local function getThreadDesc()
  101. if _G.is_middle then
  102. return "middle"
  103. end
  104. return "logic"
  105. end
  106. local tempTable = {}
  107. local function writeLog(monitor_name, monitor_list, see_count)
  108. for k in pairs(tempTable) do
  109. tempTable[k] = nil
  110. end
  111. local tempCount = 0
  112. for k, v in pairs(monitor_list) do
  113. tempCount = tempCount + 1
  114. tempTable[tempCount] = {k, v[1], v[2]}
  115. end
  116. for k in pairs(monitor_list) do
  117. monitor_list[k] = nil
  118. end
  119. if tempCount > 1 then
  120. table.sort(tempTable, cmp)
  121. end
  122. if not see_count or tempCount > 0 then
  123. local str = monitor_name .. getThreadDesc()
  124. for i = 1, tempCount do
  125. if i > 7 then break end
  126. str = str .. " key:" .. tempTable[i][1] .. " c:" .. tempTable[i][2] .. " t:" .. tempTable[i][3]
  127. end
  128. Log.write(Log.LOGID_MONITOR, str)
  129. end
  130. end
  131. function output()
  132. if not MONITOR_ENABLE then return end
  133. writeLog("TIMER:", MONITOR_TIMER)
  134. writeLog("IO:", MONITOR_PACKET)
  135. writeLog("INNER:", MONITOR_INNER_PACKET)
  136. writeLog("CUSTOM:", MONITOR_CUSTOM, true)
  137. end