Implementing Speed Counters in GDC

4 steps to timing just how long it takes for GDC to parse and generate code.

Step 1:

Add the timevar header and object into the GDC build.

Include timevar.h in gcc/d/d-gcc-includes.h

#include "ggc.h"
#include "opts.h"
#include "tm_p.h"
 /* Added timevar.h */
#include "timevar.h"

Append timevar.o to the borrowed C objects in gcc/d/Make-lang.in

D_BORROWED_C_OBJS += timevar.o

Step 2:

Define your own counters

Put your defined timevar macros in gcc/timevar.def
Here’s what mine look like:

/* GDC-Specific */
DEFTIMEVAR (TV_D_READ        , "Module::read")
DEFTIMEVAR (TV_D_PARSE       , "Module::parse")
DEFTIMEVAR (TV_D_IMPORTALL   , "Module::importAll")
DEFTIMEVAR (TV_D_SEMANTIC    , "Module::semantic")
DEFTIMEVAR (TV_D_SEMANTIC2   , "Module::semantic2")
DEFTIMEVAR (TV_D_SEMANTIC3   , "Module::semantic3")
DEFTIMEVAR (TV_D_GENOBJFILE  , "Module::genobjfile")

Step 3:

Add the counters to GDC

Add your push and pop functions anywhere in the code. timevar_push starts the timer, timevar_pop stops the timer.

As I’m interested in the top level only I’ve done mine in gcc/d/d-lang.cc
There’s a lot here, so a diff view should give you the general idea of it.

--- a/gcc/d/d-lang.cc        2010-09-17 19:02:38.580306157 +0100
+++ b/gcc/d/d-lang.cc     2010-09-11 21:24:34.059001743 +0100
@@ -1044,6 +1044,7 @@
     //global.params.verbose = 1;

     // Read files
+    timevar_push (TV_D_READ);
 #if ! V2
     aw = AsyncRead::create(modules.dim);
     for (i = 0; i read(0);
     }
 #endif
+    timevar_pop (TV_D_READ);
     // Parse files
+    timevar_push (TV_D_PARSE);
     for (i = 0; i < modules.dim; i++)
     {
        m = (Module *)modules.data[i];
@@ -1089,6 +1092,7 @@
            i--;
        }
     }
+    timevar_pop (TV_D_PARSE);
     if (global.errors)
        goto had_errors;

@@ -1116,6 +1120,7 @@

 #if ! V2
     // load all unconditional imports for better symbol resolving
+    timevar_push (TV_D_IMPORTALL);
     for (i = 0; i toChars());
        m->importAll(0);
     }
+    timevar_pop (TV_D_IMPORTALL);
     if (global.errors)
        goto had_errors;
 #endif

     // Do semantic analysis
+    timevar_push (TV_D_SEMANTIC);
     for (i = 0; i toChars());
        m->semantic();
     }
+    timevar_pop (TV_D_SEMANTIC);
     if (global.errors)
        goto had_errors;

@@ -1144,6 +1152,7 @@
 #endif

     // Do pass 2 semantic analysis
+    timevar_push (TV_D_SEMANTIC2);
     for (i = 0; i toChars());
        m->semantic2();
     }
+    timevar_pop (TV_D_SEMANTIC2);
     if (global.errors)
        goto had_errors;

     // Do pass 3 semantic analysis
+    timevar_push (TV_D_SEMANTIC3);
     for (i = 0; i toChars());
        m->semantic3();
     }
+    timevar_pop (TV_D_SEMANTIC3);
     if (global.errors)
        goto had_errors;

@@ -1233,6 +1245,7 @@
        json_generate(&modules);
 #endif

+    timevar_push (TV_D_GENOBJFILE);
     for (i = 0; i gendocfile();
        }
     }
+    timevar_pop (TV_D_GENOBJFILE);

     // better to use input_location.xxx ?
     (*debug_hooks->end_source_file) (input_line);

Step 4:

Build GDC

make

Then compile your project with the DFLAG -ftime-report and post your results!

