From 2f54345cff3aea768bb89754654127a3b0ee58e9 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Thu, 24 Jul 2014 15:06:45 +0200 Subject: [PATCH] Added "cover" command --- Makefile | 2 +- kernel/driver.cc | 11 +--- kernel/log.h | 41 +++++++++++--- kernel/register.cc | 28 ++++++---- kernel/register.h | 9 +-- passes/cmds/Makefile.inc | 1 + passes/cmds/cover.cc | 115 +++++++++++++++++++++++++++++++++++++++ passes/cmds/tee.cc | 2 +- 8 files changed, 175 insertions(+), 34 deletions(-) create mode 100644 passes/cmds/cover.cc diff --git a/Makefile b/Makefile index 10bd9407e..272161ef4 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ TARGETS = yosys yosys-config all: top-all -CXXFLAGS = -Wall -Wextra -ggdb -I"$(shell pwd)" -MD -D_YOSYS_ -fPIC -I${DESTDIR}/include +CXXFLAGS = -Wall -Wextra -ggdb -I"$(shell pwd)" -MD -DYOSYS_SRC='"$(shell pwd)"' -D_YOSYS_ -fPIC -I${DESTDIR}/include LDFLAGS = -L${DESTDIR}/lib LDLIBS = -lstdc++ -lreadline -lm -ldl QMAKE = qmake-qt4 diff --git a/kernel/driver.cc b/kernel/driver.cc index 64165737e..a4556fb1b 100644 --- a/kernel/driver.cc +++ b/kernel/driver.cc @@ -768,16 +768,9 @@ int main(int argc, char **argv) log("\n", filename_buffer); - std::map> coverage_data; - for (CoverData *p = __start_yosys_cover_list; p != __stop_yosys_cover_list; p++) { - if (coverage_data.count(p->id)) - log("WARNING: found duplicate coverage id \"%s\".\n", p->id); - coverage_data[p->id].first = stringf("%s:%d:%s", p->file, p->line, p->func); - coverage_data[p->id].second += p->counter; - } + for (auto &it : get_coverage_data()) + fprintf(f, "%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str()); - for (auto &it : coverage_data) - fprintf(f, "%-40s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str()); fclose(f); } #endif diff --git a/kernel/log.h b/kernel/log.h index 804175909..0e8d08272 100644 --- a/kernel/log.h +++ b/kernel/log.h @@ -21,7 +21,10 @@ #define LOG_H #include "kernel/rtlil.h" +#include "kernel/register.h" + #include +#include #include #include #include @@ -68,22 +71,46 @@ void log_cell(RTLIL::Cell *cell, std::string indent = ""); // This is the magic behind the code coverage counters // --------------------------------------------------- +#ifndef NDEBUG + +#define cover(_id) do { \ + static CoverData __d __attribute__((section("yosys_cover_list"), aligned(1))) = { __FILE__, __FUNCTION__, _id, __LINE__, 0 }; \ + __d.counter++; \ +} while (0) + struct CoverData { const char *file, *func, *id; int line, counter; } __attribute__ ((packed)); // this two symbols are created by the linker for the "yosys_cover_list" ELF section -#ifndef NDEBUG extern "C" struct CoverData __start_yosys_cover_list[]; extern "C" struct CoverData __stop_yosys_cover_list[]; -#endif -#ifndef NDEBUG -# define cover(_id) do { \ - static CoverData __d __attribute__((section("yosys_cover_list"), aligned(1))) = { __FILE__, __FUNCTION__, _id, __LINE__, 0 }; \ - __d.counter++; \ - } while (0) +static inline std::map> get_coverage_data() +{ + std::map> coverage_data; + + for (auto &it : REGISTER_INTERN::pass_register) { + std::string key = stringf("passes.%s", it.first.c_str()); + coverage_data[key].first = stringf("%s:%d:%s", __FILE__, __LINE__, __FUNCTION__); + coverage_data[key].second += it.second->call_counter; + } + + for (CoverData *p = __start_yosys_cover_list; p != __stop_yosys_cover_list; p++) { + if (coverage_data.count(p->id)) + log("WARNING: found duplicate coverage id \"%s\".\n", p->id); + coverage_data[p->id].first = stringf("%s:%d:%s", p->file, p->line, p->func); + coverage_data[p->id].second += p->counter; + } + + for (auto &it : coverage_data) + if (!it.second.first.compare(0, strlen(YOSYS_SRC "/"), YOSYS_SRC "/")) + it.second.first = it.second.first.substr(strlen(YOSYS_SRC "/")); + + return coverage_data; +} + #else # define cover(_id) do { } while (0) #endif diff --git a/kernel/register.cc b/kernel/register.cc index 84051948f..e7ad7ef05 100644 --- a/kernel/register.cc +++ b/kernel/register.cc @@ -32,9 +32,7 @@ using namespace REGISTER_INTERN; namespace REGISTER_INTERN { bool echo_mode = false; - int raw_register_count = 0; - bool raw_register_done = false; - Pass *raw_register_array[MAX_REG_COUNT]; + Pass *first_queued_pass; std::map frontend_register; std::map pass_register; @@ -45,9 +43,9 @@ std::vector Frontend::next_args; Pass::Pass(std::string name, std::string short_help) : pass_name(name), short_help(short_help) { - assert(!raw_register_done); - assert(raw_register_count < MAX_REG_COUNT); - raw_register_array[raw_register_count++] = this; + next_queued_pass = first_queued_pass; + first_queued_pass = this; + call_counter = 0; } void Pass::run_register() @@ -58,11 +56,10 @@ void Pass::run_register() void Pass::init_register() { - if (raw_register_done) - done_register(); - while (raw_register_count > 0) - raw_register_array[--raw_register_count]->run_register(); - raw_register_done = true; + while (first_queued_pass) { + first_queued_pass->run_register(); + first_queued_pass = first_queued_pass->next_queued_pass; + } } void Pass::done_register() @@ -70,7 +67,7 @@ void Pass::done_register() frontend_register.clear(); pass_register.clear(); backend_register.clear(); - raw_register_done = false; + assert(first_queued_pass == NULL); } Pass::~Pass() @@ -191,6 +188,7 @@ void Pass::call(RTLIL::Design *design, std::vector args) log_cmd_error("No such command: %s (type 'help' for a command overview)\n", args[0].c_str()); size_t orig_sel_stack_pos = design->selection_stack.size(); + pass_register[args[0]]->call_counter++; pass_register[args[0]]->execute(args, design); while (design->selection_stack.size() > orig_sel_stack_pos) design->selection_stack.pop_back(); @@ -271,6 +269,7 @@ void Frontend::execute(std::vector args, RTLIL::Design *design) do { FILE *f = NULL; next_args.clear(); + call_counter++; execute(f, std::string(), args, design); args = next_args; fclose(f); @@ -334,9 +333,11 @@ void Frontend::frontend_call(RTLIL::Design *design, FILE *f, std::string filenam log_cmd_error("No such frontend: %s\n", args[0].c_str()); if (f != NULL) { + frontend_register[args[0]]->call_counter++; frontend_register[args[0]]->execute(f, filename, args, design); } else if (filename == "-") { FILE *f_stdin = stdin; // workaround for OpenBSD 'stdin' implementation + frontend_register[args[0]]->call_counter++; frontend_register[args[0]]->execute(f_stdin, "", args, design); } else { if (!filename.empty()) @@ -367,6 +368,7 @@ Backend::~Backend() void Backend::execute(std::vector args, RTLIL::Design *design) { FILE *f = NULL; + call_counter++; execute(f, std::string(), args, design); if (f != stdout) fclose(f); @@ -428,9 +430,11 @@ void Backend::backend_call(RTLIL::Design *design, FILE *f, std::string filename, size_t orig_sel_stack_pos = design->selection_stack.size(); if (f != NULL) { + backend_register[args[0]]->call_counter++; backend_register[args[0]]->execute(f, filename, args, design); } else if (filename == "-") { FILE *f_stdout = stdout; // workaround for OpenBSD 'stdout' implementation + backend_register[args[0]]->call_counter++; backend_register[args[0]]->execute(f_stdout, "", args, design); } else { if (!filename.empty()) diff --git a/kernel/register.h b/kernel/register.h index b07c46177..fd073cbe7 100644 --- a/kernel/register.h +++ b/kernel/register.h @@ -47,9 +47,11 @@ extern std::vector pushed_designs; struct Pass { std::string pass_name, short_help; + int call_counter; + Pass(std::string name, std::string short_help = "** document me **"); - virtual void run_register(); virtual ~Pass(); + virtual void help(); virtual void execute(std::vector args, RTLIL::Design *design) = 0; @@ -66,6 +68,8 @@ struct Pass static void call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::string command); static void call_on_module(RTLIL::Design *design, RTLIL::Module *module, std::vector args); + Pass *next_queued_pass; + virtual void run_register(); static void init_register(); static void done_register(); }; @@ -105,9 +109,6 @@ struct Backend : Pass extern void handle_extra_select_args(Pass *pass, std::vector args, size_t argidx, size_t args_size, RTLIL::Design *design); namespace REGISTER_INTERN { - extern int raw_register_count; - extern bool raw_register_done; - extern Pass *raw_register_array[]; extern std::map pass_register; extern std::map frontend_register; extern std::map backend_register; diff --git a/passes/cmds/Makefile.inc b/passes/cmds/Makefile.inc index 3a4b2da71..6ae4495a8 100644 --- a/passes/cmds/Makefile.inc +++ b/passes/cmds/Makefile.inc @@ -17,4 +17,5 @@ OBJS += passes/cmds/scc.o OBJS += passes/cmds/log.o OBJS += passes/cmds/tee.o OBJS += passes/cmds/connwrappers.o +OBJS += passes/cmds/cover.o diff --git a/passes/cmds/cover.cc b/passes/cmds/cover.cc new file mode 100644 index 000000000..ebbdc7c4c --- /dev/null +++ b/passes/cmds/cover.cc @@ -0,0 +1,115 @@ +/* + * yosys -- Yosys Open SYnthesis Suite + * + * Copyright (C) 2014 Clifford Wolf + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +#include "kernel/register.h" +#include "kernel/rtlil.h" +#include "kernel/log.h" + +struct CoverPass : public Pass { + CoverPass() : Pass("cover", "print code coverage counters") { } + virtual void help() + { + // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| + log("\n"); + log(" cover [-q] [-o logfile|-a logfile]\n"); + log("\n"); + log("Print the code coverage counters collected using the cover() macro in the Yosys\n"); + log("C++ code. This is useful to figure out what parts of Yosys are utilized by a\n"); + log("test bench.\n"); + log("\n"); + log(" -q\n"); + log(" Do not print output to the normal destination (console and/or log file)\n"); + log("\n"); + log(" -o logfile\n"); + log(" Write output to this file, truncate if exists.\n"); + log("\n"); + log(" -a logfile\n"); + log(" Write output to this file, append if exists.\n"); + log("\n"); + log("\n"); + log("It is also possible to instruct Yosys to print the coverage counters to a file\n"); + log("using environment variables.\n"); + log("\n"); + log(" YOSYS_COVER_DIR=\"{dir-name}\" yosys {args}n"); + log("\n"); + log(" This will create a file (with an auto-generated name) in this\n"); + log(" directory and wire the coverage counters to it.\n"); + log("\n"); + log(" YOSYS_COVER_FILE=\"{file-name}\" yosys {args}n"); + log("\n"); + log(" This will append the coverage counters to the specified file.\n"); + log("\n"); + log("\n"); + log("Hint: Use the following AWK command to consolidate Yosys coverage files:\n"); + log("\n"); + log(" gawk '{ p[$3] = $1; c[$3] += $2; } END { for (i in p)\n"); + log(" printf \"%%-60s %%10d %%s\\n\", p[i], c[i], i; }' {files} | sort -k3\n"); + log("\n"); + } + virtual void execute(std::vector args, RTLIL::Design *design) + { + std::vector out_files; + bool do_log = true; + + size_t argidx; + for (argidx = 1; argidx < args.size(); argidx++) + { + if (args[argidx] == "-q") { + do_log = false; + continue; + } + if ((args[argidx] == "-o" || args[argidx] == "-a") && argidx+1 < args.size()) { + const char *open_mode = args[argidx] == "-o" ? "w" : "a+"; + FILE *f = fopen(args[++argidx].c_str(), open_mode); + if (f == NULL) { + for (auto f : out_files) + fclose(f); + log_cmd_error("Can't create file %s.\n", args[argidx].c_str()); + } + out_files.push_back(f); + continue; + } + break; + } + extra_args(args, argidx, design); + + if (do_log) { + log_header("Printing code coverage counters.\n"); + log("\n"); + } + +#ifndef NDEBUG + for (auto &it : get_coverage_data()) { + for (auto f : out_files) + fprintf(f, "%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str()); + if (do_log) + log("%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str()); + } +#else + for (auto f : out_files) + fclose(f); + + log_cmd_error("Coverage counters are only available in debug builds of Yosys."); +#endif + + for (auto f : out_files) + fclose(f); + } +} CoverPass; + diff --git a/passes/cmds/tee.cc b/passes/cmds/tee.cc index dd9591954..e2f69e599 100644 --- a/passes/cmds/tee.cc +++ b/passes/cmds/tee.cc @@ -56,7 +56,7 @@ struct TeePass : public Pass { continue; } if ((args[argidx] == "-o" || args[argidx] == "-a") && argidx+1 < args.size()) { - const char *open_mode = args[argidx] == "-o" ? "wt" : "at"; + const char *open_mode = args[argidx] == "-o" ? "w" : "a+"; FILE *f = fopen(args[++argidx].c_str(), open_mode); if (f == NULL) { for (auto cf : files_to_close) -- 2.30.2