ROSS
tw-state.c
Go to the documentation of this file.
1#include <ross.h>
2#include <assert.h>
3#include "lz4.h"
4
5/**
6 * Make a snapshot of the LP state and store it into the delta buffer
7 */
8void
9tw_snapshot(tw_lp *lp, size_t state_sz)
10{
11 assert(lp->pe->delta_buffer[0] && "increase --buddy-size argument!");
12 memcpy(lp->pe->delta_buffer[0], lp->cur_state, state_sz);
13}
14
15/**
16 * Create the delta from the current state and the snapshot.
17 * Compress it.
18 * @return The size of the compressed data placed in delta_buffer[1].
19 */
20long
21tw_snapshot_delta(tw_lp *lp, size_t state_sz)
22{
23 unsigned long i;
24 tw_clock start;
25 int ret_size = 0;
26 unsigned char *current_state = (unsigned char *)lp->cur_state;
27 unsigned char *snapshot = lp->pe->delta_buffer[0];
28 void *scratch = lp->pe->delta_buffer[2];
29
30 for (i = 0; i < state_sz; i++) {
31 snapshot[i] = current_state[i] - snapshot[i];
32 }
33
34 start = tw_clock_read();
35 ret_size = LZ4_compress_fast_extState(scratch, (char*)snapshot, (char*)lp->pe->delta_buffer[1], state_sz, g_tw_delta_sz, g_tw_lz4_knob);
36 g_tw_pe->stats.s_lz4 += (tw_clock_read() - start);
37 if (ret_size < 0) {
38 tw_error(TW_LOC, "LZ4_compress error");
39 }
40
41 start = tw_clock_read();
42 lp->pe->cur_event->delta_buddy = buddy_alloc(ret_size);
43 g_tw_pe->stats.s_buddy += (tw_clock_read() - start);
44 assert(lp->pe->cur_event->delta_buddy);
45 lp->pe->cur_event->delta_size = ret_size;
46 memcpy(lp->pe->cur_event->delta_buddy, lp->pe->delta_buffer[1], ret_size);
47
48 return ret_size;
49}
50
51/**
52 * Restore the state of lp to the (decompressed) data held in buffer
53 */
54void
55tw_snapshot_restore(tw_lp *lp, size_t state_sz)
56{
57 unsigned int i;
58 tw_clock start = tw_clock_read();
59 unsigned char *snapshot = (unsigned char *)lp->pe->cur_event->delta_buddy;
60 unsigned char *current_state = (unsigned char *)lp->cur_state;
61
62 int ret = LZ4_decompress_fast((char *)snapshot, (char*)lp->pe->delta_buffer[0], state_sz);
63 g_tw_pe->stats.s_lz4 += (tw_clock_read() - start);
64 if (ret < 0) {
65 tw_error(TW_LOC, "LZ4_decompress_fast error");
66 }
67
68 snapshot = lp->pe->delta_buffer[0];
69 for (i = 0; i < state_sz; i++) {
70 current_state[i] = current_state[i] - snapshot[i];
71 }
72}
void * buddy_alloc(unsigned size)
Definition buddy.c:234
static tw_clock tw_clock_read(void)
Definition aarch64.h:8
uint64_t tw_clock
Definition aarch64.h:6
int LZ4_decompress_fast(const char *source, char *dest, int originalSize)
Definition lz4.c:1298
int LZ4_compress_fast_extState(void *state, const char *source, char *dest, int inputSize, int maxDestSize, int acceleration)
Definition lz4.c:657
tw_pe * g_tw_pe
Definition ross-global.c:79
unsigned int g_tw_lz4_knob
Definition ross-global.c:45
void tw_error(const char *file, int line, const char *fmt,...)
Definition tw-util.c:77
size_t g_tw_delta_sz
Definition ross-global.c:38
#define TW_LOC
void * delta_buddy
Delta memory from buddy allocator.
Definition ross-types.h:307
size_t delta_size
Size of delta.
Definition ross-types.h:308
LP State Structure.
Definition ross-types.h:336
tw_pe * pe
Definition ross-types.h:340
void * cur_state
Current application LP data.
Definition ross-types.h:347
unsigned char * delta_buffer[3]
buffers used for delta encoding
Definition ross-types.h:429
tw_event * cur_event
Current event being processed.
Definition ross-types.h:426
long tw_snapshot_delta(tw_lp *lp, size_t state_sz)
Definition tw-state.c:21
void tw_snapshot_restore(tw_lp *lp, size_t state_sz)
Definition tw-state.c:55
void tw_snapshot(tw_lp *lp, size_t state_sz)
Definition tw-state.c:9