Version française
Home     About     Download     Resources     Contact us    
Browse thread
pthread_getcpuclockid et al
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Sam Steingold <sds@p...>
Subject: pthread_getcpuclockid et al
How do I time an individual Ocaml thread?

There appears to be no interface to pthread_getcpuclockid() and
clock_gettime() and it is not obvious from
ocaml/otherlibs/systhreads/posix.c how to get the pthread_t of a given
Ocaml Thread.t object (caml_thread_descr does not point to a
caml_thread_struct).

here is what I have so far:
=========================== pth_clock.mli
type clock
external getcpuclock: Thread.t -> clock = "getcpuclock"
=========================== pth_clock.mli
=========================== pth_clock.ml
external getcpuclock : Thread.t -> clock = "getcpuclock"
=========================== pth_clock.ml
=========================== pth_clock.c
#include <pthread.h>
#include <time.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/fail.h>
#include <caml/alloc.h>
#include <caml/custom.h>

/* see /usr/local/home/godi/godi/lib/godi/saved-src/ocaml.tar.gz
   ocaml/otherlibs/systhreads/posix.c */
extern pthread_t tid_to_pth (value tid);/* how do I define this??? */

/* Encapsulation of clockid_t as Caml custom blocks. */
#define clock_val(v) (*((clockid_t **) Data_custom_val(v)))

void clock_finalize (value v) { free(clock_val(v)); }

static struct custom_operations clockid_ops = {
  "caml.clockid_t",
  &clock_finalize,
  custom_compare_default,
  custom_hash_default,
  custom_serialize_default,
  custom_deserialize_default
};

/* Allocating a Caml custom block to hold the given clockid_t * */
static value alloc_clock (clockid_t * c) {
  value v = alloc_custom(&clockid_ops, sizeof(clockid_t *), 0, 1);
  clock_val(v) = c;
  return v;
}

CAMLprim value getcpuclock (value tid) {
  CAMLparam1(tid);
  clockid_t *c = malloc(sizeof(clockid_t));
  if (pthread_getcpuclockid(tid_to_pth(tid),c) == 0)
    CAMLreturn(alloc_clock(c));
  else {
    free(c);
    caml_failwith("pthread_getcpuclockid");
  }
}
=========================== pth_clock.c

Help?

Thanks.

-- 
Sam Steingold (http://www.podval.org/~sds) on Fedora Core release 5 (Bordeaux)
http://pmw.org.il http://openvotingconsortium.org http://dhimmi.com
http://thereligionofpeace.com http://palestinefacts.org http://ffii.org
Why do we want intelligent terminals when there are so many stupid users?