From nobody@FreeBSD.org  Sun Jul  4 22:26:21 2010
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 2A239106566B
	for <freebsd-gnats-submit@FreeBSD.org>; Sun,  4 Jul 2010 22:26:21 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 0E58C8FC1B
	for <freebsd-gnats-submit@FreeBSD.org>; Sun,  4 Jul 2010 22:26:21 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o64MQKdI079414
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 4 Jul 2010 22:26:20 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o64MQKAk079386;
	Sun, 4 Jul 2010 22:26:20 GMT
	(envelope-from nobody)
Message-Id: <201007042226.o64MQKAk079386@www.freebsd.org>
Date: Sun, 4 Jul 2010 22:26:20 GMT
From: Dmitry Pryanishnikov <lynx.ripe@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [patch] Add loader tunable to override SC_HISTORY_SIZE
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         148367
>Category:       kern
>Synopsis:       [syscons] [patch] Add loader tunable to override SC_HISTORY_SIZE
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jul 04 22:30:01 UTC 2010
>Closed-Date:    
>Last-Modified:  Wed Dec 15 21:50:09 UTC 2010
>Originator:     Dmitry Pryanishnikov
>Release:        RELENG_8
>Organization:
Home
>Environment:
FreeBSD lynx.homenet 8.1-PRERELEASE FreeBSD 8.1-PRERELEASE #0: Sun Jul  4 20:13:03 EEST 2010     dmitry@lynx.homenet:/databig/obj/databig/ftp/RELENG_8/src.100704/sys/lynx  i386

>Description:
It is useful to keep SC_HISTORY_SIZE value high, however it costs kernel memory. So in order to share the same kernel between machines with big and not-so-big RAM, one should be able to tune the size of back scroll buffer. The attached patch makes it possible via the loader tunable hw.syscons.history_size. Patch works with today's RELENG_8, it also applies to HEAD (and should work there, since schistory.c is the same in HEAD and RELENG_8 except __FBSDID line).
>How-To-Repeat:

>Fix:


Patch attached with submission follows:

--- sys/dev/syscons/syscons.h.orig	2010-03-02 03:56:55.000000000 +0200
+++ sys/dev/syscons/syscons.h	2010-07-04 00:43:11.366661343 +0300
@@ -572,6 +572,7 @@
 
 /* schistory.c */
 #ifndef SC_NO_HISTORY
+void		sc_init_history(void);
 int		sc_alloc_history_buffer(scr_stat *scp, int lines,
 					int prev_ysize, int wait);
 void		sc_free_history_buffer(scr_stat *scp, int prev_ysize);
--- sys/dev/syscons/syscons.c.orig	2010-03-31 18:39:46.000000000 +0300
+++ sys/dev/syscons/syscons.c	2010-07-04 00:43:11.368678117 +0300
@@ -2726,8 +2726,12 @@
     int i;
 
     /* one time initialization */
-    if (init_done == COLD)
+    if (init_done == COLD) {
 	sc_get_bios_values(&bios_value);
+#ifndef SC_NO_HISTORY
+	sc_init_history();
+#endif
+    }
     init_done = WARM;
 
     /*
--- sys/dev/syscons/schistory.c.orig	2009-08-03 11:13:06.000000000 +0300
+++ sys/dev/syscons/schistory.c	2010-07-04 00:43:11.369673234 +0300
@@ -41,6 +41,7 @@
 #include <sys/tty.h>
 #include <sys/kernel.h>
 #include <sys/malloc.h>
+#include <sys/sysctl.h>
 
 #if defined(__sparc64__) || defined(__powerpc__)
 #include <machine/sc_machdep.h>
@@ -77,17 +78,37 @@
 static int		extra_history_size
 				= SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
 
+static int		sc_history_size = SC_HISTORY_SIZE;
+
+SYSCTL_DECL(_hw_syscons);
+SYSCTL_INT(_hw_syscons, OID_AUTO, history_size, CTLFLAG_RDTUN, &sc_history_size, 0,
+    "Number of history buffer lines");
+
 /* local functions */
 static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
 static void history_to_screen(scr_stat *scp);
 
