C++性能测试工具(插桩测试)

使用示例main.cpp

// g++-13  -O3  -std=c++17  main.cpp  profile.cpp
#include <iostream>
#include <chrono>
#include <stdint.h>
#include <mutex>       // std::mutex

#include "profile.h"
#include "profile_rdtsc.h"

std::mutex mutex;

uint64_t getTimeHighResolutionClock() {
    return std::chrono::duration_cast<std::chrono::nanoseconds>(
            std::chrono::high_resolution_clock().now().time_since_epoch()).count();
}

void batchMeasureClockOverhead(){
    constexpr int kLoops = 1000;
    uint64_t t1 = rdtsc();
    for (int i = 0; i < kLoops; ++i) {
        getTimeHighResolutionClock();
    }
    uint64_t t2 = rdtsc();
    std::cout << "Batch test, std::chrono::high_resolution_clock() takes " << (t2 - t1) / kLoops << " cycles on average.\n";
}

PROFILE_REGISTER(single_measure_clock_overhead);
void singleMeasureClockOverhead(){
    PROFILE_CHECK(single_measure_clock_overhead);
    getTimeHighResolutionClock();
}

int main() {
    std::cout << "===Begin of main!===\n";

    getTimeHighResolutionClock();   // warm up

    batchMeasureClockOverhead();
    constexpr int kLoops = 1000;
    for (int i = 0; i < kLoops; ++i) {
        // 这里加锁保护,在多线程中使用此工具需更加注意
        std::lock_guard guard{mutex};
        singleMeasureClockOverhead();
    }

    std::cout << "===End of main!===\n";
}

profile.h

#ifndef PROFILER_PROFILE_H
#define PROFILER_PROFILE_H

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <string_view>
#include <format>
#include <assert.h>

#include "profile_rdtsc.h"

#define PF_DEBUG  // open performance test or not

namespace pf {

    struct ProfileInfo {
        int id_;
        int call_count_;
        uint64_t call_duration_;
        std::string func_name_;
    };

    class Profile {
    private:
        Profile() {
#ifdef PF_DEBUG
            constexpr int kTimes = 16;
            for (int i = 0; i < kTimes; ++i) {
                uint64_t begin = rdtsc_benchmark_begin();
                uint64_t end = rdtsc_benchmark_end();
                if (overhead_ > end - begin) {
                    overhead_ = end - begin;   // maybe not the smallest
                }
            }
#endif
            //std::cout << "Profile ctor called.\n";
        }

        ~Profile() {
#ifdef PF_DEBUG
            std::cout << "---> Performance details begin: <---\n";
            std::cout << "Attention, overhead is " << overhead_
                      << " cycles, and it is removed from average duration.\n";
            for (const auto &item: info_) {
                if (!item.call_count_) {
                    continue;
                }
                double average = static_cast<double>(item.call_duration_) / static_cast<double>(item.call_count_) -
                                 static_cast<double>(overhead_);
                if (average < 0) {
                    average = 0.0;  // time cost is smaller than overhead_, because the overhead_ is not accuracy.
                }
                std::cout << "[" << item.func_name_ << ", called " << item.call_count_ << " times, total duration cycle is "
                          << item.call_duration_ << ", average duration cycle is " << average << ".]" << std::endl;

            }
            std::cout << "---> Performance details end. <---\n";
#endif
        }

        Profile(const Profile &) = delete;

        Profile &operator=(const Profile &) = delete;

    public:
        static Profile &getInstance();

        void addInfo(int id, uint64_t duration) {
            assert(id >= 0 && id < static_cast<int>(info_.size()));
            ++info_[id].call_count_;
            info_[id].call_duration_ += duration;
        }

        int registerFunc(const std::string_view func_name) {
            auto [iter, success] = func_name_.insert(std::string(func_name));
            assert(success && "One function can only be registered once!");
            int func_id = static_cast<int>(info_.size());
            info_.push_back({func_id, 0, 0, *iter});

            return func_id;
        }

    private:
        std::set<std::string> func_name_;
        std::vector<ProfileInfo> info_;
        uint64_t overhead_{~0UL};
    };

