Go Down

Topic: Bridge is slow, speed boost not 21,950% but 1,000,000% from Bridge. (Read 4938 times) previous topic - next topic

sonnyyu

Far better way:

Speed boost 1,000,000% from Bridge

Linux Industrial I/O Subsystem


Install software ser2net ( C programming based):

Code: [Select]

opkg update
opkg install ser2net
opkg install coreutils-stty


Disable serial port:

Code: [Select]

nano /etc/inittab
#ttyATH0::askfirst:/bin/ash --login


Restart Yun

Setup ser2net:

Code: [Select]

nano /etc/init.d/ser2net


Code: [Select]
#!/bin/sh /etc/rc.common
# Ser2Net Init Script
START=10
STOP=15
start() {
        /usr/bin/stty -F /dev/ttyATH0 raw -clocal -echo 115200
        ser2net -C "6001:raw:600:/dev/ttyATH0:115200"
}
stop(){
        pid=`ps w | grep "ser2net"  | grep -v grep |awk '{print $1}'`
        kill -9 $pid
}


Code: [Select]

chmod 755 /etc/init.d/ser2net

/etc/init.d/ser2net enable

/etc/init.d/ser2net start




sonnyyu

ATmega32u4 code:

Code: [Select]
void setup() {
  Serial1.begin(115200);
}
void loop() {
  while (true) {
    if (Serial1.available()) {
      int c = Serial1.read();
      if (c == '~') {                 //    Tilde '~' key pressed  for start sampling?
        while (Serial1.read() != -1);
        break;
      }
    }
    delay(10);
  }
  for ( unsigned long  i = 0; i <= 4294967295; i++) {
    Serial1.print(i);
    Serial1.print(',');
    Serial1.println(random(1023));
    Serial1.flush();
    if (Serial1.available()) {
      int c = Serial1.read();
      if (c == '^') {                 //   '^' key pressed for stop?
        break;
      }
    }
    delayMicroseconds(100);
  }
}

sonnyyu

Testing:

Code: [Select]
telnet localhost 6001

Code: [Select]

0,897
1,40
2,630
...
30733,366
30734,389
30735,734


at 35 seconds, 878 trans/s. speed boost 21950%



sonnyyu

Make above solution internet enable:

at firewall open tcp port 6001.


Code: [Select]
telnet 192.168.0.104 6001


192.168.0.104 is Yun 's IP address.




sonnyyu

Save telnet console output into file:


Code: [Select]
mv /usr/bin/tee /usr/bin/tee.bk
opkg update
opkg  install coreutils-tee


Code: [Select]
telnet localhost 6001 | tee -a yun.txt

Save file into RAM disk for high speed:

Code: [Select]
telnet localhost 6001 | tee -a /tmp/yun.txt





ShapeShifter

Very interesting! Thank you for posting this alternative.

But I'm not sure where you get the 4 transactions per second? There may be things you can do with the Bridge (like synchronously run a long Process) that might be that slow, but the code you posted is essentially a replacement for the Console class, and I've never seen it be that slow. I've slightly modified your code to use the Console class (added Bridge.begin() and Console.begin() instead of Serial1.begin()) and changed all other instances of Serial1 to Console. When I run that code, I get 34 lines per second.

Yes, 34 lines per second is slower than 878 lines per second, but we can't make a direct comparison without repeating your test above on my setup. This is necessary because there are other variables at play besides the raw serial port speed, for example, the SSH output is going over our local networks and being read and displayed by our SSH clients - your network and client might be running at a very different speed than mine, and may have very different network loading.

So to have a meaningful comparison, I tried running your example, and I got hung up on the first step:

Code: [Select]
root@Yun4:~# opkg update
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/Packages.gz.
Updated list of available packages in /var/opkg-lists/attitude_adjustment.
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/Packages.sig.
Signature check passed.
root@Yun4:~# opkg install ser2net
Installing ser2net (2.7-2) to root...
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/ser2net_2.7-2_ar71xx.ipk.
Collected errors:
 * opkg_download: Failed to download http://downloads.arduino.cc/openwrtyun/1/packages/ser2net_2.7-2_ar71xx.ipk: Error.
 * opkg_install_pkg: Failed to download ser2net. Perhaps you need to run 'opkg update'?
 * opkg_install_cmd: Cannot install package ser2net.
root@Yun4:~#


