Skip to content

Commit

Permalink
Improve qvi_scope_device_id().
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel K. Gutierrez <samuel@lanl.gov>
  • Loading branch information
samuelkgutierrez committed Jul 25, 2024
1 parent da522d0 commit 21ddd26
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/qvi-bbuff-rmi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 29 additions & 1 deletion src/qvi-hwpool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<int64_t>()(c);
Expand Down
10 changes: 8 additions & 2 deletions src/qvi-hwpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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<qvi_hwloc_device_s>. */
Expand All @@ -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
);
};

/**
Expand Down
33 changes: 6 additions & 27 deletions src/qvi-scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/qvi-scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

/**
Expand Down

0 comments on commit 21ddd26

Please sign in to comment.