commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r5030 - in gnuradio/branches/developers/eb/ibu: mblock


From: eb
Subject: [Commit-gnuradio] r5030 - in gnuradio/branches/developers/eb/ibu: mblock/src/lib pmt/src/lib
Date: Mon, 16 Apr 2007 21:35:37 -0600 (MDT)

Author: eb
Date: 2007-04-16 21:35:37 -0600 (Mon, 16 Apr 2007)
New Revision: 5030

Added:
   gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_pool.cc
   gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_pool.h
Modified:
   gnuradio/branches/developers/eb/ibu/mblock/src/lib/mb_message.cc
   gnuradio/branches/developers/eb/ibu/mblock/src/lib/mb_message.h
   gnuradio/branches/developers/eb/ibu/pmt/src/lib/Makefile.am
   gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.cc
   gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_int.h
Log:
pmt and mb_message are now using cache-aligned local allocators.
This is a good start.  If/when we need more performance in this area,
we can go to a per-thread freelist without too much trouble.

Also, we really ought to switch to intrusive shared_ptrs for these
too.  This would get the shared pointer ref count into the same cache
line as the object, and reduce "false sharing."



Modified: gnuradio/branches/developers/eb/ibu/mblock/src/lib/mb_message.cc
===================================================================
--- gnuradio/branches/developers/eb/ibu/mblock/src/lib/mb_message.cc    
2007-04-16 23:34:25 UTC (rev 5029)
+++ gnuradio/branches/developers/eb/ibu/mblock/src/lib/mb_message.cc    
2007-04-17 03:35:37 UTC (rev 5030)
@@ -23,20 +23,35 @@
 #include <config.h>
 #endif
 #include <mb_message.h>
-#include <boost/pool/pool.hpp>
-#include <boost/pool/singleton_pool.hpp>
 #include <stdio.h>
+#include <pmt_pool.h>
 
 static const int CACHE_LINE_SIZE = 64;         // good guess
 
-#define ROUNDUP(x, stride) ((((x) + (stride) - 1)/(stride)) * (stride))
 
-struct msg_pool_tag {};
+#if MB_MESSAGE_LOCAL_ALLOCATOR
 
-typedef boost::singleton_pool<msg_pool_tag,
-                             ROUNDUP(sizeof(mb_message), CACHE_LINE_SIZE)> 
msg_pool;
+static pmt_pool global_msg_pool(sizeof(mb_message), CACHE_LINE_SIZE);
 
+void *
+mb_message::operator new(size_t size)
+{
+  void *p = global_msg_pool.malloc();
 
+  // fprintf(stderr, "mb_message::new p = %p\n", p);
+  assert((reinterpret_cast<intptr_t>(p) & (CACHE_LINE_SIZE - 1)) == 0);
+  return p;
+}
+
+void
+mb_message::operator delete(void *p, size_t size)
+{
+  global_msg_pool.free(p);
+}
+
+#endif
+
+
 mb_message_sptr
 mb_make_message(pmt_t signal, pmt_t data, pmt_t metadata, mb_pri_t priority)
 {
@@ -66,23 +81,3 @@
   
   return os;
 }
-
-
-#if 1
-void *
-mb_message::operator new(size_t size)
-{
-  void *p = msg_pool::malloc();
-
-  // FIXME fix alignment.
-  // fprintf(stderr, "mb_message::new p = %p\n", p);
-  // assert((reinterpret_cast<intptr_t>(p) & (CACHE_LINE_SIZE - 1)) == 0);
-  return p;
-}
-
-void
-mb_message::operator delete(void *p, size_t size)
-{
-  msg_pool::free(p);
-}
-#endif

Modified: gnuradio/branches/developers/eb/ibu/mblock/src/lib/mb_message.h
===================================================================
--- gnuradio/branches/developers/eb/ibu/mblock/src/lib/mb_message.h     
2007-04-16 23:34:25 UTC (rev 5029)
+++ gnuradio/branches/developers/eb/ibu/mblock/src/lib/mb_message.h     
2007-04-17 03:35:37 UTC (rev 5030)
@@ -24,6 +24,8 @@
 #include <mb_common.h>
 #include <iosfwd>
 
