1818#include < boost/algorithm/string/predicate.hpp>
1919#include < boost/filesystem.hpp>
2020#include < iostream>
21- #include " ReadoutCard/ChannelFactory.h "
21+ #include < pwd.h >
2222#include " CommandLineUtilities/Common.h"
2323#include " CommandLineUtilities/Options.h"
2424#include " CommandLineUtilities/Program.h"
2525#include " Pda/Util.h"
26- #include " ReadoutCard/CardDescriptor.h"
26+ #include " ReadoutCard/CardFinder.h"
27+ #include " ReadoutCard/InterprocessLock.h"
2728
2829using namespace o2 ::roc::CommandLineUtilities;
2930using namespace o2 ::roc;
@@ -60,7 +61,7 @@ class ProgramCleanup : public Program
6061 std::cout << " 4. Remove and reinsert the uio_pci_dma kernel module" << std::endl;
6162 }
6263 std::cout << std::endl;
63- std::cout << " In case instances of readout.exe or o2-roc-bench-dma are running, they will fail." << std::endl;
64+ std::cout << " In case instances of readout.exe or o2-roc-bench-dma are running, roc-cleanup will fail." << std::endl;
6465 std::cout << std::endl;
6566 std::cout << " This tool is intended to be run with elevated privileges." << std::endl;
6667 std::cout << " Are you sure you want to continue? (yes/no)" << std::endl;
@@ -71,25 +72,63 @@ class ProgramCleanup : public Program
7172 return ;
7273 }
7374
75+ Logger::setFacility (" ReadoutCard/cleanup" );
76+
77+ auto getUsername = []() -> std::string {
78+ struct passwd * passw = getpwuid (geteuid ());
79+ if (passw) {
80+ return std::string (passw->pw_name );
81+ }
82+ return {};
83+ };
84+
85+ Logger::get () << " `roc-cleanup` execution initiated by " << getUsername () << LogDebugOps << endm;
86+
87+ // Take and hold DMA locks during cleanup
88+ std::vector<std::unique_ptr<Interprocess::Lock>> dmaLocks;
89+
90+ Logger::get () << " Grabbing PDA & DMA locks" << LogDebugDevel << endm;
91+ try {
92+ auto cards = findCards ();
93+
94+ for (auto card : cards) {
95+ if (card.cardType == CardType::Cru) {
96+ std::string lockName (" Alice_O2_RoC_DMA_" + card.pciAddress .toString () + " _lock" );
97+ dmaLocks.push_back (std::make_unique<Interprocess::Lock>(lockName));
98+ } else if (card.cardType == CardType::Crorc) {
99+ for (int chan = 0 ; chan < 6 ; chan++) {
100+ std::string lockName (" Alice_O2_RoC_DMA_" + card.pciAddress .toString () + " _chan" + std::to_string (chan) + " _lock" );
101+ dmaLocks.push_back (std::make_unique<Interprocess::Lock>(lockName));
102+ }
103+ } // ignore an invalid card type
104+ }
105+ } catch (std::runtime_error& e) {
106+ Logger::get () << e.what () << LogErrorDevel << endm;
107+ return ;
108+ }
109+
74110 Pda::freePdaDmaBuffers ();
75111
76- std::cout << " Removing CRORC FIFO shared memory files" << std::endl ;
112+ Logger::get () << " Removing CRORC FIFO shared memory files" << LogDebugDevel << endm ;
77113 sysCheckRet (" rm /dev/shm/AliceO2_RoC_*" );
78- std::cout << " Removing readout 2MB hugepage mappings" << std::endl ;
114+ Logger::get () << " Removing readout 2MB hugepage mappings" << LogDebugDevel << endm ;
79115 sysCheckRet (" rm /var/lib/hugetlbfs/global/pagesize-2MB/readout*" );
80- std::cout << " Removing readout 1GB hugepage mappings" << std::endl ;
116+ Logger::get () << " Removing readout 1GB hugepage mappings" << LogDebugDevel << endm ;
81117 sysCheckRet (" rm /var/lib/hugetlbfs/global/pagesize-1GB/readout*" );
82- std::cout << " Removing o2-roc-bench-dma 2MB hugepage mappings" << std::endl ;
118+ Logger::get () << " Removing o2-roc-bench-dma 2MB hugepage mappings" << LogDebugDevel << endm ;
83119 sysCheckRet (" rm /var/lib/hugetlbfs/global/pagesize-2MB/roc-bench-dma*" );
84- std::cout << " Removing o2-roc-bench-dma 1GB hugepage mappings" << std::endl ;
120+ Logger::get () << " Removing o2-roc-bench-dma 1GB hugepage mappings" << LogDebugDevel << endm ;
85121 sysCheckRet (" rm /var/lib/hugetlbfs/global/pagesize-1GB/roc-bench-dma*" );
86122
87123 if (!mOptions .light ) {
88- std::cout << " Removing uio_pci_dma" << std::endl ;
124+ Logger::get () << " Removing uio_pci_dma" << LogDebugDevel << endm ;
89125 sysCheckRet (" modprobe -r uio_pci_dma" );
90- std::cout << " Reinserting uio_pci_dma" << std::endl ;
126+ Logger::get () << " Reinserting uio_pci_dma" << LogDebugDevel << endm ;
91127 sysCheckRet (" modprobe uio_pci_dma" );
92128 }
129+
130+ Logger::get () << " `roc-cleanup` execution finished" << LogDebugOps << endm;
131+ return ;
93132 }
94133
95134 private:
@@ -100,8 +139,8 @@ class ProgramCleanup : public Program
100139 void sysCheckRet (const char * command)
101140 {
102141 int ret = system (command);
103- if (ret) {
104- std::cerr << " Command: `" << command << " ` failed with ret=" << ret << std::endl ;
142+ if (ret && ret != 256 ) { // Don't log in case the file was not found
143+ Logger::get () << " Command: `" << command << " ` failed with ret=" << ret << LogDebugDevel << endm ;
105144 }
106145 }
107146};
0 commit comments