forked from mlco2/codecarbon
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethodology.html
More file actions
429 lines (406 loc) · 33.1 KB
/
methodology.html
File metadata and controls
429 lines (406 loc) · 33.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="./">
<head>
<meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Methodology — CodeCarbon 3.0.4 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=03e43079" />
<link rel="stylesheet" type="text/css" href="_static/css/theme.css?v=e59714d7" />
<script src="_static/jquery.js?v=5d32c60e"></script>
<script src="_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="_static/documentation_options.js?v=50b65552"></script>
<script src="_static/doctools.js?v=9bcbadda"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script src="_static/js/theme.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Model Comparisons" href="model_examples.html" />
<link rel="prev" title="Motivation" href="motivation.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="index.html" class="icon icon-home">
CodeCarbon
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Introduction</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="motivation.html">Motivation</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Methodology</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#carbon-intensity">Carbon Intensity</a></li>
<li class="toctree-l2"><a class="reference internal" href="#power-usage">Power Usage</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#gpu">GPU</a></li>
<li class="toctree-l3"><a class="reference internal" href="#ram">RAM</a></li>
<li class="toctree-l3"><a class="reference internal" href="#cpu">CPU</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#cpu-hardware">CPU hardware</a></li>
<li class="toctree-l2"><a class="reference internal" href="#rapl-metrics">RAPL Metrics</a></li>
<li class="toctree-l2"><a class="reference internal" href="#cpu-metrics-priority">CPU metrics priority</a></li>
<li class="toctree-l2"><a class="reference internal" href="#references">References</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#how-codecarbon-works">How CodeCarbon Works</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#estimation-of-equivalent-usage-emissions">Estimation of Equivalent Usage Emissions</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#car-usage">Car Usage</a></li>
<li class="toctree-l3"><a class="reference internal" href="#tv-usage">TV Usage</a></li>
<li class="toctree-l3"><a class="reference internal" href="#us-citizen-weekly-emissions">US Citizen Weekly Emissions</a></li>
<li class="toctree-l3"><a class="reference internal" href="#calculation-formula">Calculation Formula</a></li>
<li class="toctree-l3"><a class="reference internal" href="#source-code">Source Code</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="model_examples.html">Model Comparisons</a></li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Getting Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="installation.html">Installing CodeCarbon</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage.html">Quickstart</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage.html#configuration">Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">CodeCarbon API</a></li>
<li class="toctree-l1"><a class="reference internal" href="parameters.html">Parameters</a></li>
<li class="toctree-l1"><a class="reference internal" href="examples.html">Examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="comet.html">Comet Integration</a></li>
<li class="toctree-l1"><a class="reference internal" href="advanced_installation.html">Advanced Installation</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Logging</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="output.html">Output</a></li>
<li class="toctree-l1"><a class="reference internal" href="to_logger.html">Collecting emissions to a logger</a></li>
<li class="toctree-l1"><a class="reference internal" href="visualize.html">Visualize</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">CodeCarbon</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item active">Methodology</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/methodology.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="methodology">
<span id="id1"></span><h1>Methodology<a class="headerlink" href="#methodology" title="Link to this heading"></a></h1>
<p>Carbon dioxide (CO₂) emissions, expressed as kilograms of CO₂-equivalents [CO₂eq], are the product of two main factors :</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>C = Carbon Intensity of the electricity consumed for computation: quantified as g of CO₂ emitted per kilowatt-hour of electricity.
E = Energy Consumed by the computational infrastructure: quantified as kilowatt-hours.
</pre></div>
</div>
<p>Carbon dioxide emissions (CO₂eq) can then be calculated as <code class="docutils literal notranslate"><span class="pre">C</span> <span class="pre">*</span> <span class="pre">E</span></code></p>
<section id="carbon-intensity">
<h2>Carbon Intensity<a class="headerlink" href="#carbon-intensity" title="Link to this heading"></a></h2>
<p>Carbon Intensity of the consumed electricity is calculated as a weighted average of the emissions from the different
energy sources that are used to generate electricity, including fossil fuels and renewables. In this toolkit, the fossil fuels coal, petroleum, and natural gas are associated with specific carbon intensities: a known amount of carbon dioxide is emitted for each kilowatt-hour of electricity generated. Renewable or low-carbon fuels include solar power, hydroelectricity, biomass, geothermal, and more. The nearby energy grid contains a mixture of fossil fuels and low-carbon energy sources, called the Energy Mix. Based on the mix of energy sources in the local grid, the Carbon Intensity of the electricity consumed can be computed.</p>
<a class="reference internal image-reference" href="_images/grid_energy_mix.png"><img alt="Grid Energy Mix" class="align-center" src="_images/grid_energy_mix.png" style="width: 350px; height: 300px;" />
</a>
<p>When available, CodeCarbon uses global carbon intensity of electricity per cloud provider ( <a class="reference external" href="https://github.com/mlco2/codecarbon/blob/master/codecarbon/data/cloud/impact.csv">here</a> ) or per country ( <a class="reference external" href="https://github.com/mlco2/codecarbon/blob/master/codecarbon/data/private_infra/global_energy_mix.json">here</a> ).</p>
<p>If we don’t have the global carbon intensity or electricity of a country, but we have its electricity mix, we used to compute the carbon intensity of electricity using this table:</p>
<table class="docutils align-center" id="id6">
<caption><span class="caption-text">Carbon Intensity Across Energy Sources</span><a class="headerlink" href="#id6" title="Link to this table"></a></caption>
<colgroup>
<col style="width: 50.0%" />
<col style="width: 50.0%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Energy Source</p></th>
<th class="head"><p>Carbon Intensity (kg/MWh)</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Coal</p></td>
<td><p>995</p></td>
</tr>
<tr class="row-odd"><td><p>Petroleum</p></td>
<td><p>816</p></td>
</tr>
<tr class="row-even"><td><p>Natural Gas</p></td>
<td><p>743</p></td>
</tr>
<tr class="row-odd"><td><p>Geothermal</p></td>
<td><p>38</p></td>
</tr>
<tr class="row-even"><td><p>Hydroelectricity</p></td>
<td><p>26</p></td>
</tr>
<tr class="row-odd"><td><p>Nuclear</p></td>
<td><p>29</p></td>
</tr>
<tr class="row-even"><td><p>Solar</p></td>
<td><p>48</p></td>
</tr>
<tr class="row-odd"><td><p>Wind</p></td>
<td><p>26</p></td>
</tr>
</tbody>
</table>
<dl class="simple">
<dt>Sources:</dt><dd><ul class="simple">
<li><p><a class="reference external" href="https://github.com/responsibleproblemsolving/energy-usage#conversion-to-co2">for fossil energies</a></p></li>
<li><p><a class="reference external" href="http://www.world-nuclear.org/uploadedFiles/org/WNA/Publications/Working_Group_Reports/comparison_of_lifecycle.pdf">for renewables energies</a></p></li>
</ul>
</dd>
</dl>
<p>Then, for example, if the Energy Mix of the Grid Electricity is 25% Coal, 35% Petroleum, 26% Natural Gas and 14% Nuclear:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>Net Carbon Intensity = 0.25 * 995 + 0.35 * 816 + 0.26 * 743 + 0.14 * 29 = 731.59 kgCO₂/kWh
</pre></div>
</div>
<p>But it doesn’t happen anymore because Our World in Data now provides the global carbon intensity of electricity per country ( <a class="reference external" href="https://ourworldindata.org/grapher/carbon-intensity-electricity#explore-the-data">source</a> ). Some countries are missing data for last year, so we use the previous year data available.</p>
<p>If ever we have neither the global carbon intensity of a country nor it’s electricity mix, we apply a world average of 475 gCO2.eq/KWh ( <a class="reference external" href="https://www.iea.org/reports/global-energy-co2-status-report-2019/emissions">source</a> ).</p>
<p>As you can see, we try to be as accurate as possible in estimating carbon intensity of electricity. Still there is room for improvement and all contributions are welcome.</p>
</section>
<section id="power-usage">
<h2>Power Usage<a class="headerlink" href="#power-usage" title="Link to this heading"></a></h2>
<p>Power supply to the underlying hardware is tracked at frequent time intervals. This is a configurable parameter
<code class="docutils literal notranslate"><span class="pre">measure_power_secs</span></code>, with default value 15 seconds, that can be passed when instantiating the emissions’ tracker.</p>
<p>Currently, the package supports the following hardware infrastructure.</p>
<section id="gpu">
<h3>GPU<a class="headerlink" href="#gpu" title="Link to this heading"></a></h3>
<p>Tracks Nvidia GPUs energy consumption using <code class="docutils literal notranslate"><span class="pre">pynvml</span></code> library (installed with the package).</p>
</section>
<section id="ram">
<h3>RAM<a class="headerlink" href="#ram" title="Link to this heading"></a></h3>
<p>CodeCarbon v2 uses a 3 Watts for 8 GB ratio <a class="reference external" href="https://www.crucial.com/support/articles-faq-memory/how-much-power-does-memory-use">source</a> .</p>
<p>But this is not a good measure because it doesn’t take into account the number of RAM slots used in the machine, that really drive the power consumption, not the amount of RAM.
For example, in servers you could have thousands of GB of RAM but the power consumption would not be proportional to the amount of memory used, but to the number of memory modules used.</p>
<p>Old machine could use 2 Mb memory stick, where modern servers will use 128 Mb memory stick.</p>
<p>So, in CodeCarbon v3 we switch to using 5 Watts for each RAM slot. The energy consumption is calculated as follows:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>RAM Power Consumption = 5 Watts * Number of RAM slots used
</pre></div>
</div>
<p>But getting the number of RAM slots used is not possible as you need root access to get the number of RAM slots used. So we use an heuristic based on the RAM size.</p>
<p>For example keep a minimum of 2 modules. Except for ARM CPU like rapsberry pi where we will consider a 3W constant. Then consider the max RAM per module is 128GB and that RAM module only exist in power of 2 (2, 4, 8, 16, 32, 64, 128). So we can estimate the power consumption of the RAM by the number of modules used.</p>
<ul class="simple">
<li><p>For ARM CPUs (like Raspberry Pi), a constant 3W will be used as the minimum power</p></li>
<li><p>Base power per DIMM is 5W for x86 systems and 1.5W for ARM systems</p></li>
<li><p>For standard systems (up to 4 DIMMs): linear scaling at full power per DIMM</p></li>
<li><p>For medium systems (5-8 DIMMs): decreasing efficiency (90% power per additional DIMM)</p></li>
<li><p>For large systems (9-16 DIMMs): further reduced efficiency (80% power per additional DIMM)</p></li>
<li><p>For very large systems (17+ DIMMs): highest efficiency (70% power per additional DIMM)</p></li>
<li><p>Ensures at least 10W for x86 systems (assuming 2 DIMMs at minimum)</p></li>
<li><p>Ensures at least 3W for ARM systems</p></li>
</ul>
<p>Example Power Estimates:</p>
<ul class="simple">
<li><p><strong>Small laptop (8GB RAM)</strong>: ~10W (2 DIMMs at 5W each)</p></li>
<li><p><strong>Desktop (32GB RAM)</strong>: ~20W (4 DIMMs at 5W each)</p></li>
<li><p><strong>Desktop (64GB RAM)</strong>: ~20W (4 DIMMs at 5W each), the same as 32GB</p></li>
<li><p><strong>Small server (128GB RAM)</strong>: ~40W (8 DIMMs with efficiency scaling)</p></li>
<li><p><strong>Large server (1TB RAM)</strong>: ~40W (using 8x128GB DIMMs with high efficiency scaling)</p></li>
</ul>
<p>This approach significantly improves the accuracy for large servers by recognizing that RAM power consumption doesn’t scale linearly with capacity, but rather with the number of physical modules. Since we don’t have direct access to the actual DIMM configuration, this heuristic provides a more reasonable estimate than the previous linear model.</p>
<p>If you know the exact RAM power consumption of your system, then provide it using the <cite>force_ram_power</cite> parameter, which will override the automatic estimation.</p>
<p>For example, in a Ubuntu machine, you can get the number of RAM slots used with the following command:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>lshw<span class="w"> </span>-C<span class="w"> </span>memory<span class="w"> </span>-short<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>DIMM
/0/37/0<span class="w"> </span>memory<span class="w"> </span>4GiB<span class="w"> </span>DIMM<span class="w"> </span>DDR4<span class="w"> </span>Synchrone<span class="w"> </span>Unbuffered<span class="w"> </span><span class="o">(</span>Unregistered<span class="o">)</span><span class="w"> </span><span class="m">2400</span><span class="w"> </span>MHz<span class="w"> </span><span class="o">(</span><span class="m">0</span>,4<span class="w"> </span>ns<span class="o">)</span>
/0/37/1<span class="w"> </span>memory<span class="w"> </span>4GiB<span class="w"> </span>DIMM<span class="w"> </span>DDR4<span class="w"> </span>Synchrone<span class="w"> </span>Unbuffered<span class="w"> </span><span class="o">(</span>Unregistered<span class="o">)</span><span class="w"> </span><span class="m">2400</span><span class="w"> </span>MHz<span class="w"> </span><span class="o">(</span><span class="m">0</span>,4<span class="w"> </span>ns<span class="o">)</span>
/0/37/2<span class="w"> </span>memory<span class="w"> </span>4GiB<span class="w"> </span>DIMM<span class="w"> </span>DDR4<span class="w"> </span>Synchrone<span class="w"> </span>Unbuffered<span class="w"> </span><span class="o">(</span>Unregistered<span class="o">)</span><span class="w"> </span><span class="m">2400</span><span class="w"> </span>MHz<span class="w"> </span><span class="o">(</span><span class="m">0</span>,4<span class="w"> </span>ns<span class="o">)</span>
/0/37/3<span class="w"> </span>memory<span class="w"> </span>4GiB<span class="w"> </span>DIMM<span class="w"> </span>DDR4<span class="w"> </span>Synchrone<span class="w"> </span>Unbuffered<span class="w"> </span><span class="o">(</span>Unregistered<span class="o">)</span><span class="w"> </span><span class="m">2400</span><span class="w"> </span>MHz<span class="w"> </span><span class="o">(</span><span class="m">0</span>,4<span class="w"> </span>ns<span class="o">)</span>
</pre></div>
</div>
<p>Here we count 4 RAM slots used, so the power consumption will be 4 x 5 = 20 Watts, just add <cite>force_ram_power=20</cite> to the init of CodeCarbon.</p>
</section>
<section id="cpu">
<h3>CPU<a class="headerlink" href="#cpu" title="Link to this heading"></a></h3>
<ul class="simple">
<li><p><strong>On Windows or Mac (Intel)</strong></p></li>
</ul>
<p>Tracks Intel processors energy consumption using the <code class="docutils literal notranslate"><span class="pre">Intel</span> <span class="pre">Power</span> <span class="pre">Gadget</span></code>. You need to install it yourself from this <a class="reference external" href="https://www.intel.com/content/www/us/en/developer/articles/tool/power-gadget.html">source</a> . But has been discontinued. There is a discussion about it on <a class="reference external" href="https://github.com/mlco2/codecarbon/issues/457">github issues #457</a>.</p>
<ul class="simple">
<li><p><strong>Apple Silicon Chips (M1, M2)</strong></p></li>
</ul>
<p>Apple Silicon Chips contain both the CPU and the GPU.</p>
<p>Codecarbon tracks Apple Silicon Chip energy consumption using <code class="docutils literal notranslate"><span class="pre">powermetrics</span></code>. It should be available natively on any mac.
However, this tool is only usable with <code class="docutils literal notranslate"><span class="pre">sudo</span></code> rights and to our current knowledge, there are no other options to track the energy consumption of the Apple Silicon Chip without administrative rights
(if you know of any solution for this do not hesitate and <a class="reference external" href="https://github.com/mlco2/codecarbon/issues/">open an issue with your proposed solution</a>).</p>
<p>To give sudo rights without having to enter a password each time, you can modify the sudoers file with the following command:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>sudo<span class="w"> </span>visudo
</pre></div>
</div>
<p>Then add the following line at the end of the file:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>username<span class="w"> </span><span class="nv">ALL</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">(</span>root<span class="o">)</span><span class="w"> </span>NOPASSWD:<span class="w"> </span>/usr/bin/powermetrics
</pre></div>
</div>
<p>If you do not want to give sudo rights to your user, then CodeCarbon will fall back to constant mode to measure CPU energy consumption.</p>
<ul class="simple">
<li><p><strong>On Linux</strong></p></li>
</ul>
<p>Tracks Intel and AMD processor energy consumption from Intel RAPL files at <code class="docutils literal notranslate"><span class="pre">/sys/class/powercap/intel-rapl/subsystem</span></code> ( <a class="reference external" href="https://web.eece.maine.edu/~vweaver/projects/rapl/">reference</a> ).
All CPUs listed in this directory will be tracked.</p>
<p><em>Note</em>: The Power Consumption will be tracked only if the RAPL files exist at the above-mentioned path and if the user has the necessary permissions to read them.</p>
</section>
</section>
<section id="cpu-hardware">
<h2>CPU hardware<a class="headerlink" href="#cpu-hardware" title="Link to this heading"></a></h2>
<p>The CPU die is the processing unit itself. It’s a piece of semiconductor that has been sculpted/etched/deposited by various manufacturing processes into a net of logic blocks that do stuff that makes computing possible1. The processor package is what you get when you buy a single processor. It contains one or more dies, plastic/ceramic housing for dies and gold-plated contacts that match those on your motherboard.</p>
<p>In Linux kernel, energy_uj is a current energy counter in micro joules. It is used to measure CPU core’s energy consumption.</p>
<p>Micro joules is then converted in kWh, with formulas kWh=energy * 10 ** (-6) * 2.77778e-7</p>
<p>For example, on a laptop with Intel(R) Core(TM) i7-7600U, Code Carbon will read two files :
/sys/class/powercap/intel-rapl/intel-rapl:1/energy_uj and /sys/class/powercap/intel-rapl/intel-rapl:0/energy_uj</p>
</section>
<section id="rapl-metrics">
<h2>RAPL Metrics<a class="headerlink" href="#rapl-metrics" title="Link to this heading"></a></h2>
<p>RAPL stand for Running Average Power Limit, it is a feature of processors (CPU) that provide the energy consumption of the processor.</p>
<p>See <a class="reference external" href="https://blog.chih.me/read-cpu-power-with-RAPL.html">https://blog.chih.me/read-cpu-power-with-RAPL.html</a> for more information.</p>
<p>Despite the name Intel RAPL, it support AMD processors since kernel 5.8.</p>
<p>It is some files in /sys/class/powercap/intel-rapl/subsystem/ that give the energy consumption of the CPU, and sometime RAM.
There are folder for each <cite>domain</cite>, and in each folder there are a file <cite>name</cite> with the name of the domain and a <cite>energy_uj</cite> for the amount of energy in micro-joules.</p>
<p>The drawback of RAPL is that not every CPU use it the same way. We focus on the <cite>package</cite> domain, but some CPU have more domain like <cite>core</cite>, <cite>uncore</cite>, <cite>dram</cite>, <cite>psys</cite>, <cite>gpu</cite>, <cite>psys</cite> and <cite>psys-io</cite>.</p>
<p>For example :
- Intel put all the physical cores consumption in <cite>core</cite> and the <cite>package</cite> include <cite>core</cite>.
- For AMD, <cite>core</cite> have very low energy, so we don’t know if it is included in the <cite>package</cite> or not.</p>
<p>Our friend from Scaphandre, a tool to monitor energy consumption, have a good article about RAPL <a class="reference external" href="https://hubblo-org.github.io/scaphandre-documentation/explanations/rapl-domains.html">https://hubblo-org.github.io/scaphandre-documentation/explanations/rapl-domains.html</a> and also a discussion with good references: <a class="reference external" href="https://github.com/hubblo-org/scaphandre/issues/116#issuecomment-854453231">https://github.com/hubblo-org/scaphandre/issues/116#issuecomment-854453231</a> and point out that this topic is not well documented.</p>
<p><a class="reference external" href="https://user-images.githubusercontent.com/894892/120764898-ecf07280-c518-11eb-9155-92780cabcf52.png">https://user-images.githubusercontent.com/894892/120764898-ecf07280-c518-11eb-9155-92780cabcf52.png</a>
Source :“RAPL in Action: Experiences in Using RAPL for Power Measurements,” (K. N. Khan, M. Hirki, T. Niemi, J. K. Nurminen, and Z. Ou, ACM Trans. Model. Perform. Eval. Comput. Syst., vol. 3, no. 2, pp. 1–26, Apr. 2018, doi: 10.1145/3177754.)</p>
<p>Metric comparison</p>
<p>Desktop computer with AMD Ryzen Threadripper 1950X 16-Core (32 threads) Processor.
Power plug measure when idle (10% CPU): 125 W
package-0-die-0 : 68 W
package-0-die-1 : 68 W
CodeCarbon : 137 W</p>
<p>Power plug measure when loaded (100% CPU): 256 W - 125W in idle = 131 W
CorWatt PkgWatt</p>
<blockquote>
<div><p>133.13 169.82
7.54 169.82</p>
</div></blockquote>
<p>CodeCarbon : 330 W
package-0-die-0 : 166 W
package-0-die-1 : 166 W</p>
<p>RAPL: 234 sec. Joule Counter Range, at 280 Watts</p>
</section>
<section id="cpu-metrics-priority">
<h2>CPU metrics priority<a class="headerlink" href="#cpu-metrics-priority" title="Link to this heading"></a></h2>
<p>CodeCarbon will first try to read the energy consumption of the CPU from low level interface like RAPL or <code class="docutils literal notranslate"><span class="pre">powermetrics</span></code>.
If none of the tracking tools are available, CodeCarbon will be switched to a fallback mode:</p>
<blockquote>
<div><ul class="simple">
<li><p>It will first detect which CPU hardware is currently in use, and then map it to a data source listing 2000+ Intel and AMD CPUs and their corresponding thermal design powers (TDPs).</p></li>
<li><p>If the CPU is not found in the data source, a global constant will be applied.</p></li>
<li><p>If <code class="docutils literal notranslate"><span class="pre">psutil</span></code> is available, CodeCarbon will try to estimate the energy consumption from the TDP and the CPU load.</p></li>
<li><p>CodeCarbon assumes that 50% of the TDP will be the average power consumption to make this approximation.</p></li>
</ul>
</div></blockquote>
<p>Here is a drawing of the fallback mode:</p>
<img alt="CPU Fallback" class="align-center" src="_images/cpu_fallback.png" />
<p>The code doing this is available in <a class="reference external" href="https://github.com/mlco2/codecarbon/blob/master/codecarbon/core/resource_tracker.py#L24">codecarbon/core/resource_tracker.py</a>.</p>
<p>The net Energy Used is the net power supply consumed during the compute time, measured as <code class="docutils literal notranslate"><span class="pre">kWh</span></code>.</p>
<p>We compute energy consumption as the product of the power consumed and the time the power was consumed for. The formula is:
<code class="docutils literal notranslate"><span class="pre">Energy</span> <span class="pre">=</span> <span class="pre">Power</span> <span class="pre">*</span> <span class="pre">Time</span></code></p>
</section>
<section id="references">
<h2>References<a class="headerlink" href="#references" title="Link to this heading"></a></h2>
<p><a class="reference external" href="https://arxiv.org/pdf/1911.08354.pdf">Energy Usage Reports: Environmental awareness as part of algorithmic accountability</a></p>
<section id="how-codecarbon-works">
<h3>How CodeCarbon Works<a class="headerlink" href="#how-codecarbon-works" title="Link to this heading"></a></h3>
<p>CodeCarbon uses a scheduler that, by default, calls for a measure every 15 seconds, so it has no significant overhead.</p>
<p>The measure itself is fast and CodeCarbon is designed to be as light as possible with a small memory footprint.</p>
<p>The scheduler is started when the first <code class="docutils literal notranslate"><span class="pre">start</span></code> method is called and stopped when <code class="docutils literal notranslate"><span class="pre">stop</span></code> method is called.</p>
</section>
</section>
<section id="estimation-of-equivalent-usage-emissions">
<h2>Estimation of Equivalent Usage Emissions<a class="headerlink" href="#estimation-of-equivalent-usage-emissions" title="Link to this heading"></a></h2>
<p>The CodeCarbon dashboard provides equivalent emissions and energy usage comparisons to help users better understand the carbon impact of their activities. These comparisons are based on the following assumptions:</p>
<section id="car-usage">
<h3>Car Usage<a class="headerlink" href="#car-usage" title="Link to this heading"></a></h3>
<ul class="simple">
<li><p><strong>Emission factor</strong>: <em>0.12 kgCO₂ per kilometer driven</em>.</p></li>
<li><p>This value is derived from the average emissions of a European passenger car under normal driving conditions.</p></li>
</ul>
<p>Source : <a class="reference external" href="https://co2cars.apps.eea.europa.eu/?source=%7B%22track_total_hits%22%3Atrue%2C%22query%22%3A%7B%22bool%22%3A%7B%22must%22%3A%5B%7B%22constant_score%22%3A%7B%22filter%22%3A%7B%22bool%22%3A%7B%22must%22%3A%5B%7B%22bool%22%3A%7B%22should%22%3A%5B%7B%22term%22%3A%7B%22year%22%3A2023%7D%7D%5D%7D%7D%2C%7B%22bool%22%3A%7B%22should%22%3A%5B%7B%22term%22%3A%7B%22scStatus%22%3A%22Provisional%22%7D%7D%5D%7D%7D%5D%7D%7D%7D%7D%5D%7D%7D%2C%22display_type%22%3A%22tabular%22%7D">European Environment Agency</a></p>
</section>
<section id="tv-usage">
<h3>TV Usage<a class="headerlink" href="#tv-usage" title="Link to this heading"></a></h3>
<ul class="simple">
<li><p><strong>Energy consumption</strong>: <em>138 Wh per day based on average use</em>.</p></li>
<li><p>This assumes:
- An average daily usage of 6.5 hours.
- A modern television with a power consumption of approximately <em>21.2 W per hour</em>.</p></li>
</ul>
<p>Source : <a class="reference external" href="https://agirpourlatransition.ademe.fr/particuliers/maison/economies-denergie-deau/electricite-combien-consomment-appareils-maison">The French Agency for Ecological Transition</a></p>
</section>
<section id="us-citizen-weekly-emissions">
<h3>US Citizen Weekly Emissions<a class="headerlink" href="#us-citizen-weekly-emissions" title="Link to this heading"></a></h3>
<ul class="simple">
<li><p><strong>Annual emissions</strong>: <em>13.3 tons of CO₂ equivalent per year</em> for an average US citizen.</p></li>
<li><p><strong>Weekly emissions</strong>: This value is divided by the 52 weeks in a year to estimate weekly emissions:</p></li>
</ul>
<div class="math notranslate nohighlight">
\[\text{Weekly Emissions} = \frac{\text{Annual Emissions (tons)}}{52}\]</div>
<div class="math notranslate nohighlight">
\[\text{Weekly Emissions} = \frac{13.3}{52} \approx 0.256 \, \text{tons of CO₂ equivalent per week.}\]</div>
<p>Source : <a class="reference external" href="https://www.iea.org/data-and-statistics/charts/co2-total-emissions-per-capita-by-region-2000-2023">IEA CO2 total emissions per capita by region, 2000-2023</a></p>
</section>
<section id="calculation-formula">
<h3>Calculation Formula<a class="headerlink" href="#calculation-formula" title="Link to this heading"></a></h3>
<p>The equivalent emissions are calculated using this formula:</p>
<div class="math notranslate nohighlight">
\[\text{Equivalent Emissions} = \frac{\text{Total Emissions (kgCO₂)}}{\text{Emission Factor (kgCO₂/unit)}}\]</div>
<p>For example:</p>
<ul class="simple">
<li><p><strong>Car Usage</strong>: <em>1 kWh</em> of energy consumption is approximately equivalent to:
- <em>8.33 kilometers driven by a car</em> (<em>1 ÷ 0.12</em>).
- <em>11.9 hours of TV usage</em> (<em>1 ÷ 0.084</em>), if emissions are considered.</p></li>
<li><p><strong>US Citizen Emissions</strong>:
- <em>1 kWh</em> of energy consumption can be compared to a fraction of the average weekly emissions of a US citizen:</p></li>
</ul>
<div class="math notranslate nohighlight">
\[\text{US Citizen Equivalent} = \frac{\text{Total Emissions (tons)}}{0.256}\]</div>
<p>These estimates are approximate and subject to regional variations in:
- Grid emissions intensity.
- Vehicle efficiencies.</p>
</section>
<section id="source-code">
<h3>Source Code<a class="headerlink" href="#source-code" title="Link to this heading"></a></h3>
<p>The emission factors used are defined in the <a class="reference external" href="https://github.com/mlco2/codecarbon/blob/master/webapp/src/helpers/constants.ts">CodeCarbon source code</a>. They are based on publicly available data and general assumptions.</p>
</section>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="motivation.html" class="btn btn-neutral float-left" title="Motivation" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="model_examples.html" class="btn btn-neutral float-right" title="Model Comparisons" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© Copyright 2020, BCG GAMMA, Comet.ml, Haverford College, MILA.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>