Skip to content

Commit

Permalink
Harden some scope code.
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel K. Gutierrez <[email protected]>
  • Loading branch information
samuelkgutierrez committed Jul 24, 2024
1 parent d02ef1f commit d2967ad
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 81 deletions.
69 changes: 3 additions & 66 deletions src/qvi-bbuff-rmi.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
#define QVI_BBUFF_RMI_H

#include "qvi-common.h"
#include "qvi-bbuff.h"
#include "qvi-hwloc.h"
#include "qvi-hwpool.h"
#include "qvi-utils.h"

// 'Null' cpuset representation as a string.
static constexpr cstr_t QV_BUFF_RMI_NULL_CPUSET = "";
Expand Down Expand Up @@ -553,30 +553,7 @@ qvi_bbuff_rmi_pack_item_impl(
qvi_bbuff_t *buff,
const qvi_hwpool_s *data
) {
// Pack CPU.
int rc = qvi_bbuff_rmi_pack_item(buff, data->m_cpu);
if (rc != QV_SUCCESS) return rc;
// Pack ndevinfos
const size_t ndev = data->m_devs.size();
rc = qvi_bbuff_rmi_pack_item(buff, ndev);
if (rc != QV_SUCCESS) return rc;
// Pack devices.
for (const auto &dev : data->m_devs) {
rc = qvi_bbuff_rmi_pack_item(buff, dev.second.get());
if (rc != QV_SUCCESS) return rc;
}
return rc;
}

/**
* Packs const qvi_hwpool_s *
*/
inline int
qvi_bbuff_rmi_pack_item(
qvi_bbuff_t *buff,
const qvi_hwpool_s *data
) {
return qvi_bbuff_rmi_pack_item_impl(buff, data);
return data->pack(buff);
}