Here’s one I made earlier ( that’s not very tidy! :-)
D2 Druntime/Phobos time report

How to Embed Perl in your D program

This post takes influence, and reference from perldoc’s perlembed page.

Do you want to:

Use D from Perl?

Have a look at the Perl Inline API and write your own XS module. :)

Use a Unix program from Perl?

Read about back-quotes and about “system” and “exec” in perlfunc.

Use Perl from Perl?

Read about “do”, “eval”, “require” and “use” in perlfunc.

Use D from D?

Read about “mixin” in the D Language Reference for compile-time interpretation.
If you require run-time interpretation, rethink your design.

Use Perl from D?

Read on…

The D Perl Class Routine

This is made possible because Perl itself is written in C, the Perl library is the collection of compiled C programs, and D is binary compatible with C.

I’ll demonstrate this by showing a bastardized, non-portable version of miniperlmain.c containing the essentials of embedding. But re-written in D using a Class wrapper.

/*
 
  Perl/Interpreter.d
 
  Copyright (C) 2010 by Iain Buclaw
 
  You may distribute under the terms of either the GNU General Public
  License or the Artistic License. 
 
 */

module Perl.Interpreter;

/* For alloca, malloc, free */
private import std.c.stdlib;
 
/* flags in PL_exit_flags for nature of exit() */
enum
{
    EXIT_EXPECTED = 0x01,
    EXIT_DESTRUCT_END = 0x02, /* Run END in perl_destruct */
}

/* flags for Perl_call_* routines */
enum
{
    G_SCALAR = 0,
    G_ARRAY = 1,
    G_VOID = 128,  /* skip this bit when adding flags below */
    G_WANT = (128|1),
    G_DISCARD = 2,
    G_EVAL = 4,  /* Assume eval {} around subroutine call. */
    G_NOARGS = 8,  /* Don't construct a @_ array. */
    G_KEEPERR = 16,  /* Append errors to $@, don't overwrite it */
    G_NODEBUG = 32,  /* Disable debugging at toplevel.  */
    G_METHOD = 64,  /* Calling method. */
    G_FAKINGEVAL = 256,  /* Faking an eval context for call_sv or fold_constants */
}

extern (C)
{
    /* Perl Defines */
    struct interpreter {
        char broiled;
    }

    alias interpreter PerlInterpreter;
    alias void function(PerlInterpreter*) XSINIT_t;
    ubyte PL_exit_flags;

    /* Perl Prototypes */
    int Perl_call_argv(PerlInterpreter*, char *subname, int flags, char **argv);
    int perl_destruct(PerlInterpreter*);
    int perl_parse(PerlInterpreter*, XSINIT_t xsinit, int argc, char** argv, char** env);
    int perl_run(PerlInterpreter*);
    void perl_construct(PerlInterpreter*);
    void perl_free(PerlInterpreter*);
    void Perl_sys_init3(int* argc, char*** argv, char*** env);
    void Perl_sys_term();
    PerlInterpreter* perl_alloc();
 
    /* Unix System Environment */
    extern __gshared char** environ;
}


/* Copy a char[][] to a char** */
private void buildStringA(char[][] a, char** ap)
{
    foreach (s; a)
        *ap++ = (s ~ '\0').ptr;
    *ap = null;
}

/* Copy a char[] into a char* */
private void buildString(char[] a, char* ap)
{
    foreach (c; a)
        *ap++ = c;
    *ap = '\0';
}


/*** The Perl Interpreter Class ***/
class perlInterpreter
{
    int argc;
    char** argv;
    PerlInterpreter* my_perl;
    int parsestatus = 1;
    int exitstatus;

    this(ref char[][] args)
    {
        auto _argv = cast (char**)malloc ((char*).sizeof * (1 + args.length));
        buildStringA (args, _argv);
        argv = _argv;
        argc = cast (int)args.length;
    }

    this(int _argc, char** _argv)
    {
        argc = _argc;
        argv = _argv;
    }

    ~this()
    {
        if (argv)
            free (argv);
    }

