resource.h 2.1 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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
X
xulongteng 已提交
16
#include <memory>
W
wangguibao 已提交
17
#include <string>
X
xulongteng 已提交
18
#include "cube/cube-api/include/cube_api.h"
19
#include "kvdb/paddle_rocksdb.h"
W
wangguibao 已提交
20
#include "predictor/common/inner_common.h"
W
wangguibao 已提交
21
#include "predictor/framework/infer.h"
W
wangguibao 已提交
22
#include "predictor/framework/memory.h"
W
wangguibao 已提交
23 24 25 26 27 28 29

namespace baidu {
namespace paddle_serving {
namespace predictor {

class BaseRdDict;
struct DynamicResource {
W
wangguibao 已提交
30 31 32 33 34 35 36
  DynamicResource();

  ~DynamicResource();

  int initialize();

  int clear();
W
wangguibao 已提交
37 38 39
};

class Resource {
W
wangguibao 已提交
40
 public:
W
wangguibao 已提交
41 42 43 44 45 46 47
  Resource() {
    // Reference InferManager::instance() explicitly, to make sure static
    // instance of InferManager is constructed before that of Resource, and
    // destruct after that of Resource
    // See https://stackoverflow.com/a/335746/1513460
    InferManager::instance();
  }
W
wangguibao 已提交
48

W
wangguibao 已提交
49
  ~Resource() { finalize(); }
W
wangguibao 已提交
50

W
wangguibao 已提交
51 52 53 54
  static Resource& instance() {
    static Resource ins;
    return ins;
  }
W
wangguibao 已提交
55

W
wangguibao 已提交
56
  int initialize(const std::string& path, const std::string& file);
X
xulongteng 已提交
57
  int cube_initialize(const std::string& path, const std::string& file);
W
wangguibao 已提交
58
  int thread_initialize();
W
wangguibao 已提交
59

W
wangguibao 已提交
60
  int thread_clear();
W
wangguibao 已提交
61

W
wangguibao 已提交
62
  int reload();
W
wangguibao 已提交
63

W
wangguibao 已提交
64
  int finalize();
W
wangguibao 已提交
65

W
wangjiawei04 已提交
66 67
  std::shared_ptr<RocksDBWrapper> getDB();

W
wangguibao 已提交
68 69 70 71
  DynamicResource* get_dynamic_resource() {
    return reinterpret_cast<DynamicResource*>(
        THREAD_GETSPECIFIC(_tls_bspec_key));
  }
W
wangguibao 已提交
72

W
wangguibao 已提交
73 74
 private:
  int thread_finalize() { return 0; }
W
wangjiawei04 已提交
75
  std::shared_ptr<RocksDBWrapper> db;
76

W
wangguibao 已提交
77
  THREAD_KEY_T _tls_bspec_key;
W
wangguibao 已提交
78 79
};

W
wangguibao 已提交
80 81 82
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu