You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ipc_helper.h 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <map>
  5. #include <string>
  6. #include <unordered_map>
  7. #include "ipc_imp.h"
  8. #define IPC_INSTACE() ipc::IpcHelper::Instance()
  9. #define IPC_HELP_REMOTE_CALL(SHM_PTR, REMOTEFUNCID) \
  10. struct ipc_imp::MsgBody msg; \
  11. msg.type = ipc_imp::IPC_CALL_REMOTE_API; \
  12. msg.shm_ptr = static_cast<void*>(SHM_PTR); \
  13. msg.remote_func_id = static_cast<size_t>(REMOTEFUNCID); \
  14. IPC_INSTACE().send_ipc_msg(&msg);
  15. #define ASSERT_SHM_SIZE(SHM_SIZE, NEED_SIZE) \
  16. do { \
  17. if (SHM_SIZE < NEED_SIZE) { \
  18. LITE_ERROR( \
  19. "shm_size not enough to run this api need vs config: (%fMB " \
  20. "%fMB), please config it by env: LITE_DEBUG_IPC_SHM_SIZE, for " \
  21. "example config to 20MB by: export LITE_DEBUG_IPC_SHM_SIZE=20", \
  22. NEED_SIZE / 1024.0 / 1024.0, SHM_SIZE / 1024.0 / 1024.0); \
  23. __builtin_trap(); \
  24. } \
  25. } while (0)
  26. namespace ipc {
  27. template <class T>
  28. class Singleton {
  29. public:
  30. Singleton() {}
  31. static T& Instance() {
  32. static T _;
  33. return _;
  34. }
  35. };
  36. class IpcHelper : public Singleton<IpcHelper> {
  37. public:
  38. IpcHelper(const IpcHelper&) = delete;
  39. IpcHelper& operator=(const IpcHelper&) = delete;
  40. IpcHelper();
  41. ~IpcHelper();
  42. //! send msg with default timeout
  43. struct ipc_imp::MsgBody send_ipc_msg(struct ipc_imp::MsgBody* msg) {
  44. return send_msg(msg, &tv);
  45. }
  46. //! get shm ptr
  47. void* get_shm_ptr(void* consumer_ptr);
  48. //! release shm_ptr, NOT free shm_ptr
  49. void release_shm_ptr(void* consumer_ptr);
  50. //! check shm size
  51. void check_shm_size(size_t need_size) { ASSERT_SHM_SIZE(shm_size, need_size); }
  52. //! is enable ipc fork debug mode
  53. static bool is_enable_fork_debug_mode() { return sm_is_enable_fork_ipc; }
  54. static bool sm_is_enable_fork_ipc;
  55. private:
  56. //! 5 minutes
  57. struct timeval tv = {300, 0};
  58. //! map of <shm_ptr, consumer_ptr>,
  59. std::map<void*, void*> m_shm_ptr2consumer_ptr;
  60. size_t shm_size = 0;
  61. //! shm_mem for consumer_ptr == nullptr
  62. void* shm_mem_for_null_consumer_ptr;
  63. LITE_MUTEX m_mtx;
  64. };
  65. enum class RemoteFuncId : size_t {
  66. LITE_MAKE_NETWORK = 1,
  67. LITE_LOAD_MODEL_FROM_PATH = 2,
  68. LITE_GET_LAST_ERROR = 3,
  69. LITE_GET_IO_TENSOR = 4,
  70. LITE_GET_TENSOR_TOTAL_SIZE_IN_BYTE = 5,
  71. LITE_GET_TENSOR_MEMORY = 6,
  72. LITE_MEMSET = 7,
  73. LITE_FORWARD = 8,
  74. LITE_WAIT = 9,
  75. LITE_GET_OUTPUT_NAME = 10,
  76. LITE_COPY_SERVER_TENSOR_MEMORY = 11,
  77. LITE_DESTROY_NETWORK = 12,
  78. };
  79. } // namespace ipc