2022-11-02 22:41:45 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
2022-12-05 17:58:23 +00:00
|
|
|
// SCSI Target Emulator PiSCSI
|
2022-11-02 22:41:45 +00:00
|
|
|
// for Raspberry Pi
|
|
|
|
//
|
|
|
|
// Powered by XM6 TypeG Technology.
|
|
|
|
// Copyright (C) 2016-2020 GIMONS
|
2022-12-03 04:20:27 +00:00
|
|
|
// Copyright (C) 2021-2022 akuker
|
2022-11-02 22:41:45 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
#include "monitor/sm_core.h"
|
|
|
|
#include "hal/data_sample.h"
|
2022-11-09 07:40:26 +00:00
|
|
|
#include "hal/gpiobus.h"
|
2022-12-03 04:20:27 +00:00
|
|
|
#include "hal/gpiobus_factory.h"
|
2022-11-10 06:44:06 +00:00
|
|
|
#include "monitor/sm_reports.h"
|
2022-12-03 04:20:27 +00:00
|
|
|
#include "shared/log.h"
|
2022-12-05 17:58:23 +00:00
|
|
|
#include "shared/piscsi_version.h"
|
|
|
|
#include "shared/piscsi_util.h"
|
2022-11-02 22:41:45 +00:00
|
|
|
#include <climits>
|
|
|
|
#include <csignal>
|
|
|
|
#include <getopt.h>
|
2022-12-03 04:20:27 +00:00
|
|
|
#include <iostream>
|
2022-11-02 22:41:45 +00:00
|
|
|
#include <sched.h>
|
2022-12-03 04:20:27 +00:00
|
|
|
#include <sys/time.h>
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2022-12-05 17:58:23 +00:00
|
|
|
using namespace piscsi_util;
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void ScsiMon::KillHandler(int)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
void ScsiMon::ParseArguments(const vector<char *> &args)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
|
|
|
int opt;
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
while ((opt = getopt(static_cast<int>(args.size()), args.data(), "-Hhb:i:")) != -1) {
|
|
|
|
switch (opt) {
|
2022-11-02 22:41:45 +00:00
|
|
|
// The three options below are kind of a compound option with two letters
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
print_help = true;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
buff_size = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'i':
|
2022-12-03 04:20:27 +00:00
|
|
|
input_file_name = optarg;
|
|
|
|
import_data = true;
|
2022-11-02 22:41:45 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2022-12-03 04:20:27 +00:00
|
|
|
file_base_name = optarg;
|
2022-11-02 22:41:45 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cout << "default: " << optarg << endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process any remaining command line arguments (not options). */
|
|
|
|
if (optind < static_cast<int>(args.size())) {
|
2022-12-03 04:20:27 +00:00
|
|
|
while (optind < static_cast<int>(args.size())) {
|
|
|
|
file_base_name = args[optind];
|
|
|
|
optind++;
|
|
|
|
}
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
vcd_file_name = file_base_name;
|
|
|
|
vcd_file_name += ".vcd";
|
|
|
|
json_file_name = file_base_name;
|
|
|
|
json_file_name += ".json";
|
|
|
|
html_file_name = file_base_name;
|
|
|
|
html_file_name += ".html";
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
2022-11-09 07:40:26 +00:00
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
void ScsiMon::PrintHelpText(const vector<char *> &args) const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
|
|
|
LOGINFO("%s -i [input file json] -b [buffer size] [output file]", args[0])
|
|
|
|
LOGINFO(" -i [input file json] - scsimon will parse the json file instead of capturing new data")
|
|
|
|
LOGINFO(" If -i option is not specified, scsimon will read the gpio pins")
|
|
|
|
LOGINFO(" -b [buffer size] - Override the default buffer size of %d.", buff_size)
|
|
|
|
LOGINFO(" [output file] - Base name of the output files. The file extension (ex: .json)")
|
|
|
|
LOGINFO(" will be appended to this file name")
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void ScsiMon::Banner() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
|
|
|
if (import_data) {
|
2022-11-09 07:40:26 +00:00
|
|
|
LOGINFO("Reading input file: %s", input_file_name.c_str())
|
2022-12-03 04:20:27 +00:00
|
|
|
} else {
|
2022-11-02 22:41:45 +00:00
|
|
|
LOGINFO("Reading live data from the GPIO pins")
|
|
|
|
LOGINFO(" Connection type : %s", CONNECT_DESC.c_str())
|
|
|
|
}
|
|
|
|
LOGINFO(" Data buffer size: %u", buff_size)
|
|
|
|
LOGINFO(" ")
|
|
|
|
LOGINFO("Generating output files:")
|
2022-11-09 07:40:26 +00:00
|
|
|
LOGINFO(" %s - Value Change Dump file that can be opened with GTKWave", vcd_file_name.c_str())
|
|
|
|
LOGINFO(" %s - JSON file with raw data", json_file_name.c_str())
|
|
|
|
LOGINFO(" %s - HTML file with summary of commands", html_file_name.c_str())
|
2022-11-02 22:41:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScsiMon::Init()
|
|
|
|
{
|
|
|
|
// Interrupt handler settings
|
|
|
|
if (signal(SIGINT, KillHandler) == SIG_ERR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (signal(SIGHUP, KillHandler) == SIG_ERR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (signal(SIGTERM, KillHandler) == SIG_ERR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
bus = GPIOBUS_Factory::Create(BUS::mode_e::TARGET);
|
2022-11-09 07:40:26 +00:00
|
|
|
if (bus == nullptr) {
|
2022-11-02 22:41:45 +00:00
|
|
|
LOGERROR("Unable to intiailize the GPIO bus. Exiting....")
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
running = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void ScsiMon::Cleanup() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
|
|
|
if (!import_data) {
|
|
|
|
LOGINFO("Stopping data collection....")
|
|
|
|
}
|
|
|
|
LOGINFO(" ")
|
2022-11-09 07:40:26 +00:00
|
|
|
LOGINFO("Generating %s...", vcd_file_name.c_str())
|
2022-12-03 04:20:27 +00:00
|
|
|
scsimon_generate_value_change_dump(vcd_file_name, data_buffer);
|
2022-11-09 07:40:26 +00:00
|
|
|
LOGINFO("Generating %s...", json_file_name.c_str())
|
2022-12-03 04:20:27 +00:00
|
|
|
scsimon_generate_json(json_file_name, data_buffer);
|
2022-11-09 07:40:26 +00:00
|
|
|
LOGINFO("Generating %s...", html_file_name.c_str())
|
2022-12-03 04:20:27 +00:00
|
|
|
scsimon_generate_html(html_file_name, data_buffer);
|
2022-11-09 07:40:26 +00:00
|
|
|
|
2022-11-02 22:41:45 +00:00
|
|
|
bus->Cleanup();
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
void ScsiMon::Reset() const
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
|
|
|
bus->Reset();
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
int ScsiMon::run(const vector<char *> &args)
|
2022-11-02 22:41:45 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
spdlog::set_level(spdlog::level::trace);
|
|
|
|
#else
|
|
|
|
spdlog::set_level(spdlog::level::info);
|
|
|
|
#endif
|
|
|
|
spdlog::set_pattern("%^[%l]%$ %v");
|
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
piscsi_util::Banner("(SCSI Monitor Capture Tool)");
|
2022-11-11 20:08:48 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
ParseArguments(args);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
shared_ptr<DataSample> prev_sample = nullptr;
|
|
|
|
shared_ptr<DataSample> this_sample = nullptr;
|
2022-11-02 22:41:45 +00:00
|
|
|
timeval start_time;
|
|
|
|
timeval stop_time;
|
|
|
|
uint64_t loop_count = 0;
|
|
|
|
timeval time_diff;
|
|
|
|
uint64_t elapsed_us;
|
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
if (print_help) {
|
|
|
|
PrintHelpText(args);
|
2022-11-02 22:41:45 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Banner();
|
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
data_buffer.reserve(buff_size);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
2022-11-09 07:40:26 +00:00
|
|
|
if (import_data) {
|
2022-12-03 04:20:27 +00:00
|
|
|
data_idx = scsimon_read_json(input_file_name, data_buffer);
|
|
|
|
if (data_idx > 0) {
|
2022-11-09 07:40:26 +00:00
|
|
|
LOGDEBUG("Read %d samples from %s", data_idx, input_file_name.c_str())
|
2022-11-02 22:41:45 +00:00
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGINFO(" ")
|
|
|
|
LOGINFO("Now collecting data.... Press CTRL-C to stop.")
|
|
|
|
LOGINFO(" ")
|
|
|
|
|
|
|
|
// Initialize
|
|
|
|
int ret = 0;
|
|
|
|
if (!Init()) {
|
|
|
|
ret = EPERM;
|
|
|
|
goto init_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
// Set the affinity to a specific processor core
|
|
|
|
FixCpu(3);
|
|
|
|
|
|
|
|
// Scheduling policy setting (highest priority)
|
|
|
|
struct sched_param schparam;
|
|
|
|
schparam.sched_priority = sched_get_priority_max(SCHED_FIFO);
|
|
|
|
sched_setscheduler(0, SCHED_FIFO, &schparam);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Start execution
|
|
|
|
running = true;
|
2022-11-09 07:40:26 +00:00
|
|
|
bus->SetACK(false);
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
(void)gettimeofday(&start_time, nullptr);
|
|
|
|
|
|
|
|
// Main Loop
|
2022-11-09 07:40:26 +00:00
|
|
|
while (running) {
|
2022-11-02 22:41:45 +00:00
|
|
|
loop_count++;
|
2022-11-09 07:40:26 +00:00
|
|
|
if (loop_count > LLONG_MAX - 1) {
|
2022-11-02 22:41:45 +00:00
|
|
|
LOGINFO("Maximum amount of time has elapsed. SCSIMON is terminating.")
|
|
|
|
running = false;
|
|
|
|
}
|
2022-11-09 07:40:26 +00:00
|
|
|
|
|
|
|
if (data_idx >= (buff_size - 2)) {
|
2022-11-02 22:41:45 +00:00
|
|
|
LOGINFO("Internal data buffer is full. SCSIMON is terminating.")
|
|
|
|
running = false;
|
|
|
|
}
|
|
|
|
|
2022-12-03 04:20:27 +00:00
|
|
|
this_sample = bus->GetSample(loop_count);
|
|
|
|
if ((prev_sample == nullptr) || (this_sample->GetRawCapture() != prev_sample->GetRawCapture())) {
|
|
|
|
data_buffer.push_back(this_sample);
|
2022-11-02 22:41:45 +00:00
|
|
|
data_idx++;
|
|
|
|
prev_sample = this_sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect one last sample, otherwise it looks like the end of the data was cut off
|
2022-11-09 07:40:26 +00:00
|
|
|
if (data_idx < buff_size) {
|
2022-12-03 04:20:27 +00:00
|
|
|
data_buffer.push_back(bus->GetSample(loop_count));
|
2022-11-02 22:41:45 +00:00
|
|
|
data_idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)gettimeofday(&stop_time, nullptr);
|
|
|
|
|
|
|
|
timersub(&stop_time, &start_time, &time_diff);
|
|
|
|
|
|
|
|
elapsed_us = ((time_diff.tv_sec * 1000000) + time_diff.tv_usec);
|
2022-12-03 04:20:27 +00:00
|
|
|
LOGINFO("%s", ("Elapsed time: " + to_string(elapsed_us) + " microseconds (" + to_string(elapsed_us / 1000000) +
|
|
|
|
" seconds")
|
|
|
|
.c_str())
|
2022-11-09 07:40:26 +00:00
|
|
|
LOGINFO("%s", ("Collected " + to_string(data_idx) + " changes").c_str())
|
|
|
|
|
|
|
|
ns_per_loop = (double)(elapsed_us * 1000) / (double)loop_count;
|
2022-12-03 04:20:27 +00:00
|
|
|
LOGINFO("%s", ("Read the SCSI bus " + to_string(loop_count) + " times with an average of " +
|
|
|
|
to_string(ns_per_loop) + " ns for each read")
|
|
|
|
.c_str())
|
2022-11-02 22:41:45 +00:00
|
|
|
|
|
|
|
Cleanup();
|
|
|
|
|
|
|
|
init_exit:
|
|
|
|
exit(ret);
|
|
|
|
}
|