Being unable to duplicate your test, we can't draw any valid conclusions. I'm willing to concede that the Bridge Console is going to be slower than ser2net, but I contend that the difference is not nearly as dramatic as you state.

Before trying your method, I think people should be able to answer YES to these three questions:
  • Do you really need the extra speed? (I'll bet most applications don't.)
  • Are you willing to give up all other Bridge capabilities to get a faster console?
  • Are you willing to disable your only Linux console port that would let you log in and fix problems if you run into Linux issues that prevent network access using SSH?


This is the sketch I used to test the Console speed:
Code: [Select]
#include <Bridge.h>
#include <Console.h>

void setup() {
  Bridge.begin();
  Console.begin();
}

void loop() {
  while (true) {
    if (Console.available()) {
      int c = Console.read();
      if (c == '~') {                 //    Tilde '~' key pressed  for start sampling?
        while (Console.read() != -1);
        break;
      }
    }
    delay(10);
  }
  for ( unsigned long  i = 0; i <= 4294967295; i++) {
    Console.print(i);
    Console.print(',');
    Console.println(random(1023));
    Console.flush();
    if (Console.available()) {
      int c = Console.read();
      if (c == '^') {                 //   '^' key pressed for stop?
        break;
      }
    }
    delayMicroseconds(100);
  }
}


And this is how I accessed it:
Code: [Select]
ssh root@ yourYunsName.local 'telnet localhost 6571'

There are no Linux changes required. Can you run that on your setup and post the speed result? That will be the only meaningful comparison.

sonnyyu

Quote
root@Yun4:~# opkg update
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/Packages.gz.
Updated list of available packages in /var/opkg-lists/attitude_adjustment.
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/Packages.sig.
Signature check passed.
root@Yun4:~# opkg install ser2net
Installing ser2net (2.7-2) to root...
Downloading http://downloads.arduino.cc/openwrtyun/1/packages/ser2net_2.7-2_ar71xx.ipk.
Collected errors:
 * opkg_download: Failed to download http://downloads.arduino.cc/openwrtyun/1/packages/ser2net_2.7-2_ar71xx.ipk: Error.
 * opkg_install_pkg: Failed to download ser2net. Perhaps you need to run 'opkg update'?
 * opkg_install_cmd: Cannot install package ser2net.
Code: [Select]
wget http://downloads.arduino.cc/openwrtyun/1/packages/ser2net_2.7-2_ar71xx.ipk
opkg install ser2net_2.7-2_ar71xx.ipk




ShapeShifter

That works. I get 841 lines per second using ser2net, which corresponds reasonably well with your findings. Almost 24 times faster than using the Console class.

I tried one more test: using a YunServer/YunClient in the sketch, in conjunction with the Bridge. This allows an external Telnet client to connect directly with the Yun to transfer data, eliminating the need to SSH into the Yun, and then run a local Telnet client (it removes a little bit of the overhead. As expected, it is faster than using the Console class (and also more complicated) but not nearly as fast as ser2net.

Code: [Select]

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server(255);
YunClient client;

void setup() {
  Bridge.begin();
   server.noListenOnLocalhost();
   server.begin();
   client = server.accept();
}

void loop() {
  while (client.connected()) {
    if (client.available()) {
      int c = client.read();
      if (c == '~') {                 //    Tilde '~' key pressed  for start sampling?
        while (client.read() != -1);
        break;
      }
    }
    delay(10);
  }
 
  if (client.connected()) {
    for ( unsigned long  i = 0; i <= 4294967295; i++) {
      client.print(i);
      client.print(',');
      client.println(random(1023));
      client.flush();
      if (client.available()) {
        int c = client.read();
        if (c == '^') {                 //   '^' key pressed for stop?
          break;
        }
      }
      delayMicroseconds(100);
    }
  }
  else
    client = server.accept();
}


Method   Lines/sec   Improvement
Console       34             0%
YunClient     36             6%
ser2Net      841         2,374%



Console Class:
Pros:
  • Easy
  • Allows simultaneous use of other Bridge class functions
  • No changes to Linux
  • Keeps console TTY active for emergency use using YunSerialTerminal sketch
Cons:
  • Slow (but may be fast enough for many applications, especially those using the Console as debug output.)

YunClient Class:
Pros:
  • Flexible, allows direct connection by any network socket
  • Allows simultaneous use of other Bridge class functions
  • No changes to Linux
  • Keeps console TTY active for emergency use using YunSerialTerminal sketch
Cons:
  • Slow (but may be fast enough for many applications)
  • More complicated than Console class

ser2net:
Pros:
  • SPEED!!! (but not all applications need that speed.)
  • Simple (regular old serial port techniques in sketch.)
Cons:
  • Additional packages to load into Linux
  • Requires Linux configuration files
  • Disables console TTY preventing emergency use using YunSerialTerminal sketch


This looks like a great alternative if you need raw unadulterated speed over the MCU/Linux serial port, and you don't need any other functions over that port.

jessemonroy650


Robin2

ATmega32u4 code:
I've only noticed this Thread today.

The code in Reply #1 looks very like the code you would use if you were communicating between a Leonardo and a PC (except that then you would use Serial).

Has one our experts come to the conclusion that the Yun works well as a "normal" Linux computer?   :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ShapeShifter

The code in Reply #1 looks very like the code you would use if you were communicating between a Leonardo and a PC (except that then you would use Serial).
Yes, it looks like a way to take code that uses a serial port to talk to a remote computer, and change it so that it uses the network to talk to that remote computer. No changes to the sketch code, other than perhaps substituting Serial1 for Serial, but of course it takes changes to the PC code to use a network socket instead of a serial port.

Quote
Has one our experts come to the conclusion that the Yun works well as a "normal" Linux computer?   :)
Actually, it looks like quite the opposite to me. The Linux side is used to translate the serial port to a network socket server, to simplify access from a remote computer. In essence, the Linux side is pretty much bypassed and turned into a dumb communications server.

I suppose one could access the network socket from the Linux side by opening a port to localhost, and that would allow communications with code on the Linux side, but why would you do that? It would be much simpler and lower overhead to just have the Linux code use /dev/ttyATH0 to talk to the sketch directly.

Your goal of having someone turn a Yun into an RPi remains unfulfilled...  ;)

Robin2

Actually, it looks like quite the opposite to me. The Linux side is used to translate the serial port to a network socket server,
I guess that floated high over my head  :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sonnyyu

...
Cons:
  • Additional packages to load into Linux
  • Requires Linux configuration files
  • Disables console TTY preventing emergency use using YunSerialTerminal sketch

...
Disables console TTY preventing emergency use using YunSerialTerminal sketch

Not better solution but work around.

Rootfs on External Storage microSD card (extroot)

http://wiki.openwrt.org/doc/howto/extroot

http://forum.arduino.cc/index.php?topic=228204.msg1651254#msg1651254

Edit /etc/inittab at Rootfs on microSD card, comment out or delete:

Code: [Select]
#ttyATH0::askfirst:/bin/ash --login

Whenever you need access serial port again, just remove microSD card and reboot.
Whenever you need access serial port again, just remove microSD card and reboot.







sonnyyu

...
Before trying your method, I think people should be able to answer YES to these three questions:
  • Do you really need the extra speed? (I'll bet most applications don't.)
  • Are you willing to give up all other Bridge capabilities to get a faster console?
  • Are you willing to disable your only Linux console port that would let you log in and fix problems if you run into Linux issues that prevent network access using SSH?

...
Quote
Do you really need the extra speed? (I'll bet most applications don't.)
The race car could go up to 300 mph, same time it could run at 50 mph. make Toyota Corolla run at 200 mph?
I will start new thread about how to speed boost 1,000,000% from Bridge soon.



ShapeShifter

make Toyota Corolla run at 200 mph?
If you figured out how to make it go that fast, I wouldn't want to ride in it!  :o

But at 50 mph that stock Corolla has many advantages over a race car that's also doing 50, as it's much more comfortable with a softer suspension, air conditioning/heating, nice sound system, adjustable seats, and many other refinements.

The metaphor stands up well, because while you could probably get that serial port to do all the things that the Bridge can do at once (run processes, read/write files, REST API, get/put data store, mailbox, etc.) it will take a lot more work to do so - if you don't need the speed, the Bridge is a lot easier (more comfortable.)  ;)

Don't get me wrong, I think figuring ser2net out is a great contribution to the Arduino community, I applaud you for that. With the right need and application, I'm sure it will help people out, I just hope people realize it's not a universal Bridge replacement for all applications (and yes, I don't think you meant it that way and I'm not trying to imply that you did.)

Go Up