Improve CommandSave & large devcount

Begonnen von Nestor, 28 November 2023, 00:45:23

Vorheriges Thema - Nächstes Thema

Nestor

I'm running a Modbus slave device that gets polled every second by a client. The client closes the connection during a Fhem save command as incomplete data is received because Fhem is blocked.
time ~/telnet_client localhost 7072 "save"

real 0m0.746s
user 0m0.046s
sys 0m0.033s

The $devcount increases by 15 every minute due to a dozen external scripts that update readings via telnet which create a temporary telnet client device.
fhem> uptime
29 days, 05:19:37
fhem> { $devcount }
735290
fhem> count
Count: 368 devices in total.

I noticed the CommandSave sub is looping through the whole $devcount (735290) instead of the 368 devices.
https://svn.fhem.de/trac/browser/trunk/fhem/fhem.pl#L1759
Is this something that could be improved?

Nestor

Testing out this fix now.
--- - 2023-11-28 17:29:11.339588505 +0100
+++ /srv/fhem/fhem.pl 2023-11-28 17:28:31.897578120 +0100
@@ -237,6 +237,7 @@
 use vars qw($auth_refresh);
 use vars qw($cmdFromAnalyze);   # used by the warnings-sub
 use vars qw($devcount);         # Maximum device number, used for storing
+use vars qw($clientcount);      # Last device client number, used for internal NR
 use vars qw($devcountPrioSave); # Maximum prioSave device number
 use vars qw($unicodeEncoding);  # internal encoding is unicode (wide character)
 use vars qw($featurelevel);
@@ -310,6 +311,7 @@
 
 my $readytimeout = ($^O eq "MSWin32") ? 0.1 : 5.0;
 
+$clientcount = 100000;
 $init_done = 0;
 $lastDefChange = 0;
 $featurelevel = 6.2; # see also GlobalAttr
--- - 2023-11-28 17:27:24.593844933 +0100
+++ /srv/fhem/FHEM/TcpServerUtils.pm 2023-11-28 17:27:00.043913730 +0100
@@ -342,7 +342,7 @@
 
   my $cname = "${name}_${caddr}_${port}";
   my %nhash;
-  $nhash{NR}    = $devcount++;
+  $nhash{NR}    = $clientcount++;
   $nhash{NAME}  = $cname;
   $nhash{PEER}  = $caddr;
   $nhash{PORT}  = $port;

Save times are improved (0.75sec->0.18sec)
time ~/telnet_client localhost 7072 "save"

real 0m0.183s
user 0m0.050s
sys 0m0.030s

fhem> count
Count: 368 devices in total.

fhem> { $devcount }
1217
fhem> { $clientcount }
100230
$devcount is still a lot higher than the number of devices.

rudolfkoenig

Thanks for the hint and the idea for the solution.
I changed the code accordingly.

Zitat$devcount is still a lot higher than the number of devices.
Temporary devices are created in other places too, I'll try to draw the attention of the other developers to this issue.

rudolfkoenig


Nestor

Zitat von: rudolfkoenig am 29 November 2023, 13:36:51Thanks for the hint and the idea for the solution.
I changed the code accordingly.

It seems there is an unused variable in your patch? $tmpdevcount

My large devcount is mostly due to comments in my config files.
They receive a number also to determine the line number in the saved config file.
fhem> { %comments }
823
fhem> { $devcount }
1217
fhem> { $devcount - %comments }
394
fhem> count .+ raw
368