Code Monkey home page Code Monkey logo

tsc-benchmark's Introduction

TSC Benchmark

A low overhead nanosecond TSC benchmark which allows you to measure small sections of code to nanosecond accuracy.

Links

Example

benchmarking::TSCBenchmarking benchmark{};
benchmark.Initialize();
auto result = benchmark.Run(code_for_benchmarking, settings);
std::cout << "Benchmark result: " << result.time_ << " ns" << std::endl;

RDTSC And RDTSCP Instructions

The benchmark uses rdtsc instruction and simple arithmatic operations to implement a clock with 1 ns precision, and is much faster and stable in terms of latency in less than 10 ns.

Also, the rdtscp instruction can be used to check that the programm did not switch to another cpu between tsc calls, which can significantly distort the measurements. To check cpu migration during the benchmarks please pass to the TSCBenchmarking template parameter bool CheckCpuMigration the true value.

Hardware Support

The benchmark checks that your /proc/cpuinfo contains nonstop_tsc, constant_tsc. But in general, the TSC, on the all modern x86 systems, runs at constant rate and never stops across all P states.

Also, the benchmark checks whether your system supports Invariant TSC, which can significantly affect the accuracy of measurements.

TSC Reordering

The compiler may reorder the reading of the TSC during benchmark. To avoid this, benchmarking::TSCClock<Barrier BarrierType> class is used, which implements different approaches of barriers:

OneCpuId barrier (default barrier type):

cpuid
rdtsc
code
cpuid
rdtsc

LFence barrier:

cpuid
rdtsc
code
lfence
rdtsc
cpuid

MFence barrier:

cpuid
rdtsc
code
cpuid
rdtsc
mfence

Rdtscp barrier (intel approach):

cpuid
rdtsc
code
rdtscp
cpuid

Four cpuid barrier:

cpuid
rdtsc
cpuid
code
cpuid
rdtsc
cpuid

TSC Overhead

In the benchmarking::TSCBenchmarking::Initialize method, the benchmark prepare and configure the system, calibrates the TSC for accurate results.

In addition, it makes several tests to calculate the overhead from tsc calls, which then needs to be subtracted from the final measured time.

Run Benchmark

After initialization, you can run the benchmark using the benchmarking::TSCBenchmarking::Run method.

This method sets the cpu on which the benchmark will be performed, warm up the benchmark and your code, makes several runs of your code and returns the average time.

In addition, you can use a minimalistic method benchmarking::TSCBenchmarking::MeasureTime of the benchmark. Which does nothing except reading the tsc. This method can be used in the code hot path to take simple measurements first, and then to translate them in another process into a more readable format.

tsc-benchmark's People

Contributors

bagritsevichstepan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.