shell.h 2.7 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 20 21
#ifdef _WIN32
#include <windows.h>
#else
D
dongdaxiang 已提交
22
#include <sys/syscall.h>
D
dongdaxiang 已提交
23
#endif
D
dongdaxiang 已提交
24
#include <sys/types.h>
25
#ifndef _WIN32
D
dongdaxiang 已提交
26
#include <sys/wait.h>
27
#endif
D
dongdaxiang 已提交
28 29 30
#include <memory>
#include <string>
#include <utility>
G
gongweibao 已提交
31
#include <vector>
D
dongdaxiang 已提交
32
#include "paddle/fluid/platform/port.h"
33
#include "paddle/fluid/string/string_helper.h"
D
dongdaxiang 已提交
34

35 36 37 38 39
#if defined(__arm__) || defined(__aarch64__) || defined(__ARM_NEON) || \
    defined(__ARM_NEON__)
#define PADDLE_ARM
#endif

D
dongdaxiang 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
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 已提交
55 56 57 58
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 已提交
59 60 61 62 63 64 65 66 67 68 69 70

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 已提交
71
// time_out:ms, default value:-1 means forever.
72 73
// sleep_inter:ms, default -1 means not sleep.
extern std::string shell_get_command_output(const std::string& cmd,
G
gongweibao 已提交
74 75 76 77 78 79 80 81
                                            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 已提交
82 83 84

}  // namespace framework
}  // namespace paddle