summaryrefslogtreecommitdiff
path: root/src/system.yy
blob: 07272135cdeb58c97e6c1a6b5f4bb0be7f616de8 (plain) (blame)
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
282
283
284
285
286
287
288
289
290
/* parser.y (Bison file) */
%{
#define PARSER_SOURCE
#include <fstream>
#include <filesystem>
#include "system.pb.h"  // Include the generated protobuf header
#include "lexer.h"
#define yylex lexer.yynlex

using namespace std;

Entry::System msg;
bool legacy = true;

%}

%language "c++"
%require "3.2"
%define api.token.constructor
%define api.value.type variant

%define parse.error detailed
%define parse.trace true

%code requires // *.hh
{
#include <cstdint>
#include "lexer.h"
#include "location.h"
#include "util.h"
}

//%locations
//%define api.location.type {yy::location}

%parse-param {Lexer<symbol_type> &lexer}
%token <uint64_t> ULONG
%token <double> FLOAT
%token <int> KEY
%token BREAK BLANK START END VERSION CPU BASE USER NICE SWAP
%token SYSP IDLE IOWP IRQP SIRQ CORE MEM TOTAL AVAIL CACHE TSENSOR
%token SHARED TSWAP USWAP TEMP CURR TMIN TMAX TAVG COLON SLASH

%%

// Grammar Rules
start:
	block
	| start block
	;

block:
	block_start data_block block_end
	;

block_start:
	START BLANK ULONG BREAK {
		cout << "begin\t" << $ULONG << endl;
		msg.set_epoch($<uint64_t>3);
	}
	;

block_end:
	END BLANK ULONG BREAK {
		if ($<uint64_t>3 == msg.epoch()) {
			cout << "end\t" << $ULONG << endl;
			auto p = filesystem::path("proto");
			p /= to_string($<uint64_t>3);
			p += string(".proto");
			ofstream f(p, ios_base::out | ios_base::binary);
			msg.SerializeToOstream(&f);
			f.close();
		} else
			throw syntax_error(string_format("Block EPOCH mismatch: begin:%llu, end:%llu", msg.epoch(), $ULONG));
		msg.Clear();
	}

data_block:
	version_block data_list
	| cpu_legacy_list mem_legacy tmp_legacy
	;

version_block:
	VERSION BLANK ULONG BLANK ULONG BREAK {
		legacy = false;
		cout << "ver\t" << $3 << "\t" << $5 << endl;
	}
	;

data_list:
	data BREAK | data BREAK data_list;

data:
	CPU BLANK cpu_block
	| MEM BLANK mem_current
	| TEMP BLANK tmp_current
	;

cpu_percent:
	USER   { $<int>$ = 0; }
	| NICE { $<int>$ = 1; }
	| SYSP { $<int>$ = 2; }
	| IDLE { $<int>$ = 3; }
	| IOWP { $<int>$ = 4; }
	| IRQP { $<int>$ = 5; }
	| SIRQ { $<int>$ = 6; };

cpu_block:
	BASE BLANK FLOAT BLANK ULONG {
		([&] (auto base) {
			base->set_usage($<double>3);
			base->set_clock($<uint64_t>5);
			cout << "cpu\tbase\t" << base->usage() << "\t" << base->clock() << endl;
		})(msg.mutable_cpu()->mutable_base());
	}
	| cpu_percent BLANK FLOAT BLANK FLOAT {
		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 "undef";
				}
			})($<int>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;
		})($<double>3, $<double>5);

	}
	| CORE ULONG BLANK FLOAT BLANK ULONG {
		([&] (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;
		})($<uint64_t>2, $<double>4, $<uint64_t>6);

	}
	;

mem_type:
    TOTAL  { $<int>$ = 1; }
    | AVAIL  { $<int>$ = 2; }
    | CACHE  { $<int>$ = 3; }
    | SHARED { $<int>$ = 4; }
    | TSWAP  { $<int>$ = 5; }
    | USWAP  { $<int>$ = 6; }
    ;

