2021-01-16 14:23:06 -08:00
|
|
|
#pragma once
|
2020-01-17 10:07:22 -08:00
|
|
|
|
2021-10-29 18:21:57 +08:00
|
|
|
#if __has_include(<memory_resource>)
|
|
|
|
|
#define HAS_MEMORY_RESOURCE 1
|
2021-10-18 17:10:08 +08:00
|
|
|
#include <memory_resource>
|
2021-10-29 18:21:57 +08:00
|
|
|
#endif
|
2021-10-18 17:10:08 +08:00
|
|
|
|
2023-08-24 04:25:17 +08:00
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2022-10-06 12:17:22 +08:00
|
|
|
|
2021-06-13 14:31:00 +08:00
|
|
|
#include "cereal/gen/cpp/log.capnp.h"
|
2022-06-19 14:43:49 -07:00
|
|
|
#include "system/camerad/cameras/camera_common.h"
|
2021-08-31 10:33:00 +08:00
|
|
|
|
2021-06-13 14:31:00 +08:00
|
|
|
const CameraType ALL_CAMERAS[] = {RoadCam, DriverCam, WideRoadCam};
|
|
|
|
|
const int MAX_CAMERAS = std::size(ALL_CAMERAS);
|
2021-10-18 17:10:08 +08:00
|
|
|
const int DEFAULT_EVENT_MEMORY_POOL_BLOCK_SIZE = 65000;
|
2021-09-29 17:43:56 +02:00
|
|
|
|
2021-06-13 14:31:00 +08:00
|
|
|
class Event {
|
2020-01-17 10:07:22 -08:00
|
|
|
public:
|
2021-09-20 03:24:28 +08:00
|
|
|
Event(cereal::Event::Which which, uint64_t mono_time) : reader(kj::ArrayPtr<capnp::word>{}) {
|
|
|
|
|
// construct a dummy Event for binary search, e.g std::upper_bound
|
|
|
|
|
this->which = which;
|
|
|
|
|
this->mono_time = mono_time;
|
|
|
|
|
}
|
2021-10-04 21:39:59 +08:00
|
|
|
Event(const kj::ArrayPtr<const capnp::word> &amsg, bool frame = false);
|
2021-06-13 14:31:00 +08:00
|
|
|
inline kj::ArrayPtr<const capnp::byte> bytes() const { return words.asBytes(); }
|
|
|
|
|
|
2021-09-20 03:24:28 +08:00
|
|
|
struct lessThan {
|
|
|
|
|
inline bool operator()(const Event *l, const Event *r) {
|
|
|
|
|
return l->mono_time < r->mono_time || (l->mono_time == r->mono_time && l->which < r->which);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-29 18:21:57 +08:00
|
|
|
#if HAS_MEMORY_RESOURCE
|
2021-10-18 17:10:08 +08:00
|
|
|
void *operator new(size_t size, std::pmr::monotonic_buffer_resource *mbr) {
|
|
|
|
|
return mbr->allocate(size);
|
|
|
|
|
}
|
|
|
|
|
void operator delete(void *ptr) {
|
2022-02-07 02:42:41 +08:00
|
|
|
// No-op. memory used by EventMemoryPool increases monotonically until the logReader is destroyed.
|
2021-10-18 17:10:08 +08:00
|
|
|
}
|
2021-10-29 18:21:57 +08:00
|
|
|
#endif
|
2021-10-18 17:10:08 +08:00
|
|
|
|
2021-06-13 14:31:00 +08:00
|
|
|
uint64_t mono_time;
|
|
|
|
|
cereal::Event::Which which;
|
|
|
|
|
cereal::Event::Reader event;
|
|
|
|
|
capnp::FlatArrayMessageReader reader;
|
|
|
|
|
kj::ArrayPtr<const capnp::word> words;
|
2021-09-29 17:43:56 +02:00
|
|
|
bool frame;
|
2021-06-13 14:31:00 +08:00
|
|
|
};
|
|
|
|
|
|
2021-11-29 21:10:24 +08:00
|
|
|
class LogReader {
|
2021-06-13 14:31:00 +08:00
|
|
|
public:
|
2021-11-29 21:10:24 +08:00
|
|
|
LogReader(size_t memory_pool_block_size = DEFAULT_EVENT_MEMORY_POOL_BLOCK_SIZE);
|
2021-01-16 14:23:06 -08:00
|
|
|
~LogReader();
|
2023-11-14 01:19:39 +08:00
|
|
|
bool load(const std::string &url, std::atomic<bool> *abort = nullptr,
|
2022-10-06 12:17:22 +08:00
|
|
|
bool local_cache = false, int chunk_size = -1, int retries = 0);
|
2021-11-29 21:10:24 +08:00
|
|
|
bool load(const std::byte *data, size_t size, std::atomic<bool> *abort = nullptr);
|
2021-09-20 03:24:28 +08:00
|
|
|
std::vector<Event*> events;
|
2021-01-16 14:23:06 -08:00
|
|
|
|
2021-06-13 14:31:00 +08:00
|
|
|
private:
|
2023-11-14 01:19:39 +08:00
|
|
|
bool parse(std::atomic<bool> *abort);
|
2021-10-04 21:39:59 +08:00
|
|
|
std::string raw_;
|
2021-10-29 18:21:57 +08:00
|
|
|
#ifdef HAS_MEMORY_RESOURCE
|
2023-08-18 02:42:35 +08:00
|
|
|
std::unique_ptr<std::pmr::monotonic_buffer_resource> mbr_;
|
2021-10-29 18:21:57 +08:00
|
|
|
#endif
|
2020-01-17 10:07:22 -08:00
|
|
|
};
|