22#define WRITE_REQUEST_CACHE_FS_HPP
33
44class WriteRequestCacheFS {
5- int current_fd = -1 ;
6- capio_off64_t current_size = 0 ;
5+ std::unordered_map<std::string, capio_off64_t > capio_internal_write_offsets;
76
87 const capio_off64_t _max_size;
98
109 std::filesystem::path current_path;
1110
1211 // non-blocking as write is not in the pre port of CAPIO semantics
13- inline void _write_request (const off64_t count, const long tid, const long fd) const {
12+ inline void _write_request (capio_off64_t count, const long tid, const long fd) const {
1413 START_LOG (capio_syscall (SYS_gettid), " call(path=%s, count=%ld, tid=%ld)" ,
1514 current_path.c_str (), count, tid);
1615 char req[CAPIO_REQ_MAX_SIZE];
17- sprintf (req, " %04d %ld %ld %s %ld " , CAPIO_REQUEST_WRITE, tid, fd, current_path.c_str (),
16+ sprintf (req, " %04d %ld %ld %s %llu " , CAPIO_REQUEST_WRITE, tid, fd, current_path.c_str (),
1817 count);
1918 buf_requests->write (req, CAPIO_REQ_MAX_SIZE);
2019 }
@@ -24,34 +23,34 @@ class WriteRequestCacheFS {
2423
2524 ~WriteRequestCacheFS () {
2625 START_LOG (capio_syscall (SYS_gettid), " call()" );
27- this ->flush (capio_syscall (SYS_gettid));
26+ const int tid = capio_syscall (SYS_gettid);
27+ for (auto &[path, size] : capio_internal_write_offsets) {
28+ this ->flush (path, tid, -1 );
29+ }
2830 }
2931
30- void write_request (std::filesystem::path path, int tid, int fd, long count) {
32+ void write_request (const std::filesystem::path & path, int tid, int fd, long count) {
3133 START_LOG (capio_syscall (SYS_gettid), " call(path=%s, tid=%ld, fd=%ld, count=%ld)" ,
3234 path.c_str (), tid, fd, count);
33- if (fd != current_fd || path.compare (current_path) != 0 ) {
34- LOG (" File descriptor changed from previous state. updating" );
35- this ->flush (tid);
36- current_path = std::move (path);
37- current_fd = fd;
35+
36+ if (!capio_internal_write_offsets.contains (path.c_str ())) {
37+ capio_internal_write_offsets[path.c_str ()] = 0 ;
3838 }
39- current_size += count;
4039
41- if (current_size > _max_size) {
40+ capio_internal_write_offsets[path.c_str ()] += count;
41+
42+ if (capio_internal_write_offsets[path.c_str ()] >= _max_size) {
4243 LOG (" exceeded maximum cache size. flushing..." );
43- this ->flush (tid);
44+ this ->flush (path, tid, fd);
45+ capio_internal_write_offsets[path.c_str ()] = 0 ;
4446 }
4547 };
4648
47- void flush (int tid) {
48- START_LOG (capio_syscall (SYS_gettid), " call(tid=%ld)" , tid);
49- if (current_fd != -1 && current_size > 0 ) {
50- LOG (" Performing write to SHM" );
51- _write_request (tid, current_fd, current_size);
52- }
53- current_fd = -1 ;
54- current_size = 0 ;
49+ void flush (const std::filesystem::path &path, long tid, int fd) {
50+ START_LOG (capio_syscall (SYS_gettid), " call(tid=%ld, path=%s)" , tid, path.c_str ());
51+
52+ LOG (" Performing write to SHM" );
53+ _write_request (capio_internal_write_offsets[path.c_str ()], tid, fd);
5554 }
5655};
5756
0 commit comments