From 5782efe3ae7882a6a8eee0086b9c612c1d276814 Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Wed, 26 Aug 2020 17:09:23 +0200 Subject: [PATCH] Systemd logcatd (#2085) * Systemd logcatd infrastructure * This should work * Cleanup * Split * More compact and cleanup * Add to ubuntu setup package list * Run logcatd on all non pc platforms --- Dockerfile.openpilot_base | 1 + SConstruct | 2 +- release/files_common | 3 +- selfdrive/logcatd/SConscript | 8 +- .../{logcatd.cc => logcatd_android.cc} | 0 selfdrive/logcatd/logcatd_systemd.cc | 73 +++++++++++++++++++ selfdrive/manager.py | 6 +- tools/ubuntu_setup.sh | 1 + 8 files changed, 89 insertions(+), 5 deletions(-) rename selfdrive/logcatd/{logcatd.cc => logcatd_android.cc} (100%) create mode 100644 selfdrive/logcatd/logcatd_systemd.cc diff --git a/Dockerfile.openpilot_base b/Dockerfile.openpilot_base index 4cec8a36..25ac0826 100644 --- a/Dockerfile.openpilot_base +++ b/Dockerfile.openpilot_base @@ -27,6 +27,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ libopencv-dev \ libssl-dev \ libsqlite3-dev \ + libsystemd-dev \ libusb-1.0-0-dev \ libczmq-dev \ libzmq3-dev \ diff --git a/SConstruct b/SConstruct index a12fa929..3dffd914 100644 --- a/SConstruct +++ b/SConstruct @@ -311,7 +311,7 @@ SConscript(['selfdrive/locationd/SConscript']) SConscript(['selfdrive/locationd/models/SConscript']) SConscript(['selfdrive/sensord/SConscript']) -if arch == "aarch64": +if arch != "Darwin": SConscript(['selfdrive/logcatd/SConscript']) if arch != "larch64": diff --git a/release/files_common b/release/files_common index 02bee33d..6238bb4e 100644 --- a/release/files_common +++ b/release/files_common @@ -294,7 +294,8 @@ selfdrive/locationd/calibrationd.py selfdrive/locationd/calibration_helpers.py selfdrive/logcatd/SConscript -selfdrive/logcatd/logcatd.cc +selfdrive/logcatd/logcatd_android.cc +selfdrive/logcatd/logcatd_systemd.cc selfdrive/proclogd/SConscript selfdrive/proclogd/proclogd.cc diff --git a/selfdrive/logcatd/SConscript b/selfdrive/logcatd/SConscript index 67ad6730..7f87cbad 100644 --- a/selfdrive/logcatd/SConscript +++ b/selfdrive/logcatd/SConscript @@ -1,2 +1,6 @@ -Import('env', 'cereal', 'messaging') -env.Program('logcatd.cc', LIBS=[cereal, messaging, 'cutils', 'zmq', 'czmq', 'capnp', 'kj']) +Import('env', 'cereal', 'messaging', 'arch') + +if arch == "aarch64": + env.Program('logcatd', 'logcatd_android.cc', LIBS=[cereal, messaging, 'cutils', 'zmq', 'czmq', 'capnp', 'kj']) +else: + env.Program('logcatd', 'logcatd_systemd.cc', LIBS=[cereal, messaging, 'zmq', 'capnp', 'kj', 'systemd', 'json11']) diff --git a/selfdrive/logcatd/logcatd.cc b/selfdrive/logcatd/logcatd_android.cc similarity index 100% rename from selfdrive/logcatd/logcatd.cc rename to selfdrive/logcatd/logcatd_android.cc diff --git a/selfdrive/logcatd/logcatd_systemd.cc b/selfdrive/logcatd/logcatd_systemd.cc new file mode 100644 index 00000000..3cbba3f1 --- /dev/null +++ b/selfdrive/logcatd/logcatd_systemd.cc @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#include "json11.hpp" +#include + +#include "common/timing.h" +#include "messaging.hpp" + +int main(int argc, char *argv[]) { + PubMaster pm({"androidLog"}); + + sd_journal *journal; + assert(sd_journal_open(&journal, 0) >= 0); + assert(sd_journal_get_fd(journal) >= 0); // needed so sd_journal_wait() works properly if files rotate + assert(sd_journal_seek_tail(journal) >= 0); + + int r; + while (true) { + r = sd_journal_next(journal); + assert(r >= 0); + + // Wait for new message if we didn't receive anything + if (r == 0){ + do { + r = sd_journal_wait(journal, (uint64_t)-1); + assert(r >= 0); + } while (r == SD_JOURNAL_NOP); + + r = sd_journal_next(journal); + assert(r >= 0); + + if (r == 0) continue; // Try again if we still didn't get anything + } + + uint64_t timestamp = 0; + r = sd_journal_get_realtime_usec(journal, ×tamp); + assert(r >= 0); + + const void *data; + size_t length; + std::map kv; + + SD_JOURNAL_FOREACH_DATA(journal, data, length){ + std::string str((char*)data, length); + + // Split "KEY=VALUE"" on "=" and put in map + std::size_t found = str.find("="); + if (found != std::string::npos){ + kv[str.substr(0, found)] = str.substr(found + 1, std::string::npos); + } + } + + capnp::MallocMessageBuilder msg; + cereal::Event::Builder event = msg.initRoot(); + event.setLogMonoTime(nanos_since_boot()); + + // Build message + auto androidEntry = event.initAndroidLog(); + androidEntry.setTs(timestamp); + androidEntry.setMessage(json11::Json(kv).dump()); + if (kv.count("_PID")) androidEntry.setPid(std::atoi(kv["_PID"].c_str())); + if (kv.count("PRIORITY")) androidEntry.setPriority(std::atoi(kv["PRIORITY"].c_str())); + if (kv.count("SYSLOG_IDENTIFIER")) androidEntry.setTag(kv["SYSLOG_IDENTIFIER"]); + + pm.send("androidLog", msg); + } + + sd_journal_close(journal); + return 0; +} diff --git a/selfdrive/manager.py b/selfdrive/manager.py index 90b6b221..c81c6f27 100755 --- a/selfdrive/manager.py +++ b/selfdrive/manager.py @@ -217,9 +217,13 @@ persistent_processes = [ 'uploader', ] -if ANDROID: +if not PC: persistent_processes += [ 'logcatd', + ] + +if ANDROID: + persistent_processes += [ 'tombstoned', 'updated', 'deleter', diff --git a/tools/ubuntu_setup.sh b/tools/ubuntu_setup.sh index 6b363697..94c0850d 100755 --- a/tools/ubuntu_setup.sh +++ b/tools/ubuntu_setup.sh @@ -35,6 +35,7 @@ sudo apt-get update && sudo apt-get install -y \ libczmq-dev \ libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev \ libsdl1.2-dev libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev libfreetype6-dev \ + libsystemd-dev \ locales \ ocl-icd-libopencl1 \ ocl-icd-opencl-dev \