public
Created

My failing callgrind parser

  • Download Gist
CallgrindParser.cxx
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
/* CallgrindParser
Copyright (C) 2013 BlackBerry.
 
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
 
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
 
Author: Niall Douglas <ndouglas@blackberry.com>
Created: April 2013.
*/
 
#define BOOST_SPIRIT_DEBUG
#define _SCL_SECURE_NO_WARNINGS
 
#include "CallgrindParser.hpp"
#include <boost/mpl/vector.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/support_line_pos_iterator.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/spirit/include/support_utree.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>
#include <boost/spirit/include/phoenix_container.hpp>
 
namespace CallgrindParser
{
using namespace std;
using namespace boost;
using namespace boost::spirit;
 
enum token_ids
{
ID_TAG = boost::spirit::lex::min_token_id+1,
ID_TEXT,
ID_COMMENT,
ID_WHITESPACE,
ID_EOL,
ID_INTEGER,
ID_DOUBLE,
ID_OPERATOR,
ID_PUNCTUATOR
};
 
template<typename Lexer> struct callgrindoutput_tokens : lex::lexer<Lexer>
{
lex::token_def<std::string> tag;
lex::token_def<std::string> text;
lex::token_def<std::string> comment;
lex::token_def<lex::omit> white_space;
lex::token_def<lex::omit> eol;
lex::token_def<int> integer_value;
lex::token_def<int> hex_value;
lex::token_def<double> float_value;
lex::token_def<double> float_value2;
lex::token_def<std::string> operator_;
lex::token_def<> punctuator;
 
callgrindoutput_tokens()
: tag ("^[a-zA-Z_][a-zA-Z0-9_]*:")
, text ("[a-zA-Z0-9_.,@]+")
, comment ("#[^\\n]+")
, white_space ("[ \\t]+")
, eol ("\\n")
, integer_value ("[0-9][0-9]*")
, hex_value ("0[xX][0-9a-fA-F]+")
, float_value ("[0-9]*\\.[0-9]+([eE][+-]?[0-9]+)?")
, float_value2 ("[0-9]+\\.([eE][+-]?[0-9]+)?")
, operator_ ("\\(|\\)|\\*|\\+|-|\\/|%|:|=") // ( ) * + - / % : =
, punctuator ("\\[|\\]|\\(|\\)|\\.|\\*|\\+|-|~|!|\\/|%|<<|>>|<|>|<=|>=|==|!=|\\^|&|\\||&&|\\|\\||\\?|:|,|=")// [ ] ( ) . * + - ~ ! / % << >> < > <= >= == != ^ & | && || ? : , =
{
this->self.add
(tag , ID_TAG)
(comment , ID_COMMENT)
(eol , ID_EOL)
(float_value2 , ID_DOUBLE)
(float_value , ID_DOUBLE)
(hex_value , ID_INTEGER)
(integer_value, ID_INTEGER)
(operator_ , ID_OPERATOR)
(text , ID_TEXT);
 
this->self("WS") = white_space;
}
};
 
template<typename Iterator, typename Skipper> struct callgrindoutput_grammar : qi::grammar<Iterator, Skipper>
{
qi::rule<Iterator, Skipper> output;
qi::rule<Iterator, utree(), Skipper> restofline;
qi::rule<Iterator, int(), Skipper> num_int;
qi::rule<Iterator, double(), Skipper> num_float;
qi::rule<Iterator, string(), Skipper> operator_, comment, text;
 
qi::rule<Iterator, Skipper> knowntagvalues;
qi::rule<Iterator, utree(), Skipper> unknowntagvalue;
 
int version;
string cmd;
vector<string> desc, event;
string positions, events, summary;
 
template<typename TokenDef> callgrindoutput_grammar(const TokenDef &tok) : callgrindoutput_grammar::base_type(output), version(0)
{
using boost::spirit::_val;
num_int=tok.hex_value | tok.integer_value;
num_float=tok.float_value2 | tok.float_value;
operator_=tok.operator_;
comment=tok.comment;
text=tok.text;
 
restofline=*(
num_float
| num_int
| operator_
| comment
| text
); // [ std::cout << phoenix::val("restofline '") << _1 << "'\n" ];
 
unknowntagvalue=(tok.tag >> restofline);
knowntagvalues = (
(tok.tag [ qi::_pass = (_1 == "version:") ] >> tok.integer_value [ phoenix::ref(version) = _1 ])
| (tok.tag [ qi::_pass = (_1 == "cmd:") ] >> restofline)
);
 
output= +(
(knowntagvalues | unknowntagvalue [ std::cout << phoenix::val("unknown tag '") << _val << "'\n" ]
| restofline
) >> qi::token(ID_EOL)
);
BOOST_SPIRIT_DEBUG_NODE(knowntagvalues);
//BOOST_SPIRIT_DEBUG_NODE(restofline);
//BOOST_SPIRIT_DEBUG_NODE(unknowntagvalue);
//BOOST_SPIRIT_DEBUG_NODE(output);
}
};
 
std::unique_ptr<CallgrindOutput> ParseCallgrindOutput(std::istream &s)
{
std::unique_ptr<CallgrindOutput> ret(new CallgrindOutput);
typedef istreambuf_iterator<char> __base_iterator_type;
typedef multi_pass<__base_iterator_type> _base_iterator_type;
//typedef line_pos_iterator<_base_iterator_type> base_iterator_type; // TODO: This breaks the forward iterator concept check, and it shouldn't.
typedef _base_iterator_type base_iterator_type;
base_iterator_type first(make_default_multi_pass(__base_iterator_type(s))), start(first), last(make_default_multi_pass(__base_iterator_type()));
 
typedef lex::lexertl::token<base_iterator_type, mpl::vector<string, int, double>> token_type;
typedef lex::lexertl::lexer<token_type> lexer_type;
typedef callgrindoutput_tokens<lexer_type> tokens_type;
typedef qi::in_state_skipper<tokens_type::lexer_def> skipper_type;
typedef tokens_type::iterator_type iterator_type;
tokens_type lexer;
callgrindoutput_grammar<iterator_type, skipper_type> grammar(lexer);
#if 1
last=first;
for(size_t n=0; n<1024; n++, ++last);
#endif
lex::tokenize_and_phrase_parse(first, last, lexer, grammar, qi::in_state( "WS" )[ lexer.self ]);
if(first!=last)
{
string where;
base_iterator_type end(first);
for(size_t n=0; n<64; n++, ++end);
where.append(first, end);
throw runtime_error("Parsing callgrind output failed at line "+to_string(get_line(first))+":"+to_string(get_column(get_line_start(start, first), first))+" "+where);
}
return ret;
}
 
} // namespace
CallgrindParser.hpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/* CallgrindParser
Copyright (C) 2013 BlackBerry.
 
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
 
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
 
Author: Niall Douglas <ndouglas@blackberry.com>
Created: April 2013.
*/
 
