Beispiel #1
0
static int process_events(struct machine *machine, struct perf_evlist *evlist,
			  struct state *state)
{
	union perf_event *event;
	int i, ret;

	for (i = 0; i < evlist->nr_mmaps; i++) {
		while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
			ret = process_event(machine, evlist, event, state);
			perf_evlist__mmap_consume(evlist, i);
			if (ret < 0)
				return ret;
		}
	}
	return 0;
}
Beispiel #2
0
static int find_comm(struct perf_evlist *evlist, const char *comm)
{
	union perf_event *event;
	int i, found;

	found = 0;
	for (i = 0; i < evlist->nr_mmaps; i++) {
		while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
			if (event->header.type == PERF_RECORD_COMM &&
			    (pid_t)event->comm.pid == getpid() &&
			    (pid_t)event->comm.tid == getpid() &&
			    strcmp(event->comm.comm, comm) == 0)
				found += 1;
			perf_evlist__mmap_consume(evlist, i);
		}
	}
	return found;
}
Beispiel #3
0
static int do_test(struct bpf_object *obj, int (*func)(void),
		   int expect)
{
	struct record_opts opts = {
		.target = {
			.uid = UINT_MAX,
			.uses_mmap = true,
		},
		.freq	      = 0,
		.mmap_pages   = 256,
		.default_interval = 1,
	};

	char pid[16];
	char sbuf[STRERR_BUFSIZE];
	struct perf_evlist *evlist;
	int i, ret = TEST_FAIL, err = 0, count = 0;

	struct parse_events_evlist parse_evlist;
	struct parse_events_error parse_error;

	bzero(&parse_error, sizeof(parse_error));
	bzero(&parse_evlist, sizeof(parse_evlist));
	parse_evlist.error = &parse_error;
	INIT_LIST_HEAD(&parse_evlist.list);

	err = parse_events_load_bpf_obj(&parse_evlist, &parse_evlist.list, obj, NULL);
	if (err || list_empty(&parse_evlist.list)) {
		pr_debug("Failed to add events selected by BPF\n");
		return TEST_FAIL;
	}

	snprintf(pid, sizeof(pid), "%d", getpid());
	pid[sizeof(pid) - 1] = '\0';
	opts.target.tid = opts.target.pid = pid;

	/* Instead of perf_evlist__new_default, don't add default events */
	evlist = perf_evlist__new();
	if (!evlist) {
		pr_debug("Not enough memory to create evlist\n");
		return TEST_FAIL;
	}

	err = perf_evlist__create_maps(evlist, &opts.target);
	if (err < 0) {
		pr_debug("Not enough memory to create thread/cpu maps\n");
		goto out_delete_evlist;
	}

	perf_evlist__splice_list_tail(evlist, &parse_evlist.list);
	evlist->nr_groups = parse_evlist.nr_groups;

	perf_evlist__config(evlist, &opts, NULL);

	err = perf_evlist__open(evlist);
	if (err < 0) {
		pr_debug("perf_evlist__open: %s\n",
			 str_error_r(errno, sbuf, sizeof(sbuf)));
		goto out_delete_evlist;
	}

	err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
	if (err < 0) {
		pr_debug("perf_evlist__mmap: %s\n",
			 str_error_r(errno, sbuf, sizeof(sbuf)));
		goto out_delete_evlist;
	}

	perf_evlist__enable(evlist);
	(*func)();
	perf_evlist__disable(evlist);

	for (i = 0; i < evlist->nr_mmaps; i++) {
		union perf_event *event;

		while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
			const u32 type = event->header.type;

			if (type == PERF_RECORD_SAMPLE)
				count ++;
		}
	}

	if (count != expect) {
		pr_debug("BPF filter result incorrect\n");
		goto out_delete_evlist;
	}

	ret = TEST_OK;

