sample_container.cpp

Go to the documentation of this file.
00001 
00012 #include <climits>
00013 #include <set>
00014 #include <numeric>
00015 #include <algorithm>
00016 #include <vector>
00017 
00018 #include "sample_container.h"
00019 
00020 using namespace std;
00021 
00022 namespace {
00023 
00024 // FIXME: efficiency ?
00025 count_array_t add_counts(count_array_t const & counts,
00026                sample_entry const * s)
00027 {
00028     count_array_t temp(counts);
00029     temp += s->counts;
00030     return temp;
00031 }
00032 
00033 } // namespace anon
00034 
00035 
00036 sample_container::samples_iterator sample_container::begin() const
00037 {
00038     return samples.begin();
00039 }
00040 
00041 
00042 sample_container::samples_iterator sample_container::end() const
00043 {
00044     return samples.end();
00045 }
00046 
00047 
00048 sample_container::samples_iterator
00049 sample_container::begin(symbol_entry const * symbol) const
00050 {
00051     samples_storage::key_type key(symbol, 0);
00052 
00053     return samples.lower_bound(key);
00054 }
00055 
00056 
00057 sample_container::samples_iterator 
00058 sample_container::end(symbol_entry const * symbol) const
00059 {
00060     samples_storage::key_type key(symbol, ~bfd_vma(0));
00061 
00062     return samples.upper_bound(key);
00063 }
00064 
00065 
00066 void sample_container::insert(symbol_entry const * symbol,
00067                               sample_entry const & sample)
00068 {
00069     samples_storage::key_type key(symbol, sample.vma);
00070 
00071     samples_storage::iterator it = samples.find(key);
00072     if (it != samples.end()) {
00073         it->second.counts += sample.counts;
00074     } else {
00075         samples[key] = sample;
00076     }
00077 }
00078 
00079 
00080 count_array_t
00081 sample_container::accumulate_samples(debug_name_id filename_id) const
00082 {
00083     build_by_loc();
00084 
00085     sample_entry lower, upper;
00086 
00087     lower.file_loc.filename = upper.file_loc.filename = filename_id;
00088     lower.file_loc.linenr = 0;
00089     upper.file_loc.linenr = INT_MAX;
00090 
00091     typedef samples_by_loc_t::const_iterator iterator;
00092 
00093     iterator it1 = samples_by_loc.lower_bound(&lower);
00094     iterator it2 = samples_by_loc.upper_bound(&upper);
00095 
00096     return accumulate(it1, it2, count_array_t(), add_counts);
00097 }
00098 
00099 
00100 sample_entry const *
00101 sample_container::find_by_vma(symbol_entry const * symbol, bfd_vma vma) const
00102 {
00103     sample_index_t key(symbol, vma);
00104     samples_iterator it = samples.find(key);
00105     if (it != samples.end())
00106         return &it->second;
00107 
00108     return 0;
00109 }
00110 
00111 
00112 count_array_t
00113 sample_container::accumulate_samples(debug_name_id filename,
00114                                      size_t linenr) const
00115 {
00116     build_by_loc();
00117 
00118     sample_entry sample;
00119 
00120     sample.file_loc.filename = filename;
00121     sample.file_loc.linenr = linenr;
00122 
00123     typedef pair<samples_by_loc_t::const_iterator,
00124         samples_by_loc_t::const_iterator> it_pair;
00125 
00126     it_pair itp = samples_by_loc.equal_range(&sample);
00127 
00128     return accumulate(itp.first, itp.second, count_array_t(), add_counts);
00129 }
00130 
00131 
00132 void sample_container::build_by_loc() const
00133 {
00134     if (!samples_by_loc.empty())
00135         return;
00136 
00137     samples_iterator cit = samples.begin();
00138     samples_iterator end = samples.end();
00139     for (; cit != end; ++cit)
00140         samples_by_loc.insert(&cit->second);
00141 }

Generated on 8 Nov 2012 for Oprofile by  doxygen 1.6.1