Compare commits

...

5 commits

Author SHA1 Message Date
9843dd3062 bench: add a Makefile to generate csmith tests 2018-06-04 14:55:23 +02:00
6bdeda910f bench: add validate_csmith
Check that a csmith-generated file is interresting enough to be worth
keeping and compiling.
2018-06-04 14:53:59 +02:00
11a218b6ab Add benchlib 2018-06-04 14:46:55 +02:00
f872119189 Refactor generate_eh_elf args handling
Use a `config` class
2018-06-04 13:57:21 +02:00
2f91f732cb Add two csmith benching scripts 2018-05-18 11:39:01 +02:00
13 changed files with 601 additions and 48 deletions

1
.gitignore vendored
View file

@ -33,3 +33,4 @@
dwarf-assembly
__pycache__
platform.info

View file

@ -0,0 +1,103 @@
#include "DwBenchmark.hpp"
#include <ostream>
#include <cassert>
#include <chrono>
#ifdef UNWIND_EH_ELF
#include "../../stack_walker/stack_walker.hpp"
#endif
#ifdef UNWIND_LIBUNWIND
#include <libunwind.h>
#endif
using namespace std;
unique_ptr<DwBenchmark> DwBenchmark::instance = nullptr;
DwBenchmark::DwBenchmark() {
#ifdef UNWIND_EH_ELF
stack_walker_init();
#endif
}
DwBenchmark& DwBenchmark::get_instance() {
if(!DwBenchmark::instance)
instance = unique_ptr<DwBenchmark>(new DwBenchmark);
return *instance;
}
void DwBenchmark::unwind_measure() {
#ifdef UNWIND_EH_ELF
unwind_context_t context = get_context();
SingleUnwind this_measure;
this_measure.nb_frames = 0;
auto start_time = chrono::high_resolution_clock::now();
while(unwind_context(context)) {
this_measure.nb_frames++;
}
auto end_time = chrono::high_resolution_clock::now();
this_measure.nanoseconds = chrono::duration_cast<chrono::nanoseconds>(
end_time - start_time).count();
add_measure(this_measure);
#elif UNWIND_LIBUNWIND
unw_context_t context;
int rc = unw_getcontext(&context);
if(rc < 0)
assert(false);
unw_cursor_t cursor;
rc = unw_init_local(&cursor, &context);
if(rc < 0)
assert(false);
SingleUnwind this_measure;
this_measure.nb_frames = 0;
auto start_time = chrono::high_resolution_clock::now();
while(unw_step(&cursor) > 0) {
this_measure.nb_frames++;
}
auto end_time = chrono::high_resolution_clock::now();
this_measure.nanoseconds = chrono::duration_cast<chrono::nanoseconds>(
end_time - start_time).count();
add_measure(this_measure);
#else
assert(false);
#endif
}
void DwBenchmark::add_measure(const SingleUnwind& measure) {
measures.push_back(measure);
}
void DwBenchmark::add_measure(int nb_frames, size_t microseconds) {
add_measure(SingleUnwind({nb_frames, microseconds}));
}
void DwBenchmark::format_output(std::ostream& os) const {
size_t nb_unwind_frames = 0;
size_t total_nanoseconds = 0;
for(const auto& measure: measures) {
nb_unwind_frames += measure.nb_frames;
total_nanoseconds += measure.nanoseconds;
}
double clock_precision_ns =
((double)
(1000*1000*1000 * std::chrono::high_resolution_clock::period::num))
/ ((double) std::chrono::high_resolution_clock::period::den);
os << "Total time: " << total_nanoseconds << "ns" << endl
<< "Total frames: " << nb_unwind_frames << endl
<< "Avg frames/unwind: "
<< (double)nb_unwind_frames / (double)measures.size() << endl
<< "Avg time/frame: "
<< (double)total_nanoseconds / nb_unwind_frames << "ns" << endl
<< "Clock precision: " << clock_precision_ns << "ns" << endl;
}

View file