out_delete_evlist:
	perf_evlist__delete(evlist);
	return ret;
}
Beispiel #4
0
int test__PERF_RECORD(int subtest __maybe_unused)
{
    struct record_opts opts = {
        .target = {
            .uid = UINT_MAX,
            .uses_mmap = true,
        },
        .no_buffering = true,
        .mmap_pages   = 256,
    };
    cpu_set_t cpu_mask;
    size_t cpu_mask_size = sizeof(cpu_mask);
    struct perf_evlist *evlist = perf_evlist__new_dummy();
    struct perf_evsel *evsel;
    struct perf_sample sample;
    const char *cmd = "sleep";
    const char *argv[] = { cmd, "1", NULL, };
    char *bname, *mmap_filename;
    u64 prev_time = 0;
    bool found_cmd_mmap = false,
         found_libc_mmap = false,
         found_vdso_mmap = false,
         found_ld_mmap = false;
    int err = -1, errs = 0, i, wakeups = 0;
    u32 cpu;
    int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
    char sbuf[STRERR_BUFSIZE];

    if (evlist == NULL) /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */
        evlist = perf_evlist__new_default();

    if (evlist == NULL || argv == NULL) {
        pr_debug("Not enough memory to create evlist\n");
        goto out;
    }

    /*
     * Create maps of threads and cpus to monitor. In this case
     * we start with all threads and cpus (-1, -1) but then in
     * perf_evlist__prepare_workload we'll fill in the only thread
     * we're monitoring, the one forked there.
     */
    err = perf_evlist__create_maps(evlist, &opts.target);
    if (err < 0) {
        pr_debug("Not enough memory to create thread/cpu maps\n");
        goto out_delete_evlist;
    }

    /*
     * Prepare the workload in argv[] to run, it'll fork it, and then wait
     * for perf_evlist__start_workload() to exec it. This is done this way
     * so that we have time to open the evlist (calling sys_perf_event_open
     * on all the fds) and then mmap them.
     */
    err = perf_evlist__prepare_workload(evlist, &opts.target, argv, false, NULL);
    if (err < 0) {
        pr_debug("Couldn't run the workload!\n");
        goto out_delete_evlist;
    }

    /*
     * Config the evsels, setting attr->comm on the first one, etc.
     */
    evsel = perf_evlist__first(evlist);
    perf_evsel__set_sample_bit(evsel, CPU);
    perf_evsel__set_sample_bit(evsel, TID);
    perf_evsel__set_sample_bit(evsel, TIME);
    perf_evlist__config(evlist, &opts);

    err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
    if (err < 0) {
        pr_debug("sched__get_first_possible_cpu: %s\n",
                 strerror_r(errno, sbuf, sizeof(sbuf)));
        goto out_delete_evlist;
    }

    cpu = err;

    /*
     * So that we can check perf_sample.cpu on all the samples.
     */
    if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
        pr_debug("sched_setaffinity: %s\n",
                 strerror_r(errno, sbuf, sizeof(sbuf)));
        goto out_delete_evlist;
    }

    /*
     * Call sys_perf_event_open on all the fds on all the evsels,
     * grouping them if asked to.
     */
    err = perf_evlist__open(evlist);
    if (err < 0) {
        pr_debug("perf_evlist__open: %s\n",
                 strerror_r(errno, sbuf, sizeof(sbuf)));
        goto out_delete_evlist;
    }

    /*
     * mmap the first fd on a given CPU and ask for events for the other
     * fds in the same CPU to be injected in the same mmap ring buffer
     * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
     */
    err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
    if (err < 0) {
        pr_debug("perf_evlist__mmap: %s\n",
                 strerror_r(errno, sbuf, sizeof(sbuf)));
        goto out_delete_evlist;
    }

    /*
     * Now that all is properly set up, enable the events, they will
     * count just on workload.pid, which will start...
     */
    perf_evlist__enable(evlist);

    /*
     * Now!
     */
    perf_evlist__start_workload(evlist);

    while (1) {
        int before = total_events;

        for (i = 0; i < evlist->nr_mmaps; i++) {
            union perf_event *event;

            while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
                const u32 type = event->header.type;
                const char *name = perf_event__name(type);

                ++total_events;
                if (type < PERF_RECORD_MAX)
                    nr_events[type]++;

                err = perf_evlist__parse_sample(evlist, event, &sample);
                if (err < 0) {
                    if (verbose)
                        perf_event__fprintf(event, stderr);
                    pr_debug("Couldn't parse sample\n");
                    goto out_delete_evlist;
                }

                if (verbose) {
                    pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
                    perf_event__fprintf(event, stderr);
                }

                if (prev_time > sample.time) {
                    pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
                             name, prev_time, sample.time);
                    ++errs;
                }

                prev_time = sample.time;

                if (sample.cpu != cpu) {
                    pr_debug("%s with unexpected cpu, expected %d, got %d\n",
                             name, cpu, sample.cpu);
                    ++errs;
                }

                if ((pid_t)sample.pid != evlist->workload.pid) {
                    pr_debug("%s with unexpected pid, expected %d, got %d\n",
                             name, evlist->workload.pid, sample.pid);
                    ++errs;
                }

                if ((pid_t)sample.tid != evlist->workload.pid) {
                    pr_debug("%s with unexpected tid, expected %d, got %d\n",
                             name, evlist->workload.pid, sample.tid);
                    ++errs;
                }

                if ((type == PERF_RECORD_COMM ||
                        type == PERF_RECORD_MMAP ||
                        type == PERF_RECORD_MMAP2 ||
                        type == PERF_RECORD_FORK ||
                        type == PERF_RECORD_EXIT) &&
                        (pid_t)event->comm.pid != evlist->workload.pid) {
                    pr_debug("%s with unexpected pid/tid\n", name);
                    ++errs;
                }

                if ((type == PERF_RECORD_COMM ||
                        type == PERF_RECORD_MMAP ||
                        type == PERF_RECORD_MMAP2) &&
                        event->comm.pid != event->comm.tid) {
                    pr_debug("%s with different pid/tid!\n", name);
                    ++errs;
                }

                switch (type) {
                case PERF_RECORD_COMM:
                    if (strcmp(event->comm.comm, cmd)) {
                        pr_debug("%s with unexpected comm!\n", name);
                        ++errs;
                    }
                    break;
                case PERF_RECORD_EXIT:
                    goto found_exit;
                case PERF_RECORD_MMAP:
                    mmap_filename = event->mmap.filename;
                    goto check_bname;
                case PERF_RECORD_MMAP2:
                    mmap_filename = event->mmap2.filename;
check_bname:
                    bname = strrchr(mmap_filename, '/');
                    if (bname != NULL) {
                        if (!found_cmd_mmap)
                            found_cmd_mmap = !strcmp(bname + 1, cmd);
                        if (!found_libc_mmap)
                            found_libc_mmap = !strncmp(bname + 1, "libc", 4);
                        if (!found_ld_mmap)
                            found_ld_mmap = !strncmp(bname + 1, "ld", 2);
                    } else if (!found_vdso_mmap)
                        found_vdso_mmap = !strcmp(mmap_filename, "[vdso]");
                    break;

                case PERF_RECORD_SAMPLE:
                    /* Just ignore samples for now */
                    break;
                default:
                    pr_debug("Unexpected perf_event->header.type %d!\n",
                             type);
                    ++errs;
                }

                perf_evlist__mmap_consume(evlist, i);
            }
        }

        /*
         * We don't use poll here because at least at 3.1 times the
         * PERF_RECORD_{!SAMPLE} events don't honour
         * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
         */
        if (total_events == before && false)
            perf_evlist__poll(evlist, -1);

        sleep(1);
        if (++wakeups > 5) {
            pr_debug("No PERF_RECORD_EXIT event!\n");
            break;
        }
    }