    void Construct()
    {
        if (!my_perl)
        {
            Perl_sys_init3 (&argc, &argv, &environ);
            my_perl = perl_alloc();
            perl_construct (my_perl);
        }
    }
 
    void ExitFlags(uint flag)
    {
        PL_exit_flags |= flag;
    }
 
    void Parse()
    {
        if (my_perl)
            parsestatus = perl_parse (my_perl, null, argc, argv, null);
    }

    void Run()
    {
        if (my_perl && !parsestatus)
            exitstatus = perl_run (my_perl);
    }

    void CallSub(char[] subname, int flags)
    {
        auto sub = cast (char*)alloca ((char).sizeof * (1 + subname.length));
        buildString (subname, sub);

        if (my_perl && !parsestatus)
        {
            if (flags & G_DISCARD)
                Perl_call_argv (my_perl, sub, flags, argv);
            else
                exitstatus = Perl_call_argv (my_perl, sub, flags, argv);
        }
    }
 
    void Destruct()
    {
        if (my_perl)
        {
            perl_destruct (my_perl);
            perl_free (my_perl);
            Perl_sys_term();
        }
    }
}

And to build the library, simply use your D compiler of choice.

gdc -c -O2 -frelease Perl/Interpreter.d
ar rcs libperld.a Interpreter.o
ranlib libperld.a

Adding a Perl interpreter to your D program

And I’ll demonstrate how you can use the class in D.

import Perl.Interpreter;

int main(char[][] args)
{
    perlInterpreter myPerl = new perlInterpreter (args);
    myPerl.Construct();
    myPerl.ExitFlags(EXIT_DESTRUCT_END);
    myPerl.Parse();
    myPerl.Run();
    myPerl.Destruct();
    return myPerl.exitstatus;
}

And build it with the linker option ‘-lperl’ which requires libperl-dev to be installed on your system.

gdc -c -O2 -frelease dminiperl.d
gdc dminiperl.o -L. -lperld -lperl -o dminiperl

And after successful compilation, you’ll be able to use dminiperl just like Perl itself.

dminiperl myperlprog.pl

or

$ dminiperl
print "Pretty Good Perl \n";
print "10890 - 9801 is ", 10890 - 9801, "\n";

or

dminiperl -e 'printf("%x\n", 3735928559)'

Calling a Perl subroutine from your D program

To call individual Perl subroutines, Perl has four call_* functions to allow interface to. For this class though, I have only included call_argv() which takes a string argument.

import Perl.Interpreter;

void main(char[][] args)
{
    perlInterpreter myPerl = new perlInterpreter (args);
    myPerl.Construct();
    myPerl.ExitFlags(EXIT_DESTRUCT_END);
    myPerl.Parse();
    /* Skipping .Run() */
    myPerl.CallSub("showtime", G_DISCARD | G_NOARGS);
    myPerl.Destruct();
}

where showtime is a Perl subroutine that takes no arguments (that’s the G_NOARGS) and for which I’ll ignore the return value (that’s the G_DISCARD). Those flags, and others, are discussed in perlcall.

And to define the showtime subroutine in a file called showtime.pl:

print "I shan't be printed.";

sub showtime {
    print time,"\n";
}

And compile and run.

$ gdc -c -O2 -frelease showtime.d
$ gdc showtime.o -L. -lperld -lperl -o showtime
$ showtime showtime.pl
1277201943

yielding the number of seconds that elapsed between January 1, 1970 (the beginning of the Unix epoch), and the moment I began writing this sentence. :-)

Extending this D Class

For those who know perlembed well, this is just the icing of just what you can do. Sadly however, the more luxurious features, such as evaluating a Perl statement from your D program, require a little more complete D library, and a lot more time to write than I have to give in this particular essay.

If anyone wants to access any of the code / examples in this post from a source repository, or wish to collaborate in writing a complete Perl Library Interface for D. Just shout.

Moral?

You can sometimes write faster code in D, but you can always write code faster in Perl. Because you can use each from the other (with a little bit of work), combine them as you wish. :-)

Regards

What’s Bugging GDC?

Recently, I was asked the question:

Is gdc 4.3 really stable enough to be the default gdc?

As the gdc meta-package in Debian Squeeze and Ubuntu Lucid has been updated to now depend on gdc-4.3 instead of gdc-4.1. And by just playing around with D for a few days the kind fella who asked me that discovered two bugs. So it seems to him that gdc-4.3 is far from being as mature as gdc-4.1 …

I decided to look into this by running dstress against the most recent build. For those of you who don’t know, dstress is a testing suite for D to ensure that the compiler correctly compiles (or in some cases, must not compile) a collection of about 7000+ files with 32 different compiler flag combinations without dying unintentionally, ie: via assertion or a segmentation fault.

From that huge list, I got a total of 10 internal compiler errors produced.

Here is a beautified list of internal errors that I got from the report. This first list consists of GDC back-end errors only, as they will be specific to GDC. Unlike the front-end errors, which will most likely be present in DMD and possibly LDC too.

Update: Some good development has gone into GDC since writing this post, so I’m crossing off each issue as it gets resolved.

________________________________________________________________

Internal Compiler Error 1:

Code?

public struct Color
{
    double red   = 0;
    double green = 0;
    double blue  = 0;

    static Color opCall()
    {
        Color instance;
        return instance;
    }
    Color opAdd(real right){ return Color(); }
    void opAddAssign(real right){ *this = *this + right; }
}

Error?

opAddAssign_A.d: In member function ‘opAddAssign’:
opAddAssign_A.d:13: internal compiler error: in decl_ultimate_origin, at dwarf2out.c:5139

Reproducable?
Only via:

gdc -g -O3 -c opAddAssign_A.d
gdc -g -O2 -frelease -c opAddAssign_A.d
gdc -g -Os -frelease -c opAddAssign_A.d
gdmd -g -O -inline -c opAddAssign_A.d
gdmd -g -O -release -c opAddAssign_A.d

Variants?
Not known.

Workaround?
In all cases, include the compiler option “-fno-inline” to workaround the bug.

Bug seen in gdc-4.1?
No.

________________________________________________________________

Internal Compiler Error 2:

Code?

template thunk(alias fn)
{
    static void thunk(int* arg)
    {
        fn(arg);
    }
}

int main()
{
    int x = 1;
    void inner(int* arg)
    {
        if(!(arg !is &x))
            assert(0);
    }
    thunk!(inner)(&x);
    return 0;
}

Error?

bug_tree_inline_1902_B.d: In function ‘thunk’:
bug_tree_inline_1902_B.d:5: internal compiler error: in estimate_num_insns_1, at tree-inline.c:2494

Reproducable?
Always.

Variants?
Seen also in compiler error 3 and 4, but not known just how related they are.

Workaround?
Using “void thunk” instead of “static void thunk”.

Bug seen in gdc-4.1?
Yes.

________________________________________________________________

Internal Compiler Error 3:

Code?

int main()
{       
    void foo() {}       
    void function() bar = function void()
    {           
        foo();  
    };  
    return 0;
}

Error(s)?

bug_e2ir_299_A.d: In function ‘__funcliteral1’:
bug_e2ir_299_A.d:6: internal compiler error: in estimate_num_insns_1, at tree-inline.c:2494

Reproducable?
Always.

Variants?
Seen also in compiler error 2 and 4, but not known just how related they are.

Workaround?
Is apparently not supposed to be compilable. (comes under nocompile).

Bug seen in gdc-4.1?
Yes.

________________________________________________________________

Internal Compiler Error 4:

Code?

struct A (alias F)
{
    int f() { return F(); }
}

void main()
{
    int f() { return 0; }
    A!(f) a;
}

Error?

template_struct_01_A.d: In member function ‘f’:
template_struct_01_A.d:5: internal compiler error: in estimate_num_insns_1, at tree-inline.c:2494

Reproducable?
Always.

Variants?
Seen also in compiler error 2 and 3, but not known just how related they are.

Workaround?
None.

Bug seen in gdc-4.1?
Yes.

________________________________________________________________

Internal Compiler Error 5:

Code?

