Skip to content

Commit 7965c84

Browse files
committed
Instance: move the /dev/cachefiles code to class DevCachefiles
1 parent 2f70bea commit 7965c84

File tree

7 files changed

+78
-43
lines changed

7 files changed

+78
-43
lines changed

src/Cull.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "Cull.hxx"
66
#include "Walk.hxx"
7+
#include "DevCachefiles.hxx"
78
#include "io/uring/CoOperation.hxx"
89
#include "system/Error.hxx"
910
#include "co/InvokeTask.hxx"
@@ -73,7 +74,7 @@ class Cull::Operation final
7374
};
7475

7576
Cull::Cull(EventLoop &event_loop, Uring::Queue &_uring,
76-
FileDescriptor _dev_cachefiles,
77+
DevCachefiles &_dev_cachefiles,
7778
uint_least64_t _cull_files, std::size_t _cull_bytes,
7879
Callback _callback)
7980
:uring(_uring), dev_cachefiles(_dev_cachefiles),

src/Cull.hxx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#pragma once
66

7-
#include "DevCachefiles.hxx"
87
#include "WHandler.hxx"
98
#include "Chdir.hxx"
109
#include "event/DeferEvent.hxx"
@@ -20,6 +19,7 @@
2019

2120
namespace Co { class InvokeTask; }
2221
namespace Uring { class Queue; }
22+
class DevCachefiles;
2323
class Walk;
2424
class WalkDirectoryRef;
2525

@@ -30,7 +30,7 @@ class WalkDirectoryRef;
3030
*/
3131
class Cull final : WalkHandler {
3232
Uring::Queue ů
33-
DevCachefiles dev_cachefiles;
33+
DevCachefiles &dev_cachefiles;
3434

3535
using Callback = BoundMethod<void() noexcept>;
3636
const Callback callback;
@@ -56,7 +56,7 @@ class Cull final : WalkHandler {
5656
public:
5757
[[nodiscard]]
5858
Cull(EventLoop &event_loop, Uring::Queue &_uring,
59-
FileDescriptor _dev_cachefiles,
59+
DevCachefiles &_dev_cachefiles,
6060
std::size_t _cull_files, uint_least64_t _cull_bytes,
6161
Callback _callback);
6262
~Cull() noexcept;

src/DevCachefiles.cxx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,54 @@
33
// author: Max Kellermann <max.kellermann@ionos.com>
44

55
#include "DevCachefiles.hxx"
6+
#include "io/UniqueFileDescriptor.hxx"
7+
#include "util/IterableSplitString.hxx"
68
#include "util/SpanCast.hxx"
9+
#include "util/StringSplit.hxx"
710

811
#include <fmt/core.h>
912

1013
#include <string.h> // for strerror()
1114

15+
using std::string_view_literals::operator""sv;
16+
17+
DevCachefiles::DevCachefiles(EventLoop &event_loop, UniqueFileDescriptor _fd,
18+
BoundMethod<void() noexcept> _cull_callback) noexcept
19+
:device(event_loop, BIND_THIS_METHOD(OnDeviceReady), _fd.Release()),
20+
cull_callback(_cull_callback)
21+
{
22+
device.ScheduleRead();
23+
}
24+
25+
DevCachefiles::~DevCachefiles() noexcept
26+
{
27+
device.Close();
28+
}
29+
30+
void
31+
DevCachefiles::OnDeviceReady([[maybe_unused]] unsigned events) noexcept
32+
{
33+
char buffer[1024];
34+
ssize_t nbytes = GetFileDescriptor().Read(std::as_writable_bytes(std::span{buffer}));
35+
if (nbytes <= 0) {
36+
// TODO
37+
Disable();
38+
return;
39+
}
40+
41+
const std::string_view line{buffer, static_cast<std::size_t>(nbytes)};
42+
43+
bool start_cull = false;
44+
for (const std::string_view i : IterableSplitString(line, ' ')) {
45+
const auto [name, value] = Split(i, '=');
46+
if (name == "cull"sv)
47+
start_cull = value != "0"sv;
48+
}
49+
50+
if (start_cull)
51+
cull_callback();
52+
}
53+
1254
std::span<const std::byte>
1355
DevCachefiles::FormatCullFile(Buffer &buffer, std::string_view filename) noexcept
1456
{

src/DevCachefiles.hxx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#pragma once
66

7-
#include "io/FileDescriptor.hxx"
7+
#include "event/PipeEvent.hxx"
88
#include "util/StringBuffer.hxx"
99

1010
#include <cstddef>
@@ -13,22 +13,32 @@
1313

1414
#include <limits.h> // for NAME_MAX
1515

16+
class UniqueFileDescriptor;
17+
1618
/**
1719
* OO wrapper for a /dev/cachefiles file descriptor (non-owning).
1820
*/
1921
class DevCachefiles {
20-
FileDescriptor fd;
22+
PipeEvent device;
23+
24+
BoundMethod<void() noexcept> cull_callback;
2125

2226
public:
2327
[[nodiscard]]
24-
explicit DevCachefiles(FileDescriptor _fd) noexcept
25-
:fd(_fd) {}
28+
DevCachefiles(EventLoop &event_loop, UniqueFileDescriptor _fd,
29+
BoundMethod<void() noexcept> _cull_callback) noexcept;
30+
31+
~DevCachefiles() noexcept;
2632

2733
DevCachefiles(const DevCachefiles &) = delete;
2834
DevCachefiles &operator=(const DevCachefiles &) = delete;
2935

3036
FileDescriptor GetFileDescriptor() const noexcept {
31-
return fd;
37+
return device.GetFileDescriptor();
38+
}
39+
40+
void Disable() noexcept {
41+
device.Cancel();
3242
}
3343

3444
using Buffer = StringBuffer<NAME_MAX + 8>;
@@ -43,4 +53,7 @@ public:
4353

4454
[[nodiscard]]
4555
static CullResult CheckCullFileResult(std::string_view filename, int res) noexcept;
56+
57+
private:
58+
void OnDeviceReady(unsigned events) noexcept;
4659
};

src/Instance.hxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#pragma once
66

7+
#include "DevCachefiles.hxx"
78
#include "Cull.hxx"
89
#include "event/Loop.hxx"
910
#include "event/PipeEvent.hxx"
@@ -29,7 +30,7 @@ class Instance {
2930

3031
UniqueFileDescriptor cache_fd, graveyard_fd;
3132

32-
PipeEvent dev_cachefiles{event_loop, BIND_THIS_METHOD(OnDevCachefiles)};
33+
DevCachefiles dev_cachefiles;
3334

3435
std::optional<Cull> cull;
3536

@@ -44,14 +45,13 @@ public:
4445
void Run();
4546

4647
private:
47-
void OnDevCachefiles(unsigned events) noexcept;
48-
48+
void OnCull() noexcept;
4949
void StartCull();
5050
void OnCullComplete() noexcept;
5151

5252
void OnShutdown() noexcept {
5353
cull.reset();
54-
dev_cachefiles.Cancel();
54+
dev_cachefiles.Disable();
5555

5656
#ifdef HAVE_LIBSYSTEMD
5757
systemd_watchdog.Disable();

src/Main.cxx

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99
#include "system/SetupProcess.hxx"
1010
#include "io/Open.hxx"
1111
#include "io/uring/Queue.hxx"
12-
#include "util/IterableSplitString.hxx"
1312
#include "util/PrintException.hxx"
1413
#include "util/SpanCast.hxx"
15-
#include "util/StringSplit.hxx"
1614
#include "config.h"
1715

1816
#include <fmt/core.h>
@@ -55,15 +53,13 @@ OpenDevCachefiles(const Config &config)
5553

5654
inline
5755
Instance::Instance(const Config &config)
58-
:brun(config.brun), frun(config.frun),
56+
:dev_cachefiles(event_loop, OpenDevCachefiles(config), BIND_THIS_METHOD(OnCull)),
57+
brun(config.brun), frun(config.frun),
5958
culling_disabled(config.culling_disabled)
6059
{
6160
event_loop.EnableUring(16384, IORING_SETUP_SINGLE_ISSUER|IORING_SETUP_COOP_TASKRUN);
6261
event_loop.GetUring()->SetMaxWorkers(16, 16);
6362

64-
dev_cachefiles.Open(OpenDevCachefiles(config).Release());
65-
dev_cachefiles.ScheduleRead();
66-
6763
const auto fscache_fd = OpenPath(config.dir.c_str(), O_DIRECTORY);
6864
cache_fd = OpenPath(fscache_fd, "cache", O_DIRECTORY);
6965
graveyard_fd = OpenPath(fscache_fd, "graveyard", O_DIRECTORY);
@@ -76,7 +72,6 @@ Instance::Instance(const Config &config)
7672
inline
7773
Instance::~Instance() noexcept
7874
{
79-
dev_cachefiles.Close();
8075
}
8176

8277
inline void
@@ -101,7 +96,7 @@ Instance::StartCull()
10196
fmt::print(stderr, "Cull: start files={} bytes={}\n", cull_files, cull_bytes);
10297

10398
cull.emplace(event_loop, *event_loop.GetUring(),
104-
dev_cachefiles.GetFileDescriptor(),
99+
dev_cachefiles,
105100
cull_files, cull_bytes, BIND_THIS_METHOD(OnCullComplete));
106101
cull->Start(cache_fd);
107102
}
@@ -110,31 +105,12 @@ inline void
110105
Instance::OnCullComplete() noexcept
111106
{
112107
cull.reset();
113-
114-
dev_cachefiles.ScheduleRead();
115108
}
116109

117110
inline void
118-
Instance::OnDevCachefiles([[maybe_unused]] unsigned events) noexcept
111+
Instance::OnCull() noexcept
119112
{
120-
char buffer[1024];
121-
ssize_t nbytes = dev_cachefiles.GetFileDescriptor().Read(std::as_writable_bytes(std::span{buffer}));
122-
if (nbytes <= 0) {
123-
// TODO
124-
dev_cachefiles.CancelRead();
125-
return;
126-
}
127-
128-
const std::string_view line{buffer, static_cast<std::size_t>(nbytes)};
129-
130-
bool start_cull = false;
131-
for (const std::string_view i : IterableSplitString(line, ' ')) {
132-
const auto [name, value] = Split(i, '=');
133-
if (name == "cull"sv)
134-
start_cull = value != "0"sv;
135-
}
136-
137-
if (start_cull && !cull && !culling_disabled)
113+
if (!cull && !culling_disabled)
138114
StartCull();
139115
}
140116

test/RunCull.cxx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// author: Max Kellermann <max.kellermann@ionos.com>
44

55
#include "Cull.hxx"
6+
#include "DevCachefiles.hxx"
67
#include "event/Loop.hxx"
78
#include "event/ShutdownListener.hxx"
89
#include "system/Error.hxx"
@@ -39,7 +40,7 @@ struct Instance final {
3940
EventLoop event_loop;
4041
ShutdownListener shutdown_listener{event_loop, BIND_THIS_METHOD(OnShutdown)};
4142

42-
const UniqueFileDescriptor dev_cachefiles = OpenDevCachefiles();
43+
DevCachefiles dev_cachefiles{event_loop, OpenDevCachefiles(), BIND_THIS_METHOD(OnCull)};
4344

4445
std::optional<Cull> cull;
4546

@@ -56,6 +57,8 @@ struct Instance final {
5657
cull.reset();
5758
}
5859

60+
void OnCull() noexcept {}
61+
5962
void OnCullComplete() noexcept {
6063
cull.reset();
6164
shutdown_listener.Disable();

0 commit comments

Comments
 (0)