found_exit:
    if (nr_events[PERF_RECORD_COMM] > 1) {
        pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
        ++errs;
    }

    if (nr_events[PERF_RECORD_COMM] == 0) {
        pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
        ++errs;
    }

    if (!found_cmd_mmap) {
        pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
        ++errs;
    }

    if (!found_libc_mmap) {
        pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
        ++errs;
    }

    if (!found_ld_mmap) {
        pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
        ++errs;
    }

    if (!found_vdso_mmap) {
        pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
        ++errs;
    }
out_delete_evlist:
    perf_evlist__delete(evlist);
out:
    return (err < 0 || errs > 0) ? -1 : 0;
}
/*
 * This test will start a workload that does nothing then it checks
 * if the number of exit event reported by the kernel is 1 or not
 * in order to check the kernel returns correct number of event.
 */
int test__task_exit(void)
{
	int err = -1;
	union perf_event *event;
	struct perf_evsel *evsel;
	struct perf_evlist *evlist;
	struct perf_target target = {
		.uid		= UINT_MAX,
		.uses_mmap	= true,
	};
	const char *argv[] = { "true", NULL };

	signal(SIGCHLD, sig_handler);
	signal(SIGUSR1, sig_handler);

	evlist = perf_evlist__new();
	if (evlist == NULL) {
		pr_debug("perf_evlist__new\n");
		return -1;
	}
	/*
	 * We need at least one evsel in the evlist, use the default
	 * one: "cycles".
	 */
	err = perf_evlist__add_default(evlist);
	if (err < 0) {
		pr_debug("Not enough memory to create evsel\n");
		goto out_free_evlist;
	}

	/*
	 * Create maps of threads and cpus to monitor. In this case
	 * we start with all threads and cpus (-1, -1) but then in
	 * perf_evlist__prepare_workload we'll fill in the only thread
	 * we're monitoring, the one forked there.
	 */
	evlist->cpus = cpu_map__dummy_new();
	evlist->threads = thread_map__new_by_tid(-1);
	if (!evlist->cpus || !evlist->threads) {
		err = -ENOMEM;
		pr_debug("Not enough memory to create thread/cpu maps\n");
		goto out_delete_maps;
	}

	err = perf_evlist__prepare_workload(evlist, &target, argv, false, true);
	if (err < 0) {
		pr_debug("Couldn't run the workload!\n");
		goto out_delete_maps;
	}

	evsel = perf_evlist__first(evlist);
	evsel->attr.task = 1;
	evsel->attr.sample_freq = 0;
	evsel->attr.inherit = 0;
	evsel->attr.watermark = 0;
	evsel->attr.wakeup_events = 1;
	evsel->attr.exclude_kernel = 1;

	err = perf_evlist__open(evlist);
	if (err < 0) {
		pr_debug("Couldn't open the evlist: %s\n", strerror(-err));
		goto out_delete_maps;
	}

	if (perf_evlist__mmap(evlist, 128, true) < 0) {
		pr_debug("failed to mmap events: %d (%s)\n", errno,
			 strerror(errno));
		goto out_close_evlist;
	}

	perf_evlist__start_workload(evlist);

retry:
	while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
		if (event->header.type != PERF_RECORD_EXIT)
			continue;

		nr_exit++;
	}

	if (!exited || !nr_exit) {
		poll(evlist->pollfd, evlist->nr_fds, -1);
		goto retry;
	}

	if (nr_exit != 1) {
		pr_debug("received %d EXIT records\n", nr_exit);
		err = -1;
	}

	perf_evlist__munmap(evlist);
out_close_evlist:
	perf_evlist__close(evlist);
out_delete_maps:
	perf_evlist__delete_maps(evlist);
out_free_evlist:
	perf_evlist__delete(evlist);
	return err;
}
int test__syscall_open_tp_fields(void)
{
	struct perf_record_opts opts = {
		.target = {
			.uid = UINT_MAX,
			.uses_mmap = true,
		},
		.no_delay   = true,
		.freq	    = 1,
		.mmap_pages = 256,
		.raw_samples = true,
	};
	const char *filename = "/etc/passwd";
	int flags = O_RDONLY | O_DIRECTORY;
	struct perf_evlist *evlist = perf_evlist__new();
	struct perf_evsel *evsel;
	int err = -1, i, nr_events = 0, nr_polls = 0;

	if (evlist == NULL) {
		pr_debug("%s: perf_evlist__new\n", __func__);
		goto out;
	}

	evsel = perf_evsel__newtp("syscalls", "sys_enter_open");
	if (evsel == NULL) {
		pr_debug("%s: perf_evsel__newtp\n", __func__);
		goto out_delete_evlist;
	}

	perf_evlist__add(evlist, evsel);

	err = perf_evlist__create_maps(evlist, &opts.target);
	if (err < 0) {
		pr_debug("%s: perf_evlist__create_maps\n", __func__);
		goto out_delete_evlist;
	}

	perf_evsel__config(evsel, &opts);

	evlist->threads->map[0] = getpid();

	err = perf_evlist__open(evlist);
	if (err < 0) {
		pr_debug("perf_evlist__open: %s\n", strerror(errno));
		goto out_delete_maps;
	}

	err = perf_evlist__mmap(evlist, UINT_MAX, false);
	if (err < 0) {
		pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
		goto out_close_evlist;
	}

	perf_evlist__enable(evlist);

	/*
	 * Generate the event:
	 */
	open(filename, flags);

	while (1) {
		int before = nr_events;

		for (i = 0; i < evlist->nr_mmaps; i++) {
			union perf_event *event;

			while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
				const u32 type = event->header.type;
				int tp_flags;
				struct perf_sample sample;

				++nr_events;

				if (type != PERF_RECORD_SAMPLE) {
					perf_evlist__mmap_consume(evlist, i);
					continue;
				}

				err = perf_evsel__parse_sample(evsel, event, &sample);
				if (err) {
					pr_err("Can't parse sample, err = %d\n", err);
					goto out_munmap;
				}

				tp_flags = perf_evsel__intval(evsel, &sample, "flags");

				if (flags != tp_flags) {
					pr_debug("%s: Expected flags=%#x, got %#x\n",
						 __func__, flags, tp_flags);
					goto out_munmap;
				}

				goto out_ok;
			}
		}

		if (nr_events == before)
			poll(evlist->pollfd, evlist->nr_fds, 10);

		if (++nr_polls > 5) {
			pr_debug("%s: no events!\n", __func__);
			goto out_munmap;
		}
	}
