2015-11-03 12:13:03 -04:00
|
|
|
/*
|
2016-05-21 14:11:15 -03:00
|
|
|
* Copyright (C) 2016 Intel Corporation. All rights reserved.
|
2015-11-03 12:13:03 -04:00
|
|
|
*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-06-25 11:32:25 -03:00
|
|
|
#include <inttypes.h>
|
2016-05-27 09:41:32 -03:00
|
|
|
#include <stdio.h>
|
2015-11-03 12:13:03 -04:00
|
|
|
#include <time.h>
|
2016-05-21 14:11:15 -03:00
|
|
|
#include <vector>
|
2015-11-03 12:13:03 -04:00
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2015-11-03 12:13:03 -04:00
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
|
|
|
|
#include "AP_HAL_Linux.h"
|
|
|
|
#include "Util.h"
|
2016-05-21 14:11:15 -03:00
|
|
|
#include "Perf.h"
|
|
|
|
#include "Perf_Lttng.h"
|
2015-11-03 12:13:03 -04:00
|
|
|
|
2016-07-07 02:10:48 -03:00
|
|
|
#ifndef PRIu64
|
|
|
|
#define PRIu64 "llu"
|
|
|
|
#endif
|
|
|
|
|
2015-11-03 12:13:03 -04:00
|
|
|
using namespace Linux;
|
|
|
|
|
2020-03-01 19:50:34 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2016-05-21 14:11:15 -03:00
|
|
|
|
2019-02-10 14:30:07 -04:00
|
|
|
Perf *Perf::_singleton;
|
2015-11-03 12:13:03 -04:00
|
|
|
|
2016-05-21 14:15:49 -03:00
|
|
|
static inline uint64_t now_nsec()
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
2017-05-05 07:09:29 -03:00
|
|
|
return ts.tv_nsec + (ts.tv_sec * AP_NSEC_PER_SEC);
|
2016-05-21 14:15:49 -03:00
|
|
|
}
|
|
|
|
|
2019-02-10 14:30:07 -04:00
|
|
|
Perf *Perf::get_singleton()
|
2015-11-03 12:13:03 -04:00
|
|
|
{
|
2019-02-10 14:30:07 -04:00
|
|
|
if (!_singleton) {
|
|
|
|
_singleton = new Perf();
|
2015-11-03 12:13:03 -04:00
|
|
|
}
|
|
|
|
|
2019-02-10 14:30:07 -04:00
|
|
|
return _singleton;
|
2016-05-21 14:11:15 -03:00
|
|
|
}
|
2015-11-03 12:13:03 -04:00
|
|
|
|
2016-05-27 09:41:32 -03:00
|
|
|
void Perf::_debug_counters()
|
|
|
|
{
|
|
|
|
uint64_t now = AP_HAL::millis64();
|
|
|
|
|
|
|
|
if (now - _last_debug_msec < 5000) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_rwlock_rdlock(&_perf_counters_lock);
|
|
|
|
unsigned int uc = _update_count;
|
|
|
|
auto v = _perf_counters;
|
|
|
|
pthread_rwlock_unlock(&_perf_counters_lock);
|
|
|
|
|
|
|
|
if (uc != _update_count) {
|
|
|
|
fprintf(stderr, "WARNING!! potentially wrong counters!!!");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &c : v) {
|
|
|
|
if (!c.count) {
|
|
|
|
fprintf(stderr, "%-30s\t"
|
|
|
|
"(no events)\n", c.name);
|
|
|
|
} else if (c.type == Util::PC_ELAPSED) {
|
|
|
|
fprintf(stderr, "%-30s\t"
|
2016-06-25 11:32:25 -03:00
|
|
|
"count: %" PRIu64 "\t"
|
|
|
|
"min: %" PRIu64 "\t"
|
|
|
|
"max: %" PRIu64 "\t"
|
2016-05-27 09:41:32 -03:00
|
|
|
"avg: %.4f\t"
|
|
|
|
"stddev: %.4f\n",
|
2016-05-27 09:55:50 -03:00
|
|
|
c.name, c.count, c.min, c.max, c.avg, sqrt(c.m2));
|
2016-05-27 09:41:32 -03:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%-30s\t"
|
2016-06-25 11:32:25 -03:00
|
|
|
"count: %" PRIu64 "\n",
|
2016-05-27 09:41:32 -03:00
|
|
|
c.name, c.count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_last_debug_msec = now;
|
|
|
|
}
|
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
Perf::Perf()
|
|
|
|
{
|
|
|
|
if (pthread_rwlock_init(&_perf_counters_lock, nullptr) != 0) {
|
|
|
|
AP_HAL::panic("Perf: fail to initialize rw lock");
|
2015-11-03 12:13:03 -04:00
|
|
|
}
|
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
/* TODO: this number should come from vehicle code - just estimate the
|
|
|
|
* number of perf counters for now; if we grow more, it will just
|
|
|
|
* reallocate the memory pool */
|
|
|
|
_perf_counters.reserve(50);
|
2016-05-27 09:41:32 -03:00
|
|
|
|
|
|
|
#ifdef DEBUG_PERF
|
|
|
|
hal.scheduler->register_timer_process(FUNCTOR_BIND_MEMBER(&Perf::_debug_counters, void));
|
|
|
|
#endif
|
2015-11-03 12:13:03 -04:00
|
|
|
}
|
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
void Perf::begin(Util::perf_counter_t pc)
|
2015-11-03 12:13:03 -04:00
|
|
|
{
|
2016-05-21 14:11:15 -03:00
|
|
|
uintptr_t idx = (uintptr_t)pc;
|
2015-11-13 10:08:53 -04:00
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
if (idx >= _perf_counters.size()) {
|
2015-11-13 10:08:53 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-05-21 14:11:15 -03:00
|
|
|
|
|
|
|
Perf_Counter &perf = _perf_counters[idx];
|
|
|
|
if (perf.type != Util::PC_ELAPSED) {
|
|
|
|
hal.console->printf("perf_begin() called on perf_counter_t(%s) that"
|
|
|
|
" is not of PC_ELAPSED type.\n",
|
|
|
|
perf.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (perf.start != 0) {
|
|
|
|
hal.console->printf("perf_begin() called twice on perf_counter_t(%s)\n",
|
|
|
|
perf.name);
|
2015-11-03 12:13:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
_update_count++;
|
|
|
|
|
|
|
|
perf.start = now_nsec();
|
|
|
|
|
2016-05-24 10:26:26 -03:00
|
|
|
perf.lttng.begin(perf.name);
|
2015-11-03 12:13:03 -04:00
|
|
|
}
|
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
void Perf::end(Util::perf_counter_t pc)
|
2015-11-03 12:13:03 -04:00
|
|
|
{
|
2016-05-21 14:11:15 -03:00
|
|
|
uintptr_t idx = (uintptr_t)pc;
|
2015-11-03 12:13:03 -04:00
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
if (idx >= _perf_counters.size()) {
|
2015-11-13 10:08:53 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
Perf_Counter &perf = _perf_counters[idx];
|
|
|
|
if (perf.type != Util::PC_ELAPSED) {
|
|
|
|
hal.console->printf("perf_begin() called on perf_counter_t(%s) that"
|
|
|
|
" is not of PC_ELAPSED type.\n",
|
|
|
|
perf.name);
|
2015-11-03 12:13:03 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-05-21 14:11:15 -03:00
|
|
|
|
|
|
|
if (perf.start == 0) {
|
|
|
|
hal.console->printf("perf_begin() called before begin() on perf_counter_t(%s)\n",
|
|
|
|
perf.name);
|
2015-11-03 12:13:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
_update_count++;
|
|
|
|
|
|
|
|
const uint64_t elapsed = now_nsec() - perf.start;
|
|
|
|
perf.count++;
|
|
|
|
perf.total += elapsed;
|
2015-11-03 12:13:03 -04:00
|
|
|
|
2016-05-27 09:55:50 -03:00
|
|
|
if (perf.min > elapsed) {
|
|
|
|
perf.min = elapsed;
|
2015-11-03 12:13:03 -04:00
|
|
|
}
|
|
|
|
|
2016-05-27 09:55:50 -03:00
|
|
|
if (perf.max < elapsed) {
|
|
|
|
perf.max = elapsed;
|
2015-11-03 12:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-05-27 09:55:50 -03:00
|
|
|
* Maintain avg and variance of interval in nanoseconds
|
|
|
|
* Knuth/Welford recursive avg and variance of update intervals (via Wikipedia)
|
2015-11-03 12:13:03 -04:00
|
|
|
* Same implementation of PX4.
|
|
|
|
*/
|
2016-05-27 09:55:50 -03:00
|
|
|
const double delta_intvl = elapsed - perf.avg;
|
|
|
|
perf.avg += (delta_intvl / perf.count);
|
|
|
|
perf.m2 += (delta_intvl * (elapsed - perf.avg));
|
2016-05-21 14:11:15 -03:00
|
|
|
perf.start = 0;
|
2015-11-03 12:13:03 -04:00
|
|
|
|
2016-05-24 10:26:26 -03:00
|
|
|
perf.lttng.end(perf.name);
|
2015-11-03 12:13:03 -04:00
|
|
|
}
|
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
void Perf::count(Util::perf_counter_t pc)
|
2015-11-03 12:13:03 -04:00
|
|
|
{
|
2016-05-21 14:11:15 -03:00
|
|
|
uintptr_t idx = (uintptr_t)pc;
|
2015-11-13 10:08:53 -04:00
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
if (idx >= _perf_counters.size()) {
|
2015-11-13 10:08:53 -04:00
|
|
|
return;
|
|
|
|
}
|
2015-11-03 12:13:03 -04:00
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
Perf_Counter &perf = _perf_counters[idx];
|
|
|
|
if (perf.type != Util::PC_COUNT) {
|
|
|
|
hal.console->printf("perf_begin() called on perf_counter_t(%s) that"
|
|
|
|
" is not of PC_COUNT type.\n",
|
|
|
|
perf.name);
|
2015-11-03 12:13:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-21 14:11:15 -03:00
|
|
|
_update_count++;
|
|
|
|
perf.count++;
|
2015-11-03 12:13:03 -04:00
|
|
|
|
2016-05-24 10:26:26 -03:00
|
|
|
perf.lttng.count(perf.name, perf.count);
|
2016-05-21 14:11:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Util::perf_counter_t Perf::add(Util::perf_counter_type type, const char *name)
|
|
|
|
{
|
|
|
|
if (type != Util::PC_COUNT && type != Util::PC_ELAPSED) {
|
|
|
|
/*
|
|
|
|
* Other perf counters not implemented for now since they are not
|
|
|
|
* used anywhere.
|
|
|
|
*/
|
|
|
|
return (Util::perf_counter_t)(uintptr_t) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_rwlock_wrlock(&_perf_counters_lock);
|
|
|
|
Util::perf_counter_t pc = (Util::perf_counter_t) _perf_counters.size();
|
|
|
|
_perf_counters.emplace_back(type, name);
|
|
|
|
pthread_rwlock_unlock(&_perf_counters_lock);
|
|
|
|
|
|
|
|
return pc;
|
|
|
|
}
|