srsLTE/lib/include/srslte/common/time_prof.h

171 lines
4.1 KiB
C
Raw Normal View History

/*
2020-03-17 05:28:25 -07:00
* Copyright 2013-2020 Software Radio Systems Limited
*
* This file is part of srsLTE.
*
* srsLTE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srsLTE 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 Affero General Public License for more details.
*
* A copy of the GNU Affero General Public License can be found in
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
*/
#ifndef SRSLTE_TIME_PROF_H
#define SRSLTE_TIME_PROF_H
#include "srslte/common/logmap.h"
#include <chrono>
#ifdef ENABLE_TIMEPROF
2020-03-19 03:13:51 -07:00
#define TPROF_ENABLE_DEFAULT true
#else
#define TPROF_ENABLE_DEFAULT false
#endif
2020-03-19 03:13:51 -07:00
namespace srslte {
// individual time interval measure
class tprof_measure
{
public:
using tpoint = std::chrono::time_point<std::chrono::high_resolution_clock>;
2020-03-16 03:25:36 -07:00
2020-03-19 03:13:51 -07:00
tprof_measure() = default;
void start() { t1 = std::chrono::high_resolution_clock::now(); }
std::chrono::nanoseconds stop()
{
auto t2 = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1);
}
private:
tpoint t1;
};
template <typename Prof, bool Enabled = TPROF_ENABLE_DEFAULT>
class tprof
{
public:
template <typename... Args>
explicit tprof(Args&&... args) : prof(std::forward<Args>(args)...)
{
}
2020-03-19 03:13:51 -07:00
void start() { meas.start(); }
std::chrono::nanoseconds stop()
{
2020-03-19 03:13:51 -07:00
auto d = meas.stop();
prof(d);
return d;
}
2020-03-19 03:13:51 -07:00
tprof_measure meas;
Prof prof;
};
2020-03-19 03:13:51 -07:00
// specialization for when the tprof is disabled
template <typename Prof>
2020-03-19 03:13:51 -07:00
class tprof<Prof, false>
{
public:
template <typename... Args>
explicit tprof(Args&&... args)
{
}
void start() {}
std::chrono::nanoseconds stop() { return std::chrono::nanoseconds{0}; }
};
template <typename Prof, bool Enabled = TPROF_ENABLE_DEFAULT>
struct mutexed_tprof {
struct measure {
public:
2020-03-19 03:13:51 -07:00
explicit measure(mutexed_tprof<Prof>* h_) : h(h_) { meas.start(); }
~measure()
{
if (deferred) {
stop();
}
}
std::chrono::nanoseconds stop()
{
2020-03-19 03:13:51 -07:00
auto d = meas.stop();
std::lock_guard<std::mutex> lock(h->mutex);
2020-03-19 03:13:51 -07:00
h->prof(d);
return d;
}
void defer_stop() { deferred = true; }
2020-03-16 03:25:36 -07:00
2020-03-19 03:13:51 -07:00
tprof_measure meas;
mutexed_tprof<Prof>* h;
bool deferred = false;
};
2020-03-16 03:25:36 -07:00
template <typename... Args>
explicit mutexed_tprof(Args&&... args) : prof(std::forward<Args>(args)...)
2020-03-16 03:25:36 -07:00
{
}
measure start() { return measure{this}; }
2020-03-16 03:25:36 -07:00
Prof prof;
private:
std::mutex mutex;
};
template <typename Prof>
2020-03-19 03:13:51 -07:00
struct mutexed_tprof<Prof, false> {
struct measure {
public:
2020-03-19 03:13:51 -07:00
std::chrono::nanoseconds stop() { return std::chrono::nanoseconds{0}; }
void defer_stop() {}
};
template <typename... Args>
explicit mutexed_tprof(Args&&... args)
{
}
measure start() { return measure{}; }
};
struct avg_time_stats {
2020-03-19 03:13:51 -07:00
avg_time_stats(const char* name_, const char* logname, size_t print_period_);
void operator()(std::chrono::nanoseconds duration);
2020-03-19 03:13:51 -07:00
srslte::log_ref log_ptr;
std::string name;
double avg_val = 1;
long count = 0, max_val = 0, min_val = std::numeric_limits<long>::max();
2020-03-17 05:28:25 -07:00
long print_period = 0;
};
template <typename TUnit>
class sliding_window_stats
{
public:
2020-03-19 03:13:51 -07:00
sliding_window_stats(const char* name_, const char* logname, size_t print_period_ = 10);
void operator()(std::chrono::nanoseconds duration);
2020-03-19 03:13:51 -07:00
srslte::log_ref log_ptr;
std::string name;
std::vector<std::chrono::nanoseconds> sliding_window;
size_t window_idx = 0;
};
using sliding_window_stats_ms = sliding_window_stats<std::chrono::milliseconds>;
} // namespace srslte
#endif // SRSLTE_TIME_PROF_H