Compare commits

..

12 Commits

Author SHA1 Message Date
a2e8997090 change spdlog library name 2021-06-09 15:33:06 -04:00
e07831fcad cmake use precompiled spdlog 2021-06-05 08:36:31 -04:00
ec73a87d31 remove spdlog submodule 2021-06-05 00:11:06 -04:00
e3fb134dbb add spdlog to CMakeLists 2021-06-04 22:59:02 -04:00
6c3b0ceed4 submodule added 2021-06-04 22:55:49 -04:00
e5218cd885 add submodule 2021-06-04 20:39:27 -04:00
afc9b1d6fb switch to cmake 2021-06-04 20:38:33 -04:00
Nam Tran
f24628443f correct interpretation of integrals 2020-11-25 18:48:43 -06:00
Nam Tran
b8550dee70 ready to check missing trigger 2020-11-24 22:22:46 -06:00
Nam Tran
78fc7cb184 move function around 2020-11-24 18:43:59 -06:00
Nam Tran
57443a5c26 channel carries board info 2020-11-24 18:42:01 -06:00
Nam Tran
a561e6b428 set up log files 2020-11-24 18:36:58 -06:00
6 changed files with 179 additions and 132 deletions

1
.gitignore vendored
View File

@@ -2,4 +2,3 @@ r
build build
old old
dump dump
spdlog

15
CMakeLists.txt Normal file
View File

@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.0.0)
project(zynqDump VERSION 0.1.0)
set(TARGET zynqDump)
add_compile_options("-std=c++14")
include_directories("/home/nam/sw/include")
add_library(channel STATIC channel.cc)
find_library(SPDLOG
NAMES spdlog
HINTS "/home/nam/sw/lib"
)
add_executable(${TARGET} client.cc)
target_link_libraries(${TARGET} channel ${SPDLOG})

View File

@@ -1,7 +1,7 @@
target = r target = r
CXX = g++ CXX = g++
CXX_FLAGS := -std=c++14 -I/home/daq/sw/include -Wall -DSPDLOG_COMPILED_LIB CXX_FLAGS := -std=c++14 -I./include -Wall
LD_FLAGS := -lpthread -ldl -L/home/daq/sw/lib64 -lspdlog LD_FLAGS := -lpthread -ldl
BUILD := ./build BUILD := ./build
OBJS = $(BUILD)/client.o $(BUILD)/channel.o OBJS = $(BUILD)/client.o $(BUILD)/channel.o

View File