/**
Expand Down Expand Up @@ -953,47 +930,7 @@ qvi_bbuff_rmi_unpack_item(
byte_t *buffpos,
size_t *bytes_written
) {
qvi_hwpool_s *ihwp = nullptr;
int rc = qvi_new(&ihwp);
if (rc != QV_SUCCESS) return rc;

size_t bw = 0, total_bw = 0;

// Unpack CPU.
rc = qvi_bbuff_rmi_unpack_item(
ihwp->m_cpu, buffpos, &bw
);
if (rc != QV_SUCCESS) return rc;
total_bw += bw;
buffpos += bw;
// Unpack number of devices.
size_t ndev = 0;
rc = qvi_bbuff_rmi_unpack_item(
&ndev, buffpos, &bw
);
if (rc != QV_SUCCESS) goto out;
total_bw += bw;
buffpos += bw;
// Unpack devices.
for (size_t i = 0; i < ndev; ++i) {
qvi_hwpool_dev_s dev;
rc = qvi_bbuff_rmi_unpack_item(
&dev, buffpos, &bw
);
total_bw += bw;
buffpos += bw;
if (rc != QV_SUCCESS) break;

rc = ihwp->add_device(dev);
if (rc != QV_SUCCESS) break;
}
out:
if (rc != QV_SUCCESS) {
total_bw = 0;
}
*bytes_written = total_bw;
*hwp = ihwp;
return rc;
return qvi_hwpool_s::unpack(buffpos, bytes_written, hwp);
}

/**
Expand Down
66 changes: 59 additions & 7 deletions src/qvi-hwpool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
// approach using the device IDs instead of the bit positions.

#include "qvi-hwpool.h"
#include "qvi-bbuff.h"
#include "qvi-bbuff-rmi.h"
#include "qvi-utils.h"

Expand Down Expand Up @@ -187,13 +186,13 @@ qvi_hwpool_s::initialize(
}

const qvi_hwloc_bitmap_s &
qvi_hwpool_s::cpuset(void)
qvi_hwpool_s::cpuset(void) const
{
return m_cpu.cpuset;
}

const qvi_hwpool_devs_t &
qvi_hwpool_s::devices(void)
qvi_hwpool_s::devices(void) const
{
return m_devs;
}
Expand Down Expand Up @@ -232,16 +231,69 @@ qvi_hwpool_s::release_devices(void)
int
qvi_hwpool_s::pack(
qvi_bbuff_t *buff
) {
return qvi_bbuff_rmi_pack(buff, this);
) const {
// Pack the CPU.
int rc = qvi_bbuff_rmi_pack_item(buff, m_cpu);
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
// Pack the number of devices.
const size_t ndev = m_devs.size();
rc = qvi_bbuff_rmi_pack_item(buff, ndev);
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
// Pack the devices.
for (const auto &dev : m_devs) {
rc = qvi_bbuff_rmi_pack_item(buff, dev.second.get());
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
}
return rc;
}

int
qvi_hwpool_s::unpack(
qvi_bbuff_t *buff,
byte_t *buffpos,
size_t *bytes_written,
qvi_hwpool_s **hwp
) {
return qvi_bbuff_rmi_unpack(qvi_bbuff_data(buff), hwp);
size_t bw = 0, total_bw = 0;
// Create the new hardware pool.
qvi_hwpool_s *ihwp = nullptr;
int rc = qvi_new(&ihwp);
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
// Unpack the CPU into the hardare pool.
rc = qvi_bbuff_rmi_unpack_item(
ihwp->m_cpu, buffpos, &bw
);
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
total_bw += bw;
buffpos += bw;
// Unpack the number of devices.
size_t ndev;
rc = qvi_bbuff_rmi_unpack_item(
&ndev, buffpos, &bw
);
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
total_bw += bw;
buffpos += bw;
// Unpack and add the devices.
for (size_t i = 0; i < ndev; ++i) {
qvi_hwpool_dev_s dev;
rc = qvi_bbuff_rmi_unpack_item(
&dev, buffpos, &bw
);
if (qvi_unlikely(rc != QV_SUCCESS)) break;
total_bw += bw;
buffpos += bw;
//
rc = ihwp->add_device(dev);
if (qvi_unlikely(rc != QV_SUCCESS)) break;
}
out:
if (qvi_unlikely(rc != QV_SUCCESS)) {
total_bw = 0;
qvi_delete(&ihwp);
}
*bytes_written = total_bw;
*hwp = ihwp;
return rc;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/qvi-hwpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ using qvi_hwpool_devs_t = std::multimap<
>;

struct qvi_hwpool_s {
private:
/** The hardware pool's CPU. */
qvi_hwpool_cpu_s m_cpu;
/** The hardware pool's devices. */
qvi_hwpool_devs_t m_devs;
private:
/**
* Adds all devices with affinity to the
* provided, initialized hardware resource pool.
Expand Down Expand Up @@ -121,12 +121,12 @@ struct qvi_hwpool_s {
* Returns a const reference to the hardware pool's cpuset.
*/
const qvi_hwloc_bitmap_s &
cpuset(void);
cpuset(void) const;
/**
* Returns a const reference to the hardware pool's devices.
*/
const qvi_hwpool_devs_t &
devices(void);
devices(void) const;
/**
* Returns the number of objects in the hardware pool.
*/
Expand Down Expand Up @@ -154,13 +154,14 @@ struct qvi_hwpool_s {
int
pack(
qvi_bbuff_t *buff
);
) const;
/**
* Unpacks the buffer and creates a new hardware pool instance.
*/
static int
unpack(
qvi_bbuff_t *buff,
byte_t *buffpos,
size_t *bytes_written,
qvi_hwpool_s **hwp
);
};
Expand Down
7 changes: 4 additions & 3 deletions src/qvi-scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "qvi-bbuff.h"
#include "qvi-task.h"
#include "qvi-rmi.h"
#include "qvi-bbuff-rmi.h"
#include "qvi-hwpool.h"
#include "qvi-map.h"
#include "qvi-utils.h"
Expand Down Expand Up @@ -475,8 +476,8 @@ gather_hwpools(
rxpools.resize(group_size);
// Unpack the hwpools.
for (uint_t i = 0; i < group_size; ++i) {
rc = qvi_hwpool_s::unpack(
bbuffs[i], &rxpools[i]
rc = qvi_bbuff_rmi_unpack(
qvi_bbuff_data(bbuffs[i]), &rxpools[i]
);
if (rc != QV_SUCCESS) break;
}
Expand Down Expand Up @@ -572,7 +573,7 @@ scatter_hwpools(
rc = group->scatter(txbuffs.data(), root, &rxbuff);
if (rc != QV_SUCCESS) goto out;

rc = qvi_hwpool_s::unpack(rxbuff, pool);
rc = qvi_bbuff_rmi_unpack(qvi_bbuff_data(rxbuff), pool);
out:
for (auto &buff : txbuffs) {
qvi_bbuff_free(&buff);
Expand Down

0 comments on commit d2967ad

Please sign in to comment.