From d4c8acb4dd621cb95c3c22020b3c546bcb5d5a2d Mon Sep 17 00:00:00 2001 From: Mubashshir Date: Fri, 15 Nov 2024 14:23:28 +0600 Subject: Implement full v3.0 grammar Signed-off-by: Mubashshir --- .gitignore | 11 ++-- Makefile | 2 +- src/system.l | 3 ++ src/system.yy | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 156 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 854a00e..e6acb30 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,10 @@ * !**/ -!Makefile +!/Makefile !.gitignore -!src/lexer.h -!src/system.l -!src/system.proto -!src/system.yy +!src/*.h +*.pb.h +!src/*.l +!src/*.proto +!src/*.yy !test/*.log diff --git a/Makefile b/Makefile index 2b2ccce..ec5a358 100644 --- a/Makefile +++ b/Makefile @@ -48,4 +48,4 @@ clean: @$(RM) -f $(<>) $(BINARY) .PHONY: all clean install uninstall test -.INTERMEDIATE: src/system.lex.cc src/system.pb.h src/system.tab.cc src/system.tab.hh +#.INTERMEDIATE: src/system.lex.cc src/system.pb.h src/system.tab.cc src/system.tab.hh diff --git a/src/system.l b/src/system.l index b77255e..6c3cfb7 100644 --- a/src/system.l +++ b/src/system.l @@ -2,10 +2,12 @@ %option noyywrap %option debug %option yyclass="Lexer" +%option yylineno %{ #include #include "system.tab.hh" +#include "system.loc.hh" #undef YY_DECL # define YY_DECL template<> parser::symbol_type Lexer::yynlex () @@ -39,6 +41,7 @@ core: { return parser::make_CORE(); } mem { return parser::make_MEM(); } total { return parser::make_TOTAL(); } +avail { return parser::make_AVAIL(); } cache { return parser::make_CACHE(); } share { return parser::make_SHARED(); } tswap { return parser::make_TSWAP(); } diff --git a/src/system.yy b/src/system.yy index af64edd..5ee7f96 100644 --- a/src/system.yy +++ b/src/system.yy @@ -10,6 +10,7 @@ using namespace std; Entry::System msg; +bool legacy = true; %} @@ -18,19 +19,26 @@ Entry::System msg; %define api.token.constructor %define api.value.type variant +%define parse.error detailed +%define parse.trace true + %code requires // *.hh { #include #include "lexer.h" +#include "system.loc.hh" } -%parse-param {Lexer &lexer} +//%locations +//%define api.location.type {yy::location} +%parse-param {Lexer &lexer} %token NUM %token FRC +%token TOKEN %token BREAK BLANK START END VERSION CPU BASE USER NICE -%token SYSP IDLE IOWP IRQP SIRQ CORE MEM TOTAL CACHE SHARED -%token TSWAP USWAP TEMP CURR TMIN TMAX TAVG COLON SLASH +%token SYSP IDLE IOWP IRQP SIRQ CORE MEM TOTAL AVAIL CACHE +%token SHARED TSWAP USWAP TEMP CURR TMIN TMAX TAVG COLON SLASH %% @@ -41,22 +49,20 @@ start: ; block: - block_start data block_end + block_start data_block block_end ; block_start: START BLANK NUM BREAK { + cout << "begin\t" << $NUM << endl; msg.set_epoch($3); } ; block_end: END BLANK NUM BREAK { - if ($3 != msg.epoch()) { - msg.Clear(); - cout << $3 << endl; - } else { - cout << $3 << endl; + if ($3 == msg.epoch()) { + cout << "end\t" << $NUM << endl; auto p = filesystem::path("proto"); p /= to_string($3); p += string(".proto"); @@ -64,10 +70,138 @@ block_end: msg.SerializeToOstream(&f); f.close(); } + msg.Clear(); + } + +data_block: + version_block data_list + ; + +version_block: + VERSION BLANK NUM BLANK NUM BREAK { + legacy = false; + cout << "ver\t" << $3 << "\t" << $5 << endl; } + | /* legacy */ { } + ; + +data_list: + data BREAK | data BREAK data_list; data: -; + CPU BLANK cpu_block + | MEM BLANK mem_block + | TEMP BLANK temp_block + ; + +cpu_percent: + USER { $$ = yylhs.kind(); cout << $$; } + | NICE { $$ = yylhs.kind(); } + | SYSP { $$ = yylhs.kind(); } + | IDLE { $$ = yylhs.kind(); } + | IOWP { $$ = yylhs.kind(); } + | IRQP { $$ = yylhs.kind(); } + | SIRQ { $$ = yylhs.kind(); }; + +cpu_block: + BASE BLANK FRC BLANK NUM { + ([&] (auto base) { + base->set_usage($3); + base->set_clock($5); + cout << "cpu\tbase\t" << base->usage() << "\t" << base->clock() << endl; + })(msg.mutable_cpu()->mutable_base()); + } + | cpu_percent BLANK FRC BLANK FRC { + cout << $1 << endl; + string name = ([&](auto t) { + switch(t) { + case token_type::USER: return "user"; + case token_type::NICE: return "nice"; + case token_type::SYSP: return "system"; + case token_type::IDLE: return "idle"; + case token_type::IOWP: return "iowait"; + case token_type::IRQP: return "hwirq"; + case token_type::SIRQ: return "swirq"; + default: return "undefined"; + } + })($1); + + ([&] (auto a, auto b) -> void { + ::Entry::CPUStats_Subsystem sys; + sys.set_curr(a); + sys.set_diff(b); + msg.mutable_cpu()->mutable_subsystem()->insert({name, sys}); + cout << "cpu\t" << name << "%\t" << msg.cpu().subsystem().at(name).curr() + << "\t" << msg.cpu().subsystem().at(name).diff() << endl; + })($3, $5); + + } + | CORE NUM BLANK FRC BLANK NUM { + ([&] (auto core, auto a, auto b) -> void { + ::Entry::CPUStats_Core c; + c.set_usage(a); + c.set_clock(b); + msg.mutable_cpu()->mutable_cores()->insert({core, c}); + + cout << "cpu\tcore:" << core << "\t" << msg.cpu().cores().at(core).usage() + << "\t" << msg.cpu().cores().at(core).clock() << endl; + })($2, $4, $6); + + } + ; + +mem_type: + TOTAL { $$ = 1; } + | AVAIL { $$ = 2; } + | CACHE { $$ = 3; } + | SHARED { $$ = 4; } + | TSWAP { $$ = 5; } + | USWAP { $$ = 6; } + ; + +mem_block: + mem_type BLANK NUM { + ([&](auto mem, int t, uint64_t v) { + cerr << yystack_[2].kind() << ":" << endl; + cout << "mem\t"; + switch((int)t) { + case token_type::TOTAL: mem->set_total(v); cout << "\ttotal" << msg.mem().total(); break; + case token_type::AVAIL: mem->set_avail(v); cout << "\tavail" << msg.mem().avail(); break; + case token_type::CACHE: mem->set_cache(v); cout << "\tcache" << msg.mem().cache(); break; + case token_type::SHARED:mem->set_share(v); cout << "\tshare" << msg.mem().share(); break; + case token_type::TSWAP: mem->set_tswap(v); cout << "\ttswap" << msg.mem().tswap(); break; + case token_type::USWAP: mem->set_uswap(v); cout << "\tuswap" << msg.mem().uswap(); break; + default: break; + } + cout << endl; + })(msg.mutable_mem(), $1, $3); + } + ; +temp_block: + tmp_current + | tmp_legacy + ; + +tmp_current_type: CURR | TMIN | TMAX | TAVG; +tmp_current: + tmp_current_type BLANK NUM { + if (legacy) throw syntax_error("TEMP Block in legacy data."); + + using token = yy::parser::token::token_kind_type; + ([&](auto tmp, token t, uint64_t v) { + switch(t) { + case token::CURR: tmp->set_cur(v); break; + case token::TMIN: tmp->set_min(v); break; + case token::TMAX: tmp->set_max(v); break; + case token::TAVG: tmp->set_avg(v); break; + default: break; + } + })(msg.mutable_tmp(), $1, $3); + } + ; + +tmp_legacy: + ; %% namespace yy @@ -75,14 +209,12 @@ namespace yy // Report an error to the user. auto parser::error (const std::string& msg) -> void { - std::cerr << msg << '\n'; + std::cerr << msg << " on line " << lexer.lineno() << '\n'; } } -#undef PARSER_SOURCE -#include "lexer.h" - int main() { + Lexer lexer; yy::parser parse(lexer); -- cgit v1.2.3