iceoryx_doc  1.0.1
roudi.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_POSH_ROUDI_ROUDI_MULTI_PROCESS_HPP
18 #define IOX_POSH_ROUDI_ROUDI_MULTI_PROCESS_HPP
19 
20 #include "iceoryx_posh/iceoryx_posh_types.hpp"
21 #include "iceoryx_posh/internal/capro/capro_message.hpp"
22 #include "iceoryx_posh/internal/roudi/introspection/mempool_introspection.hpp"
23 #include "iceoryx_posh/internal/roudi/process_manager.hpp"
24 #include "iceoryx_posh/internal/runtime/ipc_interface_creator.hpp"
25 #include "iceoryx_posh/mepoo/mepoo_config.hpp"
26 #include "iceoryx_posh/roudi/memory/roudi_memory_interface.hpp"
27 #include "iceoryx_posh/roudi/memory/roudi_memory_manager.hpp"
28 #include "iceoryx_posh/roudi/roudi_app.hpp"
29 #include "iceoryx_utils/cxx/generic_raii.hpp"
30 #include "iceoryx_utils/internal/concurrent/smart_lock.hpp"
31 #include "iceoryx_utils/internal/relocatable_pointer/relative_pointer.hpp"
32 #include "iceoryx_utils/platform/file.hpp"
33 #include "iceoryx_utils/posix_wrapper/posix_access_rights.hpp"
34 
35 #include <cstdint>
36 #include <cstdio>
37 #include <thread>
38 
39 namespace iox
40 {
41 namespace roudi
42 {
43 using namespace iox::units::duration_literals;
44 
45 class RouDi
46 {
47  public:
51  {
52  IMMEDIATE,
53  DEFER_START
54  };
55 
57  {
59  const roudi::MonitoringMode monitoringMode = roudi::MonitoringMode::ON,
60  const bool killProcessesInDestructor = true,
61  const RuntimeMessagesThreadStart RuntimeMessagesThreadStart = RuntimeMessagesThreadStart::IMMEDIATE,
62  const version::CompatibilityCheckLevel compatibilityCheckLevel = version::CompatibilityCheckLevel::PATCH,
63  const units::Duration processKillDelay = roudi::PROCESS_DEFAULT_KILL_DELAY) noexcept
64  : m_monitoringMode(monitoringMode)
65  , m_killProcessesInDestructor(killProcessesInDestructor)
66  , m_runtimesMessagesThreadStart(RuntimeMessagesThreadStart)
67  , m_compatibilityCheckLevel(compatibilityCheckLevel)
68  , m_processKillDelay(processKillDelay)
69  {
70  }
71 
72  const roudi::MonitoringMode m_monitoringMode;
73  const bool m_killProcessesInDestructor;
74  const RuntimeMessagesThreadStart m_runtimesMessagesThreadStart;
75  const version::CompatibilityCheckLevel m_compatibilityCheckLevel;
76  const units::Duration m_processKillDelay;
77  };
78 
79  RouDi& operator=(const RouDi& other) = delete;
80  RouDi(const RouDi& other) = delete;
81 
82  RouDi(RouDiMemoryInterface& roudiMemoryInteface,
83  PortManager& portManager,
84  RoudiStartupParameters roudiStartupParameters);
85 
86  virtual ~RouDi();
87 
88  protected:
92 
97  void shutdown();
98  virtual void processMessage(const runtime::IpcMessage& message,
99  const iox::runtime::IpcMessageType& cmd,
100  const RuntimeName_t& runtimeName);
101  virtual void cyclicUpdateHook();
102  void IpcMessageErrorHandler();
103 
104  version::VersionInfo parseRegisterMessage(const runtime::IpcMessage& message,
105  uint32_t& pid,
106  uid_t& userId,
107  int64_t& transmissionTimestamp);
108 
116  void registerProcess(const RuntimeName_t& name,
117  const uint32_t pid,
118  const posix::PosixUser user,
119  const int64_t transmissionTimestamp,
120  const uint64_t sessionId,
121  const version::VersionInfo& versionInfo);
122 
125  static uint64_t getUniqueSessionIdForProcess();
126 
127  private:
128  void processRuntimeMessages();
129 
130  void monitorAndDiscoveryUpdate();
131 
132  cxx::GenericRAII m_unregisterRelativePtr{[] {}, [] { rp::BaseRelativePointer::unregisterAll(); }};
133  bool m_killProcessesInDestructor;
134  std::atomic_bool m_runMonitoringAndDiscoveryThread;
135  std::atomic_bool m_runHandleRuntimeMessageThread;
136 
137  const units::Duration m_runtimeMessagesThreadTimeout{100_ms};
138  protected:
139  RouDiMemoryInterface* m_roudiMemoryInterface{nullptr};
143  cxx::GenericRAII m_roudiMemoryManagerCleaner{[]() {},
144  [this]() {
145  if (this->m_roudiMemoryInterface->destroyMemory().has_error())
146  {
147  LogWarn() << "unable to cleanup roudi memory interface";
148  };
149  }};
150  PortManager* m_portManager{nullptr};
151  concurrent::smart_lock<ProcessManager> m_prcMgr;
152 
153  private:
154  std::thread m_monitoringAndDiscoveryThread;
155  std::thread m_handleRuntimeMessageThread;
156 
157  protected:
158  ProcessIntrospectionType m_processIntrospection;
159  MemPoolIntrospectionType m_mempoolIntrospection;
160 
161  private:
162  roudi::MonitoringMode m_monitoringMode{roudi::MonitoringMode::ON};
163  units::Duration m_processKillDelay;
164 };
165 
166 } // namespace roudi
167 } // namespace iox
168 
169 #endif // IOX_POSH_ROUDI_ROUDI_MULTI_PROCESS_HPP
Definition: port_manager.hpp:55
Definition: roudi_memory_interface.hpp:36
Definition: roudi.hpp:46
RuntimeMessagesThreadStart
Indicate whether the thread processing messages from the runtimes will start directly or deferred thi...
Definition: roudi.hpp:51
static uint64_t getUniqueSessionIdForProcess()
Creates a unique ID which can be used to check outdated IPC channel transmissions.
void shutdown()
Stops threads and kills all process known to RouDi Called in d'tor.
void startProcessRuntimeMessagesThread()
Starts the thread processing messages from the runtimes Once this is done, applications can register ...
void registerProcess(const RuntimeName_t &name, const uint32_t pid, const posix::PosixUser user, const int64_t transmissionTimestamp, const uint64_t sessionId, const version::VersionInfo &versionInfo)
Handles the registration request from process.
Definition: ipc_message.hpp:43
MonitoringMode
Controls process alive monitoring. Upon timeout, a monitored process is removed and its resources are...
Definition: iceoryx_posh_types.hpp:221
ProcessIntrospection< PublisherPortUserType > ProcessIntrospectionType
typedef for the templated process introspection class that is used by RouDi for the actual process in...
Definition: process_introspection.hpp:110
MemPoolIntrospection< mepoo::MemoryManager, mepoo::SegmentManager<>, PublisherPortUserType > MemPoolIntrospectionType
typedef for the templated mempool introspection class that is used by RouDi for the actual mempool in...
Definition: mempool_introspection.hpp:97
Definition: service_description.hpp:29