Skip to content

Commit e7f5962

Browse files
committed
hwloc/linux.h: replace hwloc_parse_cpumap_file() with hwloc_linux_read_path_as_cpumask()
Take a filename instead of FILE (and internally use open instead of fopen). Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
1 parent 32e32fa commit e7f5962

File tree

11 files changed

+32
-126
lines changed

11 files changed

+32
-126
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ Version 2.0.0
9393
+ hwloc_obj_cpuset_snprintf() is deprecated in favor of hwloc_bitmap_snprintf().
9494
+ Functions diff_load_xml*(), diff_export_xml*() and diff_destroy() in
9595
hwloc/diff.h do not need a topology as first parameter anymore.
96+
+ hwloc_parse_cpumap_file () superseded by hwloc_linux_read_path_as_cpumask()
97+
in hwloc/linux.h.
9698
* Tools
9799
- lstopo and hwloc-info have a new --filter option matching the new filtering API.
98100
- hwloc-distances was removed and replaced with lstopo --distances.

doc/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,10 +695,10 @@ man3_glibc_sched_DATA = \
695695
man3_linuxdir = $(man3dir)
696696
man3_linux_DATA = \
697697
$(DOX_MAN_DIR)/man3/hwlocality_linux.3 \
698-
$(DOX_MAN_DIR)/man3/hwloc_linux_parse_cpumap_file.3 \
699698
$(DOX_MAN_DIR)/man3/hwloc_linux_set_tid_cpubind.3 \
700699
$(DOX_MAN_DIR)/man3/hwloc_linux_get_tid_cpubind.3 \
701-
$(DOX_MAN_DIR)/man3/hwloc_linux_get_tid_last_cpu_location.3
700+
$(DOX_MAN_DIR)/man3/hwloc_linux_get_tid_last_cpu_location.3 \
701+
$(DOX_MAN_DIR)/man3/hwloc_linux_read_path_as_cpumask.3
702702

703703
man3_linux_libnumadir = $(man3dir)
704704
man3_linux_libnuma_DATA = \

hwloc/topology-linux.c

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,6 @@ hwloc__read_fd_as_cpumask(int fd, hwloc_bitmap_t set)
678678
return 0;
679679
}
680680

681-
/* FIXME: export to API? */
682681
static __hwloc_inline int
683682
hwloc__read_path_as_cpumask(const char *maskpath, hwloc_bitmap_t set, int fsroot_fd)
684683
{
@@ -707,6 +706,18 @@ hwloc__alloc_read_path_as_cpumask(const char *maskpath, int fsroot_fd)
707706
return set;
708707
}
709708

709+
int
710+
hwloc_linux_read_path_as_cpumask(const char *maskpath, hwloc_bitmap_t set)
711+
{
712+
int fd, err;
713+
fd = open(maskpath, O_RDONLY);
714+
if (fd < 0)
715+
return -1;
716+
err = hwloc__read_fd_as_cpumask(fd, set);
717+
close(fd);
718+
return err;
719+
}
720+
710721

711722
/*****************************
712723
******* CpuBind Hooks *******
@@ -1977,71 +1988,6 @@ struct hwloc_linux_cpuinfo_proc {
19771988
unsigned infos_count;
19781989
};
19791990

1980-
/* FIXME drop from API */
1981-
int
1982-
hwloc_linux_parse_cpumap_file(FILE *file, hwloc_bitmap_t set)
1983-
{
1984-
unsigned long *maps;
1985-
unsigned long map;
1986-
int nr_maps = 0;
1987-
static int _nr_maps_allocated = 8; /* Only compute the power-of-two above the kernel cpumask size once.
1988-
* Actually, it may increase multiple times if first read cpumaps start with zeroes.
1989-
*/
1990-
int nr_maps_allocated = _nr_maps_allocated;
1991-
int i;
1992-
1993-
maps = malloc(nr_maps_allocated * sizeof(*maps));
1994-
if (!maps)
1995-
return -1;
1996-
1997-
/* reset to zero first */
1998-
hwloc_bitmap_zero(set);
1999-
2000-
/* parse the whole mask */
2001-
while (fscanf(file, "%lx,", &map) == 1) /* read one kernel cpu mask and the ending comma */
2002-
{
2003-
if (nr_maps == nr_maps_allocated) {
2004-
unsigned long *tmp = realloc(maps, 2*nr_maps_allocated * sizeof(*maps));
2005-
if (!tmp) {
2006-
free(maps);
2007-
return -1;
2008-
}
2009-
maps = tmp;
2010-
nr_maps_allocated *= 2;
2011-
}
2012-
2013-
if (!map && !nr_maps)
2014-
/* ignore the first map if it's empty */
2015-
continue;
2016-
2017-
maps[nr_maps++] = map;
2018-
}
2019-
2020-
/* convert into a set */
2021-
#if KERNEL_CPU_MASK_BITS == HWLOC_BITS_PER_LONG
2022-
for(i=0; i<nr_maps; i++)
2023-
hwloc_bitmap_set_ith_ulong(set, i, maps[nr_maps-1-i]);
2024-
#else
2025-
for(i=0; i<(nr_maps+1)/2; i++) {
2026-
unsigned long mask;
2027-
mask = maps[nr_maps-2*i-1];
2028-
if (2*i+1<nr_maps)
2029-
mask |= maps[nr_maps-2*i-2] << KERNEL_CPU_MASK_BITS;
2030-
hwloc_bitmap_set_ith_ulong(set, i, mask);
2031-
}
2032-
#endif
2033-
2034-
free(maps);
2035-
2036-
/* Only update the static value with the final one,
2037-
* to avoid sharing intermediate values that we modify,
2038-
* in case there's ever multiple concurrent calls.
2039-
*/
2040-
if (nr_maps_allocated > _nr_maps_allocated)
2041-
_nr_maps_allocated = nr_maps_allocated;
2042-
return 0;
2043-
}
2044-
20451991
static void
20461992
hwloc_find_linux_cpuset_mntpnt(char **cgroup_mntpnt, char **cpuset_mntpnt, const char *root_path)
20471993
{

include/hwloc/cuda.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ hwloc_cuda_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
9696
/* If we're on Linux, use the sysfs mechanism to get the local cpus */
9797
#define HWLOC_CUDA_DEVICE_SYSFS_PATH_MAX 128
9898
char path[HWLOC_CUDA_DEVICE_SYSFS_PATH_MAX];
99-
FILE *sysfile = NULL;
10099
int domainid, busid, deviceid;
101100

102101
if (hwloc_cuda_get_device_pci_ids(topology, cudevice, &domainid, &busid, &deviceid))
@@ -108,15 +107,9 @@ hwloc_cuda_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
108107
}
109108

110109
sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domainid, busid, deviceid);
111-
sysfile = fopen(path, "r");
112-
if (!sysfile)
113-
return -1;
114-
115-
if (hwloc_linux_parse_cpumap_file(sysfile, set) < 0
110+
if (hwloc_linux_read_path_as_cpumask(path, set) < 0
116111
|| hwloc_bitmap_iszero(set))
117112
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
118-
119-
fclose(sysfile);
120113
#else
121114
/* Non-Linux systems simply get a full cpuset */
122115
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));

include/hwloc/cudart.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ hwloc_cudart_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unuse
9393
/* If we're on Linux, use the sysfs mechanism to get the local cpus */
9494
#define HWLOC_CUDART_DEVICE_SYSFS_PATH_MAX 128
9595
char path[HWLOC_CUDART_DEVICE_SYSFS_PATH_MAX];
96-
FILE *sysfile = NULL;
9796
int domain, bus, dev;
9897

9998
if (hwloc_cudart_get_device_pci_ids(topology, idx, &domain, &bus, &dev))
@@ -105,15 +104,9 @@ hwloc_cudart_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unuse
105104
}
106105

107106
sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domain, bus, dev);
108-
sysfile = fopen(path, "r");
109-
if (!sysfile)
110-
return -1;
111-
112-
if (hwloc_linux_parse_cpumap_file(sysfile, set) < 0
107+
if (hwloc_linux_read_path_as_cpumask(path, set) < 0
113108
|| hwloc_bitmap_iszero(set))
114109
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
115-
116-
fclose(sysfile);
117110
#else
118111
/* Non-Linux systems simply get a full cpuset */
119112
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));

include/hwloc/intel-mic.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ hwloc_intel_mic_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_un
6464
#define HWLOC_INTEL_MIC_DEVICE_SYSFS_PATH_MAX 128
6565
char path[HWLOC_INTEL_MIC_DEVICE_SYSFS_PATH_MAX];
6666
DIR *sysdir = NULL;
67-
FILE *sysfile = NULL;
6867
struct dirent *dirent;
6968
unsigned pcibus, pcidev, pcifunc;
7069

