From 21ddd26872cb1bd69317b14314673a54b9578932 Mon Sep 17 00:00:00 2001 From: "Samuel K. Gutierrez" Date: Thu, 25 Jul 2024 14:53:45 -0600 Subject: [PATCH] Improve qvi_scope_device_id(). Signed-off-by: Samuel K. Gutierrez --- src/qvi-bbuff-rmi.h | 4 ++-- src/qvi-hwpool.cc | 30 +++++++++++++++++++++++++++++- src/qvi-hwpool.h | 10 ++++++++-- src/qvi-scope.cc | 33 ++++++--------------------------- src/qvi-scope.h | 4 ++-- 5 files changed, 47 insertions(+), 34 deletions(-) diff --git a/src/qvi-bbuff-rmi.h b/src/qvi-bbuff-rmi.h index 86fee2f..e5f9fcb 100644 --- a/src/qvi-bbuff-rmi.h +++ b/src/qvi-bbuff-rmi.h @@ -534,7 +534,7 @@ qvi_bbuff_rmi_pack_item( rc = qvi_bbuff_rmi_pack_item(buff, data->type); if (rc != QV_SUCCESS) return rc; // Pack device ID. - rc = qvi_bbuff_rmi_pack_item(buff, data->id); + rc = qvi_bbuff_rmi_pack_item(buff, data->m_id); if (rc != QV_SUCCESS) return rc; // Pack device PCI bus ID. rc = qvi_bbuff_rmi_pack_item(buff, data->pci_bus_id); @@ -895,7 +895,7 @@ qvi_bbuff_rmi_unpack_item( buffpos += bw; rc = qvi_bbuff_rmi_unpack_item( - &dev->id, buffpos, &bw + &dev->m_id, buffpos, &bw ); if (rc != QV_SUCCESS) goto out; total_bw += bw; diff --git a/src/qvi-hwpool.cc b/src/qvi-hwpool.cc index 70a959d..1a75a46 100644 --- a/src/qvi-hwpool.cc +++ b/src/qvi-hwpool.cc @@ -134,6 +134,34 @@ pool_release_cpus_by_cpuset( } #endif +int +qvi_hwpool_dev_s::id( + qv_device_id_type_t format, + char **result +) { + int rc = QV_SUCCESS, nw = 0; + switch (format) { + case (QV_DEVICE_ID_UUID): + nw = asprintf(result, "%s", uuid.c_str()); + break; + case (QV_DEVICE_ID_PCI_BUS_ID): + nw = asprintf(result, "%s", pci_bus_id.c_str()); + break; + case (QV_DEVICE_ID_ORDINAL): + nw = asprintf(result, "%d", m_id); + break; + default: + rc = QV_ERR_INVLD_ARG; + break; + } + if (qvi_unlikely(nw == -1)) rc = QV_ERR_OOR; + + if (qvi_unlikely(rc != QV_SUCCESS)) { + *result = nullptr; + } + return rc; +} + int qvi_hwpool_s::add_devices_with_affinity( qvi_hwloc_t *hwloc @@ -306,7 +334,7 @@ namespace std { size_t operator()(const qvi_hwpool_dev_s &x) const { - const int a = x.id; + const int a = x.m_id; const int b = (int)x.type; const int64_t c = qvi_cantor_pairing(a, b); return hash()(c); diff --git a/src/qvi-hwpool.h b/src/qvi-hwpool.h index 3460719..dd83e77 100644 --- a/src/qvi-hwpool.h +++ b/src/qvi-hwpool.h @@ -46,7 +46,7 @@ struct qvi_hwpool_dev_s : qvi_hwpool_res_s { /** The bitmap encoding CPU affinity. */ qvi_hwloc_bitmap_s affinity; /** Device ID (ordinal). */ - int id = QVI_HWLOC_DEVICE_INVALID_ID; + int m_id = QVI_HWLOC_DEVICE_INVALID_ID; /** The PCI bus ID. */ std::string pci_bus_id; /** Universally Unique Identifier. */ @@ -58,7 +58,7 @@ struct qvi_hwpool_dev_s : qvi_hwpool_res_s { const qvi_hwloc_device_s &dev ) : type(dev.type) , affinity(dev.affinity) - , id(dev.id) + , m_id(dev.id) , pci_bus_id(dev.pci_bus_id) , uuid(dev.uuid) { } /** Constructor using std::shared_ptr. */ @@ -74,6 +74,12 @@ struct qvi_hwpool_dev_s : qvi_hwpool_res_s { ) const { return uuid == x.uuid; } + /** Returns the device's ID string formatted as specified. */ + int + id( + qv_device_id_type_t format, + char **result + ); }; /** diff --git a/src/qvi-scope.cc b/src/qvi-scope.cc index 7aa2f39..97c5cf2 100644 --- a/src/qvi-scope.cc +++ b/src/qvi-scope.cc @@ -201,11 +201,12 @@ qvi_scope_device_id( qv_scope_t *scope, qv_hw_obj_type_t dev_type, int dev_index, - qv_device_id_type_t id_type, - char **dev_id + qv_device_id_type_t format, + char **result ) { - int rc = QV_SUCCESS, id = 0, nw = 0; + *result = nullptr; // Look for the requested device. + int id = 0; qvi_hwpool_dev_s *finfo = nullptr; for (const auto &dinfo : scope->hwpool->devices()) { if (dev_type != dinfo.first) continue; @@ -214,31 +215,9 @@ qvi_scope_device_id( break; } } - if (qvi_unlikely(!finfo)) { - rc = QV_ERR_NOT_FOUND; - goto out; - } + if (qvi_unlikely(!finfo)) return QV_ERR_NOT_FOUND; // Format the device ID based on the caller's request. - switch (id_type) { - case (QV_DEVICE_ID_UUID): - nw = asprintf(dev_id, "%s", finfo->uuid.c_str()); - break; - case (QV_DEVICE_ID_PCI_BUS_ID): - nw = asprintf(dev_id, "%s", finfo->pci_bus_id.c_str()); - break; - case (QV_DEVICE_ID_ORDINAL): - nw = asprintf(dev_id, "%d", finfo->id); - break; - default: - rc = QV_ERR_INVLD_ARG; - break; - } - if (qvi_unlikely(nw == -1)) rc = QV_ERR_OOR; -out: - if (qvi_unlikely(rc != QV_SUCCESS)) { - *dev_id = nullptr; - } - return rc; + return finfo->id(format, result); } int diff --git a/src/qvi-scope.h b/src/qvi-scope.h index cedc4bf..1accd91 100644 --- a/src/qvi-scope.h +++ b/src/qvi-scope.h @@ -105,8 +105,8 @@ qvi_scope_device_id( qv_scope_t *scope, qv_hw_obj_type_t dev_type, int dev_index, - qv_device_id_type_t id_type, - char **dev_id + qv_device_id_type_t format, + char **result ); /**