Merge pull request #87 from gendx/faster-console

Reduce the number of syscalls made by the console in libtock-rs.
This commit is contained in:
gendx
2020-06-20 11:05:27 +02:00
committed by GitHub
4 changed files with 115 additions and 4 deletions

View File

@@ -0,0 +1,98 @@
diff --git a/src/console.rs b/src/console.rs
index ecd7ad1..ae1b826 100644
--- a/src/console.rs
+++ b/src/console.rs
@@ -16,33 +16,63 @@ mod allow_nr {
pub const SHARE_BUFFER: usize = 1;
}
+const BUFFER_SIZE: usize = 1024;
+
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<S: AsRef<[u8]>>(&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(&not_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 = &not_written_yet[num_bytes_to_print..];
}
}
- fn flush(&mut self, num_bytes_to_print: usize) {
+ 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;
+
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 +89,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 +98,12 @@ impl Console {
}
}
+impl Drop for Console {
+ fn drop(&mut self) {
+ self.flush();
+ }
+}
+
impl fmt::Write for Console {
fn write_str(&mut self, string: &str) -> Result<(), fmt::Error> {
self.write(string);

View File

@@ -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) * <A::Frequency>::frequency() / 1000000;
+ let interval = (1000 as u32) * <A::Frequency>::frequency() / 1000000;
let tics = self.alarm.now().wrapping_add(interval);
self.alarm.set_alarm(tics);
})

View File

@@ -1,5 +1,5 @@
1003863864e06553e730eec6df4bf8d30c99f697ef9380efdc35eba679b4db78 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin 0b54df6d548849e24d67b9b022ca09cb33c51f078ce85d0c9c4635ffc69902e1 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin
022268c93fa8bbd9e54e082982b87c10a0e7c0486704de8219d1bb374304636a target/nrf52840dk_merged.hex 9ff63684ca08375e643f14f33dc6dc8131681bb562fb0df18f9c7f637e90cc73 target/nrf52840dk_merged.hex
052eec0ae526038352b9f7573468d0cf7fb5ec331d4dc1a2df75fdbd514ea5ca third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle.bin 052eec0ae526038352b9f7573468d0cf7fb5ec331d4dc1a2df75fdbd514ea5ca third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle.bin
d2373ac9df2ba8feff88f19e67ec87a58e635b94f0a0f759b6fcf4c750b256c9 target/nrf52840_dongle_merged.hex d2373ac9df2ba8feff88f19e67ec87a58e635b94f0a0f759b6fcf4c750b256c9 target/nrf52840_dongle_merged.hex
908d7f4f40936d968b91ab6e19b2406612fe8c2c273d9c0b71ef1f55116780e0 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle_dfu.bin 908d7f4f40936d968b91ab6e19b2406612fe8c2c273d9c0b71ef1f55116780e0 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle_dfu.bin

View File

@@ -1,5 +1,5 @@
c182bb4902fff51b2f56810fc2a27df3646cd66ba21359162354d53445623ab8 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin 29382e72d0f3c6a72ce9517211952ff29ea270193d7f0ddc48ca69009ee29925 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin
d8b62ece387a77cc21f2c10a5f5d65d0d57bf4739b47fd86d2c9ecdd90fbfd7e target/nrf52840dk_merged.hex bb2fbf0d9dab2b489a49d1dc3db8923086ab109d14f1f1aa8296f086a03b75dd target/nrf52840dk_merged.hex
30f239390ae9bef0825731e4c82d40470fc5e9bded2bf0d942e92dbb5d4faba1 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle.bin 30f239390ae9bef0825731e4c82d40470fc5e9bded2bf0d942e92dbb5d4faba1 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle.bin
c9349bd480b30e28214bb8d58d10938889050b92d34fbeb70e3110919b3a2601 target/nrf52840_dongle_merged.hex c9349bd480b30e28214bb8d58d10938889050b92d34fbeb70e3110919b3a2601 target/nrf52840_dongle_merged.hex
e3acf15d5ae3a22aecff6cc58db5fc311f538f47328d348b7ad7db7f9ab5e72c third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle_dfu.bin e3acf15d5ae3a22aecff6cc58db5fc311f538f47328d348b7ad7db7f9ab5e72c third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle_dfu.bin