-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathgithub_query.cc
More file actions
338 lines (290 loc) · 9.83 KB
/
github_query.cc
File metadata and controls
338 lines (290 loc) · 9.83 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include <cstdio>
#include <experimental/filesystem>
#include <fstream>
#include "adapters/simdjson_adapter.h"
#include "core/fishstore.h"
using namespace fishstore;
typedef environment::QueueIoHandler handler_t;
typedef device::FileSystemDisk<handler_t, 1073741824L> disk_t;
typedef adapter::SIMDJsonAdapter adapter_t;
using store_t = core::FishStore<disk_t, adapter_t>;
class JsonGeneralScanContext : public IAsyncContext {
public:
JsonGeneralScanContext(uint16_t psf_id, const char* value)
: hash_{ core::Utility::HashBytesWithPSFID(psf_id, value, strlen(value)) },
psf_id_{ psf_id },
value_size_{ static_cast<uint32_t>(strlen(value)) },
value_{ value },
cnt{ 0 } {
}
JsonGeneralScanContext(const JsonGeneralScanContext& other)
: hash_{ other.hash_ },
psf_id_{ other.psf_id_ },
value_size_{ other.value_size_ },
cnt{ other.cnt } {
set_from_deep_copy();
char* res = (char*)malloc(value_size_);
memcpy(res, other.value_, value_size_);
value_ = res;
}
~JsonGeneralScanContext() {
if (from_deep_copy()) free((void*)value_);
}
inline void Touch(const char* payload, uint32_t payload_size) {
// printf("Record Hit: %.*s\n", payload_size, payload);
++cnt;
}
inline void Finalize() {
printf("%u record has been touched...\n", cnt);
}
inline core::KeyHash get_hash() const {
return hash_;
}
inline bool check(const core::KeyPointer* kpt) {
return kpt->mode == 0 && kpt->general_psf_id == psf_id_ &&
kpt->value_size == value_size_ &&
!memcmp(kpt->get_value(), value_, value_size_);
}
protected:
Status DeepCopy_Internal(IAsyncContext*& context_copy) {
return IAsyncContext::DeepCopy_Internal(*this, context_copy);
}
private:
core::KeyHash hash_;
uint16_t psf_id_;
uint32_t value_size_;
const char* value_;
uint32_t cnt;
};
class JsonInlineScanContext : public IAsyncContext {
public:
JsonInlineScanContext(uint32_t psf_id, int32_t value)
: psf_id_(psf_id), value_(value), cnt(0) {}
inline void Touch(const char* payload, uint32_t payload_size) {
// printf("Record Hit: %.*s\n", payload_size, payload);
++cnt;
}
inline void Finalize() {
printf("%u record has been touched...\n", cnt);
}
inline core::KeyHash get_hash() const {
return core::KeyHash{ core::Utility::GetHashCode(psf_id_, value_) };
}
inline bool check(const core::KeyPointer* kpt) {
return kpt->mode == 1 && kpt->inline_psf_id == psf_id_ && kpt->value == value_;
}
protected:
Status DeepCopy_Internal(IAsyncContext*& context_copy) {
return IAsyncContext::DeepCopy_Internal(*this, context_copy);
}
private:
uint32_t psf_id_;
int32_t value_;
uint32_t cnt;
};
void SetThreadAffinity(size_t core) {
// Assume 28 cores.
constexpr size_t kCoreCount = 36; // JDH DEBUG
#ifdef _WIN32
HANDLE thread_handle = ::GetCurrentThread();
::SetThreadAffinityMask(thread_handle, (uint64_t)0x1 << core);
#else
// On MSR-SANDBOX-049, we see CPU 0, Core 0 assigned to 0, 28;
// CPU 1, Core 0 assigned to 1, 29; etc.
cpu_set_t mask;
CPU_ZERO(&mask);
#ifdef NUMA
switch (core % 4) {
case 0:
// 0 |-> 0
// 4 |-> 2
// 8 |-> 4
core = core / 2;
break;
case 1:
// 1 |-> 28
// 5 |-> 30
// 9 |-> 32
core = kCoreCount + (core - 1) / 2;
break;
case 2:
// 2 |-> 1
// 6 |-> 3
// 10 |-> 5
core = core / 2;
break;
case 3:
// 3 |-> 29
// 7 |-> 31
// 11 |-> 33
core = kCoreCount + (core - 1) / 2;
break;
}
#else
switch (core % 2) {
case 0:
// 0 |-> 0
// 2 |-> 2
// 4 |-> 4
core = core;
break;
case 1:
// 1 |-> 28
// 3 |-> 30
// 5 |-> 32
core = (core - 1) + kCoreCount;
break;
}
#endif
CPU_SET(core, &mask);
::sched_setaffinity(0, sizeof(mask), &mask);
#endif
}
int main(int argc, char* argv[]) {
if (argc != 6) {
printf("Usage: ./github_query <input_file> <lib_file> <n_threads> <memory_buget> <store_target>\n");
return -1;
}
int n_threads = atoi(argv[3]);
std::ifstream fin(argv[1]);
std::vector<std::string> batches;
const uint32_t json_batch_size = 1;
uint32_t json_batch_cnt = 0;
size_t line_cnt = 0;
size_t record_cnt = 0;
std::string line;
while (std::getline(fin, line)) {
if (json_batch_cnt == 0 || line_cnt == json_batch_size) {
line_cnt = 1;
++json_batch_cnt;
batches.push_back(line);
}
else {
batches.back() += line;
line_cnt++;
}
++record_cnt;
}
uint32_t batch_size = json_batch_cnt / n_threads + 1;
printf("Finish loading %u batches (%zu records) of json into the memory....\n",
json_batch_cnt, record_cnt);
std::experimental::filesystem::create_directory(argv[5]);
size_t store_size = 1LL << atoi(argv[4]);
store_t store{ (1L << 24), store_size, argv[5] };
SetThreadAffinity(n_threads);
store.StartSession();
auto lib_id = store.LoadPSFLibrary(argv[2]);
auto id_proj = store.MakeProjection("/id");
auto actor_id_proj = store.MakeInlinePSF({ "/actor/id" }, lib_id, "int_projection");
auto repo_id_proj = store.MakeInlinePSF({ "/repo/id" }, lib_id, "int_projection");
auto type_proj = store.MakeProjection("/type");
auto predicate1_id =
store.MakeInlinePSF({ "/type", "/payload/action" }, lib_id, "opened_issue");
auto predicate2_id =
store.MakeInlinePSF({ "/type", "/payload/pull_request/head.repo/language" }, lib_id, "cpp_pr");
std::vector<ParserAction> parser_actions;
parser_actions.push_back({ REGISTER_GENERAL_PSF, id_proj });
parser_actions.push_back({ REGISTER_GENERAL_PSF, actor_id_proj });
parser_actions.push_back({ REGISTER_GENERAL_PSF, repo_id_proj });
parser_actions.push_back({ REGISTER_GENERAL_PSF, type_proj });
parser_actions.push_back({ REGISTER_INLINE_PSF, predicate1_id });
parser_actions.push_back({ REGISTER_INLINE_PSF, predicate2_id });
uint64_t safe_register_address, safe_unregister_address;
safe_unregister_address = store.ApplyParserShift(
parser_actions, [&safe_register_address](uint64_t safe_address) {
safe_register_address = safe_address;
});
store.CompleteAction(true);
std::this_thread::sleep_for(std::chrono::seconds(1));
std::vector<std::thread> thds;
std::atomic_uint64_t bytes_ingested{ 0 };
std::atomic_uint32_t record_ingested{ 0 };
auto worker_thd = [&](int thread_no) {
SetThreadAffinity(thread_no);
store.StartSession();
size_t begin_line = thread_no;
size_t batch_end = batches.size();
auto callback = [](IAsyncContext * ctxt, Status result) {
assert(false);
};
store.Refresh();
size_t op_cnt = 0;
for (size_t i = begin_line; i < batch_end; i += n_threads) {
auto res = store.BatchInsert(batches[i], 1);
bytes_ingested.fetch_add(batches[i].size());
record_ingested.fetch_add(res);
op_cnt += res;
if (op_cnt % 256 == 0) {
store.Refresh();
op_cnt = 0;
}
}
store.CompletePending();
store.StopSession();
};
bool finish = false;
auto callback = [](IAsyncContext * ctxt, Status result) {
assert(result == Status::Ok);
};
auto begin = std::chrono::high_resolution_clock::now();
for (int i = 0; i < n_threads; ++i) {
thds.emplace_back(std::thread(worker_thd, i));
}
std::thread timer([&n_threads, &finish, &bytes_ingested, &record_ingested]() {
SetThreadAffinity(n_threads + 1);
uint64_t last_bytes = 0;
uint32_t last_records = 0;
while (!finish) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
uint64_t current_bytes = bytes_ingested.load();
uint32_t current_records = record_ingested.load();
printf("Throughput: %.6lf MB/s, %u records/s\n",
(double)(current_bytes - last_bytes) / 1024.0 / 1024.0,
(current_records - last_records));
last_bytes = current_bytes;
last_records = current_records;
}
});
for (int i = 0; i < n_threads; ++i) {
thds[i].join();
}
auto end = std::chrono::high_resolution_clock::now();
finish = true;
timer.join();
printf("Finish inserting %u json batches in %.6f seconds....\n",
json_batch_cnt, std::chrono::duration<double>(end - begin).count());
printf("Log Size: %.6fMB\n",
store.hlog.GetTailAddress().control() / 1024.0 / 1024.0);
/// Query Examples
core::Constants::SetAvgRecordSize(2310);
JsonGeneralScanContext scan_context_push{ type_proj, "PushEvent" };
begin = std::chrono::high_resolution_clock::now();
auto status = store.Scan(scan_context_push, callback, 1);
store.CompletePending(true);
end = std::chrono::high_resolution_clock::now();
printf("Scan on `type == PushEvent` is done in %.6f seconds...\n",
std::chrono::duration<double>(end - begin).count());
JsonInlineScanContext pred_scan_context3{ predicate2_id, 1 };
begin = std::chrono::high_resolution_clock::now();
status = store.Scan(pred_scan_context3, callback, 1);
store.CompletePending(true);
end = std::chrono::high_resolution_clock::now();
printf("Scan over pre-registred predicate `type == PullRequestEvent && payload.head.pull_request.repo.language == C++` is done in %.6f seconds...\n",
std::chrono::duration<double>(end - begin).count());
parser_actions.clear();
parser_actions.push_back({ DEREGISTER_GENERAL_PSF, id_proj });
parser_actions.push_back({ DEREGISTER_GENERAL_PSF, actor_id_proj });
parser_actions.push_back({ DEREGISTER_GENERAL_PSF, repo_id_proj });
parser_actions.push_back({ DEREGISTER_GENERAL_PSF, type_proj });
parser_actions.push_back({ DEREGISTER_INLINE_PSF, predicate1_id });
parser_actions.push_back({ DEREGISTER_INLINE_PSF, predicate2_id });
safe_unregister_address = store.ApplyParserShift(
parser_actions, [&safe_register_address](uint64_t safe_address) {
safe_register_address = safe_address;
});
store.CompleteAction(true);
store.StopSession();
return 0;
}