pdserving.cpp 5.3 KB
Newer Older
W
serving  
wangguibao 已提交
1 2 3 4
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

W
wangguibao 已提交
5 6
#include <iostream>
#include <fstream>
W
wangguibao 已提交
7
#include <bthread/unstable.h> // bthread_set_worker_startfn
W
wangguibao 已提交
8 9 10 11 12
#include "common/inner_common.h"
#include "framework/workflow.h"
#include "framework/service.h"
#include "framework/manager.h"
#include "framework/server.h"
W
wangguibao 已提交
13
#include "butil/logging.h"
W
wangguibao 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include "framework/resource.h"
#include "common/constant.h"

using baidu::paddle_serving::predictor::ServerManager;
using baidu::paddle_serving::predictor::WorkflowManager;
using baidu::paddle_serving::predictor::InferServiceManager;
using baidu::paddle_serving::predictor::Resource;
using baidu::paddle_serving::predictor::FLAGS_workflow_path;
using baidu::paddle_serving::predictor::FLAGS_workflow_file;
using baidu::paddle_serving::predictor::FLAGS_inferservice_path;
using baidu::paddle_serving::predictor::FLAGS_inferservice_file;
using baidu::paddle_serving::predictor::FLAGS_logger_path;
using baidu::paddle_serving::predictor::FLAGS_logger_file;
using baidu::paddle_serving::predictor::FLAGS_resource_path;
using baidu::paddle_serving::predictor::FLAGS_resource_file;
using baidu::paddle_serving::predictor::FLAGS_reload_interval_s;
using baidu::paddle_serving::predictor::FLAGS_port;

W
wangguibao 已提交
32 33 34
using baidu::paddle_serving::configure::InferServiceConf;
using baidu::paddle_serving::configure::read_proto_conf;

W
wangguibao 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48
void print_revision(std::ostream& os, void*) {
#if defined(PDSERVING_VERSION)
    os << PDSERVING_VERSION;
#else
    os << "undefined";
#endif
#if defined(PDSERVING_BUILDTIME)
    os << ", BuildAt: " << PDSERVING_BUILDTIME;
#endif
}

static bvar::PassiveStatus<std::string> s_predictor_revision(
        "predictor_revision", print_revision, NULL);

W
wangguibao 已提交
49
DEFINE_bool(V, false, "print version, bool");
W
wangguibao 已提交
50 51 52 53 54 55 56 57
DEFINE_bool(g, false, "user defined gflag path");
DECLARE_string(flagfile);

void pthread_worker_start_fn() {
    Resource::instance().thread_initialize();
}

static void g_change_server_port() {
W
wangguibao 已提交
58 59
    InferServiceConf conf;
    if (read_proto_conf(FLAGS_inferservice_path.c_str(), FLAGS_inferservice_file.c_str(), &conf) != 0) {
W
wangguibao 已提交
60 61 62 63
        LOG(WARNING) << "failed to load configure[" << FLAGS_inferservice_path
                << "," << FLAGS_inferservice_file << "].";
        return;
    }
W
wangguibao 已提交
64 65
    uint32_t port = conf.port();
    if (port != 0) {
W
wangguibao 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79
        FLAGS_port = port;
        LOG(INFO) << "use configure[" << FLAGS_inferservice_path << "/"
            << FLAGS_inferservice_file << "] port[" << port << "] instead of flags";
    }
    return;
}

#ifdef UNIT_TEST
int ut_main(int argc, char** argv) {
#else 
int main(int argc, char** argv) {
#endif
    google::ParseCommandLineFlags(&argc, &argv, true);

W
wangguibao 已提交
80
    if (FLAGS_V) {
W
wangguibao 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94
        print_revision(std::cout, NULL);
        std::cout << std::flush;
        return 0;
    }

    if (!FLAGS_g) {
        google::SetCommandLineOption("flagfile", "conf/gflags.conf");
    }
    
    google::ParseCommandLineFlags(&argc, &argv, true);
    
    g_change_server_port();

    // initialize logger instance
W
serving  
wangguibao 已提交
95 96 97 98 99 100 101 102 103 104 105 106
    FLAGS_log_dir = "./log";

    struct stat st_buf;
    int ret = 0;
    if ((ret = stat("./log", &st_buf)) != 0) {
            mkdir("./log", 0777);
            ret = stat("./log", &st_buf);
            if (ret != 0) {
                    LOG(WARNING) << "Log path ./log not exist, and create fail";
                    return -1;
            }
    }
W
wangguibao 已提交
107
    google::InitGoogleLogging(strdup(argv[0]));
W
wangguibao 已提交
108

W
wangguibao 已提交
109
    LOG(INFO) << "Succ initialize logger";
W
wangguibao 已提交
110 111 112 113 114 115 116 117

    // initialize resource manager
    if (Resource::instance().initialize(
                FLAGS_resource_path, FLAGS_resource_file) != 0) {
        LOG(ERROR) << "Failed initialize resource, conf:" 
            << FLAGS_resource_path << "/" << FLAGS_resource_file;
        return -1; 
    }
W
wangguibao 已提交
118
    LOG(INFO) << "Succ initialize resource";
W
wangguibao 已提交
119 120 121 122 123 124 125 126

    // initialize workflow manager
    if (WorkflowManager::instance().initialize(
            FLAGS_workflow_path, FLAGS_workflow_file) != 0) {
        LOG(ERROR) << "Failed initialize workflow manager, conf:" 
            << FLAGS_workflow_path << "/" << FLAGS_workflow_file;
        return -1;
    }
W
wangguibao 已提交
127
    LOG(INFO) << "Succ initialize workflow";
W
wangguibao 已提交
128 129 130 131 132 133 134 135 136

    // initialize service manager
    if (InferServiceManager::instance().initialize(
            FLAGS_inferservice_path, FLAGS_inferservice_file) != 0) {
        LOG(ERROR) 
            << "Failed initialize infer service manager, conf:" 
            << FLAGS_inferservice_path << "/" << FLAGS_inferservice_file;
        return -1;
    }
W
wangguibao 已提交
137
    LOG(INFO) << "Succ initialize inferservice";
W
wangguibao 已提交
138 139 140

    int errcode = bthread_set_worker_startfn(pthread_worker_start_fn);
    if (errcode != 0) {
141
        LOG(ERROR) << "Failed call pthread worker start function, error_code[" << errcode << "]";
W
wangguibao 已提交
142 143 144 145 146 147 148 149
        return -1;
    }
    LOG(INFO) << "Succ call pthread worker start function";

    if (ServerManager::instance().start_and_wait() != 0) {
        LOG(ERROR) << "Failed start server and wait!";
        return -1;
    }
W
wangguibao 已提交
150
    LOG(INFO) << "Succ start service manager";
W
wangguibao 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163

    if (InferServiceManager::instance().finalize() != 0) {
        LOG(ERROR) << "Failed finalize infer service manager.";
    }

    if (WorkflowManager::instance().finalize() != 0) {
        LOG(ERROR) << "Failed finalize workflow manager";
    }

    if (Resource::instance().finalize() != 0) {
        LOG(ERROR) << "Failed finalize resource manager";
    }

W
wangguibao 已提交
164
    google::ShutdownGoogleLogging();
W
wangguibao 已提交
165 166 167
    LOG(INFO) << "Paddle Inference Server exit successfully!";
    return 0;
}