{"id":32149348,"url":"https://github.com/androp0v/powermetricskit","last_synced_at":"2025-10-21T09:54:41.355Z","repository":{"id":230370917,"uuid":"779203898","full_name":"Androp0v/PowerMetricsKit","owner":"Androp0v","description":"Swift package to retrieve realtime information on CPU energy consumption on Apple platforms using the CPU's Closed Loop Performance Controller (CLPC).","archived":false,"fork":false,"pushed_at":"2024-06-15T10:28:35.000Z","size":6315,"stargazers_count":12,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-21T09:54:29.327Z","etag":null,"topics":["apple","energy-consumption","energy-efficiency","energy-monitor","ios","macos"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Androp0v.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-03-29T09:24:05.000Z","updated_at":"2025-10-07T06:54:27.000Z","dependencies_parsed_at":"2024-04-16T16:32:13.614Z","dependency_job_id":"da756a8c-054d-46ba-8405-f680e83d33d2","html_url":"https://github.com/Androp0v/PowerMetricsKit","commit_stats":null,"previous_names":["androp0v/powermetricskit"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/Androp0v/PowerMetricsKit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Androp0v%2FPowerMetricsKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Androp0v%2FPowerMetricsKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Androp0v%2FPowerMetricsKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Androp0v%2FPowerMetricsKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Androp0v","download_url":"https://codeload.github.com/Androp0v/PowerMetricsKit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Androp0v%2FPowerMetricsKit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280240308,"owners_count":26296527,"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","status":"online","status_checked_at":"2025-10-21T02:00:06.614Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","energy-consumption","energy-efficiency","energy-monitor","ios","macos"],"created_at":"2025-10-21T09:54:37.601Z","updated_at":"2025-10-21T09:54:41.337Z","avatar_url":"https://github.com/Androp0v.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"#  PowerMetricsKit\n\n![PowerWidgetView](Sources/PowerMetricsKit/PowerMetricsKit.docc/Resources/PowerWidgetView.png)\n\nA package to retrieve realtime information on CPU energy consumption using the CPU's Closed Loop Performance Controller (CLPC) via `proc_pidinfo`.\n\n## How does it work?\n\nCode related to energy measurements is under PowerWidget/. It works by using `libproc`'s `proc_pidinfo` with the new `PROC_PIDTHREADCOUNTS` option, which returns a `struct` including per-thread energy measurements from the CPU's' CLPC, which in turn obtains the energy results from the Digital Power Estimator (DPE). The list of all threads is retrieved using `task_threads` with the PID of the current task (root privileges are required to invoke `task_threads` on other processes).\n\nThe `libproc.h` headers can't be imported on iOS, so they're reproduced at the beginning of `sample_threads.c`, as well as the result `struct`s from the `proc_pidinfo`, using the definitions and documentation available in [Apple's OSS Distributions repository for `libproc.h`](https://github.com/apple-oss-distributions/xnu/blob/aca3beaa3dfbd42498b42c5e5ce20a938e6554e5/bsd/sys/proc_info.h).\n\n## Documentation\n\nThis package is documented using DocC. Please see PowerMetricsKit's [documentation site](https://androp0v.github.io/PowerMetricsKit/documentation/powermetricskit/) or use _Xcode \u003e Product \u003e Build documentation_ to compile the documentation for the package and view it locally.\n\n![DocumentationScreenshot](DocC.png)\n\n## Results\nWhen running a single app in the foreground, the results correlate well with wall power (measured with a USB-C amp/volt meter with the device charged to 100%), after substracting the energy used by the device while idling. On macOS, results are almost identical to the `powermetrics` output (this app reports slightly less power used, which makes sense since `powermetrics` also includes CPU power used by other apps and the OS).\n\nThe results are promising when experimenting with pathologic cases, and qualitatively match the predicted behavior. For example, making all threads wait forever on a spinlock makes the CPU utilization skyrocket (~100%/core) but power consumption remains very modest (less than 5W on a M3 Max w/16 cores), compared to ALU-heavy code like the simulation included with this app, that has lower CPU utilization (~30%) but uses much more power (~20W on M3 Max w/16 cores).\n \n## Limitations\n\nSince without root privileges and/or specific entitlements only the energy of the threads from the same process can be measured, APIs that trigger work on a kernel thread (ie system calls) won't be accounted for in the power metrics of the app.\n\n## Compatibility\n\nTested on A14 Bionic or newer CPUs.\n\n## Generate documentation\n\nRun this code on a Terminal window at the root of the PowerMetricsKit package to generate the documentation for this project used in GitHub pages.\n```zsh\nswift package --allow-writing-to-directory docs generate-documentation --target PowerMetricsKit --disable-indexing --transform-for-static-hosting --hosting-base-path PowerMetricsKit --output-path docs\n```\n\n## References\n\n- [Apple's OSS Distributions source for `libproc.h`](https://github.com/apple-oss-distributions/xnu/blob/aca3beaa3dfbd42498b42c5e5ce20a938e6554e5/bsd/sys/proc_info.h).\n- [Apple's OSS Distributions overview of `Recount` subsystem](https://github.com/apple-oss-distributions/xnu/blob/5c2921b07a2480ab43ec66f5b9e41cb872bc554f/doc/recount.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrop0v%2Fpowermetricskit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrop0v%2Fpowermetricskit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrop0v%2Fpowermetricskit/lists"}