From 20830040e967d895c8be9356d4b0e2d3394b8b7f Mon Sep 17 00:00:00 2001 From: Chirag Sharma Date: Thu, 26 Aug 2021 08:09:53 -0500 Subject: [PATCH] libguard: Adding function to erase guardrecords Changes: -Adding new function to erase/wipout all guard records present in GUARD file. -Adding -e option in tool, which will call the new API to erase the data from GUARD file. -Adding UT for the newly introduced function. -Updating the README.md with new option details. Test Results: Running main() from ../googletest/src/gtest_main.cc [==========] Running 15 tests from 1 test suite. [----------] Global test environment set-up. [----------] 15 tests from TestGuardRecord [ RUN ] TestGuardRecord.CreateGuardRecord [ OK ] TestGuardRecord.CreateGuardRecord (0 ms) [ RUN ] TestGuardRecord.ClearGuardGoodPathTest [ OK ] TestGuardRecord.ClearGuardGoodPathTest (0 ms) [ RUN ] TestGuardRecord.DeleteGuardGoodPathTest [ OK ] TestGuardRecord.DeleteGuardGoodPathTest (0 ms) [ RUN ] TestGuardRecord.NegTestCaseEP [ OK ] TestGuardRecord.NegTestCaseEP (0 ms) [ RUN ] TestGuardRecord.NegTestCaseFullGuardFile No resolved Guard record found Size left in GUARD file is 0 [ OK ] TestGuardRecord.NegTestCaseFullGuardFile (0 ms) [ RUN ] TestGuardRecord.AlreadyGuardedTC Already guard record is available in the GUARD partition [ OK ] TestGuardRecord.AlreadyGuardedTC (0 ms) [ RUN ] TestGuardRecord.GetCreatedGuardRecordTC [ OK ] TestGuardRecord.GetCreatedGuardRecordTC (0 ms) [ RUN ] TestGuardRecord.DeleteByEntityPath [ OK ] TestGuardRecord.DeleteByEntityPath (0 ms) [ RUN ] TestGuardRecord.DeleteWithNotExistentEntity Guard record not found [ OK ] TestGuardRecord.DeleteWithNotExistentEntity (0 ms) [ RUN ] TestGuardRecord.DeleteByRecordId [ OK ] TestGuardRecord.DeleteByRecordId (0 ms) [ RUN ] TestGuardRecord.DeleteWithNotExistentRecordId Guard record not found [ OK ] TestGuardRecord.DeleteWithNotExistentRecordId (0 ms) [ RUN ] TestGuardRecord.GetGuardFilePathTC [ OK ] TestGuardRecord.GetGuardFilePathTC (0 ms) [ RUN ] TestGuardRecord.GetGuardFilePathWhenLibguradDidNotInitTC Guard file is not initialised. [ OK ] TestGuardRecord.GetGuardFilePathWhenLibguradDidNotInitTC (0 ms) [ RUN ] TestGuardRecord.ClearResolvedGuardRecord [ OK ] TestGuardRecord.ClearResolvedGuardRecord (0 ms) [ RUN ] TestGuardRecord.EraseAllRecordsTest [ OK ] TestGuardRecord.EraseAllRecordsTest (0 ms) [----------] 15 tests from TestGuardRecord (7 ms total) [----------] Global test environment tear-down [==========] 15 tests from 1 test suite ran. (7 ms total) [ PASSED ] 15 tests. ------------------------------------------------------------------------------ Ok: 1 Expected Fail: 0 Fail: 0 Unexpected Pass: 0 Skipped: 0 Timeout: 0 Signed-off-by: Chirag Sharma --- README.md | 5 +++++ guard.cpp | 13 ++++++++++++- libguard/guard_interface.cpp | 7 +++++++ libguard/guard_interface.hpp | 7 +++++++ test/guard_intf_test.cpp | 14 ++++++++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 688f09d..58fd020 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Options: -r,--clearall Clears GUARD states for all resources. -s,-clearresolvedrecord Delete GUARD states for oldest resolved resource -p,--listresolvedrecords Listing of resolved GUARDed resources + -e,--eraseall Erase all the GUARDed resources -v,--version Version of GUARD tool. ``` **Note:** Physical path can be fetched from device tree, using ATTR_PHYS_DEV_PATH @@ -83,3 +84,7 @@ guard -p ID | ERROR | Type | Path ffffffff | 00000000 | manual | /sys-0/node-0/dimm-0 ``` +* To erase all the guard records +``` +guard -e +``` diff --git a/guard.cpp b/guard.cpp index 3e8c648..8d909c7 100644 --- a/guard.cpp +++ b/guard.cpp @@ -108,6 +108,11 @@ void guardCreate(const std::string& physicalPath) std::cout << "Success" << std::endl; } +void guardErase() +{ + eraseAllRecords(); +} + static void exitWithError(const std::string& help, const char* err) { std::cerr << "ERROR: " << err << std::endl << help << std::endl; @@ -123,8 +128,8 @@ int main(int argc, char** argv) std::optional deleteGuardStr; bool listGuardRecords = false; bool clearAll = false; - bool clearResolvedRecord = false; bool listResolvedGuardRecords = false; + bool eraseall = false; bool gversion = false; app.set_help_flag("-h, --help", @@ -141,6 +146,8 @@ int main(int argc, char** argv) "Clears GUARD states for all resources"); app.add_flag("-p, --listresolvedrecords", listResolvedGuardRecords, "Listing of resolved GUARDed resources"); + app.add_flag("-e, --eraseall", eraseall, + "Erase all the GUARDed resources"); app.add_flag("-v, --version", gversion, "Version of GUARD tool"); CLI11_PARSE(app, argc, argv); @@ -167,6 +174,10 @@ int main(int argc, char** argv) { guardList(true); } + else if (eraseall) + { + guardErase(); + } else if (gversion) { std::cout << "GUARD Tool version is " << GUARD_VERSION << std::endl; diff --git a/libguard/guard_interface.cpp b/libguard/guard_interface.cpp index a748b7c..01786b5 100644 --- a/libguard/guard_interface.cpp +++ b/libguard/guard_interface.cpp @@ -386,6 +386,13 @@ int clearResolvedRecord() return delpos; } +void eraseAllRecords() +{ + GuardFile file(guardFilePath); + + file.erase(0, file.size()); +} + void libguard_init(bool enableDevtree) { initialize(); diff --git a/libguard/guard_interface.hpp b/libguard/guard_interface.hpp index 43415d7..21640f2 100644 --- a/libguard/guard_interface.hpp +++ b/libguard/guard_interface.hpp @@ -97,6 +97,13 @@ void libguard_init(bool enableDevtree = true); */ const fs::path& getGuardFilePath(); +/** + * @brief Erase/wipeout all the guard records from GUARD file + * + * @return NULL + */ +void eraseAllRecords(); + namespace utest { void setGuardFile(const fs::path& file); diff --git a/test/guard_intf_test.cpp b/test/guard_intf_test.cpp index ae0827c..0ff679e 100644 --- a/test/guard_intf_test.cpp +++ b/test/guard_intf_test.cpp @@ -297,3 +297,17 @@ TEST_F(TestGuardRecord, ClearResolvedGuardRecord) openpower::guard::GuardRecords records = openpower::guard::getAll(); EXPECT_EQ(records.size(), 2); } + +TEST_F(TestGuardRecord, EraseAllRecordsTest) +{ + openpower::guard::libguard_init(); + std::string phyPath = "/sys-0/node-0/proc-1/eq-0/fc-0/core-0"; + std::optional entityPath = + openpower::guard::getEntityPath(phyPath); + openpower::guard::create(*entityPath); + openpower::guard::GuardRecords records = openpower::guard::getAll(); + EXPECT_EQ(records.size(), 1); + openpower::guard::eraseAllRecords(); + openpower::guard::GuardRecords records = openpower::guard::getAll(); + EXPECT_EQ(records.size(), 0); +}