From nobody@FreeBSD.org  Mon Mar 17 17:59:47 2014
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1])
	(using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by hub.freebsd.org (Postfix) with ESMTPS id 98ED6EBF
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 17 Mar 2014 17:59:47 +0000 (UTC)
Received: from cgiserv.freebsd.org (cgiserv.freebsd.org [IPv6:2001:1900:2254:206a::50:4])
	(using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by mx1.freebsd.org (Postfix) with ESMTPS id 6D411365
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 17 Mar 2014 17:59:47 +0000 (UTC)
Received: from cgiserv.freebsd.org ([127.0.1.6])
	by cgiserv.freebsd.org (8.14.8/8.14.8) with ESMTP id s2HHxl9n008918
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 17 Mar 2014 17:59:47 GMT
	(envelope-from nobody@cgiserv.freebsd.org)
Received: (from nobody@localhost)
	by cgiserv.freebsd.org (8.14.8/8.14.8/Submit) id s2HHxlhK008917;
	Mon, 17 Mar 2014 17:59:47 GMT
	(envelope-from nobody)
Message-Id: <201403171759.s2HHxlhK008917@cgiserv.freebsd.org>
Date: Mon, 17 Mar 2014 17:59:47 GMT
From: Vasily Postnicov <shamaz.mazum@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Profiling timer does not work when setting low interval
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         187668
>Category:       kern
>Synopsis:       [kernel] Profiling timer does not work when setting low interval due to change in kern_time.c
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Mar 17 18:00:00 UTC 2014
>Closed-Date:    Wed Apr 23 12:39:06 UTC 2014
>Last-Modified:  Wed Apr 23 12:50:00 UTC 2014
>Originator:     Vasily Postnicov
>Release:        Freebsd 10.0
>Organization:
>Environment:
FreeBSD vonbraun 10.0-RELEASE FreeBSD 10.0-RELEASE #1 513d8e6(clocktst): Sun Mar 16 23:02:04 MSK 2014     vasily@vonbraun:/usr/obj/usr/src/sys/MYKERNEL10  am
d64                                                                                                                                                         
>Description:
After this commit:

https://github.com/freebsd/freebsd/commit/5dfd8a110a3e6121977586541b839200faef91da

a statistical profiler in SBCL (Steel Bank Common Lisp) does not work
when sampling interval is set very low.

I attach a test which gives unexpected results which confuse SBCL when
profiling interval is set to 0.1 ms (just as in sb-sprof test from SBCL):

Start
1
0 100 -15 676200

And with that commit reverted it gives following:
                                                    
Start
14344
0 1000 0 1000

It means that lowest possible profiling interval is set (1 ms). All as expected.
>How-To-Repeat:
Just compile and run my test and wait 15 sec.
>Fix:


Patch attached with submission follows:

#include <sys/time.h>
#include <signal.h>
#include <stdio.h>

int counter = 0;

void handler (int sig)
{
    counter++;
}

int main ()
{
    printf ("Start\n");
    struct sigaction sa;
    sa.sa_handler = handler;
    sa.sa_flags = 0;
    sigemptyset (&sa.sa_mask);
    sigaction (SIGPROF, &sa, NULL);

    struct itimerval val;
    struct itimerval val_old;
    val.it_interval.tv_sec  = 0;
    val.it_interval.tv_usec = 100;

    val.it_value.tv_sec  = 0;
    val.it_value.tv_usec = 100;

    struct timeval time;
    gettimeofday(&time, 0);
    time_t start = time.tv_sec;
    time_t cur = start;
    
    setitimer (2, &val, NULL);

    do
    {
        gettimeofday(&time, 0);
        cur = time.tv_sec;
    }
    while (cur < start+15);

    val.it_value.tv_sec  = 0;
    val.it_value.tv_usec = 0;
    setitimer (2, &val, &val_old);

    printf ("%i\n", counter);
    printf ("%li %li %li %li\n",
            val_old.it_interval.tv_sec,
            val_old.it_interval.tv_usec,
            val_old.it_value.tv_sec,
            val_old.it_value.tv_usec);

    return 0;
}


>Release-Note:
>Audit-Trail:

From: Mark Linimon <linimon@lonesome.com>
To: Vasily Postnicov <shamaz.mazum@gmail.com>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: kern/187668: Profiling timer does not work when setting low
 interval
Date: Tue, 15 Apr 2014 21:35:46 -0500

 On Mon, Mar 17, 2014 at 05:59:47PM +0000, Vasily Postnicov wrote:
 > After this commit:
 > 
 > https://github.com/freebsd/freebsd/commit/5dfd8a110a3e6121977586541b839200faef91da
 > 
 > a statistical profiler in SBCL (Steel Bank Common Lisp) does not work when sampling interval is set very low.
 
 Adding a note for annotation: this corresponds to
 http://svnweb.freebsd.org/base?view=revision&revision=247903 by mav

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/187668: commit references a PR
Date: Wed, 16 Apr 2014 18:37:50 +0000 (UTC)

 Author: mav
 Date: Wed Apr 16 18:37:46 2014
 New Revision: 264550
 URL: http://svnweb.freebsd.org/changeset/base/264550
 
 Log:
   Fix VIRTUAL and PROF interval timers for short intervals, broken at r247903.
   
   Due to the way those timers are implemented, we can't handle very short
   intervals.  In addition to that mentioned patch caused math overflows
   for short intervals.  To avoid that round those intervals to 1 tick.
   
   PR:		kern/187668
   MFC after:	1 week
 
 Modified:
   head/sys/kern/kern_time.c
 
 Modified: head/sys/kern/kern_time.c
 ==============================================================================
 --- head/sys/kern/kern_time.c	Wed Apr 16 18:23:36 2014	(r264549)
 +++ head/sys/kern/kern_time.c	Wed Apr 16 18:37:46 2014	(r264550)
 @@ -774,6 +774,14 @@ kern_setitimer(struct thread *td, u_int 
  				timevalsub(&oitv->it_value, &ctv);
  		}
  	} else {
 +		if (aitv->it_interval.tv_sec == 0 &&
 +		    aitv->it_interval.tv_usec != 0 &&
 +		    aitv->it_interval.tv_usec < tick)
 +			aitv->it_interval.tv_usec = tick;
 +		if (aitv->it_value.tv_sec == 0 &&
 +		    aitv->it_value.tv_usec != 0 &&
 +		    aitv->it_value.tv_usec < tick)
 +			aitv->it_value.tv_usec = tick;
  		PROC_SLOCK(p);
  		*oitv = p->p_stats->p_timer[which];
  		p->p_stats->p_timer[which] = *aitv;
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->closed 
State-Changed-By: mav 
State-Changed-When: Wed Apr 23 12:38:11 UTC 2014 
State-Changed-Why:  
Fix committed and merged. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=187668 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/187668: commit references a PR
Date: Wed, 23 Apr 2014 12:46:31 +0000 (UTC)

 Author: mav
 Date: Wed Apr 23 12:46:27 2014
 New Revision: 264821
 URL: http://svnweb.freebsd.org/changeset/base/264821
 
 Log:
   MFC r264550:
   Fix VIRTUAL and PROF interval timers for short intervals, broken at r247903.
   
   Due to the way those timers are implemented, we can't handle very short
   intervals.  In addition to that mentioned patch caused math overflows
   for short intervals.  To avoid that round those intervals to 1 tick.
   
   PR:		kern/187668
 
 Modified:
   stable/10/sys/kern/kern_time.c
 Directory Properties:
   stable/10/   (props changed)
 
 Modified: stable/10/sys/kern/kern_time.c
 ==============================================================================
 --- stable/10/sys/kern/kern_time.c	Wed Apr 23 12:16:36 2014	(r264820)
 +++ stable/10/sys/kern/kern_time.c	Wed Apr 23 12:46:27 2014	(r264821)
 @@ -774,6 +774,14 @@ kern_setitimer(struct thread *td, u_int 
  				timevalsub(&oitv->it_value, &ctv);
  		}
  	} else {
 +		if (aitv->it_interval.tv_sec == 0 &&
 +		    aitv->it_interval.tv_usec != 0 &&
 +		    aitv->it_interval.tv_usec < tick)
 +			aitv->it_interval.tv_usec = tick;
 +		if (aitv->it_value.tv_sec == 0 &&
 +		    aitv->it_value.tv_usec != 0 &&
 +		    aitv->it_value.tv_usec < tick)
 +			aitv->it_value.tv_usec = tick;
  		PROC_SLOCK(p);
  		*oitv = p->p_stats->p_timer[which];
  		p->p_stats->p_timer[which] = *aitv;
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
>Unformatted:
