Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/tracing: Add hist poll() support test

Add a testcase for poll() on hist file. This introduces a helper binary
to the ftracetest, because there is no good way to reliably execute
poll() on hist file.

Cc: Shuah Khan <shuah@kernel.org>
Cc: Tom Zanussi <zanussi@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Link: https://lore.kernel.org/173547867935.569911.10127126796879854182.stgit@devnote2
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Reviewed-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

authored by

Masami Hiramatsu (Google) and committed by
Steven Rostedt (Google)
80c3e285 66fc6f52

+150
+2
tools/testing/selftests/ftrace/Makefile
··· 6 6 TEST_FILES := test.d settings 7 7 EXTRA_CLEAN := $(OUTPUT)/logs/* 8 8 9 + TEST_GEN_PROGS = poll 10 + 9 11 include ../lib.mk
+74
tools/testing/selftests/ftrace/poll.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Simple poll on a file. 4 + * 5 + * Copyright (c) 2024 Google LLC. 6 + */ 7 + 8 + #include <errno.h> 9 + #include <fcntl.h> 10 + #include <poll.h> 11 + #include <stdio.h> 12 + #include <stdlib.h> 13 + #include <string.h> 14 + #include <unistd.h> 15 + 16 + #define BUFSIZE 4096 17 + 18 + /* 19 + * Usage: 20 + * poll [-I|-P] [-t timeout] FILE 21 + */ 22 + int main(int argc, char *argv[]) 23 + { 24 + struct pollfd pfd = {.events = POLLIN}; 25 + char buf[BUFSIZE]; 26 + int timeout = -1; 27 + int ret, opt; 28 + 29 + while ((opt = getopt(argc, argv, "IPt:")) != -1) { 30 + switch (opt) { 31 + case 'I': 32 + pfd.events = POLLIN; 33 + break; 34 + case 'P': 35 + pfd.events = POLLPRI; 36 + break; 37 + case 't': 38 + timeout = atoi(optarg); 39 + break; 40 + default: 41 + fprintf(stderr, "Usage: %s [-I|-P] [-t timeout] FILE\n", 42 + argv[0]); 43 + return -1; 44 + } 45 + } 46 + if (optind >= argc) { 47 + fprintf(stderr, "Error: Polling file is not specified\n"); 48 + return -1; 49 + } 50 + 51 + pfd.fd = open(argv[optind], O_RDONLY); 52 + if (pfd.fd < 0) { 53 + fprintf(stderr, "failed to open %s", argv[optind]); 54 + perror("open"); 55 + return -1; 56 + } 57 + 58 + /* Reset poll by read if POLLIN is specified. */ 59 + if (pfd.events & POLLIN) 60 + do {} while (read(pfd.fd, buf, BUFSIZE) == BUFSIZE); 61 + 62 + ret = poll(&pfd, 1, timeout); 63 + if (ret < 0 && errno != EINTR) { 64 + perror("poll"); 65 + return -1; 66 + } 67 + close(pfd.fd); 68 + 69 + /* If timeout happned (ret == 0), exit code is 1 */ 70 + if (ret == 0) 71 + return 1; 72 + 73 + return 0; 74 + }
+74
tools/testing/selftests/ftrace/test.d/trigger/trigger-hist-poll.tc
··· 1 + #!/bin/sh 2 + # SPDX-License-Identifier: GPL-2.0 3 + # description: event trigger - test poll wait on histogram 4 + # requires: set_event events/sched/sched_process_free/trigger events/sched/sched_process_free/hist 5 + # flags: instance 6 + 7 + POLL=${FTRACETEST_ROOT}/poll 8 + 9 + if [ ! -x ${POLL} ]; then 10 + echo "poll program is not compiled!" 11 + exit_unresolved 12 + fi 13 + 14 + EVENT=events/sched/sched_process_free/ 15 + 16 + # Check poll ops is supported. Before implementing poll on hist file, it 17 + # returns soon with POLLIN | POLLOUT, but not POLLPRI. 18 + 19 + # This must wait >1 sec and return 1 (timeout). 20 + set +e 21 + ${POLL} -I -t 1000 ${EVENT}/hist 22 + ret=$? 23 + set -e 24 + if [ ${ret} != 1 ]; then 25 + echo "poll on hist file is not supported" 26 + exit_unsupported 27 + fi 28 + 29 + # Test POLLIN 30 + echo > trace 31 + echo 'hist:key=comm if comm =="sleep"' > ${EVENT}/trigger 32 + echo 1 > ${EVENT}/enable 33 + 34 + # This sleep command will exit after 2 seconds. 35 + sleep 2 & 36 + BGPID=$! 37 + # if timeout happens, poll returns 1. 38 + ${POLL} -I -t 4000 ${EVENT}/hist 39 + echo 0 > tracing_on 40 + 41 + if [ -d /proc/${BGPID} ]; then 42 + echo "poll exits too soon" 43 + kill -KILL ${BGPID} ||: 44 + exit_fail 45 + fi 46 + 47 + if ! grep -qw "sleep" trace; then 48 + echo "poll exits before event happens" 49 + exit_fail 50 + fi 51 + 52 + # Test POLLPRI 53 + echo > trace 54 + echo 1 > tracing_on 55 + 56 + # This sleep command will exit after 2 seconds. 57 + sleep 2 & 58 + BGPID=$! 59 + # if timeout happens, poll returns 1. 60 + ${POLL} -P -t 4000 ${EVENT}/hist 61 + echo 0 > tracing_on 62 + 63 + if [ -d /proc/${BGPID} ]; then 64 + echo "poll exits too soon" 65 + kill -KILL ${BGPID} ||: 66 + exit_fail 67 + fi 68 + 69 + if ! grep -qw "sleep" trace; then 70 + echo "poll exits before event happens" 71 + exit_fail 72 + fi 73 + 74 + exit_pass