diff --git a/passes/cmds/Makefile.inc b/passes/cmds/Makefile.inc index 33e3ae1bc5a..f208e3e06ef 100644 --- a/passes/cmds/Makefile.inc +++ b/passes/cmds/Makefile.inc @@ -16,6 +16,7 @@ OBJS += passes/cmds/setundef.o OBJS += passes/cmds/splitnets.o OBJS += passes/cmds/splitcells.o OBJS += passes/cmds/stat.o +OBJS += passes/cmds/internal_stats.o OBJS += passes/cmds/setattr.o OBJS += passes/cmds/copy.o OBJS += passes/cmds/splice.o diff --git a/passes/cmds/internal_stats.cc b/passes/cmds/internal_stats.cc new file mode 100644 index 00000000000..fe27bee5eb8 --- /dev/null +++ b/passes/cmds/internal_stats.cc @@ -0,0 +1,120 @@ +/* + * yosys -- Yosys Open SYnthesis Suite + * + * Copyright (C) 2012 Claire Xenia 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 +#include +#include + +#include "kernel/yosys.h" +#include "kernel/celltypes.h" +#include "passes/techmap/libparse.h" +#include "kernel/cost.h" +#include "libs/json11/json11.hpp" + +#if defined(__APPLE__) && defined(__MACH__) +#include +#include +#endif + +USING_YOSYS_NAMESPACE +PRIVATE_NAMESPACE_BEGIN + +std::optional current_mem_bytes() { + +#if defined(__APPLE__) + task_basic_info_64_data_t basicInfo; + mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT; + kern_return_t error = task_info(mach_task_self(), TASK_BASIC_INFO_64, (task_info_t)&basicInfo, &count); + if (error != KERN_SUCCESS) { + return {}; // Error getting task information + } + return basicInfo.resident_size; // Return RSS in KB + +#elif defined(__linux__) + // Not all linux distributions have to have this file + std::ifstream statusFile("/proc/self/status"); + std::string line; + while (std::getline(statusFile, line)) { + if (line.find("VmRSS:") == 0) { + std::istringstream iss(line); + std::string token; + // Skip prefix + iss >> token; + uint64_t rss; + iss >> rss; + return rss * 1024; + } + } + // Error reading /proc/self/status + return {}; + +#else + return {}; +#endif +} + +struct InternalStatsPass : public Pass { + InternalStatsPass() : Pass("internal_stats", "print some internal statistics") { } + void help() override + { + // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| + log("\n"); // TODO + } + void execute(std::vector args, RTLIL::Design *design) override + { + bool json_mode = false; + + size_t argidx; + for (argidx = 1; argidx < args.size(); argidx++) + { + if (args[argidx] == "-json") { + json_mode = true; + continue; + } + break; + } + extra_args(args, argidx, design); + + if(!json_mode) + log_header(design, "Printing internal statistics.\n"); + + log_experimental("internal_stats"); + + if (json_mode) { + log("{\n"); + log(" \"creator\": %s,\n", json11::Json(yosys_version_str).dump().c_str()); + std::stringstream invocation; + std::copy(args.begin(), args.end(), std::ostream_iterator(invocation, " ")); + log(" \"invocation\": %s,\n", json11::Json(invocation.str()).dump().c_str()); + if (auto mem = current_mem_bytes()) { + log(" \"memory_now\": %s,\n", std::to_string(*mem).c_str()); + } + } + + // stats go here + + if (json_mode) { + log("\n"); + log("}\n"); + } + + } +} InternalStatsPass; + +PRIVATE_NAMESPACE_END