shmem.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright © 2013-2023 Inria. All rights reserved.
  3. * See COPYING in top-level directory.
  4. */
  5. /** \file
  6. * \brief Sharing topologies between processes
  7. */
  8. #ifndef HWLOC_SHMEM_H
  9. #define HWLOC_SHMEM_H
  10. #include "hwloc.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #elif 0
  14. }
  15. #endif
  16. /** \defgroup hwlocality_shmem Sharing topologies between processes
  17. *
  18. * These functions are used to share a topology between processes by
  19. * duplicating it into a file-backed shared-memory buffer.
  20. *
  21. * The master process must first get the required shared-memory size
  22. * for storing this topology with hwloc_shmem_topology_get_length().
  23. *
  24. * Then it must find a virtual memory area of that size that is available
  25. * in all processes (identical virtual addresses in all processes).
  26. * On Linux, this can be done by comparing holes found in /proc/\<pid\>/maps
  27. * for each process.
  28. *
  29. * Once found, it must open a destination file for storing the buffer,
  30. * and pass it to hwloc_shmem_topology_write() together with
  31. * virtual memory address and length obtained above.
  32. *
  33. * Other processes may then adopt this shared topology by opening the
  34. * same file and passing it to hwloc_shmem_topology_adopt() with the
  35. * exact same virtual memory address and length.
  36. *
  37. * @{
  38. */
  39. /** \brief Get the required shared memory length for storing a topology.
  40. *
  41. * This length (in bytes) must be used in hwloc_shmem_topology_write()
  42. * and hwloc_shmem_topology_adopt() later.
  43. *
  44. * \return the length, or -1 on error, for instance if flags are invalid.
  45. *
  46. * \note Flags \p flags are currently unused, must be 0.
  47. */
  48. HWLOC_DECLSPEC int hwloc_shmem_topology_get_length(hwloc_topology_t topology,
  49. size_t *lengthp,
  50. unsigned long flags);
  51. /** \brief Duplicate a topology to a shared memory file.
  52. *
  53. * Temporarily map a file in virtual memory and duplicate the
  54. * topology \p topology by allocating duplicates in there.
  55. *
  56. * The segment of the file pointed by descriptor \p fd,
  57. * starting at offset \p fileoffset, and of length \p length (in bytes),
  58. * will be temporarily mapped at virtual address \p mmap_address
  59. * during the duplication.
  60. *
  61. * The mapping length \p length must have been previously obtained with
  62. * hwloc_shmem_topology_get_length()
  63. * and the topology must not have been modified in the meantime.
  64. *
  65. * \note Flags \p flags are currently unused, must be 0.
  66. *
  67. * \note The object userdata pointer is duplicated but the pointed buffer
  68. * is not. However the caller may also allocate it manually in shared memory
  69. * to share it as well.
  70. *
  71. * \return 0 on success.
  72. * \return -1 with errno set to \c EBUSY if the virtual memory mapping defined
  73. * by \p mmap_address and \p length isn't available in the process.
  74. * \return -1 with errno set to \c EINVAL if \p fileoffset, \p mmap_address
  75. * or \p length aren't page-aligned.
  76. */
  77. HWLOC_DECLSPEC int hwloc_shmem_topology_write(hwloc_topology_t topology,
  78. int fd, hwloc_uint64_t fileoffset,
  79. void *mmap_address, size_t length,
  80. unsigned long flags);
  81. /** \brief Adopt a shared memory topology stored in a file.
  82. *
  83. * Map a file in virtual memory and adopt the topology that was previously
  84. * stored there with hwloc_shmem_topology_write().
  85. *
  86. * The returned adopted topology in \p topologyp can be used just like any
  87. * topology. And it must be destroyed with hwloc_topology_destroy() as usual.
  88. *
  89. * However the topology is read-only.
  90. * For instance, it cannot be modified with hwloc_topology_restrict()
  91. * and object userdata pointers cannot be changed.
  92. *
  93. * The segment of the file pointed by descriptor \p fd,
  94. * starting at offset \p fileoffset, and of length \p length (in bytes),
  95. * will be mapped at virtual address \p mmap_address.
  96. *
  97. * The file pointed by descriptor \p fd, the offset \p fileoffset,
  98. * the requested mapping virtual address \p mmap_address and the length \p length
  99. * must be identical to what was given to hwloc_shmem_topology_write() earlier.
  100. *
  101. * \note Flags \p flags are currently unused, must be 0.
  102. *
  103. * \note The object userdata pointer should not be used unless the process
  104. * that created the shared topology also placed userdata-pointed buffers
  105. * in shared memory.
  106. *
  107. * \note This function takes care of calling hwloc_topology_abi_check().
  108. *
  109. * \return 0 on success.
  110. *
  111. * \return -1 with errno set to \c EBUSY if the virtual memory mapping defined
  112. * by \p mmap_address and \p length isn't available in the process.
  113. *
  114. * \return -1 with errno set to \c EINVAL if \p fileoffset, \p mmap_address
  115. * or \p length aren't page-aligned, or do not match what was given to
  116. * hwloc_shmem_topology_write() earlier.
  117. *
  118. * \return -1 with errno set to \c EINVAL if the layout of the topology structure
  119. * is different between the writer process and the adopter process.
  120. */
  121. HWLOC_DECLSPEC int hwloc_shmem_topology_adopt(hwloc_topology_t *topologyp,
  122. int fd, hwloc_uint64_t fileoffset,
  123. void *mmap_address, size_t length,
  124. unsigned long flags);
  125. /** @} */
  126. #ifdef __cplusplus
  127. } /* extern "C" */
  128. #endif
  129. #endif /* HWLOC_SHMEM_H */