@ -0,0 +1,32 @@
#pragma once
#include <memory>
#include <vector>
class DwBenchmark {
/** Singleton class - keeps track of benchmarks performed during a run */
public:
struct SingleUnwind {
int nb_frames;
size_t nanoseconds;
};
static DwBenchmark& get_instance();
void unwind_measure(); ///< Unwind from here, and add the measure
void add_measure(const SingleUnwind& measure); ///< Add this measure
void add_measure(int nb_frames, size_t microseconds);
/** Dump formatted output on `os` displaying stats about this benchmark
* run */
void format_output(std::ostream& os) const;
private:
DwBenchmark();
static std::unique_ptr<DwBenchmark> instance;
std::vector<SingleUnwind> measures;
};

View file

@ -0,0 +1,32 @@
TARGETS=libbench.eh_elf.so libbench.unwind.so
COMMON_OBJS=bench.o
EH_ELF_OBJS=DwBenchmark.eh_elf.o
UNWIND_OBJS=DwBenchmark.unwind.o
CXX=g++
CXXFLAGS=-Wall -Wextra -O2 -std=c++14
CXXLIBS=
all: $(TARGETS)
libbench.eh_elf.so: $(EH_ELF_OBJS) $(COMMON_OBJS)
$(CXX) $(CXXFLAGS) -shared -o $@ $^ $(CXXLIBS) \
-L../../stack_walker -lstack_walker.global
libbench.unwind.so: $(UNWIND_OBJS) $(COMMON_OBJS)
$(CXX) $(CXXFLAGS) -shared -o $@ $^ $(CXXLIBS) \
-lunwind -lunwind-x86_64
%.eh_elf.o: %.cpp
$(CXX) $(CXXFLAGS) -fPIC -DUNWIND_EH_ELF -o $@ -c $<
%.unwind.o: %.cpp
$(CXX) $(CXXFLAGS) -fPIC -DUNWIND_LIBUNWIND -o $@ -c $<
%.o: %.cpp
$(CXX) $(CXXFLAGS) -fPIC -o $@ -c $<
.PHONY: clean
clean:
rm *.o $(TARGETS)

View file

@ -0,0 +1,12 @@
#include "bench.h"
#include <iostream>
#include "DwBenchmark.hpp"
void bench_unwinding() {
DwBenchmark::get_instance().unwind_measure();
}
void bench_dump_data() {
DwBenchmark::get_instance().format_output(std::cout);
}

16
benching/benchlib/bench.h Normal file
View file

@ -0,0 +1,16 @@
/** Benchmarking library: a collection of functions that can be called to
* benchmark dwarf-assembly
**/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
void bench_unwinding();
void bench_dump_data();
#ifdef __cplusplus
} // END extern "C"
#endif

1
benching/csmith/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
tests/

47
benching/csmith/Makefile Normal file
View file

