BUG/MEDIUM: pattern: make the pattern LRU cache thread-local and lockless

As reported in issue #335, a lot of contention happens on the PATLRU lock
when performing expensive regex lookups. This is absurd since the purpose
of the LRU cache was to have a fast cache for expressions, thus the cache
must not be shared between threads and must remain lockless.

This commit makes the LRU cache thread-local and gets rid of the PATLRU
lock. A test with 7 threads on 4 cores climbed from 67kH/s to 369kH/s,
or a scalability factor of 5.5.

Given the huge performance difference and the regression caused to
users migrating from processes to threads, this should be backported at
least to 2.0.

Thanks to Brian Diekelman for his detailed report about this regression.

(cherry picked from commit 403bfbb130f9fb31e52d441ebc1f8227f6883c22)
Signed-off-by: Willy Tarreau <w@1wt.eu>
diff --git a/doc/configuration.txt b/doc/configuration.txt
index f17a557..4494cfa 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -1780,7 +1780,8 @@
   configuration line (including all those loaded from files). It automatically
   invalidates entries which are updated using HTTP actions or on the CLI. The
   default cache size is set to 10000 entries, which limits its footprint to
-  about 5 MB on 32-bit systems and 8 MB on 64-bit systems. There is a very low
+  about 5 MB per process/thread on 32-bit systems and 8 MB per process/thread
+  on 64-bit systems, as caches are thread/process local. There is a very low
   risk of collision in this cache, which is in the order of the size of the
   cache divided by 2^64. Typically, at 10000 requests per second with the
   default cache size of 10000 entries, there's 1% chance that a brute force
diff --git a/include/common/hathreads.h b/include/common/hathreads.h
index b05215b..fa6487c 100644
--- a/include/common/hathreads.h
+++ b/include/common/hathreads.h
@@ -548,7 +548,6 @@
 	SSL_GEN_CERTS_LOCK,
 	PATREF_LOCK,
 	PATEXP_LOCK,
-	PATLRU_LOCK,
 	VARS_LOCK,
 	COMP_POOL_LOCK,
 	LUA_LOCK,
@@ -666,7 +665,6 @@
 	case SSL_GEN_CERTS_LOCK:   return "SSL_GEN_CERTS";
 	case PATREF_LOCK:          return "PATREF";
 	case PATEXP_LOCK:          return "PATEXP";
-	case PATLRU_LOCK:          return "PATLRU";
 	case VARS_LOCK:            return "VARS";
 	case COMP_POOL_LOCK:       return "COMP_POOL";
 	case LUA_LOCK:             return "LUA";
diff --git a/src/pattern.c b/src/pattern.c
index 6bf0c94..ec7e955 100644
--- a/src/pattern.c
+++ b/src/pattern.c
@@ -154,8 +154,7 @@
 /* This is the root of the list of all pattern_ref avalaibles. */
 struct list pattern_reference = LIST_HEAD_INIT(pattern_reference);
 
-static struct lru64_head *pat_lru_tree;
-__decl_spinlock(pat_lru_tree_lock);
+static THREAD_LOCAL struct lru64_head *pat_lru_tree;
 static unsigned long long pat_lru_seed;
 
 /*
@@ -487,15 +486,10 @@
 	if (pat_lru_tree) {
 		unsigned long long seed = pat_lru_seed ^ (long)expr;
 
-		HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 		lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
 				pat_lru_tree, expr, expr->revision);
-		if (!lru) {
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-		}
-		else if (lru->domain) {
+		if (lru && lru->domain) {
 			ret = lru->data;
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 			return ret;
 		}
 	}
@@ -515,10 +509,8 @@
 		}
 	}
 
-	if (lru) {
+	if (lru)
 		lru64_commit(lru, ret, expr, expr->revision, NULL);
-		HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-	}
 
 	return ret;
 }
@@ -534,15 +526,10 @@
 	if (pat_lru_tree) {
 		unsigned long long seed = pat_lru_seed ^ (long)expr;
 
-		HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 		lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
 				pat_lru_tree, expr, expr->revision);
-		if (!lru) {
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-		}
-		else if (lru->domain) {
+		if (lru && lru->domain) {
 			ret = lru->data;
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 			return ret;
 		}
 	}
@@ -559,10 +546,8 @@
 		}
 	}
 
-	if (lru) {
+	if (lru)
 		lru64_commit(lru, ret, expr, expr->revision, NULL);
-		HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-	}
 
 	return ret;
 }
@@ -604,15 +589,10 @@
 	if (pat_lru_tree) {
 		unsigned long long seed = pat_lru_seed ^ (long)expr;
 
-		HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 		lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
 				pat_lru_tree, expr, expr->revision);
-		if (!lru) {
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-		}
-		else if (lru->domain) {
+		if (lru && lru->domain) {
 			ret = lru->data;
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 			return ret;
 		}
 	}
@@ -626,10 +606,8 @@
 		}
 	}
 
-	if (lru) {
+	if (lru)
 		lru64_commit(lru, ret, expr, expr->revision, NULL);
-		HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-	}
 
 	return ret;
 }
@@ -674,15 +652,10 @@
 	if (pat_lru_tree) {
 		unsigned long long seed = pat_lru_seed ^ (long)expr;
 
-		HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 		lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
 				pat_lru_tree, expr, expr->revision);
-		if (!lru) {
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-		}
-		else if (lru->domain) {
+		if (lru && lru->domain) {
 			ret = lru->data;
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 			return ret;
 		}
 	}
@@ -702,10 +675,8 @@
 		break;
 	}
 
-	if (lru) {
+	if (lru)
 		lru64_commit(lru, ret, expr, expr->revision, NULL);
-		HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-	}
 
 	return ret;
 }
@@ -722,15 +693,10 @@
 	if (pat_lru_tree) {
 		unsigned long long seed = pat_lru_seed ^ (long)expr;
 
-		HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 		lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
 				pat_lru_tree, expr, expr->revision);
-		if (!lru) {
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-		}
-		else if (lru->domain) {
+		if (lru && lru->domain) {
 			ret = lru->data;
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 			return ret;
 		}
 	}
@@ -750,10 +716,8 @@
 		break;
 	}
 
-	if (lru) {
+	if (lru)
 		lru64_commit(lru, ret, expr, expr->revision, NULL);
-		HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-	}
 
 	return ret;
 }
@@ -774,15 +738,10 @@
 	if (pat_lru_tree) {
 		unsigned long long seed = pat_lru_seed ^ (long)expr;
 
-		HA_SPIN_LOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 		lru = lru64_get(XXH64(smp->data.u.str.area, smp->data.u.str.data, seed),
 				pat_lru_tree, expr, expr->revision);
-		if (!lru) {
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-		}
-		else if (lru->domain) {
+		if (lru && lru->domain) {
 			ret = lru->data;
-			HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
 			return ret;
 		}
 	}
@@ -816,10 +775,8 @@
 		}
 	}
  leave:
-	if (lru) {
+	if (lru)
 		lru64_commit(lru, ret, expr, expr->revision, NULL);
-		HA_SPIN_UNLOCK(PATLRU_LOCK, &pat_lru_tree_lock);
-	}
 
 	return ret;
 }
@@ -2685,9 +2642,6 @@
 	struct list pr = LIST_HEAD_INIT(pr);
 
 	pat_lru_seed = random();
-	if (global.tune.pattern_cache) {
-		pat_lru_tree = lru64_new(global.tune.pattern_cache);
-	}
 
 	list_for_each_entry(ref, &pattern_reference, list) {
 		if (ref->unique_id == -1) {
@@ -2726,3 +2680,19 @@
 	LIST_ADD(&pr, &pattern_reference);
 	LIST_DEL(&pr);
 }
+
+static int pattern_per_thread_lru_alloc()
+{
+	if (!global.tune.pattern_cache)
+		return 1;
+	pat_lru_tree = lru64_new(global.tune.pattern_cache);
+	return !!pat_lru_tree;
+}
+
+static void pattern_per_thread_lru_free()
+{
+	lru64_destroy(pat_lru_tree);
+}
+
+REGISTER_PER_THREAD_ALLOC(pattern_per_thread_lru_alloc);
+REGISTER_PER_THREAD_FREE(pattern_per_thread_lru_free);