Skip to content

Commit

Permalink
Add utility for creating vector access histograms
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarex committed May 31, 2017
1 parent 1ab0d7b commit 9f5515a
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
94 changes: 94 additions & 0 deletions include/util/timed_histogram.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#ifndef OSRM_UTIL_TIMED_HISTOGRAM_HPP
#define OSRM_UTIL_TIMED_HISTOGRAM_HPP

#include "util/integer_range.hpp"

#include <algorithm>
#include <atomic>
#include <mutex>
#include <sstream>
#include <vector>

namespace osrm
{
namespace util
{
namespace detail
{
extern std::atomic_uint operation;
}

/**
* Captures a histogram with a bin size of `IndexBinSize` every `TimeBinSize` count operations.
*/
template <std::size_t TimeBinSize = 1000, std::size_t IndexBinSize = 1000> class TimedHistogram
{
public:
void Count(std::size_t pos)
{
std::lock_guard<std::mutex> guard(frames_lock);
auto frame_index = detail::operation++ / TimeBinSize;

while (frame_offsets.size() <= frame_index)
{
frame_offsets.push_back(frame_counters.size());
}
BOOST_ASSERT(frame_offsets.size() == frame_index+1);

auto frame_offset = frame_offsets.back();
auto counter_index = frame_offset + pos / IndexBinSize;

while (counter_index >= frame_counters.size())
{
frame_counters.push_back(0);
}

BOOST_ASSERT(frame_counters.size() > counter_index);
frame_counters[counter_index]++;
}

// Returns the measurments as a CSV file with the columns:
// frame_id,index_bin,count
std::string DumpCSV() const
{
std::stringstream out;

const auto print_bins = [&out](auto frame_index, auto begin, auto end) {
auto bin_index = 0;
std::for_each(begin, end, [&](const auto count) {
if (count > 0)
{
out << (frame_index * TimeBinSize) << "," << (bin_index * IndexBinSize) << ","
<< count << std::endl;
}
bin_index++;
});
};

if (frame_offsets.size() == 0)
{
return "";
}

for (const auto frame_index : irange<std::size_t>(0, frame_offsets.size() - 1))
{
auto begin = frame_counters.begin() + frame_offsets[frame_index];
auto end = frame_counters.begin() + frame_offsets[frame_index + 1];
print_bins(frame_index, begin, end);
}
print_bins(frame_offsets.size() - 1,
frame_counters.begin() + frame_offsets.back(),
frame_counters.end());

return out.str();
}

private:
std::mutex frames_lock;
std::vector<std::uint32_t> frame_offsets;
std::vector<std::uint32_t> frame_counters;
};
}
}

#endif
12 changes: 12 additions & 0 deletions src/util/timed_historgram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "util/timed_histogram.hpp"

namespace osrm
{
namespace util
{
namespace detail
{
std::atomic_uint operation = {0};
}
}
}

0 comments on commit 9f5515a

Please sign in to comment.