@@ -81,17 +80,9 @@ hwloc_intel_mic_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_un
8180
while ((dirent = readdir(sysdir)) != NULL) {
8281
if (sscanf(dirent->d_name, "pci_%02x:%02x.%02x", &pcibus, &pcidev, &pcifunc) == 3) {
8382
sprintf(path, "/sys/class/mic/mic%d/pci_%02x:%02x.%02x/local_cpus", idx, pcibus, pcidev, pcifunc);
84-
sysfile = fopen(path, "r");
85-
if (!sysfile) {
86-
closedir(sysdir);
87-
return -1;
88-
}
89-
90-
if (hwloc_linux_parse_cpumap_file(sysfile, set) < 0
83+
if (hwloc_linux_read_path_as_cpumask(path, set) < 0
9184
|| hwloc_bitmap_iszero(set))
9285
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
93-
94-
fclose(sysfile);
9586
break;
9687
}
9788
}

include/hwloc/linux.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ extern "C" {
3232
* @{
3333
*/
3434

35-
/** \brief Convert a linux kernel cpumap file \p file into hwloc CPU set.
36-
*
37-
* Might be used when reading CPU set from sysfs attributes such as topology
38-
* and caches for processors, or local_cpus for devices.
39-
*/
40-
HWLOC_DECLSPEC int hwloc_linux_parse_cpumap_file(FILE *file, hwloc_cpuset_t set);
41-
4235
/** \brief Bind a thread \p tid on cpus given in cpuset \p set
4336
*
4437
* The behavior is exactly the same as the Linux sched_setaffinity system call,
@@ -66,6 +59,15 @@ HWLOC_DECLSPEC int hwloc_linux_get_tid_cpubind(hwloc_topology_t topology, pid_t
6659
*/
6760
HWLOC_DECLSPEC int hwloc_linux_get_tid_last_cpu_location(hwloc_topology_t topology, pid_t tid, hwloc_bitmap_t set);
6861

62+
/** \brief Convert a linux kernel cpumask file \p path into a hwloc bitmap \p set.
63+
*
64+
* Might be used when reading CPU set from sysfs attributes such as topology
65+
* and caches for processors, or local_cpus for devices.
66+
*
67+
* \note This function ignores the HWLOC_FSROOT environment variable.
68+
*/
69+
HWLOC_DECLSPEC int hwloc_linux_read_path_as_cpumask(const char *path, hwloc_bitmap_t set);
70+
6971
/** @} */
7072

7173

include/hwloc/nvml.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ hwloc_nvml_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
6060
/* If we're on Linux, use the sysfs mechanism to get the local cpus */
6161
#define HWLOC_NVML_DEVICE_SYSFS_PATH_MAX 128
6262
char path[HWLOC_NVML_DEVICE_SYSFS_PATH_MAX];
63-
FILE *sysfile = NULL;
6463
nvmlReturn_t nvres;
6564
nvmlPciInfo_t pci;
6665

@@ -76,15 +75,9 @@ hwloc_nvml_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
7675
}
7776

7877
sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", pci.domain, pci.bus, pci.device);
79-
sysfile = fopen(path, "r");
80-
if (!sysfile)
81-
return -1;
82-
83-
if (hwloc_linux_parse_cpumap_file(sysfile, set) < 0
78+
if (hwloc_linux_read_path_as_cpumask(path, set) < 0
8479
|| hwloc_bitmap_iszero(set))
8580
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
86-
87-
fclose(sysfile);
8881
#else
8982
/* Non-Linux systems simply get a full cpuset */
9083
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));

include/hwloc/opencl.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ hwloc_opencl_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unuse
6969
/* If we're on Linux + AMD OpenCL, use the AMD extension + the sysfs mechanism to get the local cpus */
7070
#define HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX 128
7171
char path[HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX];
72-
FILE *sysfile = NULL;
7372
cl_device_topology_amd amdtopo;
7473
cl_int clret;
7574

@@ -89,15 +88,9 @@ hwloc_opencl_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unuse
8988
}
9089

9190
sprintf(path, "/sys/bus/pci/devices/0000:%02x:%02x.%01x/local_cpus", amdtopo.pcie.bus, amdtopo.pcie.device, amdtopo.pcie.function);
92-
sysfile = fopen(path, "r");
93-
if (!sysfile)
94-
return -1;
95-
96-
if (hwloc_linux_parse_cpumap_file(sysfile, set) < 0
91+
if (hwloc_linux_read_path_as_cpumask(path, set) < 0
9792
|| hwloc_bitmap_iszero(set))
9893
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
99-
100-
fclose(sysfile);
10194
#else
10295
/* Non-Linux + AMD OpenCL systems simply get a full cpuset */
10396
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));

include/hwloc/openfabrics-verbs.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ hwloc_ibv_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
6767
get the local cpus */
6868
#define HWLOC_OPENFABRICS_VERBS_SYSFS_PATH_MAX 128
6969
char path[HWLOC_OPENFABRICS_VERBS_SYSFS_PATH_MAX];
70-
FILE *sysfile = NULL;
7170

7271
if (!hwloc_topology_is_thissystem(topology)) {
7372
errno = EINVAL;
@@ -76,15 +75,9 @@ hwloc_ibv_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
7675

7776
sprintf(path, "/sys/class/infiniband/%s/device/local_cpus",
7877
ibv_get_device_name(ibdev));
79-
sysfile = fopen(path, "r");
80-
if (!sysfile)
81-
return -1;
82-
83-
if (hwloc_linux_parse_cpumap_file(sysfile, set) < 0
78+
if (hwloc_linux_read_path_as_cpumask(path, set) < 0
8479
|| hwloc_bitmap_iszero(set))
8580
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
86-
87-
fclose(sysfile);
8881
#else
8982
/* Non-Linux systems simply get a full cpuset */
9083
hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));

0 commit comments

Comments
 (0)