1 /*
2  * Copyright © 2012-2019 Inria.  All rights reserved.
3  * See COPYING in top-level directory.
4  */
5 
6 #include "private/autogen/config.h"
7 #include "hwloc.h"
8 #include "private/private.h"
9 
10 #include <stdlib.h>
11 
12 static int
hwloc_look_fake(struct hwloc_backend * backend,struct hwloc_disc_status * dstatus)13 hwloc_look_fake(struct hwloc_backend *backend, struct hwloc_disc_status *dstatus)
14 {
15   hwloc_topology_t topology = backend->topology;
16 
17   assert(dstatus->phase == HWLOC_DISC_PHASE_TWEAK);
18 
19   if (getenv("HWLOC_DEBUG_FAKE_COMPONENT_TWEAK")) {
20     hwloc_obj_t obj;
21     int err;
22     /* restrict to single (last) PU */
23     obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_PU, hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_PU)-1);
24     assert(obj);
25     err = hwloc_topology_restrict(topology, obj->cpuset, 0);
26     assert(!err);
27     /* restrict to single (first) NUMA node */
28     obj = hwloc_get_obj_by_type(topology, HWLOC_OBJ_NUMANODE, 0);
29     assert(obj);
30     err = hwloc_topology_restrict(topology, obj->nodeset, HWLOC_RESTRICT_FLAG_BYNODESET);
31     assert(!err);
32   }
33 
34   return 0;
35 }
36 
37 static struct hwloc_backend *
hwloc_fake_component_instantiate(struct hwloc_topology * topology __hwloc_attribute_unused,struct hwloc_disc_component * component __hwloc_attribute_unused,unsigned excluded_phases __hwloc_attribute_unused,const void * _data1 __hwloc_attribute_unused,const void * _data2 __hwloc_attribute_unused,const void * _data3 __hwloc_attribute_unused)38 hwloc_fake_component_instantiate(struct hwloc_topology *topology __hwloc_attribute_unused,
39 				 struct hwloc_disc_component *component __hwloc_attribute_unused,
40 				 unsigned excluded_phases __hwloc_attribute_unused,
41 				 const void *_data1 __hwloc_attribute_unused,
42 				 const void *_data2 __hwloc_attribute_unused,
43 				 const void *_data3 __hwloc_attribute_unused)
44 {
45   struct hwloc_backend *backend;
46 
47   backend = hwloc_backend_alloc(topology, component);
48   if (!backend)
49     goto out;
50   backend->discover = hwloc_look_fake;
51 
52   if (getenv("HWLOC_DEBUG_FAKE_COMPONENT"))
53     printf("fake component instantiated\n");
54 
55   return backend;
56 
57  out:
58   return NULL;
59 }
60 
61 static struct hwloc_disc_component hwloc_fake_disc_component = {
62   "fake",
63   HWLOC_DISC_PHASE_TWEAK,
64   0, /* nothing to exclude */
65   hwloc_fake_component_instantiate,
66   100, /* make sure it's loaded before anything conflicting excludes it */
67   1,
68   NULL
69 };
70 
71 static int
hwloc_fake_component_init(unsigned long flags)72 hwloc_fake_component_init(unsigned long flags)
73 {
74   if (flags)
75     return -1;
76   if (hwloc_plugin_check_namespace("fake", "hwloc_backend_alloc") < 0)
77     return -1;
78   if (getenv("HWLOC_DEBUG_FAKE_COMPONENT"))
79     printf("fake component initialized\n");
80   return 0;
81 }
82 
83 static void
hwloc_fake_component_finalize(unsigned long flags)84 hwloc_fake_component_finalize(unsigned long flags)
85 {
86   if (flags)
87     return;
88   if (getenv("HWLOC_DEBUG_FAKE_COMPONENT"))
89     printf("fake component finalized\n");
90 }
91 
92 HWLOC_DECLSPEC extern const struct hwloc_component hwloc_fake_component; /* never linked statically in the core */
93 
94 const struct hwloc_component hwloc_fake_component = {
95   HWLOC_COMPONENT_ABI,
96   hwloc_fake_component_init, hwloc_fake_component_finalize,
97   HWLOC_COMPONENT_TYPE_DISC,
98   0,
99   &hwloc_fake_disc_component
100 };
101