Perf.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * This file is free software: you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This file is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. * See the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <inttypes.h>
  18. #include <stdio.h>
  19. #include <time.h>
  20. #include <vector>
  21. #include <AP_HAL/AP_HAL.h>
  22. #include <AP_Math/AP_Math.h>
  23. #include "AP_HAL_Linux.h"
  24. #include "Util.h"
  25. #include "Perf.h"
  26. #include "Perf_Lttng.h"
  27. #ifndef PRIu64
  28. #define PRIu64 "llu"
  29. #endif
  30. using namespace Linux;
  31. static const AP_HAL::HAL &hal = AP_HAL::get_HAL();
  32. Perf *Perf::_singleton;
  33. static inline uint64_t now_nsec()
  34. {
  35. struct timespec ts;
  36. clock_gettime(CLOCK_MONOTONIC, &ts);
  37. return ts.tv_nsec + (ts.tv_sec * AP_NSEC_PER_SEC);
  38. }
  39. Perf *Perf::get_singleton()
  40. {
  41. if (!_singleton) {
  42. _singleton = new Perf();
  43. }
  44. return _singleton;
  45. }
  46. void Perf::_debug_counters()
  47. {
  48. uint64_t now = AP_HAL::millis64();
  49. if (now - _last_debug_msec < 5000) {
  50. return;
  51. }
  52. pthread_rwlock_rdlock(&_perf_counters_lock);
  53. unsigned int uc = _update_count;
  54. auto v = _perf_counters;
  55. pthread_rwlock_unlock(&_perf_counters_lock);
  56. if (uc != _update_count) {
  57. fprintf(stderr, "WARNING!! potentially wrong counters!!!");
  58. }
  59. for (auto &c : v) {
  60. if (!c.count) {
  61. fprintf(stderr, "%-30s\t"
  62. "(no events)\n", c.name);
  63. } else if (c.type == Util::PC_ELAPSED) {
  64. fprintf(stderr, "%-30s\t"
  65. "count: %" PRIu64 "\t"
  66. "min: %" PRIu64 "\t"
  67. "max: %" PRIu64 "\t"
  68. "avg: %.4f\t"
  69. "stddev: %.4f\n",
  70. c.name, c.count, c.min, c.max, c.avg, sqrt(c.m2));
  71. } else {
  72. fprintf(stderr, "%-30s\t"
  73. "count: %" PRIu64 "\n",
  74. c.name, c.count);
  75. }
  76. }
  77. _last_debug_msec = now;
  78. }
  79. Perf::Perf()
  80. {
  81. if (pthread_rwlock_init(&_perf_counters_lock, nullptr) != 0) {
  82. AP_HAL::panic("Perf: fail to initialize rw lock");
  83. }
  84. /* TODO: this number should come from vehicle code - just estimate the
  85. * number of perf counters for now; if we grow more, it will just
  86. * reallocate the memory pool */
  87. _perf_counters.reserve(50);
  88. #ifdef DEBUG_PERF
  89. hal.scheduler->register_timer_process(FUNCTOR_BIND_MEMBER(&Perf::_debug_counters, void));
  90. #endif
  91. }
  92. void Perf::begin(Util::perf_counter_t pc)
  93. {
  94. uintptr_t idx = (uintptr_t)pc;
  95. if (idx >= _perf_counters.size()) {
  96. return;
  97. }
  98. Perf_Counter &perf = _perf_counters[idx];
  99. if (perf.type != Util::PC_ELAPSED) {
  100. hal.console->printf("perf_begin() called on perf_counter_t(%s) that"
  101. " is not of PC_ELAPSED type.\n",
  102. perf.name);
  103. return;
  104. }
  105. if (perf.start != 0) {
  106. hal.console->printf("perf_begin() called twice on perf_counter_t(%s)\n",
  107. perf.name);
  108. return;
  109. }
  110. _update_count++;
  111. perf.start = now_nsec();
  112. perf.lttng.begin(perf.name);
  113. }
  114. void Perf::end(Util::perf_counter_t pc)
  115. {
  116. uintptr_t idx = (uintptr_t)pc;
  117. if (idx >= _perf_counters.size()) {
  118. return;
  119. }
  120. Perf_Counter &perf = _perf_counters[idx];
  121. if (perf.type != Util::PC_ELAPSED) {
  122. hal.console->printf("perf_begin() called on perf_counter_t(%s) that"
  123. " is not of PC_ELAPSED type.\n",
  124. perf.name);
  125. return;
  126. }
  127. if (perf.start == 0) {
  128. hal.console->printf("perf_begin() called before begin() on perf_counter_t(%s)\n",
  129. perf.name);
  130. return;
  131. }
  132. _update_count++;
  133. const uint64_t elapsed = now_nsec() - perf.start;
  134. perf.count++;
  135. perf.total += elapsed;
  136. if (perf.min > elapsed) {
  137. perf.min = elapsed;
  138. }
  139. if (perf.max < elapsed) {
  140. perf.max = elapsed;
  141. }
  142. /*
  143. * Maintain avg and variance of interval in nanoseconds
  144. * Knuth/Welford recursive avg and variance of update intervals (via Wikipedia)
  145. * Same implementation of PX4.
  146. */
  147. const double delta_intvl = elapsed - perf.avg;
  148. perf.avg += (delta_intvl / perf.count);
  149. perf.m2 += (delta_intvl * (elapsed - perf.avg));
  150. perf.start = 0;
  151. perf.lttng.end(perf.name);
  152. }
  153. void Perf::count(Util::perf_counter_t pc)
  154. {
  155. uintptr_t idx = (uintptr_t)pc;
  156. if (idx >= _perf_counters.size()) {
  157. return;
  158. }
  159. Perf_Counter &perf = _perf_counters[idx];
  160. if (perf.type != Util::PC_COUNT) {
  161. hal.console->printf("perf_begin() called on perf_counter_t(%s) that"
  162. " is not of PC_COUNT type.\n",
  163. perf.name);
  164. return;
  165. }
  166. _update_count++;
  167. perf.count++;
  168. perf.lttng.count(perf.name, perf.count);
  169. }
  170. Util::perf_counter_t Perf::add(Util::perf_counter_type type, const char *name)
  171. {
  172. if (type != Util::PC_COUNT && type != Util::PC_ELAPSED) {
  173. /*
  174. * Other perf counters not implemented for now since they are not
  175. * used anywhere.
  176. */
  177. return (Util::perf_counter_t)(uintptr_t) -1;
  178. }
  179. pthread_rwlock_wrlock(&_perf_counters_lock);
  180. Util::perf_counter_t pc = (Util::perf_counter_t) _perf_counters.size();
  181. _perf_counters.emplace_back(type, name);
  182. pthread_rwlock_unlock(&_perf_counters_lock);
  183. return pc;
  184. }