DevelopmentProfiling

Aus FHEMWiki
Zur Navigation springen Zur Suche springen

Profiling FHEM

What is profiling?

In software engineering, profiling ("program profiling", "software profiling") is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or frequency and duration of function calls. The most common use of profiling information is to aid program optimization. (from Wikipedia)

Profiling step-by-step

You need Devel::NYTProf from CPAN.

On Debian based systems install package "libdevel-nytprof-perl": sudo apt-get install libdevel-nytprof-perl

Start fhem.pl as follows:

perl -d:NYTProf /opt/fhem/fhem.pl /opt/fhem/conf/fhem.conf

Let it run for a while and then shut down FHEM.

You will find a bunch of nytprof.out files in /opt/fhem. nytprof.out contains the profile for the parent process and each nytprof.out.<pid> file contains the profile for a child process forked by the parent process. Not nytprof.out but the largest nytprof.out.<pid> file contains the profile of the master process since FHEM does an initial fork when it is ready.

You transfer the profile of the master process into something human-readable by issueing the command

nytprofhtml -f nytprof.out.<pid>

This produces a subfolder names nytprof. Direct your browser to file index.html therein. You will get a bunch of information how FHEM spends its time.

FHEM Profilieren

Was ist profilieren

Kurz gesagt, mittels profilieren kann man ein Programm in seinem Ablauf untersuchen und so schauen, wo z.B. die Performance verbessert werden kann.

Profilieren Schritt für Schritt

Benötigt wird das Paket Devel::NYTProf dies kann auf Debian Systemen mittels sudo apt-get install libdevel-nytprof-perl installiert werden. Weiterhin benötigt wird eine .conf Datei. Soll unsere aktuelle komplette Instanz profiliert werden, können wir einfach wie folgt vorgehen. Voraussetzung ist, das unsere fhem.cfg Datei im Hauptordner fhem abgelegt ist. Typischerweise wäre das unter /opt/fhem. Dann können wir wie folgt vorgehen auf der Console:

sudo cp /opt/fhem/fhem.cfg /opt/fhem/fhem.conf

Nun haben wir im Ordner /opt/fhem die Datei fhem.conf erstellt. Als nächstes müssen wir fhem so starten, dass es profiliert wird:

  1. Fhem beenden. Entweder in fhem mittels "shutdown" in der Commandzeile, oder mittels Console: sudo /etc/init.d/fhem stop
  2. In den Ordner fhem wechseln cd /opt/fhem
  3. fhem wie folgt starten: sudo perl -d:NYTProf /opt/fhem/fhem.pl /opt/fhem/fhem.conf

Nun kann fhem solange laufen, wie wir es benötigen, oder der Prozess, welchen wir beobachten wollen, eingetreten ist. Es kann vorkommen, dass während dieser Zeit auf der Console ständig angezeigt was fhem aktuell macht. Um den Prozess nun zu beenden muss fhem herunter gefahren werden. Dafür sollte im Idealfall eine zweite Console geöffnet werden. Dort gehen wir wie folgt vor:

  1. Wir verbinden uns mittels Telnet mit unserem laufenden FHEM telnet <DeinFHEMServer> 7072
  2. Ein shutdown absetzen
  3. Fhem wird nun beendet.

Anmerkung: Sollte ein Fehler erscheinen, dass Telnet ein unbekannter Befehl ist, dann müsst ihr noch Telnet mittels sudo apt-get install telnet installieren.

Jetzt haben wir im Ordner /opt/fhem mindestens die Datei nytprof.out, manchmal auf noch nytprof.out.PIDNUMMER. Um zu testen, ob die Datei in Ordnung ist, kann folgender Befehl im Ordner /opt/fhem abgesetzt werden: sudo nytprofhtml Dies sollte ohne Fehlermeldung durchlaufen und ein Unterordner namens nytprof. mit HTML Inhalt erstellt worden sein. Über die index.html könnt ihr nun erkennen was euer fhem so die ganze Zeit macht.

Solltet ihr während des Prozesses über das WebUI von FHEM arbeiten wollen und ein save ausführen wollen, dann müsst ihr vorher die fhem.conf Datei noch wie folgt bearbeiten: sudo chown fhem:dialout fhem.conf

Links

official perlfaq: How do I profile my Perl programs?