Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
6.0 KiB

  1. //====== Copyright �, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Maps message types to strings and vice versa
  4. //
  5. //=============================================================================
  6. #ifndef MESSAGELIST_H
  7. #define MESSAGELIST_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. // Protobuf headers interfere with the valve min/max/malloc overrides. so we need to do all
  12. // this funky wrapping to make the include happy.
  13. #include <tier0/valve_minmax_off.h>
  14. #include "google/protobuf/descriptor.h"
  15. #include <tier0/valve_minmax_on.h>
  16. #include "gcsdk/jobtime.h"
  17. namespace GCSDK
  18. {
  19. extern CGCEmitGroup g_EGMessages;
  20. extern const char *PchMsgNameFromEMsg( MsgType_t eMsg );
  21. //-----------------------------------------------------------------------------
  22. // message type flags
  23. //-----------------------------------------------------------------------------
  24. static const int MT_GC = 0x01; // this message is sent to or from a Game Coordinator (will be proxied by servers along the way)
  25. static const int MT_GC_SYSTEM = 0x02; // this message was sent to or from the steam servers as a system message. Clients can't send these
  26. //-----------------------------------------------------------------------------
  27. // Various info about each message type
  28. //-----------------------------------------------------------------------------
  29. struct MsgInfo_t
  30. {
  31. MsgType_t eMsg;
  32. int nFlags;
  33. const char *pchMsgName;
  34. struct Stats_t
  35. {
  36. Stats_t() : nSourceMask(0), nCount( 0 ), uBytes( 0 ) {}
  37. uint32 nSourceMask;
  38. uint32 nCount;
  39. uint64 uBytes;
  40. };
  41. enum EStatsType
  42. {
  43. k_EStatsTypeSent,
  44. k_EStatsTypeReceived,
  45. k_EStatsTypeMultiplexedSends,
  46. k_EStatsTypeMultiplexedSendsRaw,
  47. k_EStatsType_Count
  48. };
  49. enum EStatsGroup
  50. {
  51. k_EStatsGroupGlobal,
  52. k_EStatsGroupProfile,
  53. k_EStatsGroupWindow,
  54. k_EStatsGroup_Count
  55. };
  56. Stats_t stats[ k_EStatsGroup_Count ][ k_EStatsType_Count ];
  57. };
  58. //-----------------------------------------------------------------------------
  59. // Purpose: Using protobuf reflection, bind them into a message list
  60. //-----------------------------------------------------------------------------
  61. void MsgRegistrationFromEnumDescriptor( const ::google::protobuf::EnumDescriptor *pEnumDescriptor, int nTypeMask );
  62. //-----------------------------------------------------------------------------
  63. // manages a hashed list of messages, allowing fast tests for validity and
  64. // info lookup.
  65. //-----------------------------------------------------------------------------
  66. class CMessageList
  67. {
  68. public:
  69. CMessageList();
  70. ~CMessageList();
  71. bool BInit( );
  72. // returns false if a message isn't valid or isn't one of the types specified
  73. // or true if the message is valid. ppMsgName can be NULL.
  74. bool GetMessage( MsgType_t eMsg, const char **ppMsgName, int nTypeMask );
  75. // make stats about sending messages
  76. void TallySendMessage( MsgType_t eMsg, uint32 uMsgSize, uint32 nSourceMask = 0 );
  77. void TallyReceiveMessage( MsgType_t eMsg, uint32 uMsgSize, uint32 nSourceMask = 0);
  78. void TallyMultiplexedMessage( MsgType_t eMsg, uint32 uSent, uint32 cRecipients, uint32 uMsgSize, uint32 nSourceMask = 0 );
  79. // profiling
  80. void EnableProfiling( bool bEnableProfiling );
  81. // print out our stats
  82. void PrintStats( bool bShowAll, bool bSortByFrequency, MsgInfo_t::EStatsGroup eGroup, MsgInfo_t::EStatsType eType, uint32 nSourceMask = 0 ) const;
  83. void PrintMultiplexStats( MsgInfo_t::EStatsGroup eGroup, bool bSortByFrequency, uint32 nSourceMask = 0 ) const;
  84. // Window management - This is similar to profiling in many ways, but is separate so that the base system can monitor traffic rates without
  85. // interfering with profiles.
  86. //called to obtain the totals for the timing window
  87. const MsgInfo_t::Stats_t& GetWindowTotal( MsgInfo_t::EStatsType eType ) const;
  88. //called to reset the window timings
  89. void ResetWindow();
  90. //returns how long this window has been running in microseconds
  91. uint64 GetWindowDuration() const { return GetGroupDuration( MsgInfo_t::k_EStatsGroupWindow ); }
  92. private:
  93. void TallyMessageInternal( MsgInfo_t &msgInfo, MsgInfo_t::EStatsType eBucket, uint32 unMsgSize, uint32 nSourceMask, uint32 cMessages = 1 );
  94. void AssureBucket( int nBucket );
  95. //-----------------------------------------------------------------------------
  96. // given a particular message ID, find out what bucket it would be in,
  97. // as well as which slot in that bucket.
  98. //-----------------------------------------------------------------------------
  99. static int HashMessage( MsgType_t eMsg, int &nSlot )
  100. {
  101. // hash is everything except the lowest nibble,
  102. // because buckets are 16 entries
  103. int nBucket = eMsg / m_kcBucketSize;
  104. nSlot = eMsg % m_kcBucketSize;
  105. return nBucket;
  106. }
  107. short GetMessageIndex( MsgType_t eMsg );
  108. //given a group, this will return the time that the stats have been collected over
  109. uint64 GetGroupDuration( MsgInfo_t::EStatsGroup eGroup ) const;
  110. private:
  111. //totalled stats for the current window. It would be too costly to total these all the time
  112. MsgInfo_t::Stats_t m_WindowTotals[ MsgInfo_t::k_EStatsType_Count ];
  113. //the time that we have been collecting each of these buckets
  114. CJobTime m_sCollectTime[ MsgInfo_t::k_EStatsGroup_Count ];
  115. //are we currently actively tracking a profile?
  116. bool m_bProfiling;
  117. //the duration of the last finished profile (if it is no longer running, otherwise use the timer)
  118. uint64 m_ulProfileMicrosecs;
  119. CUtlVector< short* > m_vecMessageInfoBuckets;
  120. CUtlVector<MsgInfo_t> m_vecMsgInfo;
  121. static const int m_kcBucketSize = 16;
  122. };
  123. extern CMessageList g_theMessageList;
  124. //-----------------------------------------------------------------------------
  125. // Purpose: Returns the true if the specified message is a valid GC system message.
  126. // Input : eMsg - message type to test
  127. // ppMsgName - Optional pointer to receive message name
  128. //-----------------------------------------------------------------------------
  129. inline bool BIsValidSystemMsg( MsgType_t eMsg, const char **ppMsgName )
  130. {
  131. return g_theMessageList.GetMessage( eMsg, ppMsgName, MT_GC_SYSTEM );
  132. }
  133. } // namespace GCSDK
  134. #endif // MESSAGELIST_H