summaryrefslogtreecommitdiffstats
path: root/src/common/TracepointProvider.h
blob: fe447677ccb3104d5f3f74e6e5b8918fe827f4dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_TRACEPOINT_PROVIDER_H
#define CEPH_TRACEPOINT_PROVIDER_H

#include "common/ceph_context.h"
#include "common/config_obs.h"
#include "common/ceph_mutex.h"
#include "include/dlfcn_compat.h"

class TracepointProvider : public md_config_obs_t {
public:
  struct Traits {
    const char *library;
    const char *config_key;

    Traits(const char *library, const char *config_key)
      : library(library), config_key(config_key) {
    }
  };

  class Singleton {
  public:
    Singleton(CephContext *cct, const char *library, const char *config_key)
      : tracepoint_provider(new TracepointProvider(cct, library, config_key)) {
    }
    ~Singleton() {
      delete tracepoint_provider;
    }

    inline bool is_enabled() const {
      return tracepoint_provider->m_handle != nullptr;
    }
  private:
    TracepointProvider *tracepoint_provider;
  };

  template <const Traits &traits>
  class TypedSingleton : public Singleton {
  public:
    explicit TypedSingleton(CephContext *cct)
      : Singleton(cct, traits.library, traits.config_key) {
    }
  };

  TracepointProvider(CephContext *cct, const char *library,
                     const char *config_key);
  ~TracepointProvider() override;

  TracepointProvider(const TracepointProvider&) = delete;
  TracepointProvider operator =(const TracepointProvider&) = delete;
  TracepointProvider(TracepointProvider&&) = delete;
  TracepointProvider operator =(TracepointProvider&&) = delete;

  template <const Traits &traits>
  static void initialize(CephContext *cct) {
#ifdef WITH_LTTNG
     cct->lookup_or_create_singleton_object<TypedSingleton<traits>>(
       traits.library, false, cct);
#endif
  }

protected:
  const char** get_tracked_conf_keys() const override {
    return m_config_keys;
  }
  void handle_conf_change(const ConfigProxy& conf,
			  const std::set <std::string> &changed) override;

private:
  CephContext *m_cct;
  std::string m_library;
  mutable const char* m_config_keys[2];

  ceph::mutex m_lock = ceph::make_mutex("TracepointProvider::m_lock");
  void* m_handle = nullptr;

  void verify_config(const ConfigProxy& conf);
};

#endif // CEPH_TRACEPOINT_PROVIDER_H