@ -0,0 +1,47 @@
CSMITH=/home/tobast/root/bin/csmith
CC=gcc
CCFLAGS=-O3 -w # Remove warnings: these are not meaningful
CCLIBS=-L../benchlib -I../benchlib -I/usr/include/csmith-2.3.0/
CC_UNWIND=-lbench.unwind
CC_EH_ELF=-lbench.eh_elf -L../../stack_walker -lstack_walker.global
REMOTE=seneve
SH=bash
.SECONDARY:
.PHONY: tests/%.allbin
tests/%.allbin: tests/%.eh_elf.bin tests/%.unwind.bin ;
tests/%.raw.c:
while \
( ssh seneve $(CSMITH) --max-funcs 100 > $@ ) ;\
! ./validate_csmith.py "$@" ; \
do :; done
tests/%.c: tests/%.raw.c
cat $< \
| sed 's/#include "csmith.h"/#include <bench.h>\n#include <csmith.h>/g' \
| sed 's/return /bench_unwinding(); return /g' \
| head -n $$(expr 1 + $$(grep -n "func_1();$$" $< | cut -f1 -d':')) \
> $@
echo -e "\tbench_dump_data();\n\treturn 0;\n}\n" \
>> $@
rm -f $<
tests/%.unwind.bin: tests/%.c
LD_RUN_PATH=$$(readlink -f "../benchlib") \
$(CC) $(CCFLAGS) $(CCLIBS) \
$(CC_UNWIND) \
$^ -o $@
tests/%.eh_elf.bin: tests/%.c
LD_RUN_PATH=$$(readlink -f "../benchlib"):$$(readlink -f "../../stack_walker"):eh_elfs \
$(CC) $(CCFLAGS) $(CCLIBS) \
$(CC_UNWIND) \
$^ -o $@
mkdir -p tests/eh_elfs
../../generate_eh_elf.py --remote seneve --deps -o tests/eh_elfs \
-O3 --global-switch $@
clean:
rm -f tests/*c tests/*bin tests/eh_elfs/*.eh_elf.bin.eh_elf.so

View file

@ -0,0 +1,5 @@
#!/bin/bash
csmith "$@" | \
sed 's/#include "csmith.h"/#include <bench.h>\n#include <csmith.h>/g' |\
sed 's/return /bench_unwinding(); return /g'

124
benching/csmith/gen_call_graph.py Executable file
View file

@ -0,0 +1,124 @@
#!/usr/bin/env python3
""" Generates the call graph (in dot format) of a C code generated by CSmith.
This does not parse C code, but performs only string lookups. In particular, it
assumes functions are named `func_[0-9]+` or `main`, and that a function
implementation is of the form (whitespaces meaningful)
(static)? RETURN_TYPE func_[0-9]+(.*)
{
...
}
"""
import sys
import re
def build_graph(prog):
func_declare_re = re.compile(r'(?:static )?\S.* (func_\d+|main) ?\(.*\)$')
func_call_re = re.compile(r'func_\d+')
graph = {}
cur_function = None
for line in prog:
func_declare_groups = func_declare_re.match(line)
if func_declare_groups:
func_name = func_declare_groups.group(1)
cur_function = func_name
graph[func_name] = []
elif line == '}':
cur_function = None
else:
if cur_function is None:
continue # Not interresting outside of functions
last_find_pos = 0
call_match = func_call_re.search(line, pos=last_find_pos)
while call_match is not None:
graph[cur_function].append(call_match.group(0))
last_find_pos = call_match.end()
call_match = func_call_re.search(line, pos=last_find_pos)
reachable = set()
def mark_reachable(node):
nonlocal reachable, graph
if node in reachable:
return
reachable.add(node)
for child in graph[node]:
mark_reachable(child)
mark_reachable('main')
delete = []
for node in graph:
if node not in reachable:
delete.append(node)
for node in delete:
print('> Deleted: {}'.format(node), file=sys.stderr)
graph.pop(node)
return graph
def dump_graph(graph):
print('digraph prog {')
for node in graph:
for call in graph[node]:
if call in graph:
print('\t{} -> {}'.format(node, call))
else:
print('Wtf is {} (called from {})?'.format(node, call),
file=sys.stderr)
print('}')
def dump_stats(graph, out_file):
entry_point = 'main'
depth_of = {}
def find_depth(node):
nonlocal depth_of
if node in depth_of:
return depth_of[node]
callees = graph[node]
if callees:
depth = max(map(find_depth, callees)) + 1
else:
depth = 1
depth_of[node] = depth
return depth
print("Call chain max depth: {}".format(find_depth(entry_point)),
file=out_file)
def get_prog_lines():
def do_read(handle):
return handle.readlines()
if len(sys.argv) > 1:
with open(sys.argv[1], 'r') as handle:
return do_read(handle)
else:
return do_read(sys.stdin)
def main():
prog = get_prog_lines()
graph = build_graph(prog)
dump_graph(graph)
dump_stats(graph, out_file=sys.stderr)
if __name__ == '__main__':
main()

View file

@ -0,0 +1,112 @@
#!/usr/bin/env python3
""" Checks whether a Csmith-generated file is complicated enough
This does not parse C code, but performs only string lookups. In particular, it
assumes functions are named `func_[0-9]+` or `main`, and that a function
implementation is of the form (whitespaces meaningful)
(static)? RETURN_TYPE func_[0-9]+(.*)
{
...
}
"""
import sys
import re
def build_graph(prog):
func_declare_re = re.compile(r'(?:static )?\S.* (func_\d+|main) ?\(.*\)$')
func_call_re = re.compile(r'func_\d+')
graph = {}
cur_function = None
for line in prog:
func_declare_groups = func_declare_re.match(line)
if func_declare_groups:
func_name = func_declare_groups.group(1)
cur_function = func_name
graph[func_name] = []
elif line == '}':
cur_function = None
else:
if cur_function is None:
continue # Not interresting outside of functions
last_find_pos = 0
call_match = func_call_re.search(line, pos=last_find_pos)
while call_match is not None:
graph[cur_function].append(call_match.group(0))
last_find_pos = call_match.end()
call_match = func_call_re.search(line, pos=last_find_pos)
reachable = set()
def mark_reachable(node):
nonlocal reachable, graph
if node in reachable:
return
reachable.add(node)
for child in graph[node]:
mark_reachable(child)
mark_reachable('main')
delete = []
for node in graph:
if node not in reachable:
delete.append(node)
for node in delete:
graph.pop(node)
return graph
def get_depth(graph):
entry_point = 'main'
depth_of = {}
def find_depth(node):
nonlocal depth_of
if node in depth_of:
return depth_of[node]
callees = graph[node]
if callees:
depth = max(map(find_depth, callees)) + 1
else:
depth = 1
depth_of[node] = depth
return depth
return find_depth(entry_point)
def get_prog_lines():
def do_read(handle):
return handle.readlines()
if len(sys.argv) > 1:
with open(sys.argv[1], 'r') as handle:
return do_read(handle)
else:
return do_read(sys.stdin)
def main():
prog = get_prog_lines()
graph = build_graph(prog)
if len(graph) < 5 or get_depth(graph) < 5:
print("Graph is too simple.", file=sys.stderr)
sys.exit(1)
sys.exit(0)
if __name__ == '__main__':
main()

View file

@ -11,6 +11,7 @@ import sys
import subprocess
import tempfile
import argparse
from enum import Enum
from shared_python import elf_so_deps, do_remote, is_newer
from extract_pc import generate_pc_list
@ -24,15 +25,53 @@ C_BIN = (
else os.environ['C'])
def gen_dw_asm_c(obj_path, out_path, dwarf_assembly_args):
class SwitchGenPolicy(Enum):
''' The various switch generation policies possible '''
SWITCH_PER_FUNC = '--switch-per-func'
GLOBAL_SWITCH = '--global-switch'
class Config:
''' Holds the run's settings '''
def __init__(self,
output,
objects,
sw_gen_policy=SwitchGenPolicy.GLOBAL_SWITCH,
force=False,
use_pc_list=False,
c_opt_level='3',
remote=None):
self.output = output
self.objects = objects
self.sw_gen_policy = sw_gen_policy
self.force = force
self.use_pc_list = use_pc_list
self.c_opt_level = c_opt_level
self.remote = remote
def dwarf_assembly_args(self):
''' Arguments to `dwarf_assembly` '''
return [self.sw_gen_policy.value]
def opt_level(self):
''' The optimization level to pass to gcc '''
return '-O{}'.format(self.c_opt_level)
def gen_dw_asm_c(obj_path, out_path, config, pc_list_path=None):
''' Generate the C code produced by dwarf-assembly from `obj_path`, saving
it as `out_path` '''
dw_assembly_args = config.dwarf_assembly_args()
if pc_list_path is not None:
dw_assembly_args += ['--pc-list', pc_list_path]
try:
with open(out_path, 'w') as out_handle:
# TODO enhance error handling
dw_asm_output = subprocess.check_output(
[DWARF_ASSEMBLY_BIN, obj_path] + dwarf_assembly_args) \
[DWARF_ASSEMBLY_BIN, obj_path] + dw_assembly_args) \
.decode('utf-8')
out_handle.write(dw_asm_output)
except subprocess.CalledProcessError as exn:
@ -44,18 +83,15 @@ def gen_dw_asm_c(obj_path, out_path, dwarf_assembly_args):
exn.returncode))
def gen_eh_elf(obj_path, args, dwarf_assembly_args=None):
def gen_eh_elf(obj_path, config):
''' Generate the eh_elf corresponding to `obj_path`, saving it as
`out_dir/$(basename obj_path).eh_elf.so` (or in the current working
directory if out_dir is None) '''
if args.output is None:
if config.output is None:
out_dir = '.'
else:
out_dir = args.output
if dwarf_assembly_args is None:
dwarf_assembly_args = []
out_dir = config.output
print("> {}...".format(os.path.basename(obj_path)))
@ -63,43 +99,41 @@ def gen_eh_elf(obj_path, args, dwarf_assembly_args=None):
out_so_path = os.path.join(out_dir, (out_base_name + '.so'))
pc_list_dir = os.path.join(out_dir, 'pc_list')
if is_newer(out_so_path, obj_path) and not args.force:
if is_newer(out_so_path, obj_path) and not config.force:
return # The object is recent enough, no need to recreate it
with tempfile.TemporaryDirectory() as compile_dir:
# Generate PC list
if args.use_pc_list:
pc_list_path = None
if config.use_pc_list:
pc_list_path = \
os.path.join(pc_list_dir, out_base_name + '.pc_list')
os.makedirs(pc_list_dir, exist_ok=True)
print('\tGenerating PC list…')
generate_pc_list(obj_path, pc_list_path)
dwarf_assembly_args += ['--pc-list', pc_list_path]
# Generate the C source file
print("\tGenerating C…")
c_path = os.path.join(compile_dir, (out_base_name + '.c'))
gen_dw_asm_c(obj_path, c_path, dwarf_assembly_args)
gen_dw_asm_c(obj_path, c_path, config, pc_list_path)
# Compile it into a .o
print("\tCompiling into .o…")
o_path = os.path.join(compile_dir, (out_base_name + '.o'))
opt_level = args.c_opt_level
if opt_level is None:
opt_level = '-O3'
if args.remote:
if config.remote:
remote_out = do_remote(
args.remote,
config.remote,
[C_BIN,
'-o', out_base_name + '.o',
'-c', out_base_name + '.c',
opt_level, '-fPIC'],
config.opt_level(), '-fPIC'],
send_files=[c_path],
retr_files=[(out_base_name + '.o', o_path)])
call_rc = 1 if remote_out is None else 0
else:
call_rc = subprocess.call(
[C_BIN, '-o', o_path, '-c', c_path, opt_level, '-fPIC'])
[C_BIN, '-o', o_path, '-c', c_path,
config.opt_level(), '-fPIC'])
if call_rc != 0:
raise Exception("Failed to compile to a .o file")
@ -111,15 +145,38 @@ def gen_eh_elf(obj_path, args, dwarf_assembly_args=None):
raise Exception("Failed to compile to a .so file")
def gen_all_eh_elf(obj_path, args, dwarf_assembly_args=None):
def gen_all_eh_elf(obj_path, config):
''' Call `gen_eh_elf` on obj_path and all its dependencies '''
if dwarf_assembly_args is None:
dwarf_assembly_args = []
deps = elf_so_deps(obj_path)
deps.append(obj_path)
for dep in deps:
gen_eh_elf(dep, args, dwarf_assembly_args)
gen_eh_elf(dep, config)
def gen_eh_elfs(obj_path,
out_dir,
global_switch=True,
deps=True,
remote=None):
''' Call gen{_all,}_eh_elf with args setup accordingly with the given
options '''
switch_gen_policy = (
SwitchGenPolicy.GLOBAL_SWITCH if global_switch
else SwitchGenPolicy.SWITCH_PER_FUNC
)
config = Config(
out_dir,
[obj_path],
sw_gen_policy=switch_gen_policy,
remote=remote,
)
if deps:
return gen_all_eh_elf([obj_path], config)
else:
return gen_eh_elf([obj_path], config)
def process_args():
@ -150,35 +207,40 @@ def process_args():
"ELF."))
# c_opt_level
opt_level_grp = parser.add_mutually_exclusive_group()
opt_level_grp.add_argument('-O0', action='store_const', const='-O0',
opt_level_grp.add_argument('-O0', action='store_const', const='0',
dest='c_opt_level',
help=("Compile C file with this optimization "
"level."))
opt_level_grp.add_argument('-O1', action='store_const', const='-O1',
opt_level_grp.add_argument('-O1', action='store_const', const='1',
dest='c_opt_level',
help=("Compile C file with this optimization "
"level."))
opt_level_grp.add_argument('-O2', action='store_const', const='-O2',
opt_level_grp.add_argument('-O2', action='store_const', const='2',
dest='c_opt_level',
help=("Compile C file with this optimization "
"level."))
opt_level_grp.add_argument('-O3', action='store_const', const='-O3',
opt_level_grp.add_argument('-O3', action='store_const', const='3',
dest='c_opt_level',
help=("Compile C file with this optimization "
"level."))
opt_level_grp.add_argument('-Os', action='store_const', const='-Os',
opt_level_grp.add_argument('-Os', action='store_const', const='s',
dest='c_opt_level',
help=("Compile C file with this optimization "
"level."))
opt_level_grp.set_defaults(c_opt_level='3')
switch_generation_policy = \
switch_gen_policy = \
parser.add_mutually_exclusive_group(required=True)
switch_generation_policy.add_argument('--switch-per-func',
action='store_const', const='',
help=("Passed to dwarf-assembly."))
switch_generation_policy.add_argument('--global-switch',
action='store_const', const='',
help=("Passed to dwarf-assembly."))
switch_gen_policy.add_argument('--switch-per-func',
dest='sw_gen_policy',
action='store_const',
const=SwitchGenPolicy.SWITCH_PER_FUNC,
help=("Passed to dwarf-assembly."))
switch_gen_policy.add_argument('--global-switch',
dest='sw_gen_policy',
action='store_const',
const=SwitchGenPolicy.GLOBAL_SWITCH,
help=("Passed to dwarf-assembly."))
parser.add_argument('object', nargs='+',
help="The ELF object(s) to process")
return parser.parse_args()
@ -186,20 +248,18 @@ def process_args():
def main():
args = process_args()
DW_ASSEMBLY_OPTS = {
'switch_per_func': '--switch-per-func',
'global_switch': '--global-switch',
}
dwarf_assembly_opts = []
args_dict = vars(args)
for opt in DW_ASSEMBLY_OPTS:
if opt in args and args_dict[opt] is not None:
dwarf_assembly_opts.append(DW_ASSEMBLY_OPTS[opt])
config = Config(
args.output,
args.object,
args.sw_gen_policy,
args.force,
args.use_pc_list,
args.c_opt_level,
args.remote,
)
for obj in args.object:
args.gen_func(obj, args, dwarf_assembly_opts)
args.gen_func(obj, config)
if __name__ == "__main__":

View file

@ -52,6 +52,9 @@ def do_remote(remote, command, send_files=None, retr_files=None):
The command is executed on the machine described by `remote` (see ssh(1)).
If `preload` is set, then the remote file at this path will be sourced
before running any command, allowing to set PATH and other variables.
send_files is a list of file paths that must be first copied at the root of
a temporary directory on `remote` before running the command. Consider
yourself jailed in that directory.
@ -64,6 +67,11 @@ def do_remote(remote, command, send_files=None, retr_files=None):
otherwise, on the local machine.
'''
if send_files is None:
send_files = []
if retr_files is None:
retr_files = []
def ssh_do(cmd_args, working_directory=None):
try:
cmd = ['ssh', remote]