-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use wait3 to accurately time spawned processes
We fallback to the old method on win32. Signed-off-by: Rudi Grinberg <[email protected]>
- Loading branch information
Showing
8 changed files
with
170 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ | |
dune_filesystem_stubs) | ||
(foreign_stubs | ||
(language c) | ||
(names fcntl_stubs))) | ||
(names fcntl_stubs wait3_stubs))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
val restore_cwd_and_execve : string -> string list -> env:Env.t -> _ | ||
|
||
type resource_usage = | ||
{ utime : float | ||
; stime : float | ||
} | ||
|
||
val wait3 : Unix.wait_flag list -> int * Unix.process_status * resource_usage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <caml/mlvalues.h> | ||
|
||
#ifdef _WIN32 | ||
#include <caml/fail.h> | ||
|
||
void dune_wait3(value flags) { | ||
caml_failwith("wait3: not supported on windows"); | ||
} | ||
|
||
#else | ||
|
||
#include <caml/alloc.h> | ||
#include <caml/memory.h> | ||
#include <caml/signals.h> | ||
#include <caml/unixsupport.h> | ||
|
||
#include <sys/resource.h> | ||
#include <sys/time.h> | ||
#include <sys/types.h> | ||
#include <sys/wait.h> | ||
|
||
#define TAG_WEXITED 0 | ||
#define TAG_WSIGNALED 1 | ||
#define TAG_WSTOPPED 2 | ||
|
||
CAMLextern int caml_convert_signal_number(int); | ||
CAMLextern int caml_rev_convert_signal_number(int); | ||
|
||
static value alloc_process_status(int status) { | ||
value st; | ||
|
||
if (WIFEXITED(status)) { | ||
st = caml_alloc_small(1, TAG_WEXITED); | ||
Field(st, 0) = Val_int(WEXITSTATUS(status)); | ||
} else if (WIFSTOPPED(status)) { | ||
st = caml_alloc_small(1, TAG_WSTOPPED); | ||
Field(st, 0) = Val_int(caml_rev_convert_signal_number(WSTOPSIG(status))); | ||
} else { | ||
st = caml_alloc_small(1, TAG_WSIGNALED); | ||
Field(st, 0) = Val_int(caml_rev_convert_signal_number(WTERMSIG(status))); | ||
} | ||
return st; | ||
} | ||
|
||
static int wait_flag_table[] = {WNOHANG, WUNTRACED}; | ||
|
||
value dune_wait3(value flags) { | ||
CAMLparam1(flags); | ||
CAMLlocal2(times, res); | ||
|
||
int pid, status, cv_flags; | ||
cv_flags = caml_convert_flag_list(flags, wait_flag_table); | ||
|
||
struct rusage ru; | ||
|
||
caml_enter_blocking_section(); | ||
pid = wait3(&status, cv_flags, &ru); | ||
caml_leave_blocking_section(); | ||
if (pid == -1) | ||
uerror("wait3", Nothing); | ||
|
||
times = caml_alloc_small(2 * Double_wosize, Double_array_tag); | ||
Store_double_field(times, 0, ru.ru_utime.tv_sec + ru.ru_utime.tv_usec / 1e6); | ||
Store_double_field(times, 1, ru.ru_stime.tv_sec + ru.ru_stime.tv_usec / 1e6); | ||
|
||
res = caml_alloc_tuple(3); | ||
Store_field(res, 0, Val_int(pid)); | ||
Store_field(res, 1, alloc_process_status(status)); | ||
Store_field(res, 2, times); | ||
CAMLreturn(res); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,12 @@ | |
|
||
This captures the commands that are being run: | ||
|
||
$ <trace.json grep '"[be]"' | cut -c 2- | sed -E 's/:[0-9]+/:.../g' | ||
{"args":{"process_args":["-config"]},"ph":"b","id":...,"name":"ocamlc.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"ph":"e","id":...,"name":"ocamlc.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"args":{"process_args":["-modules","-impl","prog.ml"]},"ph":"b","id":...,"name":"ocamldep.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"ph":"e","id":...,"name":"ocamldep.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"args":{"process_args":["-w","@[email protected]@30..39@[email protected]@[email protected]","-strict-sequence","-strict-formats","-short-paths","-keep-locs","-g","-bin-annot","-I",".prog.eobjs/byte","-no-alias-deps","-opaque","-o",".prog.eobjs/byte/prog.cmo","-c","-impl","prog.ml"]},"ph":"b","id":...,"name":"ocamlc.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"ph":"e","id":...,"name":"ocamlc.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"args":{"process_args":["-w","@[email protected]@30..39@[email protected]@[email protected]","-strict-sequence","-strict-formats","-short-paths","-keep-locs","-g","-I",".prog.eobjs/byte","-I",".prog.eobjs/native","-intf-suffix",".ml","-no-alias-deps","-opaque","-o",".prog.eobjs/native/prog.cmx","-c","-impl","prog.ml"]},"ph":"b","id":...,"name":"ocamlopt.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"ph":"e","id":...,"name":"ocamlopt.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"args":{"process_args":["-w","@[email protected]@30..39@[email protected]@[email protected]","-strict-sequence","-strict-formats","-short-paths","-keep-locs","-g","-o","prog.exe",".prog.eobjs/native/prog.cmx"]},"ph":"b","id":...,"name":"ocamlopt.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"ph":"e","id":...,"name":"ocamlopt.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
$ <trace.json grep '"X"' | cut -c 2- | sed -E 's/:[0-9]+/:.../g' | ||
{"args":{"process_args":["-config"]},"ph":"X","dur":...,"name":"ocamlc.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"args":{"process_args":["-modules","-impl","prog.ml"]},"ph":"X","dur":...,"name":"ocamldep.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"args":{"process_args":["-w","@[email protected]@30..39@[email protected]@[email protected]","-strict-sequence","-strict-formats","-short-paths","-keep-locs","-g","-bin-annot","-I",".prog.eobjs/byte","-no-alias-deps","-opaque","-o",".prog.eobjs/byte/prog.cmo","-c","-impl","prog.ml"]},"ph":"X","dur":...,"name":"ocamlc.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"args":{"process_args":["-w","@[email protected]@30..39@[email protected]@[email protected]","-strict-sequence","-strict-formats","-short-paths","-keep-locs","-g","-I",".prog.eobjs/byte","-I",".prog.eobjs/native","-intf-suffix",".ml","-no-alias-deps","-opaque","-o",".prog.eobjs/native/prog.cmx","-c","-impl","prog.ml"]},"ph":"X","dur":...,"name":"ocamlopt.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
{"args":{"process_args":["-w","@[email protected]@30..39@[email protected]@[email protected]","-strict-sequence","-strict-formats","-short-paths","-keep-locs","-g","-o","prog.exe",".prog.eobjs/native/prog.cmx"]},"ph":"X","dur":...,"name":"ocamlopt.opt","cat":"process","ts":...,"pid":...,"tid":...} | ||
|
||
As well as data about the garbage collector: | ||
|
||
|