Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
JITとインタプリタの性能計測
/*
* 動作環境
* CPU: Intel Core i5 6200 @2.30GHz
* RAM: 16GB
*
* 出力結果
* ret: 1
* int latency: 1840760982
* jit latency: 113407004
*/
#include <stdio.h>
#include <ubpf.h>
#include <slankdev/exception.h>
#include <slankdev/system.h>
#include <slankdev/hexdump.h>
#include <slankdev/extra/pcap.h>
class ubpf {
public:
struct ubpf_vm* vm;
ubpf() : vm(nullptr)
{
vm = ubpf_create();
if (!vm) {
throw slankdev::exception("ubpf_create");
}
}
~ubpf() { if (vm) ubpf_destroy(vm); }
void reg(uint32_t idx, const char* name, void* fn)
{
int ret = ubpf_register(vm, idx, name, fn);
if (ret < 0) {
throw slankdev::exception("ubpf_register");
}
}
void load(const void* code, uint32_t code_len)
{
char* p;
int ret = ubpf_load(vm, code, code_len, &p);
if (ret < 0) {
std::string str = "ubpf_load: ";
str += p;
throw slankdev::exception(str.c_str());
}
}
int exec(void* mem, size_t mem_len)
{
int ret = ubpf_exec(vm, mem, mem_len);
return ret;
}
ubpf_jit_fn compile()
{
char* e;
ubpf_jit_fn ret = ubpf_compile(vm, &e);
if (!ret) {
std::string str = "ubpf_compile: ";
str += e;
throw slankdev::exception(str.c_str());
}
return ret;
}
};
const uint8_t raw[] = {
/* eBPF(tcp port 80) */
0x71, 0x12, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x71, 0x13, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
0x67, 0x03, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
0x4f, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xb7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x55, 0x03, 0x0c, 0x00, 0x08, 0x00, 0x00, 0x00,
0x71, 0x12, 0x17, 0x00, 0x00, 0x00, 0x00, 0x00,
0x55, 0x02, 0x0a, 0x00, 0x06, 0x00, 0x00, 0x00,
0x71, 0x12, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0x01, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00,
0x57, 0x02, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
0x67, 0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x0f, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x69, 0x12, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x15, 0x02, 0x02, 0x00, 0x00, 0x50, 0x00, 0x00,
0x69, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x55, 0x01, 0x01, 0x00, 0x00, 0x50, 0x00, 0x00,
0xb7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x95, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
const uint8_t packet[] = {
/* arp packet */
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x08, 0x06, 0x00, 0x01,
// 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
// 0x00, 0x00,
/* tcp packet */
0x34, 0xf3, 0x9a, 0xc4, 0xe1, 0x81, 0x00, 0xe0,
0x4d, 0x10, 0xa2, 0x2c, 0x08, 0x00, 0x45, 0x00,
0x00, 0x3c, 0x00, 0x00, 0x40, 0x00, 0x33, 0x06,
0x20, 0xc2, 0xa3, 0x2c, 0xa5, 0x31, 0x0a, 0xce,
0xd3, 0xce, 0x00, 0x50, 0x8f, 0xea, 0xa9, 0x37,
0x9f, 0x1b, 0xb8, 0x60, 0x93, 0xf4, 0xa0, 0x12,
0x71, 0x20, 0xd8, 0xb9, 0x00, 0x00, 0x02, 0x04,
0x05, 0x76, 0x04, 0x02, 0x08, 0x0a, 0x77, 0x4c,
0xa1, 0xc2, 0x00, 0x75, 0x98, 0xf2, 0x01, 0x03,
0x03, 0x07,
};
int main()
{
char ss[100] = "slankdev";
int ret;
uint64_t before;
ubpf vm;
vm.load(raw, sizeof(raw));
before = slankdev::rdtsc();
for (int i=0; i<10000000; i++)
ret = vm.exec((void*)packet, sizeof(packet));
uint64_t int_latency = slankdev::rdtsc() - before;
before = slankdev::rdtsc();
ubpf_jit_fn func = vm.compile();
for (int i=0; i<10000000; i++)
ret = func((void*)packet, sizeof(packet));
uint64_t jit_latency = slankdev::rdtsc() - before;
printf("ret: %d\n", ret);
printf("int latency: %lu\n", int_latency);
printf("jit latency: %lu\n", jit_latency);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment