tracer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #pragma once
  19. #include "header.h"
  20. #include "stat_collector.h"
  21. namespace wwsearch {
  22. enum class TracerType {
  23. kIndexWriterGetCount,
  24. kIndexWriterGetKvCount,
  25. kIndexWriterGetKvSizeBytes,
  26. kIndexWriterGetConsumeTimeUs,
  27. kDocumentWriterPutCount,
  28. kDocumentWriterPutKvCount,
  29. kDocumentWriterPutKvSizeBytes,
  30. kDocumentWriterPutConsumeTimeUs,
  31. kDocumentCount,
  32. kRealInsertKeys,
  33. kGetInvertedTableSubQueryCount,
  34. kGetInvertedTableSubQueryConsumeUs,
  35. kGetDocvalueCountInCollector,
  36. kGetDocvalueConsumeUsInCollector,
  37. kFilterHitCountInCollector,
  38. kFilterConsumeUsInCollector,
  39. kExceedInnerPurgeDocsTotalLimitCount,
  40. kGetStoredocCountInCollector,
  41. kGetStoredocConsumeUsInCollector,
  42. kPostScorerConsumeUsInCollector,
  43. kPostScorerDocCountInCollector,
  44. };
  45. /* SearchTracer aims to trace all time consume and staticstic count
  46. * in wwsearch internal. This should work for each request for user.
  47. * So must notice this is not support for multi-threads, only work
  48. * for single thread.
  49. */
  50. class SearchTracer {
  51. public:
  52. SearchTracer() {}
  53. virtual ~SearchTracer() {}
  54. SearchTracer(const SearchTracer&) = delete;
  55. SearchTracer& operator=(const SearchTracer&) = delete;
  56. void Set(TracerType type, uint64_t cnt) { tracer_cnt_list_[type] = cnt; }
  57. void Add(TracerType type, uint64_t cnt) {
  58. auto iter = tracer_cnt_list_.find(type);
  59. if (iter == tracer_cnt_list_.end()) {
  60. tracer_cnt_list_[type] = cnt;
  61. return;
  62. }
  63. iter->second += cnt;
  64. }
  65. uint64_t Get(TracerType type) { return tracer_cnt_list_[type]; }
  66. void Clear() { tracer_cnt_list_.clear(); }
  67. private:
  68. std::map<TracerType, uint64_t> tracer_cnt_list_;
  69. };
  70. class TimeCostCounter {
  71. private:
  72. uint64_t start_;
  73. uint64_t end_;
  74. uint64_t total_;
  75. public:
  76. TimeCostCounter() : start_(0), end_(0), total_(0) {}
  77. virtual ~TimeCostCounter() {}
  78. void Start() {
  79. total_ += (end_ - start_);
  80. start_ = Time::NowNanos();
  81. end_ = start_;
  82. }
  83. void Stop() { end_ = Time::NowNanos(); }
  84. uint64_t CostNanos() {
  85. Stop();
  86. return (total_ + (end_ - start_));
  87. }
  88. uint64_t CostUs() { return CostNanos() / 1000; }
  89. private:
  90. };
  91. // Stop the timer and update the metric
  92. #define WWSEARCH_PERF_TIMER_STOP(metric) perf_step_timer_##metric.Stop();
  93. #define WWSEARCH_PERF_TIMER_START(metric) perf_step_timer_##metric.Start();
  94. // Declare and set start time of the timer
  95. #define WWSEARCH_PERF_TIMER_GUARD(metric) \
  96. TimeCostCounter perf_step_timer_##metric(&(perf_context.metric)); \
  97. perf_step_timer_##metric.Start();
  98. } // namespace wwsearch