summaryrefslogtreecommitdiffstats
path: root/src/rgw/driver/posix/notify.h
blob: 4463abc57c2e95d98cba271e0f90de8be96761d2 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

#pragma once

#include <iostream>
#include <string>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <optional>
#include <filesystem>
#include <limits>
#include <cstdlib>
#include "unordered_dense.h"
#include <unistd.h>
#include <poll.h>
#ifdef __linux__
#include <sys/inotify.h>
#include <sys/eventfd.h>
#endif
#include <fmt/format.h>

namespace file::listing {

  namespace sf = std::filesystem;

  class Notifiable
  {
  public:
    enum class EventType : uint8_t
    {
      ADD = 0,
      REMOVE,
      INVALIDATE
    };

    struct Event
    {
      EventType type;
      std::optional<std::string_view> name;

      Event(EventType type, std::optional<std::string_view> name) noexcept
	: type(type), name(name)
	{}

      Event(Event&& rhs) noexcept
	: type(rhs.type), name(rhs.name)
	{}
    };

    virtual ~Notifiable() {};

    virtual int notify(const std::string&, void*, const std::vector<Event>&) = 0;
  };

  class Notify
  {
    Notifiable* n;
    sf::path rp;

    Notify(Notifiable* n, const std::string& bucket_root)
      : n(n), rp(bucket_root)
      {}

    friend class Inotify;
  public:
    static std::unique_ptr<Notify> factory(Notifiable* n, const std::string& bucket_root);
    
    virtual int add_watch(const std::string& dname, void* opaque) = 0;
    virtual int remove_watch(const std::string& dname) = 0;
    virtual ~Notify()
      {}
  }; /* Notify */

#ifdef __linux__
  class Inotify : public Notify
  {
    static constexpr uint32_t rd_size = 8192;
    static constexpr uint32_t aw_mask = IN_ALL_EVENTS &
      ~(IN_MOVE_SELF|IN_OPEN|IN_ACCESS|IN_ATTRIB|IN_CLOSE_WRITE|IN_CLOSE_NOWRITE|IN_MODIFY|IN_DELETE_SELF);

    static constexpr uint64_t sig_shutdown = std::numeric_limits<uint64_t>::max() - 0xdeadbeef;

    class WatchRecord
    {
    public:
      int wd;
      std::string name;
      void* opaque;
    public:
      WatchRecord(int wd, const std::string& name, void* opaque) noexcept
	: wd(wd), name(name), opaque(opaque)
	{}

      WatchRecord(WatchRecord&& wr) noexcept
	: wd(wr.wd), name(wr.name), opaque(wr.opaque)
	{}

      WatchRecord& operator=(WatchRecord&& wr) {
	wd = wr.wd;
	name = std::move(wr.name);
	opaque = wr.opaque;
	return *this;
      }
    }; /* WatchRecord */

    using wd_callback_map_t = ankerl::unordered_dense::map<int, WatchRecord>;
    using wd_remove_map_t = ankerl::unordered_dense::map<std::string, int>;

    int wfd, efd;
    std::thread thrd;
    wd_callback_map_t wd_callback_map;
    wd_remove_map_t wd_remove_map;
    bool shutdown{false};

    class AlignedBuf
    {
      char* m;
    public:
      AlignedBuf() {
	m = static_cast<char*>(aligned_alloc(__alignof__(struct inotify_event), rd_size));
	if (! m) [[unlikely]] {
	  std::cerr << fmt::format("{} buffer allocation failure", __func__) << std::endl;
	  abort();
	}
      }
      ~AlignedBuf() {
	std::free(m);
      }
      char* get() {
	return m;
      }
    }; /* AlignedBuf */
    
