From c7493d1b9de5e7aed8ab9f558c47de686e7eec90 Mon Sep 17 00:00:00 2001 From: Guillaume Endignoux Date: Tue, 17 Mar 2020 16:26:05 +0100 Subject: [PATCH 1/4] Reduce the number of syscalls made by the console in libtock-rs. --- patches/libtock-rs/08-buffered-console.patch | 95 ++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 patches/libtock-rs/08-buffered-console.patch diff --git a/patches/libtock-rs/08-buffered-console.patch b/patches/libtock-rs/08-buffered-console.patch new file mode 100644 index 0000000..5d8a4fc --- /dev/null +++ b/patches/libtock-rs/08-buffered-console.patch @@ -0,0 +1,95 @@ +diff --git a/src/console.rs b/src/console.rs +index ecd7ad1..785de01 100644 +--- a/src/console.rs ++++ b/src/console.rs +@@ -16,33 +16,58 @@ mod allow_nr { + pub const SHARE_BUFFER: usize = 1; + } + ++const BUFFER_SIZE: usize = 64; ++ + pub struct Console { +- allow_buffer: [u8; 64], ++ allow_buffer: [u8; BUFFER_SIZE], ++ count_pending: usize, + } + + impl Console { + pub fn new() -> Console { + Console { +- allow_buffer: [0; 64], ++ allow_buffer: [0; BUFFER_SIZE], ++ count_pending: 0, + } + } + ++ fn is_empty(&self) -> bool { ++ self.count_pending == 0 ++ } ++ ++ fn is_full(&self) -> bool { ++ self.allow_buffer.len() == self.count_pending ++ } ++ ++ fn available_len(&self) -> usize { ++ self.allow_buffer.len() - self.count_pending ++ } ++ + pub fn write>(&mut self, text: S) { + let mut not_written_yet = text.as_ref(); + while !not_written_yet.is_empty() { +- let num_bytes_to_print = self.allow_buffer.len().min(not_written_yet.len()); +- self.allow_buffer[..num_bytes_to_print] ++ let num_bytes_to_print = self.available_len().min(not_written_yet.len()); ++ self.allow_buffer[self.count_pending..(self.count_pending + num_bytes_to_print)] + .copy_from_slice(¬_written_yet[..num_bytes_to_print]); +- self.flush(num_bytes_to_print); ++ self.count_pending += num_bytes_to_print; ++ ++ if self.is_full() { ++ self.flush(); ++ } ++ + not_written_yet = ¬_written_yet[num_bytes_to_print..]; + } + } + +- fn flush(&mut self, num_bytes_to_print: usize) { ++ fn flush(&mut self) { ++ let count = self.count_pending; ++ // Clear the buffer even in case of error, to avoid an infinite loop. ++ self.count_pending = 0; ++ + let result = syscalls::allow( + DRIVER_NUMBER, + allow_nr::SHARE_BUFFER, +- &mut self.allow_buffer[..num_bytes_to_print], ++ &mut self.allow_buffer[..count], + ); + if result.is_err() { + return; +@@ -59,8 +84,7 @@ impl Console { + return; + } + +- let result_code = +- unsafe { syscalls::command(DRIVER_NUMBER, command_nr::WRITE, num_bytes_to_print, 0) }; ++ let result_code = unsafe { syscalls::command(DRIVER_NUMBER, command_nr::WRITE, count, 0) }; + if result_code < 0 { + return; + } +@@ -69,6 +93,14 @@ impl Console { + } + } + ++impl Drop for Console { ++ fn drop(&mut self) { ++ if !self.is_empty() { ++ self.flush(); ++ } ++ } ++} ++ + impl fmt::Write for Console { + fn write_str(&mut self, string: &str) -> Result<(), fmt::Error> { + self.write(string); From a7c79c4f23a378c9793e3d0c8f6f6e041738f82e Mon Sep 17 00:00:00 2001 From: Guillaume Endignoux Date: Wed, 18 Mar 2020 15:28:37 +0100 Subject: [PATCH 2/4] Resolve review comments. --- patches/libtock-rs/08-buffered-console.patch | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/patches/libtock-rs/08-buffered-console.patch b/patches/libtock-rs/08-buffered-console.patch index 5d8a4fc..ba4c525 100644 --- a/patches/libtock-rs/08-buffered-console.patch +++ b/patches/libtock-rs/08-buffered-console.patch @@ -1,8 +1,8 @@ diff --git a/src/console.rs b/src/console.rs -index ecd7ad1..785de01 100644 +index ecd7ad1..ae1b826 100644 --- a/src/console.rs +++ b/src/console.rs -@@ -16,33 +16,58 @@ mod allow_nr { +@@ -16,33 +16,63 @@ mod allow_nr { pub const SHARE_BUFFER: usize = 1; } @@ -55,7 +55,12 @@ index ecd7ad1..785de01 100644 } - fn flush(&mut self, num_bytes_to_print: usize) { -+ fn flush(&mut self) { ++ pub fn flush(&mut self) { ++ if self.is_empty() { ++ // Don't trigger any syscall if the buffer is empty. ++ return; ++ } ++ + let count = self.count_pending; + // Clear the buffer even in case of error, to avoid an infinite loop. + self.count_pending = 0; @@ -68,7 +73,7 @@ index ecd7ad1..785de01 100644 ); if result.is_err() { return; -@@ -59,8 +84,7 @@ impl Console { +@@ -59,8 +89,7 @@ impl Console { return; } @@ -78,15 +83,13 @@ index ecd7ad1..785de01 100644 if result_code < 0 { return; } -@@ -69,6 +93,14 @@ impl Console { +@@ -69,6 +98,12 @@ impl Console { } } +impl Drop for Console { + fn drop(&mut self) { -+ if !self.is_empty() { -+ self.flush(); -+ } ++ self.flush(); + } +} + From 2094f8156c5a34fefaa8be00a34b087b289d0f8e Mon Sep 17 00:00:00 2001 From: Guillaume Endignoux Date: Fri, 19 Jun 2020 18:21:10 +0200 Subject: [PATCH 3/4] Increase size of console buffering. --- patches/libtock-rs/08-buffered-console.patch | 2 +- patches/tock/05-backport-fix-1960.patch | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 patches/tock/05-backport-fix-1960.patch diff --git a/patches/libtock-rs/08-buffered-console.patch b/patches/libtock-rs/08-buffered-console.patch index ba4c525..fec6963 100644 --- a/patches/libtock-rs/08-buffered-console.patch +++ b/patches/libtock-rs/08-buffered-console.patch @@ -6,7 +6,7 @@ index ecd7ad1..ae1b826 100644 pub const SHARE_BUFFER: usize = 1; } -+const BUFFER_SIZE: usize = 64; ++const BUFFER_SIZE: usize = 1024; + pub struct Console { - allow_buffer: [u8; 64], diff --git a/patches/tock/05-backport-fix-1960.patch b/patches/tock/05-backport-fix-1960.patch new file mode 100644 index 0000000..4bffaa8 --- /dev/null +++ b/patches/tock/05-backport-fix-1960.patch @@ -0,0 +1,13 @@ +diff --git a/capsules/src/segger_rtt.rs b/capsules/src/segger_rtt.rs +index 89a9ddbf..135e7b45 100644 +--- a/capsules/src/segger_rtt.rs ++++ b/capsules/src/segger_rtt.rs +@@ -240,7 +240,7 @@ impl<'a, A: hil::time::Alarm<'a>> uart::Transmit<'a> for SeggerRtt<'a, A> { + + // Start a short timer so that we get a callback and + // can issue the callback to the client. +- let interval = (100 as u32) * ::frequency() / 1000000; ++ let interval = (1000 as u32) * ::frequency() / 1000000; + let tics = self.alarm.now().wrapping_add(interval); + self.alarm.set_alarm(tics); + }) From 3d9e283eeb7dff9009828a2d2704c27fc3e1a925 Mon Sep 17 00:00:00 2001 From: Guillaume Endignoux Date: Fri, 19 Jun 2020 18:33:34 +0200 Subject: [PATCH 4/4] Update reference hashes. --- reproducible/reference_binaries_macos-10.15.sha256sum | 4 ++-- reproducible/reference_binaries_ubuntu-18.04.sha256sum | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/reproducible/reference_binaries_macos-10.15.sha256sum b/reproducible/reference_binaries_macos-10.15.sha256sum index fce6c5d..6d3272e 100644 --- a/reproducible/reference_binaries_macos-10.15.sha256sum +++ b/reproducible/reference_binaries_macos-10.15.sha256sum @@ -1,5 +1,5 @@ -1003863864e06553e730eec6df4bf8d30c99f697ef9380efdc35eba679b4db78 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin -022268c93fa8bbd9e54e082982b87c10a0e7c0486704de8219d1bb374304636a target/nrf52840dk_merged.hex +0b54df6d548849e24d67b9b022ca09cb33c51f078ce85d0c9c4635ffc69902e1 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin +9ff63684ca08375e643f14f33dc6dc8131681bb562fb0df18f9c7f637e90cc73 target/nrf52840dk_merged.hex 052eec0ae526038352b9f7573468d0cf7fb5ec331d4dc1a2df75fdbd514ea5ca third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle.bin d2373ac9df2ba8feff88f19e67ec87a58e635b94f0a0f759b6fcf4c750b256c9 target/nrf52840_dongle_merged.hex 908d7f4f40936d968b91ab6e19b2406612fe8c2c273d9c0b71ef1f55116780e0 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle_dfu.bin diff --git a/reproducible/reference_binaries_ubuntu-18.04.sha256sum b/reproducible/reference_binaries_ubuntu-18.04.sha256sum index b1ef07f..ea64676 100644 --- a/reproducible/reference_binaries_ubuntu-18.04.sha256sum +++ b/reproducible/reference_binaries_ubuntu-18.04.sha256sum @@ -1,5 +1,5 @@ -c182bb4902fff51b2f56810fc2a27df3646cd66ba21359162354d53445623ab8 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin -d8b62ece387a77cc21f2c10a5f5d65d0d57bf4739b47fd86d2c9ecdd90fbfd7e target/nrf52840dk_merged.hex +29382e72d0f3c6a72ce9517211952ff29ea270193d7f0ddc48ca69009ee29925 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin +bb2fbf0d9dab2b489a49d1dc3db8923086ab109d14f1f1aa8296f086a03b75dd target/nrf52840dk_merged.hex 30f239390ae9bef0825731e4c82d40470fc5e9bded2bf0d942e92dbb5d4faba1 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle.bin c9349bd480b30e28214bb8d58d10938889050b92d34fbeb70e3110919b3a2601 target/nrf52840_dongle_merged.hex e3acf15d5ae3a22aecff6cc58db5fc311f538f47328d348b7ad7db7f9ab5e72c third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle_dfu.bin