#ifndef CALLGRINDPARSER_H
#define CALLGRINDPARSER_H
 
#include "../NiallsCPP11Utilities/NiallsCPP11Utilities.hpp"
#include "../NiallsCPP11Utilities/std_filesystem.hpp"
 
#ifdef _USRDLL
#ifdef CALLGRINDPARSER_DLL_EXPORTS
#define CALLGRINDPARSER_API DLLEXPORTMARKUP
#else
#define CALLGRINDPARSER_API DLLIMPORTMARKUP
#endif
#else
#define CALLGRINDPARSER_API
#endif
 
namespace CallgrindParser
{
class CALLGRINDPARSER_API CallgrindOutput
{
};
//! Parses a callgrind output from stream \em s
extern CALLGRINDPARSER_API std::unique_ptr<CallgrindOutput> ParseCallgrindOutput(std::istream &s);
//! Parses a callgrind output from path \em p
inline std::unique_ptr<CallgrindOutput> ParseCallgrindOutput(std::filesystem::path p)
{
using namespace std::filesystem;
return ParseCallgrindOutput(ifstream(p));
}
 
} // namespace
 
#endif
Sample callgrind output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
version: 1
creator: callgrind-3.8.1
pid: 27324
cmd: cachegrind/GenericCPUConfigDetect/GenericCPUConfigDetect/test
part: 1
 
 
desc: I1 cache: 32768 B, 64 B, 8-way associative, 157 picosec hit latency
desc: D1 cache: 32768 B, 64 B, 8-way associative, 157 picosec hit latency
desc: LL cache: 6291456 B, 64 B, 12-way associative, 1120 picosec hit latency
desc: main memory: 1362 picosec hit latency
desc: max instruct cost: 316 picosec
desc: min instruct cost: 107 picosec
 
