<?xml version="1.0" encoding="ANSI_X3.4-1968" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><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.75.2" /><link rel="home" href="index.html" title="Unreliable Guide To Locking" /><link rel="up" href="ch09.html" title="Chapter 9. Locking Speed" /><link rel="prev" href="ch09s02.html" title="Avoiding Locks: Read Copy Update" /><link rel="next" href="ch09s04.html" title="Data Which Mostly Used By An IRQ Handler" /></head><body><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="ch09s02.html">Prev</a> </td><th width="60%" align="center">Chapter 9. Locking Speed</th><td width="20%" align="right"> <a accesskey="n" href="ch09s04.html">Next</a></td></tr></table><hr /></div><div class="sect1" title="Per-CPU Data"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="per-cpu"></a>Per-CPU Data</h2></div></div></div><p> Another technique for avoiding locking which is used fairly widely is to duplicate information for each CPU. For example, if you wanted to keep a count of a common condition, you could use a spin lock and a single counter. Nice and simple. </p><p> If that was too slow (it's usually not, but if you've got a really big machine to test on and can show that it is), you could instead use a counter for each CPU, then none of them need an exclusive lock. See <code class="function">DEFINE_PER_CPU()</code>, <code class="function">get_cpu_var()</code> and <code class="function">put_cpu_var()</code> (<code class="filename">include/linux/percpu.h</code>). </p><p> Of particular use for simple per-cpu counters is the <span class="type">local_t</span> type, and the <code class="function">cpu_local_inc()</code> and related functions, which are more efficient than simple code on some architectures (<code class="filename">include/asm/local.h</code>). </p><p> Note that there is no simple, reliable way of getting an exact value of such a counter, without introducing more locks. This is not a problem for some uses. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch09s02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch09.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch09s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Avoiding Locks: Read Copy Update </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Data Which Mostly Used By An IRQ Handler</td></tr></table></div></body></html>