You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2017/07/29 23:49:13 UTC

[trafficserver] branch master updated: Leaving History independently so that each state machine can import it

This is an automated email from the ASF dual-hosted git repository.

jpeach pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new e634659  Leaving History independently so that each state machine can import it
e634659 is described below

commit e634659783bcadbedeb1bca411d36640d18b3b59
Author: scw00 <sc...@apache.org>
AuthorDate: Sun Jul 16 16:34:26 2017 +0800

    Leaving History independently so that each state machine can import it
---
 lib/ts/History.h          |  82 ++++++++++++++++++++++++++++++++++
 lib/ts/Makefile.am        |   2 +
 lib/ts/test_History.cc    | 111 ++++++++++++++++++++++++++++++++++++++++++++++
 proxy/CoreUtils.cc        |   2 +-
 proxy/http/HttpCacheSM.cc |   9 ++--
 proxy/http/HttpPages.cc   |  10 +----
 proxy/http/HttpSM.cc      |  30 +++++--------
 proxy/http/HttpSM.h       |  22 ++-------
 8 files changed, 217 insertions(+), 51 deletions(-)

diff --git a/lib/ts/History.h b/lib/ts/History.h
new file mode 100644
index 0000000..764a3ef
--- /dev/null
+++ b/lib/ts/History.h
@@ -0,0 +1,82 @@
+/** @file
+ *
+ *  A brief file description
+ *
+ *  @section license License
+ *
+ *  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 HISTORY_H_84E66E8E_1D2A_438B_A09A_2CD8FB51B209
+#define HISTORY_H_84E66E8E_1D2A_438B_A09A_2CD8FB51B209
+
+#include "ts/SourceLocation.h"
+
+#define NO_REENTRANT 99999
+#define NO_EVENT NO_REENTRANT
+#define HISTORY_DEFAULT_SIZE 65
+
+struct HistoryEntry {
+  SourceLocation location;
+  unsigned short event = 0;
+  short reentrancy     = 0;
+};
+
+template <unsigned Count> class History
+{
+public:
+  void
+  push_back(const SourceLocation &location, int event, int reentrant = NO_REENTRANT)
+  {
+    if (overflowed()) {
+      return;
+    }
+
+    int pos                 = history_pos++ % Count;
+    history[pos].location   = location;
+    history[pos].event      = (unsigned short)event;
+    history[pos].reentrancy = (short)reentrant;
+  }
+
+  void
+  clear()
+  {
+    ink_zero(history);
+    history_pos = 0;
+  }
+
+  bool
+  overflowed() const
+  {
+    return history_pos >= Count;
+  }
+
+  unsigned int
+  size() const
+  {
+    return history_pos > Count ? Count : history_pos;
+  }
+
+  const HistoryEntry &operator[](unsigned int i) const { return history[i]; }
+
+private:
+  HistoryEntry history[Count];
+
+  unsigned int history_pos = 0;
+};
+
+#endif /* HISTORY_H_84E66E8E_1D2A_438B_A09A_2CD8FB51B209 */
diff --git a/lib/ts/Makefile.am b/lib/ts/Makefile.am
index 955a201..737dbfe 100644
--- a/lib/ts/Makefile.am
+++ b/lib/ts/Makefile.am
@@ -72,6 +72,7 @@ libtsutil_la_SOURCES = \
   HashMD5.h \
   HashSip.cc \
   HashSip.h \
+  History.h \
   HostLookup.cc \
   HostLookup.h \
   hugepages.cc \
@@ -233,6 +234,7 @@ test_X509HostnameValidator_LDADD = libtsutil.la @LIBTCL@ @LIBPCRE@ @OPENSSL_LIBS
 
 test_tsutil_LDADD = libtsutil.la @LIBTCL@ @LIBPCRE@
 test_tsutil_SOURCES = \
+	test_History.cc \
 	test_PriorityQueue.cc \
 	test_Ptr.cc \
 	test_Regex.cc \
diff --git a/lib/ts/test_History.cc b/lib/ts/test_History.cc
new file mode 100644
index 0000000..be17734
--- /dev/null
+++ b/lib/ts/test_History.cc
@@ -0,0 +1,111 @@
+/** @file
+ *
+ *  A brief file description
+ *
+ *  @section license License
+ *
+ *  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 "ts/History.h"
+#include "ts/TestBox.h"
+
+#define REMEMBER(e, r)                             \
+  {                                                \
+    history.push_back(MakeSourceLocation(), e, r); \
+  }
+
+#define SM_REMEMBER(sm, e, r)                          \
+  {                                                    \
+    sm->history.push_back(MakeSourceLocation(), e, r); \
+  }
+
+template <unsigned Count> class SM
+{
+public:
+  History<Count> history;
+};
+
+REGRESSION_TEST(History_test)(RegressionTest *t, int /* atype ATS_UNUSED */, int *pstatus)
+{
+  char buf[128];
+
+  TestBox tb(t, pstatus);
+  *pstatus = REGRESSION_TEST_PASSED;
+
+  History<HISTORY_DEFAULT_SIZE> history;
+  REMEMBER(1, 1);
+  REMEMBER(2, 2);
+  REMEMBER(3, NO_REENTRANT);
+
+  tb.check(history[0].event == 1 && history[0].reentrancy == 1, "Checking that event is 1 and reentrancy is 1");
+  tb.check(history[1].event == 2 && history[1].reentrancy == 2, "Checking that event is 2 and reentrancy is 2");
+  tb.check(history[2].event == 3 && history[2].reentrancy == (short)NO_REENTRANT,
+           "Checking that event is 3 and reentrancy is NO_REENTRANT");
+
+  tb.check(strncmp(history[0].location.str(buf, 128), "test_History.cc:51 (RegressionTest_History_test)",
+                   strlen("test_History.cc:51 (RegressionTest_History_test)")) == 0,
+           "Checking history string");
+  tb.check(strncmp(history[1].location.str(buf, 128), "test_History.cc:52 (RegressionTest_History_test)",
+                   strlen("test_History.cc:52 (RegressionTest_History_test)")) == 0,
+           "Checking history string");
+
+  SM<HISTORY_DEFAULT_SIZE> *sm = new SM<HISTORY_DEFAULT_SIZE>;
+  SM_REMEMBER(sm, 1, 1);
+  SM_REMEMBER(sm, 2, 2);
+  SM_REMEMBER(sm, 3, NO_REENTRANT);
+
+  tb.check(strncmp(sm->history[0].location.str(buf, 128), "test_History.cc:68 (RegressionTest_History_test)",
+                   strlen("test_History.cc:68 (RegressionTest_History_test)")) == 0,
+           "Checking SM's history string");
+  tb.check(strncmp(sm->history[1].location.str(buf, 128), "test_History.cc:69 (RegressionTest_History_test)",
+                   strlen("test_History.cc:69 (RegressionTest_History_test)")) == 0,
+           "Checking SM's history string");
+
+  tb.check(sm->history[0].event == 1 && sm->history[0].reentrancy == 1, "Checking that SM's event is 1 and reentrancy is 1");
+  tb.check(sm->history[1].event == 2 && sm->history[1].reentrancy == 2, "Checking that SM's event is 2 and reentrancy is 2");
+  tb.check(sm->history[2].event == 3 && sm->history[2].reentrancy == (short)NO_REENTRANT,
+           "Checking that SM's event is 3 and reentrancy is NO_REENTRANT");
+
+  SM<2> *sm2 = new SM<2>;
+
+  tb.check(sm2->history.size() == 0, "Checking that history size is 0");
+  tb.check(sm2->history.overflowed() == false, "Checking that history overflowed 1");
+  SM_REMEMBER(sm2, 1, 1);
+  tb.check(sm2->history.size() == 1, "Checking that history size is 1");
+  tb.check(sm2->history.overflowed() == false, "Checking that history overflowed 2");
+  SM_REMEMBER(sm2, 2, 2);
+  tb.check(sm2->history.size() == 2, "Checking that history size is 2");
+  tb.check(sm2->history.overflowed() == true, "Checking that history overflowed 3");
+  SM_REMEMBER(sm2, 3, NO_REENTRANT);
+  tb.check(sm2->history.size() == 2, "Checking that history size is 2");
+  tb.check(sm2->history.overflowed() == true, "Checking that history overflowed 4");
+
+  tb.check(strncmp(sm2->history[0].location.str(buf, 128), "test_History.cc:88 (RegressionTest_History_test)",
+                   strlen("test_History.cc:88 (RegressionTest_History_test)")) == 0,
+           "Checking history string");
+
+  tb.check(strncmp(sm2->history[1].location.str(buf, 128), "test_History.cc:91 (RegressionTest_History_test)",
+                   strlen("test_History.cc:91 (RegressionTest_History_test)")) == 0,
+           "Checking history string");
+
+  sm2->history.clear();
+  tb.check(sm2->history.size() == 0, "Checking that history pos is 0 after clear");
+
+  delete sm;
+  delete sm2;
+}
diff --git a/proxy/CoreUtils.cc b/proxy/CoreUtils.cc
index 72a6186..c6afdc9 100644
--- a/proxy/CoreUtils.cc
+++ b/proxy/CoreUtils.cc
@@ -696,7 +696,7 @@ CoreUtils::dump_history(HttpSM *hsm)
   printf("-------- Begin History -------------\n");
 
   // Loop through the history and dump it
-  for (int i = 0; i < hsm->history_pos; i++) {
+  for (unsigned int i = 0; i < hsm->history.size(); i++) {
     char loc[256];
     int r          = (int)hsm->history[i].reentrancy;
     int e          = (int)hsm->history[i].event;
diff --git a/proxy/http/HttpCacheSM.cc b/proxy/http/HttpCacheSM.cc
index 03ca602..d354297 100644
--- a/proxy/http/HttpCacheSM.cc
+++ b/proxy/http/HttpCacheSM.cc
@@ -34,14 +34,17 @@
 #include "HttpSM.h"
 #include "HttpDebugNames.h"
 
+#define SM_REMEMBER(sm, e, r)                          \
+  {                                                    \
+    sm->history.push_back(MakeSourceLocation(), e, r); \
+  }
+
 #define STATE_ENTER(state_name, event)                                                                                   \
   {                                                                                                                      \
-    REMEMBER(event, -1);                                                                                                 \
+    SM_REMEMBER(master_sm, event, NO_REENTRANT);                                                                         \
     Debug("http_cache", "[%" PRId64 "] [%s, %s]", master_sm->sm_id, #state_name, HttpDebugNames::get_event_name(event)); \
   }
 
-#define REMEMBER(e, r) master_sm->add_history_entry(MakeSourceLocation(), e, r);
-
 HttpCacheAction::HttpCacheAction() : sm(nullptr)
 {
 }
diff --git a/proxy/http/HttpPages.cc b/proxy/http/HttpPages.cc
index 7c994dd..40fd519 100644
--- a/proxy/http/HttpPages.cc
+++ b/proxy/http/HttpPages.cc
@@ -211,17 +211,9 @@ HttpPagesHandler::dump_history(HttpSM *sm)
   resp_add("<h4> History</h4>");
   resp_begin_table(1, 3, 60);
 
-  int size;
-
   // Figure out how big the history is and look
   //  for wrap around
-  if (sm->history_pos > HISTORY_SIZE) {
-    size = HISTORY_SIZE;
-  } else {
-    size = sm->history_pos;
-  }
-
-  for (int i = 0; i < size; i++) {
+  for (unsigned int i = 0; i < sm->history.size(); i++) {
     char buf[256];
     resp_begin_row();
 
diff --git a/proxy/http/HttpSM.cc b/proxy/http/HttpSM.cc
index ea4970a..d0cf37e 100644
--- a/proxy/http/HttpSM.cc
+++ b/proxy/http/HttpSM.cc
@@ -234,19 +234,13 @@ HttpVCTable::cleanup_all()
   }
 }
 
-#define REMEMBER_EVENT_FILTER(e) 1
-
-#define RECORD_FILE_LINE() history[pos].location = MakeSourceLocation();
+#define DebugSM(tag, ...) DebugSpecific(debug_on, tag, __VA_ARGS__)
 
-#define REMEMBER(e, r)                               \
-  {                                                  \
-    if (REMEMBER_EVENT_FILTER(e)) {                  \
-      add_history_entry(MakeSourceLocation(), e, r); \
-    }                                                \
+#define REMEMBER(e, r)                             \
+  {                                                \
+    history.push_back(MakeSourceLocation(), e, r); \
   }
 
-#define DebugSM(tag, ...) DebugSpecific(debug_on, tag, __VA_ARGS__)
-
 #ifdef STATE_ENTER
 #undef STATE_ENTER
 #endif
@@ -256,10 +250,10 @@ HttpVCTable::cleanup_all()
     DebugSM("http", "[%" PRId64 "] [%s, %s]", sm_id, #state_name, HttpDebugNames::get_event_name(event)); \
   }
 
-#define HTTP_SM_SET_DEFAULT_HANDLER(_h) \
-  {                                     \
-    REMEMBER(-1, reentrancy_count);     \
-    default_handler = _h;               \
+#define HTTP_SM_SET_DEFAULT_HANDLER(_h)   \
+  {                                       \
+    REMEMBER(NO_EVENT, reentrancy_count); \
+    default_handler = _h;                 \
   }
 
 static int next_sm_id = 0;
@@ -7037,13 +7031,11 @@ HttpSM::dump_state_on_assert()
 {
   Error("[%" PRId64 "] ------- begin http state dump -------", sm_id);
 
-  int hist_size = this->history_pos;
-  if (this->history_pos > HISTORY_SIZE) {
-    hist_size = HISTORY_SIZE;
-    Error("   History Wrap around. history_pos: %d", this->history_pos);
+  if (history.overflowed()) {
+    Error("   History Wrap around. history size: %d", history.size());
   }
   // Loop through the history and dump it
-  for (int i = 0; i < hist_size; i++) {
+  for (unsigned int i = 0; i < history.size(); i++) {
     char buf[256];
     int r = history[i].reentrancy;
     int e = history[i].event;
diff --git a/proxy/http/HttpSM.h b/proxy/http/HttpSM.h
index 23f061a..46400d6 100644
--- a/proxy/http/HttpSM.h
+++ b/proxy/http/HttpSM.h
@@ -42,6 +42,7 @@
 #include "../ProxyClientTransaction.h"
 #include "HdrUtils.h"
 #include <ts/MemView.h>
+#include <ts/History.h>
 //#include "AuthHttpAdapter.h"
 
 /* Enable LAZY_BUF_ALLOC to delay allocation of buffers until they
@@ -141,7 +142,6 @@ struct HttpTransformInfo {
 
   HttpTransformInfo() : entry(NULL), vc(NULL) {}
 };
-#define HISTORY_SIZE 64
 
 enum {
   HTTP_SM_MAGIC_ALIVE = 0x0000FEED,
@@ -267,7 +267,6 @@ public:
   void txn_hook_prepend(TSHttpHookID id, INKContInternal *cont);
   APIHook *txn_hook_get(TSHttpHookID id);
 
-  void add_history_entry(const SourceLocation &location, int event, int reentrant);
   void add_cache_sm();
   bool is_private();
   bool is_redirect_required();
@@ -300,14 +299,6 @@ public:
 protected:
   int reentrancy_count = 0;
 
-  struct History {
-    SourceLocation location;
-    unsigned short event = 0;
-    short reentrancy     = 0;
-  };
-  History history[HISTORY_SIZE];
-  int history_pos = 0;
-
   HttpTunnel tunnel;
 
   HttpVCTable vc_table;
@@ -322,6 +313,8 @@ public:
   void set_http_schedule(Continuation *);
   int get_http_schedule(int event, void *data);
 
+  History<HISTORY_DEFAULT_SIZE> history;
+
 protected:
   IOBufferReader *ua_buffer_reader     = nullptr;
   IOBufferReader *ua_raw_buffer_reader = nullptr;
@@ -617,15 +610,6 @@ HttpSM::write_response_header_into_buffer(HTTPHdr *h, MIOBuffer *b)
   }
 }
 
-inline void
-HttpSM::add_history_entry(const SourceLocation &location, int event, int reentrant)
-{
-  int pos                 = history_pos++ % HISTORY_SIZE;
-  history[pos].location   = location;
-  history[pos].event      = (unsigned short)event;
-  history[pos].reentrancy = (short)reentrant;
-}
-
 inline int
 HttpSM::find_server_buffer_size()
 {

-- 
To stop receiving notification emails like this one, please contact
['"commits@trafficserver.apache.org" <co...@trafficserver.apache.org>'].