    class ProfilingChecker {
    public:
        ProfilingChecker(int id) : id_(id), start_time_(rdtsc_benchmark_begin()) {
            //std::cout << "ProfilingChecker ctor called.\n";
        }

        ~ProfilingChecker() {
            uint64_t end_time = rdtsc_benchmark_end();
            uint64_t duration = end_time - start_time_;
            Profile::getInstance().addInfo(id_, duration);

            //std::cout << "ProfilingChecker dtor called.\n";
        }

        ProfilingChecker(const ProfilingChecker &) = delete;

        ProfilingChecker &operator=(const ProfilingChecker &) = delete;

    private:
        int id_;
        uint64_t start_time_;
    };

    int doProfileRegister(const std::string_view func_name);

#ifdef PF_DEBUG
#define PROFILE_REGISTER(func) static const int _pf_id_##func = pf::doProfileRegister(#func);
#define PROFILE_CHECK(func) pf::ProfilingChecker _checker(_pf_id_##func);
#else
#define PROFILE_REGISTER(func)
#define PROFILE_CHECK(func)
#endif

#ifdef _MSC_VER
#define PROFILE_NOINLINE __declspec(noinline)
#else
#define PROFILE_NOINLINE __attribute__((noinline))
#endif
}

#endif //PROFILER_PROFILE_H

profile.cpp

#include "profile.h"
#include <iostream>

namespace pf {

    Profile &Profile::getInstance() {
        static Profile instance;
        return instance;
    }

    int doProfileRegister(const std::string_view func_name){
        return Profile::getInstance().registerFunc(func_name);
    }
}

profile_rdtsc.h


#ifndef PROFILE_PROFILE_RDTSC_H
#define PROFILE_PROFILE_RDTSC_H

#include <stdint.h>         // uint64_t

#ifndef HAS_HW_RDTSC
#if defined(_M_X64) || defined(_M_IX86) || defined(__x86_64) || defined(__i386)
#define HAS_HW_RDTSC 1
#else
#define HAS_HW_RDTSC 0
#endif
#endif

#if HAS_HW_RDTSC
#ifdef _WIN32
#include <intrin.h>        // __rdtsc/_mm_lfence/_mm_mfence
#elif __has_include(<x86intrin.h>)
#include <x86intrin.h>     // __rdtsc/_mm_lfence/_mm_mfence
#endif
#else
#include <chrono>          // std::chrono::steady_clock/nanoseconds
#endif

// Macro to forbid the compiler from reordering instructions
#ifdef _MSC_VER
#define RDTSC_MEM_BARRIER() _ReadWriteBarrier()
#else
#define RDTSC_MEM_BARRIER() __asm__ __volatile__("" : : : "memory")
#endif

inline uint64_t rdtsc()
{
    RDTSC_MEM_BARRIER();

#if HAS_HW_RDTSC
    uint64_t result = __rdtsc();
#else
    uint64_t result = std::chrono::steady_clock::now().time_since_epoch() /
                      std::chrono::nanoseconds(1);

    //uint64_t result = std::chrono::high_resolution_clock::now().time_since_epoch() /
    //                  std::chrono::nanoseconds(1);
#endif

    RDTSC_MEM_BARRIER();

    return result;
}

#if HAS_HW_RDTSC

inline uint64_t rdtsc_benchmark_begin()
{
    // memory fence, according to x86 architecture,保障开始测试之前让CPU将之前未执行完的指令执行完
    _mm_mfence();
    _mm_lfence();
    uint64_t result = __rdtsc();
    RDTSC_MEM_BARRIER();
    return result;
}

inline uint64_t rdtsc_benchmark_end()
{
    _mm_lfence();
    uint64_t result = __rdtsc();
    RDTSC_MEM_BARRIER();
    return result;
}

#else

inline uint64_t rdtsc_benchmark_begin()
{
    return rdtsc();
}

inline uint64_t rdtsc_benchmark_end()
{
    return rdtsc();
}

#endif

#endif //PROFILE_PROFILE_RDTSC_H

可能的输出结果:
在这里插入图片描述

Reference

C++性能优化高端培训,吴咏炜,http://boolan.com
吴老师个人网站:http://wyw.dcweb.cn
吴老师知乎:https://www.zhihu.com/people/wu-yong-wei-99

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值