Skip to content

Commit 406f57d

Browse files
committed
Add some more "examples"
they're really test scripts, I should probably put them somewhere else
1 parent 756b0e6 commit 406f57d

File tree

5 files changed

+186
-9
lines changed

5 files changed

+186
-9
lines changed

examples/all-events.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
#!/usr/bin/env php
22
<?php
33

4+
// ok, these aren't all examples per se
5+
// this one lists all or available PMU events
6+
47
use function Perfidious\list_pmus;
58
use function Perfidious\list_pmu_events;
69

710
$opts = getopt('', [
8-
'all',
9-
'pmu:',
11+
'show:',
1012
'help',
1113
], $rest_index);
1214

13-
$pmu = $opts['pmu'] ?? null;
15+
$show = $opts['show'] ?? 'present';
1416

1517
if (array_key_exists('help', $opts)) {
1618
fprintf(STDERR, "Usage: " . $argv[0] . PHP_EOL);
1719
fprintf(STDERR, PHP_EOL);
1820
fprintf(STDERR, "Show event names supported by libpfm4" . PHP_EOL);
1921
fprintf(STDERR, PHP_EOL);
20-
fprintf(STDERR, " --all Show all PMUs, not just active" . PHP_EOL);
21-
fprintf(STDERR, " --pmu Search for a specific PMU" . PHP_EOL);
22+
fprintf(STDERR, " --show PMU \"present\", \"all\", a PMU ID, or a PMU name substring" . PHP_EOL);
2223
exit(0);
2324
}
2425

2526
foreach (list_pmus() as $pmu_info) {
26-
$matches_pmu = (
27-
array_key_exists('all', $opts) ||
28-
!($pmu !== null && $pmu !== $pmu_info->pmu && !str_contains($pmu_info->name, $pmu))
29-
);
27+
$matches_pmu = match ($show) {
28+
'all' => true,
29+
'present' => $pmu_info->is_present,
30+
default => $pmu_info->pmu === (int) $show || str_contains($pmu_info->name, (string) $show),
31+
};
3032
if (!$matches_pmu) {
3133
continue;
3234
}

examples/estimate-overhead.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env php
22
<?php
33

4+
// ok, these aren't all examples per se
5+
// this one tries to estimate the overhead of calling Perfidious\Handle::read()
6+
47
use function Perfidious\open;
58

69
$rest_index = null;

examples/memory.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
// ok, these aren't all examples per se
4+
// this one tries to detect leaks
5+
6+
use function Perfidious\open;
7+
8+
$rest_index = null;
9+
$opts = getopt('', [
10+
'count:',
11+
], $rest_index);
12+
$pos_args = array_slice($argv, $rest_index);
13+
14+
if (count($pos_args) <= 0) {
15+
$pos_args = [
16+
'perf::PERF_COUNT_HW_CPU_CYCLES:u',
17+
'perf::PERF_COUNT_HW_INSTRUCTIONS:u',
18+
];
19+
}
20+
21+
$start = time();
22+
$last = $start;
23+
$interval = 2;
24+
$end = $start + (60 * 5);
25+
26+
$handle = open($pos_args);
27+
$handle->enable();
28+
29+
$prev_memory = memory_get_usage();
30+
$prev_memory_real = memory_get_usage(true);
31+
32+
do {
33+
$now = time();
34+
$stats = $handle->read();
35+
$arr = $handle->readArray();
36+
37+
if ($now - $interval > $last) {
38+
$handle->disable();
39+
40+
$last = $now;
41+
42+
gc_collect_cycles();
43+
44+
$memory = memory_get_usage();
45+
$memory_real = memory_get_usage(true);
46+
47+
foreach ($stats->values as $k => $v) {
48+
printf("%s=%d\n", $k, $v);
49+
}
50+
51+
printf("memory=%d last_memory=%d delta_memory=%d\n", $memory, $prev_memory, $memory - $prev_memory);
52+
printf("memory=%d last_memory=%d delta_memory=%d (real)\n", $memory, $prev_memory, $memory - $prev_memory);
53+
54+
$prev_memory = $memory;
55+
$prev_memory_real = $memory_real;
56+
57+
$handle->enable();
58+
}
59+
60+
usleep(100_000);
61+
} while ($now < $end);

examples/sieve.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
// ok, these aren't all examples per se
5+
// this one runs the sieve of eratosthenes and outputs the cycles/instructions
6+
// array edition
7+
8+
use function Perfidious\open;
9+
10+
$rest_index = null;
11+
$opts = getopt('', [
12+
'count:',
13+
], $rest_index);
14+
$pos_args = array_slice($argv, $rest_index);
15+
16+
if (count($pos_args) <= 0) {
17+
$pos_args = [
18+
'perf::PERF_COUNT_HW_CPU_CYCLES:u',
19+
'perf::PERF_COUNT_HW_INSTRUCTIONS:u',
20+
];
21+
}
22+
23+
$count = $opts['count'] ?? 2000000;
24+
25+
$handle = open($pos_args);
26+
$handle->enable();
27+
28+
$primes = sieve($count);
29+
30+
$handle->disable();
31+
$stats = $handle->read();
32+
33+
var_dump($primes);
34+
var_dump($stats);
35+
36+
function sieve(int $n)
37+
{
38+
$lut = array_fill(0, $n, null);
39+
for ($i = 2; $i < $n; $i++) {
40+
for ($j = $i + $i; $j < $n; $j += $i) {
41+
$lut[$j] = true;
42+
}
43+
}
44+
$rv = [];
45+
for ($i = 2; $i < $n; $i++) {
46+
if (null === $lut[$i]) {
47+
$rv[] = $i;
48+
}
49+
}
50+
return $rv;
51+
}

examples/sieve2.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
// ok, these aren't all examples per se
5+
// this one runs the sieve of eratosthenes and outputs the cycles/instructions
6+
// bit string edition
7+
8+
use function Perfidious\open;
9+
10+
$rest_index = null;
11+
$opts = getopt('', [
12+
'count:',
13+
], $rest_index);
14+
$pos_args = array_slice($argv, $rest_index);
15+
16+
if (count($pos_args) <= 0) {
17+
$pos_args = [
18+
'perf::PERF_COUNT_HW_CPU_CYCLES:u',
19+
'perf::PERF_COUNT_HW_INSTRUCTIONS:u',
20+
];
21+
}
22+
23+
$count = $opts['count'] ?? 2000000;
24+
25+
$handle = open($pos_args);
26+
$handle->enable();
27+
28+
$primes = sieve($count);
29+
30+
$handle->disable();
31+
$stats = $handle->read();
32+
33+
var_dump($primes);
34+
var_dump($stats);
35+
36+
function sieve(int $n): array
37+
{
38+
$n2 = ceil($n / 8);
39+
$lut = str_repeat("\0", $n2);
40+
for ($i = 2; $i < $n; $i++) {
41+
for ($j = $i + $i; $j < $n; $j += $i) {
42+
$off = intdiv($j, 8);
43+
$c = ord($lut[$off]);
44+
$bit = $j % 8;
45+
$mask = 1 << $bit;
46+
$lut[$off] = chr($c | $mask);
47+
}
48+
}
49+
$rv = [];
50+
for ($i = 2; $i < $n; $i++) {
51+
$off = intdiv($i, 8);
52+
$c = ord($lut[$off]);
53+
$bit = $i % 8;
54+
$mask = 1 << $bit;
55+
if (($c & $mask) != $mask) {
56+
$rv[] = $i;
57+
}
58+
}
59+
return $rv;
60+
}

0 commit comments

Comments
 (0)