-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DiffTrace would be the cross-platform data structure for DiffTest. This allows the difftest to be dumped online and restored offline.
- Loading branch information
1 parent
8d059e4
commit c46b42a
Showing
7 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
|
||
#include "difftrace.h" | ||
|
||
DiffTrace::DiffTrace(const char *_trace_name, bool is_read, uint64_t _buffer_size) | ||
: is_read(is_read) { | ||
if (!is_read) { | ||
buffer_size = _buffer_size; | ||
buffer = (DiffTestState *)calloc(buffer_size, sizeof(DiffTestState)); | ||
} | ||
if (strlen(trace_name) > 31) { | ||
printf("Length of trace_name %s is more than 31 characters.\n", trace_name); | ||
printf("Please use a shorter name.\n"); | ||
exit(0); | ||
} | ||
strcpy(trace_name, _trace_name); | ||
} | ||
|
||
bool DiffTrace::append(const DiffTestState *trace) { | ||
memcpy(buffer + buffer_count, trace, sizeof(DiffTestState)); | ||
buffer_count++; | ||
if (buffer_count == buffer_size) { | ||
return trace_file_next(); | ||
} | ||
return 0; | ||
} | ||
|
||
bool DiffTrace::read_next(DiffTestState *trace) { | ||
if (!buffer || buffer_count == buffer_size) { | ||
trace_file_next(); | ||
} | ||
memcpy(trace, buffer + buffer_count, sizeof(DiffTestState)); | ||
buffer_count++; | ||
// printf("%lu...\n", buffer_count); | ||
return 0; | ||
} | ||
|
||
|
||
bool DiffTrace::trace_file_next() { | ||
static uint64_t trace_index = 0; | ||
static FILE *file = nullptr; | ||
if (file) { | ||
fclose(file); | ||
} | ||
char filename[128]; | ||
char *noop_home = getenv("NOOP_HOME"); | ||
snprintf(filename, 128, "%s/%s", noop_home, trace_name); | ||
mkdir(filename, 0755); | ||
const char *prefix = "bin"; | ||
snprintf(filename, 128, "%s/%s/%lu.%s", noop_home, trace_name, trace_index, prefix); | ||
if (is_read) { | ||
FILE *file = fopen(filename, "rb"); | ||
if (!file) { | ||
printf("File %s not found.\n", filename); | ||
exit(0); | ||
} | ||
// check the number of traces | ||
fseek(file, 0, SEEK_END); | ||
buffer_size = ftell(file) / sizeof(DiffTestState); | ||
if (buffer) { | ||
free(buffer); | ||
} | ||
buffer = (DiffTestState *)calloc(buffer_size, sizeof(DiffTestState)); | ||
// read the binary file | ||
Info("Loading %lu traces from %s ...\n", buffer_size, filename); | ||
fseek(file, 0, SEEK_SET); | ||
uint64_t read_bytes = fread(buffer, sizeof(DiffTestState), buffer_size, file); | ||
assert(read_bytes == buffer_size); | ||
fclose(file); | ||
buffer_count = 0; | ||
} | ||
else if (buffer_count > 0) { | ||
Info("Writing %lu traces to %s ...\n", buffer_count, filename); | ||
FILE *file = fopen(filename, "wb"); | ||
fwrite(buffer, sizeof(DiffTestState), buffer_count, file); | ||
fclose(file); | ||
buffer_count = 0; | ||
} | ||
trace_index++; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef __DIFFTRACE_H__ | ||
#define __DIFFTRACE_H__ | ||
|
||
#include "common.h" | ||
|
||
class DiffTrace { | ||
public: | ||
char trace_name[32]; | ||
bool is_read; | ||
|
||
DiffTrace(const char *trace_name, bool is_read, uint64_t buffer_size = 1024 * 1024); | ||
~DiffTrace() { | ||
if (!is_read) { | ||
trace_file_next(); | ||
} | ||
if (buffer) { | ||
free(buffer); | ||
} | ||
} | ||
bool append(const DiffTestState *trace); | ||
bool read_next(DiffTestState *trace); | ||
|
||
private: | ||
uint64_t buffer_size; | ||
uint64_t buffer_count = 0; | ||
DiffTestState *buffer = nullptr; | ||
|
||
bool trace_file_next(); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters