Skip to content

Commit 3d6ea2a

Browse files
Harden hardware pool interfaces. (#251)
Signed-off-by: Samuel K. Gutierrez <[email protected]>
1 parent 4c3ef21 commit 3d6ea2a

File tree

4 files changed

+130
-65
lines changed

4 files changed

+130
-65
lines changed

src/qvi-bbuff-rmi.h

+2-25
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,7 @@ qvi_bbuff_rmi_pack_item(
508508
qvi_bbuff_t *buff,
509509
const qvi_hwpool_cpu_s &data
510510
) {
511-
// Pack hints.
512-
const int rc = qvi_bbuff_rmi_pack_item(buff, data.hints);
513-
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
514-
515-
return qvi_bbuff_rmi_pack_item(buff, data.cpuset);
511+
return data.packinto(buff);
516512
}
517513

518514
/**
@@ -825,26 +821,7 @@ qvi_bbuff_rmi_unpack_item(
825821
byte_t *buffpos,
826822
size_t *bytes_written
827823
) {
828-
size_t bw = 0, total_bw = 0;
829-
// Unpack hints.
830-
int rc = qvi_bbuff_rmi_unpack_item(
831-
&cpu.hints, buffpos, &bw
832-
);
833-
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
834-
total_bw += bw;
835-
buffpos += bw;
836-
// Unpack bitmap.
837-
rc = qvi_bbuff_rmi_unpack_item(
838-
cpu.cpuset, buffpos, &bw
839-
);
840-
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
841-
total_bw += bw;
842-
out:
843-
if (qvi_unlikely(rc != QV_SUCCESS)) {
844-
total_bw = 0;
845-
}
846-
*bytes_written = total_bw;
847-
return rc;
824+
return qvi_hwpool_cpu_s::unpack(buffpos, bytes_written, cpu);
848825
}
849826

850827
/**

src/qvi-hwpool.cc

+81-22
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,64 @@ pool_release_cpus_by_cpuset(
134134
}
135135
#endif
136136

137+
qvi_hwloc_bitmap_s &
138+
qvi_hwpool_cpu_s::cpuset(void)
139+
{
140+
return m_cpuset;
141+
}
142+
143+
const qvi_hwloc_bitmap_s &
144+
qvi_hwpool_cpu_s::cpuset(void)
145+
const {
146+
return m_cpuset;
147+
}
148+
149+
int
150+
qvi_hwpool_cpu_s::packinto(
151+
qvi_bbuff_t *buff
152+
) const {
153+
// Pack hints.
154+
const int rc = qvi_bbuff_rmi_pack_item(buff, m_hints);
155+
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
156+
// Pack cpuset.
157+
return qvi_bbuff_rmi_pack_item(buff, m_cpuset);
158+
}
159+
160+
int
161+
qvi_hwpool_cpu_s::unpack(
162+
byte_t *buffpos,
163+
size_t *bytes_written,
164+
qvi_hwpool_cpu_s &cpu
165+
) {
166+
size_t bw = 0, total_bw = 0;
167+
// Unpack hints.
168+
int rc = qvi_bbuff_rmi_unpack_item(
169+
&cpu.m_hints, buffpos, &bw
170+
);
171+
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
172+
total_bw += bw;
173+
buffpos += bw;
174+
// Unpack bitmap.
175+
rc = qvi_bbuff_rmi_unpack_item(
176+
cpu.m_cpuset, buffpos, &bw
177+
);
178+
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
179+
total_bw += bw;
180+
out:
181+
if (qvi_unlikely(rc != QV_SUCCESS)) {
182+
total_bw = 0;
183+
}
184+
*bytes_written = total_bw;
185+
return rc;
186+
}
187+
137188
qvi_hwpool_dev_s::qvi_hwpool_dev_s(
138189
const qvi_hwloc_device_s &dev
139-
) : type(dev.type)
140-
, affinity(dev.affinity)
190+
) : m_type(dev.type)
191+
, m_affinity(dev.affinity)
141192
, m_id(dev.id)
142-
, pci_bus_id(dev.pci_bus_id)
143-
, uuid(dev.uuid) { }
193+
, m_pci_bus_id(dev.pci_bus_id)
194+
, m_uuid(dev.uuid) { }
144195

145196
qvi_hwpool_dev_s::qvi_hwpool_dev_s(
146197
const std::shared_ptr<qvi_hwloc_device_s> &shdev
@@ -150,7 +201,7 @@ bool
150201
qvi_hwpool_dev_s::operator==(
151202
const qvi_hwpool_dev_s &x
152203
) const {
153-
return uuid == x.uuid;
204+
return m_uuid == x.m_uuid;
154205
}
155206

156207
int
@@ -161,10 +212,10 @@ qvi_hwpool_dev_s::id(
161212
int rc = QV_SUCCESS, nw = 0;
162213
switch (format) {
163214
case (QV_DEVICE_ID_UUID):
164-
nw = asprintf(result, "%s", uuid.c_str());
215+
nw = asprintf(result, "%s", m_uuid.c_str());
165216
break;
166217
case (QV_DEVICE_ID_PCI_BUS_ID):
167-
nw = asprintf(result, "%s", pci_bus_id.c_str());
218+
nw = asprintf(result, "%s", m_pci_bus_id.c_str());
168219
break;
169220
case (QV_DEVICE_ID_ORDINAL):
170221
nw = asprintf(result, "%d", m_id);
@@ -181,27 +232,33 @@ qvi_hwpool_dev_s::id(
181232
return rc;
182233
}
183234

235+
const qvi_hwloc_bitmap_s &
236+
qvi_hwpool_dev_s::affinity(void)
237+
const {
238+
return m_affinity;
239+
}
240+
184241
int
185242
qvi_hwpool_dev_s::packinto(
186243
qvi_bbuff_t *buff
187244
) const {
188245
// Pack device hints.
189-
int rc = qvi_bbuff_rmi_pack_item(buff, hints);
246+
int rc = qvi_bbuff_rmi_pack_item(buff, m_hints);
190247
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
191248
// Pack device affinity.
192-
rc = qvi_bbuff_rmi_pack_item(buff, affinity);
249+
rc = qvi_bbuff_rmi_pack_item(buff, m_affinity);
193250
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
194251
// Pack device type.
195-
rc = qvi_bbuff_rmi_pack_item(buff, type);
252+
rc = qvi_bbuff_rmi_pack_item(buff, m_type);
196253
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
197254
// Pack device ID.
198255
rc = qvi_bbuff_rmi_pack_item(buff, m_id);
199256
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
200257
// Pack device PCI bus ID.
201-
rc = qvi_bbuff_rmi_pack_item(buff, pci_bus_id);
258+
rc = qvi_bbuff_rmi_pack_item(buff, m_pci_bus_id);
202259
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
203260
// Pack device UUID.
204-
return qvi_bbuff_rmi_pack_item(buff, uuid);
261+
return qvi_bbuff_rmi_pack_item(buff, m_uuid);
205262
}
206263

207264
int
@@ -213,21 +270,21 @@ qvi_hwpool_dev_s::unpack(
213270
size_t bw = 0, total_bw = 0;
214271

215272
int rc = qvi_bbuff_rmi_unpack_item(
216-
&dev->hints, buffpos, &bw
273+
&dev->m_hints, buffpos, &bw
217274
);
218275
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
219276
total_bw += bw;
220277
buffpos += bw;
221278

222279
rc = qvi_bbuff_rmi_unpack_item(
223-
dev->affinity, buffpos, &bw
280+
dev->m_affinity, buffpos, &bw
224281
);
225282
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
226283
total_bw += bw;
227284
buffpos += bw;
228285

229286
rc = qvi_bbuff_rmi_unpack_item(
230-
&dev->type, buffpos, &bw
287+
&dev->m_type, buffpos, &bw
231288
);
232289
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
233290
total_bw += bw;
@@ -241,14 +298,14 @@ qvi_hwpool_dev_s::unpack(
241298
buffpos += bw;
242299

243300
rc = qvi_bbuff_rmi_unpack_item(
244-
dev->pci_bus_id, buffpos, &bw
301+
dev->m_pci_bus_id, buffpos, &bw
245302
);
246303
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
247304
total_bw += bw;
248305
buffpos += bw;
249306

250307
rc = qvi_bbuff_rmi_unpack_item(
251-
dev->uuid, buffpos, &bw
308+
dev->m_uuid, buffpos, &bw
252309
);
253310
if (qvi_unlikely(rc != QV_SUCCESS)) goto out;
254311
total_bw += bw;
@@ -269,7 +326,7 @@ qvi_hwpool_s::add_devices_with_affinity(
269326
for (const auto devt : qvi_hwloc_supported_devices()) {
270327
qvi_hwloc_dev_list_t devs;
271328
rc = qvi_hwloc_get_devices_in_bitmap(
272-
hwloc, devt, m_cpu.cpuset, devs
329+
hwloc, devt, m_cpu.cpuset(), devs
273330
);
274331
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
275332
for (const auto &dev : devs) {
@@ -304,7 +361,7 @@ qvi_hwpool_s::initialize(
304361
qvi_hwloc_t *hwloc,
305362
hwloc_const_bitmap_t cpuset
306363
) {
307-
const int rc = m_cpu.cpuset.set(cpuset);
364+
const int rc = m_cpu.cpuset().set(cpuset);
308365
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
309366
// Add devices with affinity to the hardware pool.
310367
return add_devices_with_affinity(hwloc);
@@ -313,7 +370,7 @@ qvi_hwpool_s::initialize(
313370
const qvi_hwloc_bitmap_s &
314371
qvi_hwpool_s::cpuset(void) const
315372
{
316-
return m_cpu.cpuset;
373+
return m_cpu.cpuset();
317374
}
318375

319376
const qvi_hwpool_devs_t &
@@ -330,7 +387,7 @@ qvi_hwpool_s::nobjects(
330387
) {
331388
if (qvi_hwloc_obj_type_is_host_resource(obj_type)) {
332389
return qvi_hwloc_get_nobjs_in_cpuset(
333-
hwloc, obj_type, m_cpu.cpuset.cdata(), result
390+
hwloc, obj_type, m_cpu.cpuset().cdata(), result
334391
);
335392
}
336393
*result = m_devs.count(obj_type);
@@ -342,7 +399,7 @@ qvi_hwpool_s::add_device(
342399
const qvi_hwpool_dev_s &dev
343400
) {
344401
auto shdev = std::make_shared<qvi_hwpool_dev_s>(dev);
345-
m_devs.insert({dev.type, shdev});
402+
m_devs.insert({dev.m_type, shdev});
346403
return QV_SUCCESS;
347404
}
348405

@@ -421,6 +478,7 @@ qvi_hwpool_s::unpack(
421478
return rc;
422479
}
423480

481+
#if 0
424482
/**
425483
* Extend namespace std so we can easily add qvi_devinfo_ts to
426484
* unordered_sets.
@@ -439,6 +497,7 @@ namespace std {
439497
}
440498
};
441499
}
500+
#endif
442501

443502
/*
444503
* vim: ft=cpp ts=4 sts=4 sw=4 expandtab

src/qvi-hwpool.h

+45-16
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,65 @@
2323
* Base hardware pool resource class.
2424
*/
2525
struct qvi_hwpool_res_s {
26+
protected:
2627
/** Resource hint flags. */
27-
qv_scope_create_hints_t hints = QV_SCOPE_CREATE_HINT_NONE;
28+
qv_scope_create_hints_t m_hints = QV_SCOPE_CREATE_HINT_NONE;
2829
};
2930

3031
/**
3132
* Defines a hardware pool CPU. A CPU here may have multiple
3233
* processing units (PUs), which are defined in the CPU's cpuset.
3334
*/
3435
struct qvi_hwpool_cpu_s : qvi_hwpool_res_s {
36+
protected:
3537
/** The cpuset of the CPU's PUs. */
36-
qvi_hwloc_bitmap_s cpuset;
38+
qvi_hwloc_bitmap_s m_cpuset;
39+
public:
40+
/**
41+
* Returns a reference to the
42+
* CPU's resources encoded by a bitmap.
43+
*/
44+
qvi_hwloc_bitmap_s &
45+
cpuset(void);
46+
/**
47+
* Returns a const reference to the
48+
* CPU's resources encoded by a bitmap.
49+
*/
50+
const qvi_hwloc_bitmap_s &
51+
cpuset(void) const;
52+
/** Packs the instance into the provided buffer. */
53+
int
54+
packinto(
55+
qvi_bbuff_t *buff
56+
) const;
57+
/** Unpacks the buffer and creates a new hardware pool instance. */
58+
static int
59+
unpack(
60+
byte_t *buffpos,
61+
size_t *bytes_written,
62+
qvi_hwpool_cpu_s &cpu
63+
);
3764
};
3865

3966
/**
4067
* Defines a hardware pool device. This differs from a qvi_hwloc_device_s
4168
* because we only maintain information relevant for user-facing operations.
4269
*/
4370
struct qvi_hwpool_dev_s : qvi_hwpool_res_s {
71+
/** Hardware pools are our friends. */
72+
friend qvi_hwpool_s;
73+
private:
4474
/** Device type. */
45-
qv_hw_obj_type_t type = QV_HW_OBJ_LAST;
75+
qv_hw_obj_type_t m_type = QV_HW_OBJ_LAST;
4676
/** The bitmap encoding CPU affinity. */
47-
qvi_hwloc_bitmap_s affinity;
77+
qvi_hwloc_bitmap_s m_affinity;
4878
/** Device ID (ordinal). */
4979
int m_id = QVI_HWLOC_DEVICE_INVALID_ID;
5080
/** The PCI bus ID. */
51-
std::string pci_bus_id;
81+
std::string m_pci_bus_id;
5282
/** Universally Unique Identifier. */
53-
std::string uuid;
83+
std::string m_uuid;
84+
public:
5485
/** Default constructor. */
5586
qvi_hwpool_dev_s(void) = default;
5687
/** Constructor using qvi_hwloc_device_s. */
@@ -75,15 +106,17 @@ struct qvi_hwpool_dev_s : qvi_hwpool_res_s {
75106
char **result
76107
);
77108
/**
78-
* Packs the instance into the provided buffer.
109+
* Returns a const reference to the
110+
* device's affinity encoded by a bitmap.
79111
*/
112+
const qvi_hwloc_bitmap_s &
113+
affinity(void) const;
114+
/** Packs the instance into the provided buffer. */
80115
int
81116
packinto(
82117
qvi_bbuff_t *buff
83118
) const;
84-
/**
85-
* Unpacks the buffer and creates a new hardware pool device instance.
86-
*/
119+
/** Unpacks the buffer and creates a new hardware pool device instance. */
87120
static int
88121
unpack(
89122
byte_t *buffpos,
@@ -164,16 +197,12 @@ struct qvi_hwpool_s {
164197
*/
165198
int
166199
release_devices(void);
167-
/**
168-
* Packs the instance into the provided buffer.
169-
*/
200+
/** Packs the instance into the provided buffer. */
170201
int
171202
packinto(
172203
qvi_bbuff_t *buff
173204
) const;
174-
/**
175-
* Unpacks the buffer and creates a new hardware pool instance.
176-
*/
205+
/** Unpacks the buffer and creates a new hardware pool instance. */
177206
static int
178207
unpack(
179208
byte_t *buffpos,

0 commit comments

Comments
 (0)