Skip to content

Commit 176126e

Browse files
Merge pull request rusoft#12 from rusoft/update-for-rpi
Detect CPU model and MHz two ways
2 parents 02c91fb + f5cb8c8 commit 176126e

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ env PHP_MEMORY_LIMIT=64 PHP_TIME_LIMIT=30 php bench.php
7979

8080
## ChangeLog
8181

82+
@ 2019-05-10, v1.0.34
83+
84+
* Поправлено определение модели CPU и частота в MHz для процессоров ARM.
85+
8286
@ 2019-05-01, v1.0.33
8387

8488
* Новый тест для классов - доступ в данным через публичные свойства,

bench.php

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
# Company : Code24 BV, The Netherlands #
1010
# Author : Sergey Dryabzhinsky #
1111
# Company : Rusoft Ltd, Russia #
12-
# Date : MAy 01, 2019 #
13-
# Version : 1.0.33 #
12+
# Date : May 10, 2019 #
13+
# Version : 1.0.34 #
1414
# License : Creative Commons CC-BY license #
1515
# Website : https://github.com/rusoft/php-simple-benchmark-script #
1616
# Website : https://git.rusoft.ru/open-source/php-simple-benchmark-script #
1717
# #
1818
################################################################################
1919
*/
2020

21-
$scriptVersion = '1.0.33';
21+
$scriptVersion = '1.0.34';
2222

2323
ini_set('display_errors', 0);
2424
ini_set('error_log', null);
@@ -272,6 +272,10 @@
272272
// Special for nginx
273273
header('X-Accel-Buffering: no');
274274

275+
if (file_exists('/usr/bin/taskset')) {
276+
shell_exec('/usr/bin/taskset -c -p 0 ' . getmypid());
277+
}
278+
275279
/** ------------------------------- Main Constants ------------------------------- */
276280

277281
$line = str_pad("-", 91, "-");
@@ -502,13 +506,17 @@ function getCpuInfo($fireUpCpu = false)
502506
{
503507
$cpu = array(
504508
'model' => '',
509+
'vendor' => '',
505510
'cores' => 0,
506511
'mhz' => 0.0,
512+
'max-mhz' => 0.0,
513+
'min-mhz' => 0.0,
507514
'mips' => 0.0
508515
);
509516

510517
if (!is_readable('/proc/cpuinfo')) {
511518
$cpu['model'] = 'Unknown';
519+
$cpu['vendor'] = 'Unknown';
512520
$cpu['cores'] = 1;
513521
return $cpu;
514522
}
@@ -518,6 +526,9 @@ function getCpuInfo($fireUpCpu = false)
518526
$i = 30000000;
519527
while ($i--) ;
520528
}
529+
if (file_exists('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq')) {
530+
$cpu['mhz'] = ((int)file_get_contents('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq'))/1000.0;
531+
}
521532

522533
// Code from https://github.com/jrgp/linfo/blob/master/src/Linfo/OS/Linux.php
523534
// Adopted
@@ -553,7 +564,8 @@ function getCpuInfo($fireUpCpu = false)
553564
$cpu['mhz'] = (int)hexdec($value) / 1000000.0;
554565
}
555566
break;
556-
case 'bogomips': // twice of MHz usualy
567+
case 'bogomips': // twice of MHz usualy on Intel/Amd
568+
case 'BogoMIPS': // twice of MHz usualy on Intel/Amd
557569
if (empty($cpu['mhz'])) {
558570
$cpu['mhz'] = (float)$value / 2.0;
559571
}
@@ -570,6 +582,57 @@ function getCpuInfo($fireUpCpu = false)
570582
}
571583
}
572584

585+
// Raspberry Pi or other ARM board etc.
586+
$cpuData = explode("\n", shell_exec('lscpu'));
587+
foreach ($cpuData as $line) {
588+
$line = explode(':', $line, 2);
589+
590+
if (!array_key_exists(1, $line)) {
591+
continue;
592+
}
593+
594+
$key = trim($line[0]);
595+
$value = trim($line[1]);
596+
597+
// What we want are bogomips, MHz, processor, and Model.
598+
switch ($key) {
599+
// CPU model
600+
case 'Model name':
601+
if (empty($cpu['model'])) {
602+
$cpu['model'] = $value;
603+
}
604+
break;
605+
// cores
606+
case 'CPU(s)':
607+
if (empty($cpu['cores'])) {
608+
$cpu['cores'] = (int)$value;
609+
}
610+
break;
611+
// MHz
612+
case 'CPU max MHz':
613+
if (empty($cpu['max-mhz'])) {
614+
$cpu['max-mhz'] = (int)$value;
615+
}
616+
break;
617+
case 'CPU min MHz':
618+
if (empty($cpu['min-mhz'])) {
619+
$cpu['min-mhz'] = (int)$value;
620+
}
621+
break;
622+
// vendor
623+
case 'Vendor ID':
624+
if (empty($cpu['vendor'])) {
625+
$cpu['vendor'] = $value;
626+
}
627+
break;
628+
}
629+
}
630+
631+
if ($cpu['vendor'] == 'ARM') {
632+
// Unusable
633+
$cpu['mips'] = 0;
634+
}
635+
573636
return $cpu;
574637
}
575638

@@ -654,10 +717,18 @@ function mymemory_usage()
654717

655718
$cpuInfo = getCpuInfo();
656719
// CPU throttling?
657-
if (abs($cpuInfo['mips'] - $cpuInfo['mhz']) > 300) {
658-
print("<pre>\n<<< WARNING >>>\nCPU is in powersaving mode? Set CPU governor to 'performance'!\n Fire up CPU and recalculate MHz!\n</pre>" . PHP_EOL);
659-
// TIME WASTED HERE
660-
$cpuInfo = getCpuInfo(true);
720+
if ($cpuInfo['mips'] && $cpuInfo['mhz']) {
721+
if (abs($cpuInfo['mips'] - $cpuInfo['mhz']) > 300) {
722+
print("<pre>\n<<< WARNING >>>\nCPU is in powersaving mode? Set CPU governor to 'performance'!\n Fire up CPU and recalculate MHz!\n</pre>" . PHP_EOL);
723+
// TIME WASTED HERE
724+
$cpuInfo = getCpuInfo(true);
725+
}
726+
} else if ($cpuInfo['max-mhz'] && $cpuInfo['mhz']) {
727+
if (abs($cpuInfo['max-mhz'] - $cpuInfo['mhz']) > 300) {
728+
print("<pre>\n<<< WARNING >>>\nCPU is in powersaving mode? Set CPU governor to 'performance'!\n Fire up CPU and recalculate MHz!\n</pre>" . PHP_EOL);
729+
// TIME WASTED HERE
730+
$cpuInfo = getCpuInfo(true);
731+
}
661732
}
662733

663734
$memoryLimit = min(getPhpMemoryLimitBytes(), getSystemMemoryFreeLimitBytes());

0 commit comments

Comments
 (0)