1 /*
2  * Copyright © 2021 Inria.  All rights reserved.
3  * See COPYING in top-level directory.
4  */
5 
6 #include "hwloc.h"
7 
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <string.h>
12 #include <assert.h>
13 
14 #ifndef PATH_MAX
15 #define PATH_MAX 1024
16 #endif
17 
main(void)18 int main(void)
19 {
20   const char *top_srcdir;
21   char xmlpath[PATH_MAX];
22   hwloc_topology_t topology;
23   hwloc_obj_t tmp, obj, sda, cuda0, card0;
24   int err;
25 
26   top_srcdir = getenv("HWLOC_TOP_SRCDIR");
27   if (!top_srcdir) {
28     fprintf(stderr, "HWLOC_TOP_SRCDIR missing in the environment.\n");
29     exit(EXIT_FAILURE);
30   }
31 
32   snprintf(xmlpath, sizeof(xmlpath), "%s/tests/hwloc/xml/32em64t-2n8c2t-pci-normalio.xml", top_srcdir);
33   err = hwloc_topology_init(&topology);
34   assert(!err);
35   err = hwloc_topology_set_xml(topology, xmlpath);
36   assert(!err);
37   err = hwloc_topology_set_all_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_ALL);
38   assert(!err);
39   err = hwloc_topology_load(topology);
40   assert(!err);
41 
42   /* normal checks */
43 
44   /* no NUMA or package == root */
45   tmp = hwloc_get_root_obj(topology);
46   assert(tmp);
47   obj = hwloc_get_obj_with_same_locality(topology, tmp, HWLOC_OBJ_PACKAGE, NULL, NULL, 0);
48   assert(!obj);
49   obj = hwloc_get_obj_with_same_locality(topology, tmp, HWLOC_OBJ_NUMANODE, NULL, NULL, 0);
50   assert(!obj);
51 
52   /* NUMA == Package == L3 != PU */
53   tmp = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PACKAGE, 0);
54   assert(tmp);
55   obj = hwloc_get_obj_with_same_locality(topology, tmp, HWLOC_OBJ_PACKAGE, NULL, NULL, 0);
56   assert(obj);
57   assert(obj == tmp);
58   obj = hwloc_get_obj_with_same_locality(topology, tmp, HWLOC_OBJ_L3CACHE, NULL, NULL, 0);
59   assert(obj);
60   assert(obj->parent == tmp);
61   assert(obj->type == HWLOC_OBJ_L3CACHE);
62   obj = hwloc_get_obj_with_same_locality(topology, tmp, HWLOC_OBJ_NUMANODE, NULL, NULL, 0);
63   assert(obj);
64   assert(obj->parent == tmp);
65   assert(obj->type == HWLOC_OBJ_NUMANODE);
66   obj = hwloc_get_obj_with_same_locality(topology, tmp, HWLOC_OBJ_PU, NULL, NULL, 0);
67   assert(!obj);
68 
69   /* Core == L1 == L2 != Package */
70   tmp = hwloc_get_obj_by_type(topology, HWLOC_OBJ_CORE, 15);
71   assert(tmp);
72   obj = hwloc_get_obj_with_same_locality(topology, tmp, HWLOC_OBJ_PACKAGE, NULL, NULL, 0);
73   assert(!obj);
74   obj = hwloc_get_obj_with_same_locality(topology, tmp, HWLOC_OBJ_L1CACHE, NULL, NULL, 0);
75   assert(obj);
76   assert(obj == tmp->parent);
77   assert(obj->type == HWLOC_OBJ_L1CACHE);
78   obj = hwloc_get_obj_with_same_locality(topology, tmp, HWLOC_OBJ_L2CACHE, NULL, NULL, 0);
79   assert(obj);
80   assert(obj == tmp->parent->parent);
81   assert(obj->type == HWLOC_OBJ_L2CACHE);
82 
83   /* I/O checks */
84 
85   /* get pointers to some I/O objects */
86   obj = hwloc_get_pcidev_by_busid(topology, 0, 0, 0x1f, 2);
87   assert(obj);
88   sda = obj->io_first_child;
89   assert(sda);
90   assert(!strcmp(sda->name, "sda"));
91 
92   obj = hwloc_get_pcidev_by_busid(topology, 0, 0x84, 0, 0);
93   assert(obj);
94   cuda0 = obj->io_first_child;
95   assert(cuda0);
96   assert(!strcmp(cuda0->name, "cuda0"));
97   card0 = cuda0->next_sibling;
98   assert(card0);
99   assert(!strcmp(card0->name, "card0"));
100 
101   /* invalid flags */
102   obj = hwloc_get_obj_with_same_locality(topology, sda, HWLOC_OBJ_PCI_DEVICE, NULL, NULL, 1);
103   assert(!obj);
104   /* invalid subtype */
105   obj = hwloc_get_obj_with_same_locality(topology, sda, HWLOC_OBJ_PCI_DEVICE, "foo", NULL, 0);
106   assert(!obj);
107   /* invalid name */
108   obj = hwloc_get_obj_with_same_locality(topology, sda, HWLOC_OBJ_PCI_DEVICE, NULL, "bar", 0);
109   assert(!obj);
110 
111   /* get sda PCI parent */
112   obj = hwloc_get_obj_with_same_locality(topology, sda, HWLOC_OBJ_PCI_DEVICE, NULL, NULL, 0);
113   assert(obj == sda->parent);
114   /* get sda from itself */
115   obj = hwloc_get_obj_with_same_locality(topology, sda, HWLOC_OBJ_OS_DEVICE, NULL, NULL, 0);
116   assert(obj == sda);
117   /* get sda from PCI */
118   obj = hwloc_get_obj_with_same_locality(topology, sda->parent, HWLOC_OBJ_OS_DEVICE, NULL, NULL, 0);
119   assert(obj == sda);
120   /* get PCI from PCI */
121   obj = hwloc_get_obj_with_same_locality(topology, sda->parent, HWLOC_OBJ_PCI_DEVICE, NULL, NULL, 0);
122   assert(obj == sda->parent);
123 
124   /* get cuda0 from card0 */
125   obj = hwloc_get_obj_with_same_locality(topology, card0, HWLOC_OBJ_OS_DEVICE, "CUDA", NULL, 0);
126   assert(obj == cuda0);
127   obj = hwloc_get_obj_with_same_locality(topology, card0, HWLOC_OBJ_OS_DEVICE, NULL, "cuda", 0);
128   assert(obj == cuda0);
129   /* get card0 from cuda parent */
130   obj = hwloc_get_obj_with_same_locality(topology, cuda0->parent, HWLOC_OBJ_OS_DEVICE, NULL, "card", 0);
131   assert(obj == card0);
132 
133   /* invalid normal to I/O */
134   tmp = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PACKAGE, 0);
135   assert(tmp);
136   obj = hwloc_get_obj_with_same_locality(topology, tmp, HWLOC_OBJ_PCI_DEVICE, NULL, NULL, 0);
137   assert(!obj);
138   /* invalid I/O to normal */
139   obj = hwloc_get_obj_with_same_locality(topology, cuda0->parent, HWLOC_OBJ_PACKAGE, NULL, NULL, 0);
140   assert(!obj);
141 
142   hwloc_topology_destroy(topology);
143 
144   return 0;
145 }
146