Skip to content

Commit de48078

Browse files
committed
native modules working
1 parent b956efa commit de48078

File tree

9 files changed

+112
-16
lines changed

9 files changed

+112
-16
lines changed

contrib/ebench.erl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
% erlc *.erl && erl ebench.beam -run ebench start 10000 20
2+
3+
-module(ebench).
4+
-export([start/1]).
5+
6+
start([Ni, Ci]) ->
7+
Nt = list_to_integer(Ni),
8+
C = list_to_integer(Ci),
9+
N = round(Nt / C),
10+
T0 = erlang:now(),
11+
Waiter = spawn(fun() -> wait(T0, N * C) end),
12+
spawner(Waiter, N, C).
13+
14+
spawner(_Waiter, _N, 0) ->
15+
ok;
16+
spawner(Waiter, N, C) ->
17+
spawn(fun() -> loop(Waiter, N) end),
18+
spawner(Waiter, N, C - 1).
19+
20+
% X is the total number of responses to wait for
21+
wait(T0, XTotal, 0) ->
22+
T1 = erlang:now(),
23+
Diff = timer:now_diff(T1, T0),
24+
Mean = Diff / XTotal,
25+
io:format("~p requests completed in ~.2fs~n", [XTotal, Diff / 1000000]),
26+
io:format("Mean request time: ~.2fms (~.2f r/s)~n", [Mean / 1000, XTotal / (Diff / 1000000)]),
27+
init:stop();
28+
wait(T0, XTotal, X) ->
29+
receive
30+
done -> wait(T0, XTotal, X - 1)
31+
end.
32+
33+
wait(T0, X) ->
34+
wait(T0, X, X).
35+
36+
loop(_Waiter, 0) ->
37+
ok;
38+
loop(Waiter, N) ->
39+
hit(Waiter),
40+
loop(Waiter, N - 1).
41+
42+
hit(Waiter) ->
43+
% io:format("outgoing!~n", []),
44+
Host = "localhost",
45+
{ok, Sock} = gen_tcp:connect(Host, 8000, [binary, {packet, 4}]),
46+
Request = term_to_binary({call, nat, add, [1, 2]}),
47+
ok = gen_tcp:send(Sock, Request),
48+
receive
49+
{tcp, _Port, Reply} ->
50+
% io:format("~p~n", [Reply]),
51+
{reply, 3} = binary_to_term(Reply),
52+
Waiter ! done,
53+
ok;
54+
Any ->
55+
io:format("Unexpected message: ~p~n", [Any]),
56+
Waiter ! done,
57+
ok
58+
end,
59+
ok = gen_tcp:close(Sock).

elib/config.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ load_single(Config) ->
1111
native ->
1212
verify(native, Config),
1313
CodePaths = proplists:get_value(codepaths, Config),
14-
lists:each(fun(X) -> code:add_patha(X) end, CodePaths),
15-
Config;
14+
lists:map((fun code:add_patha/1), CodePaths),
15+
[{id, native} | Config];
1616
extern ->
1717
verify(extern, Config),
1818
Handler = proplists:get_value(command, Config),
1919
Number = proplists:get_value(count, Config),
2020
{ok, SupPid} = asset_pool_sup:start_link(Handler, Number),
2121
[{_Id, ChildPid, _Type, _Modules}] = supervisor:which_children(SupPid),
22-
[{pid, ChildPid} | Config]
22+
[{id, ChildPid} | Config]
2323
end.
2424

2525
verify(native, _Config) ->

elib/ernie_server.erl

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ code_change(_OldVersion, State, _Extra) -> {ok, State}.
9797
%%====================================================================
9898

9999
extract_mapping(Config) ->
100-
Pid = proplists:get_value(pid, Config),
100+
Id = proplists:get_value(id, Config),
101101
Mods = proplists:get_value(modules, Config),
102-
lists:map(fun(X) -> {X, Pid} end, Mods).
102+
lists:map(fun(X) -> {X, Id} end, Mods).
103103

104104
init_map(Configs) ->
105105
lists:foldl(fun(X, Acc) -> Acc ++ extract_mapping(X) end, [], Configs).
@@ -173,12 +173,17 @@ process_request(Request, State) ->
173173
{_Type, Mod, _Fun, _Args} = ActionTerm,
174174
Pid = proplists:get_value(Mod, State#state.map),
175175
case Pid of
176-
undefined ->
176+
native ->
177177
logger:debug("Dispatching to native module~n", []),
178-
process_native_request(Request, State);
179-
ValidPid ->
178+
process_native_request(ActionTerm, Request, State);
179+
ValidPid when is_pid(ValidPid) ->
180180
logger:debug("Found extern pid ~p~n", [ValidPid]),
181-
process_extern_request(ValidPid, Request, State)
181+
process_extern_request(ValidPid, Request, State);
182+
undefined ->
183+
Sock = Request#request.sock,
184+
gen_tcp:send(Sock, term_to_binary({error})),
185+
ok = gen_tcp:close(Sock),
186+
State
182187
end.
183188

184189
close_if_cast(ActionTerm, Request) ->
@@ -192,8 +197,12 @@ close_if_cast(ActionTerm, Request) ->
192197
ok
193198
end.
194199

195-
process_native_request(_Request, State) ->
196-
State.
200+
process_native_request(ActionTerm, Request, State) ->
201+
Count = State#state.count,
202+
State2 = State#state{count = Count + 1},
203+
logger:debug("Count = ~p~n", [Count + 1]),
204+
spawn(fun() -> native:process(ActionTerm, Request) end),
205+
State2.
197206

198207
process_extern_request(Pid, Request, State) ->
199208
case queue:is_empty(State#state.pending) of

elib/native.erl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-module(native).
2+
-export([process/2]).
3+
4+
-record(request, {sock = undefined,
5+
info = undefined,
6+
action = undefined}).
7+
8+
process(ActionTerm, Request) ->
9+
{_Type, Mod, Fun, Args} = ActionTerm,
10+
Sock = Request#request.sock,
11+
logger:debug("Calling ~p:~p(~p)~n", [Mod, Fun, Args]),
12+
Result = apply(Mod, Fun, Args),
13+
logger:debug("Result was ~p~n", [Result]),
14+
Data = term_to_binary({reply, Result}),
15+
gen_tcp:send(Sock, Data),
16+
ok = gen_tcp:close(Sock).

examples/dsl.cfg

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/dsl.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
require 'ernie'
33

44
# logfile('/tmp/ernie.log')
5-
# loglevel(Logger::INFO)
5+
loglevel(Logger::FATAL)
66

7-
mod(:test) do
7+
mod(:dsl) do
88
# Add two numbers together
99
fun(:add) do |a, b|
1010
a + b

examples/example.cfg

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[{modules, [dsl]},
2+
{type, extern},
3+
{command, "ruby examples/dsl.rb"},
4+
{count, 2}].
5+
6+
[{modules, [nat]},
7+
{type, native},
8+
{codepaths, ["examples"]}].

examples/expose.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
22
require 'ernie'
33

4-
module Test
4+
module Expose
55
# Add two numbers together
66
def add(a, b)
77
a + b
@@ -24,4 +24,4 @@ def error
2424
end
2525
end
2626

27-
Ernie.expose(:test, Test)
27+
Ernie.expose(:expose, Expose)

examples/nat.erl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-module(nat).
2+
-export([add/2]).
3+
4+
add(A, B) ->
5+
A + B.

0 commit comments

Comments
 (0)