Skip to content

Commit 5681456

Browse files
committed
added logging of mem usage, and options for memlock settings
1 parent 30eeed2 commit 5681456

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

src/testFmqMemory.cxx

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,41 @@
1414
#include <string.h>
1515
#include <chrono>
1616
#include <sys/types.h>
17+
#include <sys/mman.h>
18+
#include <unistd.h>
1719

1820
#include <fairmq/FairMQDevice.h>
1921
#include <fairmq/FairMQTransportFactory.h>
2022
//#include <fairmq/tools/Unique.h>
2123

24+
void logMemoryUsage() {
25+
double memPageSize = getpagesize() / (1024.0*1024.0);
26+
const int maxpath = 256;
27+
char fn[maxpath];
28+
snprintf(fn, maxpath, "/proc/%d/statm", getpid());
29+
FILE *fp=fopen(fn,"r");
30+
const int maxline = 256;
31+
char buf[maxline];
32+
if (fgets(buf, maxline, fp) != NULL) {
33+
int vsize, vresident, vshared, vtext, vlib, vdata, vdt;
34+
if (sscanf(buf, "%d %d %d %d %d %d %d", &vsize, &vresident, &vshared, &vtext, &vlib, &vdata, &vdt) == 7) {
35+
printf("Memory stats: size = %6.2f MB\tresident = %6.2f MB\tshared = %6.2f MB\n", vsize * memPageSize, vresident * memPageSize, vshared * memPageSize);
36+
}
37+
}
38+
fclose(fp);
39+
}
40+
41+
2242
#define GB *1073741824LLU
2343
#define SLEEPTIME 3
24-
#define WAITHERE printf("Waiting %ds ",SLEEPTIME); for(int k=0; k<SLEEPTIME; k++) {printf(".");fflush(stdout);usleep(1000000);} printf("\n");
44+
//#define WAITHERE printf("Waiting %ds ",SLEEPTIME); for(int k=0; k<SLEEPTIME; k++) {printf(".");fflush(stdout);usleep(1000000);} printf("\n");
45+
#define WAITHERE logMemoryUsage();
46+
47+
48+
// memory settings
49+
const bool memlock = false; // lock the whole process memory
50+
const bool fmqMemLock = true; // lock FMQ region
51+
const bool fmqMemZero = false; // zero FMQ region
2552

2653

2754
int main(int argc, const char* argv[]) {
@@ -34,7 +61,14 @@ int main(int argc, const char* argv[]) {
3461
return -1;
3562
}
3663

37-
printf("Startup %d\n",(int)getpid());
64+
printf("Locking process memory: %s\n", memlock ? "yes" : "no");
65+
if (memlock) {
66+
if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0) {
67+
printf("failed to lock memory\n");
68+
}
69+
}
70+
71+
printf("Startup pid %d\n",(int)getpid());
3872
WAITHERE;
3973

4074
std::unique_ptr<FairMQChannel> sendingChannel;
@@ -48,13 +82,13 @@ int main(int argc, const char* argv[]) {
4882
sendingChannel = std::make_unique<FairMQChannel>("readout-test", "pair", transportFactory);
4983
WAITHERE;
5084

51-
printf("Get unmanaged memory\n");
85+
printf("Get unmanaged memory (lock=%s, zero=%s)\n", fmqMemLock ? "yes" : "no", fmqMemZero ? "yes" : "no");
5286
long long mMemorySize = ngb GB;
5387
auto t00 = std::chrono::steady_clock::now();
5488
try {
5589
// memoryBuffer = sendingChannel->Transport()->CreateUnmanagedRegion(mMemorySize, [](void* /*data*/, size_t /*size*/, void* /*hint*/) {});
5690
memoryBuffer = sendingChannel->Transport()->CreateUnmanagedRegion(mMemorySize, [](void* /*data*/, size_t /*size*/, void* /*hint*/) {},
57-
"",0,fair::mq::RegionConfig{true, false}); // lock / zero
91+
"",0,fair::mq::RegionConfig{fmqMemLock, fmqMemZero}); // lock / zero
5892
}
5993
catch(...) {
6094
printf("Failed to get buffer (exception)\n"); return 1;
@@ -66,6 +100,7 @@ int main(int argc, const char* argv[]) {
66100
WAITHERE;
67101

68102
printf("Write to memory, by chunks of 1GB\n");
103+
t00 = std::chrono::steady_clock::now();
69104
for (unsigned int i=0; i<ngb; i++) {
70105
auto t0 = std::chrono::steady_clock::now();
71106
char *ptr=(char *)memoryBuffer->GetData();
@@ -82,11 +117,10 @@ int main(int argc, const char* argv[]) {
82117
std::chrono::duration<double> tdiff = std::chrono::steady_clock::now() - t0;
83118
printf(" %.2lf GB/s\n", 1.0/tdiff.count());
84119
}
120+
std::chrono::duration<double> tdiff1 = std::chrono::steady_clock::now() - t00;
85121
printf("Done writing\n");
86-
87-
tdiff0 = std::chrono::steady_clock::now() - t00;
88-
printf("Average: %.2lf GB/s (including alloc)\n", ngb * 1.0/(tdiff0.count()-SLEEPTIME));
89-
122+
printf("Average: %.2lf GB/s (writing)\n", ngb * 1.0/tdiff1.count());
123+
printf("Average: %.2lf GB/s (writing + malloc)\n", ngb * 1.0/(tdiff0.count() + tdiff1.count()));
90124
WAITHERE;
91125

92126
printf("Cleanup FMQ unmanaged region\n");

0 commit comments

Comments
 (0)