{"id":19365052,"url":"https://github.com/cyyself/m1-pmu-gen","last_synced_at":"2025-04-13T12:04:34.090Z","repository":{"id":244920811,"uuid":"816697974","full_name":"cyyself/m1-pmu-gen","owner":"cyyself","description":"Generate Linux Perf event tables for Apple Silicon","archived":false,"fork":false,"pushed_at":"2024-11-01T08:00:43.000Z","size":11,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-31T09:36:10.884Z","etag":null,"topics":["apple","asahilinux","perf","pmu"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cyyself.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-18T08:31:13.000Z","updated_at":"2024-11-06T08:33:37.000Z","dependencies_parsed_at":"2024-11-10T07:39:08.560Z","dependency_job_id":"6ee55bbd-097c-40d8-8f31-5c97a8d50f2c","html_url":"https://github.com/cyyself/m1-pmu-gen","commit_stats":null,"previous_names":["cyyself/m1_pmu_gen"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyyself%2Fm1-pmu-gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyyself%2Fm1-pmu-gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyyself%2Fm1-pmu-gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyyself%2Fm1-pmu-gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cyyself","download_url":"https://codeload.github.com/cyyself/m1-pmu-gen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232786436,"owners_count":18576419,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apple","asahilinux","perf","pmu"],"created_at":"2024-11-10T07:39:01.710Z","updated_at":"2025-01-06T21:17:23.053Z","avatar_url":"https://github.com/cyyself.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# M1 PMU GEN\n\nPython script to generate some tables for Apple Silicon Macs to profiling with Linux perf.\n\nThis script requires files from `/usr/share/kpep` in macOS to generate the file.\n\n## Usage - Generate the Linux perf event JSON table\n\nAssume you have copied `/usr/share/kpep` from macOS to Linux in the same directory.\n\nAnd you have cloned the Linux kernel source code and set environment variable `PATH_TO_LINUX` to the path of the kernel source code.\n\nA optional step is to download the [Apple Silicon CPU Optimization Guide](https://developer.apple.com/download/apple-silicon-cpu-optimization-guide/) to generate the event descriptions which require Apple Developer account. If you don't have this file, the script will use the event name as the description.\n\n```bash\n./gen_perf_patch.py -a ~/Downloads/Apple-Silicon-CPU-Optimization-Guide.pdf -w a14 a15 \u003e $PATH_TO_LINUX/apple_pmu.patch\ncd $PATH_TO_LINUX\npatch -p1 \u003c apple_pmu.patch\ncd tools/perf\nmake -j `nproc`\n```\n\nThen you will get a perf in current folder with the new events:\n\n```console\n$ ./perf list\nList of pre-defined events (to be used in -e or -M):\n\n  cpu-cycles OR cycles                               [Hardware event]\n  instructions                                       [Hardware event]\n  alignment-faults                                   [Software event]\n  bpf-output                                         [Software event]\n  cgroup-switches                                    [Software event]\n  context-switches OR cs                             [Software event]\n  cpu-clock                                          [Software event]\n  cpu-migrations OR migrations                       [Software event]\n  dummy                                              [Software event]\n  emulation-faults                                   [Software event]\n  major-faults                                       [Software event]\n  minor-faults                                       [Software event]\n  page-faults OR faults                              [Software event]\n  task-clock                                         [Software event]\n  duration_time                                      [Tool event]\n  user_time                                          [Tool event]\n  system_time                                        [Tool event]\n  cycles OR apple_firestorm_pmu/cycles/              [Kernel PMU event]\n  instructions OR apple_firestorm_pmu/instructions/  [Kernel PMU event]\n  cycles OR apple_icestorm_pmu/cycles/               [Kernel PMU event]\n  instructions OR apple_icestorm_pmu/instructions/   [Kernel PMU event]\n\ncore imp def:\n  atomic_xxxxxx\n....\n```\n\nEnjoy profiling your Apple Silicon Macs in Linux!\n\n## Usage - Generate the PMU Driver\n\nThe register definitions and affinity table for M1 and M2 are already updated in the kernel source code since [Linux Mainline Kernel v6.12](https://github.com/torvalds/linux/commit/3cce331ee2a4cc99328757a9bd297ad9a39609e8) and [asahi-linux v6.11](https://github.com/AsahiLinux/linux/commit/ac5bb3788b4edeac2f30cd6fd10f276e2ae98bb9). For these versions, you don't need to generate the register definitions and affinity table.\n\nIf you need to generate the register definitions and affinity table for other Apple Silicon Macs, you can follow the steps below.\n\nOn macOS:\n\n```bash\n./gen_driver.py /usr/share/kpep/a14.plist m1\n```\n\nYou will get something like this:\n\n```c\nenum m1_pmu_events {\n\tM1_PMU_PERFCTR_...\n...\n\tM1_PMU_CFG_COUNT_KERNEL\t\t\t\t\t= BIT(9),\n};\nstatic const u16 m1_pmu_event_affinity[M1_PMU_PERFCTR_LAST + 1] = {\n\t[0 ... M1_PMU_PERFCTR_LAST]\t\t\t\t= ANY_BUT_0_1,\n\t[M1_PMU_PERFCTR_...\n...\n\t[M1_PMU_PERFCTR_UNKNOWN_fd]\t\t\t\t= ONLY_2_4_6,\n};\n```\n\nNote: This is only to generate register definitions and affinity for the events in PMU driver. When the affinity table in the kernel is correct, you don't need to generate it and compile the kernel.\n\n## Why we need this?\n\nThe definition of the Apple PMU events is not in the Linux kernel source code, but we can find the event name, event code, and event affinity to each PMU counter in the `/usr/share/kpep` directory in macOS. The event descriptions are in the [Apple Silicon CPU Optimization Guide](https://developer.apple.com/download/apple-silicon-cpu-optimization-guide/).\n\nHowever, because of the license issue, we can't directly submit this information to the Linux kernel source code. I have consulted the Apple Legal Team but haven't gotten a response yet.\n\nThus, for now, having a script to generate the event definitions and descriptions for the Linux PMU driver and perf tool will be helpful for the community.\n\n## References\n\n[Apple Silicon CPU Optimization Guide-Chapter 6. Performance Monitoring](https://developer.apple.com/download/apple-silicon-cpu-optimization-guide/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyyself%2Fm1-pmu-gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcyyself%2Fm1-pmu-gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyyself%2Fm1-pmu-gen/lists"}