export.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright © 2009-2018 Inria. All rights reserved.
  3. * Copyright © 2009-2012 Université Bordeaux
  4. * Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved.
  5. * See COPYING in top-level directory.
  6. */
  7. /** \file
  8. * \brief Exporting Topologies to XML or to Synthetic strings.
  9. */
  10. #ifndef HWLOC_EXPORT_H
  11. #define HWLOC_EXPORT_H
  12. #ifndef HWLOC_H
  13. #error Please include the main hwloc.h instead
  14. #endif
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #elif 0
  18. }
  19. #endif
  20. /** \defgroup hwlocality_xmlexport Exporting Topologies to XML
  21. * @{
  22. */
  23. /** \brief Flags for exporting XML topologies.
  24. *
  25. * Flags to be given as a OR'ed set to hwloc_topology_export_xml().
  26. */
  27. enum hwloc_topology_export_xml_flags_e {
  28. /** \brief Export XML that is loadable by hwloc v1.x.
  29. * However, the export may miss some details about the topology.
  30. * \hideinitializer
  31. */
  32. HWLOC_TOPOLOGY_EXPORT_XML_FLAG_V1 = (1UL<<0)
  33. };
  34. /** \brief Export the topology into an XML file.
  35. *
  36. * This file may be loaded later through hwloc_topology_set_xml().
  37. *
  38. * By default, the latest export format is used, which means older hwloc
  39. * releases (e.g. v1.x) will not be able to import it.
  40. * Exporting to v1.x specific XML format is possible using flag
  41. * ::HWLOC_TOPOLOGY_EXPORT_XML_FLAG_V1 but it may miss some details
  42. * about the topology.
  43. * If there is any chance that the exported file may ever be imported
  44. * back by a process using hwloc 1.x, one should consider detecting
  45. * it at runtime and using the corresponding export format.
  46. *
  47. * \p flags is a OR'ed set of ::hwloc_topology_export_xml_flags_e.
  48. *
  49. * \return 0 on success, or -1 on error.
  50. *
  51. * \note See also hwloc_topology_set_userdata_export_callback()
  52. * for exporting application-specific object userdata.
  53. *
  54. * \note The topology-specific userdata pointer is ignored when exporting to XML.
  55. *
  56. * \note Only printable characters may be exported to XML string attributes.
  57. * Any other character, especially any non-ASCII character, will be silently
  58. * dropped.
  59. *
  60. * \note If \p name is "-", the XML output is sent to the standard output.
  61. */
  62. HWLOC_DECLSPEC int hwloc_topology_export_xml(hwloc_topology_t topology, const char *xmlpath, unsigned long flags);
  63. /** \brief Export the topology into a newly-allocated XML memory buffer.
  64. *
  65. * \p xmlbuffer is allocated by the callee and should be freed with
  66. * hwloc_free_xmlbuffer() later in the caller.
  67. *
  68. * This memory buffer may be loaded later through hwloc_topology_set_xmlbuffer().
  69. *
  70. * By default, the latest export format is used, which means older hwloc
  71. * releases (e.g. v1.x) will not be able to import it.
  72. * Exporting to v1.x specific XML format is possible using flag
  73. * ::HWLOC_TOPOLOGY_EXPORT_XML_FLAG_V1 but it may miss some details
  74. * about the topology.
  75. * If there is any chance that the exported buffer may ever be imported
  76. * back by a process using hwloc 1.x, one should consider detecting
  77. * it at runtime and using the corresponding export format.
  78. *
  79. * The returned buffer ends with a \0 that is included in the returned
  80. * length.
  81. *
  82. * \p flags is a OR'ed set of ::hwloc_topology_export_xml_flags_e.
  83. *
  84. * \return 0 on success, or -1 on error.
  85. *
  86. * \note See also hwloc_topology_set_userdata_export_callback()
  87. * for exporting application-specific object userdata.
  88. *
  89. * \note The topology-specific userdata pointer is ignored when exporting to XML.
  90. *
  91. * \note Only printable characters may be exported to XML string attributes.
  92. * Any other character, especially any non-ASCII character, will be silently
  93. * dropped.
  94. */
  95. HWLOC_DECLSPEC int hwloc_topology_export_xmlbuffer(hwloc_topology_t topology, char **xmlbuffer, int *buflen, unsigned long flags);
  96. /** \brief Free a buffer allocated by hwloc_topology_export_xmlbuffer() */
  97. HWLOC_DECLSPEC void hwloc_free_xmlbuffer(hwloc_topology_t topology, char *xmlbuffer);
  98. /** \brief Set the application-specific callback for exporting object userdata
  99. *
  100. * The object userdata pointer is not exported to XML by default because hwloc
  101. * does not know what it contains.
  102. *
  103. * This function lets applications set \p export_cb to a callback function
  104. * that converts this opaque userdata into an exportable string.
  105. *
  106. * \p export_cb is invoked during XML export for each object whose
  107. * \p userdata pointer is not \c NULL.
  108. * The callback should use hwloc_export_obj_userdata() or
  109. * hwloc_export_obj_userdata_base64() to actually export
  110. * something to XML (possibly multiple times per object).
  111. *
  112. * \p export_cb may be set to \c NULL if userdata should not be exported to XML.
  113. *
  114. * \note The topology-specific userdata pointer is ignored when exporting to XML.
  115. */
  116. HWLOC_DECLSPEC void hwloc_topology_set_userdata_export_callback(hwloc_topology_t topology,
  117. void (*export_cb)(void *reserved, hwloc_topology_t topology, hwloc_obj_t obj));
  118. /** \brief Export some object userdata to XML
  119. *
  120. * This function may only be called from within the export() callback passed
  121. * to hwloc_topology_set_userdata_export_callback().
  122. * It may be invoked one of multiple times to export some userdata to XML.
  123. * The \p buffer content of length \p length is stored with optional name
  124. * \p name.
  125. *
  126. * When importing this XML file, the import() callback (if set) will be
  127. * called exactly as many times as hwloc_export_obj_userdata() was called
  128. * during export(). It will receive the corresponding \p name, \p buffer
  129. * and \p length arguments.
  130. *
  131. * \p reserved, \p topology and \p obj must be the first three parameters
  132. * that were given to the export callback.
  133. *
  134. * Only printable characters may be exported to XML string attributes.
  135. *
  136. * If exporting binary data, the application should first encode into
  137. * printable characters only (or use hwloc_export_obj_userdata_base64()).
  138. * It should also take care of portability issues if the export may
  139. * be reimported on a different architecture.
  140. *
  141. * \return 0 on success.
  142. * \return -1 with errno set to \c EINVAL if a non-printable character is
  143. * passed in \p name or \b buffer.
  144. */
  145. HWLOC_DECLSPEC int hwloc_export_obj_userdata(void *reserved, hwloc_topology_t topology, hwloc_obj_t obj, const char *name, const void *buffer, size_t length);
  146. /** \brief Encode and export some object userdata to XML
  147. *
  148. * This function is similar to hwloc_export_obj_userdata() but it encodes
  149. * the input buffer into printable characters before exporting.
  150. * On import, decoding is automatically performed before the data is given
  151. * to the import() callback if any.
  152. *
  153. * This function may only be called from within the export() callback passed
  154. * to hwloc_topology_set_userdata_export_callback().
  155. *
  156. * The name must be made of printable characters for export to XML string attributes.
  157. *
  158. * The function does not take care of portability issues if the export
  159. * may be reimported on a different architecture.
  160. *
  161. * \return 0 on success.
  162. * \return -1 with errno set to \c EINVAL if a non-printable character is
  163. * passed in \p name.
  164. */
  165. HWLOC_DECLSPEC int hwloc_export_obj_userdata_base64(void *reserved, hwloc_topology_t topology, hwloc_obj_t obj, const char *name, const void *buffer, size_t length);
  166. /** \brief Set the application-specific callback for importing userdata
  167. *
  168. * On XML import, userdata is ignored by default because hwloc does not know
  169. * how to store it in memory.
  170. *
  171. * This function lets applications set \p import_cb to a callback function
  172. * that will get the XML-stored userdata and store it in the object as expected
  173. * by the application.
  174. *
  175. * \p import_cb is called during hwloc_topology_load() as many times as
  176. * hwloc_export_obj_userdata() was called during export. The topology
  177. * is not entirely setup yet. Object attributes are ready to consult,
  178. * but links between objects are not.
  179. *
  180. * \p import_cb may be \c NULL if userdata should be ignored during import.
  181. *
  182. * \note \p buffer contains \p length characters followed by a null byte ('\0').
  183. *
  184. * \note This function should be called before hwloc_topology_load().
  185. *
  186. * \note The topology-specific userdata pointer is ignored when importing from XML.
  187. */
  188. HWLOC_DECLSPEC void hwloc_topology_set_userdata_import_callback(hwloc_topology_t topology,
  189. void (*import_cb)(hwloc_topology_t topology, hwloc_obj_t obj, const char *name, const void *buffer, size_t length));
  190. /** @} */
  191. /** \defgroup hwlocality_syntheticexport Exporting Topologies to Synthetic
  192. * @{
  193. */
  194. /** \brief Flags for exporting synthetic topologies.
  195. *
  196. * Flags to be given as a OR'ed set to hwloc_topology_export_synthetic().
  197. */
  198. enum hwloc_topology_export_synthetic_flags_e {
  199. /** \brief Export extended types such as L2dcache as basic types such as Cache.
  200. *
  201. * This is required if loading the synthetic description with hwloc < 1.9.
  202. * \hideinitializer
  203. */
  204. HWLOC_TOPOLOGY_EXPORT_SYNTHETIC_FLAG_NO_EXTENDED_TYPES = (1UL<<0),
  205. /** \brief Do not export level attributes.
  206. *
  207. * Ignore level attributes such as memory/cache sizes or PU indexes.
  208. * This is required if loading the synthetic description with hwloc < 1.10.
  209. * \hideinitializer
  210. */
  211. HWLOC_TOPOLOGY_EXPORT_SYNTHETIC_FLAG_NO_ATTRS = (1UL<<1),
  212. /** \brief Export the memory hierarchy as expected in hwloc 1.x.
  213. *
  214. * Instead of attaching memory children to levels, export single NUMA node child
  215. * as normal intermediate levels, when possible.
  216. * This is required if loading the synthetic description with hwloc 1.x.
  217. * However this may fail if some objects have multiple local NUMA nodes.
  218. * \hideinitializer
  219. */
  220. HWLOC_TOPOLOGY_EXPORT_SYNTHETIC_FLAG_V1 = (1UL<<2),
  221. /** \brief Do not export memory information.
  222. *
  223. * Only export the actual hierarchy of normal CPU-side objects and ignore
  224. * where memory is attached.
  225. * This is useful for when the hierarchy of CPUs is what really matters,
  226. * but it behaves as if there was a single machine-wide NUMA node.
  227. * \hideinitializer
  228. */
  229. HWLOC_TOPOLOGY_EXPORT_SYNTHETIC_FLAG_IGNORE_MEMORY = (1UL<<3)
  230. };
  231. /** \brief Export the topology as a synthetic string.
  232. *
  233. * At most \p buflen characters will be written in \p buffer,
  234. * including the terminating \0.
  235. *
  236. * This exported string may be given back to hwloc_topology_set_synthetic().
  237. *
  238. * \p flags is a OR'ed set of ::hwloc_topology_export_synthetic_flags_e.
  239. *
  240. * \return The number of characters that were written,
  241. * not including the terminating \0.
  242. *
  243. * \return -1 if the topology could not be exported,
  244. * for instance if it is not symmetric.
  245. *
  246. * \note I/O and Misc children are ignored, the synthetic string only
  247. * describes normal children.
  248. *
  249. * \note A 1024-byte buffer should be large enough for exporting
  250. * topologies in the vast majority of cases.
  251. */
  252. HWLOC_DECLSPEC int hwloc_topology_export_synthetic(hwloc_topology_t topology, char *buffer, size_t buflen, unsigned long flags);
  253. /** @} */
  254. #ifdef __cplusplus
  255. } /* extern "C" */
  256. #endif
  257. #endif /* HWLOC_EXPORT_H */