out_ok:
	err = 0;
out_munmap:
	perf_evlist__munmap(evlist);
out_close_evlist:
	perf_evlist__close(evlist);
out_delete_maps:
	perf_evlist__delete_maps(evlist);
out_delete_evlist:
	perf_evlist__delete(evlist);
out:
	return err;
}
Beispiel #7
0
/*
 * This test will generate random numbers of calls to some getpid syscalls,
 * then establish an mmap for a group of events that are created to monitor
 * the syscalls.
 *
 * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
 * sample.id field to map back to its respective perf_evsel instance.
 *
 * Then it checks if the number of syscalls reported as perf events by
 * the kernel corresponds to the number of syscalls made.
 */
int test__basic_mmap(void)
{
	int err = -1;
	union perf_event *event;
	struct thread_map *threads;
	struct cpu_map *cpus;
	struct perf_evlist *evlist;
	cpu_set_t cpu_set;
	const char *syscall_names[] = { "getsid", "getppid", "getpgrp",
					"getpgid", };
	pid_t (*syscalls[])(void) = { (void *)getsid, getppid, getpgrp,
				      (void*)getpgid };
#define nsyscalls ARRAY_SIZE(syscall_names)
	unsigned int nr_events[nsyscalls],
		     expected_nr_events[nsyscalls], i, j;
	struct perf_evsel *evsels[nsyscalls], *evsel;

	threads = thread_map__new(-1, getpid(), UINT_MAX);
	if (threads == NULL) {
		pr_debug("thread_map__new\n");
		return -1;
	}

	cpus = cpu_map__new(NULL);
	if (cpus == NULL) {
		pr_debug("cpu_map__new\n");
		goto out_free_threads;
	}

	CPU_ZERO(&cpu_set);
	CPU_SET(cpus->map[0], &cpu_set);
	sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
	if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
		pr_debug("sched_setaffinity() failed on CPU %d: %s ",
			 cpus->map[0], strerror(errno));
		goto out_free_cpus;
	}

	evlist = perf_evlist__new();
	if (evlist == NULL) {
		pr_debug("perf_evlist__new\n");
		goto out_free_cpus;
	}

	perf_evlist__set_maps(evlist, cpus, threads);

	for (i = 0; i < nsyscalls; ++i) {
		char name[64];

		snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
		evsels[i] = perf_evsel__newtp("syscalls", name);
		if (evsels[i] == NULL) {
			pr_debug("perf_evsel__new\n");
			goto out_delete_evlist;
		}

		evsels[i]->attr.wakeup_events = 1;
		perf_evsel__set_sample_id(evsels[i], false);

		perf_evlist__add(evlist, evsels[i]);

		if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
			pr_debug("failed to open counter: %s, "
				 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
				 strerror(errno));
			goto out_delete_evlist;
		}

		nr_events[i] = 0;
		expected_nr_events[i] = 1 + rand() % 127;
	}

	if (perf_evlist__mmap(evlist, 128, true) < 0) {
		pr_debug("failed to mmap events: %d (%s)\n", errno,
			 strerror(errno));
		goto out_delete_evlist;
	}

	for (i = 0; i < nsyscalls; ++i)
		for (j = 0; j < expected_nr_events[i]; ++j) {
			int foo = syscalls[i]();
			++foo;
		}

	while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
		struct perf_sample sample;

		if (event->header.type != PERF_RECORD_SAMPLE) {
			pr_debug("unexpected %s event\n",
				 perf_event__name(event->header.type));
			goto out_delete_evlist;
		}

		err = perf_evlist__parse_sample(evlist, event, &sample);
		if (err) {
			pr_err("Can't parse sample, err = %d\n", err);
			goto out_delete_evlist;
		}

		err = -1;
		evsel = perf_evlist__id2evsel(evlist, sample.id);
		if (evsel == NULL) {
			pr_debug("event with id %" PRIu64
				 " doesn't map to an evsel\n", sample.id);
			goto out_delete_evlist;
		}
		nr_events[evsel->idx]++;
		perf_evlist__mmap_consume(evlist, 0);
	}

	err = 0;
	evlist__for_each(evlist, evsel) {
		if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
			pr_debug("expected %d %s events, got %d\n",
				 expected_nr_events[evsel->idx],
				 perf_evsel__name(evsel), nr_events[evsel->idx]);
			err = -1;
			goto out_delete_evlist;
		}
	}

