Perf.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #pragma once
  18. #include <atomic>
  19. #include <limits.h>
  20. #include <pthread.h>
  21. #include <vector>
  22. #include "AP_HAL_Linux.h"
  23. #include "Perf_Lttng.h"
  24. #include "Thread.h"
  25. #include "Util.h"
  26. namespace Linux {
  27. class Perf_Counter {
  28. using perf_counter_type = AP_HAL::Util::perf_counter_type;
  29. using perf_counter_t = AP_HAL::Util::perf_counter_t;
  30. public:
  31. Perf_Counter(perf_counter_type type_, const char *name_)
  32. : name{name_}
  33. , type{type_}
  34. , min{ULONG_MAX}
  35. {
  36. }
  37. const char *name;
  38. Perf_Lttng lttng;
  39. perf_counter_type type;
  40. uint64_t count;
  41. /* Everything below is in nanoseconds */
  42. uint64_t start;
  43. uint64_t total;
  44. uint64_t min;
  45. uint64_t max;
  46. double avg;
  47. double m2;
  48. };
  49. class Perf {
  50. using perf_counter_type = AP_HAL::Util::perf_counter_type;
  51. using perf_counter_t = AP_HAL::Util::perf_counter_t;
  52. public:
  53. ~Perf();
  54. static Perf *get_singleton();
  55. perf_counter_t add(perf_counter_type type, const char *name);
  56. void begin(perf_counter_t pc);
  57. void end(perf_counter_t pc);
  58. void count(perf_counter_t pc);
  59. unsigned int get_update_count() { return _update_count; }
  60. private:
  61. static Perf *_singleton;
  62. Perf();
  63. void _debug_counters();
  64. uint64_t _last_debug_msec;
  65. std::vector<Perf_Counter> _perf_counters;
  66. /* synchronize addition of new perf counters */
  67. pthread_rwlock_t _perf_counters_lock;
  68. /* allow to check if memory pool has changed */
  69. std::atomic<unsigned int> _update_count;
  70. };
  71. }