Skip to content

Instantly share code, notes, and snippets.

@ned14
Created April 24, 2013 14:49
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ned14/5452719 to your computer and use it in GitHub Desktop.
My failing callgrind parser
/* 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
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
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 +2 1
+3 * 1
+4 3570 1 0 1
+5 3885 1 0 1
+4 * 1
+3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 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
* *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment