sampler.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // Date: Tue Jul 28 18:14:40 CST 2015
  18. #include "butil/time.h"
  19. #include "butil/memory/singleton_on_pthread_once.h"
  20. #include "bvar/reducer.h"
  21. #include "bvar/detail/sampler.h"
  22. #include "bvar/passive_status.h"
  23. #include "bvar/window.h"
  24. namespace bvar {
  25. namespace detail {
  26. const int WARN_NOSLEEP_THRESHOLD = 2;
  27. // Combine two circular linked list into one.
  28. struct CombineSampler {
  29. void operator()(Sampler* & s1, Sampler* s2) const {
  30. if (s2 == NULL) {
  31. return;
  32. }
  33. if (s1 == NULL) {
  34. s1 = s2;
  35. return;
  36. }
  37. s1->InsertBeforeAsList(s2);
  38. }
  39. };
  40. // Call take_sample() of all scheduled samplers.
  41. // This can be done with regular timer thread, but it's way too slow(global
  42. // contention + log(N) heap manipulations). We need it to be super fast so that
  43. // creation overhead of Window<> is negliable.
  44. // The trick is to use Reducer<Sampler*, CombineSampler>. Each Sampler is
  45. // doubly linked, thus we can reduce multiple Samplers into one cicurlarly
  46. // doubly linked list, and multiple lists into larger lists. We create a
  47. // dedicated thread to periodically get_value() which is just the combined
  48. // list of Samplers. Waking through the list and call take_sample().
  49. // If a Sampler needs to be deleted, we just mark it as unused and the
  50. // deletion is taken place in the thread as well.
  51. class SamplerCollector : public bvar::Reducer<Sampler*, CombineSampler> {
  52. public:
  53. SamplerCollector()
  54. : _created(false)
  55. , _stop(false)
  56. , _cumulated_time_us(0) {
  57. create_sampling_thread();
  58. }
  59. ~SamplerCollector() {
  60. if (_created) {
  61. _stop = true;
  62. pthread_join(_tid, NULL);
  63. _created = false;
  64. }
  65. }
  66. private:
  67. // Support for fork:
  68. // * The singleton can be null before forking, the child callback will not
  69. // be registered.
  70. // * If the singleton is not null before forking, the child callback will
  71. // be registered and the sampling thread will be re-created.
  72. // * A forked program can be forked again.
  73. static void child_callback_atfork() {
  74. butil::get_leaky_singleton<SamplerCollector>()->after_forked_as_child();
  75. }
  76. void create_sampling_thread() {
  77. const int rc = pthread_create(&_tid, NULL, sampling_thread, this);
  78. if (rc != 0) {
  79. LOG(FATAL) << "Fail to create sampling_thread, " << berror(rc);
  80. } else {
  81. _created = true;
  82. pthread_atfork(NULL, NULL, child_callback_atfork);
  83. }
  84. }
  85. void after_forked_as_child() {
  86. _created = false;
  87. create_sampling_thread();
  88. }
  89. void run();
  90. static void* sampling_thread(void* arg) {
  91. static_cast<SamplerCollector*>(arg)->run();
  92. return NULL;
  93. }
  94. static double get_cumulated_time(void* arg) {
  95. return static_cast<SamplerCollector*>(arg)->_cumulated_time_us / 1000.0 / 1000.0;
  96. }
  97. private:
  98. bool _created;
  99. bool _stop;
  100. int64_t _cumulated_time_us;
  101. pthread_t _tid;
  102. };
  103. #ifndef UNIT_TEST
  104. static PassiveStatus<double>* s_cumulated_time_bvar = NULL;
  105. static bvar::PerSecond<bvar::PassiveStatus<double> >* s_sampling_thread_usage_bvar = NULL;
  106. #endif
  107. void SamplerCollector::run() {
  108. #ifndef UNIT_TEST
  109. // NOTE:
  110. // * Following vars can't be created on thread's stack since this thread
  111. // may be adandoned at any time after forking.
  112. // * They can't created inside the constructor of SamplerCollector as well,
  113. // which results in deadlock.
  114. if (s_cumulated_time_bvar == NULL) {
  115. s_cumulated_time_bvar =
  116. new PassiveStatus<double>(get_cumulated_time, this);
  117. }
  118. if (s_sampling_thread_usage_bvar == NULL) {
  119. s_sampling_thread_usage_bvar =
  120. new bvar::PerSecond<bvar::PassiveStatus<double> >(
  121. "bvar_sampler_collector_usage", s_cumulated_time_bvar, 10);
  122. }
  123. #endif
  124. butil::LinkNode<Sampler> root;
  125. int consecutive_nosleep = 0;
  126. while (!_stop) {
  127. int64_t abstime = butil::gettimeofday_us();
  128. Sampler* s = this->reset();
  129. if (s) {
  130. s->InsertBeforeAsList(&root);
  131. }
  132. int nremoved = 0;
  133. int nsampled = 0;
  134. for (butil::LinkNode<Sampler>* p = root.next(); p != &root;) {
  135. // We may remove p from the list, save next first.
  136. butil::LinkNode<Sampler>* saved_next = p->next();
  137. Sampler* s = p->value();
  138. s->_mutex.lock();
  139. if (!s->_used) {
  140. s->_mutex.unlock();
  141. p->RemoveFromList();
  142. delete s;
  143. ++nremoved;
  144. } else {
  145. s->take_sample();
  146. s->_mutex.unlock();
  147. ++nsampled;
  148. }
  149. p = saved_next;
  150. }
  151. bool slept = false;
  152. int64_t now = butil::gettimeofday_us();
  153. _cumulated_time_us += now - abstime;
  154. abstime += 1000000L;
  155. while (abstime > now) {
  156. ::usleep(abstime - now);
  157. slept = true;
  158. now = butil::gettimeofday_us();
  159. }
  160. if (slept) {
  161. consecutive_nosleep = 0;
  162. } else {
  163. if (++consecutive_nosleep >= WARN_NOSLEEP_THRESHOLD) {
  164. consecutive_nosleep = 0;
  165. LOG(WARNING) << "bvar is busy at sampling for "
  166. << WARN_NOSLEEP_THRESHOLD << " seconds!";
  167. }
  168. }
  169. }
  170. }
  171. Sampler::Sampler() : _used(true) {}
  172. Sampler::~Sampler() {}
  173. void Sampler::schedule() {
  174. *butil::get_leaky_singleton<SamplerCollector>() << this;
  175. }
  176. void Sampler::destroy() {
  177. _mutex.lock();
  178. _used = false;
  179. _mutex.unlock();
  180. }
  181. } // namespace detail
  182. } // namespace bvar