shell.h 2.8 KB
Newer Older
D
dongdaxiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <fcntl.h>
#include <sys/stat.h>
D
dongdaxiang 已提交
19
#ifdef _WIN32
20 21 22
#ifndef NOMINMAX
#define NOMINMAX  // msvc max/min macro conflict with std::min/max
#endif
D
dongdaxiang 已提交
23 24
#include <windows.h>
#else
D
dongdaxiang 已提交
25
#include <sys/syscall.h>
D
dongdaxiang 已提交
26
#endif
D
dongdaxiang 已提交
27
#include <sys/types.h>
28
#ifndef _WIN32
D
dongdaxiang 已提交
29
#include <sys/wait.h>
30
#endif
D
dongdaxiang 已提交
31 32 33
#include <memory>
#include <string>
#include <utility>
G
gongweibao 已提交
34
#include <vector>
W
wanghuancoder 已提交
35

D
dongdaxiang 已提交
36
#include "paddle/fluid/platform/port.h"
37
#include "paddle/fluid/string/string_helper.h"
D
dongdaxiang 已提交
38

39 40 41 42 43
#if defined(__arm__) || defined(__aarch64__) || defined(__ARM_NEON) || \
    defined(__ARM_NEON__)
#define PADDLE_ARM
#endif

D
dongdaxiang 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
namespace paddle {
namespace framework {

inline bool& shell_verbose_internal() {
  static bool x = false;
  return x;
}

inline bool shell_verbose() { return shell_verbose_internal(); }

inline void shell_set_verbose(bool x) { shell_verbose_internal() = x; }

extern std::shared_ptr<FILE> shell_fopen(const std::string& path,
                                         const std::string& mode);

G
gongweibao 已提交
59 60 61 62
std::shared_ptr<FILE> shell_popen(const std::string& cmd,
                                  const std::string& mode, int* err_no,
                                  int* status = NULL,
                                  bool redirect_stderr = false);
D
dongdaxiang 已提交
63 64 65 66 67 68 69 70 71 72 73 74

extern std::pair<std::shared_ptr<FILE>, std::shared_ptr<FILE>> shell_p2open(
    const std::string& cmd);

inline void shell_execute(const std::string& cmd) {
  int err_no = 0;
  do {
    err_no = 0;
    shell_popen(cmd, "w", &err_no);
  } while (err_no == -1);
}

G
gongweibao 已提交
75
// time_out:ms, default value:-1 means forever.
76 77
// sleep_inter:ms, default -1 means not sleep.
extern std::string shell_get_command_output(const std::string& cmd,
G
gongweibao 已提交
78 79 80 81 82 83 84 85
                                            int time_out = 10 * 60 * 1000,
                                            int sleep_inter = 1000);
// time_out:ms, default -1 means forever.
// sleep_inter:ms, default -1 means not sleep.
extern std::vector<std::string> shell_execute_cmd(const std::string& cmd,
                                                  int time_out = 0,
                                                  int sleep_inter = 0,
                                                  bool redirect_stderr = false);
D
dongdaxiang 已提交
86 87 88

}  // namespace framework
}  // namespace paddle