Skip to content

Commit

Permalink
Update recorder package
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Aug 15, 2022
1 parent 8dff312 commit 7bf1497
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
1 change: 1 addition & 0 deletions .pdm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ check_update = false
parallel_install = true
project_max_depth = 5
use_venv = false
path = "/usr/bin/python"

[python.feature]
install_cache = true
Expand Down
2 changes: 1 addition & 1 deletion pyxcp/examples/conf_socket_can.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TRANSPORT = "CAN"
CAN_DRIVER = "SocketCAN"
CAN_USE_DEFAULT_LISTENER = true
CHANNEL = "vcan0"
CHANNEL = "can0"
CAN_ID_MASTER = 257
CAN_ID_SLAVE = 258
CAN_ID_BROADCAST = 256
Expand Down
19 changes: 16 additions & 3 deletions pyxcp/recorder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""XCP Frame Recording Facility.
"""

from dataclasses import dataclass
from enum import IntEnum

try:
import numpy as np
import pandas as pd
except ImportError:
HAS_PANDAS = False
Expand Down Expand Up @@ -60,12 +61,24 @@ def __iter__(self):
for category, counter, timestamp, _, payload in frames:
yield (category, counter, timestamp, payload)

def reset_iter(self):
self._reader.reset()

def as_dataframe(self):
if HAS_PANDAS:
df = pd.DataFrame((f for f in self), columns=["category", "counter", "timestamp", "payload"])
df = df.set_index("timestamp")
df.category = df.category.map({v: k for k, v in FrameCategory.__members__.items()}).astype("category")
return df
else:
raise NotImplementedError("method as_dataframe() requires 'pandas' package")


class XcpLogFileWriter:
""" """

def __init__(self, file_name, p=10, c=1):
self._writer = rec._PyXcpLogFileWriter(file_name, p, c)
def __init__(self, file_name: str, prealloc=10, chunk_size=1):
self._writer = rec._PyXcpLogFileWriter(file_name, prealloc, chunk_size)

def add_frame(self, category: FrameCategory, counter: int, timestamp: float, payload: bytes):
self._writer.add_frame(category, counter % (COUNTER_MAX + 1), timestamp, len(payload), payload)
Expand Down
2 changes: 0 additions & 2 deletions pyxcp/recorder/rekorder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,6 @@ class XcpLogFileWriter
//printf("CreateFile returned: %u\n", GetLastError());
#else
m_fd = open(m_file_name.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0666);

printf("open returned: %u\n", errno);
#endif
truncate(megabytes(prealloc));
m_mmap = new mio::mmap_sink(m_fd);
Expand Down
39 changes: 18 additions & 21 deletions pyxcp/recorder/test_reko.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
from time import perf_counter

import pandas as pd

from pyxcp.recorder import FrameCategory
from pyxcp.recorder import XcpLogFileReader
from pyxcp.recorder import XcpLogFileWriter

writer = XcpLogFileWriter("test_logger", p=220)
# for idx in range(255):
# Pre-allocate a 100MB file -- Note: due to the nature of memory-mapped files, this is a HARD limit.
# Chunk size is 1MB (i.e. compression granularity).
writer = XcpLogFileWriter("test_logger", prealloc = 100, chunk_size=1)

# Write some records.
for idx in range(512 * 1024):
value = idx % 256
writer.add_frame(1, idx, perf_counter(), [value] * value)
writer.finalize()
del writer
# """
writer.add_frame(FrameCategory.CMD, idx, perf_counter(), [value] * value)
writer.finalize() # We're done.


print("Before c-tor()")
reader = XcpLogFileReader("test_logger")
print("After c-tor()")
hdr = reader.get_header()
hdr = reader.get_header() # Get file information.
print(hdr)

cnt = 0

df = pd.DataFrame((f for f in reader), columns=["category", "counter", "timestamp", "payload"])
df = df.set_index("timestamp")
df.category = df.category.map({v: k for k, v in FrameCategory.__members__.items()}).astype("category")
print(df)
df = reader.as_dataframe() # Return recordings as Pandas DataFrame.
print(df.info())
print(df.describe())

# for frame in reader:
# print(frame)
# cnt += 1

print("Finished.")
reader.reset_iter() # Non-standard method to restart iteration.

idx = 0
# Iterate over frames/records.
for frame in reader:
idx += 1
print("---")
print(f"Iterated over {idx} frames")
1 change: 1 addition & 0 deletions pyxcp/recorder/wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ PYBIND11_MODULE(rekorder, m) {
py::class_<XcpLogFileReader>(m, "_PyXcpLogFileReader")
.def(py::init<const std::string &>())
.def("next_block", &XcpLogFileReader::next_block)
.def("reset", &XcpLogFileReader::reset)
.def("get_header_as_tuple", &XcpLogFileReader::get_header_as_tuple)
;
py::class_<XcpLogFileWriter>(m, "_PyXcpLogFileWriter")
Expand Down

0 comments on commit 7bf1497

Please sign in to comment.