Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 24 additions & 5 deletions source/common/stats/stats_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,21 @@ bool TagExtractorImpl::extractTag(const std::string& stat_name, std::vector<Tag>
}

RawStatData* HeapRawStatDataAllocator::alloc(const std::string& name) {
// This must be zero-initialized
RawStatData* data = static_cast<RawStatData*>(::calloc(RawStatData::size(), 1));
data->initialize(name);
absl::string_view key = name;

if (key.size() > Stats::RawStatData::maxNameLength()) {
key.remove_suffix(key.size() - Stats::RawStatData::maxNameLength());
}

RawStatData* data = stats_set_[std::string(key)];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You're copying this pointer, not grabbing a reference to it (yes, a reference to a pointer haha), so anything you do to it after this is only affecting the local variable data and not the pointer you copied from in the map.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is the syntax for a ref to a pointer: RawStatData*& data.


if (stats_set_.count(std::string(key)) == 1) { // TODO do better

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think this if statement is backwards.

// didn't exist before now, actually create

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: make this a full sentence comment or remove.

data = static_cast<RawStatData*>(::calloc(RawStatData::size(), 1));
data->initialize(name);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Doesn't this need to be truncated?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I think it expects the full name, not the truncated key. You can see initialize() here wants to warn users if the stats are too long.

} else {
++data->ref_count_;
}
return data;
}

Expand Down Expand Up @@ -256,8 +268,15 @@ TagProducerImpl::addDefaultExtractors(const envoy::config::metrics::v2::StatsCon
}

void HeapRawStatDataAllocator::free(RawStatData& data) {
// This allocator does not ever have concurrent access to the raw data.
ASSERT(data.ref_count_ == 1);
ASSERT(data.ref_count_ > 0);
if (--data.ref_count_ > 0) {
return;
}

std::cout << data.key() << "\n";
size_t key_removed = stats_set_.erase(std::string(data.key()));
std::cout << key_removed << "\n";
ASSERT(key_removed >= 1);
::free(&data);
}

Expand Down
3 changes: 3 additions & 0 deletions source/common/stats/stats_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,9 @@ class HeapRawStatDataAllocator : public RawStatDataAllocator {
// RawStatDataAllocator
RawStatData* alloc(const std::string& name) override;
void free(RawStatData& data) override;

private:
std::unordered_map<std::string, RawStatData*> stats_set_;
};

/**
Expand Down