File: SharedMemoryLoader.cc

package info (click to toggle)
metview 5.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 242,296 kB
  • sloc: cpp: 437,117; ansic: 41,433; xml: 19,944; f90: 13,059; sh: 6,562; python: 3,953; yacc: 1,774; lex: 1,121; perl: 701; makefile: 92
file content (329 lines) | stat: -rw-r--r-- 9,722 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */


#include "mir/caching/matrix/SharedMemoryLoader.h"

#include <cerrno>
#include <cstring>

#include <fcntl.h>
#include <unistd.h>

#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>

//#include "eckit/config/Resource.h"
#include "eckit/exception/Exceptions.h"
#include "eckit/log/Bytes.h"
#include "eckit/log/Timer.h"
#include "eckit/maths/Functions.h"
#include "eckit/memory/Padded.h"
#include "eckit/memory/Shmget.h"
//#include "eckit/os/SemLocker.h"
#include "eckit/runtime/Main.h"

#include "mir/method/WeightMatrix.h"
#include "mir/util/Error.h"
#include "mir/util/Pretty.h"


namespace mir {
namespace caching {
namespace matrix {


namespace {

const int MAGIC        = 987654321;
const size_t INFO_PATH = 1024;

struct SHMInfoNotPadded {
    int ready;
    int magic;
    char path[INFO_PATH];
};

using SHMInfo = eckit::Padded<SHMInfoNotPadded, 1280>;  // aligned to 64 bytes

}  // namespace


class Unloader {

    std::vector<eckit::PathName> paths_;

    Unloader() = default;

public:
    /// This ensures unloader is destructed in correct order with other static objects (like eckit::Log)
    static Unloader& instance() {
        static Unloader unloader;
        return unloader;
    }

    void add(const eckit::PathName& path) { paths_.push_back(path); }

    Unloader(const Unloader&) = delete;
    Unloader& operator=(const Unloader&) = delete;

    ~Unloader() {
        for (auto& path : paths_) {
            try {
                SharedMemoryLoader::unloadSharedMemory(path);
            }
            catch (std::exception& e) {
                eckit::Log::error() << e.what() << std::endl;
            }
        }
    }
};


#if 0
class GlobalSemaphore {
public:
    GlobalSemaphore(eckit::PathName path) : path_(path) {

        SharedMemoryLoader::log() << "Semaphore for " << path << std::endl;

        key_t key = ::ftok(path.asString().c_str(), 1);
        if (key == key_t(-1)) {
            throw eckit::FailedSystemCall("ftok(" + path.asString() + ")");
        }
        SYSCALL(semaphore_ = ::semget(key, 1, IPC_CREAT | 0600));
    }

