diff --git a/userspace/libsinsp/eventformatter.cpp b/userspace/libsinsp/eventformatter.cpp index f535457ac6..8c19f4adb1 100644 --- a/userspace/libsinsp/eventformatter.cpp +++ b/userspace/libsinsp/eventformatter.cpp @@ -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(m_inspector, format))); + } + + return it->second->tostring(evt, res); +} diff --git a/userspace/libsinsp/eventformatter.h b/userspace/libsinsp/eventformatter.h index cc243a6831..ae21e71d7b 100644 --- a/userspace/libsinsp/eventformatter.h +++ b/userspace/libsinsp/eventformatter.h @@ -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); @@ -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. @@ -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> m_formatter_cache; + sinsp *m_inspector; +}; /*@}*/