2019-09-21 21:13:24 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_PROF_SCOPE_CYCLES_H
|
|
|
|
|
|
|
|
namespace ircd::prof
|
|
|
|
{
|
|
|
|
struct scope_cycles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Count the reference cycles for a scope using the lifetime of this object.
|
2020-06-24 17:41:48 +02:00
|
|
|
/// The result is added to the value in `result`; note that result must be
|
|
|
|
/// initialized.
|
2019-09-21 21:13:24 +02:00
|
|
|
struct ircd::prof::scope_cycles
|
|
|
|
{
|
|
|
|
uint64_t &result;
|
2020-06-24 17:41:48 +02:00
|
|
|
uint64_t started;
|
2019-09-21 21:13:24 +02:00
|
|
|
|
|
|
|
scope_cycles(uint64_t &result) noexcept;
|
2020-06-24 17:41:48 +02:00
|
|
|
scope_cycles(const scope_cycles &) = delete;
|
2019-09-21 21:13:24 +02:00
|
|
|
~scope_cycles() noexcept;
|
|
|
|
};
|
|
|
|
|
2020-02-02 02:57:24 +01:00
|
|
|
#ifdef __clang__
|
|
|
|
inline //TODO: ???
|
|
|
|
#else
|
2019-09-21 21:13:24 +02:00
|
|
|
extern inline
|
|
|
|
__attribute__((flatten, always_inline, gnu_inline, artificial))
|
2020-02-02 02:57:24 +01:00
|
|
|
#endif
|
2019-09-21 21:13:24 +02:00
|
|
|
ircd::prof::scope_cycles::scope_cycles(uint64_t &result)
|
|
|
|
noexcept
|
|
|
|
:result{result}
|
|
|
|
{
|
|
|
|
#if defined(__x86_64__) || defined(__i386__)
|
|
|
|
asm volatile ("mfence");
|
|
|
|
asm volatile ("lfence");
|
|
|
|
#endif
|
|
|
|
|
2020-06-24 17:41:48 +02:00
|
|
|
started = cycles();
|
2019-09-21 21:13:24 +02:00
|
|
|
|
|
|
|
#if defined(__x86_64__) || defined(__i386__)
|
|
|
|
asm volatile ("lfence");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-02-02 02:57:24 +01:00
|
|
|
#ifdef __clang__
|
|
|
|
inline //TODO: ???
|
|
|
|
#else
|
2019-09-21 21:13:24 +02:00
|
|
|
extern inline
|
|
|
|
__attribute__((flatten, always_inline, gnu_inline, artificial))
|
2020-02-02 02:57:24 +01:00
|
|
|
#endif
|
2019-09-21 21:13:24 +02:00
|
|
|
ircd::prof::scope_cycles::~scope_cycles()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
#if defined(__x86_64__) || defined(__i386__)
|
|
|
|
asm volatile ("mfence");
|
|
|
|
asm volatile ("lfence");
|
|
|
|
#endif
|
|
|
|
|
2020-06-24 17:41:48 +02:00
|
|
|
const uint64_t stopped(cycles());
|
|
|
|
result += stopped - started;
|
2019-09-21 21:13:24 +02:00
|
|
|
|
|
|
|
#if defined(__x86_64__) || defined(__i386__)
|
|
|
|
asm volatile ("lfence");
|
|
|
|
#endif
|
|
|
|
}
|