Fix deletion of memory tracker

We need to return all of the static variables back to their
original state in the destructor of MemoryTracker

Change-Id: Ie26d4796d9cbbec7ae374d5dd6d30336eab30809
Reviewed-on: http://review.couchbase.org/14542
Reviewed-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Tested-by: Michael Wiederhold <mike@couchbase.com>
Reviewed-on: http://review.couchbase.org/14910
Reviewed-by: Michael Wiederhold <mike@couchbase.com>
Tested-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Don't deallocate the stats name

We changed the stats length to be a maximum of 48 characters in
memcached so the name is no longer allocated on the heap. As a
result we don't need to free it anymore.

Change-Id: I402cfb9cbe533a1cef0c476a0770a0a23a8e40b6
Reviewed-on: http://review.couchbase.org/14543
Reviewed-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Tested-by: Michael Wiederhold <mike@couchbase.com>
Reviewed-on: http://review.couchbase.org/14914
Reviewed-by: Michael Wiederhold <mike@couchbase.com>
Tested-by: Chiyoung Seo <chiyoung.seo@gmail.com>
MB-4951: Update stats using a single thread

Every time we try to add an item to the database, check to see
if there is room in checkpoint queues, try to see of we have
enough memory for backfill, ... we need to check that we aren't
using more memory than we have allocated to use. This process
required getting stats from tcmalloc and getting stats required
going through a few spin locks and this resulted in a lot of
contention. This fix adds a new thread to the MemoryTracker that
will update the stats every 1/4 second in order to relieve
contention around the spin locks in tcmalloc.

Change-Id: Ibdd6fe5b066566a48f96fc348f6b63749e9568f4
Reviewed-on: http://review.couchbase.org/14544
Tested-by: Michael Wiederhold <mike@couchbase.com>
Reviewed-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Reviewed-on: http://review.couchbase.org/14915
Reviewed-by: Michael Wiederhold <mike@couchbase.com>
Tested-by: Chiyoung Seo <chiyoung.seo@gmail.com>
MB-4972: Bucket memory fragmentation has better accounting

We encountered an issue that caused a bucket to run out of memory
due to fragmentation calculation when deleteing buckets. This happened
because we accounted for fragmentation as a percentage of total
allocated memory. This becomes an issue if you have a lot of buckets
using all available memory and then delete all but one of the buckets.
If this scenario occurs then all of the fragmenation from the other
buckets can be given to the remaining bucket and render that bucket
useless. What should happen in this case is the fragmentation should
be spread across then entire processes memory. This change adjusts
the algorithm to do exactly this.

Change-Id: I3497b3f03ed332191a76720156348632b1506598
Reviewed-on: http://review.couchbase.org/14569
Tested-by: Michael Wiederhold <mike@couchbase.com>
Reviewed-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Reviewed-on: http://review.couchbase.org/14916
Reviewed-by: Michael Wiederhold <mike@couchbase.com>
Tested-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Revert "MB-4738 Have tcmalloc support for windows"

This reverts commit 8d3c4cfa5cfd68488095cc7ffeb04bf0bf1596e1.
Since tcmalloc is built and linked in dll, we can do it
in memcached only. But at the same time, still need to define
-DGLIBCXX_FORCE_NEW

Conflicts:

	win32/Makefile.mingw

Change-Id: I883eab2d889005e4afff22007750382d2214f0ec
Reviewed-on: http://review.couchbase.org/14604
Reviewed-by: Steve Yen <steve.yen@gmail.com>
Tested-by: Bin Cui <bin.cui@gmail.com>
Reviewed-on: http://review.couchbase.org/14917
Reviewed-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Tested-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Mb-5037 Make ep-engine tcmalloc aware

Even though we don't need to build it with tcmalloc, we need still
to make it tcmalloc aware during compiling time.

Change-Id: I1d0fe7a89cf70916b86d83121ce9e3d75056f38f
Reviewed-on: http://review.couchbase.org/14816
Reviewed-by: Damien Katz <damien@couchbase.com>
Reviewed-by: Steve Yen <steve.yen@gmail.com>
Tested-by: Bin Cui <bin.cui@gmail.com>
Reviewed-on: http://review.couchbase.org/14918
Reviewed-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Tested-by: Chiyoung Seo <chiyoung.seo@gmail.com>
MB-5010 Reset the thread local data whenever leaving a bucket.

A thread local data is set to a bucket pointer when a given
thread enters into that bucket for supporting the memory
accounting per bucket. This means that the thread local data
should be reset when the thread leaves from its bucket.

Conflicts:

        ep_engine.cc

Change-Id: I02124045c4503bf5d860e1063ab4d0aa11fd43b1
Reviewed-on: http://review.couchbase.org/14773
Tested-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Reviewed-by: Michael Wiederhold <mike@couchbase.com>
Reviewed-on: http://review.couchbase.org/14919
Merge branch 'branch-18'

* branch-18:
  MB-5010 Reset the thread local data whenever leaving a bucket.
  Mb-5037 Make ep-engine tcmalloc aware
  Revert "MB-4738 Have tcmalloc support for windows"
  MB-4972: Bucket memory fragmentation has better accounting
  MB-4951: Update stats using a single thread
  Don't deallocate the stats name
  Fix deletion of memory tracker

Conflicts:
	memory_tracker.cc

Change-Id: I75b0ac6e981242387fd2dc8ba35779a4246fdf6f