gizmore / phpgdo

Reference GDOv7 core implementation in PHP.
Other
2 stars 1 forks source link

Get cpu cores plattform independent when installing FFMpeg #7

Closed flederohr closed 1 year ago

flederohr commented 2 years ago
00:13 [127.0.0.1:gdo_adm] - file_get_contents(/proc/cpuinfo): Failed to open stream: No such file or directory in GDO/CLI/Process.php line 56
Backtrace starts in gdo_adm.php line 417.
 - GDO\Install\Installer::installModules([{"temp":null,"priority":1,"version":"7.0.1","license":"GDOv7-LICENSE","authors":"gizmore <gizmore@w…nited":false}]) GDO/Install/Installer.php line 74.
 - GDO\Install\Installer::installModule(Module_FFMpeg#56, false) GDO/Install/Installer.php line 118.
 - GDO\FFMpeg\Module_FFMpeg->onInstall() GDO/FFMpeg/Module_FFMpeg.php line 80.
 - GDO\FFMpeg\Method\AutodetectBinaries->detectBinariesIfNeeded() GDO/FFMpeg/Method/AutodetectBinaries.php line 24.
 - GDO\FFMpeg\Module_FFMpeg->cfgFFMpegPath() GDO/FFMpeg/Module_FFMpeg.php line 65.
 - GDO\Core\GDO_Module->getConfigVar("ffmpeg_path") GDO/Core/GDO_Module.php line 622.
 - GDO\Core\GDO_Module->getConfigColumn("ffmpeg_path") GDO/Core/GDO_Module.php line 604.
 - GDO\Core\GDO_Module->buildConfigCache() GDO/Core/GDO_Module.php line 559.
 - GDO\FFMpeg\Module_FFMpeg->getConfig() GDO/FFMpeg/Module_FFMpeg.php line 62.
 - GDO\CLI\Process::cores() GDO/CLI/Process.php line 56.
 - file_get_contents("/proc/cpuinfo") [unknown file] line ?.

as /proc/cpuinfo only exist on a linux system but not on other Unix or MAC systems you could determine the number of cpu core with sysctl -a in these cases.

See also https://helloacm.com/how-to-get-number-of-cpu-cores-using-php-function-platform-independent/

Patch proposal:

diff --git a/GDO/CLI/Process.php b/GDO/CLI/Process.php
index 0594ebd..085c809 100644
--- a/GDO/CLI/Process.php
+++ b/GDO/CLI/Process.php
@@ -51,10 +51,25 @@ final class Process
                        {
                                return (int) getenv('NUMBER_OF_PROCESSORS');
                        }
-                       else
+                       else if (is_file('/proc/cpuinfo'))
                        {
                                return substr_count(file_get_contents('/proc/cpuinfo'), 'processor');
                        }
+                       else
+                       {
+                               $ps = @popen('sysctl -a', 'rb');
+                               if (false !== $ps)
+                               {
+                                       $output = stream_get_contents($ps);
+                                       preg_match('/hw.ncpu: (\d+)/', $output, $matches);
+                                       if ($matches)
+                                       {
+                                               $ans = intval($matches[1][0]);
+                                       }
+                                       pclose($ps);
+                                       return $ans;
+                               }
+                       }
                }
                catch (\Throwable $ex)
                {
gizmore commented 1 year ago

There is now GDO/Util/Load.php which gathers performance metrics for the new Module_Hydra - A system health watcher.

Your is_file call has very delightful performance aspects, considarably, but in future, an OS dependant multi provider module should be used.


EDIT:

This means there will be multiple repos implementing the same path / namespace.

phpgdo-os-windows and phpgdo-os-linux will both implement \GDO\OS.

This way you have zero isWindows() branching etc., and implementing os-amiga is easy. (TODO).

A disadvantage is core stuff needing a choice for which provider to choose, but There could be autochoose multi providers ^^

gizmore commented 1 year ago

Thx for the link, i learned quite essantial bits from that.

Upcoming thx commit:

Module OS postponed to later.