From 4c7f964f84820c2de93782c86fd67ff3d3fd19a1 Mon Sep 17 00:00:00 2001 From: kcwu Date: Sun, 12 Mar 2006 16:07:51 +0000 Subject: rewrite core function of tumpserver, maintaining friend relation 300 timers faster. git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@3286 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- cacheserver/Makefile | 13 +- cacheserver/friend.cpp | 352 ++++++++++++++++++++++++++++++++++++++++++++++ cacheserver/utmpserver2.c | 246 ++++++++++++++++++++++++++++++++ 3 files changed, 607 insertions(+), 4 deletions(-) create mode 100644 cacheserver/friend.cpp create mode 100644 cacheserver/utmpserver2.c diff --git a/cacheserver/Makefile b/cacheserver/Makefile index e09edcca..0df01e29 100644 --- a/cacheserver/Makefile +++ b/cacheserver/Makefile @@ -1,19 +1,24 @@ # $Id$ .include "../pttbbs.mk" -PROGRAMS= utmpserver utmpsync +PROGRAMS= utmpserver utmpsync utmpserver2 UTILOBJ= ../util/util_stuff.o ../util/util_var.o ../util/util_file.o ../util/util_cache.o ../util/util_passwd.o ../util/util_record.o all: ${PROGRAMS} -.SUFFIXES: .c .o -.c.o: +.SUFFIXES: .c .cpp .o +.c.o: $(CCACHE) $(CC) $(CFLAGS) -c $*.c +.cpp.o: + $(CCACHE) $(CXX) $(CFLAGS) -c $*.cpp utmpserver: utmpserver.o $(UTILOBJ) ${CC} ${CFLAGS} ${LDFLAGS} -o $* $*.o $(UTILOBJ) +utmpserver2: utmpserver2.o friend.o $(UTILOBJ) + ${CXX} ${CFLAGS} ${LDFLAGS} -o $* $*.o $(UTILOBJ) friend.o utmpsync: utmpsync.o $(UTILOBJ) ${CC} ${CFLAGS} ${LDFLAGS} -o $* $*.o $(UTILOBJ) + clean: - rm -f *~ ${PROGRAMS} + rm -f *~ ${PROGRAMS} friend.o utmpserver.o utmpserver2.o utmpsync.o diff --git a/cacheserver/friend.cpp b/cacheserver/friend.cpp new file mode 100644 index 00000000..4f8e9663 --- /dev/null +++ b/cacheserver/friend.cpp @@ -0,0 +1,352 @@ +#define NDEBUG +#include +#include + +// for some constant and type +#include "bbs.h" + +/* for each login of user, + * input: my index, friend[MAX_FRIEND] of uid, reject[MAX_REJECT] of uid, + * for all my relation, + * output: his index, his uid, relation to me, relation to him + */ +/* 除了 user 及 utmp 之外, 全部的 ref index 都是雙向的, 確保 insert & delete O(1) */ +/* 當沒有人 refer 時則 resource recycle */ + +typedef int Uid; +typedef int Idx; + + +struct Relation { + Uid him; + short him_offset; + + Relation(Uid _him, short _him_offset=-1) :him(_him),him_offset(_him_offset) {} +}; + +template +struct freelist { + static const int KEEP = 64; + static T* list[8][KEEP]; // 2^0~2^7 + static int tail[8]; + +#define IS_2xxN(a) (a && (a&(a-1))==0) + static T* alloc(int n) { + assert(n>0); + if(n<256 && IS_2xxN(n) && sizeof(T)*n<65536) { + int t=n; + int slot; + for(slot=0; t>1; t/=2) + slot++; + assert(0<=slot && slot<8); + if(tail[slot]) { + return list[slot][--tail[slot]]; + } + } + return (T*)malloc(sizeof(T)*n); + } + static void free(T* p, int n) { + assert(n>0); + if(n<256 && IS_2xxN(n) && sizeof(T)*n<65536) { + int t=n; + int slot; + for(slot=0; t>1; t/=2) + slot++; + assert(0<=slot && slot<8); + if(tail[slot] T* freelist::list[8][KEEP]; +template int freelist::tail[8]={0}; + +template +struct myvector { + // 大致上想要 STL vector, 但 STL 的 capacity 只會增加不會縮小 + // (後來發現, 以 online friend 來說, capacity 不會縮小其實沒什麼影響) + // 此外, pointer 在 64bit 機器上要 8bytes, basic overhead 8*3 bytes, + // 但我的資料量沒那麼大, 改用 S(int or short) 存 size & capacity 比較省 + T *base; + S room, n; + + myvector() :base(0),room(0),n(0) {} + ~myvector() { + clear(); + } + S append(T data) { + if(room0); + n--; + resizefor(n); + } + void clear() { + n=0; + resizefor(n); + } + /* + T& operator[](int idx) { + return base[idx]; + } + */ + + void resizefor(S size) { + assert(size>=n); + if(size==0) { + if(base) freelist::free(base, room); + base=0; + room=0; + } else { + S origroom=room; + if(room==0) + room=MIN_ROOM; + while(roomsize) room=S(room/2); + if(room!=origroom || base==0) { + //base=(T*)realloc(base, sizeof(T)*room); + T* tmp=freelist::alloc(room); + assert(tmp); + if(n>0) + memcpy(tmp, base, sizeof(T)*n); + if(base!=0) + freelist::free(base, origroom); + base=tmp; + } + assert(base); + } + } +}; + +template +struct RelationList: public myvector { + RelationList() :myvector() {} + void add(Uid me, Uid him) { + assert(me!=him); + RelationList& bl=R::backlist(him); + short me_offset=append(Relation(him)); + short him_offset=bl.append(Relation(me,me_offset)); + + setbackoffset(me_offset,him_offset); + assert(bl.base[him_offset].him==me); + assert(bl.base[him_offset].him_offset==me_offset); + } + void deleteall(Uid me) { + for(int i=0; i& bl=R::backlist(base[i].him); + assert(bl.base[base[i].him_offset].him==me); + assert(bl.base[base[i].him_offset].him_offset==i); + bl.delete_half(base[i].him_offset); + //try_recycle(base[i].him); // dirty + } + clear(); + } + public: + void setbackoffset(short which,short offset) { + assert(0<=which && which::add(Uid me, Uid him); + friend void RelationList::deleteall(Uid me); + friend void RelationList::delete_half(short offset); +}; + +struct Like; +struct Likeby; +struct Hate; +struct Hateby; +struct Like: public Relation { + Like(Uid _him, short _him_offset=-1) :Relation(_him,_him_offset) {} + static RelationList& backlist(Uid him); +}; +struct Likeby: public Relation { + Likeby(Uid _him, short _him_offset=-1) :Relation(_him,_him_offset) {} + static RelationList& backlist(Uid him); +}; +struct Hate: public Relation { + Hate(Uid _him, short _him_offset=-1) :Relation(_him,_him_offset) {} + static RelationList& backlist(Uid him); +}; +struct Hateby: public Relation { + Hateby(Uid _him, short _him_offset=-1) :Relation(_him,_him_offset) {} + static RelationList& backlist(Uid him); +}; + + +struct Utmp { + Utmp() { + for(int i=0; i utmplist; + + RelationList like; + RelationList hate; + RelationList likeby; + RelationList hateby; + + BBSUser() :me(-1),online(0),utmplist(),like(),hate(),likeby(),hateby() {} + BBSUser(Uid uid) :me(uid),online(0),utmplist(),like(),hate(),likeby(),hateby() {} + + void login(int utmpidx, const Uid likehim[MAX_FRIEND], const Uid hatehim[MAX_REJECT]) { + if(online>0) { + /* multiple login 的話, 以最後一次的 like/hate 為準 */ + like.deleteall(me); + hate.deleteall(me); + } + utmp.utmp[utmpidx]=me; + utmplist.append(utmpidx); + online++; + assert(online==utmplist.n); + for(int i=0; i& Like::backlist(Uid him) { return userlist.users[him].likeby; } +RelationList& Likeby::backlist(Uid him) { return userlist.users[him].like; } +RelationList& Hate::backlist(Uid him) { return userlist.users[him].hateby; } +RelationList& Hateby::backlist(Uid him) { return userlist.users[him].hate; } + +struct Result { + Uid who; + int bits; + Result(Uid _who, int _bits) :who(_who),bits(_bits) {} + bool operator<(const Result& b) const { + return who work; + for(int i=0; i0) { + int newn=1; + for(int i=1; i +#include + +#include "bbs.h" + +extern void utmplogin(int uid, int index, const int like[MAX_FRIEND], const int hate[MAX_REJECT]); +extern int genfriendlist(int uid, int index, ocfs_t *fs, int maxfs); +extern void utmplogoutall(void); +#ifdef FAKEDATA +FILE *fp; +#endif +#ifdef UTMPLOG +FILE *logfp; +#endif + + +#ifdef NOFLOODING +#define MAXWAIT 1024 +#define FLUSHTIME (3600*6) + +struct { + time_t lasttime; + int count; +} flooding[MAX_USERS]; + +int nWaits, lastflushtime; +struct { + int uid; + int fd; + int index; +} waitqueue[MAXWAIT]; + +void flushwaitqueue(void) +{ + int i; + for( i = 0 ; i < nWaits ; ++i ) { + processlogin(waitqueue[i].fd, waitqueue[i].uid, waitqueue[i].index); + close(waitqueue[i].fd); + } + lastflushtime = time(NULL); + nWaits = 0; + memset(flooding, 0, sizeof(flooding)); +} +#endif /* NOFLOODING */ + +void syncutmp(int cfd) { + int i; + int like[MAX_FRIEND]; + int hate[MAX_REJECT]; + +#ifdef UTMPLOG + int x=-1; + if(logfp && ftell(logfp)> 500*(1<<20)) { + fclose(logfp); + logfp=NULL; + } + if(logfp) fwrite(&x, sizeof(x), 1, logfp); +#endif + + printf("logout all\n"); + utmplogoutall(); + fprintf(stderr,"sync begin\n"); + for(i=0; i 500*(1<<20)) { + fclose(logfp); + logfp=NULL; + } +#endif + + while( (ch = getopt(argc, argv, "p:i:h")) != -1 ) + switch( ch ){ + case 'p': + port = atoi(optarg); + break; + case 'i': + iface_ip = optarg; + break; + case 'h': + default: + fprintf(stderr, "usage: utmpserver [-i interface_ip] [-p port]\n"); + return 1; + } + +#ifdef FAKEDATA + fp=fopen("utmp.data","rb"); +#else + if( (sfd = tobind(iface_ip, port)) < 0 ) + return 1; +#endif +#ifdef NOFLOODING + lastflushtime = time(NULL); +#endif + while(1) { +#ifdef NOFLOODING + if( lastflushtime < (time(NULL) - 1800) ) + flushwaitqueue(); +#endif + +#ifdef FAKEDATA + if(fread(&cmd, sizeof(cmd), 1, fp)==0) break; +#else + len = sizeof(clientaddr); + if( (cfd = accept(sfd, (struct sockaddr *)&clientaddr, &len)) < 0 ){ + if( errno != EINTR ) + sleep(1); + continue; + } + toread(cfd, &cmd, sizeof(cmd)); +#endif + + if(cmd==-1) { + syncutmp(cfd); +#ifndef FAKEDATA + close(cfd); +#endif + continue; + } + + fail=0; +#ifdef FAKEDATA + fread(&uid, sizeof(uid), 1, fp); + fread(&index, sizeof(index), 1, fp); +#else + index=cmd; // bad protocol + if(toread(cfd, &uid, sizeof(uid)) <= 0) + fail=1; +#endif + if(index>=USHM_SIZE) { + fprintf(stderr, "bad index=%d\n",index); + fail=1; + } + + if(fail) { +#ifndef FAKEDATA + close(cfd); +#endif + continue; + } + +#ifdef NOFLOODING + if( (time(NULL) - flooding[uid].lasttime) < 20 ) + ++flooding[uid].count; + if( flooding[uid].count > 10 ){ + if( nWaits == MAXWAIT ) + flushwaitqueue(); + waitqueue[nWaits].uid = uid; + waitqueue[nWaits].index = index; + waitqueue[nWaits].fd = cfd; + ++nWaits; + + continue; + } + flooding[uid].lasttime = time(NULL); +#endif + + processlogin(cfd, uid, index); +#ifndef FAKEDATA + close(cfd); +#endif + } +#ifdef FAKEDATA + fclose(fp); +#endif + return 0; +} -- cgit v1.2.3