Esempio n. 1
0
    def _open_for_cpu(self, pmu: int, cpu: int, event: Event):
        """Opens file descriptor for event selected via event_name, for selected cpu"""
        group_file = self._group_event_leader_files_per_pmu.get(pmu,
                                                                {}).get(cpu)
        is_group_leader = group_file is None

        # man perf_event_open cite regarding the disabled variable:
        #   When creating an event group, typically the group leader is
        #     initialized with disabled set to 1 and any child events are
        #     initialized with disabled set to 0.  Despite disabled being 0,
        #     the child events will not start until the group leader is
        #     enabled.
        # Disabled when creating new leader.
        # Enabled for children
        flags = 0  # pc.PERF_FLAG_FD_CLOEXEC
        pid = -1
        if is_group_leader:
            disabled = True
            group_fd = -1
        else:
            disabled = False
            group_fd = group_file.fileno()

        self.attr = _create_event_attributes(
            pmu,
            disabled,
            event,
        )
        pfd = _perf_event_open(perf_event_attr=ctypes.byref(self.attr),
                               pid=pid,
                               cpu=cpu,
                               group_fd=group_fd,
                               flags=flags)
        perf_event_file = _create_file_from_fd(pfd)

        if is_group_leader:
            self._group_event_leader_files_per_pmu[pmu][cpu] = perf_event_file
        else:
            self._event_files.append(perf_event_file)
def test_create_file_from_invalid_fd():
    with pytest.raises(perf.UnableToOpenPerfEvents):
        perf._create_file_from_fd(-1)
def test_create_file_from_valid_fd(_):
    file = perf._create_file_from_fd(1)
    assert file is not None