+#define MB_MESSAGE_LOCAL_ALLOCATOR 1   // define to 0 or 1
+
 class mb_message;
 typedef boost::shared_ptr<mb_message> mb_message_sptr;
 
@@ -68,9 +70,10 @@
 
   void set_port_id(pmt_t port_id){ d_port_id = port_id; }
 
+#if (MB_MESSAGE_LOCAL_ALLOCATOR)
   void *operator new(size_t);
   void operator delete(void *, size_t);
-
+#endif
 };
 
 std::ostream& operator<<(std::ostream& os, const mb_message &msg);

Modified: gnuradio/branches/developers/eb/ibu/pmt/src/lib/Makefile.am
===================================================================
--- gnuradio/branches/developers/eb/ibu/pmt/src/lib/Makefile.am 2007-04-16 
23:34:25 UTC (rev 5029)
+++ gnuradio/branches/developers/eb/ibu/pmt/src/lib/Makefile.am 2007-04-17 
03:35:37 UTC (rev 5030)
@@ -21,7 +21,7 @@
 
 include $(top_srcdir)/Makefile.common
 
-INCLUDES = $(BOOST_CFLAGS) $(CPPUNIT_INCLUDES)
+INCLUDES = $(DEFINES) $(OMNITHREAD_INCLUDES) $(BOOST_CFLAGS) 
$(CPPUNIT_INCLUDES) 
 
 TESTS = test_pmt
 
@@ -62,6 +62,7 @@
 libpmt_la_SOURCES =                    \
        pmt.cc                          \
        pmt_io.cc                       \
+       pmt_pool.cc                     \
        pmt_serialize.cc                \
        pmt_unv.cc                      
 
@@ -70,6 +71,7 @@
 
 # link the library against the c++ standard library
 libpmt_la_LIBADD =                     \
+       $(OMNITHREAD_LA)                \
        -lstdc++                        
 
 include_HEADERS =                      \

Modified: gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.cc
===================================================================
--- gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.cc      2007-04-16 
23:34:25 UTC (rev 5029)
+++ gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt.cc      2007-04-17 
03:35:37 UTC (rev 5030)
@@ -26,38 +26,31 @@
 #include <vector>
 #include <pmt.h>
 #include "pmt_int.h"
-#include <boost/pool/pool.hpp>
-#include <boost/pool/singleton_pool.hpp>
 #include <stdio.h>
+#include <pmt_pool.h>
 
-
 static const int CACHE_LINE_SIZE = 64;         // good guess
 
-#define ROUNDUP(x, stride) ((((x) + (stride) - 1)/(stride)) * (stride))
+# if (PMT_LOCAL_ALLOCATOR)
 
-struct pmt_pool_tag {};
+static pmt_pool global_pmt_pool(sizeof(pmt_pair), CACHE_LINE_SIZE);
 
-typedef boost::singleton_pool<pmt_pool_tag,
-                             ROUNDUP(sizeof(pmt_pair), CACHE_LINE_SIZE)> 
msg_pool;
-
-
-#if 1
 void *
 pmt_base::operator new(size_t size)
 {
-  void *p = msg_pool::malloc();
+  void *p = global_pmt_pool.malloc();
 
-  // FIXME fix alignment.
   // fprintf(stderr, "pmt_base::new p = %p\n", p);
-  // assert((reinterpret_cast<intptr_t>(p) & (CACHE_LINE_SIZE - 1)) == 0);
+  assert((reinterpret_cast<intptr_t>(p) & (CACHE_LINE_SIZE - 1)) == 0);
   return p;
 }
 
 void
 pmt_base::operator delete(void *p, size_t size)
 {
-  msg_pool::free(p);
+  global_pmt_pool.free(p);
 }
+
 #endif
 
 

Modified: gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_int.h
===================================================================
--- gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_int.h   2007-04-16 
23:34:25 UTC (rev 5029)
+++ gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_int.h   2007-04-17 
03:35:37 UTC (rev 5030)
@@ -31,6 +31,8 @@
  * See pmt.h for the public interface
  */
 
+#define PMT_LOCAL_ALLOCATOR 1          // define to 0 or 1
+
 class pmt_base : boost::noncopyable {
 protected:
   pmt_base(){};
@@ -62,8 +64,10 @@
   virtual bool is_c32vector() const { return false; }
   virtual bool is_c64vector() const { return false; }
 
+# if (PMT_LOCAL_ALLOCATOR)
   void *operator new(size_t);
   void operator delete(void *, size_t);
+#endif
 };
 
 class pmt_bool : public pmt_base

Added: gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_pool.cc
===================================================================
--- gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_pool.cc                 
        (rev 0)
+++ gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_pool.cc 2007-04-17 
03:35:37 UTC (rev 5030)
@@ -0,0 +1,96 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2007 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <pmt_pool.h>
+#include <algorithm>
+#include <stdint.h>
+
+static inline size_t
+ROUNDUP(size_t x, size_t stride)
+{
+  return ((((x) + (stride) - 1)/(stride)) * (stride));
+}
+
+pmt_pool::pmt_pool(size_t itemsize, size_t alignment, size_t allocation_size)
+  : d_itemsize(ROUNDUP(itemsize, alignment)),
+    d_alignment(alignment),
+    d_allocation_size(std::max(allocation_size, 16 * itemsize)),
+    d_freelist(0)
+{
+}
+
+pmt_pool::~pmt_pool()
+{
+  for (unsigned int i = 0; i < d_allocations.size(); i++){
+    delete [] d_allocations[i];
+  }
+}
+
+void *
+pmt_pool::malloc()
+{
+  omni_mutex_lock l(d_mutex);
+  item *p;
+
+  if (d_freelist){     // got something?
+    p = d_freelist;
+    d_freelist = p->d_next;
+    return p;
+  }
+
+  // allocate a new chunk
+  char *alloc = new char[d_allocation_size + d_alignment - 1];
+  d_allocations.push_back(alloc);
+
+  // get the alignment we require
+  char *start = (char *)(((uintptr_t)alloc + d_alignment-1) & -d_alignment);
+  char *end = alloc + d_allocation_size + d_alignment - 1;
+  size_t n = (end - start) / d_itemsize;
+
+  // link the new items onto the free list.
+  p = (item *) start;
+  for (size_t i = 0; i < n; i++){
+    p->d_next = d_freelist;
+    d_freelist = p;
+    p = (item *)((char *) p + d_itemsize);
+  }
+
+  // now return the first one
+  p = d_freelist;
+  d_freelist = p->d_next;
+  return p;
+}
+
+void
+pmt_pool::free(void *foo)
+{
+  if (!foo)
+    return;
+
+  omni_mutex_lock l(d_mutex);
+
+  item *p = (item *) foo;
+  p->d_next = d_freelist;
+  d_freelist = p;
+}

Added: gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_pool.h
===================================================================
--- gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_pool.h                  
        (rev 0)
+++ gnuradio/branches/developers/eb/ibu/pmt/src/lib/pmt_pool.h  2007-04-17 
03:35:37 UTC (rev 5030)
@@ -0,0 +1,61 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2007 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef INCLUDED_PMT_POOL_H
+#define INCLUDED_PMT_POOL_H
+
+#include <cstddef>
+#include <omnithread.h>
+#include <vector>
+
+/*!
+ * \brief very simple thread-safe fixed-size allocation pool
+ *
+ * FIXME may want to go to global allocation with per-thread free list.
+ * This would eliminate virtually all lock contention.
+ */
+class pmt_pool {
+
+  struct item {
+    struct item        *d_next;
+  };
+  
+  omni_mutex         d_mutex;
+  
+  size_t             d_itemsize;
+  size_t             d_alignment;
+  size_t             d_allocation_size;
+  item              *d_freelist;
+  std::vector<char *> d_allocations;
+
+public:
+  /*!
+   * \param itemsize size in bytes of the items to be allocated.
+   * \param alignment alignment in bytes of all objects to be allocated (must 
be power-of-2).
+   * \param allocation_size number of bytes to allocate at a time from the 
underlying allocator.
+   */
+  pmt_pool(size_t itemsize, size_t alignment = 16, size_t allocation_size = 
4096);
+  ~pmt_pool();
+
+  void *malloc();
+  void free(void *p);
+};
+
+#endif /* INCLUDED_PMT_POOL_H */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]