add python bindings

This commit is contained in:
jimmylomro
2022-03-15 21:35:11 +00:00
parent 358f445d9e
commit 257b576221
5 changed files with 141 additions and 2 deletions

68
src/bindings.cpp Normal file
View File

@@ -0,0 +1,68 @@
#include <pybind11/pybind11.h>
#include <pybind11/functional.h>
#include <set>
#include <iostream>
#include "libnethogs.h"
namespace py = pybind11;
//--- for some reason this is a global defined in main.cpp
std::set<pid_t> pidsToWatch;
//--- hacky way to get callbacks working and handle signals
std::function<void(int, NethogsMonitorRecord const *)> empty_callback;
std::function<void(int, NethogsMonitorRecord const *)> loop_callback;
void loop_callback_wrapper(int arg1, NethogsMonitorRecord const *arg2){
py::gil_scoped_acquire acquire;
if (PyErr_CheckSignals() != 0) {
nethogsmonitor_breakloop();
PyErr_Clear();
}
else if (loop_callback) {
loop_callback(arg1, arg2);
}
}
int nethogsmonitor_loop_py(
std::function<void(int, NethogsMonitorRecord const *)> &cb,
char *filter,
int to_ms)
{
int retval;
loop_callback = cb;
{
py::gil_scoped_release release;
retval = nethogsmonitor_loop(loop_callback_wrapper, filter, to_ms);
}
loop_callback = empty_callback;
return retval;
}
//--- python module binding
PYBIND11_MODULE(nethogs, m) {
py::class_<NethogsMonitorRecord>(m, "NethogsMonitorRecord")
.def_readwrite("record_id", &NethogsMonitorRecord::record_id)
.def_readwrite("name", &NethogsMonitorRecord::name)
.def_readwrite("pid", &NethogsMonitorRecord::pid)
.def_readwrite("uid", &NethogsMonitorRecord::uid)
.def_readwrite("device_name", &NethogsMonitorRecord::device_name)
.def_readwrite("sent_bytes", &NethogsMonitorRecord::sent_bytes)
.def_readwrite("recv_bytes", &NethogsMonitorRecord::recv_bytes)
.def_readwrite("sent_kbs", &NethogsMonitorRecord::sent_kbs)
.def_readwrite("recv_kbs", &NethogsMonitorRecord::recv_kbs);
m.def("nethogsmonitor_loop", &nethogsmonitor_loop_py, R"pbdoc(
Nethogs monitor loop
)pbdoc");
m.def("nethogsmonitor_breakloop", &nethogsmonitor_breakloop, R"pbdoc(
Nethogs monitor loop break
)pbdoc");
#ifdef VERSION
m.attr("__version__") = VERSION;
#else
m.attr("__version__") = "unknown";
#endif
}

View File

@@ -268,7 +268,7 @@ Process *getProcess(unsigned long inode, const char *devicename) {
if (proc != NULL)
return proc;
if ( !(pidsToWatch.empty()) && pidsToWatch.find(node->pid) == pidsToWatch.end() ) {
if ( !(pidsToWatch.empty()) && pidsToWatch.find(node->pid) == pidsToWatch.end() ) {
return NULL;
}
@@ -440,4 +440,3 @@ void remove_timed_out_processes() {
}
void garbage_collect_processes() { garbage_collect_inodeproc(); }