From 03dd4df851ed8d5db0761ccb19e20e3d9108555a Mon Sep 17 00:00:00 2001 From: Fabian Posch Date: Thu, 11 Jan 2024 11:54:25 -0500 Subject: [PATCH] remove task from library and move some of the stuff over --- include/db_client.hpp | 11 -- include/db_types.hpp | 19 +++ include/task.hpp | 282 ------------------------------------------ src/db_client.cpp | 2 +- src/task.cpp | 151 ---------------------- src/util.h | 22 +++- 6 files changed, 41 insertions(+), 446 deletions(-) delete mode 100644 include/task.hpp delete mode 100644 src/task.cpp diff --git a/include/db_client.hpp b/include/db_client.hpp index 5e28979..02f05de 100644 --- a/include/db_client.hpp +++ b/include/db_client.hpp @@ -30,7 +30,6 @@ #include #include #include "db_types.hpp" -#include "task.hpp" namespace db { @@ -127,16 +126,6 @@ class Connection { bool connected; }; -const vector> uplink_statements = { - {"commit_task", "UPDATE tasks SET t_status = DONE, error = $2, sim_log = $3, sim_trace = $4 WHERE id = $1"} -}; - -const vector> downlink_statements = { - {"check_open_tasks", "SELECT id FROM tasks WHERE t_status=OPEN LIMIT 1"}, - // TODO add inner join to grab pipeline depth - {"fetch_task", "SELECT id, job, t_status, t_type, is_reference, reference, max_plf, error, sim_log FROM tasks WHERE id = $1 LIMIT 1"} -}; - }; #endif diff --git a/include/db_types.hpp b/include/db_types.hpp index 93a9692..3f64c53 100644 --- a/include/db_types.hpp +++ b/include/db_types.hpp @@ -92,6 +92,23 @@ inline std::ostream& operator<<(std::ostream& os, uuid_t& value) { return os; }; +/** + * @brief Representation of the current status of a job. + * + */ +enum class JobStatusType {NOT_STARTED, IN_PROGRESS, FINISHED, HALTED, UNKNOWN}; + +// string mapping for job status type enum +static std::unordered_map job_status_type_st { + {"not_started", JobStatusType::NOT_STARTED}, + {"in_progress", JobStatusType::IN_PROGRESS}, + {"finished", JobStatusType::FINISHED}, + {"halted", JobStatusType::HALTED}, + {"unknown", JobStatusType::UNKNOWN} +}; + +ENUM_CLASS_OPS(JobStatusType, job_status_type_st); + // macro for registering enum classes for PQXX template @@ -310,5 +327,7 @@ struct string_traits { }; +DB_REGISTER_ENUM_CLASS(db::JobStatusType, db::job_status_type_st, "job_status_type"); + #endif \ No newline at end of file diff --git a/include/task.hpp b/include/task.hpp deleted file mode 100644 index 46595eb..0000000 --- a/include/task.hpp +++ /dev/null @@ -1,282 +0,0 @@ -/************************************************************************* - * - * Copyright (c) 2023 Fabian Posch - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * - ************************************************************************** - */ - - -#ifndef __TASK_H__ -#define __TASK_H__ - -#include -#include -#include -#include "db_types.hpp" - -/** - * @brief Representation of the current status of a job. - * - */ -enum class JobStatusType {NOT_STARTED, IN_PROGRESS, FINISHED, HALTED, UNKNOWN}; - -// string mapping for job status type enum -static std::unordered_map job_status_type_st { - {JobStatusType::NOT_STARTED, "not_started"}, - {JobStatusType::IN_PROGRESS, "in_progress"}, - {JobStatusType::FINISHED, "finished"}, - {JobStatusType::HALTED, "halted"}, - {JobStatusType::UNKNOWN, "unknown"} -}; - -inline std::string to_string(JobStatusType const &value) { - return job_status_type_st[value]; -}; - -inline std::ostream& operator<<(std::ostream& os, JobStatusType& value) { - os << to_string(value); - return os; -}; - -JobStatusType convert_to_job_status(std::string status_string); - - -/** - * @brief Defines if a simulation task should be executed a single time or - * dependent on a central coverage model. The latter task type can also be - * executed by more than just a single node to hasten progress. - */ -enum class TaskTypeType {UNKNOWN, SINGLE, MULTI_COV}; - -// string mapping for task type enum -static std::unordered_map task_type_type_st { - {TaskTypeType::UNKNOWN, "unknown"}, - {TaskTypeType::SINGLE, "single"}, - {TaskTypeType::MULTI_COV, "multi_cov"} -}; - -inline std::string to_string(TaskTypeType const &value) { - return task_type_type_st[value]; -}; - -inline std::ostream& operator<<(std::ostream& os, TaskTypeType& value) { - os << to_string(value); - return os; -}; - -TaskTypeType convert_to_task_type(std::string type_string); - - -// TODO check if this is complete -/** - * @brief If a task simulation was aborted or completed with an error, - * this type represents the types of error encountered. - */ -enum class TaskErrorType {UNKNOWN, NONE, SYNTAX, VALUE, CODE, METASTABLE, TIMING, DEADLOCK}; - -// string mapping for task error type enum -static std::unordered_map task_error_type_st { - {TaskErrorType::UNKNOWN, "unknown"}, - {TaskErrorType::NONE, "none"}, - {TaskErrorType::SYNTAX, "syntax"}, - {TaskErrorType::VALUE, "value"}, - {TaskErrorType::CODE, "code"}, - {TaskErrorType::METASTABLE, "metastable"}, - {TaskErrorType::TIMING, "timing"}, - {TaskErrorType::DEADLOCK, "deadlock"} -}; - -inline std::string to_string(TaskErrorType const &value) { - return task_error_type_st[value]; -}; - -inline std::ostream& operator<<(std::ostream& os, TaskErrorType& value) { - os << to_string(value); - return os; -}; - -TaskErrorType convert_to_task_error(std::string error_string); - - -/** - * @brief Representation of teh current status of a task. - * - */ -enum class TaskStatusType {UNKNOWN, OPEN, IN_PROGRESS, DONE}; - -// string mapping for task status enum -static std::unordered_map task_status_type_st { - {TaskStatusType::UNKNOWN, "unknown"}, - {TaskStatusType::OPEN, "open"}, - {TaskStatusType::IN_PROGRESS, "in_progress"}, - {TaskStatusType::DONE, "done"} -}; - -inline std::string to_string(TaskStatusType const &value) { - return task_status_type_st[value]; -}; - -inline std::ostream& operator<<(std::ostream& os, TaskStatusType& value) { - os << to_string(value); - return os; -}; - -TaskStatusType convert_to_task_status(std::string status_string); - -/** - * @brief Task class represents one simulation task and its settings. - */ -class Task { - public: - - Task( db::uuid_t task_uuid, - TaskStatusType status, - TaskTypeType type, - bool is_reference, - db::uuid_t reference_uuid, - float max_plf, - TaskErrorType error, - std::string sim_log - ); - - Task( db::uuid_t task_uuid, - TaskTypeType type, - bool is_reference, - db::uuid_t reference_uuid, - float max_plf - ); - - /** - * @brief Get the UUID of this task - * - * @return db::uuid_t Task UUID - */ - const db::uuid_t& uuid = task_uuid; - - /** - * @brief Get information whether this task is a one shot simulation or contributes - * to a central coverage model. - * - * @return TaskType - */ - const TaskTypeType& task_type = task_type_; - - /** - * @brief Does this simulation task have a reference task to compare to? - * - * @return true It does - * @return false It does not - */ - const bool& is_reference_run = is_reference_; - - /** - * @brief Get the reference object UUID - * - * @return The UUID of the reference simulation task - */ - const db::uuid_t& reference_uuid = reference_uuid_; - - /** - * @brief Get the status of this task - * - * @return TaskStatusType The task status - */ - const TaskStatusType& status = status_; - - /** - * @brief Set the status of the task - * - * @param new_status The new status this task should be set to - */ - void set_status(TaskStatusType new_status) { this->status_ = new_status; }; - - /** - * @brief Get the value of the maximum allowed pipeline load factor - * - * @return float - */ - const float& max_plf = max_plf_; - - /** - * @brief Get the error with which this simulation task was completed - * - * @return TaskErrorType - */ - const TaskErrorType& error = error_; - - /** - * @brief Set the error with which this task was completed - * - * @param error - */ - void set_error(TaskErrorType error) { this->error_ = error; }; - - /** - * @brief Get the simulation log of this task. Empty if not yet simulated. - * - * @return std::string The simulation log in JSON format - */ - const std::string& sim_log = sim_log_; - - /** - * @brief Set the simulation log - * - * @param log - */ - void set_log(std::string log) { this->sim_log_ = log; }; - - /** - * @brief Get the simulation trace of this task. Empty if not yet simulated. - * - * @return std::string The simulation log in TODO format - */ - const std::string& trace = trace_; - - /** - * @brief Set the simulation trace - * - * @param trace - */ - void set_trace(std::string trace) { this->trace_ = trace; }; - - ///////////////////////////////////// - private: - - // UUID of this task in the database - db::uuid_t task_uuid; - - // Is this task a single shot or does it depend on central coverage metrics? - TaskTypeType task_type_; - - // Does this task have a reference run which we should compare the trace to? - bool is_reference_; - TaskStatusType status_; - db::uuid_t reference_uuid_; - - // maximum pipeline load factor - float max_plf_; - // error with which this task was completed - TaskErrorType error_; - - // simulation output - std::string sim_log_; - std::string trace_; - -}; - -#endif \ No newline at end of file diff --git a/src/db_client.cpp b/src/db_client.cpp index ab0be5f..8874bc4 100644 --- a/src/db_client.cpp +++ b/src/db_client.cpp @@ -187,7 +187,7 @@ JobStatusType Connection::get_job_status(std::string job) { try { pqxx::row res {txn->exec1("SELECT job_status FROM jobs WHERE id='" + job + "';")}; - *status = convert_to_job_status(res["job_status"].as()); + *status = res["job_status"].as(); } catch (pqxx::unexpected_rows& e) { std::cerr << "Error: Fetching job returned nothing or too many rows!" << std::endl; *status = JobStatusType::UNKNOWN; diff --git a/src/task.cpp b/src/task.cpp deleted file mode 100644 index de75088..0000000 --- a/src/task.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/************************************************************************* - * - * Copyright (c) 2023 Fabian Posch - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * - ************************************************************************** - */ - - -#include -#include "task.hpp" - - -Task::Task (db::uuid_t task_uuid, - TaskStatusType status, - TaskTypeType type, - bool is_reference, - db::uuid_t reference_uuid, - float max_plf, - TaskErrorType error, - std::string sim_log - ) -{ - this->task_uuid = task_uuid; - this->status_ = status; - this->task_type_ = type; - this->is_reference_ = is_reference; - this->reference_uuid_ = reference_uuid; - this->max_plf_ = max_plf; - this->error_ = error; - this->sim_log_ = sim_log; -} - -Task::Task (db::uuid_t task_uuid, - TaskTypeType type, - bool is_reference, - db::uuid_t reference_uuid, - float max_plf - ) -{ - this->task_uuid = task_uuid; - this->task_type_ = type; - this->is_reference_ = is_reference; - this->reference_uuid_ = reference_uuid; - this->max_plf_ = max_plf; -} - - -/** - * @brief Convert from pqxx response string to JobStatusType - * - * @param type_string Type returned by pqxx in string form. - * @return JobStatusType - */ -JobStatusType convert_to_job_status(std::string status_string) { - JobStatusType status = JobStatusType::UNKNOWN; - - for (auto pair : job_status_type_st) { - if (pair.second == status_string) { - status = pair.first; - break; - } - } - - if (status == JobStatusType::UNKNOWN) { - std::cerr << "Warning: Job status type unknown or could not be parsed. Given status was " << status_string << std::endl; - } - - return status; -} - - -/** - * @brief Convert from pqxx response string to TaskTypeType - * - * @param type_string Type returned by pqxx in string form. - * @return TaskTypeType - */ -TaskTypeType convert_to_task_type(std::string type_string) { - if (type_string == "SINGLE") { - return TaskTypeType::SINGLE; - } else if (type_string == "MULTI_COV") { - return TaskTypeType::MULTI_COV; - } else { - std::cerr << "Error: Could not parse task type \"" << type_string << "\"" << std::endl; - return TaskTypeType::UNKNOWN; - } -} - - -/** - * @brief Convert from pqxx response string to TaskErrorType - * - * @param error_string Error returned by pqxx in string form. - * @return TaskErrorType - */ -TaskErrorType convert_to_task_error(std::string error_string) { - if (error_string == "NONE") { - return TaskErrorType::NONE; - } else if (error_string == "SYNTAX") { - return TaskErrorType::SYNTAX; - } else if (error_string == "VALUE") { - return TaskErrorType::VALUE; - } else if (error_string == "CODE") { - return TaskErrorType::CODE; - } else if (error_string == "METASTABLE") { - return TaskErrorType::METASTABLE; - } else if (error_string == "TIMING") { - return TaskErrorType::TIMING; - } else if (error_string == "DEADLOCK") { - return TaskErrorType::DEADLOCK; - } else { - std::cerr << "Error: Could not parse task error \"" << error_string << "\"" << std::endl; - return TaskErrorType::UNKNOWN; - } -} - - -/** - * @brief Convert from pqxx response string to TaskStatusType - * - * @param status_string Status returned by pqxx in string form. - * @return TaskStatusType - */ -TaskStatusType convert_to_task_status(std::string status_string) { - if (status_string == "OPEN") { - return TaskStatusType::OPEN; - } else if (status_string == "IN_PROGRESS") { - return TaskStatusType::IN_PROGRESS; - } else if (status_string == "DONE") { - return TaskStatusType::DONE; - } else { - std::cerr << "Error: Could not parse task status \"" << status_string << "\"" << std::endl; - return TaskStatusType::UNKNOWN; - } -} - diff --git a/src/util.h b/src/util.h index bd82a91..87d2123 100644 --- a/src/util.h +++ b/src/util.h @@ -1,7 +1,7 @@ /************************************************************************* * - * Copyright (c) 2023 Fabian Posch + * Copyright (c) 2024 Fabian Posch * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -35,4 +35,24 @@ #define DEBUG_PRINT(msg) do {} while (0) #endif +#define ENUM_CLASS_OPS(T, lookup_table) \ + \ + inline std::string to_string(T const &value) { \ + std::string str = "unknown"; \ + \ + for (auto& it : lookup_table) { \ + if (it.second == value) { \ + str = it.first; \ + break; \ + } \ + } \ + \ + return str; \ + }; \ + \ + inline std::ostream& operator<<(std::ostream& os, T const &rhs) { \ + os << to_string(rhs); \ + return os; \ + }; + #endif \ No newline at end of file