    eckit::PathName path_;
    int semaphore_;
};
#endif


SharedMemoryLoader::SharedMemoryLoader(const std::string& name, const eckit::PathName& path) :
    MatrixLoader(name, path), address_(nullptr), size_(0), unload_(false) {

    eckit::Timer timer("SharedMemoryLoader: loading '" + path.asString() + "'", log());

    unload_ = name.substr(0, 4) == "tmp-";

    eckit::PathName real = path.realName();

    std::ostringstream msg("SharedMemoryLoader: ");

    msg << "path='" << real << "', hostname='" << eckit::Main::hostname() << "'";
    info() << msg.str() << std::endl;

    if (real.asString().size() >= INFO_PATH - 1) {
        warn() << msg.str() << ", path name too long, maximum=" << INFO_PATH;
        throw eckit::SeriousBug(msg.str());
    }

    // Try to get an exclusive lock, we may be waiting for another process
    // to create the memory segment and load it with the file content
    //    GlobalSemaphore gsem(real.dirName());
    //    static const int max_wait_lock = eckit::Resource<int>("$MIR_SEMLOCK_RETRIES", 60);
    //    eckit::SemLocker locker(gsem.semaphore_, gsem.path_, max_wait_lock);

    key_t key = ::ftok(real.asString().c_str(), 1);
    if (key == key_t(-1)) {
        warn() << msg.str() << "::ftok(" << real.asString() << "), " << util::Error();
        throw eckit::FailedSystemCall(msg.str());
    }

    // NOTE: size is based on file.size() which is assumed to be bigger than the memory footprint. Real size would be:
    // size_t sz = sizeof(SHMInfo) + w.footprint();

    size_t sz      = size_t(path.size()) + sizeof(SHMInfo);
    long page_size = ::sysconf(_SC_PAGESIZE);
    ASSERT(page_size > 0);
    size_t shmsize = eckit::round(sz, size_t(page_size));

    size_ = shmsize;

    msg << ", size: " << shmsize << " (" << eckit::Bytes(shmsize) << "), key: 0x" << std::hex << key << std::dec
        << ", page size: " << eckit::Bytes(page_size) << ", pages: " << Pretty( (size_t) (shmsize / page_size));

#ifdef IPC_INFO
    // Only on Linux?
    struct shminfo shm_info;
    SYSCALL(::shmctl(0, IPC_INFO, reinterpret_cast<shmid_ds*>(&shm_info)));
    msg << ", maximum shared memory segment size: " << eckit::Bytes((shm_info.shmmax >> 10) * 1024);
#endif

    // This may return EINVAL is the segment is too large 256MB
    int shmid;
    if ((shmid = eckit::Shmget::shmget(key, shmsize, IPC_CREAT | 0600)) < 0) {
        warn() << msg.str()
               << ", shmget: failed to acquire shared memory, check the maximum authorised on this system (Linux ipcs "
                  "-l, macOS/BSD ipcs -M), "
               << util::Error() << std::endl;
        throw eckit::FailedSystemCall(msg.str());
    }
    msg << ", shmid=" << shmid << std::endl;

#ifdef SHM_PAGESIZE
    {

        // log() << "SharedMemoryLoader: attempting to use 64K pages"  << std::endl;

        /* Use 64K pages to back the shared memory region */
        size_t shm_size;
        struct shmid_ds shm_buf = {0};
        psize_t psize_64k;
        psize_64k = 64 * 1024;

        shm_buf.shm_pagesize = psize_64k;
        if (::shmctl(shmid, SHM_PAGESIZE, &shm_buf)) {
            // warn() << "SharedMemoryLoader: ::shmctl(SHM_PAGESIZE) failed, " << util::Error() << std::endl;
        }
    }

#endif

    // Attach shared memory
    address_ = eckit::Shmget::shmat(shmid, nullptr, 0);
    if (address_ == (void*)-1) {
        warn() << msg.str() << ", shmat: failed to attach shared memory, " << util::Error() << std::endl;
        throw eckit::FailedSystemCall(msg.str());
    }


    try {

        auto addr = reinterpret_cast<char*>(address_);
        auto nfo  = reinterpret_cast<SHMInfo*>(addr);

        // Check if the file has been loaded in memory
        if (nfo->ready != 0) {
            log() << msg.str() << ", already loaded" << std::endl;

            if (nfo->magic != MAGIC) {
                warn() << msg.str() << ", bad magic=" << nfo->magic << std::endl;
                throw eckit::SeriousBug(msg.str());
            }

            if (real.asString() != nfo->path) {
                warn() << msg.str() << ", path mismatch, file='" << nfo->path << "'" << std::endl;
                throw eckit::SeriousBug(msg.str());
            }
        }
        else {

            method::WeightMatrix w(path);
            w.dump(addr + sizeof(SHMInfo), size());

            // Set info record for checks
            nfo->magic = MAGIC;
            std::strcpy(nfo->path, real.asString().c_str());
            nfo->ready = 1;
        }
    }
    catch (...) {
        eckit::Shmget::shmdt(address_);
        throw;
    }

    // Make sure memory is unloaded on exit
    if (unload_) {
        Unloader::instance().add(path);
    }

    // eckit::StdPipe f("ipcs", "r");
    // char line[1024];

    // while(fgets(line, sizeof(line), f)) {
    //     log() << "LOAD IPCS " << line << std::endl;
    // }
}


SharedMemoryLoader::~SharedMemoryLoader() {
    if (address_ != nullptr) {
        SYSCALL(eckit::Shmget::shmdt(address_));
    }
    if (unload_) {
        unloadSharedMemory(path_);
    }
}


void SharedMemoryLoader::loadSharedMemory(const eckit::PathName& path) {
    SharedMemoryLoader loader("shmem", path);
}


void SharedMemoryLoader::unloadSharedMemory(const eckit::PathName& path) {
    log() << "SharedMemoryLoader: unloading '" << path << "'" << std::endl;

    eckit::PathName real = path.realName();

    key_t key = ::ftok(real.asString().c_str(), 1);
    if (key == key_t(-1)) {
        warn() << "SharedMemoryLoader: ::ftok(" << real.asString() << ")" << std::endl;
        throw eckit::FailedSystemCall("SharedMemoryLoader: ::ftok");
    }

    int shmid = eckit::Shmget::shmget(key, 0, 0600);
    if (shmid < 0) {
        warn() << "SharedMemoryLoader: shmget: path='" << path << "', "
               << (errno == ENOENT ? "already unloaded" : "failed to acquire shared memory") << std::endl;
        return;
    }

    // FIXME: add to eckit::Shmget interface
    if (::shmctl(shmid, IPC_RMID, nullptr) < 0) {
        warn() << "SharedMemoryLoader: ::shmctl: cannot delete '" << path << "'" << std::endl;
    }

    log() << "SharedMemoryLoader: successfully unloaded '" << path << "'" << std::endl;

    // eckit::StdPipe f("ipcs", "r");
    // char line[1024];s
    // while(fgets(line, sizeof(line), f)) {
    //     log() << "UNLOAD IPCS " << line << std::endl;
    // }
}


void SharedMemoryLoader::print(std::ostream& out) const {
    out << "SharedMemoryLoader[path=" << path_ << ",size=" << eckit::Bytes(size_) << ",unload=" << unload_ << "]";
}


const void* SharedMemoryLoader::address() const {
    return reinterpret_cast<const char*>(address_) + sizeof(SHMInfo);
}


size_t SharedMemoryLoader::size() const {
    return size_ - sizeof(SHMInfo);
}


bool SharedMemoryLoader::inSharedMemory() const {
    return true;
}


static MatrixLoaderBuilder<SharedMemoryLoader> loader1("shared-memory");
static MatrixLoaderBuilder<SharedMemoryLoader> loader2("shmem");
static MatrixLoaderBuilder<SharedMemoryLoader> loader3("tmp-shmem");
static MatrixLoaderBuilder<SharedMemoryLoader> loader5("tmp-shared-memory");


}  // namespace matrix
}  // namespace caching
}  // namespace mir