Last active
August 29, 2015 14:09
-
-
Save andyrudoff/b3e569c479c3b7120875 to your computer and use it in GitHub Desktop.
libpmemblk examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a gist containing libpmemblk examples, so they can be embedded in web pages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014, Intel Corporation | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* | |
* * Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* | |
* * Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in | |
* the documentation and/or other materials provided with the | |
* distribution. | |
* | |
* * Neither the name of Intel Corporation nor the names of its | |
* contributors may be used to endorse or promote products derived | |
* from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
#define ASSET_NAME_MAX 256 | |
#define ASSET_USER_NAME_MAX 64 | |
#define ASSET_CHECKED_OUT 2 | |
#define ASSET_FREE 1 | |
struct asset { | |
char name[ASSET_NAME_MAX]; | |
char user[ASSET_USER_NAME_MAX]; | |
time_t time; | |
int state; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014, Intel Corporation | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* | |
* * Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* | |
* * Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in | |
* the documentation and/or other materials provided with the | |
* distribution. | |
* | |
* * Neither the name of Intel Corporation nor the names of its | |
* contributors may be used to endorse or promote products derived | |
* from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/* | |
* asset_checkin -- mark an asset as no longer checked out | |
* | |
* Usage: | |
* asset_checkin /path/to/pm-aware/file asset-ID | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <time.h> | |
#include <libpmemblk.h> | |
#include "asset.h" | |
int | |
main(int argc, char *argv[]) | |
{ | |
PMEMblkpool *pbp; | |
struct asset asset; | |
int assetid; | |
if (argc < 3) { | |
fprintf(stderr, "usage: %s assetdb asset-ID\n", argv[0]); | |
exit(1); | |
} | |
const char *path = argv[1]; | |
assetid = atoi(argv[2]); | |
/* open an array of atomically writable elements */ | |
if ((pbp = pmemblk_open(path, sizeof (struct asset))) == NULL) { | |
perror(path); | |
exit(1); | |
} | |
/* read a required element in */ | |
if (pmemblk_read(pbp, &asset, (off_t)assetid) < 0) { | |
perror("pmemblk_read"); | |
exit(1); | |
} | |
/* check if it contains any data */ | |
if ((asset.state != ASSET_FREE) && | |
(asset.state != ASSET_CHECKED_OUT)) { | |
fprintf(stderr, "Asset ID %d not found\n", assetid); | |
exit(1); | |
} | |
/* change state to free, clear user name and timestamp */ | |
asset.state = ASSET_FREE; | |
asset.user[0] = '\0'; | |
asset.time = 0; | |
if (pmemblk_write(pbp, &asset, (off_t)assetid) < 0) { | |
perror("pmemblk_write"); | |
exit(1); | |
} | |
pmemblk_close(pbp); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014, Intel Corporation | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* | |
* * Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* | |
* * Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in | |
* the documentation and/or other materials provided with the | |
* distribution. | |
* | |
* * Neither the name of Intel Corporation nor the names of its | |
* contributors may be used to endorse or promote products derived | |
* from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/* | |
* asset_checkout -- mark an asset as checked out to someone | |
* | |
* Usage: | |
* asset_checkin /path/to/pm-aware/file asset-ID name | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <time.h> | |
#include <libpmemblk.h> | |
#include "asset.h" | |
int | |
main(int argc, char *argv[]) | |
{ | |
PMEMblkpool *pbp; | |
struct asset asset; | |
int assetid; | |
if (argc < 4) { | |
fprintf(stderr, "usage: %s assetdb asset-ID name\n", argv[0]); | |
exit(1); | |
} | |
const char *path = argv[1]; | |
assetid = atoi(argv[2]); | |
/* open an array of atomically writable elements */ | |
if ((pbp = pmemblk_open(path, sizeof (struct asset))) == NULL) { | |
perror("pmemblk_open"); | |
exit(1); | |
} | |
/* read a required element in */ | |
if (pmemblk_read(pbp, &asset, (off_t)assetid) < 0) { | |
perror("pmemblk_read"); | |
exit(1); | |
} | |
/* check if it contains any data */ | |
if ((asset.state != ASSET_FREE) && | |
(asset.state != ASSET_CHECKED_OUT)) { | |
fprintf(stderr, "Asset ID %d not found", assetid); | |
exit(1); | |
} | |
if (asset.state == ASSET_CHECKED_OUT) { | |
fprintf(stderr, "Asset ID %d already checked out\n", assetid); | |
exit(1); | |
} | |
/* update user name, set checked out state, and take timestamp */ | |
strncpy(asset.user, argv[3], ASSET_USER_NAME_MAX - 1); | |
asset.user[ASSET_USER_NAME_MAX - 1] = '\0'; | |
asset.state = ASSET_CHECKED_OUT; | |
time(&asset.time); | |
/* put it back in the block */ | |
if (pmemblk_write(pbp, &asset, assetid) < 0) { | |
perror("pmemblk_write"); | |
exit(1); | |
} | |
pmemblk_close(pbp); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014, Intel Corporation | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* | |
* * Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* | |
* * Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in | |
* the documentation and/or other materials provided with the | |
* distribution. | |
* | |
* * Neither the name of Intel Corporation nor the names of its | |
* contributors may be used to endorse or promote products derived | |
* from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/* | |
* asset_list -- list all assets in an assetdb file | |
* | |
* Usage: | |
* asset_list /path/to/pm-aware/file | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <time.h> | |
#include <libpmemblk.h> | |
#include "asset.h" | |
int | |
main(int argc, char *argv[]) | |
{ | |
PMEMblkpool *pbp; | |
int assetid; | |
size_t nelements; | |
struct asset asset; | |
if (argc < 2) { | |
fprintf(stderr, "usage: %s assetdb\n", argv[0]); | |
exit(1); | |
} | |
const char *path = argv[1]; | |
/* open an array of atomically writable elements */ | |
if ((pbp = pmemblk_open(path, sizeof (struct asset))) == NULL) { | |
perror(path); | |
exit(1); | |
} | |
/* how many elements do we have? */ | |
nelements = pmemblk_nblock(pbp); | |
/* print out all the elements that contain assets data */ | |
for (assetid = 0; assetid < nelements; ++assetid) { | |
if (pmemblk_read(pbp, &asset, (off_t)assetid) < 0) { | |
perror("pmemblk_read"); | |
exit(1); | |
} | |
if ((asset.state != ASSET_FREE) && | |
(asset.state != ASSET_CHECKED_OUT)) { | |
break; | |
} | |
printf("Asset ID: %d\n", assetid); | |
if (asset.state == ASSET_FREE) | |
printf(" State: Free\n"); | |
else { | |
printf(" State: Checked out\n"); | |
printf(" User: %s\n", asset.user); | |
printf(" Time: %s", ctime(&asset.time)); | |
} | |
printf(" Name: %s\n", asset.name); | |
} | |
pmemblk_close(pbp); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014, Intel Corporation | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* | |
* * Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* | |
* * Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in | |
* the documentation and/or other materials provided with the | |
* distribution. | |
* | |
* * Neither the name of Intel Corporation nor the names of its | |
* contributors may be used to endorse or promote products derived | |
* from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/* | |
* asset_load -- given pre-allocated assetdb file, load it up with assets | |
* | |
* Usage: | |
* fallocate -l 1G /path/to/pm-aware/file | |
* asset_load /path/to/pm-aware/file asset-file | |
* | |
* The asset-file should contain the names of the assets, one per line. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <string.h> | |
#include <time.h> | |
#include <libpmemblk.h> | |
#include "asset.h" | |
int | |
main(int argc, char *argv[]) | |
{ | |
FILE *fp; | |
size_t len; | |
PMEMblkpool *pbp; | |
int assetid = 0; | |
size_t nelements; | |
char *line = NULL; | |
if (argc < 3) { | |
fprintf(stderr, "usage: %s assetdb assetlist\n", argv[0]); | |
exit(1); | |
} | |
const char *path_pool = argv[1]; | |
const char *path_list = argv[2]; | |
/* create pmemblk pool in existing (but as yet unmodified) file */ | |
if ((pbp = pmemblk_create(path_pool, | |
sizeof (struct asset), 0, 0)) == NULL) { | |
perror(path_pool); | |
exit(1); | |
} | |
nelements = pmemblk_nblock(pbp); | |
if ((fp = fopen(path_list, "r")) == NULL) { | |
perror(path_list); | |
exit(1); | |
} | |
/* | |
* Read in all the assets from the assetfile and put them in the | |
* array, if a name of the asset is longer than ASSET_NAME_SIZE_MAX, | |
* truncate it. | |
*/ | |
while (getline(&line, &len, fp) != -1) { | |
struct asset asset; | |
if (assetid >= nelements) { | |
fprintf(stderr, "%s: too many assets to fit in %s " | |
"(only %d assets loaded)\n", | |
path_list, path_pool, assetid); | |
exit(1); | |
} | |
memset(&asset, '\0', sizeof (asset)); | |
asset.state = ASSET_FREE; | |
strncpy(asset.name, line, ASSET_NAME_MAX - 1); | |
asset.name[ASSET_NAME_MAX - 1] = '\0'; | |
if (pmemblk_write(pbp, &asset, (off_t)assetid) < 0) { | |
perror("pmemblk_write"); | |
exit(1); | |
} | |
assetid++; | |
} | |
free(line); | |
fclose(fp); | |
pmemblk_close(pbp); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014, Intel Corporation | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* | |
* * Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* | |
* * Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in | |
* the documentation and/or other materials provided with the | |
* distribution. | |
* | |
* * Neither the name of Intel Corporation nor the names of its | |
* contributors may be used to endorse or promote products derived | |
* from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY LOG OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
/* | |
* manpage.c -- simple example for the libpmemblk man page | |
*/ | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <libpmemblk.h> | |
/* size of the pmemblk pool -- 1 GB */ | |
#define POOL_SIZE ((off_t)(1 << 30)) | |
/* size of each element in the pmem pool */ | |
#define ELEMENT_SIZE 1024 | |
int | |
main(int argc, char *argv[]) | |
{ | |
const char path[] = "/pmem-fs/myfile"; | |
PMEMblkpool *pbp; | |
size_t nelements; | |
char buf[ELEMENT_SIZE]; | |
/* create the pmemblk pool or open it if it already exists */ | |
pbp = pmemblk_create(path, ELEMENT_SIZE, POOL_SIZE, 0666); | |
if (pbp == NULL) | |
pbp = pmemblk_open(path, ELEMENT_SIZE); | |
if (pbp == NULL) { | |
perror(path); | |
exit(1); | |
} | |
/* how many elements fit into the file? */ | |
nelements = pmemblk_nblock(pbp); | |
printf("file holds %zu elements\n", nelements); | |
/* store a block at index 5 */ | |
strcpy(buf, "hello, world"); | |
if (pmemblk_write(pbp, buf, 5) < 0) { | |
perror("pmemblk_write"); | |
exit(1); | |
} | |
/* read the block at index 10 (reads as zeros initially) */ | |
if (pmemblk_read(pbp, buf, 10) < 0) { | |
perror("pmemblk_read"); | |
exit(1); | |
} | |
/* zero out the block at index 5 */ | |
if (pmemblk_set_zero(pbp, 5) < 0) { | |
perror("pmemblk_set_zero"); | |
exit(1); | |
} | |
/* ... */ | |
pmemblk_close(pbp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment