Ticket #10174: zmserver-mmap.patch

File zmserver-mmap.patch, 5.1 KB (added by Argus <pthorn-mythd@…>, 9 years ago)

Mapped memory patch - MythZoneminder?

  • mythplugins/mythzoneminder/mythzmserver/zmserver.cpp

    diff --git a/mythplugins/mythzoneminder/mythzmserver/zmserver.cpp b/mythplugins/mythzoneminder/mythzmserver/zmserver.cpp
    index a6c94ac..fb03b4a 100644
    a b  
    2525#include <sys/stat.h>
    2626#include <sys/shm.h>
    2727
     28#if _POSIX_MAPPED_FILES > 0L
     29#include <sys/mman.h>
     30#endif
     31
    2832#ifdef linux
    2933#  include <sys/vfs.h>
    3034#  include <sys/statvfs.h>
    ZMServer::ZMServer(int sock, bool debug) 
    218222        cout << "Shared memory key is: " << buf << endl;
    219223    }
    220224
     225    // get the MMAP path
     226    m_mmapPath = getZMSetting("ZM_PATH_MAP");
     227    if (m_debug)
     228    {
     229        cout << "Memory path directory is: " << m_mmapPath << endl;
     230    }
     231
    221232    // get the event filename format
    222233    setting = getZMSetting("ZM_EVENT_IMAGE_DIGITS");
    223234    int eventDigits = atoi(setting.c_str());
    void ZMServer::getMonitorList(void) 
    12861297
    12871298void ZMServer::initMonitor(MONITOR *monitor)
    12881299{
    1289     void *shm_ptr;
     1300    void *shm_ptr = NULL;
    12901301
    12911302    monitor->shared_data = NULL;
    12921303    monitor->shared_images = NULL;
    void ZMServer::initMonitor(MONITOR *monitor) 
    13091320            ((monitor->image_buffer_count) * (sizeof(struct timeval))) +
    13101321            ((monitor->image_buffer_count) * monitor->frame_size);
    13111322
    1312     int shmid;
    13131323
    1314     if ((shmid = shmget((m_shmKey & 0xffffff00) | monitor->mon_id,
    1315          shared_data_size, SHM_R)) == -1)
     1324#if _POSIX_MAPPED_FILES > 0L
     1325    /*
     1326     * Try to open the mmap file first if the architecture supports it.
     1327     * Otherwise, legacy shared memory will be used below.
     1328     */
     1329    stringstream mmap_filename;
     1330    mmap_filename << m_mmapPath << "/zm.mmap." << monitor->mon_id;
     1331
     1332    int mapFile = open(mmap_filename.str().c_str(), O_RDONLY, 0x0);
     1333    if (mapFile >= 0)
    13161334    {
    1317         cout << "Failed to shmget for monitor: " << monitor->mon_id << endl;
    1318         monitor->status = "Error";
    1319         switch(errno)
     1335        if (m_debug)
     1336            cout << "Opened mmap file: " << mmap_filename << endl;
     1337
     1338        shm_ptr = mmap(NULL, shared_data_size, PROT_READ,
     1339                                            MAP_SHARED, mapFile, 0x0);
     1340        if (shm_ptr == NULL)
    13201341        {
    1321             case EACCES: cout << "EACCES - no rights to access segment\n"; break;
    1322             case EEXIST: cout << "EEXIST - segment already exists\n"; break;
    1323             case EINVAL: cout << "EINVAL - size < SHMMIN or size > SHMMAX\n"; break;
    1324             case ENFILE: cout << "ENFILE - limit on open files has been reached\n"; break;
    1325             case ENOENT: cout << "ENOENT - no segment exists for the given key\n"; break;
    1326             case ENOMEM: cout << "ENOMEM - couldn't reserve memory for segment\n"; break;
    1327             case ENOSPC: cout << "ENOSPC - shmmni or shmall limit reached\n"; break;
     1342            cout << "Failed to map shared memory from file [" <<
     1343                mmap_filename << "] " <<
     1344                "for monitor: " <<
     1345                monitor->mon_id <<
     1346                endl;
     1347            monitor->status = "Error";
     1348            return;
    13281349        }
    1329 
    1330         return;
    13311350    }
    1332 
    1333     shm_ptr = shmat(shmid, 0, SHM_RDONLY);
    1334 
     1351#endif
    13351352
    13361353    if (shm_ptr == NULL)
    13371354    {
    1338         cout << "Failed to shmat for monitor: " << monitor->mon_id << endl;
    1339         monitor->status = "Error";
    1340         return;
     1355        // fail back to shmget() functionality if mapping memory above failed.
     1356        int shmid;
     1357
     1358        if ((shmid = shmget((m_shmKey & 0xffffff00) | monitor->mon_id,
     1359             shared_data_size, SHM_R)) == -1)
     1360        {
     1361            cout << "Failed to shmget for monitor: " << monitor->mon_id << endl;
     1362            monitor->status = "Error";
     1363            switch(errno)
     1364            {
     1365                case EACCES: cout << "EACCES - no rights to access segment\n"; break;
     1366                case EEXIST: cout << "EEXIST - segment already exists\n"; break;
     1367                case EINVAL: cout << "EINVAL - size < SHMMIN or size > SHMMAX\n"; break;
     1368                case ENFILE: cout << "ENFILE - limit on open files has been reached\n"; break;
     1369                case ENOENT: cout << "ENOENT - no segment exists for the given key\n"; break;
     1370                case ENOMEM: cout << "ENOMEM - couldn't reserve memory for segment\n"; break;
     1371                case ENOSPC: cout << "ENOSPC - shmmni or shmall limit reached\n"; break;
     1372            }
     1373
     1374            return;
     1375        }
     1376
     1377        shm_ptr = shmat(shmid, 0, SHM_RDONLY);
     1378
     1379
     1380        if (shm_ptr == NULL)
     1381        {
     1382            cout << "Failed to shmat for monitor: " << monitor->mon_id << endl;
     1383            monitor->status = "Error";
     1384            return;
     1385        }
    13411386    }
    13421387
    13431388    monitor->shared_data = (SharedData*)shm_ptr;
  • mythplugins/mythzoneminder/mythzmserver/zmserver.h

    diff --git a/mythplugins/mythzoneminder/mythzmserver/zmserver.h b/mythplugins/mythzoneminder/mythzmserver/zmserver.h
    index 849f4ec..8d0dc4b 100644
    a b class ZMServer 
    191191    string               m_eventFileFormat;
    192192    string               m_analyseFileFormat;
    193193    key_t                m_shmKey;
     194    string               m_mmapPath;
    194195};
    195196
    196197