From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753065AbdFMLeA (ORCPT ); Tue, 13 Jun 2017 07:34:00 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:47463 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752565AbdFMLd7 (ORCPT ); Tue, 13 Jun 2017 07:33:59 -0400 From: "Naveen N. Rao" To: Peter Zijlstra , Arnaldo Carvalho de Melo , Jiri Olsa , Ingo Molnar Cc: linux-kernel@vger.kernel.org Subject: [RFC PATCH v2 0/2] Notifications for perf sideband events Date: Tue, 13 Jun 2017 17:03:41 +0530 X-Mailer: git-send-email 2.12.2 X-TM-AS-MML: disable x-cbid: 17061311-0040-0000-0000-0000032D3E28 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17061311-0041-0000-0000-00000CA761EF Message-Id: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-06-13_07:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1706130205 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org v2: Here is a slightly different approach compared to v1: instead of introducing a new IOCTL, we ride on top of the existing support for ring buffer wakeup to generate signals as desired. Counting sideband events still requires some changes in the output path, but in normal cases, it ends up being just a comparison. The test program below needs to be changed for this version. Add below and remove the ioctl call for PERF_EVENT_IOC_COUNT_RECORDS: pe.signal_on_wakeup = 1; pe.count_sb_events = 1; pe.wakeup_events = 2; Also, I have not handled PERF_EVENT_IOC_REFRESH in this version, so IOC_ENABLE needs to be used. v1: https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1414197.html Cover letter from v1: -------------------- Currently, there is no way to ask for signals to be delivered when a certain number of sideband events have been logged into the ring buffer. This is problematic if we are only interested in, say, context switch events. This patch provides for a way to achieve this. As noted, this is a RFC and I am not too specific about the interface or the ioctl name. Kindly suggest if you think there is a better way to achieve this. - Naveen --- Here is a sample program demonstrating the same: #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) { return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); } static void sigio_handler(int n, siginfo_t *info, void *uc) { fprintf (stderr, "Caught %s\n", info->si_code == POLL_HUP ? "POLL_HUP" : (info->si_code == POLL_IN ? "POLL_IN" : "other signal")); if (ioctl(info->si_fd, PERF_EVENT_IOC_REFRESH, 2) == -1) perror("SIGIO: IOC_REFRESH"); } int main(int argc, char **argv) { struct perf_event_attr pe; struct sigaction act; int fd; void *buf; memset(&act, 0, sizeof(act)); act.sa_sigaction = sigio_handler; act.sa_flags = SA_SIGINFO; sigaction(SIGIO, &act, 0); memset(&pe, 0, sizeof(struct perf_event_attr)); pe.size = sizeof(struct perf_event_attr); pe.type = PERF_TYPE_SOFTWARE; pe.config = PERF_COUNT_SW_DUMMY; pe.disabled = 1; pe.sample_period = 1; pe.context_switch = 1; fd = perf_event_open(&pe, 0, -1, -1, 0); if (fd == -1) { fprintf(stderr, "Error opening leader %lx\n", pe.config); exit(EXIT_FAILURE); } buf = mmap(NULL, sysconf(_SC_PAGESIZE) * 2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (buf == MAP_FAILED) { fprintf(stderr, "Can't mmap buffer\n"); return -1; } if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_ASYNC) == -1) return -2; if (fcntl(fd, F_SETSIG, SIGIO) == -1) return -3; if (fcntl(fd, F_SETOWN, getpid()) == -1) return -4; if (ioctl(fd, PERF_EVENT_IOC_COUNT_RECORDS, 0) == -1) return -5; if (ioctl(fd, PERF_EVENT_IOC_REFRESH, 2) == -1) return -6; fprintf (stderr, "Sleep 1\n"); sleep(1); fprintf (stderr, "Sleep 2\n"); sleep(1); fprintf (stderr, "Sleep 3\n"); sleep(1); /* Disable the event counter */ ioctl(fd, PERF_EVENT_IOC_DISABLE, 1); close(fd); return 0; } A sample output: $ time ./cs Sleep 1 Caught POLL_HUP Sleep 2 Caught POLL_HUP Sleep 3 Caught POLL_HUP real 0m3.060s user 0m0.001s sys 0m0.005s Naveen N. Rao (2): kernel/events: Add option to notify through signals on wakeup kernel/events: Add option to enable counting sideband events in wakeup_events include/uapi/linux/perf_event.h | 4 +++- kernel/events/core.c | 14 ++++++++------ kernel/events/ring_buffer.c | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) -- 2.12.2