Repository navigation
Expand file tree
/
Copy pathbackend_ipc.h
More file actions
184 lines (159 loc) · 5.74 KB
/
Copy pathbackend_ipc.h
File metadata and controls
184 lines (159 loc) · 5.74 KB
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
// backend_ipc.h - generic backend IPC process launcher.
//
// Owns the out-of-process backend daemon lifecycle: fork/exec, command pipe,
// parent->daemon stream payload, optional shared payload buffer, binary status
// stream, and scratch work directory. Individual IPC modes keep their own
// payload protocol on top of this process wrapper.
#pragma once
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <limits>
#include <string>
#include <vector>
#if !defined(_WIN32)
# include <sys/types.h>
#endif
namespace luce::common {
enum class BackendIpcMode {
Invalid,
DFlashDraft,
PFlashCompress,
Qwen35TargetShard,
Gemma4TargetShard,
LagunaTargetShard,
MoeExpertCompute,
DeepSeek4TargetShard,
};
const char * backend_ipc_mode_name(BackendIpcMode mode);
bool parse_backend_ipc_mode(const std::string & value, BackendIpcMode & out);
enum class BackendIpcPayloadTransport {
Stream,
Shared,
Auto,
};
const char * backend_ipc_payload_transport_name(BackendIpcPayloadTransport transport);
bool parse_backend_ipc_payload_transport(const std::string & value,
BackendIpcPayloadTransport & out);
inline bool backend_ipc_checked_add_size(size_t a, size_t b, size_t & out) {
if (a > std::numeric_limits<size_t>::max() - b) {
return false;
}
out = a + b;
return true;
}
inline bool backend_ipc_payload_in_bounds(size_t offset,
size_t bytes,
size_t capacity) {
size_t end = 0;
return backend_ipc_checked_add_size(offset, bytes, end) && end <= capacity;
}
struct BackendIpcSharedPayloadHeader {
uint64_t sequence = 0;
uint64_t bytes = 0;
};
inline void backend_ipc_publish_shared_payload_header(
BackendIpcSharedPayloadHeader * header,
uint64_t sequence,
uint64_t bytes) {
#if defined(__GNUC__) || defined(__clang__)
__atomic_store_n(&header->bytes, bytes, __ATOMIC_RELAXED);
__atomic_store_n(&header->sequence, sequence, __ATOMIC_RELEASE);
#else
header->bytes = bytes;
std::atomic_thread_fence(std::memory_order_release);
header->sequence = sequence;
#endif
}
inline void backend_ipc_load_shared_payload_header(
const BackendIpcSharedPayloadHeader * header,
uint64_t & sequence,
uint64_t & bytes) {
#if defined(__GNUC__) || defined(__clang__)
sequence = __atomic_load_n(&header->sequence, __ATOMIC_ACQUIRE);
bytes = __atomic_load_n(&header->bytes, __ATOMIC_RELAXED);
#else
sequence = header->sequence;
std::atomic_thread_fence(std::memory_order_acquire);
bytes = header->bytes;
#endif
}
inline bool backend_ipc_shared_payload_header_matches(
const BackendIpcSharedPayloadHeader * header,
uint64_t sequence,
uint64_t bytes) {
if (!header || sequence == 0) return false;
uint64_t published_sequence = 0;
uint64_t published_bytes = 0;
backend_ipc_load_shared_payload_header(
header, published_sequence, published_bytes);
return published_sequence == sequence && published_bytes == bytes;
}
inline size_t backend_ipc_shared_payload_header_bytes() {
return sizeof(BackendIpcSharedPayloadHeader);
}
inline bool backend_ipc_shared_payload_map_bytes(size_t payload_bytes,
size_t & map_bytes) {
return backend_ipc_checked_add_size(
backend_ipc_shared_payload_header_bytes(), payload_bytes, map_bytes);
}
struct BackendIpcLaunchConfig {
std::string bin;
BackendIpcMode mode = BackendIpcMode::DFlashDraft;
std::string payload_path;
std::vector<std::string> args;
std::string work_dir;
BackendIpcPayloadTransport payload_transport = BackendIpcPayloadTransport::Auto;
size_t shared_payload_bytes = 0;
};
struct BackendIpcPayloadSegment {
const void * data = nullptr;
size_t bytes = 0;
};
class BackendIpcProcess {
public:
BackendIpcProcess() = default;
BackendIpcProcess(const BackendIpcProcess &) = delete;
BackendIpcProcess & operator=(const BackendIpcProcess &) = delete;
~BackendIpcProcess() { close(); }
bool start(const BackendIpcLaunchConfig & cfg);
void close();
bool active() const { return active_; }
FILE * command_stream() const { return cmd_; }
int payload_fd() const { return payload_fd_; }
int stream_fd() const { return stream_fd_; }
BackendIpcPayloadTransport resolved_payload_transport() const {
return resolved_payload_transport_;
}
bool has_shared_payload() const { return shared_payload_map_ != nullptr; }
size_t shared_payload_capacity() const { return shared_payload_capacity_; }
const std::string & work_dir() const { return work_dir_; }
std::string next_path(const char * prefix);
bool write_shared_payload(const void * data, size_t bytes, uint64_t & seq);
bool write_shared_payload_segments(const BackendIpcPayloadSegment * segments,
size_t n_segments,
uint64_t & seq);
bool read_shared_payload(void * data, size_t bytes, uint64_t seq) const;
private:
#if !defined(_WIN32)
bool init_work_dir(const std::string & requested);
bool init_shared_payload(size_t bytes);
pid_t pid_ = -1;
#endif
FILE * cmd_ = nullptr;
int payload_fd_ = -1;
int stream_fd_ = -1;
int shared_payload_fd_ = -1;
void * shared_payload_map_ = nullptr;
size_t shared_payload_bytes_ = 0;
size_t shared_payload_capacity_ = 0;
uint64_t shared_payload_seq_ = 0;
BackendIpcPayloadTransport resolved_payload_transport_ =
BackendIpcPayloadTransport::Stream;
std::string work_dir_;
int seq_ = 0;
bool owns_work_dir_ = false;
bool active_ = false;
};
} // namespace luce::common