Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change use of map to unordered_map where possible #207

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions dynolog/src/KernelCollectorBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <chrono>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

DEFINE_bool(
Expand Down Expand Up @@ -110,7 +111,7 @@ void KernelCollectorBase::readCpuStats() {
void KernelCollectorBase::readNetworkStats() {
auto devices = pfs_.get_net().get_dev();

std::map<std::string, struct RxTx> rxtxNew_;
std::unordered_map<std::string, struct RxTx> rxtxNew_;

size_t nicDevCount = 0;
for (const auto& device : devices) {
Expand Down Expand Up @@ -168,7 +169,7 @@ bool KernelCollectorBase::isMonitoringInterfaceActive(std::string interface) {
}

void KernelCollectorBase::updateNetworkStatsDelta(
const std::map<std::string, struct RxTx>& rxtxNew) {
const std::unordered_map<std::string, struct RxTx>& rxtxNew) {
rxtxDelta_.clear();
for (const auto& [devName, devRxtxNew] : rxtxNew) {
if (rxtx_.find(devName) == rxtx_.end()) {
Expand Down
6 changes: 3 additions & 3 deletions dynolog/src/KernelCollectorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <time.h>
#include <array>
#include <map>
#include <unordered_map>
#include <vector>
#include "dynolog/src/Types.h"
#include "pfs/procfs.hpp"
Expand Down Expand Up @@ -47,10 +47,10 @@ class KernelCollectorBase {
std::vector<CpuTime> perCoreCpuTime_;

// Save more recent net device stats
std::map<std::string, struct RxTx> rxtx_, rxtxDelta_;
std::unordered_map<std::string, struct RxTx> rxtx_, rxtxDelta_;

void updateNetworkStatsDelta(
const std::map<std::string, struct RxTx>& rxtxNew);
const std::unordered_map<std::string, struct RxTx>& rxtxNew);
bool isMonitoringInterfaceActive(std::string interface);

// Should match googletest/include/gtest/gtest_prod.h
Expand Down
7 changes: 4 additions & 3 deletions dynolog/src/LibkinetoConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <set>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
#include "dynolog/src/LibkinetoTypes.h"

Expand Down Expand Up @@ -69,12 +70,12 @@ class LibkinetoConfigManager {

// Map of pid ancestry -> LibkinetoProcess
using ProcessMap = std::map<std::set<int32_t>, LibkinetoProcess>;
std::map<std::string, ProcessMap> jobs_;
std::unordered_map<std::string, ProcessMap> jobs_;

// Map of gpu id -> pids
using InstancesPerGpuMap = std::map<int32_t, std::set<int32_t>>;
using InstancesPerGpuMap = std::unordered_map<int32_t, std::set<int32_t>>;
// Job id -> InstancesPerGpu
std::map<std::string, InstancesPerGpuMap> jobInstancesPerGpu_;
std::unordered_map<std::string, InstancesPerGpuMap> jobInstancesPerGpu_;
mutable std::mutex mutex_;

void setOnDemandConfigForProcess(
Expand Down
4 changes: 2 additions & 2 deletions dynolog/src/Metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "dynolog/src/Metrics.h"

#include <fmt/format.h>
#include <map>
#include <unordered_map>

namespace dynolog {

Expand Down Expand Up @@ -34,7 +34,7 @@ const std::vector<MetricDesc> getAllMetrics() {
.type = MetricType::Instant,
.desc = "How long the system has been running in seconds."},
};
static std::map<std::string, std::string> cpustats = {
static std::unordered_map<std::string, std::string> cpustats = {
{"cpu_u_ms", "user"},
{"cpu_s_ms", "system"},
{"cpu_n_ms", "nice"},
Expand Down
6 changes: 4 additions & 2 deletions dynolog/src/PerfMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "hbt/src/mon/Monitor.h"
#include "hbt/src/perf_event/BuiltinMetrics.h"

#include <unordered_map>

namespace hbt = facebook::hbt;

namespace dynolog {
Expand Down Expand Up @@ -39,8 +41,8 @@ class PerfMonitor {
const hbt::CpuSet& monCpus_;
std::shared_ptr<hbt::perf_event::PmuDeviceManager> pmuDeviceManager_;
const MuxGroupId defaultMuxGroupId_;
std::map<ElemId, std::optional<TCountReader::ReadValues>> readValues_;
std::map<ElemId, std::shared_ptr<TCountReader>> countReaders_;
std::unordered_map<ElemId, std::optional<TCountReader::ReadValues>> readValues_;
std::unordered_map<ElemId, std::shared_ptr<TCountReader>> countReaders_;
};

// singleton object for default Metrics and PmuDeviceManager
Expand Down
4 changes: 2 additions & 2 deletions dynolog/src/metric_frame/MetricFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "dynolog/src/metric_frame/MetricSeries.h"

#include <cmath>
#include <map>
#include <memory>
#include <unordered_map>
#include <variant>
#include <vector>

Expand All @@ -38,7 +38,7 @@ class MetricFrameMap : public MetricFrameBase {
void show(std::ostream& s) const override;

protected:
std::map<std::string, MetricSeriesVar> series_;
std::unordered_map<std::string, MetricSeriesVar> series_;
};

using VectorSeriesDefList = std::vector<MetricSeriesVar>;
Expand Down
4 changes: 2 additions & 2 deletions dynolog/tests/KernelCollecterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ TEST(KernelCollecterTest, NetworkStatsTest) {
}

TEST(KernelCollecterTest, UpdateNetworkStatsDeltaTest) {
std::map<std::string, struct RxTx> oneDevice;
std::map<std::string, struct RxTx> twoDevices;
std::unordered_map<std::string, struct RxTx> oneDevice;
std::unordered_map<std::string, struct RxTx> twoDevices;

KernelCollectorBase kb{get_test_root()};

Expand Down
17 changes: 9 additions & 8 deletions hbt/src/mon/Monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <pfs/procfs.hpp>
#include <mutex>
#include <unordered_map>

#ifdef HBT_ENABLE_TRACING
#include "hbt/src/mon/TraceMonitor.h"
Expand Down Expand Up @@ -200,13 +201,13 @@ class Monitor {
/// Read counts for all events opened in sampling mode
/// in all TraceCollectors.
auto readSamplingCounts() const {
using TraceCollectorReadValues = std::map<
using TraceCollectorReadValues = std::unordered_map<
std::string,
std::optional<TraceCollector::TPerCpuCountSampleGenerator::ReadValues>>;

std::lock_guard<std::mutex> lock{mutex_};

std::map<ElemId, TraceCollectorReadValues> rvs;
std::unordered_map<ElemId, TraceCollectorReadValues> rvs;

for (auto& [k, tm] : trace_monitors_) {
HBT_THROW_ASSERT_IF(tm == nullptr);
Expand All @@ -220,10 +221,10 @@ class Monitor {

/// Read counts for all events opened in counting mode
/// in all PerCpuCountReaders.
std::map<ElemId, std::optional<TCountReader::ReadValues>> readAllCounts()
std::unordered_map<ElemId, std::optional<TCountReader::ReadValues>> readAllCounts()
const {
std::lock_guard<std::mutex> lock{mutex_};
std::map<ElemId, std::optional<TCountReader::ReadValues>> rvs;
std::unordered_map<ElemId, std::optional<TCountReader::ReadValues>> rvs;

for (auto& [k, cr] : count_readers_) {
HBT_THROW_ASSERT_IF(cr == nullptr);
Expand All @@ -234,10 +235,10 @@ class Monitor {

/// Read counts for all events opened in counting mode
/// in all PerCpuCountReaders.
std::map<ElemId, std::optional<std::vector<TCountReader::ReadValues>>>
std::unordered_map<ElemId, std::optional<std::vector<TCountReader::ReadValues>>>
readAllCountsPerCpu() const {
std::lock_guard<std::mutex> lock{mutex_};
std::map<ElemId, std::optional<std::vector<TCountReader::ReadValues>>> rvs;
std::unordered_map<ElemId, std::optional<std::vector<TCountReader::ReadValues>>> rvs;

for (auto& [k, cr] : count_readers_) {
HBT_THROW_ASSERT_IF(cr == nullptr);
Expand Down Expand Up @@ -343,10 +344,10 @@ class Monitor {
#ifdef HBT_ENABLE_BPERF
/// Read counts for all events opened in counting mode
/// in all BPerfCountReaders.
std::map<ElemId, std::optional<TBPerfCountReader::ReadValues>>
std::unordered_map<ElemId, std::optional<TBPerfCountReader::ReadValues>>
readAllBPerfCounts(bool skip_offset = false) const {
std::lock_guard<std::mutex> lock{mutex_};
std::map<ElemId, std::optional<TBPerfCountReader::ReadValues>> rvs;
std::unordered_map<ElemId, std::optional<TBPerfCountReader::ReadValues>> rvs;

for (auto& [k, cr] : bperf_count_readers_) {
HBT_THROW_ASSERT_IF(cr == nullptr);
Expand Down
Loading