resource.cpp 5.8 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

W
wangguibao 已提交
15
#include "predictor/framework/resource.h"
W
wangguibao 已提交
16
#include <string>
W
wangguibao 已提交
17 18
#include "predictor/common/inner_common.h"
#include "predictor/framework/infer.h"
W
wangguibao 已提交
19 20 21 22 23

namespace baidu {
namespace paddle_serving {
namespace predictor {

W
wangguibao 已提交
24 25
using configure::ResourceConf;

W
wangguibao 已提交
26 27 28
// __thread bool p_thread_initialized = false;

static void dynamic_resource_deleter(void* d) {
W
sdk-cpp  
wangguibao 已提交
29
#if 1
W
wangguibao 已提交
30
  LOG(INFO) << "dynamic_resource_delete on " << bthread_self();
W
sdk-cpp  
wangguibao 已提交
31
#endif
W
wangguibao 已提交
32
  delete static_cast<DynamicResource*>(d);
W
wangguibao 已提交
33 34 35 36 37 38
}

DynamicResource::DynamicResource() {}

DynamicResource::~DynamicResource() {}

W
wangjiawei04 已提交
39 40 41 42 43 44 45
int DynamicResource::initialize() { 
    return 0;
}

std::shared_ptr<RocksDBWrapper> Resource::getDB() {
    return db;
}
W
wangguibao 已提交
46

W
wangguibao 已提交
47
int DynamicResource::clear() { return 0; }
W
wangguibao 已提交
48 49

int Resource::initialize(const std::string& path, const std::string& file) {
W
wangguibao 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  ResourceConf resource_conf;
  if (configure::read_proto_conf(path, file, &resource_conf) != 0) {
    LOG(ERROR) << "Failed initialize resource from: " << path << "/" << file;
    return -1;
  }

  // mempool
  if (MempoolWrapper::instance().initialize() != 0) {
    LOG(ERROR) << "Failed proc initialized mempool wrapper";
    return -1;
  }
  LOG(WARNING) << "Successfully proc initialized mempool wrapper";

  if (FLAGS_enable_model_toolkit) {
    int err = 0;
    std::string model_toolkit_path = resource_conf.model_toolkit_path();
    if (err != 0) {
      LOG(ERROR) << "read model_toolkit_path failed, path[" << path
                 << "], file[" << file << "]";
      return -1;
W
wangguibao 已提交
70
    }
W
wangguibao 已提交
71 72 73 74 75
    std::string model_toolkit_file = resource_conf.model_toolkit_file();
    if (err != 0) {
      LOG(ERROR) << "read model_toolkit_file failed, path[" << path
                 << "], file[" << file << "]";
      return -1;
W
wangguibao 已提交
76
    }
W
wangguibao 已提交
77 78 79 80 81
    if (InferManager::instance().proc_initialize(
            model_toolkit_path.c_str(), model_toolkit_file.c_str()) != 0) {
      LOG(ERROR) << "failed proc initialize modeltoolkit, config: "
                 << model_toolkit_path << "/" << model_toolkit_file;
      return -1;
W
wangguibao 已提交
82
    }
W
wangguibao 已提交
83 84 85 86 87 88
  }

  if (THREAD_KEY_CREATE(&_tls_bspec_key, dynamic_resource_deleter) != 0) {
    LOG(ERROR) << "unable to create tls_bthread_key of thrd_data";
    return -1;
  }
W
wangjiawei04 已提交
89 90 91 92 93
    //init rocksDB instance
  if (db.get() == nullptr) {
    db = RocksDBWrapper::RocksDBWrapperFactory("kvdb");
  }

W
wangguibao 已提交
94 95
  THREAD_SETSPECIFIC(_tls_bspec_key, NULL);
  return 0;
W
wangguibao 已提交
96 97 98
}

int Resource::thread_initialize() {
W
wangguibao 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  // mempool
  if (MempoolWrapper::instance().thread_initialize() != 0) {
    LOG(ERROR) << "Failed thread initialized mempool wrapper";
    return -1;
  }
  LOG(WARNING) << "Successfully thread initialized mempool wrapper";

  // infer manager
  if (FLAGS_enable_model_toolkit &&
      InferManager::instance().thrd_initialize() != 0) {
    LOG(ERROR) << "Failed thrd initialized infer manager";
    return -1;
  }

  DynamicResource* p_dynamic_resource =
      reinterpret_cast<DynamicResource*>(THREAD_GETSPECIFIC(_tls_bspec_key));
  if (p_dynamic_resource == NULL) {
    p_dynamic_resource = new (std::nothrow) DynamicResource;
    if (p_dynamic_resource == NULL) {
      LOG(ERROR) << "failed to create tls DynamicResource";
      return -1;
W
wangguibao 已提交
120
    }
W
wangguibao 已提交
121 122 123 124 125
    if (p_dynamic_resource->initialize() != 0) {
      LOG(ERROR) << "DynamicResource initialize failed.";
      delete p_dynamic_resource;
      p_dynamic_resource = NULL;
      return -1;
W
wangguibao 已提交
126 127
    }

W
wangguibao 已提交
128 129 130 131 132
    if (THREAD_SETSPECIFIC(_tls_bspec_key, p_dynamic_resource) != 0) {
      LOG(ERROR) << "unable to set tls DynamicResource";
      delete p_dynamic_resource;
      p_dynamic_resource = NULL;
      return -1;
W
wangguibao 已提交
133
    }
W
wangguibao 已提交
134
  }
W
sdk-cpp  
wangguibao 已提交
135
#if 0
W
wangguibao 已提交
136
    LOG(INFO) << "Successfully thread initialized dynamic resource";
W
sdk-cpp  
wangguibao 已提交
137
#else
W
wangguibao 已提交
138 139 140
  LOG(INFO) << bthread_self()
            << ": Successfully thread initialized dynamic resource "
            << p_dynamic_resource;
W
wangguibao 已提交
141

W
sdk-cpp  
wangguibao 已提交
142
#endif
W
wangguibao 已提交
143
  return 0;
W
wangguibao 已提交
144 145 146
}

int Resource::thread_clear() {
W
wangguibao 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  // mempool
  if (MempoolWrapper::instance().thread_clear() != 0) {
    LOG(ERROR) << "Failed thread clear mempool wrapper";
    return -1;
  }

  // infer manager
  if (FLAGS_enable_model_toolkit &&
      InferManager::instance().thrd_clear() != 0) {
    LOG(ERROR) << "Failed thrd clear infer manager";
    return -1;
  }

  DynamicResource* p_dynamic_resource =
      reinterpret_cast<DynamicResource*>(THREAD_GETSPECIFIC(_tls_bspec_key));
  if (p_dynamic_resource == NULL) {
W
sdk-cpp  
wangguibao 已提交
163
#if 0
W
wangguibao 已提交
164 165
    LOG(ERROR) << "tls dynamic resource shouldn't be null after "
        << "thread_initialize";
W
sdk-cpp  
wangguibao 已提交
166
#else
W
wangguibao 已提交
167 168 169
    LOG(ERROR)
        << bthread_self()
        << ": tls dynamic resource shouldn't be null after thread_initialize";
W
sdk-cpp  
wangguibao 已提交
170
#endif
W
wangguibao 已提交
171 172 173 174 175 176 177 178 179 180
    return -1;
  }
  if (p_dynamic_resource->clear() != 0) {
    LOG(ERROR) << "Failed to invoke dynamic resource clear";
    return -1;
  }

  LOG(INFO) << bthread_self() << "Resource::thread_clear success";
  // ...
  return 0;
W
wangguibao 已提交
181 182 183
}

int Resource::reload() {
W
wangguibao 已提交
184 185 186 187 188 189 190
  if (FLAGS_enable_model_toolkit && InferManager::instance().reload() != 0) {
    LOG(ERROR) << "Failed reload infer manager";
    return -1;
  }

  // other resource reload here...
  return 0;
W
wangguibao 已提交
191 192 193
}

int Resource::finalize() {
W
wangguibao 已提交
194 195 196 197 198
  if (FLAGS_enable_model_toolkit &&
      InferManager::instance().proc_finalize() != 0) {
    LOG(ERROR) << "Failed proc finalize infer manager";
    return -1;
  }
W
wangguibao 已提交
199

W
wangguibao 已提交
200
  THREAD_KEY_DELETE(_tls_bspec_key);
W
wangguibao 已提交
201

W
wangguibao 已提交
202
  return 0;
W
wangguibao 已提交
203 204
}

W
wangguibao 已提交
205 206 207
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu