kconfig: implement KCONFIG_PROBABILITY for randconfig
authorYann E. MORIN <yann.morin.1998@free.fr>
Sat, 13 Apr 2013 15:18:36 +0000 (17:18 +0200)
committerChristian Lamparter <chunkeey@googlemail.com>
Fri, 31 May 2013 15:56:02 +0000 (17:56 +0200)
Currently the odds to set each symbol is (rounded):
    booleans:   y: 50%          n: 50%
    tristates:  y: 33%  m: 33%  n: 33%

Introduce a KCONFIG_PROBABILITY environment variable to tweak the
probabilities (in percentage), as such:
    KCONFIG_PROBABILITY     y:n split           y:m:n split
    -----------------------------------------------------------------
[1] unset or empty          50  : 50            33  : 33  : 34
[2] N                        N  : 100-N         N/2 : N/2 : 100-N
    N:M                     N+M : 100-(N+M)      N  :  M  : 100-(N+M)
    N:M:L                    N  : 100-N          M  :  L  : 100-(M+L)

[1] The current behaviour is kept as default, for backward compatibility
[2] The solution initially implemented by Peter for Buildroot, see:
    http://git.buildroot.org/buildroot/commit/?id=3435c1afb5

Signed-off-by: Peter Korsgaard <jacmet@uclibc.org>
[yann.morin.1998@free.fr: add to Documentation/]
Signed-off-by: "Yann E. MORIN" <yann.morin.1998@free.fr>
Signed-off-by: Christian Lamparter <chunkeey@googlemail.com>
config/confdata.c

index b0f134fa0f603c76372a6be9788137504af0c1ee..7976da1ce84e15ed28ad686b7fe85ba42c3c9558 100644 (file)
@@ -1170,7 +1170,51 @@ static void set_all_choice_values(struct symbol *csym)
 void conf_set_all_new_symbols(enum conf_def_mode mode)
 {
        struct symbol *sym, *csym;
-       int i, cnt;
+       int i, cnt, pby, pty, ptm;      /* pby: probability of boolean  = y
+                                        * pty: probability of tristate = y
+                                        * ptm: probability of tristate = m
+                                        */
+
+       pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
+                                  * below, otherwise gcc whines about
+                                  * -Wmaybe-uninitialized */
+       if (mode == def_random) {
+               int n, p[3];
+               char *env = getenv("KCONFIG_PROBABILITY");
+               n = 0;
+               while( env && *env ) {
+                       char *endp;
+                       int tmp = strtol( env, &endp, 10 );
+                       if( tmp >= 0 && tmp <= 100 ) {
+                               p[n++] = tmp;
+                       } else {
+                               errno = ERANGE;
+                               perror( "KCONFIG_PROBABILITY" );
+                               exit( 1 );
+                       }
+                       env = (*endp == ':') ? endp+1 : endp;
+                       if( n >=3 ) {
+                               break;
+                       }
+               }
+               switch( n ) {
+               case 1:
+                       pby = p[0]; ptm = pby/2; pty = pby-ptm;
+                       break;
+               case 2:
+                       pty = p[0]; ptm = p[1]; pby = pty + ptm;
+                       break;
+               case 3:
+                       pby = p[0]; pty = p[1]; ptm = p[2];
+                       break;
+               }
+
+               if( pty+ptm > 100 ) {
+                       errno = ERANGE;
+                       perror( "KCONFIG_PROBABILITY" );
+                       exit( 1 );
+               }
+       }
 
        for_all_symbols(i, sym) {
                if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
@@ -1189,8 +1233,15 @@ void conf_set_all_new_symbols(enum conf_def_mode mode)
                                sym->def[S_DEF_USER].tri = no;
                                break;
                        case def_random:
-                               cnt = sym->type == S_TRISTATE ? 3 : 2;
-                               sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
+                               sym->def[S_DEF_USER].tri = no;
+                               cnt = rand() % 100;
+                               if (sym->type == S_TRISTATE) {
+                                       if (cnt < pty)
+                                               sym->def[S_DEF_USER].tri = yes;
+                                       else if (cnt < (pty+ptm))
+                                               sym->def[S_DEF_USER].tri = mod;
+                               } else if (cnt < pby)
+                                       sym->def[S_DEF_USER].tri = yes;
                                break;
                        default:
                                continue;