Skip to content

Commit 512a13a

Browse files
committed
Save TOC entries when writing frames
1 parent 4db5128 commit 512a13a

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

contrib/plugins/bap-tracing/frame_buffer.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ bool frame_buffer_is_empty(const FrameBuffer *buf) {
159159
return buf->fbuf[buf->idx] == NULL;
160160
}
161161

162+
/// @brief Dumps the file buffer as TOC entry into the file.
162163
void frame_buffer_flush_to_file(FrameBuffer *buf, WLOCKED FILE *file) {
163164
for (size_t i = 0; i <= buf->idx && i < frames_per_toc_entry; ++i) {
164165
Frame *frame = buf->fbuf[i];
@@ -167,12 +168,10 @@ void frame_buffer_flush_to_file(FrameBuffer *buf, WLOCKED FILE *file) {
167168
uint64_t packed_size = frame__pack(frame, packed_buffer);
168169
WRITE(packed_size);
169170
WRITE_BUF(packed_buffer, packed_size);
170-
buf->frames_written++;
171171
frame_free(frame);
172172
}
173173
memset(buf->fbuf, 0, sizeof(buf->fbuf));
174174
buf->idx = 0;
175-
// toc_update(); ??
176175
}
177176

178177
bool frame_buffer_new_frame_std(FrameBuffer *buf, unsigned int thread_id,

contrib/plugins/bap-tracing/frame_buffer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ typedef enum {
2020
typedef struct {
2121
Frame *fbuf[FRAMES_PER_TOC_ENTRY_]; ///< The frames buffered.
2222
size_t idx; ///< Points to currently open frame.
23-
size_t frames_written; ///< Number of frames written from buffer to file.
2423
} FrameBuffer;
2524

2625
/**

contrib/plugins/bap-tracing/tracing.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,19 @@ static GPtrArray *registers_init(void) {
9797
return registers->len ? g_steal_pointer(&registers) : NULL;
9898
}
9999

100+
static void write_toc_entry(FrameBuffer *fbuf) {
101+
g_rw_lock_writer_lock(&state.file_lock);
102+
g_rw_lock_writer_lock(&state.toc_entries_offsets_lock);
103+
frame_buffer_flush_to_file(fbuf, state.file);
104+
uint64_t next_toc_entry = ftell(state.file);
105+
g_array_append_val(state.toc_entries_offsets, next_toc_entry);
106+
g_rw_lock_writer_unlock(&state.toc_entries_offsets_lock);
107+
g_rw_lock_writer_unlock(&state.file_lock);
108+
}
109+
100110
static void log_insn_reg_access(unsigned int vcpu_index, void *udata) {
101111
g_rw_lock_reader_lock(&state.vcpus_array_lock);
102-
g_rw_lock_reader_lock(&state.frame_buffer_lock);
112+
g_rw_lock_writer_lock(&state.frame_buffer_lock);
103113

104114
FrameBuffer *fbuf = g_ptr_array_index(state.frame_buffer, vcpu_index);
105115
VCPU *vcpu = g_ptr_array_index(state.vcpus, vcpu_index);
@@ -113,17 +123,15 @@ static void log_insn_reg_access(unsigned int vcpu_index, void *udata) {
113123
}
114124

115125
if (frame_buffer_is_full(fbuf)) {
116-
g_rw_lock_writer_lock(&state.file_lock);
117-
frame_buffer_flush_to_file(fbuf, state.file);
118-
g_rw_lock_writer_unlock(&state.file_lock);
126+
write_toc_entry(fbuf);
119127
}
120128

121129
// Open new one.
122130
Instruction *insn = udata;
123131
add_new_insn_frame(vcpu, vcpu_index, fbuf, insn);
124132
add_pre_reg_state(vcpu, vcpu_index, current_regs, fbuf);
125133

126-
g_rw_lock_reader_unlock(&state.frame_buffer_lock);
134+
g_rw_lock_writer_unlock(&state.frame_buffer_lock);
127135
g_rw_lock_reader_unlock(&state.vcpus_array_lock);
128136
}
129137

@@ -228,9 +236,11 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
228236

229237
const char *target_path = "/tmp/test.trace";
230238
state.frame_buffer = g_ptr_array_new();
239+
state.toc_entries_offsets = g_array_new(false, true, sizeof(uint64_t));
231240
state.vcpus = g_ptr_array_new();
232241
state.file = fopen(target_path, "wb");
233-
if (!(state.frame_buffer || state.vcpus || state.file)) {
242+
if (!(state.frame_buffer || state.vcpus || state.file ||
243+
!state.toc_entries_offsets)) {
234244
return 1;
235245
}
236246
for (size_t i = 0; i < argc; ++i) {
@@ -242,6 +252,8 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
242252
}
243253
// write_meta(argv, envp, target_argv, target_envp);
244254

255+
g_array_append_val(state.toc_entries_offsets, offset_toc_start);
256+
245257
qemu_plugin_register_vcpu_init_cb(id, vcpu_init);
246258
qemu_plugin_register_vcpu_tb_trans_cb(id, cb_trans);
247259
qemu_plugin_register_atexit_cb(id, plugin_exit, NULL);

contrib/plugins/bap-tracing/tracing.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ typedef struct {
134134
GRWLock frame_buffer_lock;
135135
GPtrArray /*<FrameBuffer>*/ *frame_buffer; ///< Indexed by vcpu id
136136

137+
GRWLock toc_entries_offsets_lock;
138+
GArray /*<uint64_t>*/ *toc_entries_offsets;
139+
137140
GRWLock file_lock;
138141
FILE *file;
139142
} TraceState;

0 commit comments

Comments
 (0)