Commit 0da08c22 authored by Marco Bubke's avatar Marco Bubke
Browse files

QmlDesigner: Use a POSIX shared memory for unix



QSharedMemory is using SVR4 shared memory for unix which is limited to
4 MB globally which is really hurting the performance for the designer.
POSIX shared memory is lifting some of this constraints but still has
some bugs on Max OS but generally works better.

Change-Id: I74c1ffd56495f408cd9340cd159190a1175a4086
Reviewed-by: default avatarTim Jenssen <tim.jenssen@digia.com>
parent edb7961f
......@@ -29,7 +29,7 @@
#include "valueschangedcommand.h"
#include <QSharedMemory>
#include "sharedmemory.h"
#include <QCache>
#include <QDebug>
......@@ -37,7 +37,7 @@
namespace QmlDesigner {
static QCache<qint32, QSharedMemory> globalSharedMemoryCache(10000);
static QCache<qint32, SharedMemory> globalSharedMemoryCache(10000);
ValuesChangedCommand::ValuesChangedCommand()
: m_keyNumber(0)
......@@ -63,7 +63,7 @@ quint32 ValuesChangedCommand::keyNumber() const
void ValuesChangedCommand::removeSharedMemorys(const QVector<qint32> &keyNumberVector)
{
foreach (qint32 keyNumber, keyNumberVector) {
QSharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
SharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
delete sharedMemory;
}
}
......@@ -75,17 +75,11 @@ void ValuesChangedCommand::sort()
static const QLatin1String valueKeyTemplateString("Values-%1");
static QSharedMemory *createSharedMemory(qint32 key, int byteCount)
static SharedMemory *createSharedMemory(qint32 key, int byteCount)
{
QSharedMemory *sharedMemory = new QSharedMemory(QString(valueKeyTemplateString).arg(key));
SharedMemory *sharedMemory = new SharedMemory(QString(valueKeyTemplateString).arg(key));
bool sharedMemoryIsCreated = sharedMemory->create(byteCount);
if (!sharedMemoryIsCreated) {
if (sharedMemory->isAttached())
sharedMemory->attach();
sharedMemory->detach();
sharedMemoryIsCreated = sharedMemory->create(byteCount);
}
if (sharedMemoryIsCreated) {
globalSharedMemoryCache.insert(key, sharedMemory);
......@@ -109,12 +103,13 @@ QDataStream &operator<<(QDataStream &out, const ValuesChangedCommand &command)
temporaryOutDataStream << command.valueChanges();
QSharedMemory *sharedMemory = createSharedMemory(keyCounter, outDataStreamByteArray.size());
SharedMemory *sharedMemory = createSharedMemory(keyCounter, outDataStreamByteArray.size());
if (sharedMemory) {
sharedMemory->lock();
std::memcpy(sharedMemory->data(), outDataStreamByteArray.constData(), sharedMemory->size());
sharedMemory->unlock();
out << command.keyNumber();
return out;
}
......@@ -128,13 +123,18 @@ QDataStream &operator<<(QDataStream &out, const ValuesChangedCommand &command)
void readSharedMemory(qint32 key, QVector<PropertyValueContainer> *valueChangeVector)
{
QSharedMemory sharedMemory(QString(valueKeyTemplateString).arg(key));
SharedMemory sharedMemory(QString(valueKeyTemplateString).arg(key));
bool canAttach = sharedMemory.attach(QSharedMemory::ReadOnly);
if (canAttach) {
sharedMemory.lock();
QDataStream in(QByteArray::fromRawData(static_cast<const char*>(sharedMemory.constData()), sharedMemory.size()));
in.setVersion(QDataStream::Qt_4_8);
in >> *valueChangeVector;
sharedMemory.unlock();
sharedMemory.detach();
}
}
......
INCLUDEPATH += $$PWD/
HEADERS += $$PWD/addimportcontainer.h
HEADERS += $$PWD/sharedmemory.h
HEADERS += $$PWD/imagecontainer.h
HEADERS += $$PWD/idcontainer.h
HEADERS += $$PWD/informationcontainer.h
HEADERS += $$PWD/instancecontainer.h
HEADERS += $$PWD/reparentcontainer.h
HEADERS += $$PWD/propertyabstractcontainer.h
HEADERS += $$PWD/propertybindingcontainer.h
HEADERS += $$PWD/propertyvaluecontainer.h
HEADERS += $$PWD/propertyabstractcontainer.h
HEADERS += $$PWD/propertybindingcontainer.h
HEADERS += $$PWD/propertyvaluecontainer.h
SOURCES += $$PWD/addimportcontainer.cpp
SOURCES += $$PWD/imagecontainer.cpp
SOURCES += $$PWD/idcontainer.cpp
SOURCES += $$PWD/informationcontainer.cpp
SOURCES += $$PWD/instancecontainer.cpp
SOURCES += $$PWD/reparentcontainer.cpp
SOURCES += $$PWD/propertyabstractcontainer.cpp
SOURCES += $$PWD/propertybindingcontainer.cpp
SOURCES += $$PWD/propertyvaluecontainer.cpp
SOURCES += $$PWD/addimportcontainer.cpp
unix:SOURCES += $$PWD/sharedmemory_unix.cpp
!unix:SOURCES += $$PWD/sharedmemory_qt.cpp
SOURCES += $$PWD/imagecontainer.cpp
SOURCES += $$PWD/idcontainer.cpp
SOURCES += $$PWD/informationcontainer.cpp
SOURCES += $$PWD/instancecontainer.cpp
SOURCES += $$PWD/reparentcontainer.cpp
SOURCES += $$PWD/propertyabstractcontainer.cpp
SOURCES += $$PWD/propertybindingcontainer.cpp
SOURCES += $$PWD/propertyvaluecontainer.cpp
......@@ -29,7 +29,7 @@
#include "imagecontainer.h"
#include <QSharedMemory>
#include "sharedmemory.h"
#include <QCache>
#include <QDebug>
......@@ -42,7 +42,7 @@
namespace QmlDesigner {
static QCache<qint32, QSharedMemory> globalSharedMemoryCache(10000);
static QCache<qint32, SharedMemory> globalSharedMemoryCache(10000);
ImageContainer::ImageContainer()
: m_instanceId(-1),
......@@ -82,41 +82,52 @@ void ImageContainer::setImage(const QImage &image)
void ImageContainer::removeSharedMemorys(const QVector<qint32> &keyNumberVector)
{
foreach (qint32 keyNumber, keyNumberVector) {
QSharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
SharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
delete sharedMemory;
}
}
static const QLatin1String imageKeyTemplateString("Image-%1");
static QSharedMemory *createSharedMemory(qint32 key, int byteCount)
static SharedMemory *createSharedMemory(qint32 key, int byteCount)
{
QSharedMemory *sharedMemory = globalSharedMemoryCache.take(key);
if (sharedMemory == 0)
sharedMemory = new QSharedMemory(QString(imageKeyTemplateString).arg(key));
bool sharedMemoryIsCreated = sharedMemory->isAttached();
if (!sharedMemoryIsCreated)
sharedMemoryIsCreated = sharedMemory->attach();
bool sharedMemorySizeIsSmallerThanByteCount = sharedMemory->size() < byteCount;
bool sharedMemorySizeIsDoubleBiggerThanByteCount = sharedMemory->size() * 2 > byteCount;
if (!sharedMemoryIsCreated || sharedMemorySizeIsSmallerThanByteCount || sharedMemorySizeIsDoubleBiggerThanByteCount) {
sharedMemory->detach();
sharedMemoryIsCreated = sharedMemory->create(byteCount);
}
if (sharedMemoryIsCreated) {
globalSharedMemoryCache.insert(key, sharedMemory);
return sharedMemory;
SharedMemory *sharedMemory = globalSharedMemoryCache.take(key);
if (sharedMemory == 0) {
sharedMemory = new SharedMemory(QString(imageKeyTemplateString).arg(key));
bool sharedMemoryIsCreated = sharedMemory->create(byteCount);
if (sharedMemoryIsCreated) {
globalSharedMemoryCache.insert(key, sharedMemory);
} else {
delete sharedMemory;
sharedMemory = 0;
}
} else {
bool sharedMemoryIsAttached = sharedMemory->isAttached();
if (!sharedMemoryIsAttached)
sharedMemoryIsAttached = sharedMemory->attach();
bool sharedMemorySizeIsSmallerThanByteCount = sharedMemory->size() < byteCount;
bool sharedMemorySizeIsDoubleBiggerThanByteCount = sharedMemory->size() > (byteCount * 2);
if (!sharedMemoryIsAttached) {
sharedMemory->create(byteCount);
} else if (sharedMemorySizeIsSmallerThanByteCount || sharedMemorySizeIsDoubleBiggerThanByteCount) {
sharedMemory->detach();
sharedMemory->create(byteCount);
}
if (!sharedMemory->isAttached()) {
globalSharedMemoryCache.remove(key);
delete sharedMemory;
sharedMemory = 0;
}
}
return 0;
return sharedMemory;
}
static void writeSharedMemory(QSharedMemory *sharedMemory, const QImage &image)
static void writeSharedMemory(SharedMemory *sharedMemory, const QImage &image)
{
sharedMemory->lock();
......@@ -129,7 +140,6 @@ static void writeSharedMemory(QSharedMemory *sharedMemory, const QImage &image)
std::memcpy(sharedMemory->data(), headerData, 20);
std::memcpy(reinterpret_cast<char*>(sharedMemory->data()) + 20, image.constBits(), image.byteCount());
sharedMemory->unlock();
}
......@@ -156,7 +166,7 @@ QDataStream &operator<<(QDataStream &out, const ImageContainer &container)
out << qint32(0);
writeStream(out, image);
} else {
QSharedMemory *sharedMemory = createSharedMemory(container.keyNumber(), image.byteCount() + extraDataSize);
SharedMemory *sharedMemory = createSharedMemory(container.keyNumber(), image.byteCount() + extraDataSize);
out << qint32(sharedMemory != 0); // send if shared memory is used
......@@ -171,7 +181,7 @@ QDataStream &operator<<(QDataStream &out, const ImageContainer &container)
static void readSharedMemory(qint32 key, ImageContainer &container)
{
QSharedMemory sharedMemory(QString(imageKeyTemplateString).arg(key));
SharedMemory sharedMemory(QString(imageKeyTemplateString).arg(key));
bool canAttach = sharedMemory.attach(QSharedMemory::ReadOnly);
......@@ -194,6 +204,7 @@ static void readSharedMemory(qint32 key, ImageContainer &container)
container.setImage(image);
sharedMemory.unlock();
sharedMemory.detach();
}
}
......
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef QMLDESIGNER_SHAREDMEMORY_H
#define QMLDESIGNER_SHAREDMEMORY_H
#include <QSharedMemory>
#include <QSystemSemaphore>
namespace QmlDesigner {
class SharedMemory
{
friend class SharedMemoryLocker;
public:
SharedMemory();
SharedMemory(const QString &key);
~SharedMemory();
void setKey(const QString &key);
QString key() const;
bool create(int size, QSharedMemory::AccessMode mode = QSharedMemory::ReadWrite);
int size() const;
bool attach(QSharedMemory::AccessMode mode = QSharedMemory::ReadWrite);
bool isAttached() const;
bool detach();
void *data();
const void* constData() const;
const void *data() const;
bool lock();
bool unlock();
QSharedMemory::SharedMemoryError error() const;
QString errorString() const;
protected:
#ifdef Q_OS_UNIX
bool initKeyInternal();
bool cleanHandleInternal();
bool createInternal(QSharedMemory::AccessMode mode, int size);
bool attachInternal(QSharedMemory::AccessMode mode);
bool detachInternal();
int handle();
void setErrorString(const QString &function);
#endif
private:
#ifndef Q_OS_UNIX
QSharedMemory m_sharedMemory;
#else
void *m_memory;
int m_size;
QString m_key;
QByteArray m_nativeKey;
QSharedMemory::SharedMemoryError m_error;
QString m_errorString;
QSystemSemaphore m_systemSemaphore;
bool m_lockedByMe;
int m_fileHandle;
bool m_createdByMe;
#endif
};
} // namespace QmlDesigner
#endif // QMLDESIGNER_SHAREDMEMORY_H
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "sharedmemory.h"
namespace QmlDesigner {
SharedMemory::SharedMemory()
{
}
SharedMemory::SharedMemory(const QString &key)
: m_sharedMemory(key)
{
}
SharedMemory::~SharedMemory()
{
}
void SharedMemory::setKey(const QString &key)
{
m_sharedMemory.setKey(key);
}
QString SharedMemory::key() const
{
return m_sharedMemory.key();
}
bool SharedMemory::create(int size, QSharedMemory::AccessMode mode)
{
return m_sharedMemory.create(size, mode);
}
int SharedMemory::size() const
{
return m_sharedMemory.size();
}
bool SharedMemory::attach(QSharedMemory::AccessMode mode)
{
return m_sharedMemory.attach(mode);
}
bool SharedMemory::isAttached() const
{
return m_sharedMemory.isAttached();
}
bool SharedMemory::detach()
{
return m_sharedMemory.detach();
}
void *SharedMemory::data()
{
return m_sharedMemory.data();
}
const void *SharedMemory::constData() const
{
return m_sharedMemory.constData();
}
const void *SharedMemory::data() const
{
return m_sharedMemory.data();
}
bool SharedMemory::lock()
{
return m_sharedMemory.lock();
}
bool SharedMemory::unlock()
{
return m_sharedMemory.unlock();
}
QSharedMemory::SharedMemoryError SharedMemory::error() const
{
return m_sharedMemory.error();
}
QString SharedMemory::errorString() const
{
return m_sharedMemory.errorString();
}
} // namespace QmlDesigner
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "sharedmemory.h"
#include <qdir.h>
#include <qcryptographichash.h>
#include "qplatformdefs.h"
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <sys/types.h>
#include <sys/stat.h>
#ifdef Q_OS_OSX
#include <sys/posix_shm.h>
#endif
#include <fcntl.h>
#include <unistd.h>
#include <private/qcore_unix_p.h>
#ifdef Q_OS_OSX
#define NAME_MAX PSHMNAMLEN
#endif
namespace QmlDesigner {
class SharedMemoryLocker
{
public:
SharedMemoryLocker(SharedMemory *sharedMemory) : m_sharedMemory(sharedMemory)
{
Q_ASSERT(m_sharedMemory);
}
~SharedMemoryLocker()
{
if (m_sharedMemory)
m_sharedMemory->unlock();
}
bool lock()
{
if (m_sharedMemory && m_sharedMemory->lock())
return true;
m_sharedMemory = 0;
return false;
}
bool tryLocker(const QString function) {
if (!lock()) {
m_sharedMemory->m_errorString = QStringLiteral("%1: unable to lock").arg(function);
m_sharedMemory->m_error = QSharedMemory::LockError;
return false;
}
return true;
}
private:
SharedMemory *m_sharedMemory;
};
static QByteArray makePlatformSafeKey(const QString &key)
{
if (key.isEmpty())
return QByteArray();
return QCryptographicHash::hash(key.toLatin1(), QCryptographicHash::Sha1).toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
}
SharedMemory::SharedMemory()
: m_memory(0),
m_size(0),
m_error(QSharedMemory::NoError),
m_systemSemaphore(QString()),
m_lockedByMe(false),
m_fileHandle(-1),
m_createdByMe(false)
{
}
SharedMemory::SharedMemory(const QString &key)
: m_memory(0),
m_size(0),
m_error(QSharedMemory::NoError),
m_systemSemaphore(QString()),
m_lockedByMe(false),
m_fileHandle(-1),
m_createdByMe(false)
{
setKey(key);
}
SharedMemory::~SharedMemory()
{
if (m_memory) {
munmap(m_memory, m_size);
}
if (m_fileHandle != -1) {
close(m_fileHandle);
if (m_createdByMe)
shm_unlink(m_nativeKey);
}
setKey(QString());
}
void SharedMemory::setKey(const QString &key)
{
if (key == m_key && makePlatformSafeKey(key) == m_nativeKey)
return;