APM:Libraries
Perf.h
Go to the documentation of this file.
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 
19 #include <atomic>
20 #include <limits.h>
21 #include <pthread.h>
22 #include <vector>
23 
24 #include "AP_HAL_Linux.h"
25 #include "Perf_Lttng.h"
26 #include "Thread.h"
27 #include "Util.h"
28 
29 namespace Linux {
30 
31 class Perf_Counter {
34 
35 public:
36  Perf_Counter(perf_counter_type type_, const char *name_)
37  : name{name_}
38  , type{type_}
39  , min{ULONG_MAX}
40  {
41  }
42 
43  const char *name;
45 
47 
48  uint64_t count;
49 
50  /* Everything below is in nanoseconds */
51  uint64_t start;
52  uint64_t total;
53  uint64_t min;
54  uint64_t max;
55 
56  double avg;
57  double m2;
58 };
59 
60 class Perf {
63 
64 public:
65  ~Perf();
66 
67  static Perf *get_instance();
68 
69  perf_counter_t add(perf_counter_type type, const char *name);
70 
71  void begin(perf_counter_t pc);
72  void end(perf_counter_t pc);
73  void count(perf_counter_t pc);
74 
75  unsigned int get_update_count() { return _update_count; }
76 
77 private:
78  static Perf *_instance;
79 
80  Perf();
81 
82  void _debug_counters();
83 
84  uint64_t _last_debug_msec;
85 
86  std::vector<Perf_Counter> _perf_counters;
87 
88  /* synchronize addition of new perf counters */
89  pthread_rwlock_t _perf_counters_lock;
90 
91  /* allow to check if memory pool has changed */
92  std::atomic<unsigned int> _update_count;
93 };
94 
95 }
std::vector< Perf_Counter > _perf_counters
Definition: Perf.h:86
uint64_t min
Definition: Perf.h:53
uint64_t max
Definition: Perf.h:54
uint64_t count
Definition: Perf.h:48
std::atomic< unsigned int > _update_count
Definition: Perf.h:92
perf_counter_type type
Definition: Perf.h:46
pthread_rwlock_t _perf_counters_lock
Definition: Perf.h:89
AP_HAL::Util::perf_counter_t perf_counter_t
Definition: Perf.h:33
uint64_t start
Definition: Perf.h:51
Perf_Lttng lttng
Definition: Perf.h:44
const char * name
Definition: Perf.h:43
void * perf_counter_t
Definition: Util.h:101
perf_counter_type
Definition: Util.h:96
AP_HAL::Util::perf_counter_t perf_counter_t
Definition: Perf.h:62
uint64_t total
Definition: Perf.h:52
static Perf * _instance
Definition: Perf.h:78
uint64_t _last_debug_msec
Definition: Perf.h:84
unsigned int get_update_count()
Definition: Perf.h:75
Perf_Counter(perf_counter_type type_, const char *name_)
Definition: Perf.h:36
double avg
Definition: Perf.h:56