bactria  0.0.1
The bactria library is a header-only C++14 library for profiling and tracing.
Plugin.hpp
Go to the documentation of this file.
1 /* Copyright 2021 Jan Stephan
2  *
3  * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
4  * the European Commission - subsequent versions of the EUPL (the “Licence”).
5  * You may not use this work except in compliance with the Licence.
6  * You may obtain a copy of the Licence at:
7  *
8  * http://ec.europa.eu/idabc/eupl.html
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the Licence is distributed on an “AS IS” basis, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * Licence permissions and limitations under the Licence.
14  */
22 #pragma once
23 
25 #include <bactria/core/Plugin.hpp>
26 
27 #include <cstdint>
28 #include <cstdlib>
29 #include <stdexcept>
30 #include <string>
31 #include <type_traits>
32 
33 namespace bactria
34 {
35  namespace metrics
36  {
43  namespace plugin
44  {
58  inline auto activated() -> bool
59  {
60  static bool const activated = is_active() && (std::getenv("BACTRIA_METRICS_PLUGIN") != nullptr);
61  return activated;
62  }
63 
64  /* Standard function pointer declaration style is forbidden because of noexcept */
70  using create_sector_t = std::add_pointer_t<void*(char const*, std::uint32_t) noexcept>;
71 
78 
84  using destroy_sector_t = std::add_pointer_t<void(void*) noexcept>;
85 
92 
98  using enter_sector_t = std::add_pointer_t<void(void*, char const*, std::uint32_t, char const*) noexcept>;
99 
106 
112  using leave_sector_t = std::add_pointer_t<void(void*, char const*, std::uint32_t, char const*) noexcept>;
113 
120 
126  using sector_summary_t = std::add_pointer_t<void(void*) noexcept>;
127 
134 
140  using create_phase_t = std::add_pointer_t<void*(char const*) noexcept>;
141 
148 
154  using destroy_phase_t = std::add_pointer_t<void(void*) noexcept>;
155 
162 
168  using enter_phase_t = std::add_pointer_t<void(void*, char const*, std::uint32_t, char const*) noexcept>;
169 
176 
182  using leave_phase_t = std::add_pointer_t<void(void*, char const*, std::uint32_t, char const*) noexcept>;
183 
190 
199  [[nodiscard]] inline auto load() -> plugin_handle_t
200  {
201  auto const path = std::getenv("BACTRIA_METRICS_PLUGIN");
202  if(path != nullptr)
203  {
204  auto handle = system::open_plugin(path);
205 
206  system::load_func(handle, create_sector_ptr, "bactria_metrics_create_sector");
207  system::load_func(handle, destroy_sector_ptr, "bactria_metrics_destroy_sector");
208  system::load_func(handle, enter_sector_ptr, "bactria_metrics_enter_sector");
209  system::load_func(handle, leave_sector_ptr, "bactria_metrics_leave_sector");
210  system::load_func(handle, sector_summary_ptr, "bactria_metrics_sector_summary");
211 
212  system::load_func(handle, create_phase_ptr, "bactria_metrics_create_phase");
213  system::load_func(handle, destroy_phase_ptr, "bactria_metrics_destroy_phase");
214  system::load_func(handle, enter_phase_ptr, "bactria_metrics_enter_phase");
215  system::load_func(handle, leave_phase_ptr, "bactria_metrics_leave_phase");
216 
217  return handle;
218  }
219 
220  // getenv failed
221  throw std::runtime_error{std::string{"Failed to load bactria metrics plugin "} + path};
222  }
223 
231  [[nodiscard, gnu::always_inline]] inline auto create_sector(char const* name, std::uint32_t tag) noexcept
232  {
233  if(create_sector_ptr != nullptr)
234  return (create_sector_ptr) (name, tag);
235 
236  return static_cast<void*>(nullptr);
237  }
238 
246  [[gnu::always_inline]] inline auto destroy_sector(void* sector_handle) noexcept
247  {
248  if(destroy_sector_ptr != nullptr)
249  (destroy_sector_ptr)(sector_handle);
250  }
251 
259  [[gnu::always_inline]] inline auto enter_sector(
260  void* sector_handle,
261  char const* source,
262  std::uint32_t lineno,
263  char const* caller) noexcept
264  {
265  if(enter_sector_ptr != nullptr)
266  (enter_sector_ptr)(sector_handle, source, lineno, caller);
267  }
268 
276  [[gnu::always_inline]] inline auto leave_sector(
277  void* sector_handle,
278  char const* source,
279  std::uint32_t lineno,
280  char const* caller) noexcept
281  {
282  if(leave_sector_ptr != nullptr)
283  (leave_sector_ptr)(sector_handle, source, lineno, caller);
284  }
285 
293  [[gnu::always_inline]] inline auto sector_summary(void* sector_handle) noexcept
294  {
295  if(sector_summary_ptr != nullptr)
296  (sector_summary_ptr)(sector_handle);
297  }
298 
307  [[nodiscard, gnu::always_inline]] inline auto create_phase(char const* name) noexcept
308  {
309  if(create_phase_ptr != nullptr)
310  return (create_phase_ptr) (name);
311 
312  return static_cast<void*>(nullptr);
313  }
314 
322  [[gnu::always_inline]] inline auto destroy_phase(void* phase_handle) noexcept
323  {
324  if(destroy_phase_ptr != nullptr)
325  (destroy_phase_ptr)(phase_handle);
326  }
327 
335  [[gnu::always_inline]] inline auto enter_phase(
336  void* phase_handle,
337  char const* source,
338  std::uint32_t lineno,
339  char const* caller) noexcept
340  {
341  if(enter_phase_ptr != nullptr)
342  (enter_phase_ptr)(phase_handle, source, lineno, caller);
343  }
344 
352  [[gnu::always_inline]] inline auto leave_phase(
353  void* phase_handle,
354  char const* source,
355  std::uint32_t lineno,
356  char const* caller) noexcept
357  {
358  if(leave_phase_ptr != nullptr)
359  (leave_phase_ptr)(phase_handle, source, lineno, caller);
360  }
362  } // namespace plugin
363  } // namespace metrics
364 } // namespace bactria
bactria::metrics::plugin::enter_phase_ptr
auto enter_phase_ptr
Pointer to plugin function bactria_metrics_enter_phase().
Definition: Plugin.hpp:175
Plugin.hpp
bactria-internal plugin handling.
bactria::metrics::plugin::sector_summary_t
std::add_pointer_t< void(void *) noexcept > sector_summary_t
Signature for plugin function bactria_metrics_sector_summary().
Definition: Plugin.hpp:126
bactria::metrics::plugin::destroy_phase_t
std::add_pointer_t< void(void *) noexcept > destroy_phase_t
Signature for plugin function bactria_metrics_destroy_phase().
Definition: Plugin.hpp:154
bactria::metrics::plugin::destroy_sector_ptr
auto destroy_sector_ptr
Pointer to plugin function bactria_metrics_destroy_sector().
Definition: Plugin.hpp:91
bactria::is_active
auto is_active() -> bool
Checks whether the bactria library has been deactivated by the user.
Definition: Activation.hpp:52
bactria::metrics::plugin::create_phase
auto create_phase(char const *name) noexcept
Creates a plugin-specific phase handle.
Definition: Plugin.hpp:307
bactria::metrics::plugin::leave_phase_ptr
auto leave_phase_ptr
Pointer to plugin function bactria_metrics_leave_phase().
Definition: Plugin.hpp:189
bactria::metrics::plugin::create_sector
auto create_sector(char const *name, std::uint32_t tag) noexcept
Creates a plugin-specific sector handle.
Definition: Plugin.hpp:231
bactria::metrics::plugin::create_sector_ptr
auto create_sector_ptr
Pointer to plugin function bactria_metrics_create_sector().
Definition: Plugin.hpp:77
bactria::metrics::plugin::destroy_sector
auto destroy_sector(void *sector_handle) noexcept
Destroys a plugin-specific sector handle.
Definition: Plugin.hpp:246
bactria::metrics::plugin::sector_summary_ptr
auto sector_summary_ptr
Pointer to plugin function bactria_metrics_sector_summary().
Definition: Plugin.hpp:133
bactria::metrics::plugin::enter_sector_t
std::add_pointer_t< void(void *, char const *, std::uint32_t, char const *) noexcept > enter_sector_t
Signature for plugin function bactria_metrics_enter_sector().
Definition: Plugin.hpp:98
bactria::metrics::plugin::enter_sector_ptr
auto enter_sector_ptr
Pointer to plugin function bactria_metrics_enter_sector().
Definition: Plugin.hpp:105
bactria::metrics::plugin::leave_phase_t
std::add_pointer_t< void(void *, char const *, std::uint32_t, char const *) noexcept > leave_phase_t
Signature for plugin function bactria_metrics_leave_phase().
Definition: Plugin.hpp:182
bactria::metrics::plugin::create_sector_t
std::add_pointer_t< void *(char const *, std::uint32_t) noexcept > create_sector_t
Signature for plugin function bactria_metrics_create_sector().
Definition: Plugin.hpp:70
bactria::metrics::plugin::destroy_phase
auto destroy_phase(void *phase_handle) noexcept
Destroys a plugin-specific phase handle.
Definition: Plugin.hpp:322
bactria::metrics::plugin::leave_sector_ptr
auto leave_sector_ptr
Pointer to plugin function bactria_metrics_leave_sector().
Definition: Plugin.hpp:119
bactria::system::load_func
auto load_func(plugin_handle_t handle, Sig &ptr, const char *name)
The POSIX-specific function loader.
Definition: POSIX.hpp:80
bactria::reports::plugin::activated
auto activated() -> bool
Checks for an active reports plugin.
Definition: Plugin.hpp:60
bactria::metrics::plugin::sector_summary
auto sector_summary(void *sector_handle) noexcept
Plugin-specific sector summary.
Definition: Plugin.hpp:293
bactria::system::open_plugin
auto open_plugin(const char *path)
The POSIX-specific plugin loader.
Definition: POSIX.hpp:57
bactria::metrics::plugin::leave_phase
auto leave_phase(void *phase_handle, char const *source, std::uint32_t lineno, char const *caller) noexcept
Plugin-specific phase leaving.
Definition: Plugin.hpp:352
bactria::plugin_handle_t
system::plugin_handle_t plugin_handle_t
The platform-specific plugin handle type.
Definition: Plugin.hpp:41
bactria::metrics::plugin::create_phase_ptr
auto create_phase_ptr
Pointer to plugin function bactria_metrics_create_phase().
Definition: Plugin.hpp:147
bactria::metrics::plugin::leave_sector_t
std::add_pointer_t< void(void *, char const *, std::uint32_t, char const *) noexcept > leave_sector_t
Signature for plugin function bactria_metrics_leave_sector().
Definition: Plugin.hpp:112
bactria::metrics::plugin::leave_sector
auto leave_sector(void *sector_handle, char const *source, std::uint32_t lineno, char const *caller) noexcept
Plugin-specific sector leaving.
Definition: Plugin.hpp:276
bactria::metrics::plugin::enter_sector
auto enter_sector(void *sector_handle, char const *source, std::uint32_t lineno, char const *caller) noexcept
Plugin-specific sector entering.
Definition: Plugin.hpp:259
bactria::metrics::plugin::destroy_sector_t
std::add_pointer_t< void(void *) noexcept > destroy_sector_t
Signature for plugin function bactria_metrics_destroy_sector().
Definition: Plugin.hpp:84
bactria::reports::plugin::load
auto load() -> plugin_handle_t
Initializes the reports plugin.
Definition: Plugin.hpp:285
bactria::metrics::plugin::create_phase_t
std::add_pointer_t< void *(char const *) noexcept > create_phase_t
Signature for plugin function bactria_metrics_create_phase().
Definition: Plugin.hpp:140
bactria::metrics::plugin::enter_phase_t
std::add_pointer_t< void(void *, char const *, std::uint32_t, char const *) noexcept > enter_phase_t
Signature for plugin function bactria_metrics_enter_phase().
Definition: Plugin.hpp:168
Activation.hpp
Plugin activation file.
bactria::metrics::plugin::enter_phase
auto enter_phase(void *phase_handle, char const *source, std::uint32_t lineno, char const *caller) noexcept
Plugin-specific phase entering.
Definition: Plugin.hpp:335
bactria::metrics::plugin::destroy_phase_ptr
auto destroy_phase_ptr
Pointer to plugin function bactria_metrics_destroy_phase().
Definition: Plugin.hpp:161