timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
launch_process.cpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#ifndef TIMEMORY_UTILITY_LAUNCH_PROCESS_CPP_
26#define TIMEMORY_UTILITY_LAUNCH_PROCESS_CPP_ 1
27
31
32namespace tim
33{
34//
35//--------------------------------------------------------------------------------------//
36//
38launch_process(const char* cmd, const std::string& extra, std::ostream* os)
39{
40#if !defined(TIMEMORY_WINDOWS)
41 auto delim = delimit(cmd, " \t");
43 if(delim.size() < 2)
44 {
45 fp = tim::popen::popen(cmd, nullptr, nullptr);
46 }
47 else
48 {
49 static std::string _c = "-c";
50 std::array<char*, 4> _args;
51 _args.fill(nullptr);
52 char* _cshell = getenv("SHELL");
53 char* _ushell = getusershell();
54 std::string _shell = (_cshell) ? _cshell : (_ushell) ? getusershell() : "/bin/sh";
55 _args.at(0) = (char*) _shell.c_str();
56 _args.at(1) = (char*) _c.c_str();
57 _args.at(2) = (char*) cmd;
58 fp = tim::popen::popen(_args.at(0), _args.data());
59 }
60
61 if(fp == nullptr)
62 {
63 std::stringstream ss;
64 ss << "[timemory]> Error launching command: '" << cmd << "'... " << extra;
65 perror(ss.str().c_str());
66 return false;
67 }
68 if(os)
69 {
71 }
72
74 if(ec != 0)
75 {
76 std::stringstream ss;
77 ss << "[timemory]> Command: '" << cmd << "' returned a non-zero exit code: " << ec
78 << "... " << extra;
79 perror(ss.str().c_str());
80 return false;
81 }
82#else
83 if(std::system(nullptr) != 0)
84 {
85 int ec = std::system(cmd);
86
87 if(ec != 0)
88 {
89 fprintf(stderr,
90 "[timemory]> Command: '%s' returned a non-zero exit code: %i... %s\n",
91 cmd, ec, extra.c_str());
92 return false;
93 }
94 }
95 else
96 {
97 fprintf(stderr, "std::system unavailable for command: '%s'... %s\n", cmd,
98 extra.c_str());
99 return false;
100 }
101 (void) os;
102#endif
103
104 return true;
105}
106} // namespace tim
107
108#endif
STL namespace.
TIMEMORY_PIPE * popen(const char *path, char **argv, char **envp)
Definition: popen.cpp:229
std::ostream & flush_output(std::ostream &os, TIMEMORY_PIPE *proc, int max_counter)
Definition: popen.cpp:426
int pclose(TIMEMORY_PIPE *p)
Definition: popen.cpp:327
Definition: kokkosp.cpp:39
std::array< char *, 4 > _args
TIMEMORY_UTILITY_LINKAGE(bool) launch_process(const char *cmd
const std::string & extra
char * _ushell
auto ec
char * _cshell
std::string _shell
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
tim::popen::TIMEMORY_PIPE * fp
const std::string std::ostream * os
bool launch_process(const char *cmd, const std::string &extra="", std::ostream *os=nullptr)
ContainerT delimit(const std::string &line, const std::string &delimiters="\"',;: ", PredicateT &&predicate=[](const std::string &s) -> std::string { return s;})
Definition: delimit.hpp:68