class Outer
{
    this()
    {
        Inner i = new Inner();
    }

    class Inner
    {
        Strct str;
    }
}

struct Strct
{
    int i;
}

int main()
{
    Outer o = new Outer();
    return 0;
}

Error?

forward_reference_17_A.d: In member function ‘_ctor’:
forward_reference_17_A.d:5: internal compiler error: Segmentation fault

Reproducable?

Always

Variants?
Not known.

Workaround?
Declare the struct before the class.

Bug seen in gdc-4.1?
Yes

________________________________________________________________

Internal Compiler Error 6:

Code?

bool nextis(void delegate() dgpositive = {})
{
    return true;
}

bool looping(lazy bool condition)
{
    return true;
}

int main()
{
    looping(nextis());
    return 0;
}

Error?

lazy_02_A.d: In member function ‘__dgliteral2’:
lazy_02_A.d:2: internal compiler error: in getFrameForSymbol, at d/d-codegen.cc:2541

Reproducable?
Always.

Variants?
Anything that uses the following as an argument.

void delegate() foo = {}

Workaround?
None.

Bug seen in gdc-4.1?
Yes.

________________________________________________________________

Internal Compiler Error 7:

Code?

int status;

template T(alias a)
{
    int dummy;
}

class C
{
    mixin T!(
            () {
                status++;
            }
        ) mixed;
}

int main()
{
    C c = new C();
    c.mixed.a();
    if(status != 1)
    {
        assert(0);
    }
    return 0;
}

Error?

mixin_23_A.d:11: Error: delegate mixin_23_A.C.__dgliteral1 literals cannot be class members
cc1d: internal compiler error: Segmentation fault

Reproducable?
Always.

Variants?
Also seen when using:

mixin T!( delegate() { status++; } ) mixed;

And:

mixin T!( { status++; } ) mixed;

Workaround?
Using function() works around the problem, but probably not correct due to semantic differences.

mixin T!( function(){ status++; } ) mixed;

Bug seen in gdc-4.1?
No.

________________________________________________________________

In this second list are DMD frontend compiler errors, they aren’t necessarily the fault of GDC’s, and they may well be present in LDC too.

Internal Compiler Error 8:

Code?

char[] testHelper(A ...)()
{
    char[] result;
    foreach(t; a)
    {
        result ~= "int " ~ t ~ ";\n";
    }
    return result;
}

int main()
{
    mixin( testHelper!( "hello", "world" )() );
    return 0;
}

Error?

cc1d: ../../src/gcc/d/dmd/template.c:811: MATCH TemplateDeclaration::deduceFunctionTemplateMatch(Loc, Objects*, Expression*, Expressions*, Objects*): Assertion `i dim' failed.
cc1d: internal compiler error: Aborted

Reproducable?
Always.

Variants?
Not known.

Workaround?
Code is not supposed to be compilable. (comes under nocompile).

Bug seen in gdc-4.1?
No.

________________________________________________________________

Internal Compiler Error 9:

Code?

void[10] array;

Error?

Error: integral constant must be scalar type, not void
cc1d: ../../src/gcc/d/dmd/todt.c:85: dt_t* createTsarrayDt(dt_t*, Type*): Assertion `tsa->size(0) % eoa_size == 0' failed.
cc1d: internal compiler error: Aborted

Reproducable?
Always.

Variants?
Not known.

Workaround?
Code is not supposed to be compilable. (comes under nocompile).

Bug seen in gdc-4.1?
Yes.

________________________________________________________________

Internal Compiler Error 10:

Code?

int main()
{
    auto arr = [4:"vier", 0:"null", 2:"zwei"];

    if(3 != arr.length)
    {
        assert(0);
    }

    char[][] master_e = ["vier", "null", "zwei"];
    int[] master_i = [4, 0, 2];

    foreach(index, element; arr)
    {
        bool found = false;
        foreach(pos, i; master_i)
        {
            if(index == i && element == master_e[pos])
            {
                master_i = master_i[0 .. pos] ~ master_i[pos+1 .. $];
                master_e = master_e[0 .. pos] ~ master_e[pos+1 .. $];
                found = true;
                break;
            }
        }
        if(!found)
        {
            assert(0);
        }
    }
    if(master_i.length)
    {
        assert(0);
    }
    return 0;
}

