GithubHelp home page GithubHelp logo

pol0 / minitrace Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hrydgard/minitrace

0.0 0.0 0.0 345 KB

Simple C/C++ library for producing JSON traces suitable for Chrome's built-in trace viewer (about:tracing).

License: MIT License

Makefile 2.31% C 37.56% C++ 60.13%

minitrace's Introduction

minitrace

by Henrik Rydgård 2014 ([email protected])

C library with C++ helpers for producing JSON traces suitable for Chrome's excellent built-in trace viewer (chrome://tracing).

Extremely simple to build and use. Tested on Mac and Windows, but should compile anywhere you can use ANSI C with few or no changes.

Sample output (see example code below):

minitrace

Remember to be careful when interpreting the output. This is not a sampling profiler, so it only records start and stop times for blocks. This means that blocks grow even when the CPU is off running another thread, and that it can look like work is being done on more blocks at a time than you have CPUs.

How to use

  1. Include minitrace.c and minitrace.h in your project. #include minitrace.h in some common header.

  2. In your initialization code:

    mtr_init("trace.json");

  3. In your exit code:

    mtr_shutdown();

  4. In all functions you want to profile:

    // C MTR_BEGIN("GFX", "RasterizeTriangle") ... MTR_END("GFX", "RasterizeTriangle")

    // C++ MTR_SCOPE("GFX", "RasterizeTriangle")

  5. In Google Chrome open "about:tracing"

  6. Click Open, and choose your trace.json

  7. Navigate the trace view using the WASD keys, and Look for bottlenecks and optimize your application.

  8. In your final release build, build with

    -DMTR_DISABLE
    

By default, it will collect 1 million tracepoints and then stop. You can change this behaviour, see the top of the header file.

Note: Please only use string literals in MTR statements.

Example code

int main(int argc, const char *argv[]) {
  int i;
  mtr_init("trace.json");

  MTR_META_PROCESS_NAME("minitrace_test");
  MTR_META_THREAD_NAME("main thread");

  int long_running_thing_1;
  int long_running_thing_2;

  MTR_START("background", "long_running", &long_running_thing_1);
  MTR_START("background", "long_running", &long_running_thing_2);

  MTR_BEGIN("main", "outer");
  usleep(80000);
  for (i = 0; i < 3; i++) {
    MTR_BEGIN("main", "inner");
    usleep(40000);
    MTR_END("main", "inner");
    usleep(10000);
  }
  MTR_STEP("background", "long_running", &long_running_thing_1, "middle step");
  usleep(80000);
  MTR_END("main", "outer");

  usleep(50000);
  MTR_INSTANT("main", "the end");
  usleep(10000);
  MTR_FINISH("background", "long_running", &long_running_thing_1);
  MTR_FINISH("background", "long_running", &long_running_thing_2);

  mtr_flush();
  mtr_shutdown();
  return 0;
}

The output will result in something looking a little like the picture at the top of this readme.

Future plans:

  • Builtin background flush thread support with better synchronization, no more fixed limit
  • Support for more trace arguments, more tracing types

If you use this, feel free to tell me how, and what issues you may have had. [email protected]

minitrace's People

Contributors

hrydgard 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.