Skip to content

Commit

Permalink
Improve progress reporting (SIGINFO), allow tracing for profiling.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Mar 24, 2024
1 parent 28eef32 commit b6e796d
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 152 deletions.
8 changes: 5 additions & 3 deletions src/Archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@
#include "ArchiveLibarchive.h"
#endif
#include "ArchiveZip.h"
#include "Detector.h"
#include "CkmameCache.h"
#include "CkmameDB.h"
#include "Detector.h"
#include "Exception.h"
#include "Progress.h"
#include "RomDB.h"
#include "file_util.h"
#include "globals.h"
#include "RomDB.h"
#include "CkmameCache.h"

#define BUFSIZE 8192

Expand Down Expand Up @@ -469,6 +470,7 @@ Archive::GetHashesStatus Archive::get_hashes(ZipSource *source, uint64_t length,

hu.update(buf, n);
length -= n;
Progress::update();
}

hu.end();
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ set(COMMON_SOURCES
ParserSourceFile.cc
ParserSourceZip.cc
ProgramName.cc
Progress.cc
Result.cc
Rom.cc
RomDB.cc
SharedFile.cc
sighandle.cc
Stats.cc
superfluous.cc
TomlSchema.cc
Expand Down
30 changes: 10 additions & 20 deletions src/CkmameCache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "Dir.h"
#include "Exception.h"
#include "Progress.h"
#include "RomDB.h"
#include "globals.h"
#include "sighandle.h"
#include "util.h"

CkmameCachePtr ckmame_cache;
Expand Down Expand Up @@ -190,9 +190,8 @@ void CkmameCache::ensure_extra_maps() {
switch ((name_type(file))) {
case NAME_IMAGES:
case NAME_ZIP: {
if (siginfo_caught) {
print_info("currently scanning '" + file + "'");
}
Progress::set_message("currently scanning '" + file + "'");

auto a = Archive::open(file, entry.filetype, FILE_SUPERFLUOUS, 0);
// TODO: loose: add loose files in directory
break;
Expand All @@ -204,9 +203,8 @@ void CkmameCache::ensure_extra_maps() {
}
}

if (siginfo_caught) {
print_info("currently scanning '" + configuration.rom_directory + "'");
}
Progress::set_message("currently scanning '" + configuration.rom_directory + "'");

auto filetype = configuration.roms_zipped ? TYPE_DISK : TYPE_ROM;
auto a = Archive::open_toplevel(configuration.rom_directory, filetype, FILE_SUPERFLUOUS, 0);

Expand Down Expand Up @@ -275,9 +273,7 @@ bool CkmameCache::enter_dir_in_map_and_list_unzipped(const DeleteListPtr &list,
continue;
}
if (std::filesystem::is_directory(filepath)) {
if (siginfo_caught) {
print_info("currently scanning '" + filepath.string() + "'");
}
Progress::set_message("currently scanning '" + filepath.string() + "'");
auto a = Archive::open(filepath, TYPE_ROM, where, 0);
if (a) {
list->add(a.get());
Expand All @@ -286,9 +282,7 @@ bool CkmameCache::enter_dir_in_map_and_list_unzipped(const DeleteListPtr &list,
}
}

if (siginfo_caught) {
print_info("currently scanning '" + directory_name + "'");
}
Progress::set_message("currently scanning '" + directory_name + "'");
auto a = Archive::open_toplevel(directory_name, TYPE_ROM, where, 0);
if (a) {
list->add(a.get());
Expand All @@ -311,10 +305,8 @@ bool CkmameCache::enter_dir_in_map_and_list_zipped(const DeleteListPtr &list, co
enter_file_in_map_and_list(list, filepath, where);
}

if (siginfo_caught) {
print_info("currently scanning '" + dir_name + "'");
}
auto a = Archive::open_toplevel(dir_name, TYPE_DISK, where, 0);
Progress::set_message("currently scanning '" + dir_name + "'");
auto a = Archive::open_toplevel(dir_name, TYPE_DISK, where, 0);
if (a) {
list->add(a.get());
}
Expand All @@ -333,9 +325,7 @@ bool CkmameCache::enter_file_in_map_and_list(const DeleteListPtr &list, const st
switch ((nt = name_type(name))) {
case NAME_IMAGES:
case NAME_ZIP: {
if (siginfo_caught) {
print_info("currently scanning '" + name + "'");
}
Progress::set_message("currently scanning '" + name + "'");
auto a = Archive::open(name, nt == NAME_ZIP ? TYPE_ROM : TYPE_DISK, where, 0);
if (a) {
list->add(a.get());
Expand Down
18 changes: 5 additions & 13 deletions src/CkmameDB.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "Dir.h"
#include "Exception.h"
#include "fix.h"
#include "sighandle.h"
#include "Progress.h"
#include "util.h"

const std::string CkmameDB::db_name = ".ckmame.db";
Expand Down Expand Up @@ -445,17 +445,13 @@ void CkmameDB::refresh_unzipped() {
continue;
}
if (std::filesystem::is_directory(filepath)) {
if (siginfo_caught) {
print_info("currently scanning '" + filepath.string() + "'");
}
Progress::set_message("currently scanning '" + filepath.string() + "'");
auto a = Archive::open(filepath, TYPE_ROM, where, 0);
a->close();
}
}

if (siginfo_caught) {
print_info("currently scanning '" + directory + "'");
}
Progress::set_message("currently scanning '" + directory + "'");
auto a = Archive::open_toplevel(directory, TYPE_ROM, where, 0);
if (a) {
a->close();
Expand All @@ -475,9 +471,7 @@ void CkmameDB::refresh_zipped() {
switch ((nt = name_type(filepath))) {
case NAME_IMAGES:
case NAME_ZIP: {
if (siginfo_caught) {
print_info("currently scanning '" + filepath.string() + "'");
}
Progress::set_message("currently scanning '" + filepath.string() + "'");
auto a = Archive::open(filepath, nt == NAME_ZIP ? TYPE_ROM : TYPE_DISK, where, 0);
if (a) {
a->close();
Expand All @@ -492,9 +486,7 @@ void CkmameDB::refresh_zipped() {
}
}

if (siginfo_caught) {
print_info("currently scanning '" + directory + "'");
}
Progress::set_message("currently scanning '" + directory + "'");
auto a = Archive::open_toplevel(directory, TYPE_DISK, where, 0);
if (a) {
a->close();
Expand Down
87 changes: 87 additions & 0 deletions src/Progress.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Progress.cc --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "Progress.h"

//#include <chrono>
#include <csignal>
#include <iostream>

#include "globals.h"
#include "ProgramName.h"

std::string Progress::current_message;
volatile bool Progress::siginfo_caught = false;
bool Progress::trace = false;

void Progress::set_message(std::string message) {
current_message = std::move(message);

if (trace || siginfo_caught) {
print_message();
}
}

void Progress::sig_handler(int signal) {
#ifdef SIGINFO
if (signal == SIGINFO) {
siginfo_caught = true;
}
#endif
}
void Progress::print_message() {
if (current_message.empty()) {
return;
}

if (trace) {
// C++ 20:
// std::cout << std::format("%Y-%m-%d %H:%M:%S ", std::chrono::system_clock::now());
auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::cout << std::put_time(std::localtime(&now), "%Y-%m-%d %H:%M:%S ");
}
else {
std::cout << ProgramName::get() << ": ";
}
std::cout << current_message;
if (!configuration.set.empty()) {
std::cout << " in set " << configuration.set;
}
std::cout << std::endl;

siginfo_caught = false;
}

void Progress::enable() {
#ifdef SIGINFO
signal(SIGINFO, sig_handler);
#endif
}
56 changes: 56 additions & 0 deletions src/Progress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef PROGRESS_H
#define PROGRESS_H

/*
Progress.h --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <string>


class Progress {
public:
static void set_message(std::string message);
static void update() { if (siginfo_caught) {print_message();}}
static void enable();

static bool trace;

private:
static void print_message();

static void sig_handler(int sig);

static volatile bool siginfo_caught;

static std::string current_message;
};

#endif // PROGRESS_H
6 changes: 2 additions & 4 deletions src/Tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "Fixdat.h"
#include "globals.h"
#include "RomDB.h"
#include "sighandle.h"
#include "Progress.h"
#include "warn.h"
#include "CkmameCache.h"

Expand Down Expand Up @@ -117,9 +117,7 @@ void Tree::traverse() {
void Tree::traverse_internal(GameArchives *ancestor_archives) {
GameArchives archives[] = { GameArchives(), ancestor_archives[0], ancestor_archives[1] };

if (siginfo_caught) {
print_info("currently checking " + name);
}
Progress::set_message("currently checking " + name);

auto flags = check ? ARCHIVE_FL_CREATE : 0;

Expand Down
12 changes: 7 additions & 5 deletions src/ckmame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@
#include "Exception.h"
#include "Fixdat.h"
#include "ProgramName.h"
#include "Progress.h"
#include "RomDB.h"
#include "Stats.h"
#include "Tree.h"
#include "check_util.h"
#include "cleanup.h"
#include "globals.h"
#include "sighandle.h"
#include "superfluous.h"
#include "update_romdb.h"
#include "util.h"
Expand All @@ -68,7 +68,8 @@ std::filesystem::path rom_dir_normalized;
std::vector<Commandline::Option> ckmame_options = {
Commandline::Option("fix", 'F', "fix ROM set"),
Commandline::Option("game-list", 'T', "file", "read games to check from file"),
Commandline::Option("only-if-database-updated", 'U', "if dats didn't change, exit; otherwise update database and run")
Commandline::Option("only-if-database-updated", 'U', "if dats didn't change, exit; otherwise update database and run"),
Commandline::Option("trace", "trace actions, useful for profiling")
};

std::unordered_set<std::string> ckmame_used_variables = {
Expand Down Expand Up @@ -122,6 +123,9 @@ void CkMame::global_setup(const ParsedCommandline &commandline) {
else if (option.name == "only-if-database-updated") {
only_if_updated = true;
}
else if (option.name == "trace") {
Progress::trace = true;
}
}

if (!configuration.fix_romset) {
Expand Down Expand Up @@ -277,9 +281,7 @@ bool CkMame::execute(const std::vector<std::string> &arguments) {
ckmame_cache->ensure_extra_maps();
}

#ifdef SIGINFO
signal(SIGINFO, sighandle);
#endif
Progress::enable();

check_tree.traverse();
check_tree.traverse(); /* handle rechecks */
Expand Down
Loading

0 comments on commit b6e796d

Please sign in to comment.