Skip to content

Commit 1046260

Browse files
committed
Add a lock mode and allow touching/locking individual idx entries in touchdb
1 parent ba2c33f commit 1046260

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/commons/Parameters.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ Parameters::Parameters():
319319
PARAM_TEMPERATURE(PARAM_TEMPERATURE_ID, "--temperature", "Temperature", "Temperature for forward-backward", typeid(float), (void *) &temperature, "^(0\\.[0-9]+|[1-9][0-9]*\\.?[0-9]*)$", MMseqsParameter::COMMAND_EXPERT),
320320
PARAM_BLOCKLEN(PARAM_BLOCKLEN_ID, "--blocklen", "Block length", "Block length for forward-backward", typeid(int), (void *) &blocklen, "^[1-9]{1}[0-9]*$", MMseqsParameter::COMMAND_EXPERT),
321321
PARAM_FWBW_BACKTRACE_MODE(PARAM_FWBW_BACKTRACE_MODE_ID, "--fwbw-backtrace-mode", "Backtrace mode", "Backtrace mode 0: no backtrace, 1: local", typeid(int), (void *) &fwbwBacktraceMode, "^[01]$", MMseqsParameter::COMMAND_EXPERT),
322+
// touchdb
323+
PARAM_TOUCH_LOCK(PARAM_TOUCH_LOCK_ID, "--touch-lock", "Touch lock", "Lock touched database or database entries into memory. Process will not exit until killed.", typeid(bool), (void *) &touchLock, "", MMseqsParameter::COMMAND_EXPERT),
322324
// for modules that should handle -h themselves
323325
PARAM_HELP(PARAM_HELP_ID, "-h", "Help", "Help", typeid(bool), (void *) &help, "", MMseqsParameter::COMMAND_HIDDEN),
324326
PARAM_HELP_LONG(PARAM_HELP_LONG_ID, "--help", "Help", "Help", typeid(bool), (void *) &help, "", MMseqsParameter::COMMAND_HIDDEN)
@@ -1493,6 +1495,8 @@ Parameters::Parameters():
14931495
appenddbtoindex.push_back(&PARAM_V);
14941496
14951497
// touchdb
1498+
touchdb.push_back(&PARAM_ID_LIST);
1499+
touchdb.push_back(&PARAM_TOUCH_LOCK);
14961500
touchdb.push_back(&PARAM_THREADS);
14971501
touchdb.push_back(&PARAM_V);
14981502
@@ -2707,6 +2711,10 @@ void Parameters::setDefaults() {
27072711
temperature = 1;
27082712
blocklen = 16;
27092713
fwbwBacktraceMode = 1;
2714+
2715+
// touchdb
2716+
touchLock = false;
2717+
27102718
// help
27112719
help = 0;
27122720

src/commons/Parameters.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ class Parameters {
740740
float temperature;
741741
int blocklen;
742742
int fwbwBacktraceMode;
743+
744+
// touchdb
745+
bool touchLock;
746+
743747
// for modules that should handle -h themselves
744748
bool help;
745749

@@ -1107,6 +1111,10 @@ class Parameters {
11071111
PARAMETER(PARAM_TEMPERATURE)
11081112
PARAMETER(PARAM_BLOCKLEN)
11091113
PARAMETER(PARAM_FWBW_BACKTRACE_MODE)
1114+
1115+
// touchdb
1116+
PARAMETER(PARAM_TOUCH_LOCK)
1117+
11101118
// for modules that should handle -h themselves
11111119
PARAMETER(PARAM_HELP)
11121120
PARAMETER(PARAM_HELP_LONG)

src/util/touchdb.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,70 @@
33
#include "PrefilteringIndexReader.h"
44
#include "MemoryMapped.h"
55

6+
#include <sys/mman.h>
7+
68
int touchdb(int argc, const char **argv, const Command& command) {
79
Parameters& par = Parameters::getInstance();
810
par.parseParameters(argc, argv, command, true, 0, 0);
911

10-
std::string db = par.db1;
12+
const char* mlockError = "Consider granting the `CAP_IPC_LOCK` capability to this binary (e.g., `setcap cap_ipc_lock=+ep <program>`, or run as root) or raising the locked-memory limit so `mlock(2)` can succeed. Check your current limit with `ulimit -l` and increase it via `ulimit -l <KB>` for the shell, `LimitMEMLOCK=infinity` in your systemd unit, or persistent user limits in `/etc/security/limits.conf` (e.g., `youruser hard memlock unlimited`)\n";
1113

14+
std::string db = par.db1;
1215
std::string indexDB = PrefilteringIndexReader::searchForIndex(db);
1316
if (indexDB.empty() == false) {
1417
db = indexDB;
18+
if (par.idList != "") {
19+
std::string idx = db + ".index";
20+
DBReader<unsigned int> reader(db.c_str(), idx.c_str(), 1, DBReader<unsigned int>::USE_DATA | DBReader<unsigned int>::USE_INDEX);
21+
reader.open(DBReader<unsigned int>::NOSORT);
22+
23+
std::vector<std::string> ids = Util::split(par.idList, ",");
24+
for (size_t i = 0; i < ids.size(); ++i) {
25+
size_t id = reader.getId(Util::fast_atoi<unsigned int>(ids[i].c_str()));
26+
if (id == UINT_MAX) {
27+
Debug(Debug::WARNING) << "Key " << ids[i] << " not found in database\n";
28+
continue;
29+
}
30+
size_t currDataOffset = reader.getOffset(id);
31+
size_t nextDataOffset = reader.findNextOffsetid(id);
32+
size_t dataSize = nextDataOffset - currDataOffset;
33+
char* data = reader.getDataUncompressed(id);
34+
Util::touchMemory(data, dataSize);
35+
if (par.touchLock) {
36+
int res = mlock(data, dataSize);
37+
if (res != 0) {
38+
Debug(Debug::ERROR) << "Could not lock memory " << strerror(errno) << "\n";
39+
Debug(Debug::ERROR) << mlockError;
40+
reader.close();
41+
return EXIT_FAILURE;
42+
}
43+
}
44+
}
45+
46+
if (par.touchLock) {
47+
Debug(Debug::INFO) << "Touched and locked " << ids.size() << " entries. Process will not exit until killed.\n";
48+
pause();
49+
} else {
50+
Debug(Debug::INFO) << "Touched " << ids.size() << " entries\n";
51+
}
52+
53+
reader.close();
54+
return EXIT_SUCCESS;
55+
}
1556
}
1657

1758
MemoryMapped map(db, MemoryMapped::WholeFile, MemoryMapped::CacheHint::SequentialScan);
1859
Util::touchMemory(reinterpret_cast<const char*>(map.getData()), map.mappedSize());
60+
if (par.touchLock) {
61+
int res = mlock(map.getData(), map.mappedSize());
62+
if (res != 0) {
63+
Debug(Debug::ERROR) << "Could not lock memory " << strerror(errno) << "\n";
64+
Debug(Debug::ERROR) << mlockError;
65+
return EXIT_FAILURE;
66+
}
67+
Debug(Debug::INFO) << "Touched and locked database. Process will not exit until killed.\n";
68+
pause();
69+
}
1970

2071
return EXIT_SUCCESS;
2172
}

0 commit comments

Comments
 (0)