From owner-freebsd-hackers@FreeBSD.ORG Sun Mar 15 02:45:26 2015 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 48FCA741 for ; Sun, 15 Mar 2015 02:45:26 +0000 (UTC) Received: from mail-qc0-x235.google.com (mail-qc0-x235.google.com [IPv6:2607:f8b0:400d:c01::235]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 006C4CA0 for ; Sun, 15 Mar 2015 02:45:26 +0000 (UTC) Received: by qcaz10 with SMTP id z10so18696919qca.1 for ; Sat, 14 Mar 2015 19:45:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=UERT2OTXE1cMxvMQG7wENZHa98UAvWN3ZrC2W5PasFc=; b=irnQ5WMT8OxrmZepmqKCzNPJF/+YowNLCW7ywOtaksV/RMEw+PFfEzVvZMIPO7L721 Jk0rfcXCGray8MNaiFdwVfBUuydgAekR+BX5FS0PEQPMzPNUvOzAF1wDijZij7EklC/D B19Xq1Hyc23V/Hv+BaSB957IMm1xLOMrz9CfheuuHzaGh2u1kxPG3ybSY9wZi1ttsBSv 8jSjBfor7legpxfvvvFDdUSgawOVK9TeaC+7Hr2yPQ0V9s1iqEOxGGKdBGiNXTQu78/J VWeRt9WULTB4WHFbbm+x3m9uuO+fSIOh1hPb6g2kyfhIABZScFE4KTw/Ko5M+MZLC9bh OrSg== MIME-Version: 1.0 X-Received: by 10.141.28.78 with SMTP id f75mr71221647qhe.18.1426387525165; Sat, 14 Mar 2015 19:45:25 -0700 (PDT) Received: by 10.229.24.73 with HTTP; Sat, 14 Mar 2015 19:45:25 -0700 (PDT) Date: Sat, 14 Mar 2015 19:45:25 -0700 Message-ID: Subject: ngets() consuming 100% CPU From: Ivan Krivonos To: freebsd-hackers@freebsd.org Content-Type: text/plain; charset=UTF-8 X-Content-Filtered-By: Mailman/MimeDel 2.1.18-1 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Mar 2015 02:45:26 -0000 Hi All, I`ve noted that ngets() from libstand starts consuming CPU like mad when EOF appears on stdin. Not sure if this actually is a bug, but issue appears from time to time with bhyveload. Here is simpliest program allowing reproduce cat > ./bget.c #include #include #include #include int main() { char str[512]; for (;;) { ngets(str, sizeof(str)); if (str[0] == '\n') break; } return 0; } $ echo "balbalbalabl" | ./bget & $ You will see bget consuming CPU in ngets() reading EOF. The patch below fixes the problem diff --git a/lib/libstand/gets.c b/lib/libstand/gets.c index 7d54b00..806091a 100644 --- a/lib/libstand/gets.c +++ b/lib/libstand/gets.c @@ -44,8 +44,12 @@ ngets(char *buf, int n) int c; char *lp; - for (lp = buf;;) - switch (c = getchar() & 0177) { + for (lp = buf;;) { + c = getchar(); + if (c == EOF) + break; + + switch (c & 0177) { case '\n': case '\r': *lp = '\0'; @@ -79,6 +83,7 @@ ngets(char *buf, int n) putchar(c); } } + } /*NOTREACHED*/ }