+/* tune history buffer size */
+void
+sc_init_history(void)
+{
+
+	TUNABLE_INT_FETCH("hw.syscons.history_size", &sc_history_size);
+	if (sc_history_size < ROW * 4)
+	    sc_history_size = ROW * 4;
+	extra_history_size =
+	    ((sc_history_size * MAXCONS * MAXSC) > SC_MAX_HISTORY_SIZE ?
+		sc_history_size * MAXCONS * MAXSC : SC_MAX_HISTORY_SIZE) -
+		    sc_history_size * MAXCONS;
+}
+
 /* allocate a history buffer */
 int
 sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
 {
 	/*
 	 * syscons unconditionally allocates buffers upto 
-	 * SC_HISTORY_SIZE lines or scp->ysize lines, whichever 
+	 * sc_history_size lines or scp->ysize lines, whichever 
 	 * is larger. A value greater than that is allowed, 
 	 * subject to extra_history_size.
 	 */
@@ -98,7 +119,7 @@
 	int delta;				/* lines to put back */
 
 	if (lines <= 0)
-		lines = SC_HISTORY_SIZE;	/* use the default value */
+		lines = sc_history_size;	/* use the default value */
 
 	/* make it at least as large as the screen size */
 	lines = imax(lines, scp->ysize);
@@ -111,13 +132,13 @@
 	delta = 0;
 	if (prev_history) {
 		cur_lines = sc_vtb_rows(history);
-		min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
+		min_lines = imax(sc_history_size, prev_ysize);
 		if (cur_lines > min_lines)
 			delta = cur_lines - min_lines;
 	}
 
 	/* lines upto min_lines are always allowed. */
-	min_lines = imax(SC_HISTORY_SIZE, scp->ysize);
+	min_lines = imax(sc_history_size, scp->ysize);
 	if (lines > min_lines) {
 		if (lines - min_lines > extra_history_size + delta) {
 			/* too many lines are requested */
@@ -196,7 +217,7 @@
 		return;
 
 	cur_lines = sc_vtb_rows(history);
-	min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
+	min_lines = imax(sc_history_size, prev_ysize);
 	extra_history_size += (cur_lines > min_lines) ? 
 				  cur_lines - min_lines : 0;
 
--- share/man/man4/syscons.4.orig	2010-03-10 11:38:03.828597000 +0200
+++ share/man/man4/syscons.4	2010-07-04 18:41:03.496277934 +0300
@@ -442,6 +442,20 @@
 for a keyboard device if it is not currently attached to one.
 Otherwise, the driver only probes for a keyboard once during bootup.
 .El
+.Ss Loader Tunables
+Tunables can be set at the
+.Xr loader 8
+prompt before booting the kernel or stored in
+.Pa /boot/loader.conf .
+Some of these tunables also have a matching
+.Xr sysctl 8
+entry for access after boot.
+.Bl -tag -width indent
+.It hw.syscons.history_size
+Override the size of back scroll buffer (specified by the
+.Dv SC_HISTORY_SIZE
+option).
+.El
 .Sh FILES
 .Bl -tag -width /usr/share/syscons/xxxxyyyyzzz -compact
 .It Pa /dev/console


>Release-Note:
>Audit-Trail:

From: Dmitry Pryanishnikov <lynx.ripe@gmail.com>
To: bug-followup@FreeBSD.org, lynx.ripe@gmail.com
Cc:  
Subject: Re: kern/148367: [syscons] [patch] Add loader tunable to override
 SC_HISTORY_SIZE
Date: Wed, 15 Dec 2010 23:39:21 +0200

 This is a multi-part message in MIME format.
 --------------000808020106000008070103
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
 Content-Transfer-Encoding: 7bit
 
 
 Hello!
 
    The patch has been updated for schistory.c,v 1.21.2.2 (current version in 
 RELENG_8).
 
 Sincerely, Dmitry
 -- 
 nic-hdl: LYNX-RIPE
 
 --------------000808020106000008070103
 Content-Type: text/plain;
  name="syscons-history_size.patch"
 Content-Transfer-Encoding: 7bit
 Content-Disposition: attachment;
  filename="syscons-history_size.patch"
 
 --- sys/dev/syscons/syscons.h.orig	2010-08-23 14:33:35.837815000 +0300
 +++ sys/dev/syscons/syscons.h	2010-12-15 23:16:15.492041513 +0200
 @@ -573,6 +573,7 @@
  
  /* schistory.c */
  #ifndef SC_NO_HISTORY
 +void		sc_init_history(void);
  int		sc_alloc_history_buffer(scr_stat *scp, int lines,
  					int prev_ysize, int wait);
  void		sc_free_history_buffer(scr_stat *scp, int prev_ysize);
 --- sys/dev/syscons/syscons.c.orig	2010-08-23 14:33:35.827279000 +0300
 +++ sys/dev/syscons/syscons.c	2010-12-15 23:16:15.494051860 +0200
 @@ -2726,8 +2726,12 @@
      int i;
  
      /* one time initialization */
 -    if (init_done == COLD)
 +    if (init_done == COLD) {
  	sc_get_bios_values(&bios_value);
 +#ifndef SC_NO_HISTORY
 +	sc_init_history();
 +#endif
 +    }
      init_done = WARM;
  
      /*
 --- sys/dev/syscons/schistory.c.orig	2010-12-15 22:44:32.506598000 +0200
 +++ sys/dev/syscons/schistory.c	2010-12-15 23:16:15.494051860 +0200
 @@ -41,6 +41,7 @@
  #include <sys/tty.h>
  #include <sys/kernel.h>
  #include <sys/malloc.h>
 +#include <sys/sysctl.h>
  
  #if defined(__sparc64__) || defined(__powerpc__)
  #include <machine/sc_machdep.h>
 @@ -77,17 +78,37 @@
  static int		extra_history_size
  				= SC_MAX_HISTORY_SIZE - SC_HISTORY_SIZE*MAXCONS;
  
 +static int		sc_history_size = SC_HISTORY_SIZE;
 +
 +SYSCTL_DECL(_hw_syscons);
 +SYSCTL_INT(_hw_syscons, OID_AUTO, history_size, CTLFLAG_RDTUN, &sc_history_size, 0,
 +    "Number of history buffer lines");
 +
  /* local functions */
  static void copy_history(sc_vtb_t *from, sc_vtb_t *to);
  static void history_to_screen(scr_stat *scp);
  
 +/* tune history buffer size */
 +void
 +sc_init_history(void)
 +{
 +
 +	TUNABLE_INT_FETCH("hw.syscons.history_size", &sc_history_size);
 +	if (sc_history_size < ROW * 4)
 +	    sc_history_size = ROW * 4;
 +	extra_history_size =
 +	    ((sc_history_size * MAXCONS * MAXSC) > SC_MAX_HISTORY_SIZE ?
 +		sc_history_size * MAXCONS * MAXSC : SC_MAX_HISTORY_SIZE) -
 +		    sc_history_size * MAXCONS;
 +}
 +
  /* allocate a history buffer */
  int
  sc_alloc_history_buffer(scr_stat *scp, int lines, int prev_ysize, int wait)
  {
  	/*
  	 * syscons unconditionally allocates buffers up to 
 -	 * SC_HISTORY_SIZE lines or scp->ysize lines, whichever 
 +	 * sc_history_size lines or scp->ysize lines, whichever 
  	 * is larger. A value greater than that is allowed, 
  	 * subject to extra_history_size.
  	 */
 @@ -98,7 +119,7 @@
  	int delta;				/* lines to put back */
  
  	if (lines <= 0)
 -		lines = SC_HISTORY_SIZE;	/* use the default value */
 +		lines = sc_history_size;	/* use the default value */
  
  	/* make it at least as large as the screen size */
  	lines = imax(lines, scp->ysize);
 @@ -111,13 +132,13 @@
  	delta = 0;
  	if (prev_history) {
  		cur_lines = sc_vtb_rows(history);
 -		min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
 +		min_lines = imax(sc_history_size, prev_ysize);
  		if (cur_lines > min_lines)
  			delta = cur_lines - min_lines;
  	}
  
  	/* lines up to min_lines are always allowed. */
 -	min_lines = imax(SC_HISTORY_SIZE, scp->ysize);
 +	min_lines = imax(sc_history_size, scp->ysize);
  	if (lines > min_lines) {
  		if (lines - min_lines > extra_history_size + delta) {
  			/* too many lines are requested */
 @@ -196,7 +217,7 @@
  		return;
  
  	cur_lines = sc_vtb_rows(history);
 -	min_lines = imax(SC_HISTORY_SIZE, prev_ysize);
 +	min_lines = imax(sc_history_size, prev_ysize);
  	extra_history_size += (cur_lines > min_lines) ? 
  				  cur_lines - min_lines : 0;
  
 --- share/man/man4/syscons.4.orig	2010-03-02 03:56:55.000000000 +0200
 +++ share/man/man4/syscons.4	2010-12-15 23:16:15.495058710 +0200
 @@ -442,6 +442,20 @@
  for a keyboard device if it is not currently attached to one.
  Otherwise, the driver only probes for a keyboard once during bootup.
  .El
 +.Ss Loader Tunables
 +Tunables can be set at the
 +.Xr loader 8
 +prompt before booting the kernel or stored in
 +.Pa /boot/loader.conf .
 +Some of these tunables also have a matching
 +.Xr sysctl 8
 +entry for access after boot.
 +.Bl -tag -width indent
 +.It hw.syscons.history_size
 +Override the size of back scroll buffer (specified by the
 +.Dv SC_HISTORY_SIZE
 +option).
 +.El
  .Sh FILES
  .Bl -tag -width /usr/share/syscons/xxxxyyyyzzz -compact
  .It Pa /dev/console
 
 --------------000808020106000008070103--
>Unformatted:
