1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Per-CPU Data</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Unreliable Guide To Locking"><link rel="up" href="Efficiency.html" title="Chapter&#160;9.&#160;Locking Speed"><link rel="prev" href="efficiency-read-copy-update.html" title="Avoiding Locks: Read Copy Update"><link rel="next" href="mostly-hardirq.html" title="Data Which Mostly Used By An IRQ Handler"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Per-CPU Data</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="efficiency-read-copy-update.html">Prev</a>&#160;</td><th width="60%" align="center">Chapter&#160;9.&#160;Locking Speed</th><td width="20%" align="right">&#160;<a accesskey="n" href="mostly-hardirq.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="per-cpu"></a>Per-CPU Data</h2></div></div></div><p>
2      Another technique for avoiding locking which is used fairly
3      widely is to duplicate information for each CPU.  For example,
4      if you wanted to keep a count of a common condition, you could
5      use a spin lock and a single counter.  Nice and simple.
6    </p><p>
7      If that was too slow (it's usually not, but if you've got a
8      really big machine to test on and can show that it is), you
9      could instead use a counter for each CPU, then none of them need
10      an exclusive lock.  See <code class="function">DEFINE_PER_CPU()</code>,
11      <code class="function">get_cpu_var()</code> and
12      <code class="function">put_cpu_var()</code>
13      (<code class="filename">include/linux/percpu.h</code>).
14    </p><p>
15      Of particular use for simple per-cpu counters is the
16      <span class="type">local_t</span> type, and the
17      <code class="function">cpu_local_inc()</code> and related functions,
18      which are more efficient than simple code on some architectures
19      (<code class="filename">include/asm/local.h</code>).
20    </p><p>
21      Note that there is no simple, reliable way of getting an exact
22      value of such a counter, without introducing more locks.  This
23      is not a problem for some uses.
24    </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="efficiency-read-copy-update.html">Prev</a>&#160;</td><td width="20%" align="center"><a accesskey="u" href="Efficiency.html">Up</a></td><td width="40%" align="right">&#160;<a accesskey="n" href="mostly-hardirq.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Avoiding Locks: Read Copy Update&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Data Which Mostly Used By An IRQ Handler</td></tr></table></div></body></html>
25