-
Notifications
You must be signed in to change notification settings - Fork 583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integrate jemalloc #8152
base: master
Are you sure you want to change the base?
Integrate jemalloc #8152
Conversation
5f9f4cd
to
bbf7589
Compare
Numbers: Icinga/docker-icinga2#22 (comment) |
@lippserd FYI: Independent of this PR a regex index doesn't make it much faster. diff --git a/lib/base/scriptutils.cpp b/lib/base/scriptutils.cpp
index 838f20edd..001dc86ed 100644
--- a/lib/base/scriptutils.cpp
+++ b/lib/base/scriptutils.cpp
@@ -16,8 +16,12 @@
#include "base/namespace.hpp"
#include "config/configitem.hpp"
#include <boost/regex.hpp>
+#include <boost/thread/locks.hpp>
+#include <boost/thread/shared_mutex.hpp>
#include <algorithm>
#include <set>
+#include <string>
+#include <unordered_map>
#ifdef _WIN32
#include <msi.h>
#endif /* _WIN32 */
@@ -93,6 +97,11 @@ bool ScriptUtils::CastBool(const Value& value)
return value.ToBool();
}
+static struct {
+ std::unordered_map<std::string, boost::regex> Index;
+ boost::shared_mutex Mutex;
+} l_Regexes;
+
bool ScriptUtils::Regex(const std::vector<Value>& args)
{
if (args.size() < 2)
@@ -111,7 +120,25 @@ bool ScriptUtils::Regex(const std::vector<Value>& args)
else
mode = MatchAll;
- boost::regex expr(pattern.GetData());
+ const boost::regex* expr = nullptr;
+
+ {
+ auto key (pattern.GetData());
+ boost::upgrade_lock<boost::shared_mutex> shared (l_Regexes.Mutex);
+ auto pos (l_Regexes.Index.find(key));
+
+ if (pos == l_Regexes.Index.end()) {
+ boost::upgrade_to_unique_lock<boost::shared_mutex> unique (shared);
+
+ pos = l_Regexes.Index.find(key);
+
+ if (pos == l_Regexes.Index.end()) {
+ pos = l_Regexes.Index.emplace(key, key).first;
+ }
+ }
+
+ expr = &pos->second;
+ }
Array::Ptr texts;
@@ -128,7 +155,7 @@ bool ScriptUtils::Regex(const std::vector<Value>& args)
bool res = false;
try {
boost::smatch what;
- res = boost::regex_search(text.GetData(), what, expr);
+ res = boost::regex_search(text.GetData(), what, *expr);
} catch (boost::exception&) {
res = false; /* exception means something went terribly wrong */
}
@@ -144,7 +171,7 @@ bool ScriptUtils::Regex(const std::vector<Value>& args)
} else {
String text = argTexts;
boost::smatch what;
- return boost::regex_search(text.GetData(), what, expr);
+ return boost::regex_search(text.GetData(), what, *expr);
}
}
|
bbf7589
to
ddda5aa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just leave this to the administrator? They'd have to install jemalloc themselves anyways and if they to this because they want it for icinga2, they can also set an environment variable.
I already find this script strange without this PR, I'd rather see if we can get rid of this mess than extend it.
No, see #8152 (comment). |
ddda5aa
to
874a184
Compare
874a184
to
b58148e
Compare
b58148e
to
2c4c0c0
Compare
@cla-bot check |
@N-o-X Aren't you testing a real world config atm? Please could you test this PR before/after w/ the config? |
Apropos large configs: do you all agree that we just don’t need this on Raspbian for obvious reasons? |
Yes. |
The results are kinda bad. SetupVM:
Config:
TestsCommand: Results 2.13.2Run 1real 12m32.680s Run 2real 12m50.119s Results 2.13.2 + This PRRun 1real 14m40.973s Run 2real 14m35.091s |
Good to know: #8737 (comment) |
One more reason: #8737 (comment) |
I did some playing with jemalloc 5.2.1 (using Full Config
|
Something else to consider: looks like the glibc allocator is better at detecting bad stuff: $ g++ -std=c++11 -o double-free double-free.cpp
$ g++ -std=c++11 -o heap-overflow heap-overflow.cpp
$ ./double-free
free(): double free detected in tcache 2
[1] 1194835 IOT instruction (core dumped) ./double-free
$ ./heap-overflow
munmap_chunk(): invalid pointer
[1] 1194847 IOT instruction (core dumped) ./heap-overflow
$ jemalloc.sh ./double-free
$ echo $?
0
$ jemalloc.sh ./heap-overflow
$ echo $?
0 double-free.cppint main() {
auto x = new int[1];
delete[] x;
delete[] x;
} heap-overflow.cpp#include <vector>
int main() {
std::vector<int> x(1), y(1);
for (int i = 0; i < 8192; i++) {
x[i] = 42;
y[i] = 42;
}
} |
I opt for just closing this one. We should invest the time it takes to test and verify if and how this affects performance into actually improving the code. |
I'm on
Fairly sizeable deployment and suffering from memory leaks which led me here. Not sure if this is helpful, but I added pre config change test for
post config change test for
Should have some solid memory graphs in the morning |
Added fixes #8737 to OP. |
d7c282b
to
35215d8
Compare
This reverts commit 0230883.
35215d8
to
c86176a
Compare
... to speed up malloc(3) and thereby also the config loading:
fixes #8110
Also seems to fix a memory leak for some people:
fixes #8737
Reason: https://www.softwareverify.com/blog/memory-fragmentation-your-worst-nightmare/