-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgml2bin.cpp
More file actions
222 lines (215 loc) · 5.4 KB
/
gml2bin.cpp
File metadata and controls
222 lines (215 loc) · 5.4 KB
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
#include <beamsim/from_chars.hpp>
#include <beamsim/gml.hpp>
#include <beamsim/mmap.hpp>
#include <beamsim/write_file.hpp>
#include <charconv>
#include <cstdio>
#include <fstream>
#include <print>
#include <unordered_map>
using std::literals::operator""sv;
struct Tokens {
std::string_view input;
void error() const {
throw std::logic_error("gml parse error");
}
void skipWhitespace() {
auto i = input.find_first_not_of(" \n"sv);
if (i == std::string_view::npos) {
return;
}
input.remove_prefix(i);
}
std::string_view take(size_t n) {
auto r = input.substr(0, n);
input.remove_prefix(n);
return r;
}
std::string_view token() {
skipWhitespace();
if (input.empty()) {
return input;
}
if (input[0] == '[' or input[0] == ']') {
return take(1);
}
if (input[0] == '"') {
auto i = input.find('"', 1);
if (i == std::string_view::npos) {
error();
}
return take(i + 1);
}
auto i = input.find_first_of(" \n[]\""sv);
return take(i == std::string_view::npos ? input.size() : i);
}
void token(std::string_view expected) {
auto t = token();
if (t != expected) {
error();
}
}
std::optional<std::string_view> bracket() {
auto t = token();
if (t == "]"sv) {
return std::nullopt;
}
return t;
}
std::string_view str() {
auto t = token();
if (t[0] != '"') {
error();
}
if (t.back() != '"') {
error();
}
t.remove_prefix(1);
t.remove_suffix(1);
return t;
}
template <typename T>
T num() {
auto r = beamsim::numFromChars<T>(token());
if (not r or r->second != ""sv) {
error();
}
return r->first;
}
template <typename T>
std::pair<T, std::string_view> numSuffix() {
auto r = beamsim::numFromChars<T>(str());
if (not r) {
error();
}
return *r;
}
template <typename T>
T numSuffix(std::string_view expected) {
auto [num, suffix] = numSuffix<T>();
if (suffix != expected) {
error();
}
return num;
}
uint32_t kibit() {
return numSuffix<uint32_t>(" Kibit"sv);
}
uint32_t us() {
return numSuffix<uint32_t>(" us"sv);
}
};
beamsim::Gml parse(std::string_view input) {
using _Node = uint16_t;
using NodeIndex = uint16_t;
using City = uint16_t;
using _City = uint64_t;
Tokens tokens{input};
beamsim::Gml gml;
std::unordered_map<_Node, NodeIndex> node_id_index;
std::unordered_map<_City, City> city_index;
auto nodes = true;
auto bad_nodes = 0;
tokens.token("graph"sv);
tokens.token("["sv);
while (auto t = tokens.bracket()) {
if (t == "node"sv) {
auto remove = false;
if (not nodes) {
tokens.error();
}
tokens.token("["sv);
tokens.token("id"sv);
auto node_id = tokens.num<_Node>();
tokens.token("label"sv), tokens.str();
tokens.token("ip_address"sv), tokens.str();
auto t = tokens.token();
std::optional<_City> _city;
if (t == "city_code"sv) {
_city = tokens.numSuffix<_City>(""sv);
t = tokens.token();
} else {
remove = true;
}
if (t == "country_code"sv) {
tokens.str();
t = tokens.token();
} else {
remove = true;
}
if (t != "bandwidth_down"sv) {
tokens.error();
}
auto down = tokens.kibit();
tokens.token("bandwidth_up"sv);
auto up = tokens.kibit();
tokens.token("]"sv);
if (not remove) {
auto city_it = city_index.find(_city.value());
if (city_it == city_index.end()) {
city_it = city_index.emplace(_city.value(), city_index.size()).first;
}
node_id_index.emplace(node_id, gml.nodes.size());
gml.nodes.emplace_back(beamsim::Gml::Node{
.down_kibit = down,
.up_kibit = up,
.city = city_it->second,
});
} else {
++bad_nodes;
}
continue;
}
if (t == "edge"sv) {
if (nodes) {
nodes = false;
gml.latency_us.resize(gml.nodes.size());
}
tokens.token("["sv);
tokens.token("source"sv);
auto i1 = tokens.num<_Node>();
tokens.token("target"sv);
auto i2 = tokens.num<_Node>();
tokens.token("latency"sv);
auto latency = tokens.us();
tokens.token("packet_loss"sv);
tokens.num<double>();
tokens.token("label"sv);
tokens.str();
tokens.token("]"sv);
auto it1 = node_id_index.find(i1);
auto it2 = node_id_index.find(i2);
if (i1 != i2 and it1 != node_id_index.end()
and it2 != node_id_index.end()) {
gml.latency_us.at(it1->second, it2->second) = latency;
}
continue;
}
tokens.error();
}
if (bad_nodes != 0) {
std::println("gml: {} bad nodes", bad_nodes);
}
return gml;
}
int main(int argc, char **argv) {
if (argc != 3) {
std::println("usage: gml2bin [graph.gml] [graph.bin]");
return EXIT_FAILURE;
}
std::string input_path = argv[1];
std::string output_path = argv[2];
beamsim::Mmap input{input_path};
input.sequential();
if (not input.good()) {
std::println("can't read {}", input_path);
return EXIT_FAILURE;
}
auto gml = parse(input.data);
auto bin = gml.encode();
std::println("gml {}mb -> bin {}mb, {}%",
input.data.size() >> 20,
bin.size() >> 20,
100 * bin.size() / input.data.size());
beamsim::writeFile(output_path, bin);
}