Skip to content

Commit

Permalink
add lab1
Browse files Browse the repository at this point in the history
  • Loading branch information
zhf committed Nov 22, 2012
1 parent 2b1e465 commit 018fdad
Show file tree
Hide file tree
Showing 28 changed files with 4,626 additions and 0 deletions.
137 changes: 137 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
LAB=1
SOL=0
RPC=./rpc
LAB2GE=$(shell expr $(LAB) \>\= 2)
LAB3GE=$(shell expr $(LAB) \>\= 3)
LAB4GE=$(shell expr $(LAB) \>\= 4)
LAB5GE=$(shell expr $(LAB) \>\= 5)
LAB6GE=$(shell expr $(LAB) \>\= 6)
LAB7GE=$(shell expr $(LAB) \>\= 7)
CXXFLAGS = -g -MMD -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
LDLIBS = -lpthread
ifeq ($(LAB2GE),1)
ifeq ($(shell uname -s),Darwin)
ifeq ($(shell sw_vers -productVersion | sed -e "s/.*\(10\.[0-9]\).*/\1/"),10.6)
LDLIBS += -lfuse_ino64
else
LDLIBS += -lfuse
endif
else
LDLIBS += -lfuse
endif
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: lab$(LAB)
lab1: rpc/rpctest lock_server lock_tester lock_demo
lab2: rpc/rpctest lock_server lock_tester lock_demo yfs_client extent_server
lab3: yfs_client extent_server lock_server test-lab-3-b test-lab-3-c
lab4: yfs_client extent_server lock_server lock_tester test-lab-3-b\
test-lab-3-c
lab5: yfs_client extent_server lock_server test-lab-3-b test-lab-3-c
lab6: lock_server rsm_tester
lab7: lock_tester lock_server rsm_tester

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 lang/verify.h \
lang/algorithm.h
hfiles2=yfs_client.h extent_client.h extent_protocol.h extent_server.h
hfiles3=lock_client_cache.h lock_server_cache.h handle.h tprintf.h
hfiles4=log.h rsm.h rsm_protocol.h config.h paxos.h paxos_protocol.h rsm_state_transfer.h rsmtest_client.h tprintf.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 ($(LAB4GE),1)
lock_tester += lock_client_cache.cc
endif
ifeq ($(LAB7GE),1)
lock_tester+=rsm_client.cc handle.cc lock_client_cache_rsm.cc
endif
lock_tester : $(patsubst %.cc,%.o,$(lock_tester)) rpc/librpc.a

lock_server=lock_server.cc lock_smain.cc
ifeq ($(LAB4GE),1)
lock_server+=lock_server_cache.cc handle.cc
endif
ifeq ($(LAB6GE),1)
lock_server+= $(rsm_files)
endif
ifeq ($(LAB7GE),1)
lock_server+= lock_server_cache_rsm.cc
endif

lock_server : $(patsubst %.cc,%.o,$(lock_server)) rpc/librpc.a

yfs_client=yfs_client.cc extent_client.cc fuse.cc
ifeq ($(LAB3GE),1)
yfs_client += lock_client.cc
endif
ifeq ($(LAB7GE),1)
yfs_client += rsm_client.cc lock_client_cache_rsm.cc
endif
ifeq ($(LAB4GE),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-3-b=test-lab-3-b.c
test-lab-3-b: $(patsubst %.c,%.o,$(test_lab_4-b)) rpc/librpc.a

test-lab-3-c=test-lab-3-c.c
test-lab-4-c: $(patsubst %.c,%.o,$(test_lab_4-c)) rpc/librpc.a

rsm_tester=rsm_tester.cc rsmtest_client.cc
rsm_tester: $(patsubst %.cc,%.o,$(rsm_tester)) rpc/librpc.a

%.o: %.cc
$(CXX) $(CXXFLAGS) -c $< -o $@

fuse.o: fuse.cc
$(CXX) -c $(CXXFLAGS) $(FUSEFLAGS) $(MACFLAGS) $<

# mklab.inc is needed by 6.824 staff only. Just ignore it.
-include mklab.inc

-include *.d
-include rpc/*.d

clean_files=rpc/rpctest rpc/*.o rpc/*.d rpc/librpc.a *.o *.d yfs_client extent_server lock_server lock_tester lock_demo rpctest test-lab-3-b test-lab-3-c rsm_tester
.PHONY: clean handin
clean:
rm $(clean_files) -rf

handin_ignore=$(clean_files) core* *log
handin_file=$(shell whoami)-lab$(LAB).tgz
labdir=$(shell basename $(PWD))
handin:
@if test -f stop.sh; then ./stop.sh > /dev/null 2>&1 | echo ""; fi
@bash -c "cd ../; tar -X <(tr ' ' '\n' < <(echo '$(handin_ignore)')) -czvf $(handin_file) $(labdir); mv $(handin_file) $(labdir); cd $(labdir)"
@echo Please email $(handin_file) to [email protected]
@echo Thanks!
139 changes: 139 additions & 0 deletions gettime.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* 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__
15 changes: 15 additions & 0 deletions gettime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#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
18 changes: 18 additions & 0 deletions lang/algorithm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// compile time version of min and max

#ifndef algorithm_h
#define algorithm_h

template <int A, int B>
struct static_max
{
static const int value = A > B ? A : B;
};

template <int A, int B>
struct static_min
{
static const int value = A < B ? A : B;
};

#endif
15 changes: 15 additions & 0 deletions lang/verify.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// safe assertions.

#ifndef verify_client_h
#define verify_client_h

#include <stdlib.h>
#include <assert.h>

#ifdef NDEBUG
#define VERIFY(expr) do { if (!(expr)) abort(); } while (0)
#else
#define VERIFY(expr) assert(expr)
#endif

#endif
39 changes: 39 additions & 0 deletions lock_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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;
lock_protocol::status ret = cl->call(lock_protocol::stat, cl->id(), lid, r);
VERIFY (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)
{
}

24 changes: 24 additions & 0 deletions lock_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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
Loading

0 comments on commit 018fdad

Please sign in to comment.