From patchwork Fri Mar 5 20:14:30 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: 42286 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 8C9F73885C03; Fri, 5 Mar 2021 20:15:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C9F73885C03 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1614975332; bh=usjtUNlGwudA7PRwzes++bSCupg3AAaTBIieCKfPr1A=; 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=pO9Vnd1ftgiNsEm7YmOdWwm4mr36rq9/Sg4ufAKAgwv3VNWP/QoMLVpqf8jGSPqwL vFH8sxGGKb4Nj0RYGX+q2iC1gdQzgzigwF1okUjvPBM4Y3F+YPuVl6ii0v9EHtAk7T tT01Iu2xNwhBIs4ZlckW3tgDKxeR4vNg0Qi5u6U0= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) by sourceware.org (Postfix) with ESMTPS id 9677E3836C4A for ; Fri, 5 Mar 2021 20:15:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9677E3836C4A Received: by mail-qk1-x72a.google.com with SMTP id s7so3291058qkg.4 for ; Fri, 05 Mar 2021 12:15:28 -0800 (PST) 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=usjtUNlGwudA7PRwzes++bSCupg3AAaTBIieCKfPr1A=; b=WLBln5go1yoCkwK1hbSF9vi//ym2o+Vllr9Fzf1UDPo/A7aRsTLEN68C4wtki4JGbe 5Fq6VEdNKLPk6DrfGq3PYQqLPNIqH7E0s8IjSnLmDbgjyzfP/simtMpZiZB1H65eprlM tpi0sY8RijC+Nql71s+G/XBQWlneRZVsxL/c/HLbgLPMEoDqWGXvidiI7VUntjfXvp5z CgoiAmEIWCcwZsyeXNAfVI36ZXUWWw+w1PJ8Aw/RvT6uiyh3xYfKlTnBiTRXOJ9AHNqw QkgP4D41u0OKf2dIjQLtPxFGay9B1Y6fFA7C5Kd2OHFAasu3LpyS6Uks6Q/yEZ6/w6yb Km6A== X-Gm-Message-State: AOAM531spaYZhifFkjWw72h7MGPCkf7mYrOtZFw0PJm4h6Z9k7zhGKLc xjPWntSRClmRqPnIuBhojhf8dyv099bYsw== X-Google-Smtp-Source: ABdhPJyaY/vICRVJWjizn3kLe6bc3VfuQ5Ggyt0JyMCp/jeAlOPHbkgieERvdxaRXHK4VQzr6HC+5w== X-Received: by 2002:a37:30f:: with SMTP id 15mr11057731qkd.494.1614975327978; Fri, 05 Mar 2021 12:15:27 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id c5sm2526145qkl.21.2021.03.05.12.15.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Mar 2021 12:15:27 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH 04/52] time: Add getitimer and setitimer basic tests Date: Fri, 5 Mar 2021 17:14:30 -0300 Message-Id: <20210305201518.798584-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210305201518.798584-1-adhemerval.zanella@linaro.org> References: <20210305201518.798584-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.4 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. --- 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 a52b6b94bc..ccc9eefd61 100644 --- a/time/Makefile +++ b/time/Makefile @@ -48,7 +48,7 @@ tests := test_time clocktest tst-posixtz tst-strptime tst_wcsftime \ tst-strptime3 bug-getdate1 tst-strptime-whitespace tst-ftime \ 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-adjtime tst-ctime tst-difftime tst-mktime4 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