2018-11-30 22:37:57 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <x86intrin.h>
|
|
|
|
#include <ctype.h>
|
2018-08-21 05:27:25 +08:00
|
|
|
#include <assert.h>
|
2018-11-30 22:37:57 +08:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2018-08-21 05:27:25 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
2018-03-23 12:05:32 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2018-11-30 22:37:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
#include "linux-perf-events.h"
|
2018-12-19 11:18:23 +08:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <libgen.h>
|
|
|
|
#endif
|
2018-05-31 10:46:28 +08:00
|
|
|
//#define DEBUG
|
2018-11-30 22:37:57 +08:00
|
|
|
#include "simdjson/common_defs.h"
|
|
|
|
#include "simdjson/jsonparser.h"
|
|
|
|
#include "simdjson/jsonioutil.h"
|
|
|
|
#include "simdjson/parsedjson.h"
|
|
|
|
#include "simdjson/stage1_find_marks.h"
|
|
|
|
#include "simdjson/stage2_flatten.h"
|
|
|
|
#include "simdjson/stage34_unified.h"
|
2018-08-07 15:24:05 +08:00
|
|
|
using namespace std;
|
2018-03-23 12:05:32 +08:00
|
|
|
|
2018-08-21 05:27:25 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2018-11-10 10:31:14 +08:00
|
|
|
bool verbose = false;
|
2018-11-24 11:20:57 +08:00
|
|
|
bool dump = false;
|
2018-12-07 06:40:32 +08:00
|
|
|
bool jsonoutput = false;
|
2018-11-28 03:37:59 +08:00
|
|
|
bool forceoneiteration = false;
|
2018-12-19 11:18:23 +08:00
|
|
|
bool justdata = false;
|
2018-11-24 11:20:57 +08:00
|
|
|
|
2018-11-10 10:31:14 +08:00
|
|
|
int c;
|
|
|
|
|
2018-12-19 11:18:23 +08:00
|
|
|
while ((c = getopt (argc, argv, "1vdt")) != -1)
|
2018-11-10 10:31:14 +08:00
|
|
|
switch (c)
|
|
|
|
{
|
2018-12-19 11:18:23 +08:00
|
|
|
case 't':
|
|
|
|
justdata = true;
|
|
|
|
break;
|
2018-11-10 10:31:14 +08:00
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
2018-11-24 11:20:57 +08:00
|
|
|
case 'd':
|
|
|
|
dump = true;
|
|
|
|
break;
|
2018-12-07 06:40:32 +08:00
|
|
|
case 'j':
|
|
|
|
jsonoutput = true;
|
|
|
|
break;
|
2018-11-28 03:37:59 +08:00
|
|
|
case '1':
|
|
|
|
forceoneiteration = true;
|
|
|
|
break;
|
2018-11-10 10:31:14 +08:00
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
if (optind >= argc) {
|
2018-08-21 05:27:25 +08:00
|
|
|
cerr << "Usage: " << argv[0] << " <jsonfile>" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-11-10 10:31:14 +08:00
|
|
|
const char * filename = argv[optind];
|
|
|
|
if(optind + 1 < argc) {
|
|
|
|
cerr << "warning: ignoring everything after " << argv[optind + 1] << endl;
|
2018-08-21 05:27:25 +08:00
|
|
|
}
|
2018-11-10 10:31:14 +08:00
|
|
|
if(verbose) cout << "[verbose] loading " << filename << endl;
|
2018-12-01 10:31:05 +08:00
|
|
|
std::string_view p;
|
2018-11-28 03:37:59 +08:00
|
|
|
try {
|
|
|
|
p = get_corpus(filename);
|
|
|
|
} catch (const std::exception& e) { // caught by reference to base
|
|
|
|
std::cout << "Could not load the file " << filename << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-12-01 09:27:16 +08:00
|
|
|
if(verbose) cout << "[verbose] loaded " << filename << " ("<< p.size() << " bytes)" << endl;
|
2018-07-14 10:22:30 +08:00
|
|
|
#if defined(DEBUG)
|
2018-08-21 05:27:25 +08:00
|
|
|
const u32 iterations = 1;
|
2018-03-23 12:05:32 +08:00
|
|
|
#else
|
2018-12-01 09:27:16 +08:00
|
|
|
const u32 iterations = forceoneiteration ? 1 : ( p.size() < 1 * 1000 * 1000? 1000 : 10);
|
2018-03-23 12:05:32 +08:00
|
|
|
#endif
|
2018-08-21 05:27:25 +08:00
|
|
|
vector<double> res;
|
|
|
|
res.resize(iterations);
|
2018-04-26 09:36:07 +08:00
|
|
|
|
2018-05-07 19:33:23 +08:00
|
|
|
#if !defined(__linux__)
|
|
|
|
#define SQUASH_COUNTERS
|
2018-12-19 11:18:23 +08:00
|
|
|
if(justdata) {
|
|
|
|
printf("justdata (-t) flag only works under linux.\n");
|
|
|
|
}
|
2018-05-07 19:33:23 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SQUASH_COUNTERS
|
2018-08-21 05:27:25 +08:00
|
|
|
vector<int> evts;
|
|
|
|
evts.push_back(PERF_COUNT_HW_CPU_CYCLES);
|
|
|
|
evts.push_back(PERF_COUNT_HW_INSTRUCTIONS);
|
2018-10-04 21:47:34 +08:00
|
|
|
evts.push_back(PERF_COUNT_HW_BRANCH_MISSES);
|
2018-11-28 23:53:57 +08:00
|
|
|
evts.push_back(PERF_COUNT_HW_CACHE_REFERENCES);
|
|
|
|
evts.push_back(PERF_COUNT_HW_CACHE_MISSES);
|
2018-08-21 05:27:25 +08:00
|
|
|
LinuxEvents<PERF_TYPE_HARDWARE> unified(evts);
|
|
|
|
vector<u64> results;
|
|
|
|
results.resize(evts.size());
|
2018-12-07 11:34:18 +08:00
|
|
|
unsigned long cy0 = 0, cy1 = 0, cy2 = 0, cy3 = 0;
|
|
|
|
unsigned long cl0 = 0, cl1 = 0, cl2 = 0, cl3 = 0;
|
|
|
|
unsigned long mis0 = 0, mis1 = 0, mis2 = 0, mis3 = 0;
|
|
|
|
unsigned long cref0 = 0, cref1 = 0, cref2 = 0, cref3 = 0;
|
|
|
|
unsigned long cmis0 = 0, cmis1 = 0, cmis2 = 0, cmis3 = 0;
|
2018-04-26 09:36:07 +08:00
|
|
|
#endif
|
2018-08-21 05:27:25 +08:00
|
|
|
bool isok = true;
|
2018-11-10 10:31:14 +08:00
|
|
|
|
2018-08-21 05:27:25 +08:00
|
|
|
for (u32 i = 0; i < iterations; i++) {
|
2018-11-10 10:31:14 +08:00
|
|
|
if(verbose) cout << "[verbose] iteration # " << i << endl;
|
2018-12-07 11:34:18 +08:00
|
|
|
#ifndef SQUASH_COUNTERS
|
|
|
|
unified.start();
|
|
|
|
#endif
|
|
|
|
ParsedJson pj;
|
|
|
|
bool allocok = pj.allocateCapacity(p.size());
|
|
|
|
if(!allocok) {
|
|
|
|
std::cerr << "failed to allocate memory" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
#ifndef SQUASH_COUNTERS
|
|
|
|
unified.end(results);
|
|
|
|
cy0 += results[0];
|
|
|
|
cl0 += results[1];
|
|
|
|
mis0 += results[2];
|
|
|
|
cref0 += results[3];
|
|
|
|
cmis0 += results[4];
|
|
|
|
#endif
|
|
|
|
if(verbose) cout << "[verbose] allocated memory for parsed JSON " << endl;
|
|
|
|
|
2018-08-21 05:27:25 +08:00
|
|
|
auto start = std::chrono::steady_clock::now();
|
2018-05-07 19:33:23 +08:00
|
|
|
#ifndef SQUASH_COUNTERS
|
2018-08-21 05:27:25 +08:00
|
|
|
unified.start();
|
2018-04-26 09:36:07 +08:00
|
|
|
#endif
|
2018-12-01 10:31:05 +08:00
|
|
|
isok = find_structural_bits(p.data(), p.size(), pj);
|
2018-05-07 19:33:23 +08:00
|
|
|
#ifndef SQUASH_COUNTERS
|
2018-08-21 05:27:25 +08:00
|
|
|
unified.end(results);
|
|
|
|
cy1 += results[0];
|
|
|
|
cl1 += results[1];
|
2018-10-04 21:47:34 +08:00
|
|
|
mis1 += results[2];
|
2018-11-28 23:53:57 +08:00
|
|
|
cref1 += results[3];
|
|
|
|
cmis1 += results[4];
|
2018-09-24 08:42:30 +08:00
|
|
|
if (!isok) {
|
2018-09-24 08:54:29 +08:00
|
|
|
cout << "Failed out during stage 1\n";
|
2018-08-21 05:27:25 +08:00
|
|
|
break;
|
2018-09-24 08:42:30 +08:00
|
|
|
}
|
2018-08-21 05:27:25 +08:00
|
|
|
unified.start();
|
2018-04-26 09:36:07 +08:00
|
|
|
#endif
|
2018-12-01 09:27:16 +08:00
|
|
|
isok = isok && flatten_indexes(p.size(), pj);
|
2018-05-07 19:33:23 +08:00
|
|
|
#ifndef SQUASH_COUNTERS
|
2018-08-21 05:27:25 +08:00
|
|
|
unified.end(results);
|
|
|
|
cy2 += results[0];
|
|
|
|
cl2 += results[1];
|
2018-10-04 21:47:34 +08:00
|
|
|
mis2 += results[2];
|
2018-11-28 23:53:57 +08:00
|
|
|
cref2 += results[3];
|
|
|
|
cmis2 += results[4];
|
2018-09-24 08:42:30 +08:00
|
|
|
if (!isok) {
|
2018-09-24 08:54:29 +08:00
|
|
|
cout << "Failed out during stage 2\n";
|
2018-08-21 05:27:25 +08:00
|
|
|
break;
|
2018-09-24 08:42:30 +08:00
|
|
|
}
|
2018-08-21 05:27:25 +08:00
|
|
|
unified.start();
|
2018-04-26 09:36:07 +08:00
|
|
|
#endif
|
2018-09-24 08:42:30 +08:00
|
|
|
|
2018-12-01 10:31:05 +08:00
|
|
|
isok = isok && unified_machine(p.data(), p.size(), pj);
|
2018-09-24 08:42:30 +08:00
|
|
|
#ifndef SQUASH_COUNTERS
|
|
|
|
unified.end(results);
|
|
|
|
cy3 += results[0];
|
|
|
|
cl3 += results[1];
|
2018-10-04 21:47:34 +08:00
|
|
|
mis3 += results[2];
|
2018-11-28 23:53:57 +08:00
|
|
|
cref3 += results[3];
|
|
|
|
cmis3 += results[4];
|
2018-09-24 08:42:30 +08:00
|
|
|
if (!isok) {
|
2018-09-24 08:54:29 +08:00
|
|
|
cout << "Failed out during stage 34\n";
|
2018-08-21 05:27:25 +08:00
|
|
|
break;
|
2018-09-24 08:42:30 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-21 05:27:25 +08:00
|
|
|
auto end = std::chrono::steady_clock::now();
|
|
|
|
std::chrono::duration<double> secs = end - start;
|
|
|
|
res[i] = secs.count();
|
|
|
|
}
|
2018-12-07 11:34:18 +08:00
|
|
|
ParsedJson pj = build_parsed_json(p); // do the parsing again to get the stats
|
|
|
|
if( ! pj.isValid() ) {
|
2018-12-11 03:25:49 +08:00
|
|
|
std::cerr << "Could not parse. " << std::endl;
|
2018-12-07 11:34:18 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-05-07 19:33:23 +08:00
|
|
|
#ifndef SQUASH_COUNTERS
|
2018-12-19 11:18:23 +08:00
|
|
|
if(justdata) {
|
|
|
|
float cpb0 = (double)cy0 / (iterations * p.size());
|
|
|
|
float cpb1 = (double)cy1 / (iterations * p.size());
|
|
|
|
float cpb2 = (double)cy2 / (iterations * p.size());
|
|
|
|
float cpb3 = (double)cy3 / (iterations * p.size());
|
|
|
|
float cpbtotal = (double)total / (iterations * p.size());
|
|
|
|
printf("\"%s\"\t%f\t%f\t%f\t%f\t%f\n", basename(filename), cpb0,cpb1,cpb2,cpb3,cpbtotal);
|
|
|
|
} else {
|
2018-11-28 04:05:50 +08:00
|
|
|
printf("number of bytes %ld number of structural chars %u ratio %.3f\n",
|
2018-12-01 09:27:16 +08:00
|
|
|
p.size(), pj.n_structural_indexes,
|
|
|
|
(double)pj.n_structural_indexes / p.size());
|
2018-12-07 11:34:18 +08:00
|
|
|
unsigned long total = cy0 + cy1 + cy2 + cy3;
|
|
|
|
printf(
|
|
|
|
"mem alloc instructions: %10lu cycles: %10lu (%.2f %%) ins/cycles: %.2f mis. branches: %10lu (cycles/mis.branch %.2f) cache accesses: %10lu (failure %10lu)\n",
|
|
|
|
cl0 / iterations, cy0 / iterations, 100. * cy0 / total, (double)cl0 / cy0, mis0/iterations, (double)cy0/mis0, cref1 / iterations, cmis0 / iterations);
|
|
|
|
printf(" mem alloc runs at %.2f cycles per input byte.\n",
|
|
|
|
(double)cy0 / (iterations * p.size()));
|
2018-08-21 05:27:25 +08:00
|
|
|
printf(
|
2018-11-28 23:53:57 +08:00
|
|
|
"stage 1 instructions: %10lu cycles: %10lu (%.2f %%) ins/cycles: %.2f mis. branches: %10lu (cycles/mis.branch %.2f) cache accesses: %10lu (failure %10lu)\n",
|
|
|
|
cl1 / iterations, cy1 / iterations, 100. * cy1 / total, (double)cl1 / cy1, mis1/iterations, (double)cy1/mis1, cref1 / iterations, cmis1 / iterations);
|
2018-08-21 05:27:25 +08:00
|
|
|
printf(" stage 1 runs at %.2f cycles per input byte.\n",
|
2018-12-01 09:27:16 +08:00
|
|
|
(double)cy1 / (iterations * p.size()));
|
2018-08-21 05:27:25 +08:00
|
|
|
|
|
|
|
printf(
|
2018-11-28 23:53:57 +08:00
|
|
|
"stage 2 instructions: %10lu cycles: %10lu (%.2f %%) ins/cycles: %.2f mis. branches: %10lu (cycles/mis.branch %.2f) cache accesses: %10lu (failure %10lu)\n",
|
|
|
|
cl2 / iterations, cy2 / iterations, 100. * cy2 / total, (double)cl2 / cy2, mis2/iterations, (double)cy2/mis2, cref2 /iterations, cmis2 / iterations);
|
2018-08-21 05:27:25 +08:00
|
|
|
printf(" stage 2 runs at %.2f cycles per input byte and ",
|
2018-12-01 09:27:16 +08:00
|
|
|
(double)cy2 / (iterations * p.size()));
|
2018-08-21 05:27:25 +08:00
|
|
|
printf("%.2f cycles per structural character.\n",
|
|
|
|
(double)cy2 / (iterations * pj.n_structural_indexes));
|
|
|
|
|
|
|
|
printf(
|
2018-11-28 23:53:57 +08:00
|
|
|
"stage 3 instructions: %10lu cycles: %10lu (%.2f %%) ins/cycles: %.2f mis. branches: %10lu (cycles/mis.branch %.2f) cache accesses: %10lu (failure %10lu)\n",
|
|
|
|
cl3 / iterations, cy3 /iterations, 100. * cy3 / total, (double)cl3 / cy3, mis3/iterations, (double)cy3/mis3, cref3 / iterations, cmis3 / iterations);
|
2018-08-21 05:27:25 +08:00
|
|
|
printf(" stage 3 runs at %.2f cycles per input byte and ",
|
2018-12-01 09:27:16 +08:00
|
|
|
(double)cy3 / (iterations * p.size()));
|
2018-08-21 05:27:25 +08:00
|
|
|
printf("%.2f cycles per structural character.\n",
|
|
|
|
(double)cy3 / (iterations * pj.n_structural_indexes));
|
|
|
|
|
|
|
|
printf(" all stages: %.2f cycles per input byte.\n",
|
2018-12-01 09:27:16 +08:00
|
|
|
(double)total / (iterations * p.size()));
|
2018-12-19 11:18:23 +08:00
|
|
|
}
|
2018-04-26 09:36:07 +08:00
|
|
|
#endif
|
2018-08-21 05:27:25 +08:00
|
|
|
double min_result = *min_element(res.begin(), res.end());
|
2018-12-19 11:18:23 +08:00
|
|
|
if(!justdata) cout << "Min: " << min_result << " bytes read: " << p.size()
|
2018-12-01 09:27:16 +08:00
|
|
|
<< " Gigabytes/second: " << (p.size()) / (min_result * 1000000000.0)
|
2018-08-21 05:27:25 +08:00
|
|
|
<< "\n";
|
2018-12-07 06:40:32 +08:00
|
|
|
if(jsonoutput) {
|
2018-12-11 04:16:31 +08:00
|
|
|
isok = isok && pj.printjson(std::cout);
|
2018-12-07 06:40:32 +08:00
|
|
|
}
|
|
|
|
if(dump) {
|
2018-12-11 04:16:31 +08:00
|
|
|
isok = isok && pj.dump_raw_tape(std::cout);
|
2018-12-07 06:40:32 +08:00
|
|
|
}
|
2018-12-11 11:21:03 +08:00
|
|
|
free((void*)p.data());
|
2018-08-21 05:27:25 +08:00
|
|
|
if (!isok) {
|
|
|
|
printf(" Parsing failed. \n ");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
2018-03-23 12:05:32 +08:00
|
|
|
}
|