| 1 |
/* |
| 2 |
* Copyright (c) 2015 EfficiOS Inc. and Linux Foundation |
| 3 |
* Copyright (c) 2015-2020 Philippe Proulx <[email protected]> |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
* a copy of this software and associated documentation files (the |
| 7 |
* "Software"), to deal in the Software without restriction, including |
| 8 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
* distribute, sublicense, and/or sell copies of the Software, and to |
| 10 |
* permit persons to whom the Software is furnished to do so, subject to |
| 11 |
* the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 |
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
* SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <stdint.h> |
| 29 |
#include <assert.h> |
| 30 |
#include <time.h> |
| 31 |
|
| 32 |
#include "barectf-platform-linux-fs.h" |
| 33 |
#include "barectf.h" |
| 34 |
|
| 35 |
#ifdef __cplusplus |
| 36 |
# define _FROM_VOID_PTR(_type, _value) static_cast<_type *>(_value) |
| 37 |
#else |
| 38 |
# define _FROM_VOID_PTR(_type, _value) ((_type *) (_value)) |
| 39 |
#endif |
| 40 |
|
| 41 |
struct barectf_platform_linux_fs_ctx { |
| 42 |
struct barectf_default_ctx ctx; |
| 43 |
FILE *fh; |
| 44 |
int simulate_full_backend; |
| 45 |
unsigned int full_backend_rand_lt; |
| 46 |
unsigned int full_backend_rand_max; |
| 47 |
}; |
| 48 |
|
| 49 |
static uint64_t get_clock(void * const data) |
| 50 |
{ |
| 51 |
struct timespec ts; |
| 52 |
|
| 53 |
clock_gettime(CLOCK_REALTIME, &ts); |
| 54 |
return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 55 |
} |
| 56 |
|
| 57 |
static void write_packet(const struct barectf_platform_linux_fs_ctx * const platform_ctx) |
| 58 |
{ |
| 59 |
const size_t nmemb = fwrite(barectf_packet_buf(&platform_ctx->ctx), |
| 60 |
barectf_packet_buf_size(&platform_ctx->ctx), 1, platform_ctx->fh); |
| 61 |
|
| 62 |
assert(nmemb == 1); |
| 63 |
} |
| 64 |
|
| 65 |
static int is_backend_full(void * const data) |
| 66 |
{ |
| 67 |
int is_backend_full = 0; |
| 68 |
const struct barectf_platform_linux_fs_ctx * const platform_ctx = |
| 69 |
_FROM_VOID_PTR(const struct barectf_platform_linux_fs_ctx, data); |
| 70 |
|
| 71 |
if (platform_ctx->simulate_full_backend) { |
| 72 |
if (rand() % platform_ctx->full_backend_rand_max < |
| 73 |
platform_ctx->full_backend_rand_lt) { |
| 74 |
is_backend_full = 1; |
| 75 |
goto end; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
end: |
| 80 |
return is_backend_full; |
| 81 |
} |
| 82 |
|
| 83 |
static void open_packet(void * const data) |
| 84 |
{ |
| 85 |
struct barectf_platform_linux_fs_ctx * const platform_ctx = |
| 86 |
_FROM_VOID_PTR(struct barectf_platform_linux_fs_ctx, data); |
| 87 |
|
| 88 |
barectf_default_open_packet(&platform_ctx->ctx); |
| 89 |
} |
| 90 |
|
| 91 |
static void close_packet(void * const data) |
| 92 |
{ |
| 93 |
struct barectf_platform_linux_fs_ctx * const platform_ctx = |
| 94 |
_FROM_VOID_PTR(struct barectf_platform_linux_fs_ctx, data); |
| 95 |
|
| 96 |
/* Close packet now */ |
| 97 |
barectf_default_close_packet(&platform_ctx->ctx); |
| 98 |
|
| 99 |
/* Write packet to file */ |
| 100 |
write_packet(platform_ctx); |
| 101 |
} |
| 102 |
|
| 103 |
struct barectf_platform_linux_fs_ctx *barectf_platform_linux_fs_init( |
| 104 |
const unsigned int buf_size, const char * const data_stream_file_path, |
| 105 |
const int simulate_full_backend, |
| 106 |
const unsigned int full_backend_rand_lt, |
| 107 |
const unsigned int full_backend_rand_max) |
| 108 |
{ |
| 109 |
uint8_t *buf = NULL; |
| 110 |
struct barectf_platform_linux_fs_ctx *platform_ctx; |
| 111 |
struct barectf_platform_callbacks cbs; |
| 112 |
|
| 113 |
cbs.default_clock_get_value = get_clock; |
| 114 |
cbs.is_backend_full = is_backend_full; |
| 115 |
cbs.open_packet = open_packet; |
| 116 |
cbs.close_packet = close_packet; |
| 117 |
platform_ctx = _FROM_VOID_PTR(struct barectf_platform_linux_fs_ctx, |
| 118 |
malloc(sizeof(*platform_ctx))); |
| 119 |
|
| 120 |
if (!platform_ctx) { |
| 121 |
goto error; |
| 122 |
} |
| 123 |
|
| 124 |
buf = _FROM_VOID_PTR(uint8_t, malloc(buf_size)); |
| 125 |
|
| 126 |
if (!buf) { |
| 127 |
goto error; |
| 128 |
} |
| 129 |
|
| 130 |
platform_ctx->fh = fopen(data_stream_file_path, "wb"); |
| 131 |
|
| 132 |
if (!platform_ctx->fh) { |
| 133 |
goto error; |
| 134 |
} |
| 135 |
|
| 136 |
platform_ctx->simulate_full_backend = simulate_full_backend; |
| 137 |
platform_ctx->full_backend_rand_lt = full_backend_rand_lt; |
| 138 |
platform_ctx->full_backend_rand_max = full_backend_rand_max; |
| 139 |
barectf_init(&platform_ctx->ctx, buf, buf_size, cbs, platform_ctx); |
| 140 |
open_packet(platform_ctx); |
| 141 |
goto end; |
| 142 |
|
| 143 |
error: |
| 144 |
free(platform_ctx); |
| 145 |
free(buf); |
| 146 |
|
| 147 |
end: |
| 148 |
return platform_ctx; |
| 149 |
} |
| 150 |
|
| 151 |
void barectf_platform_linux_fs_fini(struct barectf_platform_linux_fs_ctx * const platform_ctx) |
| 152 |
{ |
| 153 |
if (barectf_packet_is_open(&platform_ctx->ctx) && |
| 154 |
!barectf_packet_is_empty(&platform_ctx->ctx)) { |
| 155 |
close_packet(platform_ctx); |
| 156 |
} |
| 157 |
|
| 158 |
fclose(platform_ctx->fh); |
| 159 |
free(barectf_packet_buf(&platform_ctx->ctx)); |
| 160 |
free(platform_ctx); |
| 161 |
} |
| 162 |
|
| 163 |
struct barectf_default_ctx *barectf_platform_linux_fs_get_barectf_ctx( |
| 164 |
struct barectf_platform_linux_fs_ctx * const platform_ctx) |
| 165 |
{ |
| 166 |
return &platform_ctx->ctx; |
| 167 |
} |