|
| 1 | +/* XMRig |
| 2 | + * Copyright (c) 2018-2020 SChernykh <https://github.com/SChernykh> |
| 3 | + * Copyright (c) 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com> |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef XMRIG_PROFILER_H |
| 20 | +#define XMRIG_PROFILER_H |
| 21 | + |
| 22 | + |
| 23 | +#ifndef FORCE_INLINE |
| 24 | +#if defined(_MSC_VER) |
| 25 | +#define FORCE_INLINE __forceinline |
| 26 | +#elif defined(__GNUC__) |
| 27 | +#define FORCE_INLINE __attribute__((always_inline)) inline |
| 28 | +#elif defined(__clang__) |
| 29 | +#define FORCE_INLINE __inline__ |
| 30 | +#else |
| 31 | +#define FORCE_INLINE |
| 32 | +#endif |
| 33 | +#endif |
| 34 | + |
| 35 | + |
| 36 | +#ifdef XMRIG_FEATURE_PROFILING |
| 37 | + |
| 38 | + |
| 39 | +#include <cstdint> |
| 40 | +#include <cstddef> |
| 41 | +#include <type_traits> |
| 42 | + |
| 43 | +#if defined(_MSC_VER) |
| 44 | +#include <intrin.h> |
| 45 | +#endif |
| 46 | + |
| 47 | + |
| 48 | +static FORCE_INLINE uint64_t ReadTSC() |
| 49 | +{ |
| 50 | +#ifdef _MSC_VER |
| 51 | + return __rdtsc(); |
| 52 | +#else |
| 53 | + uint32_t hi, lo; |
| 54 | + __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi)); |
| 55 | + return (((uint64_t)hi) << 32) | lo; |
| 56 | +#endif |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +struct ProfileScopeData |
| 61 | +{ |
| 62 | + const char* m_name; |
| 63 | + uint64_t m_totalCycles; |
| 64 | + uint32_t m_totalSamples; |
| 65 | + |
| 66 | + enum |
| 67 | + { |
| 68 | + MAX_THREAD_ID_LENGTH = 11, |
| 69 | + MAX_SAMPLE_COUNT = 128, |
| 70 | + MAX_DATA_COUNT = 1024 |
| 71 | + }; |
| 72 | + |
| 73 | + char m_threadId[MAX_THREAD_ID_LENGTH + 1]; |
| 74 | + |
| 75 | + static ProfileScopeData* s_data[MAX_DATA_COUNT]; |
| 76 | + static volatile long s_dataCount; |
| 77 | + static double s_tscSpeed; |
| 78 | + |
| 79 | + static void Register(ProfileScopeData* data); |
| 80 | + static void Init(); |
| 81 | +}; |
| 82 | + |
| 83 | +static_assert(std::is_trivial<ProfileScopeData>::value, "ProfileScopeData must be a trivial struct"); |
| 84 | +static_assert(sizeof(ProfileScopeData) <= 32, "ProfileScopeData struct is too big"); |
| 85 | + |
| 86 | + |
| 87 | +class ProfileScope |
| 88 | +{ |
| 89 | +public: |
| 90 | + FORCE_INLINE ProfileScope(ProfileScopeData& data) |
| 91 | + : m_data(data) |
| 92 | + { |
| 93 | + if (m_data.m_totalCycles == 0) { |
| 94 | + ProfileScopeData::Register(&data); |
| 95 | + } |
| 96 | + |
| 97 | + m_startCounter = ReadTSC(); |
| 98 | + } |
| 99 | + |
| 100 | + FORCE_INLINE ~ProfileScope() |
| 101 | + { |
| 102 | + m_data.m_totalCycles += ReadTSC() - m_startCounter; |
| 103 | + ++m_data.m_totalSamples; |
| 104 | + } |
| 105 | + |
| 106 | +private: |
| 107 | + ProfileScopeData& m_data; |
| 108 | + uint64_t m_startCounter; |
| 109 | +}; |
| 110 | + |
| 111 | + |
| 112 | +#define PROFILE_SCOPE(x) static thread_local ProfileScopeData x##_data{#x}; ProfileScope x(x##_data); |
| 113 | + |
| 114 | + |
| 115 | +#else /* XMRIG_FEATURE_PROFILING */ |
| 116 | +#define PROFILE_SCOPE(x) |
| 117 | +#endif /* XMRIG_FEATURE_PROFILING */ |
| 118 | + |
| 119 | + |
| 120 | +#include "crypto/randomx/blake2/blake2.h" |
| 121 | + |
| 122 | + |
| 123 | +struct rx_blake2b_wrapper |
| 124 | +{ |
| 125 | + FORCE_INLINE static void run(void* out, size_t outlen, const void* in, size_t inlen) |
| 126 | + { |
| 127 | + PROFILE_SCOPE(RandomX_Blake2b); |
| 128 | + rx_blake2b(out, outlen, in, inlen); |
| 129 | + } |
| 130 | +}; |
| 131 | + |
| 132 | + |
| 133 | +#endif /* XMRIG_PROFILER_H */ |
0 commit comments