# std: add Linux perf syscall bits · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/63f4778827bf7e48e7b451d3d04dc1a74126a6d3)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 63f4778827bf7e48e7b451d3d04dc1a74126a6d3

Requested commit: 63f4778827bf7e48e7b451d3d04dc1a74126a6d3

Commit: 63f4778827bf7e48e7b451d3d04dc1a74126a6d3

Tree: 6a63513d8f7b2853cecd9ee6d5775471bdd32bbf

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

````
std: add Linux perf syscall bits

Example usage:

```zig
const std = @import("std");
const PERF = std.os.linux.PERF;
const assert = std.debug.assert;

test "perf" {
    var attr: std.os.linux.perf_event_attr = .{
        .type = PERF.TYPE.HARDWARE,
        .config = @enumToInt(PERF.COUNT.HW.INSTRUCTIONS),
        .flags = .{
            .disabled = true,
            .exclude_kernel = true,
            .exclude_hv = true,
        },
    };
    const fd = try std.os.perf_event_open(&attr, 0, -1, -1, PERF.FLAG.FD_CLOEXEC);
    defer std.os.close(fd);

    _ = std.os.linux.ioctl(fd, PERF.EVENT_IOC.RESET, 0);
    _ = std.os.linux.ioctl(fd, PERF.EVENT_IOC.ENABLE, 0);

    long();

    _ = std.os.linux.ioctl(fd, PERF.EVENT_IOC.DISABLE, 0);

    var result: usize = 0;
    assert((try std.os.read(fd, std.mem.asBytes(&result))) == @sizeOf(usize));

    std.debug.print("instruction count: {d}\n", .{result});
}

fn long() void {
    var i: usize = 0;
    while (i < 100000) : (i += 1) {}
}
```

````

## Parents

- [70ef9bc75c42ec00e9d4231a2e1f1dca84144748](https://git.cafe/gitcafe/zig/commit/70ef9bc75c42ec00e9d4231a2e1f1dca84144748?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/63f4778827bf7e48e7b451d3d04dc1a74126a6d3?format=markdown)
