Skip to content

Improve state switches#1121

Merged
mstemm merged 2 commits intodevfrom
improve-state-switches
May 30, 2018
Merged

Improve state switches#1121
mstemm merged 2 commits intodevfrom
improve-state-switches

Conversation

@mstemm
Copy link
Contributor

@mstemm mstemm commented May 16, 2018

This is not ready to merge yet. It was just a way to test the idea of doing shallow copies of threadinfo structs.

@mstemm mstemm force-pushed the improve-state-switches branch 3 times, most recently from 1c5c41c to f8d7195 Compare May 16, 2018 23:04
vector<string> m_env; ///< Environment variables
string m_env_str; // m_env combined, separated by '\0', capped to SCAP_MAX_ENV_SIZE
vector<pair<string, string>> m_cgroups; ///< subsystem-cgroup pairs
string m_cgroups_str; // m_cgroups combined, separated by '\0', cappped to SCAP_MAX_CGROUPS_SIZE
Copy link
Contributor

@luca3m luca3m May 17, 2018

Choose a reason for hiding this comment

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

cgroups have already a pretty big memory impact, doubling it worries me. I would test memory usage difference with an high number of threads (40k) for example.

A possible alternative on my mind can be have just strings as storage and just at getter time, parse them and return the expected value to the caller.

@mstemm mstemm force-pushed the improve-state-switches branch 2 times, most recently from cf61245 to 6c76eec Compare May 23, 2018 06:58
@mstemm
Copy link
Contributor Author

mstemm commented May 23, 2018

Ok, this version is ready for review. I avoided storing a copy of the args/env/cgroups by using iovecs when writing the sinsp_threadinfo to the capture file.

@mstemm mstemm requested review from bertocci, gnosek and mattpag and removed request for mattpag May 23, 2018 07:00
@mstemm mstemm force-pushed the improve-state-switches branch 2 times, most recently from d7dbb41 to 0028be9 Compare May 23, 2018 21:02
size_t iov_len; /* Number of bytes to transfer */
};
#else
#include <sys/uio.h>

Choose a reason for hiding this comment

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

The function declaration only uses pointers, so let's avoid includes in scap.h. Forward declare this and include/define iovec in scap_savefile.c

std::string &rem)
{
(*iovcnt)++;
*iov = (struct iovec *) realloc(*iov, *iovcnt * sizeof(struct iovec));

Choose a reason for hiding this comment

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

We're going to realloc() a lot in edge cases like having a large number of single char args. Tcmalloc will effectively mask some calls but we should cut it to a single realloc() since this is perf sensitive. Maybe leave this as is for the cgroups part, but the env/args part needs a helper to do a single realloc().

struct iovec **iov, int *iovcnt,
std::string &rem);

void add_to_iovec(uint32_t &alen,

Choose a reason for hiding this comment

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

This is the meat of it but I found the calls of it hard to grok. It goes mutable arg, non-mutable args, and then more mutable args. Move alen after include_trailing_null, const include_trailing_null, and personally I like passing alen as a pointer so it's clear that it changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer references, but I did clean things up to be more consistent. I used pointers for methods that allocate and return iovec/iovcnt, and references everywhere else.

size_t env_len();
size_t cgroups_len();

void args_to_iovec(struct iovec **iov, int *iovcnt,

Choose a reason for hiding this comment

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

Add a comment that these functions make shallow copies and put a possibly truncated argument in rem

Also, pretty sure all the *_len() and *_to_iovec() functions and their private calls can be made const

@mstemm
Copy link
Contributor Author

mstemm commented May 30, 2018

Addressed the feedback. I'll squash down before I fully merge the PR, but can you take a look?

@mstemm mstemm force-pushed the improve-state-switches branch from 632798d to 99e8f93 Compare May 30, 2018 19:51
mstemm added 2 commits May 30, 2018 12:51
Add a new scap_write_proclist_entry_bufs which takes a scap_threadinfo
and a set of strings representing the various array-backed strings that
can be in a scap_threadinfo struct.

In the case of args/env/cgroups, which are represented within sinsp as
vectors of strings, allow writing as an iovec, so the vector elements
can be written individually without having to be combined first.

This is useful when dumping the set of (sinsp) threads, as you can dump
a thread without copying all of the strings into the scap_threadinfo
struct.
A big cost in creating a new capture file from a live event stream is
dumping the current set of threads to the capture file. This is done
in sinsp_thread_manager::dump_threads_to_file() and involves iterating
over the set of threads and converting each to a scap_threadinfo
struct, multiple times in fact.

To help make that more efficient, we can do a shallow copy of the
strings from the sinsp_threadinfo struct given that the corresponding
scap_threadinfo is temporary.

{args,env,cgroups}_to_scap are replaced by {args,env,cgroups}_to_iovec,
which assembles an iovec containing the strings in the various string
vectors, without actually copying anything in the common case. In the
case of truncation, a "remainder" string is used, which does get a
truncated copy of the last argument/env var/cgroup value that doesn't
fit.

Also, there's now no need to allocate and populate a scap_threadinfo
struct when you're computing the write size for a thread. You can get
all the sizes directly from the sinsp_threadinfo object.
@mstemm mstemm force-pushed the improve-state-switches branch from 99e8f93 to 605d128 Compare May 30, 2018 19:52
@mstemm mstemm merged commit 02f80b9 into dev May 30, 2018
@mstemm mstemm deleted the improve-state-switches branch May 30, 2018 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants