omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / research / m15 / pmu / pmu_probe.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* Probe: per-thread, inherited counters opened on both GB10 CPU PMUs. |
| 18 | * A worker thread created after the counters runs a fixed loop on an A725 |
| 19 | * core, then the same loop on an X925 core. Prints per-PMU counts and the |
| 20 | * enabled/running times (multiplexing). Read-only; changes no setting. */ |
| 21 | #define _GNU_SOURCE |
| 22 | #include <linux/perf_event.h> |
| 23 | #include <pthread.h> |
| 24 | #include <sched.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <sys/syscall.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | static const uint64_t ev[] = { 0x11, 0x08, 0x19, 0x37, 0x17, 0x13 }; |
| 33 | static const char *nm[] = { "cpu_cycles", "inst_retired", "bus_access", "ll_cache_miss_rd", |
| 34 | "l2d_cache_refill", "mem_access" }; |
| 35 | static volatile uint64_t sink; |
| 36 | |
| 37 | static void pin(int cpu) { |
| 38 | cpu_set_t s; CPU_ZERO(&s); CPU_SET(cpu, &s); |
| 39 | sched_setaffinity(0, sizeof s, &s); |
| 40 | } |
| 41 | static void work(void) { |
| 42 | uint64_t x = 1; |
| 43 | for (uint64_t i = 0; i < 200000000ull; i++) x = x * 6364136223846793005ull + i; |
| 44 | sink = x; |
| 45 | } |
| 46 | static void *worker(void *a) { |
| 47 | (void)a; |
| 48 | pin(1); work(); int c1 = sched_getcpu(); |
| 49 | pin(6); work(); int c2 = sched_getcpu(); |
| 50 | printf("worker ran on cpu %d then cpu %d\n", c1, c2); |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | int main(void) { |
| 55 | int types[2] = { 10, 11 }, fd[2][6]; |
| 56 | for (int t = 0; t < 2; t++) |
| 57 | for (int i = 0; i < 6; i++) { |
| 58 | struct perf_event_attr a; |
| 59 | memset(&a, 0, sizeof a); |
| 60 | a.size = sizeof a; |
| 61 | a.type = types[t]; |
| 62 | a.config = ev[i]; |
| 63 | a.disabled = 1; |
| 64 | a.inherit = 1; |
| 65 | a.exclude_hv = 1; |
| 66 | a.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING; |
| 67 | fd[t][i] = (int)syscall(SYS_perf_event_open, &a, 0, -1, -1, 0); |
| 68 | if (fd[t][i] < 0) { perror("perf_event_open"); return 1; } |
| 69 | } |
| 70 | for (int t = 0; t < 2; t++) for (int i = 0; i < 6; i++) ioctl(fd[t][i], PERF_EVENT_IOC_ENABLE, 0); |
| 71 | pin(0); |
| 72 | pthread_t th; |
| 73 | pthread_create(&th, NULL, worker, NULL); |
| 74 | pthread_join(th, NULL); |
| 75 | for (int t = 0; t < 2; t++) for (int i = 0; i < 6; i++) ioctl(fd[t][i], PERF_EVENT_IOC_DISABLE, 0); |
| 76 | for (int i = 0; i < 6; i++) { |
| 77 | uint64_t v[2][3]; |
| 78 | for (int t = 0; t < 2; t++) { |
| 79 | if (read(fd[t][i], v[t], sizeof v[t]) != sizeof v[t]) { perror("read"); return 1; } |
| 80 | } |
| 81 | printf("%-17s A725=%-12llu X925=%-12llu sum=%-12llu run/en A=%.4f X=%.4f\n", nm[i], |
| 82 | (unsigned long long)v[0][0], (unsigned long long)v[1][0], |
| 83 | (unsigned long long)(v[0][0] + v[1][0]), |
| 84 | v[0][1] ? (double)v[0][2] / v[0][1] : 0, v[1][1] ? (double)v[1][2] / v[1][1] : 0); |
| 85 | } |
| 86 | return 0; |
| 87 | } |