From patchwork Thu Nov 22 03:30:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Honnappa Nagarahalli X-Patchwork-Id: 151712 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp218899ljp; Wed, 21 Nov 2018 19:31:46 -0800 (PST) X-Google-Smtp-Source: AJdET5cZkU3a58dvDi4v4JK1I8fjB2W8Kad6hTh5/Z5cr5FuQlyxNz8i5Q5EgDZvRMfCrBIC6sBm X-Received: by 2002:a17:906:e0cb:: with SMTP id gl11-v6mr6799301ejb.92.1542857506348; Wed, 21 Nov 2018 19:31:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542857506; cv=none; d=google.com; s=arc-20160816; b=C2wYN8BeVbVxyQCaY0uhOihbln2+rr0ux4BE0IEQLiVmuyQ/xtT8CN/SKNvvxwY+ja eJid8U19Rs91APbRbgIQDHD6rHqhcvXll2vYZXnJGiSu5ZNy9PHYq5EA14HwGwzh7Uty u/CyiqS9fAJpUuY/dRa6YP6oPsLp/Jn85z0mxi3iuqfY45MJ3bFuRvFViPbPzHloWO5M xvWpfIVSbuEWTcZLSf4MuaX0uylVqpCj3TMuh4y2HmE13mHRoF5holw8rtG41rGAEEQR Nxk05XQQoEhBHtByku3cLaqrtHJNrW4QlTI8U2lVaZ8ifaiyKJ74JBQbxE4buTGdpxYI fM1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:message-id:date:cc:to :from; bh=gF0BUjAav8X/fWyhG9l8A1sjv8pdejEn+GDit0aGiIQ=; b=JlpWNsbOii8gD4gh7ipIeXYsmU/ypnLhlbhdIp1/X+lV7OhHuTQxS5X+cEXwvuhRCV eAtIamgv7sSBBe4tqlcJ7GKTAlEbflCNLrXrSkhb/MDFhrWZMkS853EujOAqBHshmpn6 KCDyQsKIYzm+qb/DTLJGVv1bkErYFuGlvLbfl65XoV2A1FtDAGFeJlWJL/ANEFpy7oR1 l+Qbau6oIq7jXQz2tCnUEwydhG2KOODLVvlUEWTtN4hgEfWCzBHKkPNyjfIyWAqf/iRl 2vRpkKdKdDTdwAjO8KvY92x1r4LfCfjaYORphI1+K7BycOUf/LCDamafClOHHGZi8QB4 FFWw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org Return-Path: Received: from dpdk.org (dpdk.org. [92.243.14.124]) by mx.google.com with ESMTP id t22-v6si12188967ejl.294.2018.11.21.19.31.45; Wed, 21 Nov 2018 19:31:46 -0800 (PST) Received-SPF: pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) client-ip=92.243.14.124; Authentication-Results: mx.google.com; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 211EB1B1F0; Thu, 22 Nov 2018 04:31:45 +0100 (CET) Received: from foss.arm.com (usa-sjc-mx-foss1.foss.arm.com [217.140.101.70]) by dpdk.org (Postfix) with ESMTP id A89961B1EE for ; Thu, 22 Nov 2018 04:31:43 +0100 (CET) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D121E3617; Wed, 21 Nov 2018 19:31:42 -0800 (PST) Received: from qc2400f-1.austin.arm.com (qc2400f-1.austin.arm.com [10.118.14.34]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 798FA3F5CF; Wed, 21 Nov 2018 19:31:42 -0800 (PST) From: Honnappa Nagarahalli To: dev@dpdk.org Cc: nd@arm.com, honnappa.nagarahalli@arm.com, dharmik.thakkar@arm.com, malvika.gupta@arm.com, gavin.hu@arm.com Date: Wed, 21 Nov 2018 21:30:52 -0600 Message-Id: <20181122033055.3431-1-honnappa.nagarahalli@arm.com> X-Mailer: git-send-email 2.17.1 Subject: [dpdk-dev] [RFC 0/3] tqs: add thread quiescent state library X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Lock-less data structures provide scalability and determinism. They enable use cases where locking may not be allowed (for ex: real-time applications). In the following paras, the term 'memory' refers to memory allocated by typical APIs like malloc or anything that is representative of memory, for ex: an index of a free element array. Since these data structures are lock less, the writers and readers are accessing the data structures simultaneously. Hence, while removing an element from a data structure, the writers cannot return the memory to the allocator, without knowing that the readers are not referencing that element/memory anymore. Hence, it is required to separate the operation of removing an element into 2 steps: Delete: in this step, the writer removes the element from the data structure but does not return the associated memory to the allocator. This will ensure that new readers will not get a reference to the removed element. Removing the reference is an atomic operation. Free: in this step, the writer returns the memory to the memory allocator, only after knowing that all the readers have stopped referencing the removed element. This library helps the writer determine when it is safe to free the memory. This library makes use of Thread Quiescent State (TQS). TQS can be defined as 'any point in the thread execution where the thread does not hold a reference to shared memory'. It is upto the application to determine its quiescent state. Let us consider the following diagram: Time --------------------------------------------------> | | RT1 $++++****D1****+++***D2*|**+++|+++**D3*****++++$ | | RT2 $++++****D1****++|+**D2|***++++++**D3*****++++$ | | RT3 $++++****D1****+++***|D2***|++++++**D2*****++++$ | | |<--->| Del | Free | Cannot free memory during this period RTx - Reader thread < and > - Start and end of while(1) loop ***Dx*** - Reader thread is accessing the shared data structure Dx. i.e. critical section. +++ - Reader thread is not accessing any shared data structure. i.e. non critical section or quiescent state. Del - Point in time when the reference to the entry is removed using atomic operation. Free - Point in time when the writer can free the entry. As shown thread RT1 acesses data structures D1, D2 and D3. When it is accessing D2, if the writer has to remove an element from D2, the writer cannot return the memory associated with that element to the allocator. The writer can return the memory to the allocator only after the reader stops referencng D2. In other words, reader thread RT1 has to enter a quiescent state. Similarly, since thread RT3 is also accessing D2, writer has to wait till RT3 enters quiescent state as well. However, the writer does not need to wait for RT2 to enter quiescent state. Thread RT2 was not accessing D2 when the delete operation happened. So, RT2 will not get a reference to the deleted entry. It can be noted that, the critical sections for D2 and D3 are quiescent states for D1. i.e. for a given data structure Dx, any point in the thread execution that does not reference Dx is a quiescent state. For DPDK applications, the start and end of while(1) loop (where no shared data structures are getting accessed) act as perfect quiescent states. This will combine all the shared data structure accesses into a single critical section and keeps the over head introduced by this library to the minimum. However, the length of the critical section and the number of reader threads is proportional to the time taken to identify the end of critical section. So, if the application desires, it should be possible to identify the end of critical section for each data structure. To provide the required flexibility, this library has a concept of TQS variable. The application can create one or more TQS variables to help it track the end of one or more critical sections. The application can create a TQS variable using the API rte_tqs_alloc. It takes a mask of lcore IDs that will report their quiescent states using this variable. This mask can be empty to start with. rte_tqs_register_lcore API will register a reader thread to report its quiescent state. This can be called from any control plane thread or from the reader thread. The application can create a TQS variable with no reader threads and add the threads dynamically using this API. The application can trigger the reader threads to report their quiescent state status by calling the API rte_tqs_start. It is possible for multiple writer threads to query the quiescent state status simultaneously. Hence, rte_tqs_start returns a token to each caller. The application has to call rte_tqs_check API with the token to get the current status. Option to block till all the threads enter the quiescent state is provided. If this API indicates that all the threads have entered the quiescent state, the application can free the deleted entry. The separation of triggering the reporting from querying the status provides the writer threads flexibility to do useful work instead of waiting for the reader threads to enter the quiescent state. rte_tqs_unregister_lcore API will remove a reader thread from reporting its quiescent state using a TQS variable. The rte_tqs_check API will not wait for this reader thread to report the quiescent state status anymore. Finally, a TQS variable can be deleted by calling rte_tqs_free API. Application must make sure that the reader threads are not referencing the TQS variable anymore before deleting it. The reader threads should call rte_tqs_update API to indicate that they entered a quiescent state. This API checks if a writer has triggered a quiescent state query and update the state accordingly. Next Steps: 1) Add more test cases 2) Convert to patch 3) Incorporate feedback from community 4) Add documentation Dharmik Thakkar (1): test/tqs: Add API and functional tests Honnappa Nagarahalli (2): log: add TQS log type tqs: add thread quiescent state library config/common_base | 6 + lib/Makefile | 2 + lib/librte_eal/common/include/rte_log.h | 1 + lib/librte_tqs/Makefile | 23 + lib/librte_tqs/meson.build | 5 + lib/librte_tqs/rte_tqs.c | 249 +++++++++++ lib/librte_tqs/rte_tqs.h | 352 +++++++++++++++ lib/librte_tqs/rte_tqs_version.map | 16 + lib/meson.build | 2 +- mk/rte.app.mk | 1 + test/test/Makefile | 2 + test/test/autotest_data.py | 6 + test/test/meson.build | 5 +- test/test/test_tqs.c | 540 ++++++++++++++++++++++++ 14 files changed, 1208 insertions(+), 2 deletions(-) create mode 100644 lib/librte_tqs/Makefile create mode 100644 lib/librte_tqs/meson.build create mode 100644 lib/librte_tqs/rte_tqs.c create mode 100644 lib/librte_tqs/rte_tqs.h create mode 100644 lib/librte_tqs/rte_tqs_version.map create mode 100644 test/test/test_tqs.c -- 2.17.1