https://github.com/jarred-sumner/hw-perf-counters
https://github.com/jarred-sumner/hw-perf-counters
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jarred-sumner/hw-perf-counters
- Owner: Jarred-Sumner
- Created: 2022-05-03T14:24:09.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-05-04T07:50:23.000Z (about 3 years ago)
- Last Synced: 2025-02-23T08:12:02.576Z (4 months ago)
- Language: C
- Size: 22.5 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hardware performance counters for macOS
This package exposes macOS hardware performance counters to JavaScript.
The code is based on [this gist](https://gist.github.com/ibireme/173517c208c7dc333ba962c1f0d67d12).
This package will only run in [Bun](https://bun.sh) v0.0.79 or later due to depending on `bun:ffi`.
## Usage
This package requires root access to run.
```js
import { init, run } from "hw-perf-count";// Set up the hardware performance counters.
// This loads private macOS APIs
// If sudo is not enabled, this likely will throw an error
init();const { instructions, cycles, missedBranches, branches } = run(() => {
for (let i = 0; i < 100000; i++) {
// Do something
}
});console.log({ instructions, cycles, missedBranches, branches });
``````js
import { init, start, stop, count } from "hw-perf-count";// Start counting
start();// Stop counting
stop();// How many instructions ran between start() and stop()?
console.log(count.instructions);// How many cycles ran between start() and stop()?
console.log(count.cycles);// How many branches missed between start() and stop()?
console.log(count.missedBranches);// How many branches overall between start() and stop()?
console.log(count.branches);
````count` updates when you call `stop()`.
```ts
export const count: {
get cycles(): number | BigInt;
get branches(): number | BigInt;
get instructions(): number | BigInt;
get missedBranches(): number | BigInt;
};
```