    void ev_loop() {
      std::unique_ptr<AlignedBuf> up_buf = std::make_unique<AlignedBuf>();
      struct inotify_event* event;
      char* buf = up_buf.get()->get();
      ssize_t len;
      int npoll;

      nfds_t nfds{2};
      struct pollfd fds[2] = {{wfd, POLLIN}, {efd, POLLIN}};

    restart:
      while(! shutdown) {
	npoll = poll(fds, nfds, -1); /* for up to 10 fds, poll is fast as epoll */
	if (shutdown) {
	  return;
	}
	if (npoll == -1) {
	  if (errno == EINTR) {
	    continue;
	  }
	  // XXX
	}
	if (npoll > 0) {
	  len = read(wfd, buf, rd_size);
	  if (len == -1) {
	    continue; // hopefully, was EAGAIN
	  }
	  std::vector<Notifiable::Event> evec;
	  for (char* ptr = buf; ptr < buf + len;
	       ptr += sizeof(struct inotify_event) + event->len) {
	    event = reinterpret_cast<struct inotify_event*>(ptr);
	    const auto& it = wd_callback_map.find(event->wd);
	    //std::cout << fmt::format("event! {}", event->name) << std::endl;
	    if (it == wd_callback_map.end()) [[unlikely]] {
	      /* non-destructive race, it happens */
	      continue;
	    }
	    const auto& wr = it->second;
	    if (event->mask & IN_Q_OVERFLOW) [[unlikely]] {
	      /* cache blown, invalidate */
	      evec.clear();
	      evec.emplace_back(Notifiable::Event(Notifiable::EventType::INVALIDATE, std::nullopt));
	      n->notify(wr.name, wr.opaque, evec);
	      goto restart;
	    } else {
	      if ((event->mask & IN_CREATE) ||
		  (event->mask & IN_MOVED_TO)) {
		/* new object in dir */
		evec.emplace_back(Notifiable::Event(Notifiable::EventType::ADD, event->name));
	      } else if ((event->mask & IN_DELETE) ||
			 (event->mask & IN_MOVED_FROM)) {
		/* object removed from dir */
		evec.emplace_back(Notifiable::Event(Notifiable::EventType::REMOVE, event->name));
	      }
	    } /* !overflow */
	    if (evec.size() > 0) {
	      n->notify(wr.name, wr.opaque, evec);
	    }
	  } /* events */
	} /* n > 0 */
      }
    } /* ev_loop */

    Inotify(Notifiable* n, const std::string& bucket_root)
      : Notify(n, bucket_root),
	thrd(&Inotify::ev_loop, this)
      {
	wfd = inotify_init1(IN_NONBLOCK);
	if (wfd == -1) {
	  std::cerr << fmt::format("{} inotify_init1 failed with {}", __func__, wfd) << std::endl;
	  exit(1);
	}
	efd = eventfd(0, EFD_NONBLOCK);
      }

    void signal_shutdown() {
      uint64_t msg{sig_shutdown};
      std::ignore = write(efd, &msg, sizeof(uint64_t));
    }

    friend class Notify;
  public:
    virtual int add_watch(const std::string& dname, void* opaque) override {
      sf::path wp{rp / dname};
      int wd = inotify_add_watch(wfd, wp.c_str(), aw_mask);
      if (wd == -1) {
	std::cerr << fmt::format("{} inotify_add_watch {} failed with {}", __func__, dname, wd) << std::endl;
      } else {
	wd_callback_map.insert(wd_callback_map_t::value_type(wd, WatchRecord(wd, dname, opaque)));
	wd_remove_map.insert(wd_remove_map_t::value_type(dname, wd));
      }
      return wd;
    }

    virtual int remove_watch(const std::string& dname) override {
      int r{0};
      const auto& elt = wd_remove_map.find(dname);
      if (elt != wd_remove_map.end()) {
	auto& wd = elt->second;
	r = inotify_rm_watch(wfd, wd);
	if (r == -1) {
	  std::cerr << fmt::format("{} inotify_rm_watch {} failed with {}", __func__, dname, wd) << std::endl;
	}
	wd_callback_map.erase(wd);
	wd_remove_map.erase(std::string(dname));
      }
      return r;
    }

    virtual ~Inotify() {
      shutdown = true;
      signal_shutdown();
      thrd.join();
    }
  };
#endif /* linux */

} // namespace file::listing