[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [MirageOS-devel] Mirage tracing



On 7 Oct 2014, at 16:09, Thomas Leonard <talex5@xxxxxxxxx> wrote:

> By default, threads just appear as numbers, but I added some code to
> my unikernel to label some them (I didn't annotate any of the Mirage
> libraries, though). For example, the "B.read" thread is what the
> unikernel gets back when it calls B.read on the block device, but you
> can see that behind this mirage-block-xen is creating other threads.

It is possible to associate per-Lwt-thread values, which can be used
to give threads names: https://gist.github.com/avsm/0fb00c14978a216b7ddb

(* Build with:
    ocamlbuild -use-ocamlfind -pkg lwt.unix -tag annot test.native
 *)

let id_key = Lwt.new_key ()

let log fmt =
  let tid =
    match Lwt.get id_key with
    | None -> "<unknown>"
    | Some id -> id
  in
  Printf.(kprintf (fun (msg:string) -> printf "[%s] %s\n%!" tid msg) fmt)

let in_thread ?id fn =
  Lwt.with_value id_key id fn

let () = Lwt_unix.run (
  let open Lwt in
  log "main function starts";
  in_thread ~id:"thread 1" (fun () -> log "hello");
  in_thread ~id:"thread 1" (fun () -> log "world");
  let rec looper n () =
    match n with
    | 0 -> return_unit
    | n ->
       log "looper %d" n;
       Lwt_unix.sleep (Random.float 1.0)
       >>= looper (n-1)
  in
  join [ 
    in_thread ~id:"foo" (looper 10);
    in_thread ~id:"bar" (looper 5);
    looper 7 ()
  ]
)

We could make this slightly more efficient by using symbolic
variants instead of strings (so that they would compile down to be
integers), but the basic idea would be the same.

-anil


_______________________________________________
MirageOS-devel mailing list
MirageOS-devel@xxxxxxxxxxxxxxxxxxxx
http://lists.xenproject.org/cgi-bin/mailman/listinfo/mirageos-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.