Error?

associative_array_23_B.d:3: Error: cannot infer type from this array initializer
cc1d: ../../src/gcc/d/dmd/init.c:437: virtual Expression* ArrayInitializer::toExpression(): Assertion `j < edim' failed.
cc1d: internal compiler error: Aborted

Reproducable?
Always.

Variants?
Not known.

Workaround?
None.

Bug seen in gdc-4.1?
No.

________________________________________________________________

So, as far as the dstress testing goes, there are currently 4 regressions – 2 of which are not the fault of gdc – and 6 inherited bugs in gdc-4.3 when compared to gdc-4.1.

So, back to your original question, is gdc 4.3 really stable enough to be the default gdc? Thanks to your help in identifying two underlying problems in the Debianised version, yes, I believe it is!

Despite some outstanding quirks, and the odd ICE or two in this post that would be cool to plumb down and fix. I think we are heading in for a strong release – if and when the GDC upstream developers ever get round to it. In the meantime, I will continue maintaining the Debian and Ubuntu packages, and will love to hear more of your dedicated support and patience in testing.

Got any other show-stopping bugs? Please report! :-)

Proof of Concept: Writing a Linux Kernel Module in D

I thought it best today to post this in a QA format just to ease you into it.

Alright, what narcotics are you on? Seriously…
I’d like to start this post with answering a few prejudiced questions before anyone asks. No, I’m not on narcotics. Yes, this can be done. No I don’t recommend it, and no, I’m not that crazy, but I do have quite a bit of time on my hands.

Why?
Try asking yourself, why not?

D is a horrible language. It’s made more horrible by the fact that a lot of substandard programmers use it, to the point where it’s much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do *nothing* but keep the D programmers out, that in itself would be a huge reason to use C.
I think you are confusing D with C++. ;-)

Everyone happy? OK, I’ll continue then.

You actually wrote a kernel module using only D?
OK. First I’d like to clarify, I am not using pure D here, but instead a utilising D’s capability of Interfacing to C. The glue itself is in C, and the interfacing code is wrapped in “extern (C)”, so the kernel can call the functions. But given the capability, you can mixin D with C.

OK, so you’re faking it. Somehow I’ve lost interest in this.
Suit yourself, bet you don’t want to see the code either.

Fine, so what does it look like?
Here is a hello world module.

hello.c:

#include  /* Needed by all modules */

dinterface.d:

extern (C):
int printk(char *s, ...);

int __gdc_personality_v0()
{
    // STUB: int __gdc_personality_v0() not implemented
    return 0;
}

int init_module()
{
    printk("Hello world!\n");
    return 0;
}

void cleanup_module()
{
    printk("Goodbye world!\n");
}

Makefile:

# D compiler
DC := gdc
# D objects
DOBJS := dinterface.o

ifneq ($(MAKE_KBUILD),)
# kbuild part of makefile

obj-$(CONFIG_HELLO) := hello.o
hello-y += $(DOBJS)

else
# normal part of makefile

KERNELDIR := /lib/modules/$(shell uname -r)/build

all: $(DOBJS)
	$(MAKE) -C $(KERNELDIR) M=$(shell pwd) CONFIG_HELLO=m MAKE_KBUILD=1 modules

clean:
	$(MAKE) -C $(KERNELDIR) M=$(shell pwd) MAKE_KBUILD=1 clean

%.o: %.d
	$(DC) -c $< -o $@

endif

Simply type

make

to build it, then to insert the module into the kernel

sudo insmod hello.ko

And you can see the hello world in ‘dmesg’

That simple eh? OK, what is the catch Iain…
The catch? In order to use D’s most favoured features, such as arrays, you will have to port D’s runtime, typeinfo and exception handling routines (notice that __gdc_personality_v0 is just a dummy function), as they won’t be carried over in the linking of the module.

Hmm… so have you implemented this?
As of writing, no, and I don’t intend to either. You can however look into Xomb for a minimal d runtime. But the feasibility of porting it is not really worth the hassle. Infact, more than likely you will never get it working full stop, and just end up segfaulting the kernel every time.

OK, so what is the moral of this post then?
I’m wide awake, it’s Saturday. :)

Fixing your HAL application for Ubuntu 10.04

Ubuntu Lucid Lynx sports full removal of HAL from the boot process, making Ubuntu faster to boot and faster to resume from suspend.

From a developer stance, all what has changed is that Ubuntu has switched over to D-BUS activation. So if a program tries to talk to HAL, but HAL is not running, it triggers it’s startup.

If this isn’t the case when you try to start your application, then the most likely reason is that it is too cautious and checks if HAL is running before trying to connect to it. This is true in the case of lshal, where you probably wouldn’t want HAL starting if it wasn’t already running.

Such an example of this check would be as seen below (taken from pcmanfm).

/* try to initialize the HAL context */
if (!libhal_ctx_init (hal_context, &error)
    goto failed;

What we do differently is simply dropping the check, letting the application connect to hal straight away, checking of course if that failed instead before returning true.

/* try to initialize the HAL context */
libhal_ctx_init (hal_context, &error);

Happy debugging!

Regards

Debian kFreeBSD in QEMU-KVM

Debian GNU/kFreeBSD is a port that consists of GNU userland using the GNU C library on top of FreeBSD‘s kernel, coupled with the regular Debian package set.

Installing

Assuming you don’t have KVM already installed, you’ll need to run:

sudo apt-get install kvm

Then check for the presence of /dev/kvm. If it doesn’t exist, check that virtualisation is turned on in BIOS, else your system does not have the CPU extensions required to use KVM, so will fall back to using qemu instead.

You can get the installation media either from here or grab the daily mini.iso at this location.

wget http://glibc-bsd.alioth.debian.org/install-cd/kfreebsd-i386/current/debian-20090729-kfreebsd-i386-install.iso

Create a hard drive image.

kvm-img create -f qcow kfreebsd.img 10G

And get started.

kvm -drive file=kfreebsd.img,index=0,cache=writeback,media=disk -cdrom debian-20090729-kfreebsd-i386-install.iso

Note: I use -drive instead of -hda because I find that it increases I/O performance of the guest system.

For installing Debian GNU/kFreeBSD and other useful information, refer to the install notes.

You have two ways of running through the installer, Express or Custom, I chose “Express”. This brings you to a command-line partitioner, simply press ‘a‘ to create a slice across the whole hard drive, and ‘q‘ to finish.

Next is the BootMgr, in this program, press ‘c‘ to create a ‘9500M‘ filesystem to mount on ‘/‘, followed by a the creation of a swap to fill the rest. Once done press ‘q‘ to finish.

Next, you’ll be asked what distribution you wish to install. I selected ‘minimal‘, and if you plan to do a CD installation, I suggest the same too. Else with a Network installation, you could get away with what you want.

In the next menu, select the medium you wish to install off, either choose Network or CDROM here, then wait around to let the extraction finish. Then you’ll need to press ‘Alt+F3‘ to proceed with the installation, answering any questions you may be asked.

Once finished, you’ll be prompted to answer ‘No’ to reboot. Don’t do this, instead answer ‘Yes‘ to return to the configuration menu, where you should set the Root Password first, then optionally configure anything else you may wish to. Once done, then exit the install and reboot into your new system.

Configuring for Network

Unfortunately, some of the defaults of the installation aren’t setup correctly for immediately usage.

First thing you’ll notice is no networking, so login to the root account and get that set.

nano /etc/network/interfaces

And put in the file

auto re0
iface re0 inet dhcp

Save, then restart networking

service networking restart

You can now update your system. Before you upgrade though, there is a nasty bug that prevents ‘rm’ working properly on directories on the freebsd 7.x kernels, so we’ll need to put the coreutils package on hold.

echo coreutils hold | dpkg --set-selections
apt-get update
apt-get dist-upgrade

Configuring for X

Install Xorg.

apt-get install xorg

Now to address some issues.

Firstly, if you haven’t already noticed, during the installation of xorg, the package ‘hal’ (as of writing) fails to install properly, leaving xorg in an unconfigured state.

Simply reboot the VM, login, and restart dbus.

service dbus restart

then run the installer again to finish the setup.

apt-get install -f

Secondly, xorg.conf needs to be setup correctly. To create a configuration file run:

X -configure

Allow about a 30-60 seconds for it to finish, then open the generated file for editing.

nano xorg.conf.new

Without going into too much detail, remove ‘Files’ and ‘Modules’ section, then edit the Sections outlined below as seen.

Section "InputDevice"
    Driver        "kbd"
[...]
Section "InputDevice"
    Driver        "mouse"
    Option        "Protocol" "SysMouse"
    Option        "Device" "/dev/sysmouse"
[...]
Section "Device"
    Driver        "vesa"
[...]

Note: If you have support for kvm, you can get away with having Driver “cirrus” instead of “vesa” in your config file. Am not sure of the benefits either or though.

Once done, install the file in it’s proper location.

mv xorg.conf.new /etc/X11/xorg.conf

Lastly, you need to create the mouse and keyboard policies for hal, as without them, will unusable under X.

First is the keyboard policy

nano /etc/hal/fdi/policy/10-x11-input-kbd.fdi

And put into the file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<deviceinfo version="0.2">
  <device>
    <match key="/org/freedesktop/Hal/devices/computer:system.kernel.name" string="GNU/kFreeBSD">
      <match key="info.capabilities" contains="input.keys">
        <merge key="input.x11_driver" type="string">kbd</merge>
      </match>
    </match>
  </device>
</deviceinfo>

Ditto for the mouse policy.

nano /etc/hal/fdi/policy/10-x11-input-mouse.fdi

And put into the file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<deviceinfo version="0.2">
  <device>
    <match key="/org/freedesktop/Hal/devices/computer:system.kernel.name" string="GNU/kFreeBSD">
      <match key="info.capabilities" contains="input.mouse">
        <merge key="input.x11_driver" type="string">mouse</merge>
      </match>
    </match>
  </device>
</deviceinfo>

Graphical Login

Finally, install your favourite Window-Manager and Display-Manager. I chose xfce and gdm.

apt-get install xfce gdm

And reboot once more to be greeted by a graphical login screen.

As you can see, there is still alot of work to go into the port, a lot of work to setup correctly, and even then it is not guarenteed to work everytime. :)

However, hope you find a place for this fun Debian port when you get it stable + working.

debian kfreebsd screenshot

Regards

D-BUS Errors

Since I had a difficult time figuring it out – I just thought I might post a “be all, end all” answer here.

There is a certain elusive DBus error that seems to occur in multiple applications, from Jockey to Bzr, to Ubuntu-One client.

The bottom of the traceback will look like this:

File "/var/lib/python-support/python2.5/dbus/bus.py", line 244, in get_object
follow_name_owner_changes=follow_name_owner_changes)
File "/var/lib/python-support/python2.5/dbus/proxies.py", line 241, in __init__
self._named_service = conn.activate_name_owner(bus_name)
File "/var/lib/python-support/python2.5/dbus/bus.py", line 183, in activate_name_owner
self.start_service_by_name(bus_name)
File "/var/lib/python-support/python2.5/dbus/bus.py", line 281, in start_service_by_name
'su', (bus_name, flags)))
File "/var/lib/python-support/python2.5/dbus/connection.py", line 607, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.Spawn.ChildExited: Launch helper exited with unknown return code 1

To put this into english – this essentially means that your system’s configuration for that particular piece of software has made it corrupt. The simple fix is simply removing the system configuration – or purging and reinstalling the application if you use a package manager.

ie:

sudo apt-get purge jockey-gtk jockey-common
sudo apt-get install jockey-gtk jockey-common

And I am 90% certain that doing this will retify your problem in most cases.

Follow

Get every new post delivered to your Inbox.