static void test_reopen(const char *path) { PMEMobjpool *pop1 = pmemobj_create(path, LAYOUT, PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR); if (!pop1) UT_FATAL("!create"); PMEMobjpool *pop2 = pmemobj_open(path, LAYOUT); if (pop2) UT_FATAL("pmemobj_open should not succeed"); if (errno != EWOULDBLOCK) UT_FATAL("!pmemobj_open failed but for unexpected reason"); pmemobj_close(pop1); pop2 = pmemobj_open(path, LAYOUT); if (!pop2) UT_FATAL("pmemobj_open should succeed after close"); pmemobj_close(pop2); UNLINK(path); }
int main(int argc, char *argv[]) { START(argc, argv, "obj_heap_state"); if (argc != 2) FATAL("usage: %s file-name", argv[0]); const char *path = argv[1]; PMEMobjpool *pop = NULL; if ((pop = pmemobj_create(path, LAYOUT_NAME, PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR)) == NULL) FATAL("!pmemobj_create: %s", path); pmemobj_root(pop, ROOT_SIZE); /* just to trigger allocation */ pmemobj_close(pop); pop = pmemobj_open(path, LAYOUT_NAME); ASSERTne(pop, NULL); for (int i = 0; i < ALLOCS; ++i) { PMEMoid oid; pmemobj_alloc(pop, &oid, ALLOC_SIZE, 0, NULL, NULL); OUT("%d %lu", i, oid.off); } pmemobj_close(pop); DONE(NULL); }
int main(int argc, char *argv[]) { START(argc, argv, "obj_out_of_memory"); if (argc < 3) FATAL("usage: %s size filename ...", argv[0]); size_t size = atoll(argv[1]); for (int i = 2; i < argc; i++) { const char *path = argv[i]; PMEMobjpool *pop = pmemobj_create(path, LAYOUT_NAME, 0, S_IWUSR | S_IRUSR); if (pop == NULL) FATAL("!pmemobj_create: %s", path); test_alloc(pop, size); pmemobj_close(pop); ASSERTeq(pmemobj_check(path, LAYOUT_NAME), 1); ASSERTne(pop = pmemobj_open(path, LAYOUT_NAME), NULL); test_free(pop); pmemobj_close(pop); } DONE(NULL); }
void test_allocs(PMEMobjpool *pop, const char *path) { PMEMoid oid[TEST_ALLOC_SIZE]; if (pmemobj_alloc(pop, &oid[0], 0, 0, NULL, NULL) == 0) FATAL("pmemobj_alloc(0) succeeded"); for (int i = 1; i < TEST_ALLOC_SIZE; ++i) { if (pmemobj_alloc(pop, &oid[i], i, 0, NULL, NULL) != 0) FATAL("!pmemobj_alloc"); ASSERT(!OID_IS_NULL(oid[i])); } pmemobj_close(pop); ASSERT(pmemobj_check(path, LAYOUT_NAME) == 1); ASSERT((pop = pmemobj_open(path, LAYOUT_NAME)) != NULL); for (int i = 1; i < TEST_ALLOC_SIZE; ++i) { pmemobj_free(&oid[i]); ASSERT(OID_IS_NULL(oid[i])); } pmemobj_close(pop); }
/* * pocli_free -- free main context */ static void pocli_free(struct pocli *pcli) { pmemobj_close(pcli->ctx.pop); free(pcli->inbuf); free(pcli); }
static void pool_create(const char *path, const char *layout, size_t poolsize, unsigned mode) { PMEMobjpool *pop = pmemobj_create(path, layout, poolsize, mode); if (pop == NULL) UT_OUT("!%s: pmemobj_create", path); else { struct stat stbuf; STAT(path, &stbuf); UT_OUT("%s: file size %zu mode 0%o", path, stbuf.st_size, stbuf.st_mode & 0777); pmemobj_close(pop); int result = pmemobj_check(path, layout); if (result < 0) UT_OUT("!%s: pmemobj_check", path); else if (result == 0) UT_OUT("%s: pmemobj_check: not consistent", path); } }
static void test_open_in_different_process(int argc, char **argv, int sleep) { PMEMobjpool *pop; if (sleep > 0) return; char *path = argv[1]; /* before starting the 2nd process, create a pool */ pop = pmemobj_create(path, LAYOUT, PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR); if (!pop) UT_FATAL("!create"); /* * "X" is pass as an additional param to the new process * created by ut_spawnv to distinguish second process on Windows */ uintptr_t result = ut_spawnv(argc, argv, "X", NULL); if (result == -1) UT_FATAL("Create new process failed error: %d", GetLastError()); pmemobj_close(pop); }
int main(int argc, char *argv[]) { if (argc != 2) { printf("usage: %s file-name\n", argv[0]); return 1; } PMEMobjpool *pop = pmemobj_create(argv[1], LAYOUT_NAME, PMEMOBJ_MIN_POOL, 0666); if (pop == NULL) { perror("pmemobj_create"); return 1; } PMEMoid root = pmemobj_root(pop, sizeof(struct my_root)); struct my_root *rootp = pmemobj_direct(root); char buf[MAX_BUF_LEN]; if (scanf("%9s", buf) == EOF) { fprintf(stderr, "EOF\n"); return 1; } rootp->len = strlen(buf); pmemobj_persist(pop, &rootp->len, sizeof(rootp->len)); pmemobj_memcpy_persist(pop, rootp->buf, buf, rootp->len); pmemobj_close(pop); return 0; }
int main(int argc, char *argv[]) { START(argc, argv, "obj_tx_mt"); if (argc != 2) UT_FATAL("usage: %s [file]", argv[0]); if ((pop = pmemobj_create(argv[1], "mt", PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR)) == NULL) UT_FATAL("!pmemobj_create"); int i = 0; long ncpus = sysconf(_SC_NPROCESSORS_ONLN); pthread_t *threads = MALLOC(2 * ncpus * sizeof(threads[0])); for (int j = 0; j < ncpus; ++j) { PTHREAD_CREATE(&threads[i++], NULL, tx_alloc_free, NULL); PTHREAD_CREATE(&threads[i++], NULL, tx_snap, NULL); } while (i > 0) PTHREAD_JOIN(threads[--i], NULL); pmemobj_close(pop); FREE(threads); DONE(NULL); }
int main(int argc, char *argv[]) { START(argc, argv, "obj_pmalloc_oom_mt"); if (argc != 2) FATAL("usage: %s file-name", argv[0]); const char *path = argv[1]; if ((pop = pmemobj_create(path, LAYOUT_NAME, PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR)) == NULL) FATAL("!pmemobj_create: %s", path); pthread_t t; pthread_create(&t, NULL, oom_worker, NULL); pthread_join(t, NULL); int first_thread_allocated = allocated; pthread_create(&t, NULL, oom_worker, NULL); pthread_join(t, NULL); ASSERTeq(first_thread_allocated, allocated); pmemobj_close(pop); DONE(NULL); }
int main(int argc, char *argv[]) { if (argc < 3 || argc > 4) { printf("usage: %s hashmap_tx|hashmap_atomic|ctree|btree|rbtree" " file-name [<seed>]\n", argv[0]); return 1; } const struct map_ops *ops = NULL; const char *path = argv[2]; const char *type = argv[1]; if (strcmp(type, "hashmap_tx") == 0) { ops = MAP_HASHMAP_TX; } else if (strcmp(type, "hashmap_atomic") == 0) { ops = MAP_HASHMAP_ATOMIC; } else if (strcmp(type, "ctree") == 0) { ops = MAP_CTREE; } else if (strcmp(type, "btree") == 0) { ops = MAP_BTREE; } else if (strcmp(type, "rbtree") == 0) { ops = MAP_RBTREE; } else { fprintf(stderr, "invalid hasmap type -- '%s'\n", type); return 1; } if (access(path, F_OK) != 0) { pop = pmemobj_create(path, POBJ_LAYOUT_NAME(map), PM_HASHSET_POOL_SIZE, S_IRUSR | S_IWUSR); if (pop == NULL) { fprintf(stderr, "failed to create pool: %s\n", pmemobj_errormsg()); return 1; } struct hashmap_args args; if (argc > 3) args.seed = atoi(argv[3]); else args.seed = time(NULL); srand(args.seed); mapc = map_ctx_init(ops, pop); if (!mapc) { pmemobj_close(pop); perror("map_ctx_init"); return 1; } root = POBJ_ROOT(pop, struct root); printf("seed: %u\n", args.seed); map_new(mapc, &D_RW(root)->map, &args); map = D_RO(root)->map; } else {
/* * Closes the pool. * * Throws std::logic_error if the pool has already been closed. */ void close() { if (this->pop == nullptr) throw std::logic_error("Pool already closed"); pmemobj_close(this->pop); this->pop = nullptr; }
int art_tree_map_init(struct datastore *ds, struct ds_context *ctx) { int errors = 0; char *error_string; /* calculate a required pool size */ if (ctx->psize < PMEMOBJ_MIN_POOL) ctx->psize = PMEMOBJ_MIN_POOL; if (!ctx->fileio) { if (access(ctx->filename, F_OK) != 0) { error_string = "pmemobj_create"; ctx->pop = pmemobj_create(ctx->filename, POBJ_LAYOUT_NAME(arttree_tx), ctx->psize, ctx->fmode); ctx->newpool = 1; } else { error_string = "pmemobj_open"; ctx->pop = pmemobj_open(ctx->filename, POBJ_LAYOUT_NAME(arttree_tx)); } if (ctx->pop == NULL) { perror(error_string); errors++; } } else { int flags = O_CREAT | O_RDWR | O_SYNC; /* Create a file if it does not exist. */ if ((ctx->fd = open(ctx->filename, flags, ctx->fmode)) < 0) { perror(ctx->filename); errors++; } /* allocate the pmem */ if ((errno = posix_fallocate(ctx->fd, 0, ctx->psize)) != 0) { perror("posix_fallocate"); errors++; } } if (!errors) { pmemobj_ds_set_priv(ds, ctx); } else { if (ctx->fileio) { if (ctx->fd >= 0) { close(ctx->fd); } } else { if (ctx->pop) { pmemobj_close(ctx->pop); } } } return errors; }
int main(int argc, char *argv[]) { START(argc, argv, "obj_out_of_memory"); if (argc < 3) FATAL("usage: %s size filename ...", argv[0]); size_t size = atoll(argv[1]); for (int i = 2; i < argc; i++) { const char *path = argv[i]; PMEMobjpool *pop = pmemobj_create(path, LAYOUT_NAME, 0, S_IWUSR | S_IRUSR); if (pop == NULL) FATAL("!pmemobj_create: %s", path); test_alloc(pop, size); pmemobj_close(pop); ASSERTeq(pmemobj_check(path, LAYOUT_NAME), 1); /* * To prevent subsequent opens from receiving exactly the same * volatile memory addresses a dummy malloc has to be made. * This can expose issues in which traces of previous volatile * state are leftover in the persistent pool. */ void *heap_touch = MALLOC(1); ASSERTne(pop = pmemobj_open(path, LAYOUT_NAME), NULL); test_free(pop); pmemobj_close(pop); FREE(heap_touch); } DONE(NULL); }
int main(int argc, char *argv[]) { START(argc, argv, "obj_direct"); if (argc != 3) FATAL("usage: %s [directory] [# of pools]", argv[0]); int npools = atoi(argv[2]); const char *dir = argv[1]; int r; PMEMobjpool *pops[npools]; char path[MAX_PATH_LEN]; for (int i = 0; i < npools; ++i) { snprintf(path, MAX_PATH_LEN, "%s/testfile%d", dir, i); pops[i] = pmemobj_create(path, LAYOUT_NAME, PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR); if (pops[i] == NULL) FATAL("!pmemobj_create"); } PMEMoid oids[npools]; PMEMoid tmpoids[npools]; oids[0] = OID_NULL; ASSERTeq(pmemobj_direct(oids[0]), NULL); for (int i = 0; i < npools; ++i) { oids[i] = (PMEMoid) {pops[i]->uuid_lo, 0}; ASSERTeq(pmemobj_direct(oids[i]), NULL); uint64_t off = pops[i]->heap_offset; oids[i] = (PMEMoid) {pops[i]->uuid_lo, off}; ASSERTeq(pmemobj_direct(oids[i]) - off, pops[i]); r = pmemobj_alloc(pops[i], &tmpoids[i], 100, 1, NULL, NULL); ASSERTeq(r, 0); } for (int i = 0; i < npools; ++i) { ASSERTne(pmemobj_direct(tmpoids[i]), NULL); pmemobj_free(&tmpoids[i]); ASSERTeq(pmemobj_direct(tmpoids[i]), NULL); pmemobj_close(pops[i]); ASSERTeq(pmemobj_direct(oids[i]), NULL); } DONE(NULL); }
/* * pobj_exit -- common part for the benchmarks exit functions */ static int pobj_exit(struct benchmark *bench, struct benchmark_args *args) { size_t i; auto *bench_priv = (struct pobj_bench *)pmembench_get_priv(bench); if (bench_priv->n_pools > 1) { for (i = 0; i < bench_priv->n_pools; i++) { pmemobj_close(bench_priv->pop[i]); free((char *)bench_priv->sets[i]); } } else { pmemobj_close(bench_priv->pop[0]); } free(bench_priv->sets); free(bench_priv->pop); free(bench_priv->rand_sizes); free(bench_priv->random_types); free(bench_priv); return 0; }
static void pool_open(const char *path, const char *layout) { PMEMobjpool *pop = pmemobj_open(path, layout); if (pop == NULL) UT_OUT("!%s: pmemobj_open", path); else { UT_OUT("%s: pmemobj_open: Success", path); pmemobj_close(pop); } }
/* * pobj_open_op -- main operations of the obj_open benchmark. */ static int pobj_open_op(struct benchmark *bench, struct operation_info *info) { auto *bench_priv = (struct pobj_bench *)pmembench_get_priv(bench); size_t idx = bench_priv->pool(info->worker->index); pmemobj_close(bench_priv->pop[idx]); bench_priv->pop[idx] = pmemobj_open(bench_priv->sets[idx], LAYOUT_NAME); if (bench_priv->pop[idx] == nullptr) return -1; return 0; }
int main(int argc, char *argv[]) { START(argc, argv, "obj_fragmentation2"); if (argc < 3) UT_FATAL("usage: %s filename workload [seed]", argv[0]); const char *path = argv[1]; PMEMobjpool *pop = pmemobj_create(path, LAYOUT_NAME, DEFAULT_FILE_SIZE, S_IWUSR | S_IRUSR); if (pop == NULL) UT_FATAL("!pmemobj_create: %s", path); int w = atoi(argv[2]); if (argc > 3) seed = (unsigned)atoi(argv[3]); else seed = time(NULL); objects = ZALLOC(sizeof(uint64_t) * MAX_OBJECTS); UT_ASSERTne(objects, NULL); workloads[w - 1](pop); PMEMoid oid; size_t remaining = 0; size_t chunk = 100; /* calc at chunk level */ while (pmemobj_alloc(pop, &oid, chunk, 0, NULL, NULL) == 0) remaining += pmemobj_alloc_usable_size(oid) + 16; size_t allocated_sum = 0; oid = pmemobj_root(pop, 1); for (size_t n = 0; n < nobjects; ++n) { if (objects[n] == 0) continue; oid.off = objects[n]; allocated_sum += pmemobj_alloc_usable_size(oid) + 16; } size_t used = DEFAULT_FILE_SIZE - remaining; float frag = ((float)used / allocated_sum) - 1.f; UT_ASSERT(frag <= workloads_target[w - 1]); pmemobj_close(pop); FREE(objects); DONE(NULL); }
/* * pocli_free -- free main context */ static void pocli_free(struct pocli *pcli) { while (pmemobj_tx_stage() != TX_STAGE_NONE) { while (pmemobj_tx_stage() != TX_STAGE_NONE) pmemobj_tx_process(); pmemobj_tx_end(); } pmemobj_close(pcli->ctx.pop); free(pcli->inbuf); free(pcli); }
void exit_handler(struct ds_context *ctx) { if (!ctx->fileio) { if (ctx->pop) { pmemobj_close(ctx->pop); } } else { if (ctx->fd > (-1)) { close(ctx->fd); } } }
int main(int argc, char *argv[]) { START(argc, argv, "libpmempool_rm"); if (argc < 2) FATAL_USAGE(argv[0]); unsigned flags = 0; char *optstr = "flro"; int do_open = 0; int opt; while ((opt = getopt(argc, argv, optstr)) != -1) { switch (opt) { case 'f': flags |= PMEMPOOL_RM_FORCE; break; case 'r': flags |= PMEMPOOL_RM_POOLSET_REMOTE; break; case 'l': flags |= PMEMPOOL_RM_POOLSET_LOCAL; break; case 'o': do_open = 1; break; default: FATAL_USAGE(argv[0]); } } for (int i = optind; i < argc; i++) { const char *path = argv[i]; if (do_open) { Pop = pmemobj_open(path, NULL); UT_ASSERTne(Pop, NULL); } int ret = pmempool_rm(path, flags); if (ret) { UT_OUT("!%s: %s", path, pmempool_errormsg()); } if (do_open) { UT_ASSERTne(Pop, NULL); pmemobj_close(Pop); } } DONE(NULL); }
int main(int argc, char *argv[]) { START(argc, argv, "obj_check"); if (argc < 2 || argc > 5) UT_FATAL("usage: obj_check <file> [-l <layout>] [-o]"); const char *path = argv[1]; const char *layout = NULL; PMEMobjpool *pop = NULL; int open = 0; for (int i = 2; i < argc; ++i) { if (strcmp(argv[i], "-o") == 0) open = 1; else if (strcmp(argv[i], "-l") == 0) { layout = argv[i + 1]; i++; } else UT_FATAL("Unrecognized argument: %s", argv[i]); } if (open) { pop = pmemobj_open(path, layout); if (pop == NULL) UT_OUT("!%s: pmemobj_open", path); else UT_OUT("%s: pmemobj_open: Success", path); } int ret = pmemobj_check(path, layout); switch (ret) { case 1: UT_OUT("consistent"); break; case 0: UT_OUT("not consistent: %s", pmemobj_errormsg()); break; default: UT_OUT("error: %s", pmemobj_errormsg()); break; } if (pop != NULL) pmemobj_close(pop); DONE(NULL); }
/* * test_open -- in the open test we should be able to allocate exactly * one object. */ static void test_open(const char *path) { PMEMobjpool *pop; if ((pop = pmemobj_open(path, LAYOUT_NAME)) == NULL) UT_FATAL("!pmemobj_open: %s", path); int ret = pmemobj_alloc(pop, NULL, ALLOC_SIZE, 0, NULL, NULL); UT_ASSERTeq(ret, 0); ret = pmemobj_alloc(pop, NULL, ALLOC_SIZE, 0, NULL, NULL); UT_ASSERTne(ret, 0); pmemobj_close(pop); }
static void test_open_in_different_process(int argc, char **argv, int sleep) { pid_t pid = fork(); PMEMobjpool *pop; char *path = argv[1]; if (pid < 0) UT_FATAL("fork failed"); if (pid == 0) { /* child */ if (sleep) usleep(sleep); while (os_access(path, R_OK)) usleep(100 * 1000); pop = pmemobj_open(path, LAYOUT); if (pop) UT_FATAL("pmemobj_open after fork should not succeed"); if (errno != EWOULDBLOCK) UT_FATAL("!pmemobj_open after fork failed but for " "unexpected reason"); exit(0); } pop = pmemobj_create(path, LAYOUT, PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR); if (!pop) UT_FATAL("!create"); int status; if (waitpid(pid, &status, 0) < 0) UT_FATAL("!waitpid failed"); if (!WIFEXITED(status)) UT_FATAL("child process failed"); pmemobj_close(pop); UNLINK(path); }
/* * pocli_alloc -- allocate main context */ static struct pocli * pocli_alloc(FILE *input, const char *fname, const struct pocli_cmd *cmds, size_t ncmds, size_t inbuf_len) { assert(inbuf_len < INT_MAX); struct pocli_opts opts; if (pocli_read_opts(&opts)) return NULL; struct pocli *pcli = calloc(1, sizeof (*pcli)); if (!pcli) return NULL; memcpy(&pcli->opts, &opts, sizeof (pcli->opts)); pcli->in = input; pcli->istty = isatty(fileno(pcli->in)); pcli->cmds = cmds; pcli->ncmds = ncmds; pcli->ctx.pocli = pcli; pcli->ctx.err = stderr; pcli->ctx.out = stdout; pcli->ctx.pop = pmemobj_open(fname, NULL); if (!pcli->ctx.pop) { fprintf(stderr, "%s: %s\n", fname, pmemobj_errormsg()); goto err_free_pcli; } size_t root_size = pmemobj_root_size(pcli->ctx.pop); if (root_size) pcli->ctx.root = pmemobj_root(pcli->ctx.pop, root_size); pcli->inbuf_len = inbuf_len; pcli->inbuf = malloc(inbuf_len); if (!pcli->inbuf) goto err_close_pool; return pcli; err_close_pool: pmemobj_close(pcli->ctx.pop); err_free_pcli: free(pcli); return NULL; }
static void test_lazy_load(PMEMobjpool *pop, const char *path) { PMEMoid oid[3]; int ret = pmemobj_alloc(pop, &oid[0], LAZY_LOAD_SIZE, 0, NULL, NULL); UT_ASSERTeq(ret, 0); ret = pmemobj_alloc(pop, &oid[1], LAZY_LOAD_SIZE, 0, NULL, NULL); UT_ASSERTeq(ret, 0); ret = pmemobj_alloc(pop, &oid[2], LAZY_LOAD_SIZE, 0, NULL, NULL); UT_ASSERTeq(ret, 0); pmemobj_close(pop); UT_ASSERT((pop = pmemobj_open(path, LAYOUT_NAME)) != NULL); pmemobj_free(&oid[1]); ret = pmemobj_alloc(pop, &oid[1], LAZY_LOAD_BIG_SIZE, 0, NULL, NULL); UT_ASSERTeq(ret, 0); }
int main(int argc, char *argv[]) { const char path[] = "/pmem-fs/myfile"; PMEMobjpool *pop; /* create the pmemobj pool or open it if it already exists */ pop = pmemobj_create(path, LAYOUT_NAME, POOL_SIZE, 0666); if (pop == NULL) pop = pmemobj_open(path, LAYOUT_NAME); if (pop == NULL) { perror(path); exit(1); } /* ... */ pmemobj_close(pop); }
/* * test_create -- allocate all possible objects and log the number. It should * exceed what would be possible on a single zone. * Additionally, free one object so that we can later check that it can be * allocated after the next open. */ static void test_create(const char *path) { PMEMobjpool *pop = NULL; if ((pop = pmemobj_create(path, LAYOUT_NAME, 0, S_IWUSR | S_IRUSR)) == NULL) UT_FATAL("!pmemobj_create: %s", path); PMEMoid oid; int n = 0; while (1) { if (pmemobj_alloc(pop, &oid, ALLOC_SIZE, 0, NULL, NULL) != 0) break; n++; } UT_OUT("allocated: %d", n); pmemobj_free(&oid); pmemobj_close(pop); }
int main(int argc, char *argv[]) { START(argc, argv, "heap_interrupt"); UT_COMPILE_ERROR_ON(POBJ_LAYOUT_TYPES_NUM(heap_interrupt) != 0); if (argc != 4) UT_FATAL("usage: %s file [cmd: c/o] [scenario]", argv[0]); const char *path = argv[1]; PMEMobjpool *pop = NULL; int exists = argv[2][0] == 'o'; int scenario = atoi(argv[3]); if (!exists) { if ((pop = pmemobj_create(path, POBJ_LAYOUT_NAME(heap_interrupt), 0, S_IWUSR | S_IRUSR)) == NULL) { UT_FATAL("failed to create pool\n"); } scenarios[scenario].create(pop); /* if we get here, something is wrong with function mocking */ UT_ASSERT(0); } else { if ((pop = pmemobj_open(path, POBJ_LAYOUT_NAME(heap_interrupt))) == NULL) { UT_FATAL("failed to open pool\n"); } scenarios[scenario].verify(pop); } pmemobj_close(pop); DONE(NULL); }