#include "cc/base/histograms.h"
#include <stdint.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <limits>
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/numerics/safe_conversions.h"
#include "base/synchronization/lock.h"
namespace cc {
namespace {
static const char* g_client_name = nullptr;
static bool g_multiple_client_names_set = false;
base::Lock& GetClientNameLock() {
static base::NoDestructor<base::Lock> client_name_lock;
return *client_name_lock;
}
}
void SetClientNameForMetrics(const char* client_name) {
base::AutoLock auto_lock(GetClientNameLock());
if (g_multiple_client_names_set)
return;
const char* old_client_name = g_client_name;
if (old_client_name && UNSAFE_TODO(strcmp(old_client_name, client_name))) {
g_client_name = nullptr;
g_multiple_client_names_set = true;
LOG(WARNING) << "Started multiple compositor clients (" << old_client_name
<< ", " << client_name
<< ") in one process. Some metrics will be disabled.";
return;
}
if (!old_client_name)
g_client_name = client_name;
}
const char* GetClientNameForMetrics() {
base::AutoLock auto_lock(GetClientNameLock());
return g_client_name;
}
static const int64_t kMinimumTimeMicroseconds = 1;
ScopedUMAHistogramAreaTimerBase::ScopedUMAHistogramAreaTimerBase() : area_(0) {
}
ScopedUMAHistogramAreaTimerBase::~ScopedUMAHistogramAreaTimerBase() = default;
bool ScopedUMAHistogramAreaTimerBase::GetHistogramValues(
Sample32* time_microseconds,
Sample32* pixels_per_ms) const {
return GetHistogramValues(
timer_.Elapsed(), area_.ValueOrDefault(std::numeric_limits<int>::max()),
time_microseconds, pixels_per_ms);
}
bool ScopedUMAHistogramAreaTimerBase::GetHistogramValues(
base::TimeDelta elapsed,
int area,
Sample32* time_microseconds,
Sample32* pixels_per_ms) {
elapsed = std::max(elapsed, base::Microseconds(kMinimumTimeMicroseconds));
double area_per_time = area / elapsed.InMillisecondsF();
if (std::isnan(area_per_time))
return false;
*time_microseconds = base::saturated_cast<Sample32>(elapsed.InMicroseconds());
*pixels_per_ms = base::saturated_cast<Sample32>(area_per_time);
return true;
}
}