[erlang-questions] A async c driver issues: The emulator will be halted during the execution of c driver code.

Willem de Jong w.a.de.jong@REDACTED
Wed Apr 2 21:34:46 CEST 2008


Hi,

To illustrate Kenneths point an experience I had today:

I wanted to answer Ahmeds question (recently on the list) about the use of
the new version of erlsom in the yaws soap library. It turned out that this
would not use the new mode of erlsom that works directly on binaries. I
wondered what the performance impact would be.

I did a little test: how many soap requests could yaws handle on my PC (a
simple laptop running windows XP, with the client and the server running on
the same machine)? The answer disappointed me: around 25.

Now I wondered how much of this would be due to the XML encoding, and would
working directly on binaries make a difference. I tried how often Erlang
could encode and decode a message in 1 second. Answer: 3500 times.

Conclusion: erlsom in binary mode or in the 'old' list mode, xmerl, or a C
driver: it would not make any difference in this case, as far as performance
is concerned.

All the same, if you decide to continue your attempt to use a C driver, I
would be very interested to know what the result is in terms of speed
compared to erlsom or xmerl. It would be great if you could let me know.

Regards,
Willem

2008/4/2 Kenneth Lundin <kenneth.lundin@REDACTED>:

> In the documentation you can read how to write an asynchronous driver.
> You find it here http://www.erlang.org/doc/apps/erts/driver.html#6.5.
> It's part of the ERTS users guide.
>
> It is a well known fact that drivers (the most common synchronous ones)
> block
> the VM during their execution. You must be very careful to secure that you
> don't
> execute for too long in those drivers.
>
> Are you really sure that the speed for parsing this in Erlang is not
> enough?
> You can even use native code generation with HiPE. By coding this in
> Erlang you
> avoid the problems you have with the driver.
>
> /Kenneth
>
> On 4/2/08, tate.zhou.cn <tate.zhou.cn@REDACTED> wrote:
> >
> > Hi , guys:
> >
> >
> > I worte a c driver for parsing  xml pieces. The driver works , but when
> the
> > c driver invoked , the erlang emulator will be halted until the
>  execution
> > of  c driver  completed.  during the execution of the c driver code,
> other
> > process of erlang get no chance to be execute , evenif the usetage of
> cpu is
> > around 50% (My cpu is duo core 2 ).
> >
> > Does anybody have any idea about this issues?
> >
> > Any infomation about how to write a async driver is appreciate.
> >
> >
> > List below is pieces of  my erl code and c dirver code:
> >
> > The execution of ?INFO_MSG in test_async_normal will start  until
> test_async
> > complete, although the execution time of  test_async  is more than 2
> > seconds.
> >
> >
> >
> >
> >
> >
> > erlang:
> > ==========================================
> >
> > test_async_newpid() ->
> >
> >
> >     PID = spawn(?MODULE, test_async_normal,[""]),
> >
> >     PID2 = spawn(?MODULE, test_async,[""]),
> >
> >     ?INFO_MSG("New Pid for port communication:",[PID]),
> >
> >
> >     ok.
> >
> >
> > test_async_normal(Data) ->
> >
> >
> >     timer:sleep(2* 1000),
> >     ?INFO_MSG("Hi , I am in new Pid:",[erlang:self()]),
> >
> >     ok.
> >
> > test_async (Data) ->
> >
> >     Port = open_port({spawn, vbp_xmlhandler}, [binary]),
> >
> >     Bin =  erlang:list_to_binary("hello , i come from erlang: "),
> > %load_xml_bin(),
> >
> >
> >     Port ! {self(), {command, Bin}},
> >
> >     Ret_str = return_port_data(Port),
> >
> >
> >     ?INFO_MSG("Return from port : ~p~n",[Ret_str]),
> >
> >     % ?INFO_MSG("Return from port : ~s",[Res]),
> >
> >     port_close(Port),
> >
> >
> >
> > ok.
> >
> >
> >
> > return_port_data(Port) ->
> >     receive
> >  {Port, {data, Data}} ->
> >      Data
> >     end.
> >
> >
> > ====================================================
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > C driver code :
> >
> > -----------------------------------------------------
> > static ErlDrvEntry vbp_xmlhandler_entry = {
> >     NULL,   /* init */
> >     vbp_xmlhandler_start,
> >     vbp_xmlhandler_stop,
> >     vbp_xmlhandler_async,   /* output */
> >     NULL,   /* ready_input */
> >     NULL,   /* ready_output */
> >     "vbp_xmlhandler",                 /* the name of the
> > driver */
> >     NULL,   /* finish */
> >     NULL,   /* handle */
> >     NULL,
> >     NULL,   /* timeout */
> >     NULL,   /* outputv */
> >     NULL,   /* ready_async */
> >     NULL,   /* flush */
> >     NULL,   /* call */
> >     NULL   /* event */
> > };
> > DRIVER_INIT(vbp_xmlhandler) /* must match name in driver_entry */
> > {
> >     return &vbp_xmlhandler_entry;
> > }
> >
> >
> >
> > /*
> > //   async interface for handle command.
> > //
> > */
> > static void vbp_xmlhandler_async(ErlDrvData handle, char *buff, int
> > bufflen){
> >  iostream* stream;
> >     char* test_data;
> >  char *in_data;
> >  int len;
> >  int i ;
> >  int loop_count;
> >  expat_data* d = (expat_data*)handle;
> >
> >  len = bufflen;
> >
> >
> >  stream = create_stream();
> >
> >  // force a lot of cpu time to be consumed in current invocation.
> >  loop_count = 1000000 * 80;
> >  for(i = 0;i <loop_count ;i++ ){
> >   in_data = malloc(20);
> >
> >   free(in_data);
> >  }
> >  in_data = malloc(len + 1);
> >  memset(in_data,0,len +1);
> >  memcpy(in_data,buff,len);
> >  write_str_to_stream(stream,in_data);
> >  free(in_data);
> >
> >  write_str_to_stream(stream," [append by c driver]");
> >
> >  driver_output(d ->port, stream->buff, stream->index);
> >
> >  free_stream(stream);
> > }
> >
> >
> >
> >
> >
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://www.erlang.org/mailman/listinfo/erlang-questions
> >
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080402/62c1ede1/attachment.htm>


More information about the erlang-questions mailing list