Skip to content

Commit

Permalink
lua_gc is called every 10s
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-christophe81 committed Feb 1, 2024
1 parent a1a15e6 commit d6851aa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/package-collect.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ jobs:
run: rm -rf *-debuginfo*.${{ matrix.package_extension }}

# set condition to true if artifacts are needed
- if: ${{ false }}
- if: ${{ true }}
name: Upload package artifacts
uses: actions/upload-artifact@694cdabd8bdb0f10b2cea11669e1bf5453eed0a6 # v4.2.0
with:
Expand Down
3 changes: 3 additions & 0 deletions broker/lua/inc/com/centreon/broker/lua/luabinding.hh
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class luabinding {
void _init_script(std::map<std::string, misc::variant> const& conf_params);
void _update_lua_path(std::string const& path);

unsigned _gc_broker_event_cpt;
time_t _gc_last_full_gc;

public:
luabinding(std::string const& lua_script,
std::map<std::string, misc::variant> const& conf_params,
Expand Down
33 changes: 17 additions & 16 deletions broker/lua/src/broker_event.cc
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/**
* Copyright 2020-2021 Centreon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : [email protected]
*/
* Copyright 2020-2021 Centreon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : [email protected]
*/

#include "com/centreon/broker/lua/broker_event.hh"
#include <google/protobuf/message.h>
Expand All @@ -32,6 +32,7 @@ static void _write_item(lua_State* L,
const google::protobuf::Message* p,
const google::protobuf::FieldDescriptor* f);


/**
* The Lua broker_event constructor
*
Expand Down
17 changes: 16 additions & 1 deletion broker/lua/src/luabinding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ luabinding::luabinding(std::string const& lua_script,
_flush{false},
_cache(cache),
_total{0},
_broker_api_version{1} {
_broker_api_version{1},
_gc_broker_event_cpt(0),
_gc_last_full_gc(time(nullptr)) {
size_t pos(lua_script.find_last_of('/'));
std::string path(lua_script.substr(0, pos));
_L = _load_interpreter();
Expand Down Expand Up @@ -396,6 +398,19 @@ int luabinding::write(std::shared_ptr<io::data> const& data) noexcept {
} break;
case 2:
broker_event::create(_L, data);
/*In V2, lua stores only a userdata that contains a shared_ptr of event
* (16 bytes). So garbage collector don't see amount of memory used by
* events.
* So we need to call garbage collector ourselves to reduce memory
* consumption */
time_t now = time(nullptr);
if ((++_gc_broker_event_cpt > 1000 && _gc_last_full_gc + 5 < now) ||
(_gc_last_full_gc + 30 < now)) {
_gc_broker_event_cpt = 0;
_gc_last_full_gc = now;
lua_gc(_L, LUA_GCCOLLECT, 0);
}

break;
}

Expand Down

1 comment on commit d6851aa

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
6 1 0 7 85.71 0s

Failed Tests

Name Message ⏱️ Duration Suite
EBNHGU4_BBDO3 hostgroup_1 not found in /tmp/lua-engine.log 0.000 s Hostgroups

Please sign in to comment.