@@ -1,9 +1,11 @@
#include "channel.h" #include "channel.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
Channel::Channel(char const * ptr, size_t len) { Channel::Channel(char const * ptr, size_t len, uint32_t board) {
board_id = board;
valid = true; valid = true;
uint32_t * wordData = (uint32_t *)ptr; uint32_t * wordData = (uint32_t *)ptr;
block_data = (char *)ptr;
size = ((wordData[0] & 0xFFFFFF00) >> 8) * sizeof(uint32_t); size = ((wordData[0] & 0xFFFFFF00) >> 8) * sizeof(uint32_t);
if (size != len) valid = false; if (size != len) valid = false;
@@ -11,10 +13,10 @@ Channel::Channel(char const * ptr, size_t len) {
if (version != 0x1) valid = false; if (version != 0x1) valid = false;
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
integral[i] = (wordData[i + 1] & 0xFFFFFF00)>>8; integral[i] = ((int32_t)(wordData[i+1])) >> 8;
crossing = wordData[4] & 0x00FFFFFF; crossing = wordData[4] & 0x00FFFFFF;
block_count = (wordData[5] &0xFF000000) >> 24; block_count = wordData[5] >> 24;
id = (wordData[5] & 0x00FF0000) >> 16; id = (wordData[5] & 0x00FF0000) >> 16;
spill = (wordData[5] & 0x0000FFFF); spill = (wordData[5] & 0x0000FFFF);
if ((id > 8) || (id < 1)) { if ((id > 8) || (id < 1)) {
@@ -22,13 +24,19 @@ Channel::Channel(char const * ptr, size_t len) {
} }
} }
void Channel::Print(bool raw){
void Channel::Print(){
if (valid) { if (valid) {
spdlog::info("channel {}, spill {}, size {}, blocks {}, crossing {}, integrals {}, {}, {}", if (raw) {
id, spill, size, block_count, crossing, integral[0], integral[1], integral[2]); uint32_t * wordData = (uint32_t *)block_data;
spdlog::info("{:08X} {:08X} {:08X}", wordData[0], wordData[1], wordData[2]);
spdlog::info("{:08X} {:08X} {:08X}", wordData[3], wordData[4], wordData[5]);
}
// spdlog::get("channel_logger")->info("board {}, channel {}, spill {}, size {}, blocks {}, crossing {}, integrals {}, {}, {}",
// board_id, id, spill, size, block_count, crossing, integral[0], integral[1], integral[2]);
spdlog::info("board {}, channel {}, spill {}, size {}, blocks {}, crossing {}, integrals {}, {}, {}",
board_id, id, spill, size, block_count, crossing, integral[0], integral[1], integral[2]);
} }
else { else {
spdlog::info("Invalid channel"); spdlog::get("zynqDump")->info("Invalid channel");
} }
} }

View File

@@ -5,6 +5,7 @@
#include <cstdlib> #include <cstdlib>
class Channel { class Channel {
uint32_t board_id;
uint32_t id; uint32_t id;
uint32_t size; uint32_t size;
uint32_t version; uint32_t version;
@@ -15,8 +16,8 @@ class Channel {
char * block_data; char * block_data;
bool valid; bool valid;
public: public:
Channel(char const * ptr, size_t len); Channel(char const * ptr, size_t len, uint32_t board);
void Print(); void Print(bool raw = false);
}; };
#endif /* end of include guard */ #endif /* end of include guard */

262
client.cc
View File

@@ -15,8 +15,9 @@
#include <csignal> #include <csignal>
#include "channel.h" #include "channel.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "spdlog/sinks/rotating_file_sink.h"
const char * HOST = "192.168.30.89"; const char * HOST = "192.168.19.1";
const uint32_t PORT = 9999; const uint32_t PORT = 9999;
const size_t MAX_EVENT_SIZE = 33484; // in bytes const size_t MAX_EVENT_SIZE = 33484; // in bytes
const size_t MIN_EVENT_SIZE = 204; // in bytes const size_t MIN_EVENT_SIZE = 204; // in bytes
@@ -32,12 +33,149 @@ bool bad_data = true;
int open_socket(); int open_socket();
void signalHandler( int signum ); void signalHandler( int signum );
void setup_logger(); void setup_logger();
size_t process_channel(char const * ptr, size_t len); size_t process_channel(char const * ptr, size_t len, uint32_t board_id);
size_t process_block(char const * ptr, size_t len); size_t process_block(char const * ptr, size_t len);
size_t process_block(char const * ptr, size_t len){ size_t process_block(char const * ptr, size_t len){
return len; return len;
} }
size_t process_channel(char const * ptr, size_t len){
int main(int argc, char * argv[])
{
setup_logger();
signal(SIGINT, signalHandler);
int ret = open_socket();
if (ret != 0)
return -1;
char char_buffer[MAX_EVENT_SIZE];
uint32_t * wordData = (uint32_t*) char_buffer;
ssize_t nread;
std::ofstream output;
std::string outputName = "dump.bin";
output.open (outputName, std::ios::out );
fd_set select_fds; // fd's used by select
struct timeval timeout; // Time value for time out
//Timeout set for 0 sec + 200 millisec
timeout.tv_sec = 0;
timeout.tv_usec = 200*1000;
std::chrono::duration<int, std::milli> polling_period(2);
while (running) {
// Setup the descriptor set for select()
FD_ZERO(&select_fds);
FD_SET(sock, &select_fds);
if ( select(sock + 1, &select_fds, NULL, NULL, &timeout) == 0 ){
std::this_thread::sleep_for(polling_period);
}
else{
// read in first 4 bytes
nread = recv(sock, char_buffer, 4, 0);
spdlog::get("zynqDump")->debug("read in {:d} bytes: 0x{:X}", nread, wordData[0]);
for (uint32_t j = 0; j < nread; ++j) {
output << char_buffer[j];
}
// if not A5 marker, go to next iteration (read in 4 more bytes)
if ((char_buffer[0] & 0xFF) != 0xA5){
bad_data = true;
continue;
}
else {
bad_data = false;
size_t frameSize = 4 * (wordData[0] >> 8);
nread = recv(sock, char_buffer, frameSize - 4, 0);
// nread + 4 must be between MIN_EVENT_SIZE and MAX_EVENT_SIZE, and equal to frameSize.
if (((size_t)nread != frameSize - 4) || (frameSize < MIN_EVENT_SIZE)|| (frameSize > MAX_EVENT_SIZE)){
bad_data = true;
continue;
}
else {
// ok, this seems to be good data
spdlog::get("zynqDump")->debug("next {:d} bytes (expecting {:d}): {:X} {:X} {:X} {:X}",
nread, frameSize - 4, wordData[0], wordData[1], wordData[2], wordData[3]);
spdlog::get("zynqDump")->debug("{:X} {:X} {:X} {:X}", wordData[4], wordData[5], wordData[6], wordData[7]);
for (uint32_t j = 0; j < nread; ++j) {
output << char_buffer[j];
}
uint8_t board_id = (wordData[0] >> 24) & 0xFF;
uint8_t version = wordData[0] & 0xFF;
uint16_t spill_id = (wordData[1] & 0x00FFFF00) >> 8;
uint8_t channel_count = (wordData[1]) & 0x000000FF;
spdlog::get("zynqDump")->info("board {}, version {}, spill {}, channel_count {}",
static_cast<uint32_t>(board_id),
static_cast<uint32_t>(version),
static_cast<uint32_t>(spill_id),
static_cast<uint32_t>(channel_count));
if ((board_id > 4) || (board_id < 1) || (version != 1) || (channel_count > 8) || (channel_count < 1)) {
bad_data = true;
continue;
}
else {
size_t bytes_left = (frameSize - 3 * sizeof(uint32_t));
size_t bytes_processed = 0;
while ((bytes_left - bytes_processed >= MIN_CHANNEL_SIZE) && (bytes_processed < bytes_left)) {
bytes_processed += process_channel(
char_buffer + 2 * sizeof(uint32_t) + bytes_processed,
bytes_left - bytes_processed,
static_cast<uint32_t>(board_id));
}
spdlog::get("zynqDump")->debug("Done, {} bytes processed.", bytes_processed);
}
}
}
}
}
close(sock);
return 0;
}
void signalHandler( int signum ) {
spdlog::get("zynqDump")->info("Interrupt signal ({}) received, exiting...", signum);
running = false;
}
int open_socket(){
struct sockaddr_in serv_addr;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
std::cerr << "Cannot create socket" << std::endl;
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if(inet_pton(AF_INET, HOST, &serv_addr.sin_addr)<=0) {
std::cerr << "Invalid address or Address not supported" << std::endl;
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
std::cerr << "Cannot connect to " << HOST << ":" << PORT << std::endl;
return -1;
}
return 0;
}
void setup_logger(){
try {
auto max_size = 1048576 * 5;
auto max_files = 20;
auto logger = spdlog::rotating_logger_mt("zynqDump", "output/run.txt", max_size, max_files);
auto channel_logger = spdlog::rotating_logger_mt("channel_logger", "output/chn.txt", max_size * 10, max_files);
}
catch (const spdlog::spdlog_ex &ex) {
std::cerr << "Log init failed: " << ex.what() << std::endl;
exit(-1);
}
}
size_t process_channel(char const * ptr, size_t len, uint32_t board_id){
if ((len < MIN_CHANNEL_SIZE) || (len > MAX_CHANNEL_SIZE)) { if ((len < MIN_CHANNEL_SIZE) || (len > MAX_CHANNEL_SIZE)) {
spdlog::debug("channel length must be between {} and {}.", spdlog::debug("channel length must be between {} and {}.",
MIN_CHANNEL_SIZE, MAX_CHANNEL_SIZE); MIN_CHANNEL_SIZE, MAX_CHANNEL_SIZE);
@@ -83,8 +221,8 @@ size_t process_channel(char const * ptr, size_t len){
// processing // processing
bytes_left -= channel_size; bytes_left -= channel_size;
Channel c(ptr, channel_size); Channel c(ptr, channel_size, board_id);
c.Print(); c.Print(true);
} }
} }
@@ -93,117 +231,3 @@ size_t process_channel(char const * ptr, size_t len){
return len - bytes_left; return len - bytes_left;
} }
int main(int argc, char * argv[])
{
setup_logger();
signal(SIGINT, signalHandler);
int ret = open_socket();
if (ret != 0)
return -1;
char char_buffer[MAX_EVENT_SIZE];
uint32_t * wordData = (uint32_t*) char_buffer;
ssize_t nread;
fd_set select_fds; // fd's used by select
struct timeval timeout; // Time value for time out
//Timeout set for 0 sec + 200 millisec
timeout.tv_sec = 0;
timeout.tv_usec = 200*1000;
std::chrono::duration<int, std::milli> polling_period(2);
while (running) {
// Setup the descriptor set for select()
FD_ZERO(&select_fds);
FD_SET(sock, &select_fds);
if ( select(sock + 1, &select_fds, NULL, NULL, &timeout) == 0 ){
std::this_thread::sleep_for(polling_period);
}
else{
// read in first 4 bytes
nread = recv(sock, char_buffer, 4, 0);
spdlog::info("read in {:d} bytes: 0x{:X}", nread, wordData[0]);
// if not A5 marker, go to next iteration (read in 4 more bytes)
if ((char_buffer[0] & 0xFF) != 0xA5){
bad_data = true;
continue;
}
else {
bad_data = false;
size_t frameSize = 4 * (wordData[0] >> 8);
nread = recv(sock, char_buffer, frameSize - 4, 0);
// nread + 4 must be between MIN_EVENT_SIZE and MAX_EVENT_SIZE, and equal to frameSize.
if (((size_t)nread != frameSize - 4) || (frameSize < MIN_EVENT_SIZE)|| (frameSize > MAX_EVENT_SIZE)){
bad_data = true;
continue;
}
else {
// ok, this seems to be good data
spdlog::info("next {:d} bytes (expecting {:d}): {:X} {:X} {:X} {:X}",
nread, frameSize - 4, wordData[0], wordData[1], wordData[2], wordData[3]);
spdlog::info("{:X} {:X} {:X} {:X}", wordData[4], wordData[5], wordData[6], wordData[7]);
uint8_t board_id = (wordData[0] >> 24) & 0xFF;
uint8_t version = wordData[0] & 0xFF;
uint16_t spill_id = (wordData[1] & 0x00FFFF00) >> 8;
uint8_t channel_count = (wordData[1]) & 0x000000FF;
spdlog::info("board {}, version {}, spill {}, channel_count {}",
static_cast<uint32_t>(board_id),
static_cast<uint32_t>(version),
static_cast<uint32_t>(spill_id),
static_cast<uint32_t>(channel_count));
if ((board_id > 4) || (board_id < 1) || (version != 1) || (channel_count > 8) || (channel_count < 1)) {
bad_data = true;
continue;
}
else {
size_t bytes_left = (frameSize - 3 * sizeof(uint32_t));
size_t bytes_processed = 0;
while ((bytes_left - bytes_processed >= MIN_CHANNEL_SIZE) && (bytes_processed < bytes_left)) {
bytes_processed += process_channel(char_buffer + 2 * sizeof(uint32_t) + bytes_processed,
bytes_left - bytes_processed);
}
spdlog::info("Done, {} bytes processed.", bytes_processed);
}
}
}
}
}
close(sock);
return 0;
}
void signalHandler( int signum ) {
spdlog::info("Interrupt signal ({}) received, exiting...", signum);
running = false;
}
int open_socket(){
struct sockaddr_in serv_addr;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
std::cerr << "Cannot create socket" << std::endl;
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if(inet_pton(AF_INET, HOST, &serv_addr.sin_addr)<=0) {
std::cerr << "Invalid address or Address not supported" << std::endl;
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
std::cerr << "Cannot connect to " << HOST << ":" << PORT << std::endl;
return -1;
}
return 0;
}
void setup_logger(){
}