mem_current:
	mem_type BLANK ULONG {
		([&](auto mem, int t, uint64_t v) {
			cout << "mem\t";
			switch(t) {
				case token_type::TOTAL: mem->set_total(v); cout << "total\t" << msg.mem().total(); break;
				case token_type::AVAIL: mem->set_avail(v); cout << "avail\t" << msg.mem().avail(); break;
				case token_type::CACHE: mem->set_cache(v); cout << "cache\t" << msg.mem().cache(); break;
				case token_type::SHARED:mem->set_share(v); cout << "share\t" << msg.mem().share(); break;
				case token_type::TSWAP: mem->set_tswap(v); cout << "tswap\t" << msg.mem().tswap(); break;
				case token_type::USWAP: mem->set_uswap(v); cout << "uswap\t" << msg.mem().uswap(); break;
				default: cout << "undef\t" << v; break;
			}
			cout << endl;
		})(msg.mutable_mem(), yystack_[2].kind(), $ULONG);
	}
	;

tmp_type: CURR | TMIN | TMAX | TAVG;
tmp_current:
	tmp_type BLANK ULONG {
		if (legacy) throw syntax_error("TEMP Block in legacy data.");

		using token = yy::parser::token::token_kind_type;
		([&](auto tmp, int t, uint64_t v) {
			cout << "tmp\t";
			switch(t) {
				case token::CURR: tmp->set_cur(v); cout << "cur\t" << tmp->cur(); break;
				case token::TMIN: tmp->set_min(v); cout << "min\t" << tmp->min(); break;
				case token::TMAX: tmp->set_max(v); cout << "max\t" << tmp->max(); break;
				case token::TAVG: tmp->set_avg(v); cout << "avg\t" << tmp->avg(); break;
				default: cout << "und\t" << v; break;
			}
			cout << endl;
		})(msg.mutable_tmp(), yystack_[2].kind(), $<uint64_t>3);
	}
	;

cpu_legacy: CPU BLANK cpu_block;
cpu_legacy_list:
	cpu_legacy BREAK
	| cpu_legacy BREAK cpu_legacy_list
	;

mem_legacy: mem_legacy_total mem_legacy_list BREAK;
mem_legacy_total:
	MEM BLANK ULONG {
		msg.mutable_mem()->set_total($<uint64_t>3);
		cout << "mem\t" << msg.mem().total();
	}

mem_legacy_type: AVAIL | CACHE | SHARED;
mem_legacy_list:
	BLANK mem_legacy_data
	| mem_legacy_list BLANK mem_legacy_data
	;

mem_legacy_ram:
	mem_legacy_type COLON ULONG {
		([&](auto mem, int t, uint64_t v) {
			switch(t) {
				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;
				default: cout << "\tundef:" << v; break;
			}
		})(msg.mutable_mem(), yystack_[2].kind(), $<uint64_t>3);
	}
	;

mem_legacy_data:
	mem_legacy_ram
	| SWAP COLON ULONG SLASH ULONG {
		msg.mutable_mem()->set_tswap($<uint64_t>5);
		msg.mutable_mem()->set_uswap($<uint64_t>3);
		cout << "\n\t\t\tswap:" << msg.mem().uswap() << "/" << msg.mem().tswap() << endl;
	}
	;


tmp_legacy: tmp_legacy_block BREAK {
	if (! legacy) throw syntax_error("Unknown TEMP Block data.");
	cout << endl;
};

tmp_legacy_block:
	tmp_legacy_id tmp_legacy_list
	| tmp_legacy_list;
tmp_legacy_id:
	TEMP BLANK TSENSOR ULONG {
		cout << "tmp\tPackage id " << $ULONG;
	}
	;

tmp_legacy_list:
	BLANK tmp_legacy_data | tmp_legacy_list BLANK tmp_legacy_data;

tmp_legacy_data:
	tmp_type COLON ULONG {
		using token = yy::parser::token::token_kind_type;
		([&](auto tmp, token t, uint64_t v) {
			switch(t) {
				case token::CURR: tmp->set_cur(v); cout << "\tcur:" << msg.tmp().cur(); break;
				case token::TMIN: tmp->set_min(v); cout << "\tmin:" << msg.tmp().min(); break;
				case token::TMAX: tmp->set_max(v); cout << "\tmax:" << msg.tmp().max(); break;
				case token::TAVG: tmp->set_avg(v); cout << "\tavg:" << msg.tmp().avg(); break;
				default: cout << "\tund:" << v; break;
			}
		})(msg.mutable_tmp(), $<token>1, $<uint64_t>3);
	}
%%

namespace yy
{
  // Report an error to the user.
  auto parser::error (const std::string& msg) -> void
  {
    std::cerr << "\nERROR: " << msg << " on line " << lexer.lineno() - 1<< '\n';
  }
}

int main() {

	Lexer<yy::parser::symbol_type> lexer;
	yy::parser parse(lexer);

    return parse();
}