Skip to content
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

update(gvisor): implement get_stats for gVisor #463

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion userspace/libscap/engine/gvisor/gvisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static int32_t gvisor_configure(struct scap_engine_handle engine, enum scap_sett

static int32_t gvisor_get_stats(struct scap_engine_handle engine, scap_stats* stats)
{
return SCAP_SUCCESS;
return engine.m_handle->get_stats(stats);
}

static int32_t gvisor_get_n_tracepoint_hit(struct scap_engine_handle engine, long* ret)
Expand Down
14 changes: 14 additions & 0 deletions userspace/libscap/engine/gvisor/gvisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct parse_result {
size_t size;
// pointers to each encoded event within the supplied output buffer
std::vector<scap_evt*> scap_events;
// number of events dropped by gVisor
uint32_t dropped_count;
};

struct procfs_result {
Expand Down Expand Up @@ -122,6 +124,7 @@ class sandbox_entry {
int32_t expand_buffer(size_t size);

scap_sized_buffer m_buf;
uint64_t m_last_dropped_count;
bool m_closing;
};

Expand All @@ -140,6 +143,7 @@ class engine {
uint32_t get_threadinfos(uint64_t *n, const scap_threadinfo **tinfos);
uint32_t get_fdinfos(const scap_threadinfo *tinfo, uint64_t *n, const scap_fdinfo **fdinfos);
uint32_t get_vxid(uint64_t pid);
int32_t get_stats(scap_stats *stats);
private:
int32_t process_message_from_fd(int fd);
void free_sandbox_buffers();
Expand Down Expand Up @@ -168,6 +172,16 @@ class engine {
std::string m_root_path;
std::string m_trace_session_path;

struct gvisor_stats
{
// total number of events received from gVisor
uint64_t n_evts;
// total number of drops due to parsig errors
uint64_t n_drops_parsing;
// total number of drops on gVisor side
uint64_t n_drops_gvisor;
} m_gvisor_stats;

};

} // namespace scap_gvisor
2 changes: 2 additions & 0 deletions userspace/libscap/engine/gvisor/parsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,8 @@ parse_result parse_gvisor_proto(scap_const_sized_buffer gvisor_buf, scap_sized_b
return ret;
}

// dropped count is the absolute number of events dropped from gVisor side
ret.dropped_count = hdr->dropped_count;
const char *proto = &buf[hdr->header_size];
ssize_t proto_size = gvisor_buf.size - hdr->header_size;

Expand Down
20 changes: 20 additions & 0 deletions userspace/libscap/engine/gvisor/scap_gvisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ sandbox_entry::sandbox_entry()
{
m_buf.buf = nullptr;
m_buf.size = 0;
m_last_dropped_count = 0;
m_closing = false;
}

Expand Down Expand Up @@ -83,6 +84,9 @@ int32_t sandbox_entry::expand_buffer(size_t size)
engine::engine(char *lasterr)
{
m_lasterr = lasterr;
m_gvisor_stats.n_evts = 0;
m_gvisor_stats.n_drops_parsing = 0;
m_gvisor_stats.n_drops_gvisor = 0;
}

engine::~engine()
Expand Down Expand Up @@ -411,6 +415,15 @@ uint32_t engine::get_vxid(uint64_t xid)
return parsers::get_vxid(xid);
}

int32_t engine::get_stats(scap_stats *stats)
{
stats->n_drops = m_gvisor_stats.n_drops_parsing + m_gvisor_stats.n_drops_gvisor;
stats->n_drops_bug = m_gvisor_stats.n_drops_parsing;
stats->n_drops_buffer = m_gvisor_stats.n_drops_gvisor;
stats->n_evts = m_gvisor_stats.n_evts;
return SCAP_SUCCESS;
}

// Reads one gvisor message from the specified fd, stores the resulting events overwriting m_buffers and adds pointers to m_event_queue.
// Returns:
// * SCAP_SUCCESS in case of success
Expand Down Expand Up @@ -461,6 +474,10 @@ int32_t engine::process_message_from_fd(int fd)
return SCAP_ILLEGAL_INPUT;
}

uint64_t delta = parse_result.dropped_count - m_sandbox_data[fd].m_last_dropped_count;
m_sandbox_data[fd].m_last_dropped_count = parse_result.dropped_count;
m_gvisor_stats.n_drops_gvisor += delta;

for(scap_evt *evt : parse_result.scap_events)
{
m_event_queue.push_back(evt);
Expand All @@ -479,6 +496,7 @@ int32_t engine::next(scap_evt **pevent, uint16_t *pcpuid)
{
*pevent = m_event_queue.front();
m_event_queue.pop_front();
m_gvisor_stats.n_evts++;
return SCAP_SUCCESS;
}

Expand Down Expand Up @@ -526,6 +544,7 @@ int32_t engine::next(scap_evt **pevent, uint16_t *pcpuid)

// ignore parsing errors, we will simply discard the message
if (status == SCAP_ILLEGAL_INPUT) {
m_gvisor_stats.n_drops_parsing++;
continue;
}
}
Expand All @@ -552,6 +571,7 @@ int32_t engine::next(scap_evt **pevent, uint16_t *pcpuid)
{
*pevent = m_event_queue.front();
m_event_queue.pop_front();
m_gvisor_stats.n_evts++;
return SCAP_SUCCESS;
}

Expand Down