txsummary.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2016 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "txsummary.hpp"
  14. #include "jlog.hpp"
  15. #include "jutil.hpp"
  16. #include <algorithm>
  17. using std::find_if;
  18. using std::for_each;
  19. #define VALIDATE_KEY(k) if (!(k) || !(*k)) return false
  20. #define MATCH_KEY [&](const Entry& entry) { return stricmp(entry.key.str(), key) == 0; }
  21. bool operator < (const StringAttr& a, const StringAttr& b)
  22. {
  23. return (stricmp(a.str(), b.str()) < 0);
  24. }
  25. CTxSummary::CTxSummary(unsigned creationTime)
  26. : m_creationTime(creationTime ? creationTime : msTick())
  27. {
  28. }
  29. CTxSummary::~CTxSummary()
  30. {
  31. clear();
  32. }
  33. unsigned __int64 CTxSummary::size() const
  34. {
  35. CriticalBlock block(m_sync);
  36. return m_entries.size() + m_timers.size();
  37. }
  38. void CTxSummary::clear()
  39. {
  40. CriticalBlock block(m_sync);
  41. m_entries.clear();
  42. m_timers.clear();
  43. }
  44. unsigned CTxSummary::getElapsedTime() const
  45. {
  46. return msTick() - m_creationTime;
  47. }
  48. bool CTxSummary::contains(const char* key) const
  49. {
  50. CriticalBlock block(m_sync);
  51. return __contains(key);
  52. }
  53. bool CTxSummary::append(const char* key, const char* value, const LogLevel logLevel)
  54. {
  55. VALIDATE_KEY(key);
  56. CriticalBlock block(m_sync);
  57. if (__contains(key))
  58. return false;
  59. m_entries.push_back({key, value, logLevel});
  60. return true;
  61. }
  62. bool CTxSummary::set(const char* key, const char* value, const LogLevel logLevel)
  63. {
  64. VALIDATE_KEY(key);
  65. CriticalBlock block(m_sync);
  66. Entries::iterator it = find_if(m_entries.begin(), m_entries.end(), MATCH_KEY);
  67. if (it != m_entries.end())
  68. {
  69. it->value.set(value);
  70. it->logLevel = logLevel;
  71. }
  72. else
  73. m_entries.push_back({key, value, logLevel});
  74. return true;
  75. }
  76. CumulativeTimer* CTxSummary::queryTimer(const char* name)
  77. {
  78. if (!name || !*name)
  79. return NULL;
  80. CriticalBlock block(m_sync);
  81. TimerValue& timer = m_timers[name];
  82. if (!timer)
  83. {
  84. timer.setown(new CumulativeTimer());
  85. }
  86. return timer;
  87. }
  88. bool CTxSummary::updateTimer(const char* name, unsigned long long delta, const LogLevel logLevel)
  89. {
  90. Owned<CumulativeTimer> timer;
  91. timer.set(queryTimer(name));
  92. if (!timer)
  93. return false;
  94. timer->add(delta);
  95. timer->setLogLevel(logLevel);
  96. return true;
  97. }
  98. void CTxSummary::serialize(StringBuffer& buffer, const LogLevel logLevel) const
  99. {
  100. CriticalBlock block(m_sync);
  101. for (const Entry& entry : m_entries)
  102. {
  103. if (entry.logLevel > logLevel)
  104. continue;
  105. if (entry.value.length())
  106. buffer.appendf("%s=%s;", entry.key.str(), entry.value.str());
  107. else
  108. buffer.appendf("%s;", entry.key.str());
  109. }
  110. for (const std::pair<TimerKey, TimerValue>& entry : m_timers)
  111. {
  112. if (entry.second->getLogLevel() <= logLevel)
  113. buffer.appendf("%s=%" I64F "ums;", entry.first.str(), entry.second->getTotalMillis());
  114. }
  115. }
  116. void CTxSummary::log(const LogLevel logLevel)
  117. {
  118. if (__contains("user") || __contains("req"))
  119. {
  120. StringBuffer summary;
  121. serialize(summary, logLevel);
  122. DBGLOG("TxSummary[%s]", summary.str());
  123. }
  124. }
  125. bool CTxSummary::__contains(const char* key) const
  126. {
  127. return find_if(m_entries.begin(), m_entries.end(), MATCH_KEY) != m_entries.end();
  128. }