From patchwork Tue Mar 23 13:44:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 42748 X-Patchwork-Delegate: siddhesh@gotplt.org Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0C7D33850405; Tue, 23 Mar 2021 13:45:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0C7D33850405 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1616507113; bh=OY9WMHeQzpfDUfyJkl/M9wzlkaSNBo6bfed6b1VMAdI=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=c5uHNs6sdEUpvx28lrqlojf2Wtqxe2Ny8pQHf+RyUC8qvlD4VOWk8poHh15gni+5R SmYU8ykayTP98Z3D23ir7X9gENkWUeOxsTdr9I6XqGr2h/WgI+2C0EUcnSGtiGF71L FaeF0bZT31c1IefaPiVgdG3QmfLjCL6TkwHDLZ3g= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by sourceware.org (Postfix) with ESMTPS id 14D973850404 for ; Tue, 23 Mar 2021 13:45:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 14D973850404 Received: by mail-qk1-x734.google.com with SMTP id z10so14308804qkz.13 for ; Tue, 23 Mar 2021 06:45:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OY9WMHeQzpfDUfyJkl/M9wzlkaSNBo6bfed6b1VMAdI=; b=ePngc43b+xHSmOiJaFa99E++ZqWLmn1trGPV2yzyEoysgVUvkta6MF9iycrrWaVaO0 4VQix6R8grodf8muchv9WT7UVnen9fLOxsBoKF1C7f3ELwkIl6mRnKYuHBfeQPBPup7Z P8hyOR62BpK2XI84xZFFOJZNinoVcPgMe+r8fN261PUkwqBQUKcIlsUCM1CehDLIT5RA XhZljjSTQIL86tb64AnDbDTFApLtSuWiMFO5yZ9agrEzpWE5Vpw3y6/r9httqgk2tkg5 naUXBB4FS6jYvRlR+6bsARB6vhPFQoxB8czonR4Aqw+9pGZzbY9cANxXyY/mxDeWbGCi ccnw== X-Gm-Message-State: AOAM531Y/V4dgnd9gPtqWhwaQEZSSSP4GQR3rWQ7LCGGd55BoW6J2Cnu SnS/5h9wY/SSONn9Ey0arKCGPZIW50xXng== X-Google-Smtp-Source: ABdhPJw6UpHfJmxNqjFle2Ug9QXavGrbAF7t0CVG9QBNZSMrObeqtCrg1Dep54qMK3WaUzlz8l1Rjg== X-Received: by 2002:ae9:eb58:: with SMTP id b85mr5452704qkg.168.1616507109250; Tue, 23 Mar 2021 06:45:09 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id z24sm12783384qkz.65.2021.03.23.06.45.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Mar 2021 06:45:09 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 06/15] time: Add getitimer and setitimer basic tests Date: Tue, 23 Mar 2021 10:44:49 -0300 Message-Id: <20210323134458.2563022-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210323134458.2563022-1-adhemerval.zanella@linaro.org> References: <20210323134458.2563022-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Checked on i686-linux-gnu and x86_64-linux-gnu. Reviewed-by: Siddhesh Poyarekar --- time/Makefile | 2 +- time/tst-itimer.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 time/tst-itimer.c diff --git a/time/Makefile b/time/Makefile index 99b5d42666..91cb02a003 100644 --- a/time/Makefile +++ b/time/Makefile @@ -49,7 +49,7 @@ tests := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \ tst-tzname tst-y2039 bug-mktime4 tst-strftime2 tst-strftime3 \ tst-clock tst-clock2 tst-clock_nanosleep tst-cpuclock1 \ tst-adjtime tst-ctime tst-difftime tst-mktime4 tst-clock_settime \ - tst-settimeofday + tst-settimeofday tst-itimer include ../Rules diff --git a/time/tst-itimer.c b/time/tst-itimer.c new file mode 100644 index 0000000000..e464d6b092 --- /dev/null +++ b/time/tst-itimer.c @@ -0,0 +1,175 @@ +/* Basic tests for getitimer and setitimer. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include + +static sig_atomic_t cnt; + +static void +alrm_handler (int sig) +{ + if (++cnt > 3) + cnt = 3; +} + +static void +intr_sleep (int sec) +{ + struct timespec ts = { .tv_sec = sec, .tv_nsec = 0 }; + while (nanosleep (&ts, &ts) == -1 && errno == EINTR) + ; +} + +static int +do_test (void) +{ + struct itimerval it, it_old; + const int timers[] = { ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF }; + for (int i = 0; i < array_length (timers); i++) + { + TEST_COMPARE (getitimer (timers[i], &it), 0); + + /* No timer set, all value should be 0. */ + TEST_COMPARE (it.it_interval.tv_sec, 0); + TEST_COMPARE (it.it_interval.tv_usec, 0); + TEST_COMPARE (it.it_value.tv_sec, 0); + TEST_COMPARE (it.it_value.tv_usec, 0); + + it.it_interval.tv_sec = 10; + it.it_interval.tv_usec = 20; + TEST_COMPARE (setitimer (timers[i], &it, NULL), 0); + + TEST_COMPARE (setitimer (timers[i], &(struct itimerval) { 0 }, &it_old), + 0); + /* ITIMER_REAL returns { 0, 0 } for single-sort timers, while + other timers returns setitimer value. */ + if (timers[i] == ITIMER_REAL) + { + TEST_COMPARE (it_old.it_interval.tv_sec, 0); + TEST_COMPARE (it_old.it_interval.tv_usec, 0); + } + else + { + TEST_COMPARE (it_old.it_interval.tv_sec, 10); + TEST_COMPARE (it_old.it_interval.tv_usec, 20); + } + + /* Create a periodic timer and check if the return value is the one + previously set. */ + it.it_interval.tv_sec = 10; + it.it_interval.tv_usec = 20; + it.it_value.tv_sec = 30; + it.it_value.tv_usec = 40; + TEST_COMPARE (setitimer (timers[i], &it, NULL), 0); + + TEST_COMPARE (setitimer (timers[i], &(struct itimerval) { 0 }, &it_old), + 0); + TEST_COMPARE (it.it_interval.tv_sec, it_old.it_interval.tv_sec); + TEST_COMPARE (it.it_interval.tv_usec, it_old.it_interval.tv_usec); + + if (sizeof (time_t) == 4) + continue; + + /* Same as before, but with a 64 bit time_t value. */ + it.it_interval.tv_sec = (time_t) 0x1ffffffffull; + it.it_interval.tv_usec = 20; + it.it_value.tv_sec = 0; + it.it_value.tv_usec = 0; + + /* Linux does not provide 64 bit time_t support for getitimer and + setitimer on architectures with 32 bit time_t support. */ + if (sizeof (__time_t) == 8) + { + TEST_COMPARE (setitimer (timers[i], &it, NULL), 0); + TEST_COMPARE (setitimer (timers[i], &(struct itimerval) { 0 }, + &it_old), + 0); + /* ITIMER_REAL returns { 0, 0 } for single-sort timers, while other + timers returns setitimer value. */ + if (timers[i] == ITIMER_REAL) + { + TEST_COMPARE (it_old.it_interval.tv_sec, 0ull); + TEST_COMPARE (it_old.it_interval.tv_usec, 0); + } + else + { + TEST_COMPARE (it_old.it_interval.tv_sec, 0x1ffffffffull); + TEST_COMPARE (it_old.it_interval.tv_usec, 20); + } + } + else + { + TEST_COMPARE (setitimer (timers[i], &it, NULL), -1); + TEST_COMPARE (errno, EOVERFLOW); + } + + /* Create a periodic timer and check if the return value is the one + previously set. */ + it.it_interval.tv_sec = (time_t) 0x1ffffffffull; + it.it_interval.tv_usec = 20; + it.it_value.tv_sec = 30; + it.it_value.tv_usec = 40; + if (sizeof (__time_t) == 8) + { + TEST_COMPARE (setitimer (timers[i], &it, NULL), 0); + + TEST_COMPARE (setitimer (timers[i], &(struct itimerval) { 0 }, + &it_old), + 0); + TEST_COMPARE (it.it_interval.tv_sec, it_old.it_interval.tv_sec); + TEST_COMPARE (it.it_interval.tv_usec, it_old.it_interval.tv_usec); + } + else + { + TEST_COMPARE (setitimer (timers[i], &it, NULL), -1); + TEST_COMPARE (errno, EOVERFLOW); + } + } + + { + struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 }; + sigemptyset (&sa.sa_mask); + xsigaction (SIGALRM, &sa, NULL); + } + + /* Setup a timer to 0.1s and sleep for 1s and check to 3 signal handler + execution. */ + it.it_interval.tv_sec = 0; + it.it_interval.tv_usec = 100000; + it.it_value.tv_sec = 0; + it.it_value.tv_usec = 100000; + + /* Check ITIMER_VIRTUAL and ITIMER_PROF would require to generate load + and be subject to system load. */ + cnt = 0; + TEST_COMPARE (setitimer (ITIMER_REAL, &it, NULL), 0); + intr_sleep (1); + TEST_COMPARE (cnt, 3); + TEST_COMPARE (setitimer (ITIMER_REAL, &(struct itimerval) { 0 }, NULL), 0); + + return 0; +} + +#include