out_delete_evlist:
	perf_evlist__delete(evlist);
	cpus	= NULL;
	threads = NULL;
out_free_cpus:
	cpu_map__delete(cpus);
out_free_threads:
	thread_map__delete(threads);
	return err;
}
Beispiel #8
0
/*
 * This test will open software clock events (cpu-clock, task-clock)
 * then check their frequency -> period conversion has no artifact of
 * setting period to 1 forcefully.
 */
static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
{
	int i, err = -1;
	volatile int tmp = 0;
	u64 total_periods = 0;
	int nr_samples = 0;
	char sbuf[STRERR_BUFSIZE];
	union perf_event *event;
	struct perf_evsel *evsel;
	struct perf_evlist *evlist;
	struct perf_event_attr attr = {
		.type = PERF_TYPE_SOFTWARE,
		.config = clock_id,
		.sample_type = PERF_SAMPLE_PERIOD,
		.exclude_kernel = 1,
		.disabled = 1,
		.freq = 1,
	};
	struct cpu_map *cpus;
	struct thread_map *threads;

	attr.sample_freq = 500;

	evlist = perf_evlist__new();
	if (evlist == NULL) {
		pr_debug("perf_evlist__new\n");
		return -1;
	}

	evsel = perf_evsel__new(&attr);
	if (evsel == NULL) {
		pr_debug("perf_evsel__new\n");
		goto out_delete_evlist;
	}
	perf_evlist__add(evlist, evsel);

	cpus = cpu_map__dummy_new();
	threads = thread_map__new_by_tid(getpid());
	if (!cpus || !threads) {
		err = -ENOMEM;
		pr_debug("Not enough memory to create thread/cpu maps\n");
		goto out_free_maps;
	}

	perf_evlist__set_maps(evlist, cpus, threads);

	cpus	= NULL;
	threads = NULL;

	if (perf_evlist__open(evlist)) {
		const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";

		err = -errno;
		pr_debug("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64 " in this test.\n",
			 str_error_r(errno, sbuf, sizeof(sbuf)),
			 knob, (u64)attr.sample_freq);
		goto out_delete_evlist;
	}

	err = perf_evlist__mmap(evlist, 128);
	if (err < 0) {
		pr_debug("failed to mmap event: %d (%s)\n", errno,
			 str_error_r(errno, sbuf, sizeof(sbuf)));
		goto out_delete_evlist;
	}

	perf_evlist__enable(evlist);

	/* collect samples */
	for (i = 0; i < NR_LOOPS; i++)
		tmp++;

	perf_evlist__disable(evlist);

	while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
		struct perf_sample sample;

		if (event->header.type != PERF_RECORD_SAMPLE)
			goto next_event;

		err = perf_evlist__parse_sample(evlist, event, &sample);
		if (err < 0) {
			pr_debug("Error during parse sample\n");
			goto out_delete_evlist;
		}

		total_periods += sample.period;
		nr_samples++;
next_event:
		perf_evlist__mmap_consume(evlist, 0);
	}

	if ((u64) nr_samples == total_periods) {
		pr_debug("All (%d) samples have period value of 1!\n",
			 nr_samples);
		err = -1;
	}

out_free_maps:
	cpu_map__put(cpus);
	thread_map__put(threads);
out_delete_evlist:
	perf_evlist__delete(evlist);
	return err;
}

int test__sw_clock_freq(struct test *test __maybe_unused, int subtest __maybe_unused)
{
	int ret;

	ret = __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK);
	if (!ret)
		ret = __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK);

	return ret;
}
Beispiel #9
0
/**
 * test__perf_time_to_tsc - test converting perf time to TSC.
 *
 * This function implements a test that checks that the conversion of perf time
 * to and from TSC is consistent with the order of events.  If the test passes
 * %0 is returned, otherwise %-1 is returned.  If TSC conversion is not
 * supported then then the test passes but " (not supported)" is printed.
 */
