-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_writer.cpp
152 lines (122 loc) · 4.02 KB
/
file_writer.cpp
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
#include <array>
#include <cassert>
#include <syscall.h>
#include <sys/types.h>
#include <fcntl.h>
#include "configuration.hpp"
#include "libsyscall_intercept_hook_point.h"
#include "memory.hpp"
#include "segments.hpp"
#include "file_writer.hpp"
namespace {
constexpr uint32_t minimumSegmentSize()
{
return sizeof(SegmentTagType) + sizeof(SegmentLengthType);
}
static constexpr uint32_t PAGE_SIZE = 1024 * 1024;
static constexpr uint32_t FLIP_THRESHOLD = 128 * 1024;
static constexpr uint32_t FLUSH_THRESHOLD = PAGE_SIZE - minimumSegmentSize();
static constexpr int INVALID_FD = -1;
int openFileForWriting(const char *path)
{
int64_t return_value = syscall_no_intercept(SYS_open, path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (return_value >= 0) {
return static_cast<int>(return_value);
} else {
return -1;
}
}
void flushBuffer(BufferView &buffer_view, int fd)
{
if (fd != INVALID_FD) {
uint32_t num_bytes = buffer_view.getCurrentPosition();
const char *data = buffer_view.getRawBuffer();
syscall_no_intercept(SYS_write, fd, data, num_bytes);
}
}
void writeFillerSegment(BufferView &view, uint32_t payload_length)
{
ScopedSegment seg(view, SegmentTag::Filler);
view.writeRecurringByte(payload_length, 0);
}
}
FileWriter::FileWriter(const Configuration &configuration) :
m_configuration{configuration},
m_buffers{PAGE_SIZE, PAGE_SIZE},
m_working_buffer{&m_buffers.front()},
m_standby_buffer{&m_buffers.back()},
m_flush_pending_working_data{m_working_buffer->getBufferView()},
m_output_file_fd{INVALID_FD} { }
FileWriter::~FileWriter()
{
flush(false);
syscall_no_intercept(SYS_close, m_output_file_fd);
}
bool FileWriter::openOutputFile()
{
if (m_output_file_fd == INVALID_FD) {
m_output_file_fd = openFileForWriting(m_configuration.getOutputFilePath());
}
return (m_output_file_fd != INVALID_FD);
}
BufferView &FileWriter::getBufferView()
{
return m_working_buffer->getBufferView();
}
void FileWriter::lock()
{
// TODO: Implement this when multithreaded capturing is to be supported
}
void FileWriter::unlock()
{
if (!getBufferView().hasRoomFor(FLIP_THRESHOLD)) {
assert(m_standby_buffer->getBufferView().getCurrentPosition() == 0);
flipWorkingBuffer();
m_flush_pending_working_data = m_working_buffer->getBufferView();
}
if (getTotalDataSize() >= FLUSH_THRESHOLD) {
flush(true);
}
m_flush_pending_working_data = m_working_buffer->getBufferView();
// TODO: Implement this when multithreaded capturing is to be supported
}
void FileWriter::flush(bool add_filler)
{
uint32_t flush_pending_data_size = getFlushPendingDataSize();
assert(flush_pending_data_size < PAGE_SIZE);
flushBuffer(m_standby_buffer->getBufferView(), m_output_file_fd);
m_standby_buffer->resetView();
flushBuffer(m_flush_pending_working_data, m_output_file_fd);
m_working_buffer->getBufferView().subtract(m_flush_pending_working_data);
if (add_filler) {
uint32_t filler_payload_length = PAGE_SIZE - flush_pending_data_size - minimumSegmentSize();
outputFillerSegment(filler_payload_length);
}
}
void FileWriter::flipWorkingBuffer()
{
m_standby_buffer = m_working_buffer;
if (m_working_buffer == &m_buffers[0]) {
m_working_buffer = &m_buffers[1];
} else {
m_working_buffer = &m_buffers[0];
}
}
uint32_t FileWriter::getFlushPendingDataSize()
{
uint32_t data_size = m_standby_buffer->getBufferView().getCurrentPosition();
data_size += m_flush_pending_working_data.getCurrentPosition();
return data_size;
}
uint32_t FileWriter::getTotalDataSize() const
{
return m_standby_buffer->getBufferView().getCurrentPosition() +
m_working_buffer->getBufferView().getCurrentPosition();
}
void FileWriter::outputFillerSegment(uint32_t payload_length)
{
BufferView view = m_standby_buffer->getBufferView();
writeFillerSegment(view, payload_length);
flushBuffer(view, m_output_file_fd);
m_standby_buffer->resetView();
}