Skip to content

Rewrote remap_stats plugin to use C++ - #6006

Merged
SolidWallOfCode merged 1 commit into
apache:masterfrom
silvanocerza:remap-stats-cpp
Oct 23, 2019
Merged

SolidWallOfCode merged 1 commit into
apache:masterfrom
silvanocerza:remap-stats-cpp

Conversation

@silvanocerza

Copy link
Copy Markdown
Contributor

This should fix #5737

@maskit maskit added the Plugins label Oct 10, 2019
@maskit maskit added this to the 10.0.0 milestone Oct 10, 2019
@silvanocerza

Copy link
Copy Markdown
Contributor Author

I think am not running the whole testsuite locally, right now I'm doing it think like so:

./configure --enable-experimental-plugins --enable-example-plugins --enable-ccache --enable-luajit --enable-debug
make -j6
make check -j6

Is it wrong or am I missing something? Sorry but I can't find any documentation how to run the tests.

@maskit

maskit commented Oct 10, 2019

Copy link
Copy Markdown
Member

It seems like compile errors. Here's one of the errors.

experimental/remap_stats/remap_stats.cc:59:64: error: format '%s' expects argument of type 'char*', but argument 3 has type 'const string* {aka const std::basic_string<char>*}' [-Werror=format=]
         TSDebug(DEBUG_TAG, "Error creating stat_name: %s", name);
                                                                ^

Please run the configure script with --enable-werror.

@silvanocerza

Copy link
Copy Markdown
Contributor Author

Thanks, should be all right now. I had to create a VM with Ubuntu 19.04 after all since on Arch I get also deprecation warnings for <sys/sysctl.h>.

static std::string
create_stat_name(const std::string &h, const std::string &b)
{
std::string res(std::string("plugin.") + PLUGIN_NAME + '.' + h + '.' + b);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is more efficiently done with

std::string_view PREFIX{"plugin."};
static const size_t pn_len = strlen(PLUGIN_NAME);
std::string res;
res.reserve(PREFIX.size() + pn_len + 1+ h.size() + 1 + b.size() + 1);
res = PREFIX;
res += PLUGIN_NAME;
res += '.';
res += h;
res += '.';
res += b;
// Or this for the daring - 
// snprintf(res.data(), res.capacity(), "%s.%s.%s.%s", PREFIX.data(), PLUGIN_NAME, h.c_str(), b.c_str());
return std::move(res);
}

Yes, it's a lot more code, but it does at most one allocation. It's shorter if you abuse snprintf.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll go with the longer version. 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that in C++17, std::string::data() was changed to return char* instead of const char* precisely to allow this kind of use of snprintf and equivalents.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you prefer the version with snprintf I can go with that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

gcc9 may complain about the unnecessary call to std::move in the return as res is already local scope. Depends on the compiler debug levels I'm guessing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@traeak I've been doing that quite a bit lately, using gcc 9, and haven't had it complain. I think it's not until C++20 that the compiler is allowed to move returned local variables.

char *remap, *hostname;
char *unknown = "unknown";
char stat_name[MAX_STAT_LENGTH];
std::string remap, hostname;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These three should be std::string_view. Note the original code presumes them to be C strings.

@silvanocerza silvanocerza Oct 21, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, would it make sense for stat_name to be std::string_view too? Or just remap, hostname and unknown?

I guess it shouldn't since in stat_add I call TSStatFindName and that expects a const char *, just want to be sure. :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The primary reason to use string_view instead of string is to avoid unnecessary memory allocation. I would be tempted to keep the original char stat_name[MAX_STAT_LENGTH]; for that reason. Or if you want to use std::string, then reserve that amount of space to have only 1 allocation. You can pass a string_view to something that expects const char*, you just need to be careful about whether it's expected to be a C string. In this particular case, the original logic clearly assumed that.

You could also look at ts::LocalBufferWriter which was written for this situation. E.g.

ts::LocalBufferWriter<MAX_STAT_LENGTH> stat_w;
// ...
stat_w.reset().reduce(1); // clear any current data, reserve 1 byte for later.
stat_w.print("plugin.{}.{}.{}", PLUGIN_NAME, remap, "in_bytes");
stat_w.extend(1).write('\0'); // get back the byte and null terminate the string.

The reduce / extend calls make sure the name is a null terminated C string even if the
output is larger than MAX_STAT_LENGTH. LocalBufferWriter stack allocates its internal buffer and clips output to that size.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ts::LocalBufferWriter seems to be perfect for this, should I pass a const ts::LocalBufferWriter & to stat_add though? Or a const char * would be better? After all I'll always use it like a C string in that method. If I call stat_add with const ts::LocalBufferWriter & should also use it as a key for the internal hash?

I'm leaning toward calling stat_add with const char * but I want to know your opinion too.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should definitely not call it with ts::LocalBufferWriter. Generally you would use the view method to pass a string_view after writing to the buffer. In this case, since it's dealing with a predefined low level C interface, I would go ahead and pass in a const char*, via stat_w.data().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah, didn't see the key for a hash question. The Buffer Writer stuff is useful only for writing to buffers, it's not suitable for persistence. The normal use for that is to use string_view as the key and get use the view method of the Buffer Writer for it. In these cases you need to be careful of the buffer lifetime. If you need it to persist (so as to serve as a key, for instance), you should use ts::FixedBufferWriter which writes on an externally defined buffer. You could also use ts::bwprint which does the busy work of doing all this to a std::string. Then you can use that string as the key. E.g.

std::string stat_name;
ts::bwprint(stat_name, "plugin.{}.{}.{}", ...);

That takes care of resizing the string as needed and you get the null termination without explicit work. It does potentially require an allocation, but that's probably OK here since this is startup and not run time code.

I really need to improve the documentation on this, although there is some - https://docs.trafficserver.apache.org/en/latest/developer-guide/internal-libraries/buffer-writer.en.html

@traeak

traeak commented Oct 21, 2019

Copy link
Copy Markdown
Contributor

For the relatively small number of stats elements in the hash table I'm wondering if std::map<> might be faster. I know for small numbers of entries (thousands?) std::map<> is faster, especially with string based keys.

@silvanocerza

Copy link
Copy Markdown
Contributor Author

For the relatively small number of stats elements in the hash table I'm wondering if std::map<> might be faster. I know for small numbers of entries (thousands?) std::map<> is faster, especially with string based keys.

I don't know the number of entries in this case but I'd try to run a benchmark, or find someone that already tried and talks about it.

@traeak

traeak commented Oct 21, 2019

Copy link
Copy Markdown
Contributor

The std::map is only a suggestion based on curiousity. I don't think anyone would block the PR over it.

@SolidWallOfCode SolidWallOfCode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Really, I think this is good enough as a first pass to accept, I don't want to hang it up by requiring maximum optimization.

@SolidWallOfCode
SolidWallOfCode merged commit a42e61f into apache:master Oct 23, 2019
@zwoop

zwoop commented Nov 4, 2019

Copy link
Copy Markdown
Contributor

Cherry-picked to v9.0.x branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite remap_stats to use C++ / std::unordered_map

5 participants