├── .travis.yml
├── LICENSE
├── README.md
├── demo.cc
├── dollar.cpp
└── dollar.hpp
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: cpp
2 | sudo: required
3 |
4 | compiler:
5 | - clang
6 | - gcc
7 |
8 | install:
9 | - wget --quiet -O - https://raw.githubusercontent.com/r-lyeh/depot/master/travis.pre.sh | bash -x
10 |
11 | script:
12 | - wget --quiet -O - https://raw.githubusercontent.com/r-lyeh/depot/master/travis.build.sh | bash -x
13 | - wget --quiet -O - https://raw.githubusercontent.com/r-lyeh/depot/master/travis.run.sh | bash -x
14 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2015,2016 r-lyeh (https://github.com/r-lyeh)
2 |
3 | This software is provided 'as-is', without any express or implied
4 | warranty. In no event will the authors be held liable for any damages
5 | arising from the use of this software.
6 |
7 | Permission is granted to anyone to use this software for any purpose,
8 | including commercial applications, and to alter it and redistribute it
9 | freely, subject to the following restrictions:
10 |
11 | 1. The origin of this software must not be misrepresented; you must not
12 | claim that you wrote the original software. If you use this software
13 | in a product, an acknowledgment in the product documentation would be
14 | appreciated but is not required.
15 | 2. Altered source versions must be plainly marked as such, and must not be
16 | misrepresented as being the original software.
17 | 3. This notice may not be removed or altered from any source distribution.
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Dollar :moneybag:
2 |
3 | - [x] Generic instrumented CPU profiler (C++11)
4 | - [x] Ascii results in plain text, Markdown, TSV and CSV formats.
5 | - [x] Compatible JSON traces for Chrome's built-in trace viewer (chrome://tracing).
6 | - [x] Tiny, self-contained, cross-platform, header-only.
7 | - [x] ZLIB/libPNG licensed.
8 |
9 | ## Quick tutorial (tl;dr)
10 | ```c++
11 | #include "dollar.hpp" // dollar is enabled by default. compile with -D$= to disable any profiling
12 | int main() { $ // <-- put a dollar after every curly brace to determinate cpu cost of the scope
13 | for( int x = 0; x < 10000000; ++x ) { $ // <-- functions or loops will apply too
14 | // slow stuff...
15 | }
16 | dollar::text(std::cout); // report stats to std::cout in text format; see also csv(), tsv() and markdown()
17 | dollar::chrome(std::ofstream("chrome.json")); // write tracing results to a json file (for chrome://tracing embedded profiler)
18 | dollar::clear(); // clear all scopes (like when entering a new frame)
19 | }
20 | ```
21 |
22 | ## API
23 | - Determinate the CPU cost of any scope by putting a `$` dollar sign in front of it.
24 | - Or just insert an `dollar raii("name")` object.
25 | - The predefined `$` macro just adds function name, line and number to the RAII object name.
26 | - Use `dollar::text(ostream)`, `dollar::csv(ostream)`, `dollar::tsv(ostream)` or `dollar::markdown(ostream)` to print a text report in any ostream object (like `std::cout`).
27 | - Additionally, use `dollar::chrome(ostream)` to write a chrome://tracing friendly json trace.
28 | - Finally, call `dollar::clear()` when entering a new frame.
29 |
30 | ## API: auto format tool
31 | - @snail23 was generous enough to create this [auto format tool that write dollars for you](https://github.com/snailsoft/format)
32 |
33 | ## Build options
34 | - Dollar is enabled by default. Compile with `-D$=` to disable it.
35 | - Define `DOLLAR_USE_OPENMP` to use OpenMP timers (instead of `` timers)
36 | - Define `DOLLAR_MAX_TRACES` to change number of maximum instrumented samples (default: 512).
37 | - Define `DOLLAR_CPUMETER_WIDTH` to change width of CPU meter bars (default: 10 characters).
38 |
39 | ## Upcoming
40 | - std::vector (graph? opengl?)
41 | - "name %d" format
42 |
43 | ## Showcase
44 | ```c++
45 | ~/dollar> cat ./sample.cc
46 |
47 | #include
48 | #include
49 | #include
50 | #include
51 | #include "dollar.hpp"
52 |
53 | void x( int counter ) { $
54 | while( counter-- > 0 ) { $
55 | std::this_thread::sleep_for( std::chrono::microseconds( int(0.00125 * 1000000) ) );
56 | }
57 | }
58 | void c( int counter ) { $
59 | while( counter-- > 0 ) { $
60 | std::this_thread::sleep_for( std::chrono::microseconds( int(0.00125 * 1000000) ) );
61 | }
62 | }
63 | void y( int counter ) { $
64 | while( counter-- > 0 ) { $
65 | std::this_thread::sleep_for( std::chrono::microseconds( int(0.00125 * 1000000) ) );
66 | if( counter % 2 ) c(counter); else x(counter);
67 | }
68 | }
69 | void a( int counter ) { $
70 | while( counter-- > 0 ) { $
71 | std::this_thread::sleep_for( std::chrono::microseconds( int(0.00125 * 1000000) ) );
72 | y(counter);
73 | }
74 | }
75 |
76 | int main() { $
77 | a(10);
78 |
79 | // write tracing results to a json file (for chrome://tracing embedded profiler)
80 | std::ofstream file("chrome.json");
81 | dollar::chrome(file);
82 |
83 | // display results
84 | dollar::text(std::cout);
85 |
86 | // clear next frame
87 | dollar::clear();
88 | }
89 |
90 | ~/dollar> g++ sample.cc -std=c++11 && ./a.out
91 | 1. +-main (./dollar.hpp:535) [..........] 0.00% CPU ( 0.000ms) 1 hits
92 | 2. +-a (./dollar.hpp:528) [..........] 0.00% CPU ( 0.000ms) 1 hits
93 | 3. +-a (./dollar.hpp:529) [..........] 5.71% CPU ( 20.000ms) 10 hits
94 | 4. +-y (./dollar.hpp:522) [..........] 0.00% CPU ( 0.000ms) 10 hits
95 | 5. +-y (./dollar.hpp:523) [==........] 25.71% CPU ( 90.000ms) 45 hits
96 | 6. |-c (./dollar.hpp:517) [..........] 0.00% CPU ( 0.000ms) 20 hits
97 | 7. | +-c (./dollar.hpp:518) [===.......] 34.29% CPU ( 120.000ms) 60 hits
98 | 8. +-x (./dollar.hpp:512) [..........] 0.00% CPU ( 0.000ms) 25 hits
99 | 9. +-x (./dollar.hpp:513) [===.......] 34.29% CPU ( 120.000ms) 60 hits
100 |
101 | ~/dollar> g++ sample.cc -std=c++11 -D$= && ./a.out
102 | ~/dollar>
103 | ```
104 |
105 | 
106 |
107 | ## Changelog
108 | - v1.2.0 (2016/10/03): Add chrome://tracing profiler support; Project renamed
109 | - v1.1.0 (2016/05/03): New tree view and CPU meters (ofxProfiler style); Smaller implementation;
110 | - v1.0.1 (2015/11/15): Fix win32 `max()` macro conflict
111 | - v1.0.0 (2015/08/02): Macro renamed
112 | - v0.0.0 (2015/03/13): Initial commit
113 |
--------------------------------------------------------------------------------
/demo.cc:
--------------------------------------------------------------------------------
1 | #define DOLLAR_BUILD_DEMO
2 | #include "dollar.hpp"
3 |
--------------------------------------------------------------------------------
/dollar.cpp:
--------------------------------------------------------------------------------
1 | #include "dollar.hpp"
2 |
--------------------------------------------------------------------------------
/dollar.hpp:
--------------------------------------------------------------------------------
1 | // Dollar is a generic instrumented CPU profiler (C++11), header-only and zlib/libpng licensed.
2 | // Dollar outputs traces for chrome:://tracing and also ASCII, CSV, TSV and Markdown text formats.
3 | // - rlyeh ~~ listening to Team Ghost / High hopes.
4 |
5 | /* usage:
6 | #include "dollar.hpp" // dollar is enabled by default. compile with -D$= to disable any profiling
7 | int main() { $ // <-- put a dollar after every curly brace to determinate cpu cost of the scope
8 | for( int x = 0; x < 10000000; ++x ) { $ // <-- functions or loops will apply too
9 | // slow stuff...
10 | }
11 | std::ofstream file("chrome.json");
12 | dollar::chrome(file); // write tracing results to a json file (for chrome://tracing embedded profiler)
13 | dollar::text(std::cout); // report stats to std::cout in text format; see also csv(), tsv() and markdown()
14 | dollar::clear(); // clear all scopes (like when entering a new frame)
15 | }
16 | */
17 |
18 | #pragma once
19 |
20 | #define DOLLAR_VERSION "1.2.0" /* (2016/10/03) Add chrome://tracing profiler support; Project renamed;
21 | #define DOLLAR_VERSION "1.1.0" /* (2016/05/03) New tree view and CPU meters (ofxProfiler style); Smaller implementation;
22 | #define DOLLAR_VERSION "1.0.1" // (2015/11/15) Fix win32 `max()` macro conflict
23 | #define DOLLAR_VERSION "1.0.0" // (2015/08/02) Macro renamed
24 | #define DOLLAR_VERSION "0.0.0" // (2015/03/13) Initial commit */
25 |
26 | #ifdef $
27 |
28 | #include
29 |
30 | namespace dollar {
31 |
32 | inline void csv( std::ostream &cout ) {}
33 | inline void tsv( std::ostream &cout ) {}
34 | inline void text( std::ostream &cout ) {}
35 | inline void chrome( std::ostream &cout ) {}
36 | inline void markdown( std::ostream &cout ) {}
37 | inline void pause( bool paused ) {}
38 | inline bool is_paused() {}
39 | inline void clear() {}
40 |
41 | };
42 |
43 | #else
44 | #include
45 |
46 | #include
47 | #include
48 | #include