Commit 820edd8f authored by Anjo Vahldiek's avatar Anjo Vahldiek
Browse files

blub

parents
LAB=1
SOL=1
RPC=./rpc
LAB2GE=$(shell expr $(LAB) \>\= 2)
LAB4GE=$(shell expr $(LAB) \>\= 4)
LAB5GE=$(shell expr $(LAB) \>\= 5)
LAB6GE=$(shell expr $(LAB) \>\= 6)
LAB7GE=$(shell expr $(LAB) \>\= 7)
LAB8GE=$(shell expr $(LAB) \>\= 8)
CXXFLAGS = -g -MD -Wall -I. -I$(RPC) -DLAB=$(LAB) -DSOL=$(SOL) -D_FILE_OFFSET_BITS=64
FUSEFLAGS= -D_FILE_OFFSET_BITS=64 -DFUSE_USE_VERSION=25 -I/usr/local/include/fuse -I/usr/include/fuse
ifeq ($(shell uname -s),Darwin)
MACFLAGS= -D__FreeBSD__=10
else
MACFLAGS=
endif
LDFLAGS = -L. -L/usr/local/lib
ifeq ($(LAB2GE),1)
LDLIBS = -lfuse -lpthread
else
LDLIBS = -lpthread
endif
LDLIBS += $(shell test -f `gcc -print-file-name=librt.so` && echo -lrt)
LDLIBS += $(shell test -f `gcc -print-file-name=libdl.so` && echo -ldl)
CC = g++
CXX = g++
lab: lab1
lab1: rpc/rpctest lock_server lock_tester lock_demo
lab2: yfs_client extent_server
lab3: yfs_client extent_server
lab4: yfs_client extent_server lock_server test-lab-4-b test-lab-4-c
lab5: yfs_client extent_server lock_server lock_tester test-lab-4-b\
test-lab-4-c
lab6: yfs_client extent_server lock_server test-lab-4-b test-lab-4-c
lab7: lock_server
lab8: lock_tester lock_server
hfiles1=rpc/fifo.h rpc/connection.h rpc/rpc.h rpc/marshall.h rpc/method_thread.h\
rpc/thr_pool.h rpc/pollmgr.h rpc/jsl_log.h rpc/slock.h rpc/rpctest.cc\
lock_protocol.h lock_server.h lock_client.h gettime.h gettime.cc
hfiles2=yfs_client.h extent_client.h extent_protocol.h extent_server.h
hfiles3=lock_client_cache.h lock_server_cache.h
hfiles4=log.h rsm.h rsm_protocol.h config.h paxos.h paxos_protocol.h rsm_state_transfer.h handle.h
hfiles5=rsm_state_transfer.h rsm_client.h
rsm_files = rsm.cc paxos.cc config.cc log.cc handle.cc
rpclib=rpc/rpc.cc rpc/connection.cc rpc/pollmgr.cc rpc/thr_pool.cc rpc/jsl_log.cc gettime.cc
rpc/librpc.a: $(patsubst %.cc,%.o,$(rpclib))
rm -f $@
ar cq $@ $^
ranlib rpc/librpc.a
rpc/rpctest=rpc/rpctest.cc
rpc/rpctest: $(patsubst %.cc,%.o,$(rpctest)) rpc/librpc.a
lock_demo=lock_demo.cc lock_client.cc
lock_demo : $(patsubst %.cc,%.o,$(lock_demo)) rpc/librpc.a
lock_tester=lock_tester.cc lock_client.cc
ifeq ($(LAB5GE),1)
lock_tester += lock_client_cache.cc
endif
ifeq ($(LAB8GE),1)
lock_tester+=rsm_client.cc
endif
lock_tester : $(patsubst %.cc,%.o,$(lock_tester)) rpc/librpc.a
lock_server=lock_server.cc lock_smain.cc
ifeq ($(LAB5GE),1)
lock_server+=lock_server_cache.cc
endif
ifeq ($(LAB7GE),1)
lock_server+= $(rsm_files)
endif
lock_server : $(patsubst %.cc,%.o,$(lock_server)) rpc/librpc.a
yfs_client=yfs_client.cc extent_client.cc fuse.cc
ifeq ($(LAB4GE),1)
yfs_client += lock_client.cc
endif
ifeq ($(LAB8GE),1)
yfs_client += rsm_client.cc
endif
ifeq ($(LAB5GE),1)
yfs_client += lock_client_cache.cc
endif
yfs_client : $(patsubst %.cc,%.o,$(yfs_client)) rpc/librpc.a
extent_server=extent_server.cc extent_smain.cc
extent_server : $(patsubst %.cc,%.o,$(extent_server)) rpc/librpc.a
test-lab-4-b=test-lab-4-b.c
test-lab-4-b: $(patsubst %.c,%.o,$(test_lab_4-b)) rpc/librpc.a
test-lab-4-c=test-lab-4-c.c
test-lab-4-c: $(patsubst %.c,%.o,$(test_lab_4-c)) rpc/librpc.a
%.o: %.cc
$(CXX) $(CXXFLAGS) -c $< -o $@
fuse.o: fuse.cc
$(CXX) -c $(CXXFLAGS) $(FUSEFLAGS) $(MACFLAGS) $<
l1:
./mklab.pl 1 0 l1 GNUmakefile $(rpclib) $(rpctest) $(lock_server)\
$(lock_demo) $(lock_tester) $(hfiles1)
l1-sol:
./mklab.pl 1 1 l1-sol GNUmakefile $(rpclib) $(rpctest) $(lock_server)\
$(lock_demo) $(lock_tester) $(hfiles1)
l2:
./mklab.pl 2 0 l2 GNUmakefile $(yfs_client) $(extent_server) start.sh\
stop.sh test-lab-2.pl mkfs.sh $(hfiles2)
l2-sol:
./mklab.pl 2 2 l2-sol GNUmakefile $(yfs_client) $(extent_server) $(rpclib) $(lock_server) start.sh\
stop.sh test-lab-2.pl mkfs.sh $(hfiles2) $(hfiles1)
l3:
./mklab.pl 3 0 l3 GNUmakefile $(yfs_client) $(extent_server) start.sh\
stop.sh test-lab-2.pl mkfs.sh $(hfiles2) test-lab-3.pl
l3-sol:
./mklab.pl 3 3 l3-sol GNUmakefile $(yfs_client) $(extent_server) $(rpclib) $(lock_server) start.sh\
stop.sh test-lab-2.pl mkfs.sh $(hfiles2) $(hfiles1) test-lab-3.pl
l4:
./mklab.pl 4 0 l4 GNUmakefile test-lab-4-a.pl $(yfs_client)\
$(extent_server) start.sh stop.sh test-lab-2.pl mkfs.sh\
$(hfiles2) $(test-lab-4-b) $(test-lab-4-c)
l4-sol:
./mklab.pl 4 4 l4-sol GNUmakefile test-lab-4-a.pl $(yfs_client) $(rpclib) $(rpctest) $(lock_server)\
$(extent_server) start.sh stop.sh test-lab-2.pl mkfs.sh\
$(lock_demo) $(lock_tester) $(hfiles1) $(hfiles2) $(test-lab-4-b) $(test-lab-4-c)
l5:
./mklab.pl 5 0 l5 GNUmakefile test-lab-4-a.pl $(yfs_client)\
$(extent_server) $(lock_server) start.sh stop.sh test-lab-2.pl \
mkfs.sh $(hfiles1) $(hfiles2) $(hfiles3) $(lock_tester) \
$(test-lab-4-b) $(test-lab-4-c)
l5-sol:
./mklab.pl 5 5 l5-sol GNUmakefile test-lab-4-a.pl $(yfs_client) $(rpclib) $(rpctest) $(lock_server)\
$(extent_server) $(lock_server) start.sh stop.sh test-lab-2.pl \
mkfs.sh $(hfiles1) $(hfiles2) $(hfiles3) $(lock_tester) \
$(test-lab-4-b) $(test-lab-4-c)
l6:
./mklab.pl 6 0 l6 GNUmakefile test-lab-4-a.pl $(yfs_client)\
$(extent_server) start.sh stop.sh test-lab-2.pl mkfs.sh\
$(hfiles2) $(test-lab-4-b) $(test-lab-4-c)
l6-sol:
./mklab.pl 6 6 l6-sol GNUmakefile test-lab-4-a.pl $(yfs_client) $(rpclib) $(rpctest) $(lock_server)\
$(extent_server) $(lock_server) start.sh stop.sh test-lab-2.pl mkfs.sh\
$(hfiles1) $(hfiles2) $(hfiles3) $(lock_tester) $(test-lab-4-b) $(test-lab-4-c)
l7:
./mklab.pl 7 0 l7 GNUmakefile rsm_tester.pl $(rsm_files) $(hfiles4) lock_smain.cc
l7-sol:
./mklab.pl 7 7 l7-sol GNUmakefile test-lab-4-a.pl $(yfs_client) $(rpclib) $(rpctest) $(lock_server) \
$(extent_server) $(lock_server) start.sh stop.sh test-lab-2.pl mkfs.sh\
$(hfiles1) $(hfiles2) $(hfiles3) $(lock_tester) $(test-lab-4-b) $(test-lab-4-c)\
rsm_tester.pl $(rsm_files) $(hfiles4)
l8:
./mklab.pl 8 0 l8 GNUmakefile rsm_client.cc $(hfiles5) rsm.cc rsm.h
l8-sol:
./mklab.pl 8 8 l8-sol GNUmakefile test-lab-4-a.pl $(yfs_client) $(rpclib) $(rpctest) $(lock_server) \
$(extent_server) $(lock_server) start.sh stop.sh test-lab-2.pl mkfs.sh\
$(hfiles1) $(hfiles2) $(hfiles3) $(lock_tester) $(test-lab-4-b) $(test-lab-4-c)\
rsm_tester.pl $(rsm_files) $(hfiles4) rsm_client.cc $(hfiles5)
-include *.d
.PHONY : clean
clean :
rm -rf rpc/rpctest rpc/*.o rpc/*.d rpc/librpc.a *.o *.d yfs_client extent_server lock_server lock_tester lock_demo rpctest test-lab-4-b test-lab-4-c
/*
* Copyright (c), MM Weiss
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the MM Weiss 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 OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* clock_gettime_stub.c
* gcc -Wall -c clock_gettime_stub.c
* posix realtime functions; MacOS user space glue
*/
/* @comment
* other possible implementation using intel builtin rdtsc
* rdtsc-workaround: http://www.mcs.anl.gov/~kazutomo/rdtsc.html
*
* we could get the ticks by doing this
*
* __asm __volatile("mov %%ebx, %%esi\n\t"
* "cpuid\n\t"
* "xchg %%esi, %%ebx\n\t"
* "rdtsc"
* : "=a" (a),
* "=d" (d)
* );
* we could even replace our tricky sched_yield call by assembly code to get a better accurency,
* anyway the following C stub will satisfy 99% of apps using posix clock_gettime call,
* moreover, the setter version (clock_settime) could be easly written using mach primitives:
* http://www.opensource.apple.com/source/xnu/xnu-${VERSION}/osfmk/man/ (clock_[set|get]_time)
*
* hackers don't be crackers, don't you use a flush toilet?
*
*
* @see draft: ./posix-realtime-stub/posix-realtime-stub.c
*
*/
#ifdef __APPLE__
#pragma weak clock_gettime
#include <sys/time.h>
#include <sys/resource.h>
#include <mach/mach.h>
#include <mach/clock.h>
#include <mach/mach_time.h>
#include <errno.h>
#include <unistd.h>
#include <sched.h>
typedef enum {
CLOCK_REALTIME,
CLOCK_MONOTONIC,
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID
} clockid_t;
static mach_timebase_info_data_t __clock_gettime_inf;
int clock_gettime(clockid_t clk_id, struct timespec *tp) {
kern_return_t ret;
clock_serv_t clk;
clock_id_t clk_serv_id;
mach_timespec_t tm;
uint64_t start, end, delta, nano;
task_basic_info_data_t tinfo;
task_thread_times_info_data_t ttinfo;
mach_msg_type_number_t tflag;
int retval = -1;
switch (clk_id) {
case CLOCK_REALTIME:
case CLOCK_MONOTONIC:
clk_serv_id = clk_id == CLOCK_REALTIME ? CALENDAR_CLOCK : SYSTEM_CLOCK;
if (KERN_SUCCESS == (ret = host_get_clock_service(mach_host_self(), clk_serv_id, &clk))) {
if (KERN_SUCCESS == (ret = clock_get_time(clk, &tm))) {
tp->tv_sec = tm.tv_sec;
tp->tv_nsec = tm.tv_nsec;
retval = 0;
}
}
if (KERN_SUCCESS != ret) {
errno = EINVAL;
retval = -1;
}
break;
case CLOCK_PROCESS_CPUTIME_ID:
case CLOCK_THREAD_CPUTIME_ID:
start = mach_absolute_time();
if (clk_id == CLOCK_PROCESS_CPUTIME_ID) {
getpid();
} else {
sched_yield();
}
end = mach_absolute_time();
delta = end - start;
if (0 == __clock_gettime_inf.denom) {
mach_timebase_info(&__clock_gettime_inf);
}
nano = delta * __clock_gettime_inf.numer / __clock_gettime_inf.denom;
tp->tv_sec = nano * 1e-9;
tp->tv_nsec = nano - (tp->tv_sec * 1e9);
retval = 0;
break;
default:
errno = EINVAL;
retval = -1;
}
return retval;
}
#endif // __APPLE__
#ifndef gettime_h
#define gettime_h
#ifdef __APPLE__
typedef enum {
CLOCK_REALTIME,
CLOCK_MONOTONIC,
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID
} clockid_t;
int clock_gettime(clockid_t clk_id, struct timespec *tp);
#endif
#endif
// RPC stubs for clients to talk to lock_server
#include "lock_client.h"
#include "rpc.h"
#include <arpa/inet.h>
#include <sstream>
#include <iostream>
#include <stdio.h>
lock_client::lock_client(std::string dst)
{
sockaddr_in dstsock;
make_sockaddr(dst.c_str(), &dstsock);
cl = new rpcc(dstsock);
if (cl->bind() < 0) {
printf("lock_client: call bind\n");
}
}
int
lock_client::stat(lock_protocol::lockid_t lid)
{
int r;
int ret = cl->call(lock_protocol::stat, cl->id(), lid, r);
assert (ret == lock_protocol::OK);
return r;
}
lock_protocol::status
lock_client::acquire(lock_protocol::lockid_t lid)
{
}
lock_protocol::status
lock_client::release(lock_protocol::lockid_t lid)
{
}
// lock client interface.
#ifndef lock_client_h
#define lock_client_h
#include <string>
#include "lock_protocol.h"
#include "rpc.h"
#include <vector>
// Client interface to the lock server
class lock_client {
protected:
rpcc *cl;
public:
lock_client(std::string d);
virtual ~lock_client() {};
virtual lock_protocol::status acquire(lock_protocol::lockid_t);
virtual lock_protocol::status release(lock_protocol::lockid_t);
virtual lock_protocol::status stat(lock_protocol::lockid_t);
};
#endif
//
// Lock demo
//
#include "lock_protocol.h"
#include "lock_client.h"
#include "rpc.h"
#include <arpa/inet.h>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
std::string dst;
lock_client *lc;
int
main(int argc, char *argv[])
{
int r;
if(argc != 2){
fprintf(stderr, "Usage: %s [host:]port\n", argv[0]);
exit(1);
}
dst = argv[1];
lc = new lock_client(dst);
r = lc->stat(1);
printf ("stat returned %d\n", r);
}
// lock protocol
#ifndef lock_protocol_h
#define lock_protocol_h
#include "rpc.h"
class lock_protocol {
public:
enum xxstatus { OK, RETRY, RPCERR, NOENT, IOERR };
typedef int status;
typedef unsigned long long lockid_t;
enum rpc_numbers {
acquire = 0x7001,
release,
subscribe, // for lab 5
stat
};
};
#endif
// the lock server implementation
#include "lock_server.h"
#include <sstream>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
lock_server::lock_server():
nacquire (0)
{
}
lock_protocol::status
lock_server::stat(int clt, lock_protocol::lockid_t lid, int &r)
{
lock_protocol::status ret = lock_protocol::OK;
printf("stat request from clt %d\n", clt);
r = nacquire;
return ret;
}
// this is the lock server
// the lock client has a similar interface
#ifndef lock_server_h
#define lock_server_h
#include <string>
#include "lock_protocol.h"
#include "lock_client.h"
#include "rpc.h"
class lock_server {
protected:
int nacquire;
public:
lock_server();
~lock_server() {};
lock_protocol::status stat(int clt, lock_protocol::lockid_t lid, int &);
};
#endif
#include "rpc.h"
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include "lock_server.h"
#include "jsl_log.h"
// Main loop of lock_server
int
main(int argc, char *argv[])
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
srandom(getpid());
if(argc != 2){
fprintf(stderr, "Usage: %s port\n", argv[0]);
exit(1);
}
//jsl_set_debug(2);
#ifndef RSM
lock_server ls;
rpcs server(atoi(argv[1]));
server.reg(lock_protocol::stat, &ls, &lock_server::stat);
#endif
while(1)
sleep(1000);
}
//
// Lock server tester
//
#include "lock_protocol.h"
#include "lock_client.h"
#include "rpc.h"
#include "jsl_log.h"
#include <arpa/inet.h>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
// must be >= 2
int nt = 10; //XXX: lab1's rpc handlers are blocking. Since rpcs uses a thread pool of 10 threads, we cannot test more than 10 blocking rpc.
std::string dst;
lock_client **lc = new lock_client * [nt];
lock_protocol::lockid_t a = 1;
lock_protocol::lockid_t b = 2;
lock_protocol::lockid_t c = 3;
// check_grant() and check_release() check that the lock server
// doesn't grant the same lock to both clients.
// it assumes that lock names are distinct in the first byte.
int ct[256];
pthread_mutex_t count_mutex;
void
check_grant(lock_protocol::lockid_t lid)
{
pthread_mutex_lock(&count_mutex);
int x = lid & 0xff;
if(ct[x] != 0){
fprintf(stderr, "error: server granted %016llx twice\n", lid);
fprintf(stdout, "error: server granted %016llx twice\n", lid);
exit(1);
}
ct[x] += 1;
pthread_mutex_unlock(&count_mutex);
}
void
check_release(lock_protocol::lockid_t lid)
{
pthread_mutex_lock(&count_mutex);
int x = lid & 0xff;
if(ct[x] != 1){
fprintf(stderr, "error: client released un-held lock %016llx\n", lid);
exit(1);
}
ct[x] -= 1;
pthread_mutex_unlock(&count_mutex);
}
void
test1(void)
{
printf ("acquire a release a acquire a release a\n");
lc[0]->acquire(a);
check_grant(a);
lc[0]->release(a);
check_release(a);
lc[0]->acquire(a);
check_grant(a);
lc[0]->release(a);
check_release(a);
printf ("acquire a acquire b release b release a\n");
lc[0]->acquire(a);
check_grant(a);
lc[0]->acquire(b);
check_grant(b);
lc[0]->release(b);
check_release(b);
lc[0]->release(a);
check_release(a);
}
void *
test2(void *x)
{
int i = * (int *) x;
printf ("test2: client %d acquire a release a\n", i);
lc[i]->acquire(a);
printf ("test2: client %d acquire done\n", i);