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

Cache evt formatters #771

Merged
merged 2 commits into from
Feb 28, 2017
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
23 changes: 23 additions & 0 deletions userspace/libsinsp/eventformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,26 @@ bool sinsp_evt_formatter::tostring(sinsp_evt* evt, OUT string* res)
return false;
}
#endif // HAS_FILTERING

sinsp_evt_formatter_cache::sinsp_evt_formatter_cache(sinsp *inspector)
: m_inspector(inspector)
{
}

sinsp_evt_formatter_cache::~sinsp_evt_formatter_cache()
{
}

bool sinsp_evt_formatter_cache::tostring(sinsp_evt *evt, string &format, OUT string *res)
{
auto it = m_formatter_cache.lower_bound(format);

if(it == m_formatter_cache.end() ||
it->first != format)
{
it = m_formatter_cache.emplace_hint(it,
std::make_pair(format, make_shared<sinsp_evt_formatter>(m_inspector, format)));
}

return it->second->tostring(evt, res);
}
33 changes: 27 additions & 6 deletions userspace/libsinsp/eventformatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class SINSP_PUBLIC sinsp_evt_formatter
/*!
\brief Constructs a formatter.

\param inspector Pointer to the inspector instance that will generate the
\param inspector Pointer to the inspector instance that will generate the
events to be formatter.
\param fmt The printf-like format to use. The accepted format is the same
as the one of the sysdig '-p' command line flag, so refer to the sysdig
manual for details.
as the one of the sysdig '-p' command line flag, so refer to the sysdig
manual for details.
*/
sinsp_evt_formatter(sinsp* inspector, const string& fmt);

Expand All @@ -50,16 +50,16 @@ class SINSP_PUBLIC sinsp_evt_formatter
\brief Fills res with the string rendering of the event.

\param evt Pointer to the event to be converted into string.
\param res Pointer to the string that will be filled with the result.
\param res Pointer to the string that will be filled with the result.

\return true if the string should be shown (based on the initial *),
\return true if the string should be shown (based on the initial *),
false otherwise.
*/
bool tostring(sinsp_evt* evt, OUT string* res);

/*!
\brief Fills res with end of capture string rendering of the event.
\param res Pointer to the string that will be filled with the result.
\param res Pointer to the string that will be filled with the result.

\return true if there is a string to show (based on the format),
false otherwise.
Expand All @@ -80,4 +80,25 @@ class SINSP_PUBLIC sinsp_evt_formatter
Json::FastWriter m_writer;
};

/*!
\brief Caching version of sinsp_evt_formatter
This class is a wrapper around sinsp_evt_formatter, maintaining a
cache of previously seen formatters. It avoids the overhead of
recreating sinsp_evt_formatter objects for each event.
*/
class SINSP_PUBLIC sinsp_evt_formatter_cache
{
public:
sinsp_evt_formatter_cache(sinsp *inspector);
virtual ~sinsp_evt_formatter_cache();

// Fills in res with the event formatted according to
// format. Creates a new sinsp_evt_formatter object if
// necessary.
bool tostring(sinsp_evt *evt, std::string &format, OUT std::string *res);

private:
std::map<std::string,std::shared_ptr<sinsp_evt_formatter>> m_formatter_cache;
sinsp *m_inspector;
};
/*@}*/