Skip to content

Commit e3222ba

Browse files
kostorrpippohub
authored andcommitted
Implement socket locks as an IPC locking mechnism
1 parent 19d8083 commit e3222ba

File tree

16 files changed

+134
-221
lines changed

16 files changed

+134
-221
lines changed

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ set(SRCS
7676
src/Factory/ChannelFactory.cxx
7777
src/DmaChannelBase.cxx
7878
src/ChannelPaths.cxx
79-
src/Driver.cxx
8079
src/Dummy/DummyDmaChannel.cxx
8180
src/Dummy/DummyBar.cxx
8281
src/ExceptionInternal.cxx
@@ -212,7 +211,7 @@ set(TEST_SRCS
212211
test/TestChannelPaths.cxx
213212
test/TestCruDataFormat.cxx
214213
test/TestEnums.cxx
215-
test/TestInterprocessLock.cxx
214+
#test/TestInterprocessLock.cxx
216215
test/TestMemoryMappedFile.cxx
217216
test/TestParameters.cxx
218217
test/TestPciAddress.cxx

include/ReadoutCard/Driver.h

Lines changed: 0 additions & 27 deletions
This file was deleted.

include/ReadoutCard/ReadoutCard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "ReadoutCard/CardType.h"
88
#include "ReadoutCard/ChannelFactory.h"
99
#include "ReadoutCard/DmaChannelInterface.h"
10-
#include "ReadoutCard/Driver.h"
1110
#include "ReadoutCard/Exception.h"
1211
#include "ReadoutCard/Parameters.h"
13-
#include "ReadoutCard/RegisterReadWriteInterface.h"
12+
#include "ReadoutCard/RegisterReadWriteInterface.h"

src/CommandLineUtilities/Options.cxx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ Parameters::CardIdType getOptionCardId(const po::variables_map& map)
202202
return Parameters::cardIdFromString(string);
203203
}
204204

205+
std::string getOptionCardIdString(const po::variables_map& map)
206+
{
207+
return getOption(option::cardId, map);
208+
}
209+
205210
} // namespace Options
206211
} // namespace Util
207212
} // namespace roc

src/CommandLineUtilities/Options.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ int getOptionRegisterValue(const boost::program_options::variables_map& map);
3232
int getOptionChannel(const boost::program_options::variables_map& map);
3333
ResetLevel::type getOptionResetLevel(const boost::program_options::variables_map& map);
3434
Parameters::CardIdType getOptionCardId(const boost::program_options::variables_map& map);
35+
std::string getOptionCardIdString(const boost::program_options::variables_map& map);
3536
int getOptionRegisterRange(const boost::program_options::variables_map& map);
3637

3738
} // namespace Options
3839
} // namespace CommandLineUtilities
3940
} // namespace roc
4041
} // namespace AliceO2
4142

42-
#endif // ALICEO2_READOUTCARD_OPTIONS_H
43+
#endif // ALICEO2_READOUTCARD_OPTIONS_H

src/CommandLineUtilities/ProgramDmaBench.cxx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
///
33
/// \brief Utility that tests ReadoutCard DMA performance
44
/// \author Pascal Boeschoten ([email protected])
5+
/// \author Kostas Alexopoulos ([email protected])
56

67

78
#include <chrono>
@@ -35,6 +36,7 @@
3536
#include "ReadoutCard/MemoryMappedFile.h"
3637
#include "ReadoutCard/Parameters.h"
3738
#include "ReadoutCard/ReadoutCard.h"
39+
#include "time.h"
3840
#include "Utilities/Hugetlbfs.h"
3941
#include "Utilities/SmartPointer.h"
4042
#include "Utilities/Util.h"
@@ -229,14 +231,16 @@ class ProgramDmaBench: public Program
229231

230232
// Create channel buffer
231233
{
232-
driver::freeUnusedChannelBuffers();
233-
234234
if (mBufferSize < mSuperpageSize) {
235235
throw ParameterException() << ErrorInfo::Message("Buffer size smaller than superpage size");
236236
}
237237

238-
std::string bufferName = (b::format("roc-bench-dma_id=%s_chan=%s_pages") % map["id"].as<std::string>()
239-
% mOptions.dmaChannel).str();
238+
// Add time to the buffer's filename. This way we guard our buffer from being overwritten by another process
239+
// as this is before the DMA Channel initialization and no lock protection is in place.
240+
std::string bufferName = (b::format("roc-bench-dma_id=%s_chan=%s_%s_pages") % map["id"].as<std::string>()
241+
% mOptions.dmaChannel
242+
% time(0)).str();
243+
240244
Utilities::HugepageType hugepageType;
241245
mMemoryMappedFile = Utilities::tryMapFile(mBufferSize, bufferName, !mOptions.noRemovePagesFile, &hugepageType);
242246

src/Crorc/CrorcDmaChannel.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#include "Utilities/SmartPointer.h"
1919

2020
namespace b = boost;
21-
namespace bip = boost::interprocess;
22-
namespace bfs = boost::filesystem;
21+
//namespace bip = boost::interprocess;
22+
//namespace bfs = boost::filesystem;
2323
using std::cout;
2424
using std::endl;
2525
using namespace std::literals;

src/DmaChannelBase.cxx

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
/// \brief Implementation of the DmaChannelBase class.
33
///
44
/// \author Pascal Boeschoten ([email protected])
5+
/// \author Kostas Alexopoulos ([email protected])
56

67
#include <boost/filesystem.hpp>
78
#include "DmaChannelBase.h"
89
#include <iostream>
9-
#include "ChannelPaths.h"
10+
//#include "ChannelPaths.h"
1011
#include "Common/System.h"
1112
#include "Utilities/SmartPointer.h"
1213
#include "Visitor.h"
@@ -47,6 +48,48 @@ void DmaChannelBase::checkParameters(Parameters& parameters)
4748
}
4849
}
4950

51+
void DmaChannelBase::freeUnusedChannelBuffer()
52+
{
53+
namespace bfs = boost::filesystem;
54+
InfoLogger::InfoLogger logger;
55+
56+
try {
57+
Pda::PdaLock lock{}; // We're messing around with PDA buffers so we need this even though we hold the DMA lock
58+
} catch (const LockException& exception) {
59+
log("Failed to acquire PDA lock", InfoLogger::InfoLogger::Debug);
60+
throw;
61+
}
62+
63+
try {
64+
std::string pciPath = "/sys/bus/pci/drivers/uio_pci_dma/";
65+
if (boost::filesystem::exists(pciPath)) {
66+
for (auto &entry : boost::make_iterator_range(bfs::directory_iterator(pciPath), {})) {
67+
auto filename = entry.path().filename().string();
68+
if (filename.size() == 12) {
69+
// The PCI directory names are 12 characters long
70+
auto pciAddress = filename.substr(5); // Remove leading '0000:'
71+
72+
if (PciAddress::fromString(pciAddress) && (pciAddress == mCardDescriptor.pciAddress.toString())) {
73+
// This is a valid PCI address and it's *ours*
74+
std::string dmaPath("/sys/bus/pci/drivers/uio_pci_dma/" + filename + "/dma");
75+
for (auto &entry : boost::make_iterator_range(bfs::directory_iterator(dmaPath), {})) {
76+
auto bufferId = entry.path().filename().string();
77+
if (bfs::is_directory(entry)) {
78+
std::string mapPath = dmaPath + "/" + bufferId + "/map";
79+
std::string freePath = dmaPath + "/free";
80+
logger << "Freeing PDA buffer '" + mapPath + "'" << InfoLogger::InfoLogger::endm;
81+
AliceO2::Common::System::executeCommand("echo " + bufferId + " > " + freePath);
82+
}
83+
}
84+
}
85+
}
86+
}
87+
}
88+
} catch (const boost::filesystem::filesystem_error &e) {
89+
logger << "Failed to free buffers: " << e.what() << InfoLogger::InfoLogger::endm;
90+
throw;
91+
}
92+
}
5093

5194
DmaChannelBase::DmaChannelBase(CardDescriptor cardDescriptor, Parameters& parameters,
5295
const AllowedChannels& allowedChannels)
@@ -62,41 +105,24 @@ DmaChannelBase::DmaChannelBase(CardDescriptor cardDescriptor, Parameters& parame
62105
// Do some basic Parameters validity checks
63106
checkParameters(parameters);
64107

65-
// Create parent directories
66-
auto paths = getPaths();
67-
for (const auto& p : {paths.fifo(), paths.lock()}) {
68-
Common::System::makeParentDirectories(p);
69-
}
70-
71-
// Get lock
108+
//try to acquire lock
72109
log("Acquiring DMA channel lock", InfoLogger::InfoLogger::Debug);
73-
try {
74-
Utilities::resetSmartPtr(mInterprocessLock, paths.lock(), paths.namedMutex());
110+
try{
111+
Utilities::resetSmartPtr(mInterprocessLock, "Alice_O2_RoC_DMA_" + cardDescriptor.pciAddress.toString() +"_lock");
75112
}
76-
catch (const NamedMutexLockException& exception) {
77-
if (parameters.getForcedUnlockEnabled().get_value_or(false)) {
78-
log("Failed to acquire DMA channel mutex. Forced unlock enabled - attempting cleanup and retry",
79-
InfoLogger::InfoLogger::Warning);
80-
// The user has indicated to the driver that it is safe to force the unlock. We do it by deleting the lock.
81-
boost::interprocess::named_mutex::remove(paths.namedMutex().c_str());
82-
// Try again...
83-
Utilities::resetSmartPtr(mInterprocessLock, paths.lock(), paths.namedMutex());
84-
} else {
85-
log("Failed to acquire DMA channel lock", InfoLogger::InfoLogger::Debug);
86-
throw;
87-
}
113+
catch (const LockException& exception) {
114+
log("Failed to acquire DMA channel lock", InfoLogger::InfoLogger::Debug);
115+
throw;
88116
}
117+
89118
log("Acquired DMA channel lock", InfoLogger::InfoLogger::Debug);
119+
120+
freeUnusedChannelBuffer();
90121
}
91122

92123
DmaChannelBase::~DmaChannelBase()
93124
{
94125
log("Releasing DMA channel lock", InfoLogger::InfoLogger::Debug);
95-
96-
std::string channelLockPath = "/dev/shm/AliceO2_RoC_" + std::string(getCardDescriptor().pciAddress.toString()) + "_Channel_" + std::to_string((int) getChannelNumber ()) + ".lock";
97-
std::string channelMutexPath = "/dev/shm/sem.AliceO2_RoC_" + std::string(getCardDescriptor().pciAddress.toString()) + "_Channel_" + std::to_string((int) getChannelNumber ()) + "_Mutex";
98-
bfs::remove(channelLockPath);
99-
bfs::remove(channelMutexPath);
100126
}
101127

102128
void DmaChannelBase::log(const std::string& message, boost::optional<InfoLogger::InfoLogger::Severity> severity)

src/DmaChannelBase.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "CardDescriptor.h"
1616
#include "ChannelPaths.h"
1717
#include "InterprocessLock.h"
18+
#include "Pda/PdaLock.h"
1819
#include "ReadoutCard/DmaChannelInterface.h"
1920
#include "ReadoutCard/Exception.h"
2021
#include "ReadoutCard/Parameters.h"
@@ -112,6 +113,9 @@ class DmaChannelBase: public DmaChannelInterface
112113
/// Check the validity of basic parameters
113114
void checkParameters(Parameters& parameters);
114115

116+
/// Free device's PDA Channel Buffer
117+
void freeUnusedChannelBuffer();
118+
115119
/// Type of the card
116120
const CardDescriptor mCardDescriptor;
117121

src/Driver.cxx

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)