perf_counter_ptr perf_counters::get_counter(const char* app, const char *section, const char *name, dsn_perf_counter_type_t flags, const char *dsptr, bool create_if_not_exist /*= false*/) { std::string full_name; perf_counter::build_full_name(app, section, name, full_name); if (create_if_not_exist) { utils::auto_write_lock l(_lock); auto it = _counters.find(full_name); if (it == _counters.end()) { dassert(_quick_counters_empty_slots.size() > 0, "no more slots for perf counters, please increase [core] perf_counter_max_count" ); uint64_t idx = _quick_counters_empty_slots.front(); _quick_counters_empty_slots.pop(); // increase version idx++; perf_counter_ptr counter = _factory(app, section, name, flags, dsptr); counter->_index = idx; // index << 32 | version _quick_counters[idx >> 32] = counter.get(); _counters.emplace(std::piecewise_construct, std::forward_as_tuple(full_name), std::forward_as_tuple(counter)); return counter; } else {
perf_counter_ptr perf_counters::get_counter(const char *section, const char *name, perf_counter_type flags, bool create_if_not_exist /*= false*/) { char section_name[512] = ""; //::GetModuleBaseNameA(::GetCurrentProcess(), ::GetModuleHandleA(nullptr), section_name, 256); //strcat(section_name, "."); strcat(section_name, section); if (create_if_not_exist) { auto_write_lock l(_lock); auto it = _counters.find(section_name); if (it == _counters.end()) { same_section_counters sc; it = _counters.insert(all_counters::value_type(section_name, sc)).first; } auto it2 = it->second.find(name); if (it2 == it->second.end()) { perf_counter_ptr counter(_factory(section_name, name, flags)); it->second.insert(same_section_counters::value_type(name, std::make_pair(counter, flags))); return counter; } else { dassert (it2->second.second == flags, "counters with the same name %s.%s with differnt types", section_name, name); return it2->second.first; } } else { auto_read_lock l(_lock); auto it = _counters.find(section_name); if (it == _counters.end()) return nullptr; auto it2 = it->second.find(name); if (it2 == it->second.end()) return nullptr; return it2->second.first; } }
T& get() { return *(_value? _value : _value = _factory()); }
T const& get() const { return *(_value? _value : _value = _factory()); }