trevdatastreams's picture
Add bounded Avro C++ collection-count allocation PoC
b99d599 verified
Raw
History Blame Contribute Delete
1.48 kB
#include <avro/DataFile.hh>
#include <avro/Generic.hh>
#include <cstdint>
#include <exception>
#include <iostream>
#include <string>
#include <sys/resource.h>
namespace {
std::uint64_t maximumResidentBytes() {
rusage usage{};
getrusage(RUSAGE_SELF, &usage);
#if defined(__APPLE__)
return static_cast<std::uint64_t>(usage.ru_maxrss);
#else
return static_cast<std::uint64_t>(usage.ru_maxrss) * 1024;
#endif
}
} // namespace
int main(int argc, char** argv) {
if (argc != 2) {
std::cerr << "usage: " << argv[0] << " FILE.avro\n";
return 2;
}
try {
avro::DataFileReader<avro::GenericDatum> reader(argv[1]);
avro::GenericDatum datum(reader.readerSchema());
const bool decoded = reader.read(datum);
std::cout << "decoded=" << decoded
<< " max_rss_bytes=" << maximumResidentBytes() << "\n";
return 0;
} catch (const std::bad_alloc& error) {
std::cerr << "BAD_ALLOC " << error.what()
<< " max_rss_bytes=" << maximumResidentBytes() << "\n";
return 42;
} catch (const std::length_error& error) {
std::cerr << "LENGTH_ERROR " << error.what()
<< " max_rss_bytes=" << maximumResidentBytes() << "\n";
return 43;
} catch (const std::exception& error) {
std::cerr << "EXCEPTION " << error.what()
<< " max_rss_bytes=" << maximumResidentBytes() << "\n";
return 44;
}
}