Skip to content

enable_all! / disable_all! (PR_TASK_PERF_EVENTS_DISABLE) is per-thread #59

Closed

Description

@topolarity

Despite what https://man7.org/linux/man-pages/man2/PR_TASK_PERF_EVENTS_ENABLE.2const.html#DESCRIPTION says, PR_TASK_PERF_EVENTS_DISABLE disables / enables counters on the current thread, not process.

Counts after PR_TASK_PERF_EVENTS_DISABLE:

  main:   0 (expected 0, events disabled)
  worker: 537
Test Source (by AI)
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <linux/perf_event.h>

static pthread_barrier_t go, done;

static volatile uint64_t sink;
static void burn(void) {
    uint64_t x = 0;
    for (uint64_t i = 0; i < 200000000ULL; i++)
        x += i * 0x9e3779b97f4a7c15ULL;
    sink = x;
}

static uint64_t read_counter(int fd) {
    uint64_t v; read(fd, &v, sizeof(v)); return v;
}

static int open_sw_counter(void) {
    struct perf_event_attr a = { .size = sizeof(a),
        .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_CLOCK };
    return syscall(__NR_perf_event_open, &a, 0, -1, -1, 0);
}

static void *worker(void *arg) {
    int fd = open_sw_counter();
    pthread_barrier_wait(&go);
    uint64_t c0 = read_counter(fd);
    burn();
    printf("  worker: %lu\n", read_counter(fd) - c0);
    pthread_barrier_wait(&done);
    close(fd);
    return NULL;
}

int main(void) {
    pthread_barrier_init(&go, NULL, 2);
    pthread_barrier_init(&done, NULL, 2);
    int fd = open_sw_counter();
    pthread_t tid;
    pthread_create(&tid, NULL, worker, NULL);

    prctl(PR_TASK_PERF_EVENTS_DISABLE);
    sleep(1);

    pthread_barrier_wait(&go);
    uint64_t c0 = read_counter(fd);
    burn();
    printf("  main:   %lu (expected 0, events disabled)\n",
           read_counter(fd) - c0);
    pthread_barrier_wait(&done);

    pthread_join(tid, NULL);
    close(fd);
}

Activity

  1. changed the title [-]`disable_all!` (`PR_TASK_PERF_EVENTS_DISABLE`)[/-] [+]`disable_all!` (`PR_TASK_PERF_EVENTS_DISABLE`) is per-thread[/+] on Apr 8, 2026
  2. changed the title [-]`disable_all!` (`PR_TASK_PERF_EVENTS_DISABLE`) is per-thread[/-] [+]`enable_all!` / `disable_all!` (`PR_TASK_PERF_EVENTS_DISABLE`) is per-thread[/+] on Apr 8, 2026
  3. topolarity commented on Apr 8, 2026

    @topolarity
    MemberAuthor

    Took a closer look at the kernel implementation: https://github.com/torvalds/linux/blob/3036cd0d3328220a1858b1ab390be8b562774e8a/kernel/events/core.c#L6767-L6781

    perf events are associated with the thread that create them even if they are put into another thread's context, which explains why our enable_all!() / disable_all! scale in overhead per thread event.

    The C test above creates events on two separate threads, unlike LinuxPerf. So the LinuxPerf implementation is actually already correct, although clearly the prctl documentation could use some improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions