Debian Codegraph @

utils/lstopo/lstopo-shmem.c

raw
1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2018-2020 Inria. All rights reserved.
4 * See COPYING in top-level directory.
5 */
6
7 #include <private/autogen/config.h>
8 #include <private/private.h>
9
10 #include <hwloc.h>
11 #include <hwloc/shmem.h>
12 #include <string.h>
13 #include <fcntl.h>
14 #include <sys/stat.h>
15 #include <sys/mman.h>
16
17 #include "lstopo.h"
18
19 struct lstopo_shmem_header {
20 #define LSTOPO_SHMEM_HEADER_VERSION 1U
21 uint32_t header_version;
22 uint32_t header_length;
23 uint64_t mmap_address;
24 uint64_t mmap_length;
25 uint64_t file_offset;
26 };
27
28 extern uint64_t shmem_output_addr;
29
30 #ifndef MAP_ANONYMOUS /* for Mac OS X 9 */
31 #define MAP_ANONYMOUS MAP_ANON
32 #endif
33
34 static unsigned long
35 find_mmap_addr(unsigned long length)
36 {
37 unsigned long addr;
38 void *tmp_mmap;
39 int err;
40
41 /* try to find a good address starting from something in the middle of the entire/full address space */
42 #if SIZEOF_VOID_P == 8
43 addr = 0x8000000000000000UL;
44 #else
45 addr = 0x80000000UL;
46 #endif
47 printf("Testing mmaps to find room for length %lu\n", length);
48
49 again:
50 tmp_mmap = mmap((void*)(uintptr_t)addr, length, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
51 if (tmp_mmap != MAP_FAILED) {
52 err = munmap((void*)(uintptr_t)tmp_mmap, length);
53 assert(!err);
54 if (tmp_mmap == (void*)(uintptr_t) addr) {
55 /* worked! */
56 printf(" test mmap at 0x%lx succeeded, let's use that!\n", addr);
57 return addr;
58 }
59 printf(" test mmap at 0x%lx returned another address\n", addr);
60 } else
61 printf(" test mmap at 0x%lx failed (errno %d)\n", addr, errno);
62 /* couldn't map there, try again with a smaller address */
63 addr >>= 1;
64 if (addr)
65 goto again;
66
67 return 0;
68 }
69
70 int
71 output_shmem(struct lstopo_output *loutput, const char *filename)
72 {
73 struct lstopo_shmem_header header;
74 size_t shmem_length;
75 struct stat st;
76 int fd, err;
77
78 if (!filename || !strcasecmp(filename, "-.shmem")) {
79 fprintf(stderr, "Cannot export shmem topology to stdout.\n");
80 return -1;
81 }
82
83 if (!stat(filename, &st) && !loutput->overwrite) {
84 fprintf(stderr, "Failed to export shmem topology to %s (%s)\n", filename, strerror(EEXIST));
85 return -1;
86 }
87
88 err = hwloc_shmem_topology_get_length(loutput->topology, &shmem_length, 0);
89 if (err < 0) {
90 if (errno == ENOSYS)
91 fprintf(stderr, "shmem topology not supported\n"); /* this line must match the grep line in test-lstopo-shmem */
92 else
93 fprintf(stderr, "Failed to compute shmem topology export length\n");
94 return -1;
95 }
96
97 fd = open(filename, O_RDWR|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR);
98 if (fd < 0) {
99 fprintf(stderr, "Failed to open shmem topology file %s (%s)\n", filename, strerror(errno));
100 return -1;
101 }
102
103 if (!loutput->shmem_output_addr)
104 loutput->shmem_output_addr = find_mmap_addr(shmem_length);
105 if (!loutput->shmem_output_addr) {
106 fprintf(stderr, "Failed to find a shmem topology mmap address\n"); /* this line must match the grep line in test-lstopo-shmem */
107 close(fd);
108 unlink(filename);
109 return -1;
110 }
111
112 header.header_version = LSTOPO_SHMEM_HEADER_VERSION;
113 header.header_length = sizeof(header);
114 header.mmap_address = loutput->shmem_output_addr;
115 header.mmap_length = shmem_length;
116 header.file_offset = hwloc_getpagesize();
117 err = write(fd, &header, sizeof(header));
118 if (err != sizeof(header)) {
119 fprintf(stderr, "Failed to write shmem topology header\n");
120 close(fd);
121 unlink(filename);
122 return -1;
123 }
124
125 if (hwloc_shmem_topology_write(loutput->topology, fd, header.file_offset, (void*)(uintptr_t)loutput->shmem_output_addr, shmem_length, 0) < 0) {
126 if (errno == EBUSY)
127 fprintf(stderr, "Failed to export shmem topology, memory range is busy\n"); /* this line must match the grep line in test-lstopo-shmem */
128 else
129 fprintf(stderr, "Failed to export shmem topology to %s (%s)\n", filename, strerror(errno));
130 close(fd);
131 unlink(filename);
132 return -1;
133 }
134
135 close(fd);
136
137 printf("Exported shmem topology to %s for mmap address 0x%llx length %lu\n", filename, (unsigned long long) loutput->shmem_output_addr, (unsigned long) shmem_length);
138
139 return 0;
140 }
141
142 int lstopo_shmem_adopt(const char *input, hwloc_topology_t *topologyp)
143 {
144 hwloc_topology_t adopted;
145 struct lstopo_shmem_header header;
146 int fd, err;
147
148 fd = open(input, O_RDONLY);
149 if (fd < 0)
150 return -1;
151
152 err = read(fd, &header, sizeof(header));
153 if (err < (int) sizeof(header)) {
154 fprintf(stderr, "Failed to read shmem topology header\n");
155 close(fd);
156 return -1;
157 }
158
159 if (header.header_version != LSTOPO_SHMEM_HEADER_VERSION
160 || header.header_length != sizeof(header)) {
161 fprintf(stderr, "Unexpected shmem topology header version %u length %u (instead of %u %u)\n",
162 header.header_version, header.header_length,
163 LSTOPO_SHMEM_HEADER_VERSION, (unsigned) sizeof(header));
164 close(fd);
165 return -1;
166 }
167
168 err = hwloc_shmem_topology_adopt(&adopted, fd, header.file_offset, (void*)(uintptr_t)header.mmap_address, header.mmap_length, 0);
169 close(fd);
170 if (err < 0) {
171 if (errno == EBUSY)
172 fprintf(stderr, "Failed to adopt shmem topology, memory range is busy\n"); /* this line must match the grep line in test-lstopo-shmem */
173 else
174 fprintf(stderr, "Failed to adopt shmem topology (%s)\n", strerror(errno));
175 return -1;
176 }
177
178 err = hwloc_topology_dup(topologyp, adopted);
179 hwloc_topology_destroy(adopted);
180 if (err < 0) {
181 fprintf(stderr, "Failed to duplicate adopted shmem topology (%s)\n", strerror(errno));
182 return -1;
183 }
184
185 return 0;
186 }