Files
mercury/deep_profiler/conf.m
Zoltan Somogyi c02eb5163e Carve io.{call_system,environment}.m out of io.m.
library/io.call_system.m:
    Move the code in the "system access predicates" section of io.m
    to this new module.

library/io.environment.m:
    Move the predicates dealing with environment variables in io.m
    to this new module.

library/io.m:
    Delete the code moved to the new modules.

    Leave behind in io.m "forwarding predicates", predicates that do nothing
    except call the moved predicates in the new modules, to provide backward
    compatibility. But do mark the forwarding predicates as obsolete,
    to tell people to update their (at their leisure, since the obsoleteness
    warning can be turned off).

    Also leave behind in io.m the definitions of the types used
    by some parameters of some of the moved predicates.

library/MODULES_DOC:
    List the new modules among the documented modules.

library/library.m:
    List the new modules, including io.file.m (added in a previous change)
    among the documented standard library modules.

NEWS:
    Announce the changes.

browser/browse.m:
browser/interactive_query.m:
compiler/fact_table.m:
compiler/handle_options.m:
compiler/make.module_target.m:
compiler/mercury_compile_main.m:
compiler/module_cmds.m:
compiler/optimize.m:
compiler/options_file.m:
deep_profiler/conf.m:
deep_profiler/mdprof_cgi.m:
deep_profiler/mdprof_test.m:
library/io.file.m:
mdbcomp/trace_counts.m:
ssdb/ssdb.m:
tests/general/environment.m:
tests/hard_coded/closeable_channel_test.m:
tests/hard_coded/setenv.m:
tests/hard_coded/system_sort.m:
    Call the moved predicates directly in their new modules,
    not indirectly through io.m.
2022-03-08 09:38:27 +11:00

197 lines
6.1 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 2001-2002, 2004-2008, 2010 The University of Melbourne.
% Copyright (C) 2015-2017, 2019-2020 The Mercury team.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%---------------------------------------------------------------------------%
%
% Author: zs.
%
% This module contains primitives whose parameters are decided by
% ../configure.ac. This module picks them up from the #defines put into
% runtime/mercury_conf.h by the configure script.
%
%---------------------------------------------------------------------------%
:- module conf.
:- interface.
:- import_module io.
%---------------------------------------------------------------------------%
% Given a pathname, return a shell command that will create a named pipe
% with that pathname.
%
:- func make_pipe_cmd(string) = string.
% The name of the server and, optionally, the port on which mdprof is
% being run.
%
:- pred server_name_port(string::out, io::di, io::uo) is det.
% The virtual path under which this program is being executed, used for
% self-referencing URLs.
%
:- pred script_name(string::out, io::di, io::uo) is det.
:- func getpid = int.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module io.call_system.
:- import_module io.environment.
:- import_module io.file.
:- import_module list.
:- import_module maybe.
:- import_module require.
:- import_module string.
%---------------------------------------------------------------------------%
make_pipe_cmd(PipeName) = Cmd :-
mkfifo_cmd(CmdName),
( if CmdName = "" then
unexpected($pred, "do not know what command to use")
else
string.format("%s %s", [s(CmdName), s(PipeName)], Cmd)
).
server_name_port(Machine, !IO) :-
server_name(ServerName, !IO),
maybe_server_port(MaybeServerPort, !IO),
(
MaybeServerPort = yes(Port),
Machine = ServerName ++ ":" ++ Port
;
MaybeServerPort = no,
Machine = ServerName
).
:- pred server_name(string::out, io::di, io::uo) is det.
server_name(ServerName, !IO) :-
io.environment.get_environment_var("SERVER_NAME", MaybeServerName, !IO),
(
MaybeServerName = yes(ServerName)
;
MaybeServerName = no,
server_name_2(ServerName, !IO)
).
:- pred server_name_2(string::out, io::di, io::uo) is det.
server_name_2(ServerName, !IO) :-
io.file.make_temp_file(TmpFileResult, !IO),
(
TmpFileResult = ok(TmpFile),
hostname_cmd(HostnameCmd),
ServerRedirectCmd =
string.format("%s > %s", [s(HostnameCmd), s(TmpFile)]),
io.call_system.call_system(ServerRedirectCmd, Res1, !IO),
(
Res1 = ok(ResCode),
( if ResCode = 0 then
io.open_input(TmpFile, TmpRes, !IO),
(
TmpRes = ok(TmpStream),
io.read_file_as_string(TmpStream, TmpReadRes, !IO),
(
TmpReadRes = ok(ServerNameNl),
( if
string.remove_suffix(ServerNameNl, "\n",
ServerNamePrime)
then
ServerName = ServerNamePrime
else
unexpected($pred, "malformed server name")
)
;
TmpReadRes = error(_, _),
unexpected($pred, "cannot read server's name")
),
io.close_input(TmpStream, !IO)
;
TmpRes = error(_),
unexpected($pred,
"cannot open file to find the server's name")
),
io.file.remove_file(TmpFile, _, !IO)
else
unexpected($pred,
"cannot execute cmd to find the server's name")
)
;
Res1 = error(_),
unexpected($pred,
"cannot execute cmd to find the server's name")
)
;
TmpFileResult = error(_),
unexpected($pred, "Cannot create temporary file")
).
:- pred maybe_server_port(maybe(string)::out, io::di, io::uo) is det.
maybe_server_port(MaybeServerPort, !IO) :-
io.environment.get_environment_var("SERVER_PORT", MaybeServerPort, !IO).
script_name(ScriptName, !IO) :-
io.environment.get_environment_var("SCRIPT_NAME", MaybeScriptName, !IO),
(
MaybeScriptName = yes(ScriptName)
;
MaybeScriptName = no,
% Should not happen if this predicate is called as a CGI program,
% but this predicate is also called by other tools.
ScriptName = "/cgi-bin/mdprof_cgi"
).
:- pred mkfifo_cmd(string::out) is det.
:- pragma foreign_proc("C",
mkfifo_cmd(Mkfifo::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
/* shut up warnings about casting away const */
Mkfifo = (MR_String) (MR_Integer) MR_MKFIFO;
").
:- pred hostname_cmd(string::out) is det.
:- pragma foreign_proc("C",
hostname_cmd(Hostname::out),
[will_not_call_mercury, promise_pure, thread_safe],
"
/* shut up warnings about casting away const */
Hostname = (MR_String) (MR_Integer) MR_HOSTNAMECMD;
").
:- pragma foreign_decl("C",
"
#ifdef MR_DEEP_PROFILER_ENABLED
#include <sys/types.h>
#include <unistd.h>
#endif
").
:- pragma foreign_proc("C",
getpid = (Pid::out),
[will_not_call_mercury, promise_pure],
"
#ifdef MR_DEEP_PROFILER_ENABLED
Pid = getpid();
#else
MR_fatal_error(""the deep profiler is not supported"");
#endif
").
%---------------------------------------------------------------------------%
:- end_module conf.
%---------------------------------------------------------------------------%