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

namespace baidu {
namespace paddle_serving {
namespace predictor {

// __thread bool p_thread_initialized = false;

static void dynamic_resource_deleter(void* d) {
W
sdk-cpp  
wangguibao 已提交
12 13 14
#if 1
    LOG(INFO) << "dynamic_resource_delete on " << bthread_self();
#endif
W
wangguibao 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    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) {
    comcfg::Configure conf;
    if (conf.load(path.c_str(), file.c_str()) != 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 = conf["model_toolkit_path"].to_cstr(&err);
        if (err != 0) {
            LOG(ERROR) << "read model_toolkit_path failed, path["
                    << path << "], file[" << file << "]";
            return -1;
        }
        std::string model_toolkit_file = conf["model_toolkit_file"].to_cstr(&err);
        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) {
        LOG(FATAL) << "Failed thrd initialized infer manager"; 
        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) {
            LOG(FATAL) << "failed to create tls DynamicResource";
            return -1;
        }
        if (p_dynamic_resource->initialize() != 0) {
            LOG(FATAL) << "DynamicResource initialize failed.";
            delete p_dynamic_resource;
            p_dynamic_resource = NULL;
            return -1;
        }

        if (THREAD_SETSPECIFIC(_tls_bspec_key, p_dynamic_resource) != 0) {
            LOG(FATAL) << "unable to set tls DynamicResource";
            delete p_dynamic_resource;
            p_dynamic_resource = NULL;
            return -1;
        }

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

W
sdk-cpp  
wangguibao 已提交
116
#endif
W
wangguibao 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    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) {
        LOG(FATAL) << "Failed thrd clear infer manager"; 
        return -1;
    }

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

W
sdk-cpp  
wangguibao 已提交
147
     LOG(INFO) << bthread_self() << "Resource::thread_clear success";
W
wangguibao 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    // ...
    return 0;
}

int Resource::reload() {
    if (FLAGS_enable_model_toolkit && InferManager::instance().reload() != 0) {
        LOG(FATAL) << "Failed reload infer manager"; 
        return -1;
    }
    
    // other resource reload here...
    return 0;
}

int Resource::finalize() {
    if (FLAGS_enable_model_toolkit && InferManager::instance().proc_finalize() != 0) {
        LOG(FATAL) << "Failed proc finalize infer manager";
        return -1;
    }

    THREAD_KEY_DELETE(_tls_bspec_key);

    return 0;
}

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