yosys/passes/cmds/cover.cc

158 lines
5.2 KiB
C++
Raw Normal View History

2014-07-24 08:06:45 -05:00
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2014 Clifford Wolf <clifford@clifford.at>
*
* 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.
*
*/
2014-10-10 11:02:17 -05:00
#include "kernel/yosys.h"
#include <sys/types.h>
#ifndef _WIN32
# include <unistd.h>
#else
# include <io.h>
#endif
#include "kernel/register.h"
#include "kernel/rtlil.h"
#include "kernel/log.h"
2014-07-24 08:06:45 -05:00
2014-09-27 09:17:53 -05:00
USING_YOSYS_NAMESPACE
PRIVATE_NAMESPACE_BEGIN
2014-07-24 08:06:45 -05:00
struct CoverPass : public Pass {
CoverPass() : Pass("cover", "print code coverage counters") { }
void help() YS_OVERRIDE
2014-07-24 08:06:45 -05:00
{
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
log("\n");
2014-07-25 05:04:40 -05:00
log(" cover [options] [pattern]\n");
2014-07-24 08:06:45 -05:00
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");
2014-07-25 05:04:40 -05:00
log(" -o file\n");
2014-07-24 08:06:45 -05:00
log(" Write output to this file, truncate if exists.\n");
log("\n");
2014-07-25 05:04:40 -05:00
log(" -a file\n");
2014-07-24 08:06:45 -05:00
log(" Write output to this file, append if exists.\n");
log("\n");
2014-07-25 05:04:40 -05:00
log(" -d dir\n");
log(" Write output to a newly created file in the specified directory.\n");
log("\n");
log("When one or more pattern (shell wildcards) are specified, then only counters\n");
log("matching at least one pattern are printed.\n");
log("\n");
2014-07-24 08:06:45 -05:00
log("\n");
2014-07-25 05:04:40 -05:00
log("It is also possible to instruct Yosys to print the coverage counters on program\n");
log("exit to a file using environment variables:\n");
2014-07-24 08:06:45 -05:00
log("\n");
2014-07-25 05:04:40 -05:00
log(" YOSYS_COVER_DIR=\"{dir-name}\" yosys {args}\n");
2014-07-24 08:06:45 -05:00
log("\n");
log(" This will create a file (with an auto-generated name) in this\n");
2014-07-25 05:04:40 -05:00
log(" directory and write the coverage counters to it.\n");
2014-07-24 08:06:45 -05:00
log("\n");
2014-07-25 05:04:40 -05:00
log(" YOSYS_COVER_FILE=\"{file-name}\" yosys {args}\n");
2014-07-24 08:06:45 -05:00
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");
2014-07-25 05:22:37 -05:00
log("\n");
log("Coverage counters are only available in Yosys for Linux.\n");
2014-07-25 05:22:37 -05:00
log("\n");
2014-07-24 08:06:45 -05:00
}
void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
2014-07-24 08:06:45 -05:00
{
std::vector<FILE*> out_files;
2014-07-25 05:04:40 -05:00
std::vector<std::string> patterns;
2014-07-24 08:06:45 -05:00
bool do_log = true;
size_t argidx;
for (argidx = 1; argidx < args.size(); argidx++)
{
if (args[argidx] == "-q") {
do_log = false;
continue;
}
2014-07-25 05:04:40 -05:00
if ((args[argidx] == "-o" || args[argidx] == "-a" || args[argidx] == "-d") && argidx+1 < args.size()) {
const char *open_mode = args[argidx] == "-a" ? "a+" : "w";
std::string filename = args[++argidx];
if (args[argidx-1] == "-d") {
2014-10-11 03:49:43 -05:00
#ifdef _WIN32
log_cmd_error("The 'cover -d' option is not supported on win32.\n");
#else
2014-07-25 05:04:40 -05:00
char filename_buffer[4096];
snprintf(filename_buffer, 4096, "%s/yosys_cover_%d_XXXXXX.txt", filename.c_str(), getpid());
filename = mkstemps(filename_buffer, 4);
2014-10-11 03:49:43 -05:00
#endif
2014-07-25 05:04:40 -05:00
}
FILE *f = fopen(filename.c_str(), open_mode);
2014-07-24 08:06:45 -05:00
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;
}
2014-07-25 05:04:40 -05:00
while (argidx < args.size() && args[argidx].substr(0, 1) != "-")
patterns.push_back(args[argidx++]);
2014-07-24 08:06:45 -05:00
extra_args(args, argidx, design);
if (do_log) {
2016-04-21 16:28:37 -05:00
log_header(design, "Printing code coverage counters.\n");
2014-07-24 08:06:45 -05:00
log("\n");
}
#if defined(YOSYS_ENABLE_COVER) && (defined(__linux__) || defined(__FreeBSD__))
2014-07-24 08:06:45 -05:00
for (auto &it : get_coverage_data()) {
2014-07-25 05:04:40 -05:00
if (!patterns.empty()) {
for (auto &p : patterns)
2014-10-10 11:02:17 -05:00
if (patmatch(p.c_str(), it.first.c_str()))
2014-07-25 05:04:40 -05:00
goto pattern_match;
continue;
}
pattern_match:
2014-07-24 08:06:45 -05:00
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("This version of Yosys was not built with support for code coverage counters.\n");
2014-07-24 08:06:45 -05:00
#endif
for (auto f : out_files)
fclose(f);
}
} CoverPass;
2014-09-27 09:17:53 -05:00
PRIVATE_NAMESPACE_END