Monitor.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. Msg.proto_handler[protoId](human, container)
  38. if MONITOR_ENABLE then
  39. handleTime2 = global.get_usec()
  40. MONITOR_PACKET[0][1] = MONITOR_PACKET[0][1] + 1 -- 包量
  41. MONITOR_PACKET[0][2] = MONITOR_PACKET[0][2] + handleTime2 - handleTime1 -- 处理时间
  42. MONITOR_PACKET[protoId][1] = MONITOR_PACKET[protoId][1] + 1
  43. MONITOR_PACKET[protoId][2] = MONITOR_PACKET[protoId][2] + handleTime2 - handleTime1
  44. end
  45. end
  46. function handleTimer(func, t, key)
  47. if MONITOR_ENABLE then
  48. handleTime1 = global.get_usec()
  49. end
  50. func(t)
  51. if MONITOR_ENABLE then
  52. MONITOR_TIMER[0] = MONITOR_TIMER[0] or {0, 0}
  53. MONITOR_TIMER[key] = MONITOR_TIMER[key] or {0, 0}
  54. handleTime2 = global.get_usec()
  55. MONITOR_TIMER[0][1] = MONITOR_TIMER[0][1] + 1
  56. MONITOR_TIMER[0][2] = MONITOR_TIMER[0][2] + handleTime2 - handleTime1
  57. MONITOR_TIMER[key][1] = MONITOR_TIMER[key][1] + 1
  58. MONITOR_TIMER[key][2] = MONITOR_TIMER[key][2] + handleTime2 - handleTime1
  59. end
  60. end
  61. local InnerMsg = require("core.InnerMsg")
  62. function handleInnerPacket(fd, container, protoId)
  63. if MONITOR_ENABLE then
  64. MONITOR_INNER_PACKET[0] = MONITOR_INNER_PACKET[0] or {0, 0}
  65. MONITOR_INNER_PACKET[protoId] = MONITOR_INNER_PACKET[protoId] or {0, 0}
  66. handleTime1 = global.get_usec()
  67. end
  68. InnerMsg.proto_handler[protoId](fd, container)
  69. if MONITOR_ENABLE then
  70. handleTime2 = global.get_usec()
  71. MONITOR_INNER_PACKET[0][1] = MONITOR_INNER_PACKET[0][1] + 1 -- 包量
  72. MONITOR_INNER_PACKET[0][2] = MONITOR_INNER_PACKET[0][2] + handleTime2 - handleTime1 -- 处理时间
  73. MONITOR_INNER_PACKET[protoId][1] = MONITOR_INNER_PACKET[protoId][1] + 1
  74. MONITOR_INNER_PACKET[protoId][2] = MONITOR_INNER_PACKET[protoId][2] + handleTime2 - handleTime1
  75. end
  76. end
  77. custom_table_temp = custom_table_temp or {}
  78. function custom1(key)
  79. if not MONITOR_ENABLE then return end
  80. custom_table_temp[key] = global.get_usec()
  81. end
  82. function custom2(key)
  83. if not MONITOR_ENABLE then return end
  84. MONITOR_CUSTOM[key] = MONITOR_CUSTOM[key] or {0, 0}
  85. local time2 = global.get_usec()
  86. MONITOR_CUSTOM[key][1] = MONITOR_CUSTOM[key][1] + 1
  87. MONITOR_CUSTOM[key][2] = MONITOR_CUSTOM[key][2] + time2 - custom_table_temp[key]
  88. end
  89. local function cmp(a, b)
  90. return a[3] > b[3]
  91. end
  92. local function getThreadDesc()
  93. if _G.is_middle then
  94. return "middle"
  95. end
  96. return "logic"
  97. end
  98. local tempTable = {}
  99. local function writeLog(monitor_name, monitor_list, see_count)
  100. for k in pairs(tempTable) do
  101. tempTable[k] = nil
  102. end
  103. local tempCount = 0
  104. for k, v in pairs(monitor_list) do
  105. tempCount = tempCount + 1
  106. tempTable[tempCount] = {k, v[1], v[2]}
  107. end
  108. for k in pairs(monitor_list) do
  109. monitor_list[k] = nil
  110. end
  111. if tempCount > 1 then
  112. table.sort(tempTable, cmp)
  113. end
  114. if not see_count or tempCount > 0 then
  115. local str = monitor_name .. getThreadDesc()
  116. for i = 1, tempCount do
  117. if i > 7 then break end
  118. str = str .. " key:" .. tempTable[i][1] .. " c:" .. tempTable[i][2] .. " t:" .. tempTable[i][3]
  119. end
  120. Log.write(Log.LOGID_MONITOR, str)
  121. end
  122. end
  123. function output()
  124. if not MONITOR_ENABLE then return end
  125. writeLog("TIMER:", MONITOR_TIMER)
  126. writeLog("IO:", MONITOR_PACKET)
  127. writeLog("INNER:", MONITOR_INNER_PACKET)
  128. writeLog("CUSTOM:", MONITOR_CUSTOM, true)
  129. end