You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2008/08/20 01:11:46 UTC

svn commit: r687192 - in /activemq/activemq-cpp/trunk/src: main/ main/activemq/util/ test/ test/activemq/util/

Author: tabish
Date: Tue Aug 19 16:11:46 2008
New Revision: 687192

URL: http://svn.apache.org/viewvc?rev=687192&view=rev
Log:
https://issues.apache.org/activemq/browse/AMQCPP-189

Adding a memory usage tracking class used to help the Connector track how much memory has been taken up be sent messages that are waiting for a ProducerAck.

Added:
    activemq/activemq-cpp/trunk/src/main/activemq/util/MemoryUsage.cpp
    activemq/activemq-cpp/trunk/src/main/activemq/util/MemoryUsage.h
    activemq/activemq-cpp/trunk/src/test/activemq/util/MemoryUsageTest.cpp
    activemq/activemq-cpp/trunk/src/test/activemq/util/MemoryUsageTest.h
Modified:
    activemq/activemq-cpp/trunk/src/main/Makefile.am
    activemq/activemq-cpp/trunk/src/test/Makefile.am
    activemq/activemq-cpp/trunk/src/test/testRegistry.cpp

Modified: activemq/activemq-cpp/trunk/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/Makefile.am?rev=687192&r1=687191&r2=687192&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/main/Makefile.am Tue Aug 19 16:11:46 2008
@@ -24,6 +24,7 @@
     activemq/util/PrimitiveValueNode.cpp \
     activemq/util/LongSequenceGenerator.cpp \
     activemq/util/URISupport.cpp \
+    activemq/util/MemoryUsage.cpp \
     activemq/cmsutil/DynamicDestinationResolver.cpp \
     activemq/cmsutil/ResourceLifecycleManager.cpp \
     activemq/cmsutil/CmsAccessor.cpp \
@@ -279,6 +280,7 @@
     activemq/util/PrimitiveMap.h \
     activemq/util/PrimitiveList.h \
     activemq/util/URISupport.h \
+    activemq/util/MemoryUsage.h \
     cms/DeliveryMode.h \
     cms/TemporaryQueue.h \
     cms/MapMessage.h \

Added: activemq/activemq-cpp/trunk/src/main/activemq/util/MemoryUsage.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/util/MemoryUsage.cpp?rev=687192&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/util/MemoryUsage.cpp (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/util/MemoryUsage.cpp Tue Aug 19 16:11:46 2008
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MemoryUsage.h"
+#include <decaf/util/concurrent/Concurrent.h>
+
+using namespace activemq;
+using namespace activemq::util;
+using namespace decaf::util;
+using namespace decaf::util::concurrent;
+using namespace std;
+
+////////////////////////////////////////////////////////////////////////////////
+MemoryUsage::MemoryUsage() {
+    this->limit = 0;
+    this->usage = 0;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+MemoryUsage::MemoryUsage( unsigned long long limit ) {
+    this->limit = limit;
+    this->usage = 0;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+MemoryUsage::~MemoryUsage() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void MemoryUsage::waitForSpace() {
+
+    synchronized( &mutex ) {
+        while( this->isFull() ) {
+            mutex.wait();
+        }
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void MemoryUsage::waitForSpace( unsigned int timeout ) {
+
+    if( this->isFull() ) {
+        synchronized( &mutex ) {
+            mutex.wait( timeout );
+        }
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void MemoryUsage::increaseUsage( unsigned long long value ) {
+
+    if( value == 0 ) {
+        return;
+    }
+
+    synchronized( &mutex ) {
+        this->usage += value;
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void MemoryUsage::decreaseUsage( unsigned long long value ) {
+
+    if( value == 0 ) {
+        return;
+    }
+
+    synchronized( &mutex ) {
+        this->usage -= value;
+        mutex.notifyAll();
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool MemoryUsage::isFull() const {
+    bool result = false;
+
+    synchronized( &mutex ) {
+        result = this->usage >= this->limit;
+    }
+
+    return result;
+}

Added: activemq/activemq-cpp/trunk/src/main/activemq/util/MemoryUsage.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/util/MemoryUsage.h?rev=687192&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/activemq/util/MemoryUsage.h (added)
+++ activemq/activemq-cpp/trunk/src/main/activemq/util/MemoryUsage.h Tue Aug 19 16:11:46 2008
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _ACTIVEMQ_UTIL_MEMORYUSAGE_H_
+#define _ACTIVEMQ_UTIL_MEMORYUSAGE_H_
+
+#include <activemq/util/Config.h>
+#include <decaf/util/concurrent/Mutex.h>
+
+namespace activemq {
+namespace util {
+
+    class AMQCPP_API MemoryUsage {
+    private:
+
+        // The physical limit of memory usage this object allows.
+        unsigned long long limit;
+
+        // Amount of memory currently used in Bytes.
+        unsigned long long usage;
+
+        // Mutex to lock usage and wait on.
+        mutable decaf::util::concurrent::Mutex mutex;
+
+    public:
+
+        /**
+         * Default Constructor.
+         */
+        MemoryUsage();
+
+        /**
+         * Creates an instance of an Usage monitor with a set limit.
+         * @param limit - amount in bytes of memory this manager allows.
+         */
+        MemoryUsage( unsigned long long limit );
+
+        virtual ~MemoryUsage();
+
+        /**
+         * Waits forever for more space to be returned to this Usage Manager.
+         */
+        virtual void waitForSpace();
+
+        /**
+         * Waits for more space to be returned to this Usage Manager, times out
+         * when the given timespan in milliseconds elapses.
+         * @param timeout The time to wait for more space.
+         */
+        virtual void waitForSpace( unsigned int timeout );
+
+        /**
+         * Tries to increase the usage by value amount but blocks if this object is
+         * currently full.
+         * @param value Amount of usage in bytes to add.
+         */
+        virtual void enqueueUsage( unsigned long long value ) {
+            waitForSpace();
+            increaseUsage(value);
+        }
+
+        /**
+         * Increases the usage by the value amount which is in bytes
+         * @param value Amount of usage in bytes to add.
+         */
+        virtual void increaseUsage( unsigned long long value );
+
+        /**
+         * Decreases the usage by the value amount.
+         * @param value Amount of space to return to the pool, in Bytes
+         */
+        virtual void decreaseUsage( unsigned long long value );
+
+        /**
+         * Returns true if this Usage instance is full, i.e. Usage >= 100%
+         */
+        virtual bool isFull() const;
+
+        /**
+         * Gets the current usage amount.
+         * @return the amount of bytes currently used.
+         */
+        unsigned long long getUsage() const {
+            return usage;
+        }
+
+        /**
+         * Sets the current usage amount
+         * @param usage - The amount of bytes to tag as used.
+         */
+        void setUsage( unsigned long long usage ) {
+            this->usage = usage;
+        }
+
+        /**
+         * Gets the current limit amount.
+         * @return the amount of bytes that can be used before full.
+         */
+        unsigned long long getLimit() const {
+            return limit;
+        }
+
+        /**
+         * Sets the current limit amount
+         * @param limit - The amount of bytes that can be used before full.
+         */
+        void setLimit( unsigned long long limit ) {
+            this->limit = limit;
+        }
+
+    };
+
+}}
+
+#endif /*_ACTIVEMQ_UTIL_MEMORYUSAGE_H_*/

Modified: activemq/activemq-cpp/trunk/src/test/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/Makefile.am?rev=687192&r1=687191&r2=687192&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/test/Makefile.am Tue Aug 19 16:11:46 2008
@@ -74,6 +74,7 @@
   activemq/util/PrimitiveListTest.cpp \
   activemq/util/PrimitiveMapTest.cpp \
   activemq/util/URISupportTest.cpp \
+  activemq/util/MemoryUsageTest.cpp \
   activemq/util/LongSequenceGeneratorTest.cpp \
   decaf/internal/util/ByteArrayAdapterTest.cpp \
   decaf/internal/nio/ByteArrayPerspectiveTest.cpp \
@@ -187,6 +188,7 @@
   activemq/util/PrimitiveListTest.h \
   activemq/util/PrimitiveMapTest.h \
   activemq/util/URISupportTest.h \
+  activemq/util/MemoryUsageTest.h \
   activemq/util/LongSequenceGeneratorTest.h \
   decaf/internal/util/ByteArrayAdapterTest.h \
   decaf/internal/nio/ByteArrayPerspectiveTest.h \

Added: activemq/activemq-cpp/trunk/src/test/activemq/util/MemoryUsageTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/util/MemoryUsageTest.cpp?rev=687192&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/util/MemoryUsageTest.cpp (added)
+++ activemq/activemq-cpp/trunk/src/test/activemq/util/MemoryUsageTest.cpp Tue Aug 19 16:11:46 2008
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "MemoryUsageTest.h"
+#include <activemq/util/MemoryUsage.h>
+
+#include <decaf/lang/Runnable.h>
+#include <decaf/lang/System.h>
+#include <decaf/lang/Thread.h>
+
+using namespace activemq;
+using namespace activemq::util;
+using namespace decaf::lang;
+
+class UsageRunner : public decaf::lang::Runnable {
+private:
+
+    MemoryUsage* usage;
+
+public:
+
+    UsageRunner( MemoryUsage* usage ) {
+        this->usage = usage;
+    }
+
+    virtual void run(){
+        Thread::sleep( 50 );
+        this->usage->decreaseUsage( this->usage->getUsage() );
+    }
+};
+
+void MemoryUsageTest::testCTors() {
+
+    MemoryUsage usage1;
+    MemoryUsage usage2( 1024 );
+
+    CPPUNIT_ASSERT( usage1.getLimit() == 0 );
+    CPPUNIT_ASSERT( usage2.getLimit() == 1024 );
+
+    CPPUNIT_ASSERT( usage1.getUsage() == 0 );
+    CPPUNIT_ASSERT( usage2.getUsage() == 0 );
+}
+
+void MemoryUsageTest::testUsage() {
+
+    MemoryUsage usage1( 2048 );
+
+    CPPUNIT_ASSERT( !usage1.isFull() );
+    CPPUNIT_ASSERT( usage1.getUsage() == 0 );
+
+    usage1.increaseUsage( 1024 );
+
+    CPPUNIT_ASSERT( !usage1.isFull() );
+    CPPUNIT_ASSERT( usage1.getUsage() == 1024 );
+
+    usage1.decreaseUsage( 512 );
+
+    CPPUNIT_ASSERT( !usage1.isFull() );
+    CPPUNIT_ASSERT( usage1.getUsage() == 512 );
+
+    usage1.setUsage( 2048 );
+
+    CPPUNIT_ASSERT( usage1.isFull() );
+    CPPUNIT_ASSERT( usage1.getUsage() == 2048 );
+
+    usage1.increaseUsage( 1024 );
+    CPPUNIT_ASSERT( usage1.isFull() );
+    CPPUNIT_ASSERT( usage1.getUsage() == 3072 );
+}
+
+void MemoryUsageTest::testTimedWait() {
+
+    MemoryUsage usage( 2048 );
+    usage.increaseUsage( 5072 );
+
+    unsigned long long startTime = System::currentTimeMillis();
+
+    usage.waitForSpace( 100 );
+
+    unsigned long long endTime = System::currentTimeMillis();
+
+    CPPUNIT_ASSERT( endTime - startTime >= 100 );
+}
+
+void MemoryUsageTest::testWait() {
+
+    MemoryUsage usage( 2048 );
+    usage.increaseUsage( 5072 );
+    UsageRunner runner( &usage );
+
+    Thread myThread( &runner );
+    myThread.start();
+
+    usage.waitForSpace();
+    CPPUNIT_ASSERT( usage.getUsage() == 0 );
+}

Added: activemq/activemq-cpp/trunk/src/test/activemq/util/MemoryUsageTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/util/MemoryUsageTest.h?rev=687192&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/util/MemoryUsageTest.h (added)
+++ activemq/activemq-cpp/trunk/src/test/activemq/util/MemoryUsageTest.h Tue Aug 19 16:11:46 2008
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _ACTIVEMQ_UTIL_MEMORYUSAGETEST_H_
+#define _ACTIVEMQ_UTIL_MEMORYUSAGETEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+namespace activemq {
+namespace util {
+
+    class MemoryUsageTest  : public CppUnit::TestFixture {
+
+        CPPUNIT_TEST_SUITE( MemoryUsageTest );
+        CPPUNIT_TEST( testCTors );
+        CPPUNIT_TEST( testUsage );
+        CPPUNIT_TEST( testTimedWait );
+        CPPUNIT_TEST( testWait );
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+
+        MemoryUsageTest() {}
+        virtual ~MemoryUsageTest() {}
+
+        void testCTors();
+        void testUsage();
+        void testTimedWait();
+        void testWait();
+
+    };
+
+}}
+
+#endif /* _ACTIVEMQ_UTIL_MEMORYUSAGETEST_H_ */

Modified: activemq/activemq-cpp/trunk/src/test/testRegistry.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/testRegistry.cpp?rev=687192&r1=687191&r2=687192&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/testRegistry.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/testRegistry.cpp Tue Aug 19 16:11:46 2008
@@ -149,6 +149,8 @@
 CPPUNIT_TEST_SUITE_REGISTRATION( activemq::util::PrimitiveMapTest );
 #include <activemq/util/URISupportTest.h>
 CPPUNIT_TEST_SUITE_REGISTRATION( activemq::util::URISupportTest );
+#include <activemq/util/MemoryUsageTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION( activemq::util::MemoryUsageTest );
 
 #include <decaf/internal/util/ByteArrayAdapterTest.h>
 CPPUNIT_TEST_SUITE_REGISTRATION( decaf::internal::util::ByteArrayAdapterTest );