event: EPpsec = 316 Ir + 1120 I1mr + 1120 D1mr + 1120 D1mw + 1362 ILmr + 1362 DLmr + 1362 DLmw
event: EPpsec : Estimated Possible Picosecs
 
desc: Timerange: Basic block 0 - 666942660
desc: Trigger: Program termination
 
positions: instr line
events: Ir Dr Dw I1mr D1mr D1mw ILmr DLmr DLmw Bc Bcm Bi Bim AcCost1 SpLoss1 AcCost2 SpLoss2 sysCount sysTime
summary: 10763752557 3340873136 6188138 1252 379847215 66721 1242 190430756 66509 572457016 3682334 4034895 513 0 0 0 0 2016394 559
 
 
ob=(3) /lib/x86_64-linux-gnu/libc-2.15.so
fl=(73) /build/buildd/eglibc-2.15/setjmp/../sysdeps/x86_64/bsd-_setjmp.S
fn=(362) _setjmp
0x36270 34 1 0 0 1 0 0 1 0 0 0 0 0 0 166 48
+2 +2 1
cfi=(74) /build/buildd/eglibc-2.15/setjmp/../sysdeps/x86_64/setjmp.S
cfn=(364) __sigsetjmp
calls=1 0x361d0 -10
* * 27 6 10 2 0 0 2 0 0 1 1
 
fl=(79) /build/buildd/eglibc-2.15/malloc/malloc.c
fn=(394) _int_malloc
0x80290 3415 1 0 1 1 0 0 1 0 0 0 0 0 0 62 16
+2 * 1 0 1
+2 * 1 0 1
+2 * 1
+3 * 1 0 1
+2 * 1 0 1
+1 * 1 0 1
+1 * 1
+3 * 1
+4 +30 1
+4 * 1 0 0 0 0 0 0 0 0 1
+6 * 1
+4 * 1
+5 * 1
+3 * 1
+4 * 1 0 0 1 0 0 1 0 0 0 0 0 0 250 44
+4 * 1
+4 +8 1 1
+7 * 1 0 0 0 0 0 0 0 0 1 1
jcnd=1/1 0x80378 +36
* *
0x80363 3898 1
+4 * 1
+3 * 1 1
+1 * 1 1
+1 * 1 1
+2 * 1 1
+2 * 1 1
+2 * 1 1
+2 * 1 1
+4 3489 1 0 0 1 0 0 1 0 0 0 0 0 0 90 36
+7 * 1 0 0 1 0 0 1 0 0 1 1 0 0 1000 62
jcnd=1/1 +99 +41
* *
+99 +41 1 0 0 1 0 0 1 0 0 0 0 0 0 125 34
+3 * 1
+4 * 1
+4 * 1 0 0 0 0 0 0 0 0 1
+6 * 1
+3 * 1
+4 * 1
+4 * 1 0 0 1 0 0 1 0 0 1 1 0 0 100 20
jcnd=1/1 0x80a9f *
* *
+10 +1 1 1
+3 * 1
+4 * 1
+3 * 1 0 0 0 0 0 0 0 0 1
+2 4215 1 1
+8 * 1
+7 * 1 0 0 0 0 0 0 0 0 1 1
jcnd=1/1 0x80a40 1913
* *
+24 3885 1
+3 -2 1 0 0 1 0 0 1 0 0 0 0 0 0 58
+3 3570 1
+4 3885 1
+4 -2 1
+4 * 1
+3 +2 1
+4 -2 1 0 1
+5 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2
+5 +1 127
+4 -2 127
+7 +2 127 0 127 0 0 32 0 0 32 0 0 0 0 3986 8 3986 8
+4 * 127 0 127
+4 -2 127 0 0 0 0 0 0 0 0 127 9
jcnd=126/127 -24 +1
* *
+2 +6 1
+7 * 1
+3 * 1 0 0 0 0 0 0 0 0 1 1
jcnd=1/1 0x80e20 +4
* *
+9 +7 1
+4 -2 1
+3 * 1 0 1
+3 +2 1 0 1 0 0 1 0 0 1 0 0 0 0 500 56 500 56
+4 * 1
jump=1 0x8043b 3885
* *
+36 3530 1 0 0 1 0 0 1 0 0 0 0 0 0 125 30
+3 * 1
+4 * 1
+4 * 1 0 0 0 0 0 0 0 0 1
+6 * 1
+3 * 1
+4 * 1
+4 * 1 0 0 1 0 0 1 0 0 1 1 0 0 1000 62
jcnd=1/1 0x80c46 *
* *
0x80c46 * 1 0 0 1 0 0 1 0 0 0 0 0 0 142 34
+3 * 1
+6 * 1
+4 * 1
+3 * 1
+4 * 1
+4 * 1
jump=1 0x80408 +1
* *
0x80d53 2370 1 1 0 1 1 0 1 0 0 0 0 0 0 1090 74
+7 +1 1
+3 * 1
+3 * 1
+3 -1 1 1 0 0 1 0 0 0 0 0 0 0 0 200 52
+4 * 1 0 1
+5 * 1
+4 +11 1 1
+7 -11 1 0 1
+5 +11 1 0 0 0 0 0 0 0 0 1
+2 * 1 1 0 1 1 0 1 1 0 0 0 0 0 200 58 100 36
+6 * 1 1
+6 * 1 0 0 0 0 0 0 0 0 1
+2 +13 1 1
+5 * 1 1
+5 * 1
+5 * 1
+3 * 1
+3 +4 1
+3 * 1 0 0 0 0 0 0 0 0 1 1
jcnd=1/1 0x814b0 +2
* *
0x80e20 1922 1 0 1 1 0 0 1 0 0 0 0 0 0 500 48
+11 * 1
jump=1 0x80a6d +3
* *
0x80f25 3893 1 0 0 1 0 0 1 0 0 0 0 0 0 142 36
+3 * 1
+3 * 1 0 0 0 0 0 0 0 0 1
+6 * 1 1 0 0 1 0 0 1 0 0 0 0 0 1000 60 1000 60
+6 * 1
+2 * 1 0 0 0 0 0 0 0 0 1
+6 * 1 0 0 1 0 0 1 0 0 0 0 0 0 500 56
+3 * 1
jump=1 0x80363 +5
* *
0x814b0 2400 1 0 0 1 0 0 1 0 0 0 0 0 0 250 48
+3 * 1
+2 * 1
+6 * 1
+5 * 1 0 0 1 0 0 1 0 0 0 0 0 0 66
+5 * 1
+3 * 1 0 1
cfi=(85) /build/buildd/eglibc-2.15/misc/../sysdeps/unix/syscall-template.S
cfn=(396) mmap
calls=1 0xf0060 82
* * 6 1 0 1 0 0 1 0 0 1 0 0 0 0 0 0 0 1
+5 +2 1
+4 * 1 0 0 0 0 0 0 0 0 1
+6 +12 1
+4 * 1
+3 * 1 0 0 0 0 0 0 0 0 1
+2 +3 1
+3 * 1
+4 * 1 0 1 0 0 1 0 0 1 0 0 0 0 500 56 500 56
+4 +4 1 1
+6 * 1
+3 * 1 1
+6 * 1 0 1
+6 * 1 0 0 1 0 0 1 0 0 1 0 0 0 125 16
+2 +1 1 0 1
+6 +2 1 1
+7 +1 1 1
+7 -1 1 0 1
+7 +1 1 0 0 0 0 0 0 0 0 1
+6 +1 1 0 1
+7 * 1
jump=1 0x80f25 3893
* *

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.