-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.h
106 lines (89 loc) · 3.2 KB
/
common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* (C) Copyright 2024 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************
* hpcat: display NUMA and CPU affinities in the context of HPC applications
* common.h: Common functions
*
* URL https://github.com/HewlettPackard/hpcat
******************************************************************************/
#ifndef HPCAT_COMMON_H
#define HPCAT_COMMON_H
#include <stdio.h>
#include <hwloc.h>
#define PCI_STR_MAX 32
#define MAX_DEVICES 32
/**
* Retrieve NUMA affinity of a device based on its PCIe address
*
* @param domain[in] PCIe Domain of the device
* @param bus[in] PCIe Bus Address of the device
* @return NUMA node affinity or -1
*/
static inline int get_device_numa_affinity(const int domain, const int bus)
{
int numa_node = -1;
char numa_file[PATH_MAX];
snprintf(numa_file, PATH_MAX - 1,
"/sys/class/pci_bus/%04x:%02x/device/numa_node", domain, bus);
FILE* file = fopen(numa_file, "r");
if (file == NULL)
return -1;
int ret = fscanf(file, "%d", &numa_node);
fclose(file);
if (ret == EOF || ret == 0)
return -1;
return numa_node;
}
/**
* Convert a list (comma separated values) to a bitmap
*
* @param bitmap[out] Preallocated hwloc bitmap
* @param list_str[in] String containing a list of values
* @return Success: 0, Error: -1
*/
static inline int strlist_to_bitmap(hwloc_bitmap_t bitmap, char *list_str)
{
char *token = strtok(list_str, ",");
while (token != NULL)
{
char *endptr;
long val = strtol(token, &endptr, 10);
if (*endptr != '\0')
return -1;
if (val > MAX_DEVICES)
return -1;
hwloc_bitmap_set(bitmap, val);
token = strtok(NULL, ",");
}
return 0;
}
/**
* Set first bits of a bitmap
*
* @param bitmap[out] Preallocated hwloc bitmap
* @param count[in] Bits to set
*/
static inline void set_first_bits_bitmap(hwloc_bitmap_t bitmap, const int count)
{
for (int i = 0; i < count; i++)
hwloc_bitmap_set(bitmap, i);
}
#endif /* HPCAT_COMMON_H */