int test__perf_time_to_tsc(void)
{
	struct record_opts opts = {
		.mmap_pages	     = UINT_MAX,
		.user_freq	     = UINT_MAX,
		.user_interval	     = ULLONG_MAX,
		.freq		     = 4000,
		.target		     = {
			.uses_mmap   = true,
		},
		.sample_time	     = true,
	};
	struct thread_map *threads = NULL;
	struct cpu_map *cpus = NULL;
	struct perf_evlist *evlist = NULL;
	struct perf_evsel *evsel = NULL;
	int err = -1, ret, i;
	const char *comm1, *comm2;
	struct perf_tsc_conversion tc;
	struct perf_event_mmap_page *pc;
	union perf_event *event;
	u64 test_tsc, comm1_tsc, comm2_tsc;
	u64 test_time, comm1_time = 0, comm2_time = 0;

	threads = thread_map__new(-1, getpid(), UINT_MAX);
	CHECK_NOT_NULL__(threads);

	cpus = cpu_map__new(NULL);
	CHECK_NOT_NULL__(cpus);

	evlist = perf_evlist__new();
	CHECK_NOT_NULL__(evlist);

	perf_evlist__set_maps(evlist, cpus, threads);

	CHECK__(parse_events(evlist, "cycles:u", NULL));

	perf_evlist__config(evlist, &opts);

	evsel = perf_evlist__first(evlist);

	evsel->attr.comm = 1;
	evsel->attr.disabled = 1;
	evsel->attr.enable_on_exec = 0;

	CHECK__(perf_evlist__open(evlist));

	CHECK__(perf_evlist__mmap(evlist, UINT_MAX, false));

	pc = evlist->mmap[0].base;
	ret = perf_read_tsc_conversion(pc, &tc);
	if (ret) {
		if (ret == -EOPNOTSUPP) {
			fprintf(stderr, " (not supported)");
			return 0;
		}
		goto out_err;
	}

	perf_evlist__enable(evlist);

	comm1 = "Test COMM 1";
	CHECK__(prctl(PR_SET_NAME, (unsigned long)comm1, 0, 0, 0));

	test_tsc = rdtsc();

	comm2 = "Test COMM 2";
	CHECK__(prctl(PR_SET_NAME, (unsigned long)comm2, 0, 0, 0));

	perf_evlist__disable(evlist);

	for (i = 0; i < evlist->nr_mmaps; i++) {
		while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
			struct perf_sample sample;

			if (event->header.type != PERF_RECORD_COMM ||
			    (pid_t)event->comm.pid != getpid() ||
			    (pid_t)event->comm.tid != getpid())
				goto next_event;

			if (strcmp(event->comm.comm, comm1) == 0) {
				CHECK__(perf_evsel__parse_sample(evsel, event,
								 &sample));
				comm1_time = sample.time;
			}
			if (strcmp(event->comm.comm, comm2) == 0) {
				CHECK__(perf_evsel__parse_sample(evsel, event,
								 &sample));
				comm2_time = sample.time;
			}
next_event:
			perf_evlist__mmap_consume(evlist, i);
		}
	}

	if (!comm1_time || !comm2_time)
		goto out_err;

	test_time = tsc_to_perf_time(test_tsc, &tc);
	comm1_tsc = perf_time_to_tsc(comm1_time, &tc);
	comm2_tsc = perf_time_to_tsc(comm2_time, &tc);

	pr_debug("1st event perf time %"PRIu64" tsc %"PRIu64"\n",
		 comm1_time, comm1_tsc);
	pr_debug("rdtsc          time %"PRIu64" tsc %"PRIu64"\n",
		 test_time, test_tsc);
	pr_debug("2nd event perf time %"PRIu64" tsc %"PRIu64"\n",
		 comm2_time, comm2_tsc);

	if (test_time <= comm1_time ||
	    test_time >= comm2_time)
		goto out_err;

	if (test_tsc <= comm1_tsc ||
	    test_tsc >= comm2_tsc)
		goto out_err;

	err = 0;

out_err:
	if (evlist) {
		perf_evlist__disable(evlist);
		perf_evlist__delete(evlist);
	}

	return err;
}