diff --git a/selfdrive/loggerd/SConscript b/selfdrive/loggerd/SConscript index 0d3fe55048..2adcfb846c 100644 --- a/selfdrive/loggerd/SConscript +++ b/selfdrive/loggerd/SConscript @@ -7,7 +7,7 @@ libs = [logger_lib, common, cereal, messaging, visionipc, 'avformat', 'avcodec', 'swscale', 'avutil', 'yuv', 'bz2', 'OpenCL'] -src = ['main.cc', 'loggerd.cc'] +src = ['loggerd.cc'] if arch in ["aarch64", "larch64"]: src += ['omx_encoder.cc'] libs += ['OmxCore', 'gsl', 'CB'] + gpucommon @@ -24,8 +24,8 @@ if arch == "Darwin": del libs[libs.index('OpenCL')] env['FRAMEWORKS'] = ['OpenCL'] -env.Program('loggerd', src, LIBS=libs) +env.Program('loggerd', ['main.cc'] + src, LIBS=libs) env.Program('bootlog.cc', LIBS=libs) if GetOption('test'): - env.Program('tests/test_logger', ['tests/test_runner.cc', 'tests/test_logger.cc', env.Object('logger_util', '#/selfdrive/ui/replay/util.cc')], LIBS=[libs] + ['curl', 'crypto']) + env.Program('tests/test_logger', ['tests/test_runner.cc', 'tests/test_loggerd.cc', 'tests/test_logger.cc', env.Object('logger_util', '#/selfdrive/ui/replay/util.cc')] + src, LIBS=[libs] + ['curl', 'crypto', 'bz2']) diff --git a/selfdrive/loggerd/loggerd.h b/selfdrive/loggerd/loggerd.h index 59f4cb6709..ca5a638f85 100644 --- a/selfdrive/loggerd/loggerd.h +++ b/selfdrive/loggerd/loggerd.h @@ -115,4 +115,5 @@ struct LoggerdState { bool camera_synced[WideRoadCam + 1] = {}; }; +bool sync_encoders(LoggerdState *state, CameraType cam_type, uint32_t frame_id); void loggerd_thread(); diff --git a/selfdrive/loggerd/tests/test_loggerd.cc b/selfdrive/loggerd/tests/test_loggerd.cc new file mode 100644 index 0000000000..500aa6f338 --- /dev/null +++ b/selfdrive/loggerd/tests/test_loggerd.cc @@ -0,0 +1,50 @@ +#include + +#include "catch2/catch.hpp" +#include "selfdrive/loggerd/loggerd.h" + +int random_int(int min, int max) { + std::random_device dev; + std::mt19937 rng(dev()); + std::uniform_int_distribution dist(min, max); + return dist(rng); +} + +int get_synced_frame_id(LoggerdState *s, CameraType cam_type, int start_frame_id) { + int frame_id = start_frame_id; + while (!sync_encoders(s, cam_type, frame_id)) { + ++frame_id; + usleep(0); + } + return frame_id; +}; + +TEST_CASE("sync_encoders") { + const int max_waiting = GENERATE(1, 2, 3); + + for (int test_cnt = 0; test_cnt < 10; ++test_cnt) { + LoggerdState s{.max_waiting = max_waiting}; + std::vector start_frames; + std::vector> futures; + + for (int i = 0; i < max_waiting; ++i) { + int start_frame_id = random_int(0, 20); + start_frames.push_back(start_frame_id); + futures.emplace_back(std::async(std::launch::async, get_synced_frame_id, &s, (CameraType)i, start_frame_id)); + } + + // get results + int synced_frame_id = 0; + for (int i = 0; i < max_waiting; ++i) { + if (i == 0) { + synced_frame_id = futures[i].get(); + // require synced_frame_id equal start_frame_id if max_waiting == 1 + if (max_waiting == 1) { + REQUIRE(synced_frame_id == start_frames[0]); + } + } else { + REQUIRE(futures[i].get() == synced_frame_id); + } + } + } +}