resource.cpp 5.2 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8
#include "common/inner_common.h"
#include "framework/resource.h"
#include "framework/infer.h"

namespace baidu {
namespace paddle_serving {
namespace predictor {

W
wangguibao 已提交
9 10
using configure::ResourceConf;

W
wangguibao 已提交
11 12 13
// __thread bool p_thread_initialized = false;

static void dynamic_resource_deleter(void* d) {
W
sdk-cpp  
wangguibao 已提交
14 15 16
#if 1
    LOG(INFO) << "dynamic_resource_delete on " << bthread_self();
#endif
W
wangguibao 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    delete static_cast<DynamicResource*>(d);
}

DynamicResource::DynamicResource() {}

DynamicResource::~DynamicResource() {}

int DynamicResource::initialize() {
    return 0;
}

int DynamicResource::clear() {
    return 0;
}

int Resource::initialize(const std::string& path, const std::string& file) {
W
wangguibao 已提交
33 34 35
    ResourceConf resource_conf;
    if (configure::read_proto_conf(path, file, &resource_conf) != 0) {
        LOG(ERROR) << "Failed initialize resource from: "
W
wangguibao 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
            << 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;
W
wangguibao 已提交
49
        std::string model_toolkit_path = resource_conf.model_toolkit_path();
W
wangguibao 已提交
50 51 52 53 54
        if (err != 0) {
            LOG(ERROR) << "read model_toolkit_path failed, path["
                    << path << "], file[" << file << "]";
            return -1;
        }
W
wangguibao 已提交
55
        std::string model_toolkit_file = resource_conf.model_toolkit_file();
W
wangguibao 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        if (err != 0) {
            LOG(ERROR) << "read model_toolkit_file failed, path["
                    << path << "], file[" << file << "]";
            return -1;
        }
        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;
        }
    }

    if (THREAD_KEY_CREATE(&_tls_bspec_key, dynamic_resource_deleter) != 0) {
        LOG(ERROR) << "unable to create tls_bthread_key of thrd_data";
        return -1;
    }
    THREAD_SETSPECIFIC(_tls_bspec_key, NULL);
    return 0;
}

int Resource::thread_initialize() {
    // 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) {
87
        LOG(ERROR) << "Failed thrd initialized infer manager"; 
W
wangguibao 已提交
88 89 90 91 92 93 94
        return -1;
    }

    DynamicResource* p_dynamic_resource = (DynamicResource*) THREAD_GETSPECIFIC(_tls_bspec_key);
    if (p_dynamic_resource == NULL) {
        p_dynamic_resource = new (std::nothrow) DynamicResource;
        if (p_dynamic_resource == NULL) {
95
            LOG(ERROR) << "failed to create tls DynamicResource";
W
wangguibao 已提交
96 97 98
            return -1;
        }
        if (p_dynamic_resource->initialize() != 0) {
99
            LOG(ERROR) << "DynamicResource initialize failed.";
W
wangguibao 已提交
100 101 102 103 104 105
            delete p_dynamic_resource;
            p_dynamic_resource = NULL;
            return -1;
        }

        if (THREAD_SETSPECIFIC(_tls_bspec_key, p_dynamic_resource) != 0) {
106
            LOG(ERROR) << "unable to set tls DynamicResource";
W
wangguibao 已提交
107 108 109 110 111 112
            delete p_dynamic_resource;
            p_dynamic_resource = NULL;
            return -1;
        }

    }
W
sdk-cpp  
wangguibao 已提交
113
#if 0
W
wangguibao 已提交
114
    LOG(INFO) << "Successfully thread initialized dynamic resource";
W
sdk-cpp  
wangguibao 已提交
115 116
#else
    LOG(INFO) << bthread_self() << ": Successfully thread initialized dynamic resource " << p_dynamic_resource;
W
wangguibao 已提交
117

W
sdk-cpp  
wangguibao 已提交
118
#endif
W
wangguibao 已提交
119 120 121 122 123 124 125 126 127 128 129 130
    return 0;
}

int Resource::thread_clear() {
    // 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) {
131
        LOG(ERROR) << "Failed thrd clear infer manager"; 
W
wangguibao 已提交
132 133 134 135 136
        return -1;
    }

    DynamicResource* p_dynamic_resource = (DynamicResource*) THREAD_GETSPECIFIC(_tls_bspec_key);
    if (p_dynamic_resource == NULL) {
W
sdk-cpp  
wangguibao 已提交
137
#if 0
138
        LOG(ERROR) << "tls dynamic resource shouldn't be null after thread_initialize"; 
W
sdk-cpp  
wangguibao 已提交
139
#else
140
        LOG(ERROR) << bthread_self() << ": tls dynamic resource shouldn't be null after thread_initialize"; 
W
sdk-cpp  
wangguibao 已提交
141
#endif
W
wangguibao 已提交
142 143 144
        return -1;
    }
    if (p_dynamic_resource->clear() != 0) {
145
        LOG(ERROR) << "Failed to invoke dynamic resource clear"; 
W
wangguibao 已提交
146 147 148
        return -1;
    }

W
sdk-cpp  
wangguibao 已提交
149
     LOG(INFO) << bthread_self() << "Resource::thread_clear success";
W
wangguibao 已提交
150 151 152 153 154 155
    // ...
    return 0;
}

int Resource::reload() {
    if (FLAGS_enable_model_toolkit && InferManager::instance().reload() != 0) {
156
        LOG(ERROR) << "Failed reload infer manager"; 
W
wangguibao 已提交
157 158 159 160 161 162 163 164 165
        return -1;
    }
    
    // other resource reload here...
    return 0;
}

int Resource::finalize() {
    if (FLAGS_enable_model_toolkit && InferManager::instance().proc_finalize() != 0) {
166
        LOG(ERROR) << "Failed proc finalize infer manager";
W
wangguibao 已提交
167 168 169 170 171 172 173 174 175 176 177
        return -1;
    }

    THREAD_KEY_DELETE(_tls_bspec_key);

    return 0;
}

} // predictor
} // paddle_serving
} // baidu