Commit 08360e01 authored by Rich Prohaska's avatar Rich Prohaska Committed by Yoni Fogel

freebsd portability lib. addresses #1789

git-svn-id: file:///svn/toku/tokudb@12442 c7de825b-a66e-492c-adef-691d508d4ae1
parent 47b4f176
# -*- Mode: Makefile -*-
.DEFAULT_GOAL=install
TOKUROOT=../
INCLUDEDIRS=-I.
SKIP_LIBPORTABILITYRULE=1
include $(TOKUROOT)toku_include/Makefile.include
OPT_AROPT=-qnoipo #Disable ipo for lib creation even when optimization is on.
SRCS = $(wildcard *.c)
OBJS = $(patsubst %.c,%.$(OEXT),$(SRCS))
TARGET = libtokuportability.$(AEXT)
build install: $(LIBPORTABILITY)
$(LIBPORTABILITY): $(TARGET)
if ! diff $< $@ 2>/dev/null; then cp $< $@; fi
$(TARGET): $(OBJS)
$(OBJS): CFLAGS += -DTOKU_ALLOW_DEPRECATED
#Blank on purpose
check: $(TARGET)
cd tests && $(MAKE) check
clean:
rm -rf $(TARGET) $(LIBPORTABILITY)
cd tests && $(MAKE) clean
#include <sys/endian.h>
#define __BYTE_ORDER _BYTE_ORDER
#define __LITTLE_ENDIAN _LITTLE_ENDIAN
#define __BIG_ENDIAN _BIG_ENDIAN
../linux/file.c
\ No newline at end of file
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/thr.h>
#include <sys/resource.h>
#include "toku_portability.h"
#include "toku_os_types.h"
#include "toku_assert.h"
int
toku_portability_init(void) {
return 0;
}
int
toku_portability_destroy(void) {
return 0;
}
int
toku_os_getpid(void) {
return getpid();
}
int
toku_os_gettid(void) {
long tid;
int r = thr_self(&tid);
assert(r == 0);
return tid;
}
int
toku_os_get_number_processors(void) {
return sysconf(_SC_NPROCESSORS_CONF);
}
int
toku_os_get_number_active_processors(void) {
return sysconf(_SC_NPROCESSORS_ONLN);
}
int
toku_os_get_pagesize(void) {
return sysconf(_SC_PAGESIZE);
}
uint64_t
toku_os_get_phys_memory_size(void) {
uint64_t npages = sysconf(_SC_PHYS_PAGES);
uint64_t pagesize = sysconf(_SC_PAGESIZE);
return npages*pagesize;
}
int
toku_os_get_file_size(int fildes, int64_t *fsize) {
struct stat sbuf;
int r = fstat(fildes, &sbuf);
if (r==0) {
*fsize = sbuf.st_size;
}
return r;
}
int
toku_os_get_unique_file_id(int fildes, struct fileid *id) {
struct stat statbuf;
memset(id, 0, sizeof(*id));
int r=fstat(fildes, &statbuf);
if (r==0) {
memset(id, 0, sizeof(*id));
id->st_dev = statbuf.st_dev;
id->st_ino = statbuf.st_ino;
}
return r;
}
int
toku_os_lock_file(char *name) {
int r;
int fd = open(name, O_RDWR|O_CREAT, S_IRUSR | S_IWUSR);
if (fd>=0) {
r = flock(fd, LOCK_EX | LOCK_NB);
if (r!=0) {
r = errno; //Save errno from flock.
close(fd);
fd = -1; //Disable fd.
errno = r;
}
}
return fd;
}
int
toku_os_unlock_file(int fildes) {
int r = flock(fildes, LOCK_UN);
if (r==0) r = close(fildes);
return r;
}
int
toku_os_mkdir(const char *pathname, mode_t mode) {
int r = mkdir(pathname, mode);
return r;
}
int
toku_os_get_process_times(struct timeval *usertime, struct timeval *kerneltime) {
int r;
struct rusage rusage;
r = getrusage(RUSAGE_SELF, &rusage);
if (r == -1)
return errno;
if (usertime)
*usertime = rusage.ru_utime;
if (kerneltime)
*kerneltime = rusage.ru_stime;
return 0;
}
int
toku_os_initialize_settings(int UU(verbosity)) {
int r = 0;
static int initialized = 0;
assert(initialized==0);
initialized=1;
return r;
}
#if 0
int
toku_os_get_max_rss(int64_t *maxrss) {
char statusname[100];
sprintf(statusname, "/proc/%d/status", getpid());
FILE *f = fopen(statusname, "r");
if (f == NULL)
return errno;
int r = ENOENT;
char line[100];
while (fgets(line, sizeof line, f)) {
r = sscanf(line, "VmHWM:\t%lld kB\n", (long long *) maxrss);
if (r == 1) {
*maxrss *= 1<<10;
r = 0;
break;
}
}
fclose(f);
return r;
}
int
toku_os_get_rss(int64_t *rss) {
char statusname[100];
sprintf(statusname, "/proc/%d/status", getpid());
FILE *f = fopen(statusname, "r");
if (f == NULL)
return errno;
int r = ENOENT;
char line[100];
while (fgets(line, sizeof line, f)) {
r = sscanf(line, "VmRSS:\t%lld kB\n", (long long *) rss);
if (r == 1) {
*rss *= 1<<10;
r = 0;
break;
}
}
fclose(f);
return r;
}
#endif
int
toku_os_is_absolute_name(const char* path) {
return path[0] == '/';
}
int
toku_os_get_max_process_data_size(uint64_t *maxdata) {
int r;
struct rlimit rlimit;
r = getrlimit(RLIMIT_DATA, &rlimit);
if (r == 0) {
uint64_t d;
d = rlimit.rlim_max;
// with the "right" macros defined, the rlimit is a 64 bit number on a
// 32 bit system. getrlimit returns 2**64-1 which is clearly wrong.
// for 32 bit processes, we assume that 1/2 of the address space is
// used for mapping the kernel. this may be pessimistic.
if (sizeof (void *) == 4 && d > (1ULL << 31))
d = 1ULL << 31;
*maxdata = d;
} else
r = errno;
return r;
}
int
toku_stat(const char *name, toku_struct_stat *buf) {
int r = stat(name, buf);
return r;
}
int
toku_fstat(int fd, toku_struct_stat *buf) {
int r = fstat(fd, buf);
return r;
}
#include <stdlib.h>
../linux/memory.c
\ No newline at end of file
../linux/os_malloc.c
\ No newline at end of file
# -*- Mode: Makefile -*-
CPPFLAGS = -D_GNU_SOURCE -DTOKU_ALLOW_DEPRECATED
CPPFLAGS += -I../../toku_include -I.. -I.
CFLAGS = -Wall -Werror -g -O0 -std=c99
LDFLAGS = ../libtokuportability.a -lpthread
SRCS = $(wildcard test-*.c)
TARGETS = $(patsubst %.c,%,$(SRCS))
RUNTARGETS = $(patsubst %,%.tdbrun,$(TARGETS))
VGRIND = valgrind
ifeq ($(CC),icc)
CFLAGS += -diag-disable 981
endif
HERE=linux/tests
ifeq ($(SUMMARIZE),1)
SUMMARIZE_CMD = ;if test $$? = 0; then printf "%-60sPASS\n" $(HERE)/$@; else printf "%-60sFAIL\n" $(HERE)/$@ ; test 0 = 1; fi
SUMMARIZE_SHOULD_FAIL= ;if test $$? = 0; then printf "%-60sXFAIL\n" $(HERE)/$@; else printf "%-60sXPASS\n" $(HERE)/$@ ; test 0 = 1; fi
INVERTER=;test $$? -ne 0
else
SUMMARIZE_CMD =
endif
all: $(TARGETS)
%: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $< $(LDFLAGS)
test-gettime: test-gettime.c
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $< $(LDFLAGS) -lrt
.PHONY: check
check: $(TARGETS) $(RUNTARGETS);
%.tdbrun: %
ifeq ($(VGRIND),)
./$< $(SUMMARIZE_CMD)
else
$(VGRIND) --log-file=$<.valgrind ./$<; \
if [ $$? = 0 ] ; then \
grep "LEAK SUMMARY" $<.valgrind >/dev/null 2>&1; \
if [ $$? = 0 ] ; then cat $<.valgrind; test 0 = 1; fi \
fi \
$(SUMMARIZE_CMD)
endif
clean:
rm -rf $(TARGETS) *.valgrind pwrite4g.data testdir
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#if defined(__linux__)
#include <bits/wordsize.h>
#endif
#include "toku_os.h"
int main(int argc, char *argv[]) {
int verbose = 0;
int i;
for (i=1; i<argc; i++) {
if (strcmp(argv[i], "-v") == 0) {
verbose = 1;
continue;
}
if (strcmp(argv[i], "-q") == 0) {
verbose = 0;
continue;
}
}
// get the data size
uint64_t maxdata;
int r = toku_os_get_max_process_data_size(&maxdata);
assert(r == 0);
if (verbose) printf("maxdata=%"PRIu64"\n", maxdata);
// check the data size
#if defined(__linux__)
#if __WORDSIZE == 64
assert(maxdata > (1ULL << 32));
#elif __WORDSIZE == 32
assert(maxdata < (1ULL << 32));
#else
#error
#endif
#else
assert(maxdata > (1ULL << 32));
#endif
return 0;
}
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <assert.h>
#include "toku_os.h"
int main(void) {
assert(toku_os_get_pagesize() == getpagesize());
return 0;
}
/* Verify that toku_os_pwrite does the right thing when writing beyond 4GB. */
#include <fcntl.h>
#include <test.h>
#include <assert.h>
int test_main(int argc, char *argv[]) {
char fname[] = "pwrite4g.data";
int r;
unlink(fname);
int fd = open(fname, O_RDWR | O_CREAT | O_BINARY, S_IRWXU|S_IRWXG|S_IRWXO);
assert(fd>=0);
char buf[] = "hello";
int64_t offset = (1LL<<32) + 100;
r = toku_os_pwrite(fd, buf, sizeof(buf), offset);
assert(r==sizeof(buf));
int64_t fsize;
r = toku_os_get_file_size(fd, &fsize);
assert(r == 0);
assert(fsize > 100 + (signed)sizeof(buf));
r = close(fd);
assert(r==0);
return 0;
}
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <assert.h>
#include <sys/thr.h>
#include "toku_os.h"
static int gettid(void) {
long tid; int r = thr_self(&tid); assert(r == 0);
return tid;
}
int main(void) {
assert(toku_os_getpid() == getpid());
assert(toku_os_gettid() == gettid());
return 0;
}
#include <toku_portability.h>
int test_main(int argc, char *argv[]);
int
main(int argc, char *argv[]) {
toku_portability_init();
int r = test_main(argc, argv);
toku_portability_destroy();
return r;
}
../linux/toku_assert.c
\ No newline at end of file
../linux/toku_htonl.h
\ No newline at end of file
../linux/toku_os_types.h
\ No newline at end of file
#define _GNU_SOURCE 1
#include <toku_pthread.h>
int toku_pthread_yield(void) {
pthread_yield();
return 0;
}
../linux/toku_pthread.h
\ No newline at end of file
../linux/toku_time.h
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment