├── .gitignore ├── CoverArt ├── 1563899655.jpg ├── 1563899838.png ├── 1563899899.jpg ├── 1563899938.png ├── 1563899944.jpg ├── 1563899977.png ├── 1563899986.jpg ├── 1563900023.jpg ├── 1563900063.png ├── 1563900067.jpg ├── 1563900154.png ├── 3D Digital and Print.jpg ├── 3D Digital and Print.png ├── 9781484249314.tif ├── Front.png └── Spine.png ├── FrontCover.png ├── LICENSE ├── README.md ├── chapter01 ├── .gitignore ├── Makefile ├── README.md └── pmemkv.cpp ├── chapter02 └── README.txt ├── chapter03 ├── .gitignore ├── Makefile ├── README.md ├── Windows_Get-PmemDisk.out ├── df-h.out ├── ipmctl_show_-dimm.out ├── ipmctl_show_-region.out ├── map_file_windows_example.c ├── mmap_example.c ├── ndctl_list_-DRN.out └── pmem_map_file.c ├── chapter04 └── README.md ├── chapter05 └── README.md ├── chapter06 ├── .gitignore ├── Makefile ├── README.md ├── full_copy.c ├── manpage.c └── simple_copy.c ├── chapter07 ├── Makefile ├── README.md ├── pmemobj_alloc.c ├── preader.c ├── pwriter.c ├── reserve_publish.c └── tx.c ├── chapter08 ├── .gitignore ├── Makefile ├── README.md ├── allocation.cpp ├── containers.cpp ├── non_trivial_copy.cpp ├── p.cpp ├── persistent_queue.hpp ├── queue.cpp ├── transaction.cpp ├── volatile_pointers.cpp └── volatile_queue.hpp ├── chapter09 ├── Makefile ├── README.md ├── config_structure.c ├── js-phonebook.js ├── phonebook.cpp └── pmemkv_config.h ├── chapter10 ├── .gitignore ├── Makefile ├── README.md ├── pmem_allocator.cpp ├── pmem_config.c ├── pmem_detect_kind.c ├── vector_of_strings.cpp └── vmemcache.c ├── chapter11 ├── Makefile ├── README.md ├── data_oriented_design.cpp ├── simplekv.cpp ├── simplekv.hpp ├── simplekv_rebuild.cpp ├── simplekv_rebuild.hpp └── versioning_insert.cpp ├── chapter12 ├── Makefile ├── README.md ├── leak.c ├── listing_12-11.c ├── listing_12-13.c ├── listing_12-16.c ├── listing_12-17.c ├── listing_12-23.c ├── listing_12-25.c ├── listing_12-28.c ├── listing_12-33.c ├── listing_12-36.c ├── listing_12-38.c ├── listing_12-40.c ├── listing_12-44.cpp ├── listing_12-45.cpp ├── listing_12-48.cpp ├── listing_12-51.cpp ├── listing_12-9.c └── stackoverflow.c ├── chapter13 └── README.md ├── chapter14 ├── Makefile ├── README.md ├── listing_14-1.cpp ├── listing_14-2.cpp ├── listing_14-3.cpp └── listing_14-4.cpp ├── chapter15 └── README.md ├── chapter16 └── README.md ├── chapter17 └── README.md ├── chapter18 ├── Makefile ├── README.md ├── hello.c └── pool.set ├── chapter19 └── README.md └── contributing.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.lst 2 | a.out 3 | -------------------------------------------------------------------------------- /CoverArt/1563899655.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563899655.jpg -------------------------------------------------------------------------------- /CoverArt/1563899838.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563899838.png -------------------------------------------------------------------------------- /CoverArt/1563899899.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563899899.jpg -------------------------------------------------------------------------------- /CoverArt/1563899938.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563899938.png -------------------------------------------------------------------------------- /CoverArt/1563899944.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563899944.jpg -------------------------------------------------------------------------------- /CoverArt/1563899977.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563899977.png -------------------------------------------------------------------------------- /CoverArt/1563899986.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563899986.jpg -------------------------------------------------------------------------------- /CoverArt/1563900023.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563900023.jpg -------------------------------------------------------------------------------- /CoverArt/1563900063.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563900063.png -------------------------------------------------------------------------------- /CoverArt/1563900067.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563900067.jpg -------------------------------------------------------------------------------- /CoverArt/1563900154.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/1563900154.png -------------------------------------------------------------------------------- /CoverArt/3D Digital and Print.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/3D Digital and Print.jpg -------------------------------------------------------------------------------- /CoverArt/3D Digital and Print.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/3D Digital and Print.png -------------------------------------------------------------------------------- /CoverArt/9781484249314.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/9781484249314.tif -------------------------------------------------------------------------------- /CoverArt/Front.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/Front.png -------------------------------------------------------------------------------- /CoverArt/Spine.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/CoverArt/Spine.png -------------------------------------------------------------------------------- /FrontCover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pmem/book/d5000a8eea46e643859bf8f1a0dfa7f672f5c8ef/FrontCover.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2020, Persistent Memory Programming 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PROJECT NOT UNDER ACTIVE MANAGEMENT # 2 | This project will no longer be maintained by Intel. 3 | Intel has ceased development and contributions including, but not limited to, maintenance, bug fixes, new releases, or updates, to this project. 4 | Intel no longer accepts patches to this project. 5 | If you have an ongoing need to use this project, are interested in independently developing it, or would like to maintain patches for the open source software community, please create your own fork of this project. 6 | 7 | # Apress Source Code 8 | 9 | This repository accompanies *Programming Persistent Memory - A Comprehensive Guide for Developers* by Steve Scargall (Apress, 2020). 10 | 11 | [comment]: #cover 12 | ![Cover image](FrontCover.png) 13 | 14 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 15 | 16 | ## Releases 17 | 18 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 19 | 20 | ## Contributions 21 | 22 | See the file Contributing.md for more information on how you can contribute to this repository. 23 | -------------------------------------------------------------------------------- /chapter01/.gitignore: -------------------------------------------------------------------------------- 1 | pmemkv 2 | -------------------------------------------------------------------------------- /chapter01/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, Intel Corporation 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # 15 | # * Neither the name of Intel Corporation nor the names of its 16 | # contributors may be used to endorse or promote products derived 17 | # from this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | # 32 | # Makefile for chapter1 examples 33 | # 34 | 35 | .SUFFIXES: .lst 36 | 37 | all: pmemkv listings 38 | 39 | listings: pmemkv.lst 40 | 41 | %.lst: %.cpp 42 | expand -t 4 < $^ | cat -n > $@ 43 | 44 | pmemkv: pmemkv.cpp 45 | $(CXX) -o pmemkv pmemkv.cpp -lpmemkv 46 | 47 | clean: 48 | $(RM) *.o core a.out 49 | 50 | clobber: clean 51 | $(RM) pmemkv *.lst 52 | 53 | .PHONY: all clean clobber listings 54 | -------------------------------------------------------------------------------- /chapter01/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 1 - Introduction to Persistent Memory. 2 | -------------------------------------------------------------------------------- /chapter01/pmemkv.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * pmemkv.cpp -- demonstrate a high-level key-value API for pmem 35 | */ 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | using namespace pmem::kv; 42 | using std::cerr; 43 | using std::cout; 44 | using std::endl; 45 | using std::string; 46 | 47 | /* 48 | * for this example, create a 1 Gig file 49 | * called "/daxfs/kvfile" 50 | */ 51 | auto PATH = "/daxfs/kvfile"; 52 | const uint64_t SIZE = 1024 * 1024 * 1024; 53 | 54 | /* 55 | * kvprint -- print a single key-value pair 56 | */ 57 | int kvprint(string_view k, string_view v) { 58 | cout << "key: " << k.data() << 59 | " value: " << v.data() << endl; 60 | return 0; 61 | } 62 | 63 | int main() { 64 | // start by creating the db object 65 | db *kv = new db(); 66 | assert(kv != nullptr); 67 | 68 | // create the config information for 69 | // libpmemkv's open method 70 | config cfg; 71 | 72 | if (cfg.put_string("path", PATH) != status::OK) { 73 | cerr << pmemkv_errormsg() << endl; 74 | exit(1); 75 | } 76 | if (cfg.put_uint64("force_create", 1) != status::OK) { 77 | cerr << pmemkv_errormsg() << endl; 78 | exit(1); 79 | } 80 | if (cfg.put_uint64("size", SIZE) != status::OK) { 81 | cerr << pmemkv_errormsg() << endl; 82 | exit(1); 83 | } 84 | 85 | 86 | // open the key-value store, using the cmap engine 87 | if (kv->open("cmap", std::move(cfg)) != status::OK) { 88 | cerr << errormsg() << endl; 89 | exit(1); 90 | } 91 | 92 | // add some keys and values 93 | if (kv->put("key1", "value1") != status::OK) { 94 | cerr << errormsg() << endl; 95 | exit(1); 96 | } 97 | if (kv->put("key2", "value2") != status::OK) { 98 | cerr << errormsg() << endl; 99 | exit(1); 100 | } 101 | if (kv->put("key3", "value3") != status::OK) { 102 | cerr << errormsg() << endl; 103 | exit(1); 104 | } 105 | 106 | // iterate through the key-value store, printing them 107 | kv->get_all(kvprint); 108 | 109 | // stop the pmemkv engine 110 | delete kv; 111 | 112 | exit(0); 113 | } 114 | -------------------------------------------------------------------------------- /chapter02/README.txt: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 2 - Persistent Memory Architecture. 2 | -------------------------------------------------------------------------------- /chapter03/.gitignore: -------------------------------------------------------------------------------- 1 | mmap_example 2 | pmem_map_file 3 | testfile 4 | -------------------------------------------------------------------------------- /chapter03/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, Intel Corporation 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # 15 | # * Neither the name of Intel Corporation nor the names of its 16 | # contributors may be used to endorse or promote products derived 17 | # from this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | # 32 | # Makefile for chapter3 examples 33 | # 34 | 35 | .SUFFIXES: .lst 36 | 37 | all: mmap_example pmem_map_file listings 38 | 39 | listings: mmap_example.lst pmem_map_file.lst map_file_windows_example.lst 40 | 41 | %.lst: %.c 42 | cat -n $^ > $@ 43 | 44 | mmap_example: mmap_example.c 45 | $(CC) -o mmap_example mmap_example.c 46 | 47 | pmem_map_file: pmem_map_file.c 48 | $(CC) -o pmem_map_file pmem_map_file.c -lpmem 49 | 50 | clean: 51 | $(RM) *.o core a.out testfile 52 | 53 | clobber: clean 54 | $(RM) mmap_example pmem_map_file *.lst 55 | 56 | .PHONY: all clean clobber listings 57 | -------------------------------------------------------------------------------- /chapter03/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 3 - Operating System Support for Persistent Memory. 2 | -------------------------------------------------------------------------------- /chapter03/Windows_Get-PmemDisk.out: -------------------------------------------------------------------------------- 1 | PS C:\Users\Administrator> Get-PmemDisk 2 | 3 | Number Size Health Atomicity Removable Physical device IDs Unsafe shutdowns 4 | ------ ---- ------ --------- --------- ------------------- ---------------- 5 | 2 249 GB Healthy None True {1} 36 6 | 7 | 8 | PS C:\Users\Administrator> Get-Disk 2 | Get-Partition 9 | 10 | PartitionNumber DriveLetter Offset Size Type 11 | --------------- ----------- ------ ---- ---- 12 | 1 24576 15.98 MB Reserved 13 | 2 D 16777216 248.98 GB Basic 14 | -------------------------------------------------------------------------------- /chapter03/df-h.out: -------------------------------------------------------------------------------- 1 | ### 2 | ### Executing 'df -h' as a non-root user to see if 3 | ### any persistent memory filesystems are mounted. 4 | ### This example shows two /dev/pmem* devices are mounted. 5 | ### 6 | 7 | $ df -h /dev/pmem* 8 | Filesystem Size Used Avail Use% Mounted on 9 | /dev/pmem0 1.5T 77M 1.4T 1% /mnt/pmemfs0 10 | /dev/pmem1 1.5T 77M 1.4T 1% /mnt/pmemfs1 11 | -------------------------------------------------------------------------------- /chapter03/ipmctl_show_-dimm.out: -------------------------------------------------------------------------------- 1 | # ipmctl show -dimm 2 | DimmID | Capacity | HealthState | ActionRequired | LockState | FWVersion 3 | ============================================================================== 4 | 0x0001 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 5 | 0x0011 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 6 | 0x0021 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 7 | 0x0101 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 8 | 0x0111 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 9 | 0x0121 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 10 | 0x1001 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 11 | 0x1011 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 12 | 0x1021 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 13 | 0x1101 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 14 | 0x1111 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 15 | 0x1121 | 252.4 GiB | Healthy | 0 | Disabled | 01.02.00.5367 16 | -------------------------------------------------------------------------------- /chapter03/ipmctl_show_-region.out: -------------------------------------------------------------------------------- 1 | 2 | SocketID | ISetID | PersistentMemoryType | Capacity | FreeCapacity | HealthState 3 | ================================================================================================ 4 | 0x0000 | 0x2d3c7f48f4e22ccc | AppDirect | 1512.0 GiB | 0.0 GiB | Healthy 5 | 0x0001 | 0xdd387f488ce42ccc | AppDirect | 1512.0 GiB | 1512.0 GiB | Healthy 6 | -------------------------------------------------------------------------------- /chapter03/map_file_windows_example.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * map_file_windows_example.c -- windows memory mapped file example 35 | * 36 | * This file memory-maps a file and stores a string to it. It is 37 | * a quick example of how to use MapViewOfFileEx() and how to flush 38 | * changes to the file. 39 | * 40 | * To run this example, provide a single argument, which is the name of 41 | * a test file that is at least 4k in length. This program will overwrite 42 | * the file contents! 43 | */ 44 | 45 | #include 46 | #include 47 | #include 48 | #include 49 | #include 50 | #include 51 | #include 52 | 53 | int 54 | main(int argc, char *argv[]) 55 | { 56 | if (argc != 2) { 57 | fprintf(stderr, "Usage: %s filename\n", 58 | argv[0]); 59 | exit(1); 60 | } 61 | 62 | /* Create the file or open if the file exists */ 63 | HANDLE fh = CreateFile(argv[1], 64 | GENERIC_READ|GENERIC_WRITE, 65 | 0, 66 | NULL, 67 | OPEN_EXISTING, 68 | FILE_ATTRIBUTE_NORMAL, 69 | NULL); 70 | 71 | if (fh == INVALID_HANDLE_VALUE) { 72 | fprintf(stderr, "CreateFile, gle: 0x%08x", 73 | GetLastError()); 74 | exit(1); 75 | } 76 | 77 | /* 78 | * Get the file length for use when 79 | * memory mapping later 80 | * */ 81 | DWORD filelen = GetFileSize(fh, NULL); 82 | if (filelen == 0) { 83 | fprintf(stderr, "GetFileSize, gle: 0x%08x", 84 | GetLastError()); 85 | exit(1); 86 | } 87 | 88 | /* Create a file mapping object */ 89 | HANDLE fmh = CreateFileMapping(fh, 90 | NULL, /* security attributes */ 91 | PAGE_READWRITE, 92 | 0, 93 | 0, 94 | NULL); 95 | 96 | if (fmh == NULL) { 97 | fprintf(stderr, "CreateFileMapping, 98 | gle: 0x%08x", GetLastError()); 99 | exit(1); 100 | } 101 | 102 | /* 103 | * Map into our address space and get a pointer 104 | * to the beginning 105 | * */ 106 | char *pmaddr = (char *)MapViewOfFileEx(fmh, 107 | FILE_MAP_ALL_ACCESS, 108 | 0, 109 | 0, 110 | filelen, 111 | NULL); /* hint address */ 112 | 113 | if (pmaddr == NULL) { 114 | fprintf(stderr, "MapViewOfFileEx, 115 | gle: 0x%08x", GetLastError()); 116 | exit(1); 117 | } 118 | 119 | /* 120 | * On windows must leave the file handle(s) 121 | * open while mmaped 122 | * */ 123 | 124 | /* store a string to the beginning of the file */ 125 | strcpy(pmaddr, "This is new data written to 126 | the file"); 127 | 128 | /* 129 | * Flush this page with length rounded up to 4K 130 | * page size 131 | * */ 132 | if (FlushViewOfFile(pmaddr, 4096) == FALSE) { 133 | fprintf(stderr, "FlushViewOfFile, 134 | gle: 0x%08x", GetLastError()); 135 | exit(1); 136 | } 137 | 138 | /* Flush the complete file to backing storage */ 139 | if (FlushFileBuffers(fh) == FALSE) { 140 | fprintf(stderr, "FlushFileBuffers, 141 | gle: 0x%08x", GetLastError()); 142 | exit(1); 143 | } 144 | 145 | /* Explicitly unmap before closing the file */ 146 | if (UnmapViewOfFile(pmaddr) == FALSE) { 147 | fprintf(stderr, "UnmapViewOfFile, 148 | gle: 0x%08x", GetLastError()); 149 | exit(1); 150 | } 151 | 152 | CloseHandle(fmh); 153 | CloseHandle(fh); 154 | 155 | printf("Done.\n"); 156 | exit(0); 157 | } 158 | -------------------------------------------------------------------------------- /chapter03/mmap_example.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * mmap_example.c -- small, self-contained example using mmap 35 | * 36 | * This file memory-maps a file and stores a string to it. It is 37 | * a quick example of how to use mmap() and msync() with Persistent 38 | * Memory. 39 | * 40 | * To build this example: 41 | * gcc -o mmap_example mmap_example.c 42 | * 43 | * To run it, create a 4k file, run the example, and dump the result: 44 | * dd if=/dev/zero of=testfile bs=4k count=1 45 | * od -c testfile # see it is all zeros 46 | * ./mmap_example testfile 47 | * od -c testfile # see the result of this program 48 | */ 49 | 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | #include 59 | 60 | int 61 | main(int argc, char *argv[]) 62 | { 63 | int fd; 64 | struct stat stbuf; 65 | char *pmaddr; 66 | 67 | if (argc != 2) { 68 | fprintf(stderr, "Usage: %s filename\n", 69 | argv[0]); 70 | exit(1); 71 | } 72 | 73 | if ((fd = open(argv[1], O_RDWR)) < 0) 74 | err(1, "open %s", argv[1]); 75 | 76 | if (fstat(fd, &stbuf) < 0) 77 | err(1, "stat %s", argv[1]); 78 | 79 | /* 80 | * Map the file into our address space for read 81 | * & write. Use MAP_SHARED so stores are visible 82 | * to other programs. 83 | */ 84 | if ((pmaddr = mmap(NULL, stbuf.st_size, 85 | PROT_READ|PROT_WRITE, 86 | MAP_SHARED, fd, 0)) == MAP_FAILED) 87 | err(1, "mmap %s", argv[1]); 88 | 89 | /* Don't need the fd anymore because the mapping 90 | * stays around */ 91 | close(fd); 92 | 93 | /* store a string to the Persistent Memory */ 94 | strcpy(pmaddr, "This is new data written to the 95 | file"); 96 | 97 | /* 98 | * Simplest way to flush is to call msync(). 99 | * The length needs to be rounded up to a 4k page. 100 | */ 101 | if (msync((void *)pmaddr, 4096, MS_SYNC) < 0) 102 | err(1, "msync"); 103 | 104 | printf("Done.\n"); 105 | exit(0); 106 | } 107 | -------------------------------------------------------------------------------- /chapter03/pmem_map_file.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #ifndef _WIN32 39 | #include 40 | #else 41 | #include 42 | #endif 43 | #include 44 | #include 45 | 46 | /* Using 4K of pmem for this example */ 47 | #define PMEM_LEN 4096 48 | 49 | int 50 | main(int argc, char *argv[]) 51 | { 52 | char *pmemaddr; 53 | size_t mapped_len; 54 | int is_pmem; 55 | 56 | if (argc != 2) { 57 | fprintf(stderr, "Usage: %s filename\n", 58 | argv[0]); 59 | exit(1); 60 | } 61 | 62 | /* Create a pmem file and memory map it. */ 63 | if ((pmemaddr = pmem_map_file(argv[1], PMEM_LEN, 64 | PMEM_FILE_CREATE, 0666, &mapped_len, 65 | &is_pmem)) == NULL) { 66 | perror("pmem_map_file"); 67 | exit(1); 68 | } 69 | 70 | /* Store a string to the persistent memory. */ 71 | char s[] = "This is new data written to the file"; 72 | strcpy(pmemaddr, s); 73 | 74 | /* Flush our string to persistence. */ 75 | if (is_pmem) 76 | pmem_persist(pmemaddr, sizeof(s)); 77 | else 78 | pmem_msync(pmemaddr, sizeof(s)); 79 | 80 | /* Delete the mappings. */ 81 | pmem_unmap(pmemaddr, mapped_len); 82 | 83 | printf("Done.\n"); 84 | exit(0); 85 | } 86 | -------------------------------------------------------------------------------- /chapter04/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 4 - Fundamental Concepts of Persistent Memory Programming. 2 | -------------------------------------------------------------------------------- /chapter05/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 5 Introducing the Persistent Memory Development Kit (PMDK) 2 | -------------------------------------------------------------------------------- /chapter06/.gitignore: -------------------------------------------------------------------------------- 1 | full_copy 2 | manpage 3 | simple_copy 4 | -------------------------------------------------------------------------------- /chapter06/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, Intel Corporation 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # 15 | # * Neither the name of Intel Corporation nor the names of its 16 | # contributors may be used to endorse or promote products derived 17 | # from this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | # 32 | # Makefile for chapter6 examples 33 | # 34 | 35 | .SUFFIXES: .lst 36 | 37 | all: full_copy manpage simple_copy listings 38 | 39 | listings: full_copy.lst manpage.lst simple_copy.lst 40 | 41 | %.lst: %.c 42 | expand -t 4 < $^ | cat -n > $@ 43 | 44 | full_copy: full_copy.c 45 | $(CC) -o full_copy full_copy.c -lpmem 46 | 47 | manpage: manpage.c 48 | $(CC) -o manpage manpage.c -lpmem 49 | 50 | simple_copy: simple_copy.c 51 | $(CC) -o simple_copy simple_copy.c -lpmem 52 | 53 | clean: 54 | $(RM) *.o core a.out *.lst 55 | 56 | clobber: clean 57 | $(RM) full_copy manpage simple_copy *.lst 58 | 59 | .PHONY: all clean clobber listings 60 | -------------------------------------------------------------------------------- /chapter06/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 6 - libpmem: Low-Level Persistent Memory Support 2 | -------------------------------------------------------------------------------- /chapter06/full_copy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014-2017, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * full_copy.c - show how to use pmem_memcpy_nodrain() 35 | * 36 | * usage: full_copy src-file dst-file 37 | * 38 | * Copies src-file to dst-file in 4k chunks. 39 | */ 40 | 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #ifndef _WIN32 48 | #include 49 | #else 50 | #include 51 | #endif 52 | #include 53 | #include 54 | 55 | /* Copying 4K at a time to pmem for this example */ 56 | #define BUF_LEN 4096 57 | 58 | /* 59 | * do_copy_to_pmem 60 | */ 61 | static void 62 | do_copy_to_pmem(char *pmemaddr, int srcfd, off_t len) 63 | { 64 | char buf[BUF_LEN]; 65 | int cc; 66 | 67 | /* 68 | * Copy the file, 69 | * saving the last flush & drain step to the end 70 | */ 71 | while ((cc = read(srcfd, buf, BUF_LEN)) > 0) { 72 | pmem_memcpy_nodrain(pmemaddr, buf, cc); 73 | pmemaddr += cc; 74 | } 75 | 76 | if (cc < 0) { 77 | perror("read"); 78 | exit(1); 79 | } 80 | 81 | /* Perform final flush step */ 82 | pmem_drain(); 83 | } 84 | 85 | /* 86 | * do_copy_to_non_pmem - copy to a non-pmem 87 | * memory mapped file 88 | */ 89 | static void 90 | do_copy_to_non_pmem(char *addr, int srcfd, off_t len) 91 | { 92 | char *startaddr = addr; 93 | char buf[BUF_LEN]; 94 | int cc; 95 | 96 | /* 97 | * Copy the file, saving the last flush 98 | * step to the end 99 | */ 100 | while ((cc = read(srcfd, buf, BUF_LEN)) > 0) { 101 | memcpy(addr, buf, cc); 102 | addr += cc; 103 | } 104 | 105 | if (cc < 0) { 106 | perror("read"); 107 | exit(1); 108 | } 109 | 110 | /* Flush it */ 111 | if (pmem_msync(startaddr, len) < 0) { 112 | perror("pmem_msync"); 113 | exit(1); 114 | } 115 | } 116 | 117 | int 118 | main(int argc, char *argv[]) 119 | { 120 | int srcfd; 121 | struct stat stbuf; 122 | char *pmemaddr; 123 | size_t mapped_len; 124 | int is_pmem; 125 | 126 | if (argc != 3) { 127 | fprintf(stderr, 128 | "usage: %s src-file dst-file\n", 129 | argv[0]); 130 | exit(1); 131 | } 132 | 133 | /* Open src-file */ 134 | if ((srcfd = open(argv[1], O_RDONLY)) < 0) { 135 | perror(argv[1]); 136 | exit(1); 137 | } 138 | 139 | /* Find the size of the src-file */ 140 | if (fstat(srcfd, &stbuf) < 0) { 141 | perror("fstat"); 142 | exit(1); 143 | } 144 | 145 | /* create a pmem file and memory map it */ 146 | if ((pmemaddr = pmem_map_file(argv[2], 147 | stbuf.st_size, 148 | PMEM_FILE_CREATE|PMEM_FILE_EXCL, 149 | 0666, &mapped_len, &is_pmem)) == NULL) { 150 | perror("pmem_map_file"); 151 | exit(1); 152 | } 153 | 154 | /* 155 | * Determine if range is true pmem, 156 | * call appropriate copy routine 157 | * */ 158 | if (is_pmem) 159 | do_copy_to_pmem(pmemaddr, srcfd, 160 | stbuf.st_size); 161 | else 162 | do_copy_to_non_pmem(pmemaddr, srcfd, 163 | stbuf.st_size); 164 | 165 | close(srcfd); 166 | pmem_unmap(pmemaddr, mapped_len); 167 | 168 | exit(0); 169 | } 170 | -------------------------------------------------------------------------------- /chapter06/manpage.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * manpage.c - simple example for the libpmem man page 35 | */ 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #ifndef _WIN32 44 | #include 45 | #else 46 | #include 47 | #endif 48 | #include 49 | #include 50 | 51 | /* Using 4K of pmem for this example */ 52 | #define PMEM_LEN 4096 53 | 54 | #define PATH "/pmem-fs/myfile" 55 | 56 | int 57 | main(int argc, char *argv[]) 58 | { 59 | char *pmemaddr; 60 | size_t mapped_len; 61 | int is_pmem; 62 | 63 | /* Create a pmem file and memory map it */ 64 | if ((pmemaddr = pmem_map_file(PATH, PMEM_LEN, 65 | PMEM_FILE_CREATE, 66 | 0666, &mapped_len, &is_pmem)) == NULL) { 67 | perror("pmem_map_file"); 68 | exit(1); 69 | } 70 | 71 | /* Store a string to the persistent memory */ 72 | strcpy(pmemaddr, "hello, persistent memory"); 73 | 74 | /* Flush above strcpy to persistence */ 75 | if (is_pmem) 76 | pmem_persist(pmemaddr, mapped_len); 77 | else 78 | pmem_msync(pmemaddr, mapped_len); 79 | 80 | /* 81 | * Delete the mappings. The region is also 82 | * automatically unmapped when the process is 83 | * terminated. 84 | */ 85 | pmem_unmap(pmemaddr, mapped_len); 86 | } 87 | -------------------------------------------------------------------------------- /chapter06/simple_copy.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * simple_copy.c 35 | * 36 | * usage: simple_copy src-file dst-file 37 | * 38 | * Reads 4k from src-file and writes it to dst-file. 39 | */ 40 | 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #ifndef _WIN32 48 | #include 49 | #else 50 | #include 51 | #endif 52 | #include 53 | #include 54 | 55 | /* Just copying 4K to pmem for this example */ 56 | #define BUF_LEN 4096 57 | 58 | int 59 | main(int argc, char *argv[]) 60 | { 61 | int srcfd; 62 | char buf[BUF_LEN]; 63 | char *pmemaddr; 64 | size_t mapped_len; 65 | int is_pmem; 66 | int cc; 67 | 68 | if (argc != 3) { 69 | fprintf(stderr, 70 | "usage: %s src-file dst-file\n", argv[0]); 71 | exit(1); 72 | } 73 | 74 | /* open src-file */ 75 | if ((srcfd = open(argv[1], O_RDONLY)) < 0) { 76 | perror(argv[1]); 77 | exit(1); 78 | } 79 | 80 | /* create a pmem file and memory map it */ 81 | if ((pmemaddr = pmem_map_file(argv[2], BUF_LEN, 82 | PMEM_FILE_CREATE|PMEM_FILE_EXCL, 83 | 0666, &mapped_len, &is_pmem)) == NULL) { 84 | perror("pmem_map_file"); 85 | exit(1); 86 | } 87 | 88 | /* read up to BUF_LEN from srcfd */ 89 | if ((cc = read(srcfd, buf, BUF_LEN)) < 0) { 90 | pmem_unmap(pmemaddr, mapped_len); 91 | perror("read"); 92 | exit(1); 93 | } 94 | 95 | /* write it to the pmem */ 96 | if (is_pmem) { 97 | pmem_memcpy_persist(pmemaddr, buf, cc); 98 | } else { 99 | memcpy(pmemaddr, buf, cc); 100 | pmem_msync(pmemaddr, cc); 101 | } 102 | 103 | close(srcfd); 104 | pmem_unmap(pmemaddr, mapped_len); 105 | 106 | exit(0); 107 | } 108 | -------------------------------------------------------------------------------- /chapter07/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2015-2020, Intel Corporation 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions 6 | # are met: 7 | # 8 | # * Redistributions of source code must retain the above copyright 9 | # notice, this list of conditions and the following disclaimer. 10 | # 11 | # * Redistributions in binary form must reproduce the above copyright 12 | # notice, this list of conditions and the following disclaimer in 13 | # the documentation and/or other materials provided with the 14 | # distribution. 15 | # 16 | # * Neither the name of the copyright holder nor the names of its 17 | # contributors may be used to endorse or promote products derived 18 | # from this software without specific prior written permission. 19 | # 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | # 32 | 33 | CC = gcc 34 | CXX = g++ 35 | RM = rm -f 36 | 37 | TARGETS = pwriter preader pmemobj_alloc.c reserve_publish.c tx.c 38 | TARGETS_LISTINGS = $(addsuffix .lst, $(TARGETS)) 39 | 40 | LIBS = -lpmemobj -lpmem -lpthread 41 | 42 | all: $(TARGETS) listings 43 | listings: $(TARGETS_LISTINGS) 44 | 45 | %: %.c 46 | $(CC) -g -o $@ $< $(LIBS) 47 | 48 | %.lst: %.c 49 | cat -n $^ > $@ 50 | 51 | clean: 52 | $(RM) $(TARGETS) $(TARGETS_LISTINGS) 53 | 54 | clobber: clean 55 | $(RM) pmemkv *.lst 56 | 57 | .PHONY: all clean clobber listings 58 | -------------------------------------------------------------------------------- /chapter07/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 7 - libpmemobj: A Native Transactional Object Store. 2 | -------------------------------------------------------------------------------- /chapter07/pmemobj_alloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * pmemobj_alloc.c - An example to show how to use 35 | * pmemobj_alloc() 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | 43 | #define die(...) do {fprintf(stderr, __VA_ARGS__); exit(1);} while(0) 44 | #define POOL "/mnt/pmem/paintball" 45 | #define LAYOUT "paintball" 46 | 47 | typedef uint32_t color; 48 | 49 | static int paintball_init(PMEMobjpool *pop, void *ptr, void *arg) 50 | { 51 | *(color *)ptr = time(0) & 0xffffff; 52 | pmemobj_persist(pop, ptr, sizeof(color)); 53 | return 0; 54 | } 55 | 56 | int main() 57 | { 58 | PMEMobjpool *pool = pmemobj_open(POOL, LAYOUT); 59 | if (!pool) { 60 | pool = pmemobj_create(POOL, LAYOUT, PMEMOBJ_MIN_POOL, 0666); 61 | if (!pool) 62 | die("Couldn't open pool: %m\n"); 63 | 64 | } 65 | PMEMoid root = pmemobj_root(pool, sizeof(PMEMoid) * 6); 66 | if (OID_IS_NULL(root)) 67 | die("Couldn't access root object.\n"); 68 | 69 | PMEMoid *chamber = (PMEMoid *)pmemobj_direct(root) + (getpid() % 6); 70 | if (OID_IS_NULL(*chamber)) { 71 | printf("Reloading.\n"); 72 | if (pmemobj_alloc(pool, chamber, sizeof(color), 0, paintball_init, 0)) 73 | die("Failed to alloc: %m\n"); 74 | } else { 75 | printf("Shooting %06x colored bullet.\n", *(color *)pmemobj_direct(*chamber)); 76 | pmemobj_free(chamber); 77 | } 78 | 79 | pmemobj_close(pool); 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /chapter07/preader.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * preader.c - Read a string from a 35 | * persistent memory pool 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | 43 | #define LAYOUT_NAME "rweg" 44 | #define MAX_BUF_LEN 31 45 | 46 | struct my_root { 47 | size_t len; 48 | char buf[MAX_BUF_LEN]; 49 | }; 50 | 51 | int 52 | main(int argc, char *argv[]) 53 | { 54 | if (argc != 2) { 55 | fprintf(stderr, "usage: %s file-name\n", argv[0]); 56 | exit(1); 57 | } 58 | 59 | PMEMobjpool *pop = pmemobj_open(argv[1], 60 | LAYOUT_NAME); 61 | 62 | if (pop == NULL) { 63 | perror("pmemobj_open"); 64 | exit(1); 65 | } 66 | 67 | PMEMoid root = pmemobj_root(pop, 68 | sizeof(struct my_root)); 69 | struct my_root *rootp = pmemobj_direct(root); 70 | 71 | if (rootp->len == strlen(rootp->buf)) 72 | printf("%s\n", rootp->buf); 73 | 74 | pmemobj_close(pop); 75 | 76 | exit(0); 77 | } 78 | -------------------------------------------------------------------------------- /chapter07/pwriter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * pwriter.c - Write a string to a 35 | * persistent memory pool 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | 43 | #define LAYOUT_NAME "rweg" 44 | #define MAX_BUF_LEN 31 45 | 46 | struct my_root { 47 | size_t len; 48 | char buf[MAX_BUF_LEN]; 49 | }; 50 | 51 | int 52 | main(int argc, char *argv[]) 53 | { 54 | if (argc != 2) { 55 | fprintf(stderr, "usage: %s file-name\n", argv[0]); 56 | exit(1); 57 | } 58 | 59 | PMEMobjpool *pop = pmemobj_create(argv[1], 60 | LAYOUT_NAME, PMEMOBJ_MIN_POOL, 0666); 61 | 62 | if (pop == NULL) { 63 | perror("pmemobj_create"); 64 | exit(1); 65 | } 66 | 67 | PMEMoid root = pmemobj_root(pop, 68 | sizeof(struct my_root)); 69 | 70 | struct my_root *rootp = pmemobj_direct(root); 71 | 72 | char buf[MAX_BUF_LEN] = "Hello PMEM World"; 73 | 74 | rootp->len = strlen(buf); 75 | pmemobj_persist(pop, &rootp->len, 76 | sizeof(rootp->len)); 77 | 78 | pmemobj_memcpy_persist(pop, rootp->buf, buf, 79 | rootp->len); 80 | 81 | pmemobj_close(pop); 82 | 83 | exit(0); 84 | } 85 | -------------------------------------------------------------------------------- /chapter07/reserve_publish.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * reserve_publish.c – An example using the 35 | * reserve/publish libpmemobj API 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | 43 | #define die(...) do {fprintf(stderr, __VA_ARGS__); exit(1);} while(0) 44 | #define POOL "/mnt/pmem/balance" 45 | 46 | static PMEMobjpool *pool; 47 | 48 | struct account { 49 | PMEMoid name; 50 | uint64_t balance; 51 | }; 52 | TOID_DECLARE(struct account, 0); 53 | 54 | /* 55 | * Even though we return the oid in a volatile register, there's no 56 | * persistent leak as all "struct account" (type 1) allocations are 57 | * reachable via POBJ_FOREACH_TYPE(). 58 | */ 59 | static PMEMoid new_account(const char *name, int deposit) 60 | { 61 | int len = strlen(name) + 1; 62 | 63 | struct pobj_action act[2]; 64 | PMEMoid str = pmemobj_reserve(pool, act + 0, len, 0); 65 | if (OID_IS_NULL(str)) 66 | die("Can't allocate string: %m\n"); 67 | /* 68 | * memcpy below must flush, but doesn't need to drain -- even just a 69 | * single drain after all flushes is enough. 70 | */ 71 | pmemobj_memcpy(pool, pmemobj_direct(str), name, len, PMEMOBJ_F_MEM_NODRAIN); 72 | TOID(struct account) acc; 73 | PMEMoid acc_oid = pmemobj_reserve(pool, act + 1, sizeof(struct account), 1); 74 | TOID_ASSIGN(acc, acc_oid); 75 | if (TOID_IS_NULL(acc)) 76 | die("Can't allocate account: %m\n"); 77 | D_RW(acc)->name = str; 78 | D_RW(acc)->balance = deposit; 79 | pmemobj_persist(pool, D_RW(acc), sizeof(struct account)); 80 | pmemobj_publish(pool, act, 2); 81 | return acc_oid; 82 | } 83 | 84 | int main() 85 | { 86 | if (!(pool = pmemobj_create(POOL, "", PMEMOBJ_MIN_POOL, 0600))) 87 | die("Can't create pool “%s”: %m\n", POOL); 88 | 89 | TOID(struct account) account_a, account_b; 90 | TOID_ASSIGN(account_a, new_account("Julius Caesar", 100)); 91 | TOID_ASSIGN(account_b, new_account("Mark Anthony", 50)); 92 | 93 | int price = 42; 94 | struct pobj_action act[2]; 95 | pmemobj_set_value(pool, &act[0], &D_RW(account_a)->balance, D_RW(account_a)->balance - price); 96 | pmemobj_set_value(pool, &act[1], &D_RW(account_b)->balance, D_RW(account_b)->balance + price); 97 | pmemobj_publish(pool, act, 2); 98 | 99 | pmemobj_close(pool); 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /chapter07/tx.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * tx.c - An example using the transaction API 35 | */ 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | #define die(...) do {fprintf(stderr, __VA_ARGS__); exit(1);} while(0) 43 | #define POOL "/mnt/pmem/balance" 44 | 45 | static PMEMobjpool *pool; 46 | 47 | 48 | struct account { 49 | PMEMoid name; 50 | uint64_t balance; 51 | }; 52 | 53 | POBJ_LAYOUT_BEGIN(a); 54 | POBJ_LAYOUT_TOID(a, struct account); 55 | POBJ_LAYOUT_END(a); 56 | 57 | /* 58 | * Even though we return the oid in a volatile register, there's no 59 | * persistent leak as all "struct account" (type 1) allocations are 60 | * reachable via POBJ_FOREACH_TYPE(). 61 | */ 62 | static PMEMoid new_account(const char *name, int deposit) 63 | { 64 | int len = strlen(name) + 1; 65 | 66 | struct pobj_action act[2]; 67 | PMEMoid str = pmemobj_reserve(pool, act + 0, len, 0); 68 | if (OID_IS_NULL(str)) 69 | die("Can't allocate string: %m\n"); 70 | /* 71 | * memcpy below must flush, but doesn't need to drain -- even just a 72 | * single drain after all flushes is enough. 73 | */ 74 | pmemobj_memcpy(pool, pmemobj_direct(str), name, len, PMEMOBJ_F_MEM_NODRAIN); 75 | TOID(struct account) acc; 76 | PMEMoid acc_oid = pmemobj_reserve(pool, act + 1, sizeof(struct account), 1); 77 | TOID_ASSIGN(acc, acc_oid); 78 | if (TOID_IS_NULL(acc)) 79 | die("Can't allocate account: %m\n"); 80 | D_RW(acc)->name = str; 81 | D_RW(acc)->balance = deposit; 82 | pmemobj_persist(pool, D_RW(acc), sizeof(struct account)); 83 | pmemobj_publish(pool, act, 2); 84 | return acc_oid; 85 | } 86 | 87 | int main() 88 | { 89 | if (!(pool = pmemobj_create(POOL, "", PMEMOBJ_MIN_POOL, 0600))) 90 | die("Can't create pool “%s”: %m\n", POOL); 91 | 92 | TOID(struct account) account_a, account_b; 93 | TOID_ASSIGN(account_a, new_account("Julius Caesar", 100)); 94 | TOID_ASSIGN(account_b, new_account("Mark Anthony", 50)); 95 | 96 | int price = 42; 97 | TX_BEGIN(pool) { 98 | TX_ADD_DIRECT(&D_RW(account_a)->balance); 99 | TX_ADD_DIRECT(&D_RW(account_b)->balance); 100 | D_RW(account_a)->balance -= price; 101 | D_RW(account_b)->balance += price; 102 | } TX_END 103 | 104 | pmemobj_close(pool); 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /chapter08/.gitignore: -------------------------------------------------------------------------------- 1 | allocation 2 | non_trivial_copy 3 | p 4 | queue 5 | transaction 6 | volatile_pointers 7 | -------------------------------------------------------------------------------- /chapter08/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, Intel Corporation 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # 15 | # * Neither the name of Intel Corporation nor the names of its 16 | # contributors may be used to endorse or promote products derived 17 | # from this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | # 32 | # Makefile for chapter1 examples 33 | # 34 | 35 | .SUFFIXES: .lst 36 | 37 | all: transaction p allocation listings non_trivial_copy volatile_pointers queue containers 38 | 39 | listings: transaction.lst p.lst allocation.lst non_trivial_copy.lst volatile_pointers.lst persistent_queue.lst volatile_queue.lst queue.lst containers.lst 40 | 41 | %.lst: %.cpp 42 | cat -n $^ > $@ 43 | 44 | %.lst: %.hpp 45 | cat -n $^ > $@ 46 | 47 | transaction: transaction.cpp 48 | $(CXX) -o transaction transaction.cpp -lpmemobj 49 | 50 | p: p.cpp 51 | $(CXX) -o p p.cpp -lpmemobj 52 | 53 | allocation: allocation.cpp 54 | $(CXX) -o allocation allocation.cpp -lpmemobj 55 | 56 | non_trivial_copy: non_trivial_copy.cpp 57 | $(CXX) -c -o non_trivial_copy non_trivial_copy.cpp 58 | 59 | volatile_pointers: volatile_pointers.cpp 60 | $(CXX) -c -o volatile_pointers volatile_pointers.cpp 61 | 62 | queue: queue.cpp 63 | $(CXX) -o queue queue.cpp -lpmemobj 64 | 65 | containers: containers.cpp 66 | $(CXX) -o containers containers.cpp -lpmemobj 67 | 68 | clean: 69 | $(RM) *.o core a.out 70 | 71 | clobber: clean 72 | $(RM) transaction p allocation volatile_pointers queue non_trivial_copy containers *.lst 73 | 74 | .PHONY: all clean clobber listings 75 | -------------------------------------------------------------------------------- /chapter08/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 8 - libpmemobj++: The adaptable language - C++ and Persistent Memory 2 | 3 | libpmemobj++ >= 1.8 is required to compile them. 4 | -------------------------------------------------------------------------------- /chapter08/allocation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | struct my_data { 40 | my_data(int a, int b): a(a), b(b) { 41 | 42 | } 43 | 44 | int a; 45 | int b; 46 | }; 47 | 48 | struct root { 49 | pmem::obj::persistent_ptr mdata; 50 | }; 51 | 52 | int main(int argc, char *argv[]) { 53 | // pool created using: pmempool create obj -s 8M -l lx /daxfs/file 54 | auto pop = pmem::obj::pool::open("/daxfs/file", "tx"); 55 | 56 | auto r = pop.root(); 57 | 58 | pmem::obj::transaction::run(pop, [&]() { 59 | r->mdata = pmem::obj::make_persistent(1, 2); 60 | }); 61 | 62 | pmem::obj::transaction::run(pop, [&]() { 63 | pmem::obj::delete_persistent(r->mdata); 64 | }); 65 | pmem::obj::make_persistent_atomic(pop, r->mdata, 2, 3); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /chapter08/containers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | using vector_type = pmem::obj::vector; 40 | 41 | struct root { 42 | pmem::obj::persistent_ptr vec_p; 43 | }; 44 | 45 | int main(int argc, char *argv[]) 46 | { 47 | if (argc < 2) { 48 | std::cerr << "Usage: " << argv[0] << " path_to_pool" << std::endl; 49 | return 1; 50 | } 51 | 52 | auto path = argv[1]; 53 | pmem::obj::pool pool; 54 | 55 | try { 56 | pool = pmem::obj::pool::open(path, "vector"); 57 | } catch(pmem::pool_error &e) { 58 | std::cerr << e.what() << std::endl; 59 | std::cerr << "To create pool run: pmempool create obj --layout=vector -s 100M path_to_pool" << std::endl; 60 | } 61 | 62 | auto root = pool.root(); 63 | 64 | /* creating pmem::obj::vector in transaction */ 65 | pmem::obj::transaction::run(pool, [&] { 66 | root->vec_p = pmem::obj::make_persistent(/* optional constructor arguments */); 67 | }); 68 | 69 | vector_type &pvector = *(root->vec_p); 70 | 71 | pvector.reserve(10); 72 | assert(pvector.size() == 0); 73 | assert(pvector.capacity() == 10); 74 | 75 | pvector = {0, 1, 2, 3, 4}; 76 | assert(pvector.size() == 5); 77 | assert(pvector.capacity() == 10); 78 | 79 | pvector.shrink_to_fit(); 80 | assert(pvector.size() == 5); 81 | assert(pvector.capacity() == 5); 82 | 83 | for (unsigned i = 0; i < pvector.size(); ++i) 84 | assert(pvector.const_at(i) == static_cast(i)); 85 | 86 | pvector.push_back(5); 87 | assert(pvector.const_at(5) == 5); 88 | assert(pvector.size() == 6); 89 | 90 | pvector.emplace(pvector.cbegin(), pvector.back()); 91 | assert(pvector.const_at(0) == 5); 92 | for (unsigned i = 1; i < pvector.size(); ++i) 93 | assert(pvector.const_at(i) == static_cast(i - 1)); 94 | 95 | std::vector stdvector = {5, 4, 3, 2, 1}; 96 | pvector = stdvector; 97 | 98 | try { 99 | pmem::obj::transaction::run(pool, [&] { 100 | for (auto &e : pvector) 101 | e++; 102 | /* 6, 5, 4, 3, 2 */ 103 | 104 | for (auto it = pvector.begin(); it != pvector.end(); it++) 105 | *it += 2; 106 | /* 8, 7, 6, 5, 4 */ 107 | 108 | for (unsigned i = 0; i < pvector.size(); i++) 109 | pvector[i]--; 110 | /* 7, 6, 5, 4, 3 */ 111 | 112 | std::sort(pvector.begin(), pvector.end()); 113 | for (unsigned i = 0; i < pvector.size(); ++i) 114 | assert(pvector.const_at(i) == static_cast(i + 3)); 115 | 116 | pmem::obj::transaction::abort(0); 117 | }); 118 | } catch (pmem::manual_tx_abort &) { 119 | /* expected transaction abort */ 120 | } catch (std::exception &e) { 121 | std::cerr << e.what() << std::endl; 122 | } 123 | 124 | assert(pvector == stdvector); /* pvector element's value was rolled back */ 125 | 126 | try { 127 | pmem::obj::delete_persistent(&pvector); 128 | } catch (std::exception &e) { 129 | } 130 | 131 | return 0; 132 | } 133 | -------------------------------------------------------------------------------- /chapter08/non_trivial_copy.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | 35 | class nonTriviallyCopyable { 36 | private: 37 | int* i; 38 | public: 39 | nonTriviallyCopyable (const nonTriviallyCopyable & from) 40 | { 41 | /* perform non-trivial copying routine */ 42 | i = new int(*from.i); 43 | } 44 | }; 45 | -------------------------------------------------------------------------------- /chapter08/p.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | struct bad_example { 40 | int some_int; 41 | float some_float; 42 | }; 43 | 44 | struct good_example { 45 | pmem::obj::p pint; 46 | pmem::obj::p pfloat; 47 | }; 48 | 49 | struct root { 50 | bad_example bad; 51 | good_example good; 52 | }; 53 | 54 | int main(int argc, char *argv[]) { 55 | // pool created using: pmempool create obj -s 8M -l p /daxfs/file 56 | auto pop = pmem::obj::pool::open("/daxfs/file", "p"); 57 | 58 | auto r = pop.root(); 59 | 60 | pmem::obj::transaction::run(pop, [&]() { 61 | r->bad.some_int = 10; 62 | r->good.pint = 10; 63 | 64 | r->good.pint += 1; 65 | }); 66 | 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /chapter08/persistent_queue.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | struct queue_node { 45 | pmem::obj::p value; 46 | pmem::obj::persistent_ptr next; 47 | }; 48 | 49 | struct queue { 50 | void 51 | push(pmem::obj::pool_base &pop, int value) 52 | { 53 | pmem::obj::transaction::run(pop, [&]{ 54 | auto node = pmem::obj::make_persistent(); 55 | node->value = value; 56 | node->next = nullptr; 57 | 58 | if (head == nullptr) { 59 | head = tail = node; 60 | } else { 61 | tail->next = node; 62 | tail = node; 63 | } 64 | }); 65 | } 66 | 67 | int 68 | pop(pmem::obj::pool_base &pop) 69 | { 70 | int value; 71 | pmem::obj::transaction::run(pop, [&]{ 72 | if (head == nullptr) 73 | throw std::out_of_range("no elements"); 74 | 75 | auto head_ptr = head; 76 | value = head->value; 77 | 78 | head = head->next; 79 | pmem::obj::delete_persistent(head_ptr); 80 | 81 | if (head == nullptr) 82 | tail = nullptr; 83 | }); 84 | 85 | return value; 86 | } 87 | 88 | void 89 | show() 90 | { 91 | auto node = head; 92 | while (node != nullptr) { 93 | std::cout << "show: " << node->value << std::endl; 94 | node = node->next; 95 | } 96 | 97 | std::cout << std::endl; 98 | } 99 | 100 | private: 101 | pmem::obj::persistent_ptr head = nullptr; 102 | pmem::obj::persistent_ptr tail = nullptr; 103 | }; 104 | -------------------------------------------------------------------------------- /chapter08/queue.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * queue.cpp -- implementation of a volatile queue. 35 | * create the pool for this program using pmempool, for example: 36 | * pmempool create obj --layout=queue -s 1G queue_pool 37 | */ 38 | 39 | #include "persistent_queue.hpp" 40 | 41 | enum queue_op { 42 | PUSH, 43 | POP, 44 | SHOW, 45 | EXIT, 46 | MAX_OPS, 47 | }; 48 | 49 | const char *ops_str[MAX_OPS] = {"push", "pop", "show", "exit"}; 50 | 51 | queue_op 52 | parse_queue_ops(const std::string &ops) 53 | { 54 | for (int i = 0; i < MAX_OPS; i++) { 55 | if (ops == ops_str[i]) { 56 | return (queue_op)i; 57 | } 58 | } 59 | return MAX_OPS; 60 | } 61 | 62 | int 63 | main(int argc, char *argv[]) 64 | { 65 | if (argc < 2) { 66 | std::cerr << "Usage: " << argv[0] << " path_to_pool" << std::endl; 67 | return 1; 68 | } 69 | 70 | auto path = argv[1]; 71 | pmem::obj::pool pool; 72 | 73 | try { 74 | pool = pmem::obj::pool::open(path, "queue"); 75 | } catch(pmem::pool_error &e) { 76 | std::cerr << e.what() << std::endl; 77 | std::cerr << "To create pool run: pmempool create obj --layout=queue -s 100M path_to_pool" << std::endl; 78 | } 79 | 80 | auto q = pool.root(); 81 | 82 | while (1) { 83 | std::cout << "[push value|pop|show|exit]" << std::endl; 84 | 85 | std::string command; 86 | std::cin >> command; 87 | 88 | // parse string 89 | auto ops = parse_queue_ops(std::string(command)); 90 | 91 | switch (ops) { 92 | case PUSH: { 93 | int value; 94 | std::cin >> value; 95 | 96 | q->push(pool, value); 97 | 98 | break; 99 | } 100 | case POP: { 101 | std::cout << q->pop(pool) << std::endl; 102 | break; 103 | } 104 | case SHOW: { 105 | q->show(); 106 | break; 107 | } 108 | case EXIT: { 109 | exit(0); 110 | } 111 | default: { 112 | std::cerr << "unknown ops" << std::endl; 113 | exit(0); 114 | } 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /chapter08/transaction.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | struct root { 38 | }; 39 | 40 | int main(int argc, char *argv[]) { 41 | // pool created using: pmempool create obj -s 8M -l lx /daxfs/file 42 | auto pop = pmem::obj::pool::open("/daxfs/file", "tx"); 43 | 44 | auto r = pop.root(); 45 | 46 | // start a transaction 47 | pmem::obj::transaction::run(pop, [&]() { 48 | // do transactional work 49 | }); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /chapter08/volatile_pointers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | struct root { 40 | int* vptr1; 41 | int* vptr2; 42 | }; 43 | 44 | int main(int argc, char *argv[]) { 45 | // pool created using: pmempool create obj -s 8M -l lx /daxfs/file 46 | auto pop = pmem::obj::pool::open("/daxfs/file", "tx"); 47 | 48 | auto r = pop.root(); 49 | 50 | int a1 = 1; 51 | 52 | pmem::obj::transaction::run(pop, [&](){ 53 | auto ptr = pmem::obj::make_persistent(0); 54 | r->vptr1 = ptr.get(); 55 | r->vptr2 = &a1; 56 | }); 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /chapter08/volatile_queue.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | struct queue_node { 39 | int value; 40 | struct queue_node *next; 41 | }; 42 | 43 | struct queue { 44 | void 45 | push(int value) 46 | { 47 | auto node = new queue_node; 48 | node->value = value; 49 | node->next = nullptr; 50 | 51 | if (head == nullptr) { 52 | head = tail = node; 53 | } else { 54 | tail->next = node; 55 | tail = node; 56 | } 57 | } 58 | 59 | int 60 | pop() 61 | { 62 | if (head == nullptr) 63 | throw std::out_of_range("no elements"); 64 | 65 | auto head_ptr = head; 66 | auto value = head->value; 67 | 68 | head = head->next; 69 | delete head_ptr; 70 | 71 | if (head == nullptr) 72 | tail = nullptr; 73 | 74 | return value; 75 | } 76 | 77 | void 78 | show() 79 | { 80 | auto node = head; 81 | while (node != nullptr) { 82 | std::cout << "show: " << node->value << std::endl; 83 | node = node->next; 84 | } 85 | 86 | std::cout << std::endl; 87 | } 88 | 89 | private: 90 | queue_node *head = nullptr; 91 | queue_node *tail = nullptr; 92 | }; 93 | -------------------------------------------------------------------------------- /chapter09/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, Intel Corporation 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # 15 | # * Neither the name of Intel Corporation nor the names of its 16 | # contributors may be used to endorse or promote products derived 17 | # from this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | # 32 | # Makefile for chapter9 33 | # 34 | 35 | .SUFFIXES: .lst 36 | 37 | all: config_structure phonebook listings 38 | 39 | listings: config_structure.lst phonebook.lst 40 | 41 | %.lst: %.c* 42 | cat -n $^ > $@ 43 | 44 | config_structure: config_structure.c 45 | $(CXX) -o config_structure config_structure.c -lpmemkv 46 | 47 | phonebook: phonebook.cpp 48 | $(CXX) -o phonebook phonebook.cpp -lpmemkv 49 | 50 | clean: 51 | $(RM) *.o core a.out 52 | 53 | clobber: clean 54 | $(RM) config_structure phonebook *.lst 55 | 56 | .PHONY: all clean clobber listings 57 | -------------------------------------------------------------------------------- /chapter09/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 9 - pmemkv: A Persistent In-Memory Key-Value Store. 2 | -------------------------------------------------------------------------------- /chapter09/config_structure.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* example of config structure usage */ 34 | 35 | #include 36 | #include "pmemkv_config.h" 37 | 38 | const char* PATH = "/daxfs/kvfile"; 39 | const uint64_t SIZE = 1024 * 1024 * 1024; // 1 Gig 40 | 41 | int main() { 42 | 43 | assert(NULL != config_setup(PATH, 1, SIZE)); 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /chapter09/js-phonebook.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * js-phonebook.js -- phonebook example with pmemkv JavaScript bindings 35 | */ 36 | 37 | const Database = require('./lib/all'); 38 | 39 | function assert(condition) { 40 | if (!condition) throw new Error('Assert failed'); 41 | } 42 | 43 | console.log('Create a key-value store using the "cmap" engine'); 44 | const db = new Database('cmap', '{"path":"/daxfs/kvfile", "size":1073741824, "force_create":1}'); 45 | 46 | console.log('Add 2 entries with name and phone number'); 47 | db.put('John', '123-456-789'); 48 | db.put('Kate', '987-654-321'); 49 | 50 | console.log('Count elements'); 51 | assert(db.count_all == 2); 52 | 53 | console.log('Read key back'); 54 | assert(db.get('John') === '123-456-789'); 55 | 56 | console.log('Iterate through the phonebook'); 57 | db.get_all((k, v) => console.log(`name: ${k}, number: ${v}`)); 58 | 59 | console.log('Remove one record'); 60 | db.remove('John'); 61 | 62 | console.log('Lookup of removed record'); 63 | assert(!db.exists('John')); 64 | 65 | console.log('Stopping engine'); 66 | db.stop(); 67 | -------------------------------------------------------------------------------- /chapter09/phonebook.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * phonebook.cpp -- phonebook example with pmemkv 35 | */ 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include "pmemkv_config.h" 42 | 43 | using namespace pmem::kv; 44 | 45 | auto PATH = "/daxfs/kvfile"; 46 | const uint64_t FORCE_CREATE = 1; 47 | const uint64_t SIZE = 1024 * 1024 * 1024; // 1 Gig 48 | 49 | int main() { 50 | // Prepare config for pmemkv database 51 | pmemkv_config *cfg = config_setup(PATH, FORCE_CREATE, SIZE); 52 | assert(cfg != nullptr); 53 | 54 | // Create a key-value store using the "cmap" engine. 55 | db kv; 56 | 57 | if (kv.open("cmap", config(cfg)) != status::OK) { 58 | std::cerr << db::errormsg() << std::endl; 59 | return 1; 60 | } 61 | 62 | // Add 2 entries with name and phone number 63 | if (kv.put("John", "123-456-789") != status::OK) { 64 | std::cerr << db::errormsg() << std::endl; 65 | return 1; 66 | } 67 | if (kv.put("Kate", "987-654-321") != status::OK) { 68 | std::cerr << db::errormsg() << std::endl; 69 | return 1; 70 | } 71 | 72 | // Count elements 73 | size_t cnt; 74 | if (kv.count_all(cnt) != status::OK) { 75 | std::cerr << db::errormsg() << std::endl; 76 | return 1; 77 | } 78 | assert(cnt == 2); 79 | 80 | // Read key back 81 | std::string number; 82 | if (kv.get("John", &number) != status::OK) { 83 | std::cerr << db::errormsg() << std::endl; 84 | return 1; 85 | } 86 | assert(number == "123-456-789"); 87 | 88 | // Iterate through the phonebook 89 | if (kv.get_all([](string_view name, string_view number) { 90 | std::cout << "name: " << name.data() << 91 | ", number: " << number.data() << std::endl; 92 | return 0; 93 | }) != status::OK) { 94 | std::cerr << db::errormsg() << std::endl; 95 | return 1; 96 | } 97 | 98 | // Remove one record 99 | if (kv.remove("John") != status::OK) { 100 | std::cerr << db::errormsg() << std::endl; 101 | return 1; 102 | } 103 | 104 | // Look for removed record 105 | assert(kv.exists("John") == status::NOT_FOUND); 106 | 107 | // Try to use one of methods of ordered engines 108 | assert(kv.get_above("John", [](string_view key, string_view value) { 109 | std::cout << "This callback should never be called" << std::endl; 110 | return 1; 111 | }) == status::NOT_SUPPORTED); 112 | 113 | // Close database (optional) 114 | kv.close(); 115 | 116 | return 0; 117 | } 118 | -------------------------------------------------------------------------------- /chapter09/pmemkv_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* example of config structure usage */ 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | pmemkv_config* config_setup(const char* path, const uint64_t fcreate, const uint64_t size) { 40 | pmemkv_config *cfg = pmemkv_config_new(); 41 | assert(cfg != nullptr); 42 | 43 | if (pmemkv_config_put_string(cfg, "path", path) != PMEMKV_STATUS_OK) { 44 | fprintf(stderr, "%s", pmemkv_errormsg()); 45 | return NULL; 46 | } 47 | 48 | if (pmemkv_config_put_uint64(cfg, "force_create", fcreate) != PMEMKV_STATUS_OK) { 49 | fprintf(stderr, "%s", pmemkv_errormsg()); 50 | return NULL; 51 | } 52 | 53 | if (pmemkv_config_put_uint64(cfg, "size", size) != PMEMKV_STATUS_OK) { 54 | fprintf(stderr, "%s", pmemkv_errormsg()); 55 | return NULL; 56 | } 57 | 58 | return cfg; 59 | } 60 | -------------------------------------------------------------------------------- /chapter10/.gitignore: -------------------------------------------------------------------------------- 1 | pmem_allocator 2 | pmem_config 3 | pmem_detect_kind 4 | vector_of_strings 5 | vmemcache 6 | -------------------------------------------------------------------------------- /chapter10/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, Intel Corporation 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # 15 | # * Neither the name of Intel Corporation nor the names of its 16 | # contributors may be used to endorse or promote products derived 17 | # from this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | # 32 | # Makefile for chapter10 examples 33 | # 34 | 35 | TARGETS = pmem_allocator vector_of_strings pmem_config pmem_detect_kind vmemcache 36 | TARGETS_LISTINGS = $(addsuffix .lst, $(TARGETS)) 37 | 38 | all: $(TARGETS) listings 39 | listings: $(TARGETS_LISTINGS) 40 | 41 | %.lst: %.cpp 42 | expand -t 4 < $^ | cat -n > $@ 43 | 44 | %.lst: %.c 45 | expand -t 4 < $^ | cat -n > $@ 46 | 47 | %: %.cpp 48 | $(CXX) -g -o $@ $< -lmemkind -std=c++11 49 | 50 | %: %.c 51 | $(CC) -g -o $@ $< -lmemkind -lvmemcache 52 | 53 | clean: 54 | $(RM) *.o core a.out 55 | 56 | clobber: clean 57 | $(RM) $(TARGETS) $(TARGETS_LISTINGS) 58 | 59 | .PHONY: all clean clobber listings 60 | -------------------------------------------------------------------------------- /chapter10/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 10 - Volatile Use of Persistent Memory 2 | -------------------------------------------------------------------------------- /chapter10/pmem_allocator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * pmem_allocator.cpp - pmem::allocator with std:vector 35 | */ 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | int main(int argc, char *argv[]) { 42 | const size_t pmem_max_size = 64 * 1024 * 1024; //64 MB 43 | const std::string pmem_dir("/daxfs"); 44 | 45 | // Create allocator object 46 | libmemkind::pmem::allocator 47 | alc(pmem_dir, pmem_max_size); 48 | 49 | // Create std::vector with our allocator. 50 | std::vector> v(alc); 52 | 53 | for (int i = 0; i < 100; ++i) 54 | v.push_back(i); 55 | 56 | for (int i = 0; i < 100; ++i) 57 | assert(v[i] == i); 58 | 59 | exit(0); 60 | } 61 | -------------------------------------------------------------------------------- /chapter10/pmem_config.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * pmem_config.c - demonstrate configuration functions 35 | */ 36 | 37 | #include 38 | 39 | #include 40 | #include 41 | #include 42 | 43 | #define PMEM_MAX_SIZE (1024 * 1024 * 32) 44 | 45 | char path[PATH_MAX] = "/daxfs"; 46 | 47 | void memkind_fatal(int err) 48 | { 49 | char error_message[MEMKIND_ERROR_MESSAGE_SIZE]; 50 | 51 | memkind_error_message(err, error_message, 52 | MEMKIND_ERROR_MESSAGE_SIZE); 53 | fprintf(stderr, "%s\n", error_message); 54 | exit(1); 55 | } 56 | 57 | int main(int argc, char *argv[]) 58 | { 59 | struct memkind *pmem_kind; 60 | int err; 61 | 62 | if (argc > 2) { 63 | fprintf(stderr, 64 | "Usage: %s [pmem_kind_dir_path]\n", 65 | argv[0]); 66 | exit(1); 67 | } else if (argc == 2 && 68 | (realpath(argv[1], path) == NULL)) { 69 | perror(argv[1]); 70 | exit(1); 71 | } 72 | 73 | struct memkind_config *test_cfg = 74 | memkind_config_new(); 75 | if (test_cfg == NULL) { 76 | fprintf(stderr, 77 | "memkind_config_new: out of memory\n"); 78 | exit(1); 79 | } 80 | 81 | memkind_config_set_path(test_cfg, path); 82 | memkind_config_set_size(test_cfg, PMEM_MAX_SIZE); 83 | memkind_config_set_memory_usage_policy(test_cfg, 84 | MEMKIND_MEM_USAGE_POLICY_CONSERVATIVE); 85 | 86 | // Create PMEM partition with the configuration 87 | err = memkind_create_pmem_with_config(test_cfg, 88 | &pmem_kind); 89 | if (err) { 90 | memkind_fatal(err); 91 | } 92 | 93 | err = memkind_destroy_kind(pmem_kind); 94 | if (err) { 95 | memkind_fatal(err); 96 | } 97 | 98 | memkind_config_delete(test_cfg); 99 | 100 | exit(0); 101 | } 102 | -------------------------------------------------------------------------------- /chapter10/pmem_detect_kind.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * pmem_detect_kind.c - illustrate the kind detection API 35 | */ 36 | 37 | #include 38 | 39 | #include 40 | #include 41 | #include 42 | 43 | char path[PATH_MAX] = "/daxfs"; 44 | 45 | void memkind_fatal(int err) 46 | { 47 | char error_message[MEMKIND_ERROR_MESSAGE_SIZE]; 48 | 49 | memkind_error_message(err, error_message, 50 | MEMKIND_ERROR_MESSAGE_SIZE); 51 | fprintf(stderr, "%s\n", error_message); 52 | exit(1); 53 | } 54 | 55 | int main(int argc, char *argv[]) 56 | { 57 | struct memkind *pmem_kind; 58 | int err; 59 | void *buf0; 60 | void *buf1; 61 | 62 | if (argc > 2) { 63 | fprintf(stderr, 64 | "Usage: %s [pmem_kind_dir_path]\n", 65 | argv[0]); 66 | exit(1); 67 | } else if (argc == 2 && (realpath(argv[1], path) 68 | == NULL)) { 69 | perror(argv[1]); 70 | exit(1); 71 | } 72 | 73 | err = memkind_create_pmem(path, 0, &pmem_kind); 74 | if (err) { 75 | memkind_fatal(err); 76 | } 77 | 78 | /* do some allocations... */ 79 | buf0 = memkind_malloc(pmem_kind, 1000); 80 | buf1 = memkind_malloc(MEMKIND_DEFAULT, 1000); 81 | 82 | /* look up the kind of an allocation */ 83 | if (memkind_detect_kind(buf0) == MEMKIND_DEFAULT) { 84 | printf("buf0 is DRAM\n"); 85 | } else { 86 | printf("buf0 is pmem\n"); 87 | } 88 | 89 | err = memkind_destroy_kind(pmem_kind); 90 | if (err) { 91 | memkind_fatal(err); 92 | } 93 | 94 | exit(0); 95 | } 96 | -------------------------------------------------------------------------------- /chapter10/vector_of_strings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * vector_of_strings.cpp - demonstrate a vector of strings in pmem 35 | */ 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | typedef libmemkind::pmem::allocator str_alloc_type; 45 | 46 | typedef std::basic_string, str_alloc_type> pmem_string; 47 | 48 | typedef libmemkind::pmem::allocator vec_alloc_type; 49 | 50 | typedef std::vector > vector_type; 51 | 52 | int main(int argc, char *argv[]) { 53 | const size_t pmem_max_size = 64 * 1024 * 1024; //64 MB 54 | const std::string pmem_dir("/daxfs"); 55 | 56 | // Create allocator object 57 | vec_alloc_type alc(pmem_dir, pmem_max_size); 58 | // Create std::vector with our allocator. 59 | vector_type v(alc); 60 | 61 | v.emplace_back("Foo"); 62 | v.emplace_back("Bar"); 63 | 64 | for (auto str : v) { 65 | std::cout << str << std::endl; 66 | } 67 | 68 | exit(0); 69 | } 70 | -------------------------------------------------------------------------------- /chapter10/vmemcache.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020 Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * vmemcache.c - example showing how to vmemcahe is used 35 | */ 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | 42 | #define STR_AND_LEN(x) (x), strlen(x) 43 | 44 | VMEMcache *cache; 45 | 46 | void on_miss(VMEMcache *cache, const void *key, 47 | size_t key_size, void *arg) 48 | { 49 | vmemcache_put(cache, STR_AND_LEN("meow"), 50 | STR_AND_LEN("Cthulhu fthagn")); 51 | } 52 | 53 | void get(const char *key) 54 | { 55 | char buf[128]; 56 | ssize_t len = vmemcache_get(cache, 57 | STR_AND_LEN(key), buf, sizeof(buf), 0, NULL); 58 | if (len >= 0) 59 | printf("%.*s\n", (int)len, buf); 60 | else 61 | printf("(key not found: %s)\n", key); 62 | } 63 | 64 | int main() 65 | { 66 | cache = vmemcache_new(); 67 | if (vmemcache_add(cache, "/daxfs")) { 68 | fprintf(stderr, "error: vmemcache_add: %s\n", 69 | vmemcache_errormsg()); 70 | exit(1); 71 | } 72 | 73 | // Query a non-existent key 74 | get("meow"); 75 | 76 | // Insert then query 77 | vmemcache_put(cache, STR_AND_LEN("bark"), 78 | STR_AND_LEN("Lorem ipsum")); 79 | get("bark"); 80 | 81 | // Install an on-miss handler 82 | vmemcache_callback_on_miss(cache, on_miss, 0); 83 | get("meow"); 84 | 85 | vmemcache_delete(cache); 86 | 87 | exit(0); 88 | } 89 | -------------------------------------------------------------------------------- /chapter11/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, Intel Corporation 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # 15 | # * Neither the name of Intel Corporation nor the names of its 16 | # contributors may be used to endorse or promote products derived 17 | # from this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | # 32 | # Makefile for chapter11 examples 33 | # 34 | 35 | .SUFFIXES: .lst 36 | 37 | PROGS = data_oriented_design simplekv simplekv_rebuild versioning_insert 38 | 39 | all: $(PROGS) listings 40 | 41 | listings: simplekv.lst simplekv_rebuild.lst data_oriented_design.lst versioning_insert.lst 42 | 43 | simplekv.lst: simplekv.hpp 44 | cat -n $^ > $@ 45 | 46 | simplekv_rebuild.lst: simplekv_rebuild.hpp 47 | cat -n $^ > $@ 48 | 49 | data_oriented_design.lst: data_oriented_design.cpp 50 | cat -n $^ > $@ 51 | 52 | versioning_insert.lst: versioning_insert.cpp 53 | cat -n $^ > $@ 54 | 55 | simplekv: simplekv.cpp 56 | $(CXX) -o simplekv simplekv.cpp -lpmemobj 57 | 58 | simplekv_rebuild: simplekv_rebuild.cpp 59 | $(CXX) -o simplekv_rebuild simplekv_rebuild.cpp -lpmemobj 60 | 61 | data_oriented_design: data_oriented_design.cpp 62 | $(CXX) -o data_oriented_design data_oriented_design.cpp -lpmemobj 63 | 64 | versioning_insert: versioning_insert.cpp 65 | $(CXX) -o versioning_insert versioning_insert.cpp -g -lpmemobj 66 | 67 | clean: 68 | $(RM) *.o core a.out 69 | 70 | clobber: clean 71 | $(RM) $(PROGS) *.lst 72 | 73 | .PHONY: all clean clobber listings 74 | -------------------------------------------------------------------------------- /chapter11/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 11 - Designing Data Structures for Persistent Memory. 2 | 3 | simplekv.hpp and simplekv_rebuild.hpp contain code from from examples from 4 | 11.5.1 and 11.5.2 respectively. simplekv.cpp and simplekv_rebuild.cpp 5 | implement pool handling and command line interface for those examples. 6 | 7 | Pseudocode from 11.5.3 is in b+tree_insert.cpp 8 | 9 | libpmemobj++ >= 1.8 is required to compile them. 10 | -------------------------------------------------------------------------------- /chapter11/data_oriented_design.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | #include "libpmemobj++/container/array.hpp" 34 | #include "libpmemobj++/pool.hpp" 35 | #include "libpmemobj++/transaction.hpp" 36 | 37 | struct soa { 38 | int a[1000]; 39 | int b[1000]; 40 | }; 41 | 42 | struct root { 43 | soa soa_records; 44 | std::pair aos_records[1000]; 45 | }; 46 | 47 | int main() 48 | { 49 | pmem::obj::pool pop; 50 | 51 | try { 52 | pop = pmem::obj::pool::create("/daxfs/pmpool", "data_oriented", 53 | PMEMOBJ_MIN_POOL, 0666); 54 | 55 | auto root = pop.root(); 56 | 57 | pmem::obj::transaction::run(pop, [&]{ 58 | pmem::obj::transaction::snapshot(&root->soa_records); 59 | for (int i = 0; i < 1000; i++) { 60 | root->soa_records.a[i]++; 61 | } 62 | 63 | for (int i = 0; i < 1000; i++) { 64 | pmem::obj::transaction::snapshot( 65 | &root->aos_records[i].first); 66 | root->aos_records[i].first++; 67 | } 68 | }); 69 | } catch (std::exception &e) { 70 | std::cerr << e.what() << std::endl; 71 | return 1; 72 | } 73 | 74 | pop.close(); 75 | 76 | return 0; 77 | } 78 | 79 | -------------------------------------------------------------------------------- /chapter11/simplekv.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * simplekv.cpp -- implementation of simple kv which uses vector to hold 35 | * values, string as a key and array to hold buckets 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | #include "simplekv.hpp" 43 | 44 | using kv_type = simple_kv; 45 | 46 | struct root { 47 | pmem::obj::persistent_ptr kv; 48 | }; 49 | 50 | void 51 | show_usage(char *argv[]) 52 | { 53 | std::cerr << "usage: " << argv[0] 54 | << " file-name [get key|put key value]" << std::endl; 55 | } 56 | 57 | int 58 | main(int argc, char *argv[]) 59 | { 60 | if (argc < 3) { 61 | show_usage(argv); 62 | return 1; 63 | } 64 | 65 | const char *path = argv[1]; 66 | 67 | pmem::obj::pool pop; 68 | 69 | try { 70 | pop = pmem::obj::pool::open(path, "simplekv"); 71 | auto r = pop.root(); 72 | 73 | if (r->kv == nullptr) { 74 | pmem::obj::transaction::run(pop, [&] { 75 | r->kv = pmem::obj::make_persistent(); 76 | }); 77 | } 78 | 79 | if (std::string(argv[2]) == "get" && argc == 4) 80 | std::cout << r->kv->get(argv[3]) << std::endl; 81 | else if (std::string(argv[2]) == "put" && argc == 5) 82 | r->kv->put(argv[3], std::stoi(argv[4])); 83 | else { 84 | show_usage(argv); 85 | 86 | pop.close(); 87 | 88 | return 1; 89 | } 90 | } catch (pmem::pool_error &e) { 91 | std::cerr << e.what() << std::endl; 92 | std::cerr 93 | << "To create pool run: pmempool create obj --layout=simplekv -s 100M path_to_pool" 94 | << std::endl; 95 | } catch (std::exception &e) { 96 | std::cerr << e.what() << std::endl; 97 | } 98 | 99 | pop.close(); 100 | 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /chapter11/simplekv.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * simplekv.hpp -- implementation of simple kv which uses vector to hold 35 | * values, string as a key and array to hold buckets 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | 48 | #include 49 | #include 50 | #include 51 | 52 | /** 53 | * Value - type of the value stored in hashmap 54 | * N - number of buckets in hashmap 55 | */ 56 | template 57 | class simple_kv { 58 | private: 59 | using key_type = pmem::obj::string; 60 | using bucket_type = pmem::obj::vector< 61 | std::pair>; 62 | using bucket_array_type = pmem::obj::array; 63 | using value_vector = pmem::obj::vector; 64 | 65 | bucket_array_type buckets; 66 | value_vector values; 67 | 68 | public: 69 | simple_kv() = default; 70 | 71 | const Value & 72 | get(const std::string &key) const 73 | { 74 | auto index = std::hash{}(key) % N; 75 | 76 | for (const auto &e : buckets[index]) { 77 | if (e.first == key) 78 | return values[e.second]; 79 | } 80 | 81 | throw std::out_of_range("no entry in simplekv"); 82 | } 83 | 84 | void 85 | put(const std::string &key, const Value &val) 86 | { 87 | auto index = std::hash{}(key) % N; 88 | 89 | /* get pool on which this simple_kv resides */ 90 | auto pop = pmem::obj::pool_by_vptr(this); 91 | 92 | /* search for element with specified key - if found 93 | * transactionally update its value */ 94 | for (const auto &e : buckets[index]) { 95 | if (e.first == key) { 96 | pmem::obj::transaction::run( 97 | pop, [&] { values[e.second] = val; }); 98 | 99 | return; 100 | } 101 | } 102 | 103 | /* if there is no element with specified key, insert new value 104 | * to the end of values vector and put reference in proper 105 | * bucket transactionally */ 106 | pmem::obj::transaction::run(pop, [&] { 107 | values.emplace_back(val); 108 | buckets[index].emplace_back(key, values.size() - 1); 109 | }); 110 | } 111 | }; 112 | -------------------------------------------------------------------------------- /chapter11/simplekv_rebuild.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * simplekv_rebuild.cpp -- example usage of simple kv which uses vector to hold 35 | * values, string as a key and array to hold buckets. This version stores only 36 | * vectors of keys and values on persistent memory and rebuilds volatile hashmap 37 | * on restart. 38 | * 39 | * This example expects user input from stdin. 40 | */ 41 | 42 | #include 43 | #include 44 | #include 45 | 46 | #include "simplekv_rebuild.hpp" 47 | 48 | using pmem_kv_type = simple_kv_persistent; 49 | 50 | struct root { 51 | pmem::obj::persistent_ptr kv; 52 | }; 53 | 54 | int 55 | main(int argc, char *argv[]) 56 | { 57 | if (argc < 2) { 58 | std::cerr << "usage: " << argv[0] << " file-name" << std::endl; 59 | return 1; 60 | } 61 | 62 | const char *path = argv[1]; 63 | pmem::obj::pool pop; 64 | 65 | try { 66 | pop = pmem::obj::pool::open(path, "simplekv_rebuild"); 67 | auto r = pop.root(); 68 | 69 | if (r->kv == nullptr) { 70 | pmem::obj::transaction::run(pop, [&] { 71 | r->kv = pmem::obj::make_persistent< 72 | pmem_kv_type>(); 73 | }); 74 | } 75 | 76 | auto runtime_kv = simple_kv_runtime(r->kv.get()); 77 | 78 | std::cout << "usage: [get key|put key value|exit]" << std::endl; 79 | 80 | std::string op; 81 | while (std::cin >> op) { 82 | std::string key; 83 | int value; 84 | 85 | if (op == "get" && std::cin >> key) 86 | std::cout << runtime_kv.get(key) << std::endl; 87 | else if (op == "put" && std::cin >> key && 88 | std::cin >> value) 89 | runtime_kv.put(key, value); 90 | else if (op == "exit") 91 | break; 92 | else { 93 | std::cout 94 | << "usage: [get key|put key value|exit]" 95 | << std::endl; 96 | continue; 97 | } 98 | } 99 | 100 | } catch (pmem::pool_error &e) { 101 | std::cerr << e.what() << std::endl; 102 | std::cerr 103 | << "To create pool run: pmempool create obj --layout=simplekv_rebuild -s 100M path_to_pool" 104 | << std::endl; 105 | } catch (std::exception &e) { 106 | std::cerr << e.what() << std::endl; 107 | } 108 | 109 | pop.close(); 110 | 111 | return 0; 112 | } 113 | -------------------------------------------------------------------------------- /chapter11/versioning_insert.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* This is simplified version of insert operation from b+tree: 34 | * https://github.com/pmem/pmemkv/blob/master/src/engines-experimental/stree/persistent_b_tree.h */ 35 | 36 | #include 37 | 38 | #include "libpmemobj++/pool.hpp" 39 | #include "libpmemobj++/persistent_ptr.hpp" 40 | 41 | template 42 | struct entries_t { 43 | Value entries[slots]; 44 | size_t size; 45 | }; 46 | 47 | template 48 | class array { 49 | public: 50 | void insert(pmem::obj::pool_base &pop, const Value &entry); 51 | void insert_element(pmem::obj::pool_base &pop, const Value &entry); 52 | 53 | entries_t v[2]; 54 | uint32_t current; 55 | }; 56 | 57 | template 58 | void array::insert_element(pmem::obj::pool_base &pop, 59 | const Value &entry) { 60 | auto &working_copy = v[1 - current]; 61 | auto &consistent_copy = v[current]; 62 | 63 | auto consistent_insert_position = std::lower_bound( 64 | std::begin(consistent_copy.entries), 65 | std::begin(consistent_copy.entries) + consistent_copy.size, 66 | entry); 67 | auto working_insert_position = std::begin(working_copy.entries) + 68 | std::distance(std::begin(consistent_copy.entries), 69 | consistent_insert_position); 70 | 71 | std::copy(std::begin(consistent_copy.entries), 72 | consistent_insert_position, 73 | std::begin(working_copy.entries)); 74 | 75 | *working_insert_position = entry; 76 | 77 | std::copy(consistent_insert_position, 78 | std::begin(consistent_copy.entries) + consistent_copy.size, 79 | working_insert_position + 1); 80 | 81 | working_copy.size = consistent_copy.size + 1; 82 | } 83 | 84 | template 85 | void array::insert(pmem::obj::pool_base &pop, 86 | const Value &entry){ 87 | insert_element(pop, entry); 88 | pop.persist(&(v[1 - current]), sizeof(entries_t)); 89 | 90 | current = 1 - current; 91 | pop.persist(¤t, sizeof(current)); 92 | } 93 | 94 | int main() 95 | { 96 | pmem::obj::pool> pop; 97 | 98 | try { 99 | pop = pmem::obj::pool>::create("/daxfs/pmpool", "versioning_insert", 100 | PMEMOBJ_MIN_POOL, 0666); 101 | 102 | auto root = pop.root(); 103 | 104 | root->insert(pop, 0); 105 | root->insert(pop, 5); 106 | root->insert(pop, 3); 107 | root->insert(pop, 1); 108 | root->insert(pop, 2); 109 | 110 | for (int i = 0; i < root->v[root->current].size; i++) 111 | std::cout << root->v[root->current].entries[i]; 112 | 113 | } catch (std::exception &e) { 114 | std::cerr << e.what() << std::endl; 115 | return 1; 116 | } 117 | 118 | pop.close(); 119 | 120 | return 0; 121 | } 122 | -------------------------------------------------------------------------------- /chapter12/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, Intel Corporation 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # 15 | # * Neither the name of Intel Corporation nor the names of its 16 | # contributors may be used to endorse or promote products derived 17 | # from this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | CC = gcc 32 | CXX = g++ 33 | RM = rm -f 34 | 35 | TARGETS=leak stackoverflow listing_12-9 listing_12-11 listing_12-13 listing_12-16 listing_12-17 listing_12-17 listing_12-23 listing_12-25 listing_12-28 listing_12-33 listing_12-36 listing_12-38 listing_12-40 listing_12-44 listing_12-45 listing_12-48 listing_12-51 36 | TARGETS_LISTINGS = $(addsuffix .lst, $(TARGETS)) 37 | 38 | all: $(TARGETS) listings 39 | listings: $(TARGETS_LISTINGS) 40 | 41 | %: %.c 42 | $(CC) -g -o $@ $< -lpthread -lpmemobj 43 | 44 | %: %.cpp 45 | $(CXX) -g -o $@ $< -lpthread -lpmemobj -std=c++11 46 | 47 | %.lst: %.c 48 | cat -n $^ > $@ 49 | 50 | %.lst: %.cpp 51 | cat -n $^ > $@ 52 | 53 | clean: 54 | $(RM) $(TARGETS) $(TARGETS_LISTINGS) 55 | -------------------------------------------------------------------------------- /chapter12/README.md: -------------------------------------------------------------------------------- 1 | This are the listings for Chapter 12 - Debugging Persistent Memory Applications. 2 | -------------------------------------------------------------------------------- /chapter12/leak.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * leak.c -- example of a memory leak 30 | */ 31 | 32 | #include 33 | 34 | void func(void) { 35 | int *stack = malloc(100 * sizeof(int)); 36 | } 37 | 38 | int main(void) { 39 | func(); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /chapter12/listing_12-11.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12.11.c -- example of writing to persistent memory without flushing, 30 | * using VALGRIND macros 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | int main(int argc, char *argv[]) { 39 | int fd, *data; 40 | 41 | // open the file and allocate enough space for an 42 | // integer 43 | fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666); 44 | posix_fallocate(fd, 0, sizeof(int)); 45 | 46 | // memory map the file and register the mapped 47 | // memory with VALGRIND 48 | data = (int *) mmap(NULL, sizeof(int), 49 | PROT_READ|PROT_WRITE, 50 | MAP_SHARED_VALIDATE | MAP_SYNC, 51 | fd, 0); 52 | VALGRIND_PMC_REGISTER_PMEM_MAPPING(data, 53 | sizeof(int)); 54 | 55 | // write to pmem 56 | *data = 1234; 57 | 58 | // unmap the memory and un-register it with 59 | // VALGRIND 60 | munmap(data, sizeof(int)); 61 | VALGRIND_PMC_REMOVE_PMEM_MAPPING(data, 62 | sizeof(int)); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /chapter12/listing_12-13.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-13.c -- example of writing to persistent memory with flushing, 30 | * using VALGRIND macros 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | // flusing from user space 41 | void flush(const void *addr, size_t len) { 42 | uintptr_t flush_align = 64, uptr; 43 | for (uptr = (uintptr_t)addr & ~(flush_align - 1); 44 | uptr < (uintptr_t)addr + len; 45 | uptr += flush_align) 46 | _mm_clflush((char *)uptr); 47 | } 48 | 49 | int main(int argc, char *argv[]) { 50 | int fd, *data; 51 | 52 | // open the file and allocate space for one 53 | // integer 54 | fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666); 55 | posix_fallocate(fd, 0, sizeof(int)); 56 | 57 | // map the file and register it with VALGRIND 58 | data = (int *)mmap(NULL, sizeof(int), 59 | PROT_READ | PROT_WRITE, 60 | MAP_SHARED_VALIDATE | MAP_SYNC, fd, 0); 61 | VALGRIND_PMC_REGISTER_PMEM_MAPPING(data, 62 | sizeof(int)); 63 | 64 | // write and flush 65 | *data = 1234; 66 | flush((void *)data, sizeof(int)); 67 | 68 | // unmap and un-register 69 | munmap(data, sizeof(int)); 70 | VALGRIND_PMC_REMOVE_PMEM_MAPPING(data, 71 | sizeof(int)); 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /chapter12/listing_12-16.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-16.c -- example of writing to persistent memory with a write 30 | * dependency; the code does not flush 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | int main(int argc, char *argv[]) { 39 | int fd, *ptr, *data, *flag; 40 | 41 | fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666); 42 | posix_fallocate(fd, 0, sizeof(int)*2); 43 | 44 | ptr = (int *) mmap(NULL, sizeof(int)*2, 45 | PROT_READ | PROT_WRITE, 46 | MAP_SHARED_VALIDATE | MAP_SYNC, 47 | fd, 0); 48 | 49 | data = &(ptr[1]); 50 | flag = &(ptr[0]); 51 | *data = 1234; 52 | *flag = 1; 53 | 54 | munmap(ptr, 2 * sizeof(int)); 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /chapter12/listing_12-17.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-17.c -- example of reading from persistent memory with a write 30 | * dependency 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | int main(int argc, char *argv[]) { 38 | int fd, *ptr, *data, *flag; 39 | 40 | fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666); 41 | posix_fallocate(fd, 0, 2 * sizeof(int)); 42 | 43 | ptr = (int *) mmap(NULL, 2 * sizeof(int), 44 | PROT_READ | PROT_WRITE, 45 | MAP_SHARED_VALIDATE | MAP_SYNC, 46 | fd, 0); 47 | 48 | data = &(ptr[1]); 49 | flag = &(ptr[0]); 50 | if (*flag == 1) 51 | printf("data = %d\n", *data); 52 | 53 | munmap(ptr, 2 * sizeof(int)); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /chapter12/listing_12-23.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-23.c -- example of writing to persistent memory with a write 30 | * dependency. The code flushes both writes 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | void flush(const void *addr, size_t len) { 41 | uintptr_t flush_align = 64, uptr; 42 | for (uptr = (uintptr_t)addr & ~(flush_align - 1); 43 | uptr < (uintptr_t)addr + len; 44 | uptr += flush_align) 45 | _mm_clflush((char *)uptr); 46 | } 47 | 48 | int main(int argc, char *argv[]) { 49 | int fd, *ptr, *data, *flag; 50 | 51 | fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666); 52 | posix_fallocate(fd, 0, sizeof(int) * 2); 53 | 54 | ptr = (int *) mmap(NULL, sizeof(int) * 2, 55 | PROT_READ | PROT_WRITE, 56 | MAP_SHARED_VALIDATE | MAP_SYNC, 57 | fd, 0); 58 | 59 | data = &(ptr[1]); 60 | flag = &(ptr[0]); 61 | *data = 1234; 62 | flush((void *) data, sizeof(int)); 63 | *flag = 1; 64 | flush((void *) flag, sizeof(int)); 65 | 66 | munmap(ptr, 2 * sizeof(int)); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /chapter12/listing_12-25.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-25.c -- example of writing a transaction with memory location not 30 | * added to the transaction 31 | */ 32 | 33 | #include 34 | 35 | struct my_root { 36 | int value; 37 | int is_odd; 38 | }; 39 | 40 | // registering type 'my_root' in the layout 41 | POBJ_LAYOUT_BEGIN(example); 42 | POBJ_LAYOUT_ROOT(example, struct my_root); 43 | POBJ_LAYOUT_END(example); 44 | 45 | int main(int argc, char *argv[]) { 46 | // creating the pool 47 | PMEMobjpool *pop= pmemobj_create("/mnt/pmem/pool", 48 | POBJ_LAYOUT_NAME(example), 49 | (1024 * 1024 * 100), 0666); 50 | 51 | // transation 52 | TX_BEGIN(pop) { 53 | TOID(struct my_root) root 54 | = POBJ_ROOT(pop, struct my_root); 55 | 56 | // adding root.value to the transaction 57 | TX_ADD_FIELD(root, value); 58 | 59 | D_RW(root)->value = 4; 60 | D_RW(root)->is_odd = D_RO(root)->value % 2; 61 | } TX_END 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /chapter12/listing_12-28.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-28.c -- example of writing within a transaction 30 | */ 31 | 32 | #include 33 | 34 | struct my_root { 35 | int value; 36 | int is_odd; 37 | }; 38 | 39 | POBJ_LAYOUT_BEGIN(example); 40 | POBJ_LAYOUT_ROOT(example, struct my_root); 41 | POBJ_LAYOUT_END(example); 42 | 43 | int main(int argc, char *argv[]) { 44 | PMEMobjpool *pop= pmemobj_create("/mnt/pmem/pool", 45 | POBJ_LAYOUT_NAME(example), 46 | (1024 * 1024 * 100), 0666); 47 | 48 | TX_BEGIN(pop) { 49 | TOID(struct my_root) root 50 | = POBJ_ROOT(pop, struct my_root); 51 | 52 | // adding full root to the transaction 53 | TX_ADD(root); 54 | 55 | D_RW(root)->value = 4; 56 | D_RW(root)->is_odd = D_RO(root)->value % 2; 57 | } TX_END 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /chapter12/listing_12-33.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-33.c -- example of two threads adding the same persistent memory 30 | * location to their respective transactions simultaneously 31 | */ 32 | 33 | #include 34 | #include 35 | 36 | struct my_root { 37 | int value; 38 | int is_odd; 39 | }; 40 | 41 | POBJ_LAYOUT_BEGIN(example); 42 | POBJ_LAYOUT_ROOT(example, struct my_root); 43 | POBJ_LAYOUT_END(example); 44 | 45 | pthread_mutex_t lock; 46 | 47 | // function to be run by extra thread 48 | void *func(void *args) { 49 | PMEMobjpool *pop = (PMEMobjpool *) args; 50 | 51 | TX_BEGIN(pop) { 52 | pthread_mutex_lock(&lock); 53 | TOID(struct my_root) root 54 | = POBJ_ROOT(pop, struct my_root); 55 | TX_ADD(root); 56 | D_RW(root)->value = D_RO(root)->value + 3; 57 | pthread_mutex_unlock(&lock); 58 | } TX_END 59 | } 60 | 61 | int main(int argc, char *argv[]) { 62 | PMEMobjpool *pop= pmemobj_create("/mnt/pmem/pool", 63 | POBJ_LAYOUT_NAME(example), 64 | (1024 * 1024 * 10), 0666); 65 | 66 | pthread_t thread; 67 | pthread_mutex_init(&lock, NULL); 68 | 69 | TX_BEGIN(pop) { 70 | pthread_mutex_lock(&lock); 71 | TOID(struct my_root) root 72 | = POBJ_ROOT(pop, struct my_root); 73 | TX_ADD(root); 74 | pthread_create(&thread, NULL, 75 | func, (void *) pop); 76 | D_RW(root)->value = D_RO(root)->value + 4; 77 | D_RW(root)->is_odd = D_RO(root)->value % 2; 78 | pthread_mutex_unlock(&lock); 79 | // wait to make sure other thread finishes 1st 80 | pthread_join(thread, NULL); 81 | } TX_END 82 | 83 | pthread_mutex_destroy(&lock); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /chapter12/listing_12-36.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-36.c -- example of persistent memory overwriting (variable data) 30 | * before flushing 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | void flush(const void *addr, size_t len) { 41 | uintptr_t flush_align = 64, uptr; 42 | for (uptr = (uintptr_t)addr & ~(flush_align - 1); 43 | uptr < (uintptr_t)addr + len; 44 | uptr += flush_align) 45 | _mm_clflush((char *)uptr); 46 | } 47 | 48 | int main(int argc, char *argv[]) { 49 | int fd, *data; 50 | 51 | fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666); 52 | posix_fallocate(fd, 0, sizeof(int)); 53 | 54 | data = (int *)mmap(NULL, sizeof(int), 55 | PROT_READ | PROT_WRITE, 56 | MAP_SHARED_VALIDATE | MAP_SYNC, 57 | fd, 0); 58 | VALGRIND_PMC_REGISTER_PMEM_MAPPING(data, 59 | sizeof(int)); 60 | 61 | // writing twice before flushing 62 | *data = 1234; 63 | *data = 4321; 64 | flush((void *)data, sizeof(int)); 65 | 66 | munmap(data, sizeof(int)); 67 | VALGRIND_PMC_REMOVE_PMEM_MAPPING(data, 68 | sizeof(int)); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /chapter12/listing_12-38.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-38.c -- example of redundant flushing of a persistent memory 30 | * variable 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | void flush(const void *addr, size_t len) { 41 | uintptr_t flush_align = 64, uptr; 42 | for (uptr = (uintptr_t)addr & ~(flush_align - 1); 43 | uptr < (uintptr_t)addr + len; 44 | uptr += flush_align) 45 | _mm_clflush((char *)uptr); 46 | } 47 | 48 | int main(int argc, char *argv[]) { 49 | int fd, *data; 50 | 51 | fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666); 52 | posix_fallocate(fd, 0, sizeof(int)); 53 | 54 | data = (int *)mmap(NULL, sizeof(int), 55 | PROT_READ | PROT_WRITE, 56 | MAP_SHARED_VALIDATE | MAP_SYNC, 57 | fd, 0); 58 | 59 | VALGRIND_PMC_REGISTER_PMEM_MAPPING(data, 60 | sizeof(int)); 61 | 62 | *data = 1234; 63 | flush((void *)data, sizeof(int)); 64 | flush((void *)data, sizeof(int)); // extra flush 65 | 66 | munmap(data, sizeof(int)); 67 | VALGRIND_PMC_REMOVE_PMEM_MAPPING(data, 68 | sizeof(int)); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /chapter12/listing_12-40.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-40.c -- example of writing to persisting memory with a write 30 | * dependency. The code does an extra flush for the flag 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | void flush(const void *addr, size_t len) { 41 | uintptr_t flush_align = 64, uptr; 42 | for (uptr = (uintptr_t)addr & ~(flush_align - 1); 43 | uptr < (uintptr_t)addr + len; 44 | uptr += flush_align) 45 | _mm_clflush((char *)uptr); 46 | } 47 | 48 | int main(int argc, char *argv[]) { 49 | int fd, *ptr, *data, *flag; 50 | 51 | fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666); 52 | posix_fallocate(fd, 0, sizeof(int) * 2); 53 | 54 | ptr = (int *) mmap(NULL, sizeof(int) * 2, 55 | PROT_READ | PROT_WRITE, 56 | MAP_SHARED_VALIDATE | MAP_SYNC, 57 | fd, 0); 58 | data = &(ptr[1]); 59 | flag = &(ptr[0]); 60 | 61 | *data = 1234; 62 | flush((void *) data, sizeof(int)); 63 | *flag = 1; 64 | flush((void *) flag, sizeof(int)); 65 | flush((void *) flag, sizeof(int)); // extra flush 66 | 67 | munmap(ptr, 2 * sizeof(int)); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /chapter12/listing_12-44.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-44.cpp -- example of writing to persistent memory with an 30 | * out-of-order write bug 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | using namespace std; 45 | namespace pobj = pmem::obj; 46 | 47 | struct header_t { 48 | uint32_t counter; 49 | uint8_t reserved[60]; 50 | }; 51 | struct record_t { 52 | char name[63]; 53 | char valid; 54 | }; 55 | struct root { 56 | pobj::persistent_ptr header; 57 | pobj::persistent_ptr records; 58 | }; 59 | 60 | pobj::pool pop; 61 | 62 | int main(int argc, char *argv[]) { 63 | 64 | // everything between BEGIN and END can be 65 | // assigned a particular engine in pmreorder 66 | VALGRIND_PMC_EMIT_LOG("PMREORDER_TAG.BEGIN"); 67 | 68 | pop = pobj::pool::open("/mnt/pmem/file", 69 | "RECORDS"); 70 | auto proot = pop.root(); 71 | 72 | // allocation of memory and initialization to zero 73 | pobj::transaction::run(pop, [&] { 74 | proot->header 75 | = pobj::make_persistent(); 76 | proot->header->counter = 0; 77 | proot->records 78 | = pobj::make_persistent(10); 79 | proot->records[0].valid = 0; 80 | }); 81 | 82 | pobj::persistent_ptr header 83 | = proot->header; 84 | pobj::persistent_ptr records 85 | = proot->records; 86 | 87 | VALGRIND_PMC_EMIT_LOG("PMREORDER_TAG.END"); 88 | 89 | header->counter = 0; 90 | for (uint8_t i = 0; i < 10; i++) { 91 | header->counter++; 92 | if (rand() % 2 == 0) { 93 | snprintf(records[i].name, 63, 94 | "record #%u", i + 1); 95 | pop.persist(records[i].name, 63); // flush 96 | records[i].valid = 2; 97 | } else 98 | records[i].valid = 1; 99 | pop.persist(&(records[i].valid), 1); // flush 100 | } 101 | pop.persist(&(header->counter), 4); // flush 102 | 103 | pop.close(); 104 | return 0; 105 | } 106 | -------------------------------------------------------------------------------- /chapter12/listing_12-45.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-45.cpp -- reading the data structure written by listing_12-44.cpp 30 | * to persistent memory 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | using namespace std; 38 | namespace pobj = pmem::obj; 39 | 40 | struct header_t { 41 | uint32_t counter; 42 | uint8_t reserved[60]; 43 | }; 44 | struct record_t { 45 | char name[63]; 46 | char valid; 47 | }; 48 | struct root { 49 | pobj::persistent_ptr header; 50 | pobj::persistent_ptr records; 51 | }; 52 | 53 | pobj::pool pop; 54 | 55 | int main(int argc, char *argv[]) { 56 | 57 | pop = pobj::pool::open("/mnt/pmem/file", 58 | "RECORDS"); 59 | auto proot = pop.root(); 60 | pobj::persistent_ptr header 61 | = proot->header; 62 | pobj::persistent_ptr records 63 | = proot->records; 64 | 65 | for (uint8_t i = 0; i < header->counter; i++) { 66 | if (records[i].valid == 2) { 67 | printf("found valid record\n"); 68 | printf(" name = %s\n", 69 | records[i].name); 70 | } 71 | } 72 | 73 | pop.close(); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /chapter12/listing_12-48.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-48.cpp -- checking the consistency of the data structure written 30 | * by listing_12-44.cpp 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | using namespace std; 38 | namespace pobj = pmem::obj; 39 | 40 | struct header_t { 41 | uint32_t counter; 42 | uint8_t reserved[60]; 43 | }; 44 | struct record_t { 45 | char name[63]; 46 | char valid; 47 | }; 48 | struct root { 49 | pobj::persistent_ptr header; 50 | pobj::persistent_ptr records; 51 | }; 52 | 53 | pobj::pool pop; 54 | 55 | int main(int argc, char *argv[]) { 56 | 57 | pop = pobj::pool::open("/mnt/pmem/file", 58 | "RECORDS"); 59 | auto proot = pop.root(); 60 | pobj::persistent_ptr header 61 | = proot->header; 62 | pobj::persistent_ptr records 63 | = proot->records; 64 | 65 | for (uint8_t i = 0; i < header->counter; i++) { 66 | if (records[i].valid < 1 or 67 | records[i].valid > 2) 68 | return 1; // data struc. corrupted 69 | } 70 | 71 | pop.close(); 72 | return 0; // everything ok 73 | } 74 | -------------------------------------------------------------------------------- /chapter12/listing_12-51.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-51.cpp -- fixing listing_12-44.cpp by moving the incrementation 30 | * of the counter to the end of the loop 31 | */ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | using namespace std; 45 | namespace pobj = pmem::obj; 46 | 47 | struct header_t { 48 | uint32_t counter; 49 | uint8_t reserved[60]; 50 | }; 51 | struct record_t { 52 | char name[63]; 53 | char valid; 54 | }; 55 | struct root { 56 | pobj::persistent_ptr header; 57 | pobj::persistent_ptr records; 58 | }; 59 | 60 | pobj::pool pop; 61 | 62 | int main(int argc, char *argv[]) { 63 | 64 | VALGRIND_PMC_EMIT_LOG("PMREORDER_TAG.BEGIN"); 65 | 66 | pop = pobj::pool::open("/mnt/pmem/file", 67 | "RECORDS"); 68 | auto proot = pop.root(); 69 | 70 | pobj::transaction::run(pop, [&] { 71 | proot->header 72 | = pobj::make_persistent(); 73 | proot->header->counter = 0; 74 | proot->records 75 | = pobj::make_persistent(10); 76 | proot->records[0].valid = 0; 77 | }); 78 | pobj::persistent_ptr header 79 | = proot->header; 80 | pobj::persistent_ptr records 81 | = proot->records; 82 | 83 | VALGRIND_PMC_EMIT_LOG("PMREORDER_TAG.END"); 84 | 85 | header->counter = 0; 86 | for (uint8_t i = 0; i < 10; i++) { 87 | if (rand() % 2 == 0) { 88 | snprintf(records[i].name, 63, 89 | "record #%u", i + 1); 90 | pop.persist(records[i].name, 63); 91 | records[i].valid = 2; 92 | } else 93 | records[i].valid = 1; 94 | pop.persist(&(records[i].valid), 1); 95 | header->counter++; 96 | } 97 | pop.persist(&(header->counter), 4); 98 | 99 | pop.close(); 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /chapter12/listing_12-9.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * listing_12-9.c -- example of writing to persisting memory without flushing 30 | */ 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | int main(int argc, char *argv[]) { 37 | int fd, *data; 38 | fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666); 39 | posix_fallocate(fd, 0, sizeof(int)); 40 | data = (int *) mmap(NULL, sizeof(int), PROT_READ | 41 | PROT_WRITE, MAP_SHARED_VALIDATE | 42 | MAP_SYNC, fd, 0); 43 | *data = 1234; 44 | munmap(data, sizeof(int)); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /chapter12/stackoverflow.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2020, Intel Corporation 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Intel Corporation nor the names of its contributors 13 | may be used to endorse or promote products derived from this software 14 | without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | /* 29 | * stackoverflow.c -- example of an out-of-bound bug 30 | */ 31 | 32 | #include 33 | 34 | int main() { 35 | int *stack = malloc(100 * sizeof(int)); 36 | stack[100] = 1234; 37 | free(stack); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /chapter13/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 13 - Enabling Persistence in a Real World Application. 2 | -------------------------------------------------------------------------------- /chapter14/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, Intel Corporation 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # 15 | # * Neither the name of Intel Corporation nor the names of its 16 | # contributors may be used to endorse or promote products derived 17 | # from this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | CC = gcc 32 | CXX = g++ 33 | RM = rm -f 34 | 35 | TARGETS=listing_14-1 listing_14-2 listing_14-3 listing_14-4 36 | TARGETS_LISTINGS = $(addsuffix .lst, $(TARGETS)) 37 | 38 | all: $(TARGETS) listings 39 | listings: $(TARGETS_LISTINGS) 40 | 41 | %: %.cpp 42 | $(CXX) -g -o $@ $< -lpthread -lpmemobj -std=c++11 43 | 44 | %.lst: %.cpp 45 | sed -i 's/\t/ /g' $^ 46 | cat -n $^ > $@ 47 | 48 | clean: 49 | $(RM) $(TARGETS) $(TARGETS_LISTINGS) 50 | -------------------------------------------------------------------------------- /chapter14/README.md: -------------------------------------------------------------------------------- 1 | This are the listings for Chapter 14 - Concurrency and Persistent Memory. 2 | -------------------------------------------------------------------------------- /chapter14/listing_14-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * listing_14-1.cpp -- listing_14-1.cpp demonstrates that PMDK transactions 35 | * do not support isolation 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | using namespace std; 47 | namespace pobj = pmem::obj; 48 | 49 | struct root { 50 | pobj::p counter; 51 | }; 52 | 53 | using pop_type = pobj::pool; 54 | 55 | void increment(pop_type &pop) { 56 | auto proot = pop.root(); 57 | pobj::transaction::run(pop, [&] { 58 | proot->counter.get_rw() += 1; 59 | }); 60 | } 61 | 62 | int main(int argc, char *argv[]) { 63 | pop_type pop = 64 | pop_type::open("/daxfs/file", "COUNTER_INC"); 65 | 66 | auto proot = pop.root(); 67 | 68 | cout << "Counter = " << proot->counter << endl; 69 | 70 | std::vector workers; 71 | workers.reserve(10); 72 | for (int i = 0; i < 10; ++i) { 73 | workers.emplace_back(increment, std::ref(pop)); 74 | } 75 | 76 | for (int i = 0; i < 10; ++i) { 77 | workers[i].join(); 78 | } 79 | 80 | cout << "Counter = " << proot->counter << endl; 81 | 82 | pop.close(); 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /chapter14/listing_14-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * listing_14-2.cpp -- listing_14-2.cpp demonstrates that PMDK transactions 35 | * do not support isolation 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include // for std::unique_lock 46 | #include 47 | 48 | using namespace std; 49 | namespace pobj = pmem::obj; 50 | 51 | struct root { 52 | pobj::mutex mtx; 53 | pobj::p counter; 54 | }; 55 | 56 | using pop_type = pobj::pool; 57 | 58 | void increment(pop_type &pop) { 59 | auto proot = pop.root(); 60 | pobj::transaction::run(pop, [&] { 61 | std::unique_lock lock(proot->mtx); 62 | proot->counter.get_rw() += 1; 63 | }); 64 | } 65 | 66 | int main(int argc, char *argv[]) { 67 | pop_type pop = 68 | pop_type::open("/daxfs/file", "COUNTER_INC"); 69 | 70 | auto proot = pop.root(); 71 | 72 | cout << "Counter = " << proot->counter << endl; 73 | 74 | std::vector workers; 75 | workers.reserve(10); 76 | for (int i = 0; i < 10; ++i) { 77 | workers.emplace_back(increment, std::ref(pop)); 78 | } 79 | 80 | for (int i = 0; i < 10; ++i) { 81 | workers[i].join(); 82 | } 83 | 84 | cout << "Counter = " << proot->counter << endl; 85 | 86 | pop.close(); 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /chapter14/listing_14-3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * listing_14-3.cpp -- listing_14-3.cpp demonstrates correct synchroization 35 | * for concurrent PMDK transactions 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | 47 | using namespace std; 48 | namespace pobj = pmem::obj; 49 | 50 | struct root { 51 | pobj::mutex mtx; 52 | pobj::p counter; 53 | }; 54 | 55 | using pop_type = pobj::pool; 56 | 57 | void increment(pop_type &pop) { 58 | auto proot = pop.root(); 59 | pobj::transaction::run(pop, [&] { 60 | proot->counter.get_rw() += 1; 61 | }, proot->mtx); 62 | } 63 | 64 | int main(int argc, char *argv[]) { 65 | pop_type pop = 66 | pop_type::open("/daxfs/file", "COUNTER_INC"); 67 | 68 | auto proot = pop.root(); 69 | 70 | cout << "Counter = " << proot->counter << endl; 71 | 72 | std::vector workers; 73 | workers.reserve(10); 74 | for (int i = 0; i < 10; ++i) { 75 | workers.emplace_back(increment, std::ref(pop)); 76 | } 77 | 78 | for (int i = 0; i < 10; ++i) { 79 | workers[i].join(); 80 | } 81 | 82 | cout << "Counter = " << proot->counter << endl; 83 | 84 | pop.close(); 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /chapter14/listing_14-4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of Intel Corporation nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * listing_14-4.cpp -- listing_14-4.cpp demonstrates how to use 35 | * std::mutex on persistent memory 36 | */ 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | 43 | namespace pobj = pmem::obj; 44 | 45 | struct root { 46 | pobj::experimental::v mtx; 47 | }; 48 | 49 | using pop_type = pobj::pool; 50 | 51 | int main(int argc, char *argv[]) { 52 | pop_type pop = 53 | pop_type::open("/daxfs/file", "MUTEX"); 54 | 55 | auto proot = pop.root(); 56 | 57 | proot->mtx.get().lock(); 58 | 59 | pop.close(); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /chapter15/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 15 - Profiling and Performance. 2 | -------------------------------------------------------------------------------- /chapter16/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 16 - PMDK Internals: Important Algorithms and Data Structures 2 | -------------------------------------------------------------------------------- /chapter17/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 17 - Reliability, Availability, and Serviceability 2 | -------------------------------------------------------------------------------- /chapter18/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2020, Intel Corporation 2 | # 3 | # Redistribution and use in source and binary forms, with or without 4 | # modification, are permitted provided that the following conditions 5 | # are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright 8 | # notice, this list of conditions and the following disclaimer. 9 | # 10 | # * Redistributions in binary form must reproduce the above copyright 11 | # notice, this list of conditions and the following disclaimer in 12 | # the documentation and/or other materials provided with the 13 | # distribution. 14 | # 15 | # * Neither the name of Intel Corporation nor the names of its 16 | # contributors may be used to endorse or promote products derived 17 | # from this software without specific prior written permission. 18 | # 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | 31 | # 32 | # Makefile for chapter19 examples 33 | # 34 | 35 | .SUFFIXES: .lst 36 | 37 | all: hello listings 38 | 39 | listings: hello.lst 40 | 41 | %.lst: %.c 42 | expand -t 4 < $^ | cat -n > $@ 43 | 44 | hello: hello.c 45 | $(CC) -o hello hello.c -lrpmem 46 | 47 | clean: 48 | $(RM) *.o 49 | 50 | clobber: clean 51 | $(RM) hello *.lst 52 | 53 | .PHONY: all clean clobber listings 54 | -------------------------------------------------------------------------------- /chapter18/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 18 - Remote Persistent Memory 2 | -------------------------------------------------------------------------------- /chapter18/hello.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020, Intel Corporation 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions 6 | * are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in 13 | * the documentation and/or other materials provided with the 14 | * distribution. 15 | * 16 | * * Neither the name of the copyright holder nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | /* 34 | * hello.c -- demonstrate API for librpmem 35 | */ 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | #include 45 | 46 | /* 47 | * English and Spanish translation of the message 48 | */ 49 | enum lang_t {en, es}; 50 | static const char *hello_str[] = { 51 | [en] = "Hello world!", 52 | [es] = "¡Hola Mundo!" 53 | }; 54 | 55 | /* 56 | * structure to store the current message 57 | */ 58 | #define STR_SIZE 100 59 | struct hello_t { 60 | enum lang_t lang; 61 | char str[STR_SIZE]; 62 | }; 63 | 64 | /* 65 | * write_hello_str -- write a message to the local memory 66 | */ 67 | static inline void 68 | write_hello_str(struct hello_t *hello, enum lang_t lang) 69 | { 70 | hello->lang = lang; 71 | strncpy(hello->str, hello_str[hello->lang], STR_SIZE); 72 | } 73 | 74 | /* 75 | * remote_open -- setup the librpmem replication 76 | */ 77 | static inline RPMEMpool* 78 | remote_open(const char *target, const char *poolset, void *pool, 79 | size_t pool_size, int *created) 80 | { 81 | /* fill pool_attributes */ 82 | struct rpmem_pool_attr pool_attr; 83 | memset(&pool_attr, 0, sizeof(pool_attr)); 84 | strncpy(pool_attr.signature, "HELLO", RPMEM_POOL_HDR_SIG_LEN); 85 | 86 | /* create a remote pool */ 87 | unsigned nlanes = 1; 88 | RPMEMpool *rpp = rpmem_create(target, poolset, pool, pool_size, &nlanes, 89 | &pool_attr); 90 | if (rpp) { 91 | *created = 1; 92 | return rpp; 93 | } 94 | 95 | /* create failed so open a remote pool */ 96 | assert(errno == EEXIST); 97 | rpp = rpmem_open(target, poolset, pool, pool_size, &nlanes, &pool_attr); 98 | assert(rpp != NULL); 99 | *created = 0; 100 | 101 | return rpp; 102 | } 103 | 104 | int 105 | main(int argc, char *argv[]) 106 | { 107 | /* for this example, assume 32MiB pool */ 108 | size_t pool_size = 32 * 1024 * 1024; 109 | void *pool = NULL; 110 | int created; 111 | 112 | /* allocate a page size aligned local memory pool */ 113 | long pagesize = sysconf(_SC_PAGESIZE); 114 | assert(pagesize >= 0); 115 | int ret = posix_memalign(&pool, pagesize, pool_size); 116 | assert(ret == 0 && pool != NULL); 117 | 118 | /* skip to the beginning of the message */ 119 | size_t hello_off = 4096; /* rpmem header size */ 120 | struct hello_t *hello = (struct hello_t *)(pool + hello_off); 121 | 122 | RPMEMpool *rpp = remote_open("target", "pool.set", pool, pool_size, 123 | &created); 124 | if (created) { 125 | /* reset local memory pool */ 126 | memset(pool, 0, pool_size); 127 | write_hello_str(hello, en); 128 | } else { 129 | /* read message from the remote pool */ 130 | ret = rpmem_read(rpp, hello, hello_off, sizeof(*hello), 0); 131 | assert(ret == 0); 132 | 133 | /* translate the message */ 134 | const int lang_num = (sizeof(hello_str) / sizeof(hello_str[0])); 135 | enum lang_t lang = (enum lang_t)((hello->lang + 1) % lang_num); 136 | write_hello_str(hello, lang); 137 | } 138 | 139 | /* write message to the remote pool */ 140 | ret = rpmem_persist(rpp, hello_off, sizeof(*hello), 0, 0); 141 | printf("%s\n", hello->str); 142 | assert(ret == 0); 143 | 144 | /* close the remote pool */ 145 | ret = rpmem_close(rpp); 146 | assert(ret == 0); 147 | 148 | /* release local memory pool */ 149 | free(pool); 150 | return 0; 151 | } 152 | -------------------------------------------------------------------------------- /chapter18/pool.set: -------------------------------------------------------------------------------- 1 | PMEMPOOLSET 2 | 32M /mountpoint/myfile 3 | -------------------------------------------------------------------------------- /chapter19/README.md: -------------------------------------------------------------------------------- 1 | These are the listings for Chapter 19 - Advanced Topics 2 | -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! 15 | --------------------------------------------------------------------------------