3drobotics / solodevguide

Solo Development Guide (SDG).
http://dev.3dr.com/
41 stars 62 forks source link

Only ~52% of CPU available - background processes #314

Open alexblack opened 8 years ago

alexblack commented 8 years ago

I'm processing images on the Solo, and realizing that performance is slower than it might be due to background processes using some of the CPU. For example, after killing some processes time to run cv2.Canny on an image dropped from 250ms to 150ms.

I wrote a python program cpu.py to do an infinite loop, and top reports it uses ~52% cpu:

Mem: 142636K used, 369248K free, 0K shrd, 16168K buff, 58708K cached
CPU:  73% usr  22% sys   0% nic   0% idle   0% io   0% irq   3% sirq
Load average: 2.45 1.06 0.40 2/80 949
  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
  933   894 root     R     5924   1%  54% python cpu.py

Can I kill some of these processes without much impact? I'm flying the drone using dronekit, but also using the remote occasionally to abort a flight, or move the drone around manually. Not using the gimbal, or smart shot, or much else.

Sample CPU logging:

2016-05-31 06:03:12,468
   pid name                  utime  stime  pct
   895 (python)                 78     31  11%
   881 (telem_down)             19     87  11%
    71 (mmcqd/0)                 0     61   6%
   899 (python)                 37     17   5%
    64 (kworker/0:2)             0     53   5%
   896 (python)                 34      4   4%
   883 (pixrc_udp)               4     21   3%
   880 (python)                 12      3   2%
   872 (dataflash_logge)         5      7   1%
   898 (python)                  5      0   1%
peterbarker commented 8 years ago

On Mon, 30 May 2016, Alex Black wrote:

Can I kill these processes without any impact?

No. They're there for a reason :-) OTOH, you can probably do without some of the functionality.

  • /usr/bin/main.py - Looks like used for smart shot?

Not sure what this does. I do believe it is smart shots, 'though.

  • /usr/bin/telem_forwarder - Looks like used for smart shot?

telem_forwarder takes telemetry data from the PixHawk and gives it out to clients - most notably Artoo.

Kill this one if you don't care about being able to fly your Copter....

  • /usr/bin/pixrc - when I killed this the drone made a noise and lights flashed... d'oh

Likewise :-)

Peter Barker | Programmer,Sysadmin,Geek. pbarker@barker.dropbear.id.au | You need a bigger hammer. :: It's a hack! Expect underscores! - Nigel Williams

alexblack commented 8 years ago

@peterbarker ok, so sounds like I can kill /usr/bin/.main.py then?

What is Artoo?

alexblack commented 8 years ago

Say my goal here is to fly the drone as bare bones as possible, is there anything else I might be able to do without?

telem_forwarder appears to take 10-12% cpu

  PID  PPID USER     STAT   VSZ %VSZ %CPU COMMAND
 8213  8199 root     S    29900   6%  13% /usr/bin/telem_forwarder
 8150     2 root     SW       0   0%   7% [kworker/0:2]
 7488     1 root     S    29968   6%   2% /usr/bin/pixrc
  880     1 root     S    12624   2%   2% dataflash_logger
hamishwillee commented 8 years ago

Artoo is the internal name for the Solo Controller. The forwarder sends telemetry to the controller.

alexblack commented 8 years ago

I've freed up a lot of the CPU from background processes by commenting out these lines in /etc/inittab and then restarting the solo:

#TOP:345:respawn:proc_top /log/3dr-top.log
#TEMP:345:respawn:log_temp
#DSYN:4:respawn:dataFlashMAVLink-to-artoo.py
#DFL:4:respawn:dataflash_logger
#API:4:respawn:run_shotmanager.sh

Then, before calling dronekit.connect you need to fire up Mav (which used to be done, along with shot manager, in /usr/bin/run_shotmanager.sh):

def startMav():
  """Requried if you're not running `/usr/bin/run_shotmanager.sh` on the Solo"""
  print "Starting up MAV..."
  from dronekit_solo import SoloVehicle
  from dronekit.mavlink import MAVConnection
  mav_vehicle = dronekit.connect("udpout:127.0.0.1:14560", wait_ready=False, vehicle_class=SoloVehicle, source_system=255, use_native=True, heartbeat_timeout=-1)
  out = MAVConnection("udpout:127.0.0.1:14550", source_system=254)
  mav_vehicle._handler.pipe(out)
  out.start()    
  print "Sleeping 5s to let MAV start..."
  # Not sure if this sleep is needed or not...
  time.sleep(5)
  print "Started MAV."

This combined with setting my process priority higher:

def setProcessPriority():
  """Set our process priority high so we get more CPU time hopefully"""
  import os, psutil
  p = psutil.Process(os.getpid())
  old = p.nice()
  p.nice(-20)
  print "Process {} priority set from {} to {}".format(p, old, p.nice())

Has got my process's CPU usage up from ~52% to ~92%.