Skip to content

Commit

Permalink
Not-so-pretty fix for logical numbering output. Needs improvement
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Mercier <[email protected]>
  • Loading branch information
GuillaumeMercier committed Feb 12, 2025
1 parent de1c932 commit a1f4dca
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/qvi-bbuff-rmi.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ qvi_bbuff_rmi_pack_item_impl(
}
// Non-null data.
char *datas = nullptr;
int rc = qvi_hwloc_bitmap_asprintf(data, &datas);
int rc = qvi_hwloc_bitmap_asprintf(NULL, data, &datas);
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;
// We are sending the string representation of the cpuset.
rc = buff->append(datas, strlen(datas) + 1);
Expand Down
87 changes: 80 additions & 7 deletions src/qvi-hwloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@ qvi_hwloc_obj_type_is_host_resource(

int
qvi_hwloc_bitmap_string(
hwloc_topology_t topo,
hwloc_const_bitmap_t bitmap,
qv_bind_string_format_t format,
char **result
) {
switch (format) {
case QV_BIND_STRING_AS_BITMAP:
return qvi_hwloc_bitmap_asprintf(bitmap, result);
return qvi_hwloc_bitmap_asprintf(topo, bitmap, result);
case QV_BIND_STRING_AS_LIST:
return qvi_hwloc_bitmap_list_asprintf(bitmap, result);
return qvi_hwloc_bitmap_list_asprintf(topo, bitmap, result);
default:
*result = nullptr;
return QV_ERR_INVLD_ARG;
Expand Down Expand Up @@ -816,11 +817,11 @@ qvi_hwloc_emit_cpubind(
if (rc != QV_SUCCESS) return rc;

char *cpusets = nullptr;
rc = qvi_hwloc_bitmap_asprintf(cpuset, &cpusets);
rc = qvi_hwloc_bitmap_asprintf(hwl->topo, cpuset, &cpusets);
if (rc != QV_SUCCESS) goto out;

qvi_log_info(
"[pid={} tid={}] cpubind={}",
"[pid={} tid={}] cpubind (physical)=[{}",
getpid(), task_id, cpusets
);
out:
Expand All @@ -831,6 +832,7 @@ qvi_hwloc_emit_cpubind(

int
qvi_hwloc_bitmap_asprintf(
hwloc_topology_t topo,
hwloc_const_cpuset_t cpuset,
char **result
) {
Expand All @@ -840,12 +842,48 @@ qvi_hwloc_bitmap_asprintf(
qvi_log_error("hwloc_bitmap_asprintf() failed");
return QV_ERR_OOR;
}

if (topo) {
hwloc_obj_t obj_pu = nullptr;

int num_pus = hwloc_get_nbobjs_inside_cpuset_by_type(topo, cpuset, HWLOC_OBJ_PU);
int num_pus_cp = num_pus;

int num_digits = 1;
while(num_pus_cp /= 10) num_digits++;

char head[] = "] | (logical)=[";
int str_size = strlen(head)+num_pus*(num_digits+1)+1;
char *str = (char *)calloc(str_size,sizeof(char));

strcpy(str,head);

char *core_str = (char *)calloc(num_digits+2,sizeof(char));
for(int idx = 0; idx < num_pus ; idx++){
obj_pu = hwloc_get_next_obj_inside_cpuset_by_type(topo, cpuset, HWLOC_OBJ_PU, obj_pu);
sprintf(core_str,"%i%s",obj_pu->logical_index, idx == (num_pus-1) ? "]" :",");
strncat(str,core_str,strlen(core_str));
memset(core_str,0,strlen(core_str));
}

char *newstr = (char *)calloc(strlen(str)+strlen(iresult)+1,sizeof(char));
strcpy(newstr,iresult);
strcat(newstr,str);

free(core_str);
free(str);
free(iresult);

iresult = newstr;
}

*result = iresult;
return QV_SUCCESS;
}

int
qvi_hwloc_bitmap_list_asprintf(
hwloc_topology_t topo,
hwloc_const_cpuset_t cpuset,
char **result
) {
Expand All @@ -855,6 +893,41 @@ qvi_hwloc_bitmap_list_asprintf(
qvi_log_error("hwloc_bitmap_list_asprintf() failed");
return QV_ERR_OOR;
}

if (topo) {
hwloc_obj_t obj_pu = nullptr;

int num_pus = hwloc_get_nbobjs_inside_cpuset_by_type(topo, cpuset, HWLOC_OBJ_PU);
int num_pus_cp = num_pus;

int num_digits = 1;
while(num_pus_cp /= 10) num_digits++;

char head[] = "] | (logical)=[";
int str_size = strlen(head)+num_pus*(num_digits+1)+1;
char *str = (char *)calloc(str_size,sizeof(char));

strcpy(str,head);

char *core_str = (char *)calloc(num_digits+2,sizeof(char));
for(int idx = 0; idx < num_pus ; idx++){
obj_pu = hwloc_get_next_obj_inside_cpuset_by_type(topo, cpuset, HWLOC_OBJ_PU, obj_pu);
sprintf(core_str,"%i%s",obj_pu->logical_index, idx == (num_pus-1) ? "]" :",");
strncat(str,core_str,strlen(core_str));
memset(core_str,0,strlen(core_str));
}

char *newstr = (char *)calloc(strlen(str)+strlen(iresult)+1,sizeof(char));
strcpy(newstr,iresult);
strcat(newstr,str);

free(core_str);
free(str);
free(iresult);

iresult = newstr;
}

*result = iresult;
return QV_SUCCESS;
}
Expand All @@ -869,7 +942,7 @@ qvi_hwloc_cpuset_debug(
#endif
assert(cpuset);
char *cpusets = nullptr;
int rc = qvi_hwloc_bitmap_asprintf(cpuset, &cpusets);
int rc = qvi_hwloc_bitmap_asprintf(nullptr, cpuset, &cpusets);
if (rc != QV_SUCCESS) {
qvi_abort();
}
Expand Down Expand Up @@ -957,7 +1030,7 @@ qvi_hwloc_task_get_cpubind_as_string(
int rc = qvi_hwloc_task_get_cpubind(hwl, task_id, &cpuset);
if (rc != QV_SUCCESS) return rc;

rc = qvi_hwloc_bitmap_asprintf(cpuset, cpusets);
rc = qvi_hwloc_bitmap_asprintf(hwl->topo, cpuset, cpusets);
qvi_hwloc_bitmap_delete(&cpuset);
return rc;
}
Expand Down Expand Up @@ -1185,7 +1258,7 @@ qvi_hwloc_devices_emit(
}
for (auto &dev : *devlist) {
char *cpusets = nullptr;
int rc = qvi_hwloc_bitmap_asprintf(dev->affinity.cdata(), &cpusets);
int rc = qvi_hwloc_bitmap_asprintf(hwl->topo, dev->affinity.cdata(), &cpusets);
if (rc != QV_SUCCESS) return rc;

qvi_log_info(" Device Name: {}", dev->name);
Expand Down
3 changes: 3 additions & 0 deletions src/qvi-hwloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ qvi_hwloc_emit_cpubind(
*/
int
qvi_hwloc_bitmap_asprintf(
hwloc_topology_t topo,
hwloc_const_cpuset_t cpuset,
char **result
);
Expand All @@ -196,6 +197,7 @@ qvi_hwloc_bitmap_asprintf(
*/
int
qvi_hwloc_bitmap_list_asprintf(
hwloc_topology_t topo,
hwloc_const_cpuset_t cpuset,
char **result
);
Expand Down Expand Up @@ -537,6 +539,7 @@ qvi_hwloc_get_devices_in_bitmap(

int
qvi_hwloc_bitmap_string(
hwloc_topology_t topo,
hwloc_const_bitmap_t bitmap,
qv_bind_string_format_t format,
char **result
Expand Down
4 changes: 3 additions & 1 deletion src/qvi-scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ qv_scope::bind_string(
int rc = m_group->task()->bind_top(&bitmap);
if (qvi_unlikely(rc != QV_SUCCESS)) return rc;

rc = qvi_hwloc_bitmap_string(bitmap, format, result);
hwloc_topology_t topo = qvi_hwloc_get_topo_obj(m_group->hwloc());

rc = qvi_hwloc_bitmap_string(topo, bitmap, format, result);
qvi_hwloc_bitmap_delete(&bitmap);
return rc;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common-test-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ctu_emit_task_bind(
ers = "qv_bind_string() failed";
ctu_panic("%s (rc=%s)", ers, qv_strerr(rc));
}
printf("[%d] cpubind=%s\n", pid, binds);
printf("[%d] cpubind (physical)=[%s\n", pid, binds);
free(binds);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/internal/test-hwloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ main(void)
ctu_panic("%s (rc=%s)", ers, qv_strerr(rc));
}

rc = qvi_hwloc_bitmap_asprintf(bitmap, &binds);
rc = qvi_hwloc_bitmap_asprintf(NULL, bitmap, &binds);
if (rc != QV_SUCCESS) {
ers = "qvi_hwloc_bitmap_asprintf() failed";
ctu_panic("%s (rc=%s)", ers, qv_strerr(rc));
Expand Down
2 changes: 1 addition & 1 deletion tests/internal/test-rmi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ client(
}

char *res;
qvi_hwloc_bitmap_asprintf(bitmap, &res);
qvi_hwloc_bitmap_asprintf(NULL, bitmap, &res);
printf("# [%d] cpubind = %s\n", who, res);
hwloc_bitmap_free(bitmap);
free(res);
Expand Down

0 comments on commit a1f4dca

Please sign in to comment.