From: Imre Palik <imrep.amz@gmail.com>
To: paulmck@linux.vnet.ibm.com
Cc: perfbook@vger.kernel.org, "Palik, Imre" <imrep.amz@gmail.com>
Subject: [PATCH 3/4] Updating count.tex with new counter code
Date: Mon, 23 Jul 2018 22:07:27 +0200 [thread overview]
Message-ID: <1532376448-15103-4-git-send-email-imrep.amz@gmail.com> (raw)
In-Reply-To: <1532376448-15103-1-git-send-email-imrep.amz@gmail.com>
From: "Palik, Imre" <imrep.amz@gmail.com>
Now count.text reflects the changes made to the counter implementations, to
restrict too eager compilers.
Signed-off-by: Imre Palik <imrep.amz@gmail.com>
---
count/count.tex | 93 +++++++++++++++++++++++++++++----------------------------
1 file changed, 47 insertions(+), 46 deletions(-)
diff --git a/count/count.tex b/count/count.tex
index 561256a..82d4a7f 100644
--- a/count/count.tex
+++ b/count/count.tex
@@ -1003,41 +1003,42 @@ comes at the cost of the additional thread running \co{eventual()}.
5
6 void inc_count(void)
7 {
- 8 counter++;
- 9 }
- 10
- 11 long read_count(void)
- 12 {
- 13 int t;
- 14 long sum;
- 15
- 16 spin_lock(&final_mutex);
- 17 sum = finalcount;
- 18 for_each_thread(t)
- 19 if (counterp[t] != NULL)
- 20 sum += *counterp[t];
- 21 spin_unlock(&final_mutex);
- 22 return sum;
- 23 }
- 24
- 25 void count_register_thread(void)
- 26 {
- 27 int idx = smp_thread_id();
- 28
- 29 spin_lock(&final_mutex);
- 30 counterp[idx] = &counter;
- 31 spin_unlock(&final_mutex);
- 32 }
- 33
- 34 void count_unregister_thread(int nthreadsexpected)
- 35 {
- 36 int idx = smp_thread_id();
- 37
- 38 spin_lock(&final_mutex);
- 39 finalcount += counter;
- 40 counterp[idx] = NULL;
- 41 spin_unlock(&final_mutex);
- 42 }
+ 8 WRITE_ONCE(counter,
+ 9 READ_ONCE(counter) + 1);counter++;
+ 10 }
+ 11
+ 12 long read_count(void)
+ 13 {
+ 14 int t;
+ 15 long sum;
+ 16
+ 17 spin_lock(&final_mutex);
+ 18 sum = finalcount;
+ 19 for_each_thread(t)
+ 20 if (counterp[t] != NULL)
+ 21 sum += *counterp[t];
+ 22 spin_unlock(&final_mutex);
+ 23 return sum;
+ 24 }
+ 25
+ 26 void count_register_thread(void)
+ 27 {
+ 28 int idx = smp_thread_id();
+ 29
+ 30 spin_lock(&final_mutex);
+ 31 counterp[idx] = &counter;
+ 32 spin_unlock(&final_mutex);
+ 33 }
+ 34
+ 35 void count_unregister_thread(int nthreadsexpected)
+ 36 {
+ 37 int idx = smp_thread_id();
+ 38
+ 39 spin_lock(&final_mutex);
+ 40 finalcount += counter;
+ 41 counterp[idx] = NULL;
+ 42 spin_unlock(&final_mutex);
+ 43 }
\end{verbbox}
}
\centering
@@ -1105,18 +1106,18 @@ value of the counter and exiting threads.
} \QuickQuizEnd
The \co{inc_count()} function used by updaters is quite simple, as can
-be seen on lines~6-9.
+be seen on lines~6-10.
The \co{read_count()} function used by readers is a bit more complex.
-Line~16 acquires a lock to exclude exiting threads, and line~21 releases
+Line~17 acquires a lock to exclude exiting threads, and line~22 releases
it.
-Line~17 initializes the sum to the count accumulated by those threads that
-have already exited, and lines~18-20 sum the counts being accumulated
+Line~18 initializes the sum to the count accumulated by those threads that
+have already exited, and lines~19-21 sum the counts being accumulated
by threads currently running.
-Finally, line~22 returns the sum.
+Finally, line~23 returns the sum.
\QuickQuiz{}
- Doesn't the check for \co{NULL} on line~19 of
+ Doesn't the check for \co{NULL} on line~20 of
Listing~\ref{lst:count:Per-Thread Statistical Counters}
add extra branch mispredictions?
Why not have a variable set permanently to zero, and point
@@ -1156,7 +1157,7 @@ Finally, line~22 returns the sum.
\co{inc_count()} fastpath.
} \QuickQuizEnd
-Lines~25-32 show the \co{count_register_thread()} function, which
+Lines~26-33 show the \co{count_register_thread()} function, which
must be called by each thread before its first use of this counter.
This function simply sets up this thread's element of the \co{counterp[]}
array to point to its per-thread \co{counter} variable.
@@ -1177,14 +1178,14 @@ array to point to its per-thread \co{counter} variable.
a hundred or so CPUs, there is no need to get fancy.
} \QuickQuizEnd
-Lines~34-42 show the \co{count_unregister_thread()} function, which
+Lines~35-43 show the \co{count_unregister_thread()} function, which
must be called prior to exit by each thread that previously called
\co{count_register_thread()}.
-Line~38 acquires the lock, and line~41 releases it, thus excluding any
+Line~39 acquires the lock, and line~42 releases it, thus excluding any
calls to \co{read_count()} as well as other calls to
\co{count_unregister_thread()}.
-Line~39 adds this thread's \co{counter} to the global \co{finalcount},
-and then line~40 \co{NULL}s out its \co{counterp[]} array entry.
+Line~40 adds this thread's \co{counter} to the global \co{finalcount},
+and then line~41 \co{NULL}s out its \co{counterp[]} array entry.
A subsequent call to \co{read_count()} will see the exiting thread's
count in the global \co{finalcount}, and will skip the exiting thread
when sequencing through the \co{counterp[]} array, thus obtaining
--
2.7.4
next prev parent reply other threads:[~2018-07-23 20:07 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-23 20:07 Changing non-volatile access to volatile in counter examples Imre Palik
2018-07-23 20:07 ` [PATCH 1/4] Changing counttorture defaults Imre Palik
2018-07-23 20:07 ` [PATCH 2/4] Making the counter implementations safer Imre Palik
2018-07-23 20:07 ` Imre Palik [this message]
2018-07-23 20:07 ` [PATCH 4/4] Regenerating the atomic counter graph on a more modern CPU Imre Palik
2018-07-23 20:55 ` Changing non-volatile access to volatile in counter examples Paul E. McKenney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1532376448-15103-4-git-send-email-imrep.amz@gmail.com \
--to=imrep.amz@gmail.com \
--cc=paulmck@linux.vnet.ibm.com \
--cc=perfbook@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.