bandwidth.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "bandwidth.h"
  2. #include "global.h"
  3. #include "services/stdServiceImpl.h"
  4. using namespace std;
  5. using namespace uniString;
  6. using namespace stringUtil;
  7. #define DEBUG_LOG(...) do { if (gOptions.statsDebug()) DLOG(__VA_ARGS__); } while (0)
  8. #define LOGNAME "[BANDWIDTH] "
  9. static __uint64 g_bandWidthTable[bandWidth::ALL_OTHER];
  10. static AOL_namespace::rwLock g_bandwidthLock;
  11. const __uint64 bandWidth::getAmount(const bandWidth::usageType_t type)
  12. {
  13. stackRWLock sl (g_bandwidthLock);
  14. if (type < ALL_SENT)
  15. {
  16. return g_bandWidthTable[type];
  17. }
  18. switch (type)
  19. {
  20. case bandWidth::ALL_SENT:
  21. {
  22. return g_bandWidthTable[CLIENT_V1_SENT] +
  23. g_bandWidthTable[CLIENT_V2_SENT] +
  24. g_bandWidthTable[CLIENT_HTTP_SENT] +
  25. g_bandWidthTable[CLIENT_FLV_SENT] +
  26. g_bandWidthTable[CLIENT_M4A_SENT] +
  27. g_bandWidthTable[SOURCE_V1_SENT] +
  28. g_bandWidthTable[SOURCE_V2_SENT] +
  29. g_bandWidthTable[PUBLIC_WEB] +
  30. g_bandWidthTable[PRIVATE_WEB] +
  31. g_bandWidthTable[FLASH_POLICY] +
  32. g_bandWidthTable[RELAY_V2_SENT]+
  33. g_bandWidthTable[YP_SENT] +
  34. g_bandWidthTable[AUTH_AND_METRICS];
  35. }
  36. case bandWidth::ALL_RECV:
  37. {
  38. return g_bandWidthTable[SOURCE_V1_RECV] +
  39. g_bandWidthTable[SOURCE_V2_RECV] +
  40. g_bandWidthTable[RELAY_MISC_RECV] +
  41. g_bandWidthTable[RELAY_V1_RECV] +
  42. g_bandWidthTable[RELAY_V2_RECV] +
  43. g_bandWidthTable[YP_RECV] +
  44. g_bandWidthTable[ADVERTS];
  45. }
  46. case bandWidth::ALL_WEB:
  47. {
  48. return g_bandWidthTable[PUBLIC_WEB] +
  49. g_bandWidthTable[PRIVATE_WEB];
  50. }
  51. case bandWidth::ALL_SOURCE_SENT:
  52. {
  53. return g_bandWidthTable[SOURCE_V1_SENT] +
  54. g_bandWidthTable[SOURCE_V2_SENT];
  55. }
  56. case bandWidth::ALL_SOURCE_RECV:
  57. {
  58. return g_bandWidthTable[SOURCE_V1_RECV] +
  59. g_bandWidthTable[SOURCE_V2_RECV];
  60. }
  61. case bandWidth::ALL_CLIENT_SENT:
  62. {
  63. return g_bandWidthTable[CLIENT_V1_SENT] +
  64. g_bandWidthTable[CLIENT_V2_SENT] +
  65. g_bandWidthTable[CLIENT_HTTP_SENT] +
  66. g_bandWidthTable[CLIENT_FLV_SENT] +
  67. g_bandWidthTable[CLIENT_M4A_SENT];
  68. }
  69. case bandWidth::ALL_RELAY_RECV:
  70. {
  71. return g_bandWidthTable[RELAY_MISC_RECV] +
  72. g_bandWidthTable[RELAY_V1_RECV] +
  73. g_bandWidthTable[RELAY_V2_RECV];
  74. }
  75. case bandWidth::ALL_OTHER:
  76. {
  77. return g_bandWidthTable[FLASH_POLICY] +
  78. g_bandWidthTable[RELAY_V2_SENT] +
  79. g_bandWidthTable[YP_SENT] +
  80. g_bandWidthTable[YP_RECV] +
  81. g_bandWidthTable[AUTH_AND_METRICS] +
  82. g_bandWidthTable[ADVERTS];
  83. }
  84. default:
  85. {
  86. return 0;
  87. }
  88. }
  89. }
  90. void bandWidth::updateAmount(const bandWidth::usageType_t type, const __uint64 amount)
  91. {
  92. if (amount > 0)
  93. {
  94. stackRWLock sl (g_bandwidthLock, false);
  95. g_bandWidthTable[type] += amount;
  96. g_bandWidthTable[bandWidth::ALL] += amount;
  97. }
  98. }
  99. void bandWidth::getFinalAmounts()
  100. {
  101. __uint64 all = getAmount(bandWidth::ALL);
  102. if (all > 0)
  103. {
  104. ILOG(LOGNAME "Total: " + tos(all) +
  105. ", Sent: " + tos(getAmount(bandWidth::ALL_SENT)) +
  106. ", Recv: " + tos(getAmount(bandWidth::